Files
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

74 lines
1.9 KiB
C

/* Calls bounded in time (rt/bounded.h, linuxu/src/amdgpu-rt/bounded.c) on
* the real wait pool: work that finishes in time hands its argument back;
* work that overruns returns -ETIMEDOUT at the bound and frees its argument
* itself when it ends. */
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <rt/bounded.h>
#define LINUX_ETIMEDOUT 110
struct work {
unsigned int sleep_ms;
int ran;
int *freed;
};
static void work_main(void *arg)
{
struct work *w = arg;
usleep(w->sleep_ms * 1000);
w->ran = 1;
}
static void work_release(void *arg)
{
struct work *w = arg;
__atomic_store_n(w->freed, 1, __ATOMIC_RELEASE);
free(w);
}
static double ms_now(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1e3 + ts.tv_nsec / 1e6;
}
int main(void)
{
int freed = 0;
struct work *w = calloc(1, sizeof(*w));
double start;
/* In time: the caller owns the argument, release is not called. */
w->sleep_ms = 5;
w->freed = &freed;
assert(rt_bounded_run(work_main, w, work_release, 500) == 0);
assert(w->ran && !freed && rt_bounded_overrunning() == 0);
free(w);
/* Overrun: -ETIMEDOUT at the bound; the work frees its argument. */
w = calloc(1, sizeof(*w));
w->sleep_ms = 300;
w->freed = &freed;
start = ms_now();
assert(rt_bounded_run(work_main, w, work_release, 50) == -LINUX_ETIMEDOUT);
assert(ms_now() - start >= 45 && ms_now() - start < 250);
assert(rt_bounded_overrunning() == 1 && !__atomic_load_n(&freed, __ATOMIC_ACQUIRE));
for (int i = 0; i < 200 && !__atomic_load_n(&freed, __ATOMIC_ACQUIRE); ++i)
usleep(5000);
assert(__atomic_load_n(&freed, __ATOMIC_ACQUIRE) && rt_bounded_overrunning() == 0);
printf("PASS bounded calls: in-time work returns its argument, overrunning work times out "
"at the bound and frees its argument when it ends\n");
return 0;
}