Commit Graph
13 Commits
Author SHA1 Message Date
Anees Iqbal 4ed0e6923a 🐎 Schedule a buffer with no scheduled handler where it is seen
A buffer with a completed handler paid two hops to its queue's completion
queue, one for its CB_SCHEDULED and one for its CB_COMPLETE, though its
scheduling runs nothing of the app's when it has no scheduled handler. Such a
buffer's scheduling now runs on the completion thread as it takes the record,
where none of the queue's records still waits on the completion queue, as the
records of a buffer with no handler at all already do.

What the guest sees: for a buffer with no scheduled handler, the status turns
Scheduled and waitUntilScheduled returns from the completion thread, one
thread wake sooner. Behind a record of its queue still to run, it waits its
turn as before, so the status still flips in the ring's order among the
handlers. The completed handler still runs on the completion queue.
2026-11-05 21:59:36 -08:00
Anees Iqbal 09eb85c73c ✅ Time the guest's buffer scheduling and completion
The car 12 probe put a lone buffer's commit-to-scheduled-handler at about
390 us p50 on the RTX 5080 host, against 198 on an AMD Mac. Part of that is the
guest's own hops: the completion thread's wake, then the queue's completion
queue for each record. shell_tests --queue-timing [rounds] times them on the
bundle's real queue, reader and completion thread, with the kext's wait stood
in for: how long after the host's post of a CB_SCHEDULED, then a CB_COMPLETE,
the handlers run and waitUntilScheduled and waitUntilCompleted return, for a
buffer with both handlers, one with a completed handler alone and one with
none. It is a measurement, run by hand, never in make test.
2026-11-05 21:59:36 -08:00
Anees Iqbal 0f98581125 🐛 Write a buffer's copies back as its completion is taken
The copies a command buffer wrote that no surface retire waits for went
back only at its delivery, behind the queue's earlier completed handlers:
app memory under a no-copy buffer (RgfxMemoryShadow), and the copy of a
surface whose writes were not named to the kext. A handler that ran long,
or a CPU that read the memory once the GPU was done (a shared event the
buffer signalled), saw the bytes as of an earlier buffer.

Those copies now go back on the queue's write-back queue as the buffer's
CB_COMPLETE is taken, in the ring's order, as the named surfaces' copies
already did; the delivery waits for them, so the status and the waiters
still come after the write-back.

What the guest sees: a buffer's GPU writes to such memory show as soon as
its completion is taken, not after the handlers of the buffers before it.
2026-11-05 21:59:35 -08:00
Anees Iqbal a674eb81e5 🐛 Write a buffer's surface copies back off its queue's handlers
A command buffer that wrote an IOSurface's copy, or came behind one, wrote
its copies back and retired its writes at its delivery, on its queue's
completion queue, behind the queue's earlier handlers. A completed handler
that locked a surface a later buffer of its queue writes waited in the kext
for that buffer's retire, which could not come until the handler returned:
the lock waited out the kext's 2 s, then went on without the buffer's
results. Metal's lock waits only for the GPU.

What changes for the guest: such a buffer writes its copies back and retires
its writes on a serial queue of its queue's own, one buffer at a time in the
ring's order, as soon as its completion is taken; its delivery waits for
that, so its handlers and its status still see its results in the surfaces'
pages. A lock in an earlier handler goes on once the later buffer's copies
are written back. A buffer that writes only surfaces in place and is named
retires at the take as before; one behind a copy still retires after it.

The write-back empties the buffer's named accesses under _namedLock before
it retires, as the delivery did: a held buffer whose CB_SCHEDULED did not
come, named at its delivery's scheduling, and one whose post's naming is
still to come, name no write to the kext after its retire, which would hold
the surface's CPU locks for good (agreed with fx-decoder's held naming).

queue_tests' test_in_place_writes_retire_at_the_take now has the copy and the
in-place buffer behind it retire, in order and after the copy's write-back,
while the handler before them holds their deliveries; and a held buffer
writing the copy, completed with no CB_SCHEDULED, retires there and is named
to the kext neither at its post nor after its retire.
2026-11-05 21:59:32 -08:00
Anees Iqbal 5888901b46 ✅ Test that a mid-buffer signal wakes a CPU wait in the bundle
The host now posts a signal the buffer has work after while the buffer runs,
ahead of its CB_SCHEDULED. The test posts such a value on a queue's ring the
completion thread drives, and checks that a thread waiting on the CPU for it
wakes while the buffer is still Committed, that the value it then sets goes to
the host, and that the buffer completes once the host lets its wait go.
2026-11-05 21:59:31 -08:00
Anees Iqbal 9fc6ecb551 🐎 Deliver a handler-less command buffer where its completion is seen
A command buffer's scheduling and completion records ran on its queue's
completion queue whatever they held, so the waiter for a buffer with no handler
woke two wakes after the completion thread took its record off the ring: a
libdispatch worker's for the record, then its own. Metal's waiter for a buffer
with no completed handler is woken by the thread that saw it complete (on the
M3 Max the wait returned 3 to 10 us after the status read Completed).

