From d7fec652decf1fc0f20856f34f77fd741c356493 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 21 Nov 2026 19:14:23 -0800 Subject: [PATCH] :bug: Let a decoder's state go off rt's completion thread at a loss At a device's loss the table walk (fault_walk) upgrades each decoder's Weak on the thread that read the first failed command buffer, rt's completion thread among them. Where the decoder went during the walk, the walk's hold was the last one, and the decoder's objects were dropped there: a drop that waited for a command buffer's completion would hang the thread that completes it. TableWalk::each now hands a last hold to a thread of its own, reims-vgpu.graphics-tables-let-go, through the platform's threads, so a refused spawn ends the process as theirs do. The test drops the decoder in the walk's callback and checks that its state went on that thread, and that a walk of a decoder that outlives it spawns nothing. The host sees only a new library thread name; nothing the guest sees changes. --- crates/reims-vgpu/src/graphics/decode/mod.rs | 105 +++++++++++++++++++ crates/reims-vgpu/src/graphics/fault_walk.rs | 6 +- docs/api.md | 3 +- docs/architecture.md | 4 +- 4 files changed, 114 insertions(+), 4 deletions(-) diff --git a/crates/reims-vgpu/src/graphics/decode/mod.rs b/crates/reims-vgpu/src/graphics/decode/mod.rs index 5cb1e7d83..d389f314e 100644 --- a/crates/reims-vgpu/src/graphics/decode/mod.rs +++ b/crates/reims-vgpu/src/graphics/decode/mod.rs @@ -865,6 +865,14 @@ pub(crate) type EachTableObject<'a, P> = dyn for<'o> FnMut(u32, u64, bool, Table /// A decoder's tables, held weakly, for the walk at its device's loss (`super::fault_walk`). pub(crate) struct TableWalk(Weak>); +/// The thread a decoder's state is let go of on where the walk at its device's loss held it last +/// ([`TableWalk::each`]). +pub(crate) const TABLES_LET_GO: &str = "reims-vgpu.graphics-tables-let-go"; + +/// That thread's stack: a std thread's, as the decoder's workers have, which let go of the same +/// objects. +const TABLES_LET_GO_STACK: usize = 2 << 20; + impl TableWalk

{ /// Whether the decoder's state is still there. pub(crate) fn is_live(&self) -> bool { @@ -876,6 +884,13 @@ impl TableWalk

{ /// `bound` at most in all for the locks: the map of contexts, held through the walk, so no /// context is let go of here, and each table's. Answers the contexts walked, those whose /// table was not had in time, and whether the map was not. + /// + /// The walk holds the decoder's state from its start to its end. Where the decoder went + /// meanwhile, so the walk's hold is the last, the state is let go of on a thread of its own + /// ([`TABLES_LET_GO`]), not on the walk's: that is the thread that reads a lost device's + /// first failed command buffer, rt's completion thread among them, and a drop of the + /// decoder's objects there that waited for a command buffer's completion would hang it. A + /// refused thread ends the process, as the platform's threads do. pub(crate) fn each( &self, bound: std::time::Duration, @@ -884,6 +899,26 @@ impl TableWalk

{ let Some(shared) = self.0.upgrade() else { return (0, Vec::new(), false); }; + let walked = Self::each_of(&shared, bound, f); + if let Some(last) = Arc::into_inner(shared) { + let threads = last.platform.threads().handle(); + reims_vgpu_vulkan::core::rt::spawn_named( + &*threads, + TABLES_LET_GO, + TABLES_LET_GO_STACK, + Box::new(last), + || {}, + ); + } + walked + } + + /// [`Self::each`]'s walk of `shared`. + fn each_of( + shared: &Shared

, + bound: std::time::Duration, + f: &mut EachTableObject<'_, P>, + ) -> (usize, Vec, bool) { let clock = shared.platform.clock(); let start = clock.now_mono().as_nanos(); let left = || { @@ -14347,4 +14382,74 @@ lacks {part}" (RGFX_PIPELINE_REFUSED, RGFX_PIPELINE_KIND_RENDER) ); } + + /// The walk of a decoder's tables at its device's loss lets go of the decoder's state, where + /// the decoder went during the walk and its hold is the last, on a thread of its own, not on + /// the walk's (rt's completion thread at a loss); where the decoder is still there, the walk + /// lets go of nothing and starts no thread + #[test] + fn a_loss_walk_holding_a_decoder_s_state_last_lets_it_go_off_its_own_thread() { + use std::sync::mpsc; + use std::time::Duration; + + /// Sends the name of the thread it is dropped on. + struct Tell(mpsc::Sender>); + impl Drop for Tell { + fn drop(&mut self) { + let _ = self + .0 + .send(std::thread::current().name().map(str::to_owned)); + } + } + + let p = Arc::new(FakePlatform::new()); + let device = p.device(); + let mut dec = GraphicsDecoder::::new( + Arc::clone(&p), + Arc::new(reims_vgpu_graphics::region::Region::new(0x10000).unwrap()), + ); + let buffer = RgfxBufferCreate { + header: RgfxMsgHeader { + r#type: RGFX_CMD_BUFFER_CREATE, + size: size_of::() as u32, + seqno: 0, + }, + obj_id: 1, + gpu_address: 0x4_0000, + length: 65536, + ..RgfxBufferCreate::default() + }; + assert_eq!(dec.decode(&device, buffer.as_bytes()).unwrap(), Ok(())); + dec.end_message(); + // The state's drop drops the hook, which tells the thread it was dropped on. + let (tx, rx) = mpsc::channel(); + let tell = Tell(tx); + dec.set_walk_hook(Some(Arc::new(move |_: usize| { + let _ = &tell; + }))); + let walk = dec.table_walk(); + let spawned = |p: &FakePlatform| { + (p.fake_threads().spawned().into_iter()) + .filter(|&(name, _)| name == TABLES_LET_GO) + .count() + }; + + // The decoder outlives the walk: nothing is let go of, and no thread started. + let mut seen = 0; + let walked = walk.each(Duration::from_secs(5), &mut |_, _, _, _| seen += 1); + assert_eq!((walked, seen), ((1, Vec::new(), false), 1)); + assert_eq!(rx.try_recv(), Err(mpsc::TryRecvError::Empty)); + assert_eq!(spawned(&p), 0); + + // The decoder goes during the walk: the walk's hold is the last, and the state goes on + // the let-go thread once the walk is done. + let mut held = Some(dec); + let walked = walk.each(Duration::from_secs(5), &mut |_, _, _, _| drop(held.take())); + assert_eq!(walked, (1, Vec::new(), false)); + assert!(held.is_none()); + let on = rx.recv_timeout(Duration::from_secs(10)); + assert_eq!(on, Ok(Some(TABLES_LET_GO.to_owned()))); + assert_eq!(spawned(&p), 1); + assert!(!walk.is_live()); + } } diff --git a/crates/reims-vgpu/src/graphics/fault_walk.rs b/crates/reims-vgpu/src/graphics/fault_walk.rs index 893b80549..20681f4f9 100644 --- a/crates/reims-vgpu/src/graphics/fault_walk.rs +++ b/crates/reims-vgpu/src/graphics/fault_walk.rs @@ -2,8 +2,10 @@ //! at its loss to place the driver's fault addresses among their live objects //! (`reims_vgpu_vulkan::rt_platform::fault`'s docs). The walk keeps no record before the loss: it //! reads the tables as they are then, waiting `fault::BOUND` in all at most for their locks, and -//! names the contexts whose tables it did not have in time. A device of another platform is told -//! nothing. +//! names the contexts whose tables it did not have in time. Where the decoder goes during the +//! walk, so the walk holds its state last, the state is let go of on a thread of its own, not on +//! the walk's, which may be rt's completion thread (`decode::TableWalk::each`). A device of +//! another platform is told nothing. //! //! As a table stores a buffer, texture or heap, the object is told the guest's id for it //! ([`name`]), which the device's history of released ranges and its lines of the command buffers diff --git a/docs/api.md b/docs/api.md index 3ff5153fb..8594457cc 100644 --- a/docs/api.md +++ b/docs/api.md @@ -397,7 +397,8 @@ returns. The library logs through the `log` crate; a Rust host installs its own Threads are named `reims-vgpu.`: those above, `reims-vgpu.display` (the display thread), `reims-vgpu.window` (a window's drawing thread), `reims-vgpu.ui` (on Linux and Windows, the thread every window's events and callbacks run on), and `reims-vgpu.graphics-video-`, -`reims-vgpu.graphics-let-go-` and `reims-vgpu.graphics-event-waits` (a graphics device's). The names are diagnostics, not contract. +`reims-vgpu.graphics-let-go-`, `reims-vgpu.graphics-event-waits` and +`reims-vgpu.graphics-tables-let-go` (a graphics device's). The names are diagnostics, not contract. A queue's worker, the display thread among them, has core's `WORKER_STACK`, 512 KiB. ### The locks around `raise_interrupt` diff --git a/docs/architecture.md b/docs/architecture.md index 6d3ac2197..25225a666 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -496,7 +496,9 @@ so a block that takes a recursive lock its caller holds would re-enter it. process's releases (`reims-vgpu.graphics-closes-`), staged copies of surfaces' caches into their pages (`reims-vgpu.graphics-into-pages-`), the video caches' spare surfaces made ahead on Windows (`reims-vgpu.graphics-drawn-spares-`), video - (`reims-vgpu.graphics-video-`) and event waits (`reims-vgpu.graphics-event-waits`). How + (`reims-vgpu.graphics-video-`) and event waits (`reims-vgpu.graphics-event-waits`), and + the thread a decoder's state is let go of on where the walk of its tables at the device's loss + held it last (`reims-vgpu.graphics-tables-let-go`, `graphics::decode`'s `TableWalk::each`). How long a pool keeps an idle thread is `reims-vgpu-graphics`'s `keep`. The guest's Shared pool has two threads of its own (15.1): its taker (`reims-vgpu.graphics-pool-ahead`), which takes the blocks of its own asks for more pool, and its thread (`reims-vgpu.graphics-pool`), which