Files
geramyloveless b190c105a8 Kernel arguments in CPU-visible VRAM, published with the HDP flush
HRX on Linux keeps a queue's kernel arguments in CPU-visible VRAM and
publishes them with the HDP flush register when the HSA runtime reports
HSA_AMD_AGENT_INFO_HDP_FLUSH and lets the CPU at the device coarse pool.
This brings that path to the Mac, where a CPU store to a GPU that stopped
answering can panic the machine, so every such store is bracketed under
the client's gate.

patches/linux/amdgpu-mmio-remap-4k-hole.patch: the NBIO/NBIF HDP flush
remap stays in the 4 KiB register hole the hardware leaves unused,
whatever the host page size (upstream moved it with PAGE_SIZE and turned
it off for pages over 4 KiB). KFD's own MMIO paths still refuse large
pages; nothing upstream maps the page into a process.

linuxu (kfd_session.c): rt_kfd_session_hdp_flush gives the host page of
the register BAR holding the remap page, after checking that no register
block of the device starts in the rest of it. VRAM can take a window VA,
and rt_kfd_bo_make_cpu_visible pins such a buffer in the CPU-visible
window: unmapped from the GPU, off the eviction fence, pinned with
CPU_ACCESS_REQUIRED, the move waited for, mapped again at the same VA.
rt_kfd_bo_bar_ranges reports its BAR ranges; a free unpins it first.