A buffer with no scheduled or completed handler, whose surface writes are all
in place and which writes no copy of app memory, now has its records run on the
thread that takes them off the ring, where none of its queue's records is still
to run on the queue, as rgfxDeliver:here: already ran them where no thread
drives the ring. A buffer with a handler, and any behind it while its record is
still to run, runs on the queue in the ring's order as before, its waiter woken
after its status is final. The decision is made at the commit, after which no
handler can be added.
2026-11-05 21:59:27 -08:00
Anees Iqbal 6abd44a64f 🐎 Retire a buffer's in-place surface writes as its completion is taken
A command buffer's writes of shared IOSurfaces retired at its delivery, on its
queue's completion queue, behind the queue's earlier records and their
handlers. A CPU lock of a surface the buffer drew in place waited for that
retire, or for the kext's next look at the device's completions, though the
GPU's results were in the surface's pages from the device's completion on.
Metal's lock waits only for the GPU.

A buffer whose written surfaces are all in place, and already named to the
kext, now retires its writes on the completion thread as it takes its
CB_COMPLETE, with the record's seqno (its _seqno is written after the post,
which the host may complete first). A buffer with a copy to write back keeps
its retire at its delivery, after the write-back, and so does every later
buffer of the queue until that retire is done: the kext retires a queue's
writes up to a seqno, so an early retire behind a copy would retire the copy
before its write-back. No call is added: the one retire moves.

queue_tests' test_in_place_writes_retire_at_the_take pins both: the in-place
retire comes while an earlier handler holds the delivery (it fails with the
take's retire turned off), and a copy then an in-place buffer retire only at
their deliveries, in order.
2026-11-05 21:59:26 -08:00
Anees Iqbal 6f1efe0ccf 🐛 Give a command buffer's region block back as it goes uncompleted
A command buffer whose message is too large for its queue's ring puts it
in a block of the queue's body pool, and only its completion gave the
block back. One let go with no completion to come (posted on a queue
whose completion ring nothing reads) kept its block for good, so the
region lost a block a buffer. Its dealloc now gives the block back, as it
already gave back its slot and its inline bytes' chunks.

A buffer posted with a completion to come is held by its reader until
that completion, so a block given back at dealloc is one the host has
read, or never will. The test lets uncommitted and posted buffers go one
after another and checks that they reuse one block and that the queue's
going returns it to the region.
2026-11-05 21:59:23 -08:00
Anees Iqbal 3a6d6cec1d 🐛 End the process at a second enqueue or commit, as Metal does
A second enqueue, an enqueue after the commit and a second commit returned
with nothing done. On Metal each ends the process at the call (measured on
the M3 Max, an AMD 5300M and an Intel 630), so code that relies on the
silence is code that dies on a Mac.

What changes for the guest: a second enqueue, or one after the commit,
ends the process with "enqueue an already enqueued command buffer" from
-[_MTLCommandBuffer enqueue]; a second commit, or a
commitAndWaitUntilSubmitted after the commit, ends it with "commit an
already committed command buffer" from -[IOGPUMetalCommandBuffer validate],
the M3's words (the AMD's are the same from MTLIOAccelCommandBuffer, and
the Intel trips its completed-handler assertion instead).

The shell test that committed twice to show nothing more was posted now
stops at the one commit; tests/queue_tests.mm checks the four deaths.
2026-11-05 21:59:23 -08:00
Anees Iqbal e60a8f9952 🐛 Post a queue's command buffers in the order they were enqueued
enqueue only changed the status, and each commit posted its buffer at once,
so buffers ran in commit order. An engine that encodes on several threads and
fixes the GPU order with enqueue then got a write and its read, or two
writes, in the wrong order (aud-bundle-01).

What changes for the guest: a queue's command buffers go on its submission
ring in enqueue order, as Metal runs them on the M3 Max, an AMD 5300M and an
Intel 630 (measured). The queue keeps its enqueued, unposted buffers oldest
first; a commit with no enqueue before it enqueues the buffer there. A
buffer committed behind one not yet committed stays Committed and unposted,
and commitAndWaitUntilSubmitted of it waits, until that one is committed;
that commit then posts every ready buffer behind it, in order. An enqueued
buffer let go uncommitted stays and holds back the buffers after it, as
Metal's does (measured on all three).

One thread at a time posts a queue's ready buffers. Its lock is taken again
only by a handler the poster itself runs, where no thread drives the ring,
and then the poster's loop posts the handler's buffer after the one it is
posting.

The checks are tests/queue_tests.mm's, with the audit's two enqueue checks
folded in, plus four threads committing 400 enqueued buffers in reverse.
2026-11-05 21:59:23 -08:00
Anees Iqbal 90b4ebf663 🐛 Run each queue's handlers on its own completion queue
One completion thread ran every queue's scheduled and completed handlers
inline, and setCompletionQueue: was kept but changed nothing. A completed
handler of one queue that waited on a semaphore or a shared event another
queue's handler signals held up every queue's completions in the process,
presents and waitUntilCompleted included, for good where the wait had no
timeout. WindowServer's handlers ran off the serial queue it gave, and one
queue's write-backs delayed every other queue's (aud-bundle-02).

What changes for the guest:
- A queue's records (its buffers' scheduled and completed handlers, their
  write-backs, status changes and waiters) run in ring order on its
  completion queue: the dispatch queue setCompletionQueue: gave, else a
  serial queue of its own labelled com.Metal.CompletionQueueDispatch at
  user-interactive QoS. That is where Metal runs them, one queue's one at a
  time and two queues' at once, and the status changes among the handlers
  (measured on the M3 Max, an AMD 5300M and an Intel 630).
- The completion thread runs no handler. It marks a buffer submitted as it
  takes the CB_SUBMITTED or CB_SCHEDULED, and at the CB_COMPLETE it gives
  back the queue's slot, the message's block and the inline bytes' chunks, so
  commitAndWaitUntilSubmitted and -commandBuffer never wait for handlers, as
  Metal's do not.
- The waits no longer take the ring inside a handler. A wait for a later
  buffer of the handler's own queue, for its own buffer, or from the queue's
  completion queue for that queue, now waits for good. Metal's does too on
  all three GPUs (measured). A wait for another queue's buffer is released.

Where no completion thread drives the ring (the unit tests, or a thread that
ended), the thread that takes the ring runs the records, as before.

The tests follow: the thread test checks the handlers' queue, the nested
same-queue wait goes, and the check of a commitAndWaitUntilSubmitted
answered by CB_SCHEDULED takes Committed or Scheduled at the return, as
Metal may, then Scheduled after waitUntilScheduled. tests/queue_tests.mm adds
the audit's cross-queue check, the completion queue, the order of one
queue's records and the slots taken back at the read.
2026-11-05 21:59:23 -08:00
Anees Iqbal c7a1286287 🐛 End the process at a handler added after commit, as Metal does
addCompletedHandler: and addScheduledHandler: appended to their lists with no
lock and no look at the status. An add while the completion was delivered
mutated the list the delivering thread enumerated, ending the process there
with an NSGenericException; an add after the delivery was never run
(aud-bundle-09).

What changes for the guest: a handler added after the commit, before or after
the buffer completes, ends the process at the call with Metal's words,
"Completed handler provided after commit call" or "Scheduled handler provided
after commit call", as Metal does on the M3 Max, an AMD 5300M and an Intel 630
(measured). presentDrawable: after the commit adds a scheduled handler, so it
ends there too. The adds and the commit take one lock, so the delivering
thread walks lists nothing changes any more.

One shell test added its handler after the commit; it adds it before now.
The checks are tests/queue_tests.mm's, with the audit's two late-handler
checks folded in.
2026-11-05 21:59:23 -08:00
Anees Iqbal 6254f3fc0a 🐛 Hold an encoder's command buffer until its endEncoding
An encoder held its command buffer weakly but wrote through the buffer's
RgfxEncoder, which the buffer's dealloc deletes. A buffer that went while its
encoder was open, such as a Swift makeCommandBuffer() temporary or one made in
a pool that drained, left the encoder writing freed memory (aud-bundle-10).

What changes for the guest: each encoder (render, compute, blit, parallel and
a parallel pass's sub-encoder) now holds its buffer strongly from its making
until its endEncoding, then lets it go. That is what Metal's encoders do
(measured on the M3 Max, an AMD 5300M and an Intel 630): a buffer whose only
holder is an open encoder lives on, an ended encoder no longer keeps it, and
the ended encoder's commandBuffer is nil once the buffer is gone.

The check is tests/queue_tests.mm, a new part of the shell tests, with the
audit's encoder check folded in.
2026-11-05 21:59:23 -08:00