A doorbell cost the submitting thread a trip through the driver: 14.7 us
for the synchronous AQLQueueKick of build 263 (20 us async before it), on
the one delivery thread every client's calls share. On Linux a process
writes the doorbell page it mmaps from /dev/kfd: one store. From build 264
the runtime does the same.
- The client maps its KFD process's doorbell slice (MLG_DOORBELL_MEMORY_TYPE,
uncached): the page of the doorbell BAR KFD gave that process
(rt_kfd_session_doorbells), holding only its own doorbells, as libhsakmt
maps it. AQLQueueCreate asked for three outputs answers the queue's
doorbell in it (out[2]), or MLG_DOORBELL_NONE for a queue rung through
AQLQueueKick (a legacy queue). A KFD queue whose doorbell cannot be
mapped is destroyed again and the call fails.
- A store to the GPU's BARs once it stops answering can panic the Mac, so
every doorbell write goes through a gate (doorbell_gate.h): a page the
driver and the client share (MLG_DOORBELL_GATE_MEMORY_TYPE). The client
counts itself busy, writes only while the gate is open, and counts itself
out; the driver closes the gate and waits until busy is zero (Dekker's
protocol, sequentially consistent both sides), so once a close returns
the client writes nothing. A closed gate writes nothing: the runtime
then sends the doorbell as AQLQueueKick, which answers what the device
can take (Offline while it suspends: the doorbell waits for resume, as
before; NoDevice once it is gone).
- The driver closes every gate for a power transition (power_set), and for
a device reset's whole window (the recovery hooks, before upstream halts
the device until the PCI restore put the BARs back; a failed reset
wedges and keeps them closed). Both doorbell paths, the clients' and the
delivery thread's kick table, now hold a set of reasons instead of one
flag, so a power change inside a reset opens nothing. A client's gate
is retired (closed, drained, forgotten) at its close, its KFD process's
release (whose doorbell slice may go to another process), the session's
close or quarantine, a transport fault and a removal. The wait is a
bounded spin (it may run on the delivery thread): a client stopped
inside a write for 20 ms is named in the log.
- Runtime: the queue keeps its doorbell and gate from creation until
inactivate; a write never lowers the doorbell (as the driver's paths).
Measured on the R9700 over Thunderbolt 5 (lat.cpp, display mirroring):
doorbell store 14.7 -> 0.38 us median; empty dispatch round trip 43.9 ->
32.4 us (Linux on the same card: 31.3); back-to-back wall per dispatch with
a doorbell each 16.8 -> 12.7 us. LemonSeed Engine decode is GPU-bound:
plain 28.2 -> 28.7 tok/s, DFlash2 51.3 -> 51.4.
Tests: test-doorbell-gate races client threads ringing through their gates
against power holds, device-reset holds inside them, client closes and
session closes, under ThreadSanitizer and AddressSanitizer; a doorbell
written after a close returns fails it (dropping the drain does). It also
names a client stopped mid-write. test-kfd-session checks the slice (a
page of BAR 2, the process's own) and that the client's offset is the word
the driver's kicks write; test-dext-compute-production the doorbell
lookup, gate publish, power and reset holds and retire at the client's
release; test-kick-table holds and releases both reasons;
test-hsa-power-transport the runtime's maps, its direct store, a closed
gate, and the failures that destroy the queue again.
A doorbell cost the submitting thread an async session call: about 20 us,
twice a synchronous call through the driver (a Ping is 10 us), because an
async call sets up its completion and hops to the session queue. From
build 263, AQLQueueKick called synchronously rings the doorbell on the
delivery thread itself.
DriverKit delivers every client's calls on that thread, which must never
sleep (build 241). The doorbell there takes no session lock and reads none
of the session queue's tables:
- kick_table.h: the KFD queues that may be rung on the delivery thread, by
handle and owner, under a spinlock nothing holds across a sleep. The
session queue publishes a queue once it exists and retires it before
anything of it goes: its DESTROY_QUEUE, its owner's close, the device's
stop. A ring holds the lock across its doorbell write, so once retire
returns no doorbell of the queue is written and its memory may go. The
table also closes for power transitions (power_set), which waits for a
write in progress.
- rt_kfd_queue_kick_nowait: the doorbell under the session's kick
spinlock alone. A queue is ringable from its creation until its
DESTROY_QUEUE (queue_release_locked clears it under that lock first,
since the doorbell is then released to other queues). Both doorbell
paths share the highest value written, and neither writes a lower one.
As on Linux, where the process writes its doorbell page itself, the
value is not checked against the write index.
- The PCI access gate under the doorbell write is a spinlock and atomic
counters, so nothing on that path sleeps. The store to the BAR stays in
the driver, behind its removal gate; nothing is mapped into the client.
What the fast path cannot vouch for (a legacy queue, a busy or closing
session, an unpublished queue) it answers kIOReturnUnsupported, and the
runtime sends that doorbell as the async session call. Offline,
VMError, NoDevice and BadArgument answer as the async call does.
Tests: test-kick-table races four delivery threads against queues
published, retired and freed at once, client closes and power gates, under
ThreadSanitizer and AddressSanitizer. No ring reaches a retired, freed,
foreign or gated queue. The delivery threads kept ringing while the
session thread slept (a slow DESTROY_QUEUE): rings during the sleeps 2830
under TSan, 29100 under ASan. Longest wait for the lock 137 us under TSan,
38 us under ASan. test-kfd-session covers the nowait path on the real KFD
session code: written, never lower, refused from DESTROY_QUEUE on, -EFAULT
after a GPU fault. test-dext-compute-production covers publish, owner,
gate and retire on destroy and on client close. test-hsa-power-transport
covers the runtime's synchronous form and each answer.
On the R9700, the faulting process got HSA_STATUS_ERROR rather than
HSA_STATUS_ERROR_MEMORY_FAULT. The CP stops the queue and writes an error
code to its inactive signal before KFD's interrupt work signals the
process's memory event. The queue service took the code for a broken
scratch request and failed with -EIO, the selector answered
kIOReturnNotReady, and the runtime faulted the whole connection before
the fault report could arrive.
Now dext_kfd_queue_service returns -ENOEXEC with the code for a
non-scratch inactive value, and selector 59 answers kIOReturnIOError with
the code in output 1, which is an answer, not a failed call. The runtime
keeps polling such a queue for up to 250 ms. If the fault report
(kIOReturnVMError) arrives it reports the fault, address and reason as
before. Otherwise it reports HSA_STATUS_ERROR_EXCEPTION and prints the
code. Nothing waits in the driver.
test_dext_kfd checks the code is returned with -ENOEXEC. The
production test's kfd-fault-isolated scenario checks the stopped queue is
answered with the code without freezing the device, before the fault
itself arrives.
A client killed with an open engine left its session uncertain whenever
DESTROY_QUEUE failed: remove_queue_mes times out when a wave neither
finishes nor preempts, KFD asks for a GPU reset that recovery-off turns
into a log line, and the session kept every buffer. The dext then
quarantined the device until a replug.
Tear a dying client down as upstream recovers a hung MES user queue
(amdgpu_userq's detect_and_reset before the unmap): when DESTROY_QUEUE
fails after KFD already took the queue off its lists, MES resets the
compute queues it finds hung and removes this one again with
remove_queue_after_reset, under the device queue manager's lock as
remove_queue_mes runs. A queue KFD still lists is retried with
DESTROY_QUEUE instead, never a second DESTROY_QUEUE on a detached one.
Close tries every queue, and keeps memory only while something is still
unconfirmed.
An SDMA copy that outlives its timeout keeps its fence: the session
settles once it signals, and close waits for it once more, bounded.
rt_kfd_session_settle retries all of this without closing, and
rt_kfd_session_failure names the first step that could not confirm the
GPU let go. Each failure and recovery is logged with the queue and
doorbell.
The KFD fixture gains a MES whose queues can hang (REMOVE_QUEUE fails
until a hung-queue reset), die or answer slowly, kfd_hws_hang's reset
request as recovery-off logs it, and an SDMA engine that holds copies.
The tests kill clients mid-session with a hung queue, a slow MES, a MES
that never answers (kept, then recovered by closing again), a destroy
retried by a settle, a copy that outlives its timeout, a bounded launch
whose wave never preempts, and a client closed with a hung persistent
queue; every buffer and allocation is accounted for afterwards.
Runs the unmodified upstream Linux amdgpu and amdkfd drivers on Apple
Silicon Macs inside a DriverKit system extension, for AMD GPUs attached
over Thunderbolt. The Linux sources come from a pinned submodule and are
compiled against linuxu, a userspace implementation of the Linux kernel
APIs they use. Includes the HSA runtime, the host installer app, the
setup and firmware fetch scripts, and the offline test suite.