From 9e8baebc4891da6653179935bbb03e909933f90d Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 22 Nov 2026 13:54:57 -0800 Subject: [PATCH] Runtime: behind a cut head waiting for room, what would be sent through waits in its queue's order What: next_ready does not pick an entry that hold() would send its queue through to (ended before it went, nothing for the GPU, or presenting: Entry::sent_through, the one condition both now read) while the FIFO's head that waits for room is a cut command buffer. The FIFO offers nothing until room comes; the head's pieces go at the room's pace, and the entry goes behind them in the queue's order, scheduled as it reaches the head (or, presenting, once submitted). The runtime README's room paragraph says so. Why: the submit thread spun with the work lock held. Behind a cut head waiting for room (waits_for_room's None arm), next_ready returned the first entry not held, with waits; hold() took its early return for it (ended, empty or presenting), raising send_through and leaving it unheld, and a cut head never goes through on send_through (waits_for_room's `through`), so nothing next_ready reads changed: pass() continued on Staged and picked the same entry again, for as long as the room stayed full. Completions are settled only at pass()'s top and commits wait for the lock, so the room never came back. A cut command buffer of three or more pieces waits for room at its third piece by room() alone, so the window opened during each such command buffer's run. The fix holds Metal's order within the queue: the entry completes after the head and is scheduled no earlier than it. Verified: the four forms, each a test and a process of its own, hang without the fix and pass with it on lavapipe; with the compile check, the pieces tests and the runtime suite on lavapipe: see the jobs named where it is handed over. --- crates/metal2vulkan-runtime/README.md | 5 ++- crates/metal2vulkan-runtime/src/submit.rs | 39 +++++++++++++++++++---- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 786ebeddd..80b70943f 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -1894,7 +1894,10 @@ made, with no lock, and never rewritten for layouts. at once as it is staged, and so does a presenting command buffer's last (see "Presenting"); so a command buffer that signals an event, in any segment, is staged only once the device has room for it, as one held for room behind it is, and a wait for its signal waits with it, while a - presenting one has its queue go at once, through to it. So of a chain of command buffers, each + presenting one has its queue go at once, through to it, and so does one with nothing for the + GPU or one ended before it went, except behind a cut command buffer, whose pieces go at the + room's pace: such a one goes after its last piece, as the room comes, and a cut one that + presents waits for room at each piece as any cut one does. So of a chain of command buffers, each waiting for the signal of the one before it, the links past the device's room go as earlier ones complete. The driver runs a submission's command buffers as one kernel job, which a job timeout ends as a whole diff --git a/crates/metal2vulkan-runtime/src/submit.rs b/crates/metal2vulkan-runtime/src/submit.rs index 18227250c..53e3271c6 100644 --- a/crates/metal2vulkan-runtime/src/submit.rs +++ b/crates/metal2vulkan-runtime/src/submit.rs @@ -312,6 +312,20 @@ struct Entry { held: Option, } +impl Entry { + /// Whether, met behind a head that waits for room, it has its queue sent through to it + /// rather than held ([`WorkState::hold`]): committed and ended before it went, holding + /// nothing for the GPU that no copy-in waits for, or presenting a drawable, which is + /// scheduled once submitted. + fn sent_through(&self, captured: &capture::Captured) -> bool { + self.pending.as_ref().is_some_and(|pending| { + self.error.is_some() + || (pending.gpu.is_empty() && !captured.waits_for(pending.seq)) + || pending.gpu.presents() + }) + } +} + /// Commands recorded into one command buffer apart from any command buffer of the guest's (zero /// fills, texture copies, and new textures' moves to their home layouts), the calls made before /// one commit and after the one before it: submitted ahead of everything in the first submission @@ -1618,7 +1632,8 @@ impl WorkState { /// no longer waits, the earliest held: it was scheduled before anything that comes next. /// Else, of the committed heads that are ended or whose next segment is ready, and of the /// first command buffers not yet scheduled behind heads that wait, ended or with every - /// segment's waits met, the earliest committed. + /// segment's waits met, the earliest committed; where the head that waits is cut, none + /// [`hold`](Self::hold) would send its queue through to ([`Entry::sent_through`]). fn next_ready(&self) -> Option<(usize, bool)> { let mut held: Option<(u64, usize)> = None; let mut best: Option<(usize, bool, u64)> = None; @@ -1635,6 +1650,19 @@ impl WorkState { true => fifo.entries.iter().find(|entry| entry.held.is_none()), false => fifo.entries.front(), }; + // A cut head's every piece waits for room, never sent through (`waits_for_room`), so + // an entry `hold` would send the queue through to, the head or one behind it, is not + // picked while it waits: `hold` would leave it as it was, picked again at once, the + // work lock held, for as long as the room stays full. It goes in the queue's order as + // the room comes. + let head = fifo.entries.front().and_then(|head| head.pending.as_ref()); + let captured = &self.changes.captured; + if waits + && head.is_some_and(|head| head.gpu.cut) + && entry.is_some_and(|entry| entry.sent_through(captured)) + { + continue; + } let Some(Entry { pending: Some(pending), error, @@ -1804,14 +1832,13 @@ impl WorkState { let Some(entry) = fifo.entries.iter_mut().find(|entry| entry.held.is_none()) else { return; }; - let Some(pending) = entry.pending.as_mut() else { - return; - }; - let empty = pending.gpu.is_empty() && !captured.waits_for(pending.seq); - if entry.error.is_some() || empty || pending.gpu.presents() { + if entry.sent_through(captured) { fifo.send_through = fifo.send_through.max(entry.place); return; } + let Some(pending) = entry.pending.as_mut() else { + return; + }; let scheduled = std::mem::take(&mut pending.handlers.scheduled); entry.cb.scheduled(now, !scheduled.is_empty()); if !scheduled.is_empty() {