dext (hdp_flush.h): QueryInfo MLG_QUERY_BAR_WRITES lets a KFD client
opt in. It gets its gate (published, so every power transition, reset,
quarantine, fault, removal and close closes it and drains), the HDP
flush page as client memory, and CPU access to hostable VRAM: BOAlloc
flag DEXT_COMPUTE_BO_FLAG_HOSTABLE, then BOMap pins it and hands out its
BAR ranges. A client that never asks gets none of it. A retired gate is
marked so the client knows it never reopens. Closes that stop the device
answering (the session's close, quarantine, reset) wait up to 1 s for
an open bracket. Build 265.

HSA runtime: mac_hsa_bar_writes_enable, mac_hsa_bar_write_begin and
mac_hsa_bar_write_end. After enable the agent reports HDP_FLUSH, the CPU
agent's access to the VRAM pool is DISALLOWED_BY_DEFAULT, VRAM is
allocated hostable, and hsa_amd_agents_allow_access with a CPU agent maps
an allocation write combined at its own address, as ROCr does on a
large-BAR device; nothing else is CPU-mapped. begin waits while the gate
is held and fails once the device is gone, after replacing every BAR
mapping of the process with host memory (bar_mapping_retire.h); kicks
that find the GPU disconnected do the same.

libmlg_drm: a transport that says the device is gone has every mapping
retired the same way, so Vulkan clients' stores through mapped VRAM and
doorbells land in host memory too.

Tests: the session test pins hostable VRAM, checks its BAR ranges, a
full window and the HDP page with and without a register block beside
it; the production test enables BAR writes, maps hostable VRAM and
ranges it; hsa-bar-writes checks what is reported before and after
enable, the CPU grant, nested brackets, a held gate and a lost device;
the gate and libmlg_drm tests check retired gates and mappings.
2026-10-06 19:29:29 -07:00

239 lines
7.8 KiB
C

/* A submission's stores into the BARs as one bracket (mlg_bar_write_begin
* and mlg_bar_write_end in dext/sources/doorbell_gate.h), raced the way the
* driver races them: client threads submit without pause, each submission
* copying its kernel arguments into the client's VRAM kernarg ring with
* memcpy, storing the HDP flush register and reading it back, and ringing
* the doorbell (a nested bracket, mlg_doorbell_ring), while the session
* queue holds every gate for power transitions and resets, retires one
* client's gate (its close) and every gate (the session's close or a
* removal).
*
* What it proves: once a hold or retire returns, none of a client's
* stores reaches its ring, its HDP register or its doorbell until the gate
* opens again, however many stores a submission makes. The device side
* poisons all three the moment the close returns and checks them before
* reopening; a store in between changes them (and is a data race
* ThreadSanitizer reports). */
#define MLG_DOORBELL_GATE_SLOTS 8u
#include "doorbell_gate.h"
#include <assert.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define CLIENTS 4u
#define SUBMITTERS 2u /* queues per client, each its own thread */
#define RING_BYTES 4096u /* a queue's kernarg ring in VRAM */
#define KERNARG_BYTES 192u /* one submission's arguments (3 blocks) */
#define POISON 0xa5u
#define POISON64 0xa5a5a5a5a5a5a5a5ull
struct queue {
uint8_t ring[RING_BYTES]; /* the device's VRAM */
volatile uint32_t hdp_mem_flush_cntl; /* the device's register (one per
* queue here, so the submitters
* do not race each other) */
volatile uint64_t doorbell; /* the device's doorbell */
uint32_t write; /* the submitter's ring position */
};
struct client {
uint64_t owner;
struct mlg_doorbell_gate *_Atomic gate; /* the client's mapping */
struct queue queues[SUBMITTERS];
_Atomic uint64_t value;
_Atomic uint64_t submitted, refused, nested_refused;
};
static struct mlg_doorbell_gates gates;
static struct client clients[CLIENTS];
static _Atomic int stop;
static uint64_t now_ns(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
}
static struct mlg_doorbell_drain drain(uint64_t bound_ns)
{
struct mlg_doorbell_drain d = {now_ns, bound_ns, 0, 0};
return d;
}
/* One submission as HRX makes it: kernargs, HDP publication, doorbell. */
static void *submitter(void *arg)
{
const unsigned n = (unsigned)(uintptr_t)arg;
struct client *c = &clients[n % CLIENTS];
struct queue *q = &c->queues[n / CLIENTS];
uint8_t args[KERNARG_BYTES];
while (!atomic_load(&stop)) {
struct mlg_doorbell_gate *gate = atomic_load(&c->gate);
const uint64_t v = atomic_fetch_add(&c->value, 1) + 1;
memset(args, (int)(v & 0x7f), sizeof(args));
if (!mlg_bar_write_begin(gate)) {
atomic_fetch_add(&c->refused, 1);
continue;
}
if (q->write + KERNARG_BYTES > RING_BYTES)
q->write = 0;
memcpy(&q->ring[q->write], args, sizeof(args));
q->write += KERNARG_BYTES;
q->hdp_mem_flush_cntl = 1u;
(void)q->hdp_mem_flush_cntl;
if (!mlg_doorbell_ring(gate, &q->doorbell, v))
atomic_fetch_add(&c->nested_refused, 1);
mlg_bar_write_end(gate);
atomic_fetch_add(&c->submitted, 1);
}
return NULL;
}
static struct mlg_doorbell_gate *new_gate(void)
{
struct mlg_doorbell_gate *gate = calloc(1, sizeof(*gate));
assert(gate);
gate->magic = MLG_DOORBELL_GATE_MAGIC;
return gate;
}
/* The device: from here no store of @c may land. */
static void poison(struct client *c)
{
for (unsigned i = 0; i < SUBMITTERS; ++i) {
c->queues[i].hdp_mem_flush_cntl = (uint32_t)POISON64;
memset(c->queues[i].ring, POISON, RING_BYTES);
c->queues[i].doorbell = POISON64;
}
}
static void check_poison(struct client *c)
{
for (unsigned i = 0; i < SUBMITTERS; ++i) {
assert(c->queues[i].hdp_mem_flush_cntl == (uint32_t)POISON64);
for (unsigned b = 0; b < RING_BYTES; ++b)
assert(c->queues[i].ring[b] == POISON);
assert(c->queues[i].doorbell == POISON64);
}
}
static void quiet_window(void)
{
usleep(300);
}
int main(void)
{
pthread_t threads[CLIENTS * SUBMITTERS];
unsigned holds = 0, retires = 0, removals = 0;
struct mlg_doorbell_drain d;
for (unsigned i = 0; i < CLIENTS; ++i) {
clients[i].owner = 100 + i;
atomic_store(&clients[i].gate, new_gate());
assert(mlg_doorbell_gates_publish(&gates, clients[i].owner, atomic_load(&clients[i].gate)));
}
/* A closed gate opens no bracket and leaves busy as it was. */
{
struct mlg_doorbell_gate *g = new_gate();
assert(!mlg_bar_write_begin(g) && g->busy == 0);
g->open = 1;
assert(mlg_bar_write_begin(g) && g->busy == 1);
assert(mlg_bar_write_begin(g) && g->busy == 2); /* nested */
mlg_bar_write_end(g);
mlg_bar_write_end(g);
assert(g->busy == 0);
free(g);
}
for (unsigned i = 0; i < CLIENTS * SUBMITTERS; ++i)
assert(!pthread_create(&threads[i], NULL, submitter, (void *)(uintptr_t)i));
const uint64_t until = now_ns() + 1500000000ull;
for (unsigned round = 0; now_ns() < until; ++round) {
usleep(200);
switch (round % 3) {
case 0: /* a power transition or a reset: every gate */
d = drain(1000000000ull);
mlg_doorbell_gates_hold(&gates, round % 2 ? MLG_DOORBELL_HOLD_RESET : MLG_DOORBELL_HOLD_POWER,
true, &d);
assert(!d.stuck);
/* Held, not retired: the client waits for it to open. */
for (unsigned i = 0; i < CLIENTS; ++i)
assert(!mlg_doorbell_gate_retired(atomic_load(&clients[i].gate)));
for (unsigned i = 0; i < CLIENTS; ++i)
poison(&clients[i]);
quiet_window();
for (unsigned i = 0; i < CLIENTS; ++i)
check_poison(&clients[i]);
mlg_doorbell_gates_hold(&gates, round % 2 ? MLG_DOORBELL_HOLD_RESET : MLG_DOORBELL_HOLD_POWER,
false, &d);
++holds;
break;
case 1: { /* one client's close; it comes back with a new gate */
struct client *c = &clients[(round / 3) % CLIENTS];
d = drain(1000000000ull);
mlg_doorbell_gates_retire(&gates, c->owner, &d);
assert(!d.stuck);
/* Closed for good: the client stops and retires its mappings. */
assert(mlg_doorbell_gate_retired(atomic_load(&c->gate)));
poison(c);
quiet_window();
check_poison(c);
atomic_store(&c->gate, new_gate()); /* old one leaked: still mapped */
assert(mlg_doorbell_gates_publish(&gates, c->owner, atomic_load(&c->gate)));
++retires;
break;
}
case 2: /* the session's close or a removal: every gate */
if (round % 7)
break;
d = drain(1000000000ull);
mlg_doorbell_gates_retire(&gates, 0, &d);
assert(!d.stuck && mlg_doorbell_gates_count(&gates) == 0);
for (unsigned i = 0; i < CLIENTS; ++i)
assert(mlg_doorbell_gate_retired(atomic_load(&clients[i].gate)));
for (unsigned i = 0; i < CLIENTS; ++i)
poison(&clients[i]);
quiet_window();
for (unsigned i = 0; i < CLIENTS; ++i)
check_poison(&clients[i]);
for (unsigned i = 0; i < CLIENTS; ++i) {
atomic_store(&clients[i].gate, new_gate());
assert(mlg_doorbell_gates_publish(&gates, clients[i].owner,
atomic_load(&clients[i].gate)));
}
++removals;
break;
}
}
atomic_store(&stop, 1);
for (unsigned i = 0; i < CLIENTS * SUBMITTERS; ++i)
pthread_join(threads[i], NULL);
uint64_t submitted = 0, refused = 0, nested = 0;
for (unsigned i = 0; i < CLIENTS; ++i) {
submitted += atomic_load(&clients[i].submitted);
refused += atomic_load(&clients[i].refused);
nested += atomic_load(&clients[i].nested_refused);
}
assert(holds && retires && removals && submitted && refused);
printf("test_bar_write_gate: %llu submissions (%llu refused at begin, %llu doorbells refused "
"inside an open bracket); %u holds, %u client closes, %u session closes: no store "
"after a close\n",
(unsigned long long)submitted, (unsigned long long)refused, (unsigned long long)nested, holds,
retires, removals);
return 0;
}