Clients refuse a driver that does not serve session calls async

An async session call to a driver older than this protocol is answered
synchronously and never completed, so a client that awaited it would
wait forever (a new runtime in /usr/local/lib talking to a driver that
is not yet replaced, or whose activation failed).

RuntimeBuild's compiled build (DEXT_RUNTIME_BUILD) is now 243, the first
to serve session calls async (MLG_SESSION_CALLS_ASYNC_BUILD).
host/selector_call.h asks it once per connection
(mlg_selector_call_on) before the first async call and, against an older
driver, fails at once with kIOReturnUnsupported; the HSA runtime keeps
the answer per connection and forgets it on close, libmlg_drm per
process, and both say on stderr that the installed driver is older than
build 243. scripts/mlg_owner_call.py refuses such a driver when it
connects. The Linux-file path needs nothing: an older driver refuses
LX_CALL_ASYNC as an unknown selector at once.

test-selector-call runs selector_call.h against mock drivers: build 242
and a three-word RuntimeBuild fail at once with no async call, a driver
that does not answer is reported gone, a 243 driver gets the async call,
the protocol is asked once per connection, and calls that never sleep
stay synchronous.
This commit is contained in:
geramyloveless committed 2026-10-04 21:55:03 -07:00
1 parent 121b63ba35
commit ff324ff9a0
9 files changed
+265 -22

No files matched your search

