Files
mac_linuxgpu/hsa/tests/test_transport_dispatch.cpp
geramyloveless 9d10aa0e33 Session calls never run on the driver's incoming-call thread
DriverKit delivers every client's calls, and the driver's own Stop, on
one thread, and runs each under its target queue with a synchronous
dispatch. Session clients shared the owner's queue, and observers and
Linux-file clients hopped to it synchronously, so a session call that
slept there (a probe, an allocation that evicts, a fence wait, a session
close), or anything queued behind one, held every client; on a GPU that
stopped, forever (build 241).

Every client now has a queue of its own, and nothing on the delivery
thread waits for the owner's queue:
- Selectors that never sleep answer there: Ping, RuntimeBuild (from flags
  the session queue publishes; the context is never touched), the cached
  QueryInfo tags, the power query and the power wait's registration, and
  OWNER_RESULT (88).
- Every other selector must be called with IOConnectCallAsync*: the call
  returns at once with a token and runs on the owner's queue, in arrival
  order, exactly as it ran there before; it completes with its IOReturn,
  scalar outputs and structure output length, and OWNER_RESULT fetches the
  structure output once. EVENT_WAIT keeps its own completion; a wait that
  does not start completes at once with the reason. A synchronous call of
  such a selector is refused (kIOReturnNotPermitted, logged once per
  client).
- SysfsRead and DrmInfo stay synchronous but bounded: the read runs on a
  wait-pool thread (rt_bounded_run, new) and the call waits at most
  250 ms (kIOReturnTimeout; while one overruns, the next is kIOReturnBusy).
  The observer's self-test, Retire and ReleaseQuarantine are async calls.
- A session client's Stop, and an observer's FinishStop, run on the owner's
  queue after the client's queued calls. Linux-file clients join the
  session on the owner's queue with their first open (LX_CALL_ASYNC),
  and the first open's InitDevice and HostWindow are async calls.
- CopyClientMemoryForType no longer touches the session: BOMap makes the
  BO's descriptor on the owner's queue and the mapping looks it up; the
  raw BAR lease takes its own spinlock.

Clients: host/owner_call.h and host/selector_call.h (header-only) call a
selector as the driver serves it, synchronously or as an awaited async
call; the HSA runtime, the host app (callScalar, callStruct, callMethod)
and libmlg_drm use them, and scripts/mlg_owner_call.py does the same for
drm-selftest.py and display-test.py's initialization. A client older
than this driver fails at its first session call that can sleep, with
the reason in the driver log.

Tests: test-owner-call runs the production owner_call machinery
(extracted, DriverKit mocked) against a session call that sleeps: calls
return at once, cached queries and the power wait answer while it
sleeps, later calls queue in order, completions carry the right layout,
OWNER_RESULT once, BO memory is found without the session, and a hung
bounded read times out within its bound. test-bounded covers
rt_bounded_run on the real wait pool; test-session-shutdown checks the
selector rules and runs the Stop paths through the owner's queue.
2026-10-04 21:22:42 -07:00

95 lines
4.5 KiB
C++

/* Selector 51 (ComputeDispatch) on the production IOKit transport. The
* Linux-shim driver runs it as a one-shot AQL dispatch that borrows a queue
* slot, and answers kIOReturnNoResources, before submitting anything, when
* every slot is held. Like selector 55 that is "nothing submitted", not a
* session fault. Only the IOKit calls are replaced; no service is opened. */
#define MAC_HSA_IDLE_DIAGNOSTIC
#define IOServiceClose test_service_close
#define IOConnectCallScalarMethod test_scalar
#define IOConnectCallMethod test_method
#define mlg_fw_service_start_connection test_fw_start
#define mlg_fw_service_stop test_fw_stop
/* The IOKit calls are replaced: every selector reaches them synchronously. */
#define MLG_SELECTOR_CALL_TEST_SYNC
#include "../src/transport_iokit.cpp"
#include <cassert>
/* No initialization runs here, so the firmware servicer never starts. */
extern "C" int test_fw_start(uint32_t, const char *, struct mlg_fw_service **) { assert(false); return -1; }
extern "C" void test_fw_stop(struct mlg_fw_service *service) { assert(!service); }
static constexpr io_connect_t test_port = 515;
static kern_return_t next_result = KERN_SUCCESS;
static unsigned dispatches, closes;
static uint64_t next_fence = 1;
extern "C" kern_return_t test_service_close(io_connect_t port) {
assert(port == test_port); ++closes; return KERN_SUCCESS;
}
extern "C" kern_return_t test_scalar(mach_port_t port, uint32_t selector, const uint64_t *,
uint32_t inputs, uint64_t *out, uint32_t *count) {
assert(port == test_port && selector == 43 && !inputs && *count == 3);
out[0] = 0x414d444750554142ull; out[1] = 1; out[2] = 219;
return KERN_SUCCESS;
}
extern "C" kern_return_t test_method(mach_port_t port, uint32_t selector, const uint64_t *, uint32_t,
const void *input, size_t inputBytes, uint64_t *out, uint32_t *count, void *, size_t *) {
assert(port == test_port && selector == 51 && input && inputBytes == sizeof(amdgpu::ComputeDispatchRequest));
assert(*count == 3);
++dispatches;
if (next_result != KERN_SUCCESS) return next_result;
out[0] = 0; out[1] = next_fence++; out[2] = 3;
return KERN_SUCCESS;
}
namespace mac_hsa { namespace {
struct IdleDiagnosticAccess {
static amdgpu::ComputeDispatchRequest request() {
amdgpu::ComputeDispatchRequest r{};
r.version = 2; r.codeHandle = 0x10000; r.codeBytes = sizeof(uint32_t);
r.groups[0] = r.groups[1] = r.groups[2] = 1;
r.threads[0] = 32; r.threads[1] = r.threads[2] = 1;
r.rsrc1 = 0xc0000; r.timeoutUS = 100000;
return r;
}
static void run() {
IOKitConnection connection(OriginalAtomicCaps{}, true);
connection.ownerPort = test_port;
connection.state = IOKitConnection::State::Ready;
connection.queueSlotLimit = 1; /* one AQL queue slot, as on the R9700 */
uint64_t fence = 0;
/* Another client holds the slot: refused, nothing submitted, healthy. */
next_result = kIOReturnNoResources;
assert(connection.dispatch(request(), fence) == HSA_STATUS_ERROR_OUT_OF_RESOURCES);
assert(dispatches == 1 && !fence && connection.state == IOKitConnection::State::Ready);
next_result = KERN_SUCCESS;
assert(connection.dispatch(request(), fence) == HSA_STATUS_SUCCESS && fence == 1);
/* This session's persistent queue holds the only slot: refused
* without asking the driver. The loader's code-sync takes the same
* path and leaves the session healthy. */
connection.hardwareQueues.emplace(7, std::array<uint64_t, 2>{1, 2});
assert(connection.dispatch(request(), fence) == HSA_STATUS_ERROR_OUT_OF_RESOURCES);
connection.codeSyncBuffer = {0x10000, 0x200000000ull, 16384};
connection.codeSyncUploaded = true;
assert(connection.invalidateCodeCaches() == HSA_STATUS_ERROR_OUT_OF_RESOURCES);
assert(dispatches == 2 && connection.state == IOKitConnection::State::Ready);
connection.hardwareQueues.clear();
assert(connection.invalidateCodeCaches() == HSA_STATUS_SUCCESS && dispatches == 3);
/* Any other failure still cannot prove completion: a session fault. */
next_result = kIOReturnError;
assert(connection.dispatch(request(), fence) == HSA_STATUS_ERROR);
assert(connection.state == IOKitConnection::State::Faulted);
connection.codeSyncBuffer = {};
}
};
} }
int main() {
mac_hsa::IdleDiagnosticAccess::run();
assert(closes == 1);
std::puts("Selector 51 NoResources and held queue slots leave the session healthy; other failures fault");
}