diff --git a/CHANGELOG.md b/CHANGELOG.md index e8074f550..f4e710527 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -148,6 +148,7 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109). - Fix required immediate slots calculation and remove `naga::valid::FunctionInfo::immediate_slots_used`. By @beicause in [#9725](https://github.com/gfx-rs/wgpu/pull/9725). - Fix a spurious assertion failure in `Device::maintain` when multiple threads race polling the same device. By @AdrianEddy in [#9958](https://github.com/gfx-rs/wgpu/pull/9958). - Fix `PendingSubmission` releasing its lock guards out of stacking order, which tripped `--cfg wgpu_validate_locks` on any submission. By @AdrianEddy in [#9960](https://github.com/gfx-rs/wgpu/pull/9960). +- Fix `Buffer::unmap` failing with `NotMapped` when it races a `Buffer::map` running on another thread. By @AdrianEddy in [#9959](https://github.com/gfx-rs/wgpu/pull/9959). - Fix separate depth/stencil read-only state and `SYNC-HAZARD-WRITE-AFTER-WRITE` Vulkan validation error. By @beicause in [#9763](https://github.com/gfx-rs/wgpu/pull/9763). - Fix initialization tracking for some cases of array textures, 3d textures, and depth/stencil textures with divergent usage in a render pass. By @andyleiserson in [#10002](https://github.com/gfx-rs/wgpu/pull/10002) and [#10060](https://github.com/gfx-rs/wgpu/pull/10060). - Fixed a deadlock between `Queue::compact_blas` and `Queue::submit`, which acquired `Device::command_indices` and `Queue::pending_writes` in opposite orders. By @mstampfli in [#10118](https://github.com/gfx-rs/wgpu/pull/10118). diff --git a/tests/tests/wgpu-validation/api/buffer_mapping.rs b/tests/tests/wgpu-validation/api/buffer_mapping.rs index 375b712d3..1331d13cc 100644 --- a/tests/tests/wgpu-validation/api/buffer_mapping.rs +++ b/tests/tests/wgpu-validation/api/buffer_mapping.rs @@ -245,3 +245,132 @@ fn unmap_while_visible() { let _mapping0 = buffer.slice(..).get_mapped_range_mut().unwrap(); buffer.unmap(); } + +/// Regression test for [#9959]: `Buffer::unmap` racing a `Buffer::map` in +/// progress on another thread must never fail with `NotMapped`. +/// +/// The main thread repeatedly requests a mapping and resolves it with `poll`; +/// the unmapper thread issues exactly one `unmap` per iteration, only after +/// that iteration's `map_async` has returned, with a swept spin delay so +/// successive iterations probe different points of the map. Once `map_async` +/// has been issued, the buffer is never observably unmapped until an `unmap` +/// succeeds: the racing `unmap` may abort the pending mapping or unmap the +/// freshly installed one, but it must not fail. Before the fix, `Buffer::map` +/// swapped `map_state` out for the duration of the HAL map, and an `unmap` +/// landing in that window failed with `NotMapped` even though its `map_async` +/// went on to resolve `Ok` — an interleaving no sequential ordering of the two +/// calls permits. +/// +/// At the time of writing, unmodified trunk failed this test on every run, +/// with ~700 spurious failures per 25 000 iterations and the first one inside +/// the first dozen iterations. The full test takes ~0.25 s to 2 s, depending on machine. +/// Failure rate is machine-dependent too, so lowering the iterations risks false negatives. +/// +/// [#9959]: https://github.com/gfx-rs/wgpu/pull/9959 +#[test] +#[cfg(not(target_arch = "wasm32"))] +fn concurrent_mapping_and_unmapping() { + use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; + use std::sync::{Arc, Mutex}; + + const ITERATIONS: u64 = 25_000; + /// Spin-delay sweep period, in multiples of 4 `spin_loop` hints, covering + /// the duration of the HAL map. + const MAX_OFFSET: u64 = 250; + + let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); + + let buffer = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("map/unmap race"), + size: 1024, + usage: wgpu::BufferUsages::MAP_READ, + mapped_at_creation: false, + }); + + // `Buffer::unmap` reports failure through the uncaptured-error handler; a + // hit is a change of this counter across the unmapper's call. + let errors = Arc::new(AtomicUsize::new(0)); + let last_error = Arc::new(Mutex::new(String::new())); + device.on_uncaptured_error(Arc::new({ + let errors = Arc::clone(&errors); + let last_error = Arc::clone(&last_error); + move |error: wgpu::Error| { + *last_error.lock().unwrap() = error.to_string(); + errors.fetch_add(1, Ordering::SeqCst); + } + })); + + // Lockstep protocol: `map_issued = i + 1` publishes that iteration i's + // `map_async` has returned; `unmap_done = i + 1` publishes that the + // unmapper's verdict for iteration i is in `unmap_failed`. + let map_issued = Arc::new(AtomicU64::new(0)); + let unmap_done = Arc::new(AtomicU64::new(0)); + let unmap_failed = Arc::new(AtomicU64::new(0)); + + let unmapper = std::thread::spawn({ + let buffer = buffer.clone(); + let errors = Arc::clone(&errors); + let map_issued = Arc::clone(&map_issued); + let unmap_done = Arc::clone(&unmap_done); + let unmap_failed = Arc::clone(&unmap_failed); + move || { + for i in 0..ITERATIONS { + while map_issued.load(Ordering::SeqCst) != i + 1 { + std::hint::spin_loop(); + } + for _ in 0..(i % MAX_OFFSET) * 4 { + std::hint::spin_loop(); + } + let before = errors.load(Ordering::SeqCst); + buffer.unmap(); + let failed = errors.load(Ordering::SeqCst) != before; + unmap_failed.store(failed as u64, Ordering::SeqCst); + unmap_done.store(i + 1, Ordering::SeqCst); + } + } + }); + + let map_result = Arc::new(Mutex::new(None::>)); + let mut hits = Vec::new(); + for i in 0..ITERATIONS { + map_result.lock().unwrap().take(); + buffer.map_async(wgpu::MapMode::Read, .., { + let map_result = Arc::clone(&map_result); + move |result| { + *map_result.lock().unwrap() = Some(result); + } + }); + map_issued.store(i + 1, Ordering::SeqCst); + device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); + while unmap_done.load(Ordering::SeqCst) != i + 1 { + std::hint::spin_loop(); + } + + if unmap_failed.load(Ordering::SeqCst) != 0 { + let map_ok = matches!(*map_result.lock().unwrap(), Some(Ok(()))); + hits.push(format!( + "iteration {i}: unmap failed ({}) while its map_async {}", + last_error.lock().unwrap(), + if map_ok { + "resolved Ok" + } else { + "did not resolve Ok" + }, + )); + if map_ok { + // The spurious failure left the mapping in place; really unmap + // so the next iteration starts idle. + buffer.unmap(); + } + } + } + unmapper.join().unwrap(); + + assert!( + hits.is_empty(), + "unmap must never fail while racing a map_async it was issued after \ + ({} spurious failures in {ITERATIONS} iterations; first: {})", + hits.len(), + hits[0], + ); +} diff --git a/wgpu-core/src/lock/rank.rs b/wgpu-core/src/lock/rank.rs index eb4b4361a..776a327be 100644 --- a/wgpu-core/src/lock/rank.rs +++ b/wgpu-core/src/lock/rank.rs @@ -169,6 +169,7 @@ define_lock_ranks! { SHARED_TRACKER_INDEX_ALLOCATOR_INNER, } rank BUFFER_MAP_STATE "Buffer::map_state" followed by { + BUFFER_INITIALIZATION_STATUS, DEVICE_TRACE, SHARED_TRACKER_INDEX_ALLOCATOR_INNER, } diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 42338305a..d92f49a23 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -945,18 +945,23 @@ impl Buffer { /// Other errors are returned within `BufferMapPendingClosure`. #[must_use] pub(crate) fn map(&self, snatch_guard: &SnatchGuard) -> Option { - // This _cannot_ be inlined into the match. If it is, the lock will be held - // open through the whole match, resulting in a deadlock when we try to re-lock - // the buffer back to active. - let mapping = mem::replace(&mut *self.map_state.lock(), BufferMapState::Idle); - let pending_mapping = match mapping { + // Hold the lock on `map_state` until we have updated it with the + // outcome of the mapping, to prevent concurrent activity from + // observing an intermediate state. + // + // Unfortunately this does mean that we hold the guard across + // `crate::device::map_buffer` and the associated call to + // `handle_hal_error`, which may invoke a device loss callback. + // See . + let mut map_state = self.map_state.lock(); + let pending_mapping = match mem::replace(&mut *map_state, BufferMapState::Idle) { BufferMapState::Waiting(pending_mapping) => pending_mapping, // Mapping cancelled BufferMapState::Idle => return None, // Mapping queued at least twice by map -> unmap -> map // and was already successfully mapped below - BufferMapState::Active { .. } => { - *self.map_state.lock() = mapping; + mapping @ BufferMapState::Active { .. } => { + *map_state = mapping; return None; } _ => panic!("No pending mapping."), @@ -972,7 +977,7 @@ impl Buffer { snatch_guard, ) { Ok(mapping) => { - *self.map_state.lock() = BufferMapState::Active { + *map_state = BufferMapState::Active { mapping, range: pending_mapping.range.clone(), host, @@ -982,7 +987,7 @@ impl Buffer { Err(e) => Err(e), } } else { - *self.map_state.lock() = BufferMapState::Active { + *map_state = BufferMapState::Active { mapping: hal::BufferMapping { ptr: NonNull::dangling(), is_coherent: true,