+10 -2
View File
@@ -887,8 +887,16 @@ test-bounded:
-o $(BUILD)/tests/test_bounded
$(BUILD)/tests/test_bounded
test: test-owner-call test-bounded
.PHONY: test-owner-call test-bounded
# host/selector_call.h against mock old and new drivers.
test-selector-call:
@mkdir -p $(BUILD)/tests
$(CC) -std=gnu11 -g -O1 -Wall -Wextra -Werror -Wno-unused-function -fsanitize=address,undefined \
-Ihost linuxu/tests/test_selector_call.c -framework IOKit -framework CoreFoundation \
-o $(BUILD)/tests/test_selector_call
$(BUILD)/tests/test_selector_call
test: test-owner-call test-bounded test-selector-call
.PHONY: test-owner-call test-bounded test-selector-call
test-session-shutdown:
bash scripts/test-session-shutdown.sh
+2 -1
View File
@@ -10,7 +10,8 @@
#define DEXT_RUNTIME_MAGIC 0x414d444750554142ull
#define DEXT_RUNTIME_ABI 1u
#define DEXT_RUNTIME_BUILD 192u /* 192: KFD compute sessions, QueryInfo tag 12 */
#define DEXT_RUNTIME_BUILD 243u /* 192: KFD compute sessions, QueryInfo tag 12;
243: session calls are async (session_state.h) */
#define DEXT_BO_SLOTS 8192u
/* Queue handles across clients: legacy HQDs plus KFD user queues. */
#define DEXT_QUEUE_SLOTS 1024u
+49 -5
View File
@@ -11,11 +11,31 @@
#include "owner_call.h"
#include "../dext/sources/session_state.h"
static inline kern_return_t mlg_selector_call(io_connect_t connection, uint32_t selector,
const uint64_t *input, uint32_t input_count,
const void *input_struct, size_t input_struct_size,
uint64_t *output, uint32_t *output_count,
void *output_struct, size_t *output_struct_size)
/* Whether the driver behind @connection serves session calls async: 1,
* 0 for an older driver (which answers them synchronously and would never
* complete an async call), or -1 when RuntimeBuild itself failed. */
static inline int mlg_driver_async_session_calls(io_connect_t connection)
{
uint64_t build[4] = {0, 0, 0, 0};
uint32_t count = 4;
if (IOConnectCallScalarMethod(connection, MLG_SELECTOR_RUNTIME_BUILD, NULL, 0, build,
&count) != kIOReturnSuccess)
return -1;
return count >= 4 && build[3] >= MLG_SESSION_CALLS_ASYNC_BUILD;
}
/* As mlg_selector_call, with the driver's protocol remembered in *@state
* (0 before the first call; the caller keeps it per connection). A
* selector that must be async, against a driver older than
* MLG_SESSION_CALLS_ASYNC_BUILD, fails at once with kIOReturnUnsupported:
* never a wait for a completion that will not come. */
static inline kern_return_t mlg_selector_call_on(io_connect_t connection, int *state,
uint32_t selector,
const uint64_t *input, uint32_t input_count,
const void *input_struct, size_t input_struct_size,
uint64_t *output, uint32_t *output_count,
void *output_struct, size_t *output_struct_size)
{
#ifndef MLG_SELECTOR_CALL_TEST_SYNC
if (mlg_call_is_synchronous(selector, input, input_count))
@@ -30,9 +50,33 @@ static inline kern_return_t mlg_selector_call(io_connect_t connection, uint32_t
input_struct_size, output, output_count, output_struct,
output_struct_size);
}
if (*state == 0) {
const int async = mlg_driver_async_session_calls(connection);
if (async < 0)
return kIOReturnNotAttached;
*state = async ? 1 : -1;
}
if (*state < 0)
return kIOReturnUnsupported; /* a driver older than build 243 */
return mlg_owner_call(connection, selector, input, input_count, input_struct,
input_struct_size, output, output_count, output_struct,
output_struct_size);
}
/* @selector as the driver serves it (the protocol checked on each async
* call: for callers that make few). */
static inline kern_return_t mlg_selector_call(io_connect_t connection, uint32_t selector,
const uint64_t *input, uint32_t input_count,
const void *input_struct, size_t input_struct_size,
uint64_t *output, uint32_t *output_count,
void *output_struct, size_t *output_struct_size)
{
int state = 0;
return mlg_selector_call_on(connection, &state, selector, input, input_count,
input_struct, input_struct_size, output, output_count,
output_struct, output_struct_size);
}
#endif
+42 -9
View File
@@ -26,14 +26,47 @@
#include <optional>
#include <string>
#include <thread>
#include <unordered_map>
// A selector as the driver serves it (host/selector_call.h).
// Whether each open connection's driver serves session calls async
// (host/selector_call.h): checked on its first async call, forgotten when
// it closes.
static std::mutex sessionCallProtocolsLock;
static std::unordered_map<io_connect_t, int> sessionCallProtocols;
static kern_return_t closeConnection(io_connect_t port) {
{
std::lock_guard lock(sessionCallProtocolsLock);
sessionCallProtocols.erase(port);
}
return IOServiceClose(port);
}
// A selector as the driver serves it (host/selector_call.h). Against a
// driver older than build 243 a session call fails at once, with the
// reason on stderr, never waiting for a completion that will not come.
static kern_return_t rpcMethod(io_connect_t port, uint32_t selector, const uint64_t *input,
uint32_t inputs, const void *inputStruct, size_t inputStructSize,
uint64_t *output, uint32_t *outputs, void *outputStruct,
size_t *outputStructSize) {
return mlg_selector_call(port, selector, input, inputs, inputStruct, inputStructSize,
output, outputs, outputStruct, outputStructSize);
int state;
{
std::lock_guard lock(sessionCallProtocolsLock);
state = sessionCallProtocols[port];
}
const int before = state;
const auto result = mlg_selector_call_on(port, &state, selector, input, inputs, inputStruct,
inputStructSize, output, outputs, outputStruct,
outputStructSize);
if (state != before) {
std::lock_guard lock(sessionCallProtocolsLock);
sessionCallProtocols[port] = state;
if (state < 0)
std::fprintf(stderr, "mac_linuxgpu: the installed MacLinuxGPU driver is older than build %u "
"and this runtime needs it: install the matching driver\n",
MLG_SESSION_CALLS_ASYNC_BUILD);
}
return result;
}
namespace mac_hsa {
@@ -119,8 +152,8 @@ public:
IOConnectUnmapMemory64(ownerPort, buffer.memoryType, mach_task_self(), reinterpret_cast<uintptr_t>(buffer.host));
}
stopFirmwareService(); // Never left running past initialization.
if (ownerPort) IOServiceClose(ownerPort);
if (pendingProbePort) IOServiceClose(pendingProbePort);
if (ownerPort) closeConnection(ownerPort);
if (pendingProbePort) closeConnection(pendingProbePort);
if (service) IOObjectRelease(service);
}
bool supportsBuffers() const override { return true; }
@@ -622,7 +655,7 @@ public:
std::array<uint64_t, 3> build{}; uint64_t tag = 4, stage = 0;
auto status = call(probe, 43, nullptr, 0, build.data(), 3);
if (status == HSA_STATUS_SUCCESS) status = call(probe, 21, &tag, 1, &stage, 1);
if (probe != ownerPort && IOServiceClose(probe) != KERN_SUCCESS) {
if (probe != ownerPort && closeConnection(probe) != KERN_SUCCESS) {
pendingProbePort = probe;
state = State::Faulted;
return HSA_STATUS_ERROR;
@@ -1126,7 +1159,7 @@ private:
nullptr, nullptr));
}, emit);
(void)std::fflush(stderr);
const auto closed=IOServiceClose(ownerPort);
const auto closed=closeConnection(ownerPort);
if (closed==KERN_SUCCESS) ownerPort=IO_OBJECT_NULL;
// A failed claimed session must not automatically retry probe on
// the next property query. Retain a failed-close port for teardown.
@@ -1140,7 +1173,7 @@ private:
firmware.clear();
if (!claimed) {
// Busy acquisition must not leave an idle client blocking Host Stop.
IOServiceClose(ownerPort);
closeConnection(ownerPort);
ownerPort = IO_OBJECT_NULL;
state = State::Unclaimed;
return status;
@@ -1385,7 +1418,7 @@ public:
io_connect_t port = ownerPort;
if (!port && IOServiceOpen(service, mach_task_self(), 0, &port) != KERN_SUCCESS)
return HSA_STATUS_ERROR_INVALID_AGENT;
struct Close { io_connect_t port; ~Close() { if (port) IOServiceClose(port); } } close{ownerPort ? 0 : port};
struct Close { io_connect_t port; ~Close() { if (port) closeConnection(port); } } close{ownerPort ? 0 : port};
uint64_t identity[3]{};
auto status = call(port, 43, nullptr, 0, identity, 3);
if (status != HSA_STATUS_SUCCESS) return status;
+14 -3
View File
@@ -2,6 +2,7 @@
* MLG_USER_CLIENT_LINUX_FILE) of the MacLinuxGPU DriverKit extension, and
* the selectors of rt/lx_abi.h. */
#include <errno.h>
#include <stdio.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
@@ -106,9 +107,19 @@ static int scalar(uint32_t selector, const uint64_t *in, uint32_t nin, uint64_t
if (r)
return r;
/* HostWindow and InitDevice can sleep: async session calls. */
return from_ioreturn(mlg_selector_call(c, selector, in, nin, NULL, 0, out,
out ? &n : NULL, NULL, NULL));
/* HostWindow and InitDevice can sleep: async session calls, which a
* driver older than build 243 does not serve (refused at once). */
{
static int protocol; /* one driver connection per process */
kern_return_t kr = mlg_selector_call_on(c, &protocol, selector, in, nin, NULL, 0, out,
out ? &n : NULL, NULL, NULL);
if (kr == kIOReturnUnsupported && protocol < 0)
fprintf(stderr, "libmlg_drm: the installed MacLinuxGPU driver is older than build "
"%u, which this library needs: install the matching driver\n",
MLG_SESSION_CALLS_ASYNC_BUILD);
return from_ioreturn(kr);
}
}
static int scalar_call(void *ctx, uint32_t selector, const uint64_t *in, uint32_t nin,
+1 -1
View File
@@ -432,7 +432,7 @@ int main(int argc, char **argv)
uint64_t words[8], window[3], ring, meta, q, status;
startup_failure=7; kfd_supported_error=0;
assert(dext_compute_start(&pdev)==0);
assert(dext_compute_runtime_build(words)==0 && words[2]==192);
assert(dext_compute_runtime_build(words)==0 && words[2]==243);
dext_compute_set_kfd_policy(false);
dext_compute_select_client(3);
assert(dext_compute_query_info(12,words,8)==8 && words[1]==1 && !words[2]);
+128
View File
@@ -0,0 +1,128 @@
/* host/selector_call.h against mock drivers: a selector that must be async
* goes async only to a driver that serves it so (RuntimeBuild's compiled
* build >= MLG_SESSION_CALLS_ASYNC_BUILD); to an older one (build 242, or
* one whose RuntimeBuild answers three words) it fails at once with
* kIOReturnUnsupported, never waiting for a completion that will not come.
* The protocol is asked once per connection; calls that never sleep are
* synchronous everywhere. Only the IOKit calls are replaced. */
#include <assert.h>
#include <stdio.h>
#include <IOKit/IOKitLib.h>
static kern_return_t mock_scalar(mach_port_t, uint32_t, const uint64_t *, uint32_t, uint64_t *, uint32_t *);
static kern_return_t mock_method(mach_port_t, uint32_t, const uint64_t *, uint32_t, const void *, size_t,
uint64_t *, uint32_t *, void *, size_t *);
static kern_return_t mock_async(mach_port_t, uint32_t, mach_port_t, uint64_t *, uint32_t, const uint64_t *,
uint32_t, const void *, size_t, uint64_t *, uint32_t *, void *, size_t *);
static IONotificationPortRef mock_port_create(mach_port_t);
static void mock_port_destroy(IONotificationPortRef);
static mach_port_t mock_port_machport(IONotificationPortRef);
#define IOConnectCallScalarMethod mock_scalar
#define IOConnectCallMethod mock_method
#define IOConnectCallAsyncMethod mock_async
#define IONotificationPortCreate mock_port_create
#define IONotificationPortDestroy mock_port_destroy
#define IONotificationPortGetMachPort mock_port_machport
#include "selector_call.h"
static uint64_t driver_build; /* RuntimeBuild out[3]; 0: three words only */
static int build_fails, builds, syncs, asyncs, ports;
static kern_return_t mock_scalar(mach_port_t c, uint32_t selector, const uint64_t *in, uint32_t nin,
uint64_t *out, uint32_t *count)
{
(void)c; (void)in; (void)nin;
if (selector == MLG_SELECTOR_RUNTIME_BUILD) {
++builds;
if (build_fails)
return kIOReturnNotAttached;
out[0] = 0x414d444750554142ull; out[1] = 1; out[2] = 0;
if (driver_build && *count >= 4) {
out[3] = driver_build;
*count = 4;
} else {
*count = 3;
}
return kIOReturnSuccess;
}
++syncs;
if (count) *count = 0;
return kIOReturnSuccess;
}
static kern_return_t mock_method(mach_port_t c, uint32_t selector, const uint64_t *in, uint32_t nin,
const void *s, size_t sn, uint64_t *out, uint32_t *count, void *so, size_t *son)
{
(void)s; (void)sn; (void)so; (void)son;
return mock_scalar(c, selector, in, nin, out, count);
}
/* A 243 driver that refuses the call before starting it: nothing waits. */
static kern_return_t mock_async(mach_port_t c, uint32_t selector, mach_port_t wake, uint64_t *ref,
uint32_t nref, const uint64_t *in, uint32_t nin, const void *s, size_t sn,
uint64_t *out, uint32_t *count, void *so, size_t *son)
{
(void)c; (void)selector; (void)wake; (void)ref; (void)nref; (void)in; (void)nin;
(void)s; (void)sn; (void)out; (void)count; (void)so; (void)son;
++asyncs;
return kIOReturnBadArgument;
}
static IONotificationPortRef mock_port_create(mach_port_t main)
{
(void)main;
++ports;
return (IONotificationPortRef)(uintptr_t)0x1000;
}
static void mock_port_destroy(IONotificationPortRef port) { (void)port; --ports; }
static mach_port_t mock_port_machport(IONotificationPortRef port) { (void)port; return 9; }
int main(void)
{
const io_connect_t c = 7;
uint64_t out[3] = {0};
uint32_t n = 3;
int state = 0;
/* Calls that never sleep: synchronous, the protocol never asked. */
assert(mlg_selector_call_on(c, &state, MLG_SELECTOR_PING, NULL, 0, NULL, 0, out, &n, NULL, NULL) ==
kIOReturnSuccess && syncs == 1 && builds == 0 && state == 0);
/* A build-242 driver: InitDevice fails at once, no async call, and
* the protocol is asked once for the connection. */
driver_build = 242;
assert(mlg_selector_call_on(c, &state, 9, NULL, 0, NULL, 0, NULL, NULL, NULL, NULL) ==
kIOReturnUnsupported && state < 0 && builds == 1 && asyncs == 0);
assert(mlg_selector_call_on(c, &state, 16, out, 1, NULL, 0, out, &n, NULL, NULL) ==
kIOReturnUnsupported && builds == 1 && asyncs == 0);
/* An older RuntimeBuild (three words): older still. */
driver_build = 0;
state = 0;
assert(mlg_selector_call_on(c, &state, 9, NULL, 0, NULL, 0, NULL, NULL, NULL, NULL) ==
kIOReturnUnsupported && state < 0 && asyncs == 0);
/* No answer to RuntimeBuild: the driver is gone, nothing is assumed. */
build_fails = 1;
state = 0;
assert(mlg_selector_call_on(c, &state, 9, NULL, 0, NULL, 0, NULL, NULL, NULL, NULL) ==
kIOReturnNotAttached && state == 0 && asyncs == 0);
build_fails = 0;
/* A 243 driver: the call goes async (here refused before it starts,
* so the call returns that at once); the protocol is asked once. */
driver_build = MLG_SESSION_CALLS_ASYNC_BUILD;
state = 0;
builds = 0;
assert(mlg_selector_call_on(c, &state, 9, NULL, 0, NULL, 0, NULL, NULL, NULL, NULL) ==
kIOReturnBadArgument && state > 0 && asyncs == 1 && builds == 1);
assert(mlg_selector_call_on(c, &state, 16, out, 1, NULL, 0, out, &n, NULL, NULL) ==
kIOReturnBadArgument && asyncs == 2 && builds == 1 && ports == 0);
/* The stateless form asks on every async call. */
assert(mlg_selector_call(c, 9, NULL, 0, NULL, 0, NULL, NULL, NULL, NULL) ==
kIOReturnBadArgument && builds == 2);
puts("PASS selector calls: async only to a driver that serves it so; an older driver's "
"session call fails at once, a missing one is reported gone, the protocol asked once "
"per connection, calls that never sleep stay synchronous");
return 0;
}
+13
View File
@@ -14,6 +14,8 @@ OWNER_RESULT = 88 # session_state.h MLG_SELECTOR_OWNER_RESULT
OWNER_HEADER = 4 # MLG_OWNER_ASYNC_HEADER
OWNER_SCALARS = 12 # MLG_OWNER_ASYNC_SCALARS
PING = 0
RUNTIME_BUILD = 43
SESSION_CALLS_ASYNC_BUILD = 243 # MLG_SESSION_CALLS_ASYNC_BUILD
CALLOUT_FUNC, CALLOUT_REFCON, CALLOUT_COUNT = 1, 2, 3 # IOKit's kIOAsyncCallout* indices
MACH_RCV_MSG, MACH_RCV_TIMEOUT, MACH_RCV_TIMED_OUT = 0x2, 0x100, 0x10004003
NOT_ATTACHED, IPC_ERROR = 0xe00002d8, 0xe00002c3
@@ -44,6 +46,17 @@ class OwnerCall:
system.mach_msg.argtypes = [c.c_void_p, c.c_int, c.c_uint, c.c_uint, c.c_uint,
c.c_uint, c.c_uint]
system.mach_msg.restype = c.c_int
# An older driver answers such calls synchronously and never
# completes them: refuse it now rather than wait forever.
build = (c.c_uint64 * 4)()
count = c.c_uint(4)
result = io.IOConnectCallMethod(connection, RUNTIME_BUILD, None, 0, None, 0, build,
c.byref(count), None, None)
if result:
raise RuntimeError(f"RuntimeBuild failed: {result & 0xffffffff:#x}")
if count.value < 4 or build[3] < SESSION_CALLS_ASYNC_BUILD:
raise RuntimeError("the installed MacLinuxGPU driver is older than build "
f"{SESSION_CALLS_ASYNC_BUILD}; install the matching driver")
def __call__(self, selector, scalars, data=b"", outputs=0, capacity=0):
io, system = self.io, self.system
+6 -1
View File
@@ -31,10 +31,15 @@ for driver, mirror in (("MLG_SELECTOR_OWNER_RESULT", "MLG_OWNER_CALL_SELECTOR_RE
("MLG_OWNER_ASYNC_SCALARS", "MLG_OWNER_CALL_SCALARS")):
if number(state, driver) != number(client, mirror):
raise SystemExit(f"host/owner_call.h {mirror} differs from session_state.h {driver}")
backend = pathlib.Path("dext/sources/dext_compute_backend.inc").read_text()
runtime = re.search(r"#define DEXT_RUNTIME_BUILD (\d+)u", backend)
if not runtime or int(runtime.group(1)) < int(number(state, "MLG_SESSION_CALLS_ASYNC_BUILD")):
raise SystemExit("DEXT_RUNTIME_BUILD predates MLG_SESSION_CALLS_ASYNC_BUILD: clients would refuse this driver")
python = pathlib.Path("scripts/mlg_owner_call.py").read_text()
for driver, mirror in (("MLG_SELECTOR_OWNER_RESULT", "OWNER_RESULT"),
("MLG_OWNER_ASYNC_HEADER", "OWNER_HEADER"),
("MLG_OWNER_ASYNC_SCALARS", "OWNER_SCALARS")):
("MLG_OWNER_ASYNC_SCALARS", "OWNER_SCALARS"),
("MLG_SESSION_CALLS_ASYNC_BUILD", "SESSION_CALLS_ASYNC_BUILD")):
found = re.search(r"^" + mirror + r"\s*=\s*(\d+)", python, re.M)
if not found or found.group(1) != number(state, driver):
raise SystemExit(f"scripts/mlg_owner_call.py {mirror} differs from session_state.h {driver}")