Hold what a blit, a per-sample copy and an indirect sparse update use until the command buffer completes

Metal's command buffer retains what its encoders were given until it completes, so a program may
release a buffer or texture once it committed. Two paths did not hold theirs:
- A blit between a buffer and a multisampled texture, emulated per sample, retained only its
  copy's plan (the raw VkBuffer and its own images), not the program's Buffer or the texture.
- An indirect sparse mapping update's host step replaced the update with the updates its arguments
  state; where they state none, nothing held the texture and buffer, and the replaced update was
  dropped under the command buffer's state lock, so a no-copy buffer's last release could run its
  deallocator under that lock.

What changes:
- EncoderCore::blit_at, the one way a blit command takes a buffer's VkBuffer, retains the buffer
  until completion, so every blit path holds it; the per-sample copy holds the texture it copies
  into or out of.
- read_mappings moves the replaced sparse update into the command buffer's retained list,
  released at completion outside the lock.
- A buffer or texture these commands used is now released at completion rather than at the
  program's release.

Why: the consumer's resource synchronization on an RTX 5080 copies a multisampled Depth32Float
texture into a no-copy buffer over guest pages, commits, waits until scheduled and drops the
buffer. The import was freed 3 ms after the commit, and the copy engine's write at its first page
faulted 285 ms later and lost the device. METAL2VULKAN_LOG_MEMORY placed the fault at offset 0 of
the freed import and showed the next import mapped at the same address: with that mapping first,
the copy would have written into other guest memory silently.

Verified:
- tests::blit_holds (device test): each leg puts the blit behind 1 GiB of fills, commits, waits
  until scheduled, releases the no-copy buffer and asserts its deallocator has not run while
  pending, then checks the bytes in the host pages (buffer to and from no-copy, fill, multisampled
  R32Uint both ways, multisampled Depth32Float set Volatile after release).
- tests::encoder_holds (device tests), one leg per way a command reaches memory: draws reading
  vertex, fragment, index and indirect buffers, a draw into a texture over a no-copy buffer,
  dispatches, an indirect dispatch, use_resource and use_heap reaches, an indirect command
  buffer's dispatch, and sparse indirect updates that map a level and that map nothing. While
  pending, the deallocator has not run and every watched object lives.
- Before the fix, on lavapipe under the validation layer 17 passed and 2 failed (the sparse
  update's lost texture, and a heap's placed buffer, which Metal's contract does not hold either
  and the test no longer watches); on the Iris Xe 18 passed, 1 failed (the placed buffer).
- The fixed runtime built under m2v_gpu and m2v_cpu_icd with clippy -D warnings clean; the fixed
  legs ran in the following device batches.
This commit is contained in:
Anees Iqbal committed 2026-10-19 00:15:59 +03:00
1 parent 2b97fc1cf0
commit ef7fe8aa56
7 files changed
+1446 -7

No files matched your search

@@ -2420,8 +2420,8 @@ impl CommandBufferShared {
/// The host step's reading of batch `index`'s indirect sparse updates (`sparse/indirect.rs`),
/// before its segments' steps: each is replaced, in its place, by the updates its arguments
/// state now. A refusal is kept for the batch's submission, which fails the command buffer with
/// it, and the rest stay unread.
/// state now, and held until the command buffer completes. A refusal is kept for the batch's
/// submission, which fails the command buffer with it, and the rest stay unread.
fn read_mappings(self: &Arc<Self>, index: usize) {
let device = &self.device;
let unread: Vec<(usize, usize, SparseUpdate)> = {
@@ -2463,7 +2463,17 @@ impl CommandBufferShared {
let mut state = lock(&self.state);
match read {
Ok(updates) => {
state.recorded[encoder].sparse.splice(at..at + 1, updates);
// The update read goes to `retained`: its texture and buffer are held until
// the command buffer completes, as Metal's command buffer holds what its
// encoders were given, though its arguments map nothing; and it is released
// outside this lock (the buffer's last release may run a deallocator).
let read: Vec<SparseUpdate> = state.recorded[encoder]
.sparse
.splice(at..at + 1, updates)
.collect();
state
.retained
.extend(read.into_iter().map(|update| Box::new(update) as Box<_>));
}
Err(failure) => {
state.step_failure = Some(match failure {