Merge branch 'feature/radv-apple-vram' into merge/radv-vram-into-display-c

# Conflicts:
#	dext/sources/MacLinuxGPUXcode.mm
This commit is contained in:
geramyloveless committed 2026-10-04 08:33:41 -07:00
commit b8999f34ed
13 files changed
+558 -66

No files matched your search

+17 -4
View File
@@ -79,6 +79,7 @@
#include <rt/drm_info.h>
#include <rt/lx_abi.h>
#include <rt/lx_files.h>
#include <rt/lx_timing.h>
#include <rt/kfd_session.h>
#include <rt/wait_pool.h>
#include <rt/sysfs.h>
@@ -2394,7 +2395,8 @@ static void event_wait_main(void *arg)
}
static kern_return_t lx_call(MacLinuxGPUUserClient *client, struct rt_lx_client *lx,
uint64_t selector, IOUserClientMethodArguments *a)
uint64_t selector, IOUserClientMethodArguments *a,
uint64_t entered_ns, uint64_t admitted_ns)
{
const uint64_t *in = a->scalarInput;
uint64_t *out = a->scalarOutput;
@@ -2442,15 +2444,24 @@ static kern_return_t lx_call(MacLinuxGPUUserClient *client, struct rt_lx_client
}
LxOutput reply;
if (!lx_output(a, reply)) return kIOReturnBadArgument;
const uint32_t cmd = (uint32_t)in[1];
const uint64_t args_ns = rt_lx_time_ns();
rt_lx_timing_add(cmd, RT_LX_HOP_ADMIT, admitted_ns - entered_ns);
rt_lx_timing_add(cmd, RT_LX_HOP_ARGS, args_ns - admitted_ns);
size_t bytes = 0;
int64_t result = 0;
const int r = rt_lx_ioctl(lx, (int)in[0], (uint32_t)in[1], frame.bytes, frame.length,
const int r = rt_lx_ioctl(lx, (int)in[0], cmd, frame.bytes, frame.length,
reply.bytes, reply.capacity, &bytes, &result);
if (r) return lx_transport_error(r);
const uint64_t ran_ns = rt_lx_time_ns();
out[0] = (uint64_t)result;
out[1] = bytes;
a->scalarOutputCount = 2;
return lx_output_done(a, reply, bytes);
const kern_return_t ret = lx_output_done(a, reply, bytes);
const uint64_t done_ns = rt_lx_time_ns();
rt_lx_timing_add(cmd, RT_LX_HOP_REPLY, done_ns - ran_ns);
rt_lx_timing_add(cmd, RT_LX_HOP_TOTAL, done_ns - entered_ns);
return ret;
}
case MLG_SELECTOR_LX_RESULT: {
if (nin != 1 || a->scalarOutputCount < 2) return kIOReturnBadArgument;
@@ -2516,12 +2527,14 @@ static kern_return_t lx_scanout_call(struct rt_lx_client *lx, IOUserClientMethod
static kern_return_t lx_external_method(MacLinuxGPUUserClient *client, uint64_t selector,
IOUserClientMethodArguments *arguments)
{
const uint64_t entered_ns = rt_lx_time_ns();
struct rt_lx_client *lx = nullptr;
kern_return_t ret = lx_state(client, &lx);
if (ret != kIOReturnSuccess) return ret;
if (!s_lxCalls.enter()) return kIOReturnNotReady;
ret = selector == MLG_SELECTOR_LX_SCANOUT ? lx_scanout_call(lx, arguments) :
lx_call(client, lx, selector, arguments);
lx_call(client, lx, selector, arguments, entered_ns,
rt_lx_time_ns());
s_lxCalls.leave();
return ret;
}
+2
View File
@@ -102,6 +102,8 @@ int rt_lx_result(struct rt_lx_client *c, uint64_t token, void *rbuf,
size_t cap, size_t *reply_bytes, int64_t *result);
/* Async calls running or kept. */
unsigned int rt_lx_async_outstanding(struct rt_lx_client *c);
/* The client's async workers (they stay, at most MLG_LX_MAX_ASYNC). */
unsigned int rt_lx_async_workers(struct rt_lx_client *c);
/* What backs a mapping. */
#define RT_LX_RANGE_CPU 1u /* addr: dext virtual address */
+67
View File
@@ -0,0 +1,67 @@
/* Where the time of a Linux-file request goes, hop by hop.
*
* The dext's user client and the Linux-file runtime (lx_files.c) time each
* request's hops with the uptime clock and add them here, per DRM/KFD
* request number (the command's low byte). The device's sysfs file
* mlg_lx_timing shows, per request number, the count and the mean of each
* hop in nanoseconds; the totals only grow, so a reader takes the
* difference of two reads (scripts/read-sysfs.py mlg_lx_timing).
*
* The hops of one LX_IOCTL, in order:
* admit user client: entry to the Linux-file state checked
* args user client: request frame and reply buffer set up
* frame frame checked and copied, call admitted (rt_lx_ioctl)
* pages the argument pages mapped into the Linux process
* copyin IN segments copied to the pages
* enter the calling thread becomes a thread of the Linux process
* ioctl the driver's unlocked_ioctl
* leave the thread leaves the Linux process
* copyout OUT segments copied to the reply
* release the argument pages unmapped
* finish call ended, frame copy freed
* reply user client: reply handed to IOKit, return
* total user client: entry to return
* An LX_IOCTL_ASYNC request (a wait) has no args or reply hops; its
* "spawn" hop is the time from the request to a worker taking it. */
#ifndef LINUXU_RT_LX_TIMING_H
#define LINUXU_RT_LX_TIMING_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
enum rt_lx_hop {
RT_LX_HOP_ADMIT,
RT_LX_HOP_ARGS,
RT_LX_HOP_FRAME,
RT_LX_HOP_PAGES,
RT_LX_HOP_COPYIN,
RT_LX_HOP_ENTER,
RT_LX_HOP_IOCTL,
RT_LX_HOP_LEAVE,
RT_LX_HOP_COPYOUT,
RT_LX_HOP_RELEASE,
RT_LX_HOP_FINISH,
RT_LX_HOP_REPLY,
RT_LX_HOP_TOTAL,
RT_LX_HOP_SPAWN,
RT_LX_HOP_COUNT
};
/* The uptime clock in nanoseconds. */
uint64_t rt_lx_time_ns(void);
/* Add @ns to hop @hop of request @cmd (counted once per request, on TOTAL
* for synchronous requests and on IOCTL for asynchronous ones). */
void rt_lx_timing_add(uint32_t cmd, enum rt_lx_hop hop, uint64_t ns);
/* The sysfs report: writes at most @size bytes to @buf, returns the length. */
long rt_lx_timing_show(char *buf, unsigned long size);
struct device;
/* Adds the device's mlg_lx_timing file if it is not there yet. */
void rt_lx_timing_register(struct device *dev);
#ifdef __cplusplus
}
#endif
#endif
+123 -40
View File
@@ -21,6 +21,7 @@
#include <drm/drm_file.h>
#include <drm/drm_ioctl.h>
#include <rt/chrdev.h>
#include <rt/lx_timing.h>
#include <rt/lx_files.h>
#include <rt/process.h>
#include "lx_internal.h"
@@ -62,16 +63,17 @@ struct lx_map {
struct lx_async {
struct lx_async *next;
struct lx_async *queued_next; /* waiting for a worker */
struct rt_lx_client *c;
uint64_t token;
int fd;
uint32_t cmd;
void *frame;
size_t frame_bytes;
uint64_t queued_ns; /* when the request arrived (rt/lx_timing.h) */
rt_lx_done_fn done;
void *ctx;
pthread_t thread;
bool started, finished, joined;
bool finished;
int64_t result;
void *rep;
size_t reply_bytes;
@@ -91,6 +93,15 @@ struct rt_lx_client {
unsigned int calls; /* calls inside the process */
struct lx_async *async;
unsigned int async_count;
/* The workers that run async calls: they stay for the client's next
* calls (starting a thread per wait costs more than the wait's round
* trip), at most MLG_LX_MAX_ASYNC, as many as calls ever overlapped. */
struct lx_async *queued, **queued_tail;
unsigned int queued_count; /* calls no worker took yet */
unsigned int idle_workers; /* workers waiting for a call */
unsigned int workers;
pthread_t worker_threads[MLG_LX_MAX_ASYNC];
pthread_cond_t work;
uint64_t next_token;
struct lx_map *maps;
uint64_t next_map;
@@ -120,15 +131,19 @@ int rt_lx_client_create(struct pci_dev *pdev, int pid, const char *comm,
c->pending_va = LX_PENDING_VA;
pthread_mutex_init(&c->lock, NULL);
pthread_cond_init(&c->changed, NULL);
pthread_cond_init(&c->work, NULL);
c->queued_tail = &c->queued;
pthread_mutex_init(&c->arena, NULL);
c->proc = linuxu_process_create(pid, comm && comm[0] ? comm : "lx-client");
if (!c->proc) {
pthread_mutex_destroy(&c->arena);
pthread_cond_destroy(&c->work);
pthread_cond_destroy(&c->changed);
pthread_mutex_destroy(&c->lock);
kfree(c);
return -ENOMEM;
}
rt_lx_timing_register(&pdev->dev);
*out = c;
return 0;
}
@@ -540,11 +555,15 @@ static int run_ioctl(struct rt_lx_client *c, int fd, uint32_t cmd, const void *f
uint8_t *out;
long ret;
int r;
uint64_t t0 = rt_lx_time_ns(), t1;
(void)frame_bytes;
r = pages_acquire(c, segs, head->nsegs, &pages);
if (r)
return r;
t1 = rt_lx_time_ns();
rt_lx_timing_add(cmd, RT_LX_HOP_PAGES, t1 - t0);
t0 = t1;
for (uint32_t i = 0; i < head->nsegs; ++i)
if (segs[i].dir & MLG_LX_SEG_IN)
pages_copy(&pages, segs[i].va,
@@ -556,12 +575,18 @@ static int run_ioctl(struct rt_lx_client *c, int fd, uint32_t cmd, const void *f
(uint64_t)INT64_MAX : now + head->timeout_ns;
pages_copy(&pages, head->timeout_va, &deadline, sizeof(deadline), true);
}
t1 = rt_lx_time_ns();
rt_lx_timing_add(cmd, RT_LX_HOP_COPYIN, t1 - t0);
t0 = t1;
r = linuxu_process_enter(c->proc, &saved);
if (r) {
pages_release(c, &pages);
return r;
}
file = fget(fd);
t1 = rt_lx_time_ns();
rt_lx_timing_add(cmd, RT_LX_HOP_ENTER, t1 - t0);
t0 = t1;
if (!file) {
ret = -EBADF;
} else {
@@ -573,10 +598,16 @@ static int run_ioctl(struct rt_lx_client *c, int fd, uint32_t cmd, const void *f
ret = -ENOTTY;
else
ret = file->f_op->unlocked_ioctl(file, cmd, (unsigned long)head->arg);
t1 = rt_lx_time_ns();
rt_lx_timing_add(cmd, RT_LX_HOP_IOCTL, t1 - t0);
t0 = t1;
fput(file);
}
linuxu_process_leave(&saved);
ret = lx_errno(ret);
t1 = rt_lx_time_ns();
rt_lx_timing_add(cmd, RT_LX_HOP_LEAVE, t1 - t0);
t0 = t1;
/* Linux leaves whatever the call wrote in user memory, failed or
* not: every OUT segment goes back. */
@@ -595,7 +626,11 @@ static int run_ioctl(struct rt_lx_client *c, int fd, uint32_t cmd, const void *f
rh.out_segments++;
}
memcpy(rep, &rh, sizeof(rh));
t1 = rt_lx_time_ns();
rt_lx_timing_add(cmd, RT_LX_HOP_COPYOUT, t1 - t0);
t0 = t1;
pages_release(c, &pages);
rt_lx_timing_add(cmd, RT_LX_HOP_RELEASE, rt_lx_time_ns() - t0);
*reply_bytes = rh.total_bytes;
*result = ret;
return 0;
@@ -605,7 +640,7 @@ int rt_lx_ioctl(struct rt_lx_client *c, int fd, uint32_t cmd,
const void *frame, size_t frame_bytes, void *rep,
size_t rep_cap, size_t *reply_bytes, int64_t *result)
{
uint64_t out_bytes = 0;
uint64_t out_bytes = 0, t0 = rt_lx_time_ns(), t1;
void *copy;
int r;
@@ -627,23 +662,28 @@ int rt_lx_ioctl(struct rt_lx_client *c, int fd, uint32_t cmd,
r = -ENOSPC;
if (!r)
r = call_begin(c);
t1 = rt_lx_time_ns();
rt_lx_timing_add(cmd, RT_LX_HOP_FRAME, t1 - t0);
if (!r) {
r = run_ioctl(c, fd, cmd, copy, frame_bytes, out_bytes, rep, reply_bytes, result);
t1 = rt_lx_time_ns();
call_end(c);
}
kvfree(copy);
rt_lx_timing_add(cmd, RT_LX_HOP_FINISH, rt_lx_time_ns() - t1);
return r;
}
/* ---- async ---- */
static void *async_main(void *arg)
static void async_run(struct lx_async *a)
{
struct lx_async *a = arg;
struct rt_lx_client *c = a->c;
uint64_t out_bytes = 0;
int consumed = 0;
rt_lx_timing_add(a->cmd, RT_LX_HOP_SPAWN, rt_lx_time_ns() - a->queued_ns);
a->status = mlg_lx_frame_check(a->frame, a->frame_bytes, a->cmd, &out_bytes);
if (!a->status) {
a->rep = kvzalloc(mlg_lx_reply_bytes(out_bytes), GFP_KERNEL);
@@ -682,37 +722,49 @@ static void *async_main(void *arg)
c->calls--;
pthread_cond_broadcast(&c->changed);
pthread_mutex_unlock(&c->lock);
}
/* A worker: runs queued calls until the client goes away. */
static void *async_worker(void *arg)
{
struct rt_lx_client *c = arg;
pthread_mutex_lock(&c->lock);
for (;;) {
struct lx_async *a = c->queued;
if (a) {
c->queued = a->queued_next;
if (!c->queued)
c->queued_tail = &c->queued;
c->queued_count--;
pthread_mutex_unlock(&c->lock);
async_run(a);
pthread_mutex_lock(&c->lock);
continue;
}
if (c->dying)
break;
c->idle_workers++;
pthread_cond_wait(&c->work, &c->lock);
c->idle_workers--;
}
pthread_mutex_unlock(&c->lock);
return NULL;
}
/* Join finished workers and drop delivered results. Caller holds c->lock;
* the lock is dropped around joins. */
/* Drop finished calls whose results were delivered. Caller holds c->lock. */
static void async_reap_locked(struct rt_lx_client *c)
{
for (bool again = true; again;) {
again = false;
for (struct lx_async **link = &c->async; *link; link = &(*link)->next) {
struct lx_async *a = *link;
for (struct lx_async **link = &c->async; *link;) {
struct lx_async *a = *link;
if (!a->finished)
continue;
if (!a->joined) {
pthread_t thread = a->thread;
a->joined = true;
pthread_mutex_unlock(&c->lock);
pthread_join(thread, NULL);
pthread_mutex_lock(&c->lock);
again = true;
break;
}
if (!a->rep && !a->done) {
*link = a->next;
c->async_count--;
kfree(a);
again = true;
break;
}
if (a->finished && !a->rep && !a->done) {
*link = a->next;
c->async_count--;
kfree(a);
} else {
link = &a->next;
}
}
}
@@ -740,6 +792,7 @@ int rt_lx_ioctl_async(struct rt_lx_client *c, int fd, uint32_t cmd,
}
memcpy(a->frame, frame, frame_bytes);
a->frame_bytes = frame_bytes;
a->queued_ns = rt_lx_time_ns();
a->c = c;
a->fd = fd;
a->cmd = cmd;
@@ -751,19 +804,26 @@ int rt_lx_ioctl_async(struct rt_lx_client *c, int fd, uint32_t cmd,
r = -ESRCH;
} else if (c->async_count >= MLG_LX_MAX_ASYNC) {
r = -EAGAIN;
} else if (c->queued_count + 1 > c->idle_workers &&
(c->workers >= MLG_LX_MAX_ASYNC ||
pthread_create(&c->worker_threads[c->workers], NULL, async_worker, c))) {
/* Every worker is busy and no other can start. Not reached while
* fewer than MLG_LX_MAX_ASYNC calls are out (one worker each). */
r = -EAGAIN;
} else {
if (c->queued_count + 1 > c->idle_workers)
c->workers++; /* the one just started takes it */
else
pthread_cond_signal(&c->work);
a->token = c->next_token++;
c->calls++;
r = pthread_create(&a->thread, NULL, async_main, a) ? -EAGAIN : 0;
if (r) {
c->calls--;
} else {
a->started = true;
a->next = c->async;
c->async = a;
c->async_count++;
*token = a->token;
}
*c->queued_tail = a;
c->queued_tail = &a->queued_next;
c->queued_count++;
a->next = c->async;
c->async = a;
c->async_count++;
*token = a->token;
}
pthread_mutex_unlock(&c->lock);
if (r) {
@@ -820,6 +880,18 @@ unsigned int rt_lx_async_outstanding(struct rt_lx_client *c)
return n;
}
unsigned int rt_lx_async_workers(struct rt_lx_client *c)
{
unsigned int n;
if (!c)
return 0;
pthread_mutex_lock(&c->lock);
n = c->workers;
pthread_mutex_unlock(&c->lock);
return n;
}
/* ---- mmap ---- */
int rt_lx_bar_of(struct pci_dev *pdev, uint64_t bus, uint64_t bytes, uint32_t *bar,
@@ -1185,6 +1257,16 @@ void rt_lx_client_destroy(struct rt_lx_client *c)
a->done = NULL;
}
async_reap_locked(c);
/* No call is queued (calls counts them): the workers leave. */
pthread_cond_broadcast(&c->work);
for (unsigned int i = 0; i < c->workers; i++) {
pthread_t thread = c->worker_threads[i];
pthread_mutex_unlock(&c->lock);
pthread_join(thread, NULL);
pthread_mutex_lock(&c->lock);
}
c->workers = 0;
while (c->maps) {
struct lx_map *m = c->maps;
@@ -1198,6 +1280,7 @@ void rt_lx_client_destroy(struct rt_lx_client *c)
linuxu_process_exit(c->proc);
mmu_notifier_synchronize();
pthread_mutex_destroy(&c->arena);
pthread_cond_destroy(&c->work);
pthread_cond_destroy(&c->changed);
pthread_mutex_destroy(&c->lock);
kfree(c);
+141
View File
@@ -0,0 +1,141 @@
/* Hop timing of Linux-file requests (rt/lx_timing.h). */
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <linux/device.h>
#include <linux/rcupdate.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/sysfs.h>
#include <rt/lx_timing.h>
#include <rt/task.h>
struct hop_total {
uint64_t count, ns;
};
static struct hop_total timing[256][RT_LX_HOP_COUNT];
static const char *const hop_names[RT_LX_HOP_COUNT] = {
"admit", "args", "frame", "pages", "copyin", "enter", "ioctl",
"leave", "copyout", "release", "finish", "reply", "total", "spawn",
};
uint64_t rt_lx_time_ns(void)
{
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
}
void rt_lx_timing_add(uint32_t cmd, enum rt_lx_hop hop, uint64_t ns)
{
struct hop_total *t;
if ((unsigned int)hop >= RT_LX_HOP_COUNT)
return;
t = &timing[cmd & 0xff][hop];
__atomic_add_fetch(&t->count, 1, __ATOMIC_RELAXED);
__atomic_add_fetch(&t->ns, ns, __ATOMIC_RELAXED);
}
/* The runtime's primitives, timed where they run (the dext's DriverKit
* implementations, or the host's): each line is the mean of a loop. */
#define PRIMITIVE_LOOPS 2000
static volatile uintptr_t primitive_sink;
static long show_primitives(char *buf, unsigned long size)
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static DEFINE_SPINLOCK(spin);
struct { const char *name; uint64_t ns; } r[8];
unsigned int k = 0;
uint64_t t;
long len = 0;
#define TIME_LOOP(label, body) do { \
t = rt_lx_time_ns(); \
for (int i = 0; i < PRIMITIVE_LOOPS; i++) { body; } \
r[k].name = label; r[k].ns = (rt_lx_time_ns() - t) / PRIMITIVE_LOOPS; k++; \
} while (0)
TIME_LOOP("clock", primitive_sink += rt_lx_time_ns());
TIME_LOOP("malloc_free_64", { void *p = malloc(64); primitive_sink += (uintptr_t)p; free(p); });
TIME_LOOP("kzalloc_kfree_16k", { void *p = kzalloc(16384, GFP_KERNEL); primitive_sink += (uintptr_t)p; kfree(p); });
TIME_LOOP("mutex", { pthread_mutex_lock(&mutex); pthread_mutex_unlock(&mutex); });
TIME_LOOP("spinlock", { spin_lock(&spin); spin_unlock(&spin); });
TIME_LOOP("rcu_read", { rcu_read_lock(); rcu_read_unlock(); });
TIME_LOOP("current", primitive_sink += (uintptr_t)linuxu_current_task_peek());
TIME_LOOP("errno", primitive_sink += (uintptr_t)errno);
#undef TIME_LOOP
for (unsigned int i = 0; i < k; i++) {
int n = snprintf(buf + len, size - (unsigned long)len, "%s%s=%llu", i ? " " : "# primitives ns: ",
r[i].name, (unsigned long long)r[i].ns);
if (n < 0 || (unsigned long)n >= size - (unsigned long)len)
return len;
len += n;
}
if ((unsigned long)len + 1 < size)
buf[len++] = '\n';
return len;
}
long rt_lx_timing_show(char *buf, unsigned long size)
{
unsigned long len = 0;
int n;
len = (unsigned long)show_primitives(buf, size);
n = snprintf(buf + len, size - len, "# nr count: hop=mean_ns ...\n");
if (n < 0 || (unsigned long)n >= size - len)
return (long)len;
len += (unsigned long)n;
for (unsigned int nr = 0; nr < 256; nr++) {
uint64_t count = 0;
for (unsigned int h = 0; h < RT_LX_HOP_COUNT; h++) {
uint64_t c = __atomic_load_n(&timing[nr][h].count, __ATOMIC_RELAXED);
if (c > count)
count = c;
}
if (!count)
continue;
n = snprintf(buf + len, size - len, "0x%02x %llu:", nr, (unsigned long long)count);
if (n < 0 || (unsigned long)n >= size - len)
break;
len += (unsigned long)n;
for (unsigned int h = 0; h < RT_LX_HOP_COUNT; h++) {
const uint64_t c = __atomic_load_n(&timing[nr][h].count, __ATOMIC_RELAXED);
const uint64_t ns = __atomic_load_n(&timing[nr][h].ns, __ATOMIC_RELAXED);
if (!c)
continue;
n = snprintf(buf + len, size - len, " %s=%llu", hop_names[h],
(unsigned long long)(ns / c));
if (n < 0 || (unsigned long)n >= size - len)
return (long)len;
len += (unsigned long)n;
}
n = snprintf(buf + len, size - len, "\n");
if (n < 0 || (unsigned long)n >= size - len)
break;
len += (unsigned long)n;
}
return (long)len;
}
static ssize_t mlg_lx_timing_show(struct device *dev, struct device_attribute *attr, char *buf)
{
(void)dev;
(void)attr;
return rt_lx_timing_show(buf, PAGE_SIZE);
}
static DEVICE_ATTR_RO(mlg_lx_timing);
void rt_lx_timing_register(struct device *dev)
{
/* -EEXIST once it is there: the first client of the device adds it. */
if (dev)
(void)device_create_file(dev, &dev_attr_mlg_lx_timing);
}
+62 -18
View File
@@ -1,6 +1,7 @@
/* Cross-thread RCU grace periods for the one-device DriverKit runtime. */
#include <pthread.h>
#include <stdint.h>
#include <time.h>
#include <linux/rcupdate.h>
#include <rt/fatal.h>
@@ -33,34 +34,62 @@ static uintptr_t rcu_tls_get(void) { return rcu_tls_state; }
static void rcu_tls_set(uintptr_t value) { rcu_tls_state = value; }
#endif
/* TLS state packs nesting in bits 1.. and the reader epoch in bit 0. */
/* TLS state packs nesting in bits 1.. and the reader epoch in bit 0.
*
* Readers take no lock: an outermost rcu_read_lock counts itself in its
* epoch's counter and checks the epoch did not flip meanwhile (else it
* moves to the new one), and rcu_read_unlock uncounts itself. A grace
* period flips the epoch and waits for the old epoch's counter to drain;
* a reader that empties a counter wakes it only when one is waiting.
* With sequentially consistent atomics on both sides, a reader the grace
* period's check missed sees the flip in its own recheck. Readers are on
* every fence, syncobj and scheduler path, so they must not serialize on a
* mutex or signal a condition per call. */
static pthread_mutex_t rcu_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t rcu_changed = PTHREAD_COND_INITIALIZER;
static pthread_cond_t rcu_pending = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
static unsigned int rcu_epoch;
static uint64_t rcu_readers[2];
static unsigned int rcu_gp_waiting;
static struct rcu_head *rcu_first;
static struct rcu_head *rcu_last;
static uint64_t rcu_enqueued;
static uint64_t rcu_completed;
static int rcu_worker_started;
static int rcu_worker_idle;
static pthread_t rcu_worker;
/* Callbacks wait this long before their grace period, so the ones queued
* meanwhile share it (and the worker's wakeup). Linux batches the same. */
#define RCU_BATCH_NS 1000000ull
void rcu_read_lock(void)
{
uintptr_t state = rcu_tls_get();
uintptr_t nesting = state >> 1;
unsigned int epoch;
if (nesting) {
if (nesting == (UINTPTR_MAX >> 1))
LINUXU_FATAL("rcu_read_lock nesting overflow");
rcu_tls_set(((nesting + 1) << 1) | (state & 1));
return;
}
pthread_mutex_lock(&rcu_lock);
rcu_readers[rcu_epoch]++;
rcu_tls_set(((uintptr_t)1 << 1) | rcu_epoch);
pthread_mutex_unlock(&rcu_lock);
for (;;) {
epoch = __atomic_load_n(&rcu_epoch, __ATOMIC_SEQ_CST);
__atomic_add_fetch(&rcu_readers[epoch], 1, __ATOMIC_SEQ_CST);
if (__atomic_load_n(&rcu_epoch, __ATOMIC_SEQ_CST) == epoch)
break;
/* Flipped meanwhile: count in the new epoch instead. */
if (!__atomic_sub_fetch(&rcu_readers[epoch], 1, __ATOMIC_SEQ_CST) &&
__atomic_load_n(&rcu_gp_waiting, __ATOMIC_SEQ_CST)) {
pthread_mutex_lock(&rcu_lock);
pthread_cond_broadcast(&rcu_changed);
pthread_mutex_unlock(&rcu_lock);
}
}
rcu_tls_set(((uintptr_t)1 << 1) | epoch);
}
void rcu_read_unlock(void)
@@ -68,6 +97,8 @@ void rcu_read_unlock(void)
uintptr_t state = rcu_tls_get();
uintptr_t nesting = state >> 1;
unsigned int epoch = (unsigned int)(state & 1);
uint64_t left;
if (!nesting)
LINUXU_FATAL("rcu_read_unlock without rcu_read_lock");
if (nesting > 1) {
@@ -75,16 +106,14 @@ void rcu_read_unlock(void)
return;
}
rcu_tls_set(0);
pthread_mutex_lock(&rcu_lock);
if (!rcu_readers[epoch]) {
/* Do not park with the grace-period lock held. */
pthread_mutex_unlock(&rcu_lock);
left = __atomic_sub_fetch(&rcu_readers[epoch], 1, __ATOMIC_SEQ_CST);
if (left == UINT64_MAX)
LINUXU_FATAL("rcu reader count underflow");
}
rcu_readers[epoch]--;
if (!rcu_readers[epoch])
if (!left && __atomic_load_n(&rcu_gp_waiting, __ATOMIC_SEQ_CST)) {
pthread_mutex_lock(&rcu_lock);
pthread_cond_broadcast(&rcu_changed);
pthread_mutex_unlock(&rcu_lock);
pthread_mutex_unlock(&rcu_lock);
}
}
int rcu_read_lock_count(void) { return (int)(rcu_tls_get() >> 1); }
@@ -100,10 +129,15 @@ static void rcu_grace_period(void)
pthread_mutex_lock(&rcu_lock);
head = rcu_first;
rcu_first = rcu_last = NULL;
old = rcu_epoch;
rcu_epoch ^= 1;
while (rcu_readers[old])
old = __atomic_load_n(&rcu_epoch, __ATOMIC_SEQ_CST);
__atomic_store_n(&rcu_epoch, old ^ 1, __ATOMIC_SEQ_CST);
/* Before the check below: a reader that drains the counter after it
* sees this and wakes the wait (it takes rcu_lock, held until the
* wait releases it). */
__atomic_store_n(&rcu_gp_waiting, 1, __ATOMIC_SEQ_CST);
while (__atomic_load_n(&rcu_readers[old], __ATOMIC_SEQ_CST))
pthread_cond_wait(&rcu_changed, &rcu_lock);
__atomic_store_n(&rcu_gp_waiting, 0, __ATOMIC_SEQ_CST);
pthread_mutex_unlock(&rcu_lock);
while (head) {
next = head->next;
@@ -121,9 +155,17 @@ static void *rcu_worker_main(void *unused)
{
(void)unused;
for (;;) {
struct timespec batch = { 0, (long)RCU_BATCH_NS };
pthread_mutex_lock(&rcu_lock);
while (!rcu_first)
while (!rcu_first) {
rcu_worker_idle = 1;
pthread_cond_wait(&rcu_pending, &rcu_lock);
rcu_worker_idle = 0;
}
/* Let the callbacks queued in the next moment share this grace
* period. A barrier or synchronize_rcu runs its own. */
(void)pthread_cond_timedwait_relative_np(&rcu_pending, &rcu_lock, &batch);
pthread_mutex_unlock(&rcu_lock);
rcu_grace_period();
}
@@ -164,7 +206,9 @@ void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *head))
rcu_enqueued++;
/* Startup failure leaves callbacks queued for a later retry or barrier. */
(void)rcu_start_worker_locked();
pthread_cond_signal(&rcu_pending);
/* Only an idle worker needs the wake; a busy one takes this batch next. */
if (rcu_worker_idle)
pthread_cond_signal(&rcu_pending);
pthread_mutex_unlock(&rcu_lock);
}
+9 -1
View File
@@ -488,12 +488,20 @@ static int notify_cond(pthread_cond_t *c)
int error = cond_for(c, &state);
if (error)
return error;
unsigned int waiters;
cond_lock(state);
/* All waiters may observe this generation. Extra wakes are permitted;
* callers must recheck their predicate while holding their mutex. */
state->generation++;
waiters = state->waiters;
cond_unlock(state);
(void)dext_cond_wake(&state->sleepers, true);
/* A waiter counts itself and reads the generation in one cond_lock
* section: one that comes after this saw the new generation and does
* not sleep. With none, the wake (a hop onto the sleep queue) is
* skipped; signals with nobody waiting are the common case. */
if (waiters)
(void)dext_cond_wake(&state->sleepers, true);
return 0;
}
+14
View File
@@ -77,6 +77,7 @@ static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
static bool queue_missing;
static atomic_int queue_sleeps;
static atomic_int queue_wakes;
extern unsigned long dext_cond_polled;
int dext_cond_block(void **sleepers, bool (*still)(void *), void (*release)(void *), void *arg,
uint64_t deadline_ns)
@@ -115,6 +116,7 @@ int dext_cond_wake(void **sleepers, bool all)
{
if (queue_missing)
return ENOTSUP;
atomic_fetch_add(&queue_wakes, 1);
pthread_mutex_lock(&queue_lock);
while (*sleepers) {
struct mock_sleeper *sleeper = *sleepers;
@@ -300,6 +302,18 @@ int main(void)
test_condition();
/* Waits slept on the queue: none polled. */
assert(dext_cond_polled == 0 && atomic_load(&queue_sleeps) > 0);
{
/* Signals with nobody waiting do not hop onto the sleep queue. */
pthread_cond_t idle = PTHREAD_COND_INITIALIZER;
const int wakes = atomic_load(&queue_wakes);
for (int i = 0; i < 100; i++) {
assert(DK(pthread_cond_signal)(&idle) == 0);
assert(DK(pthread_cond_broadcast)(&idle) == 0);
}
assert(atomic_load(&queue_wakes) == wakes);
assert(DK(pthread_cond_destroy)(&idle) == 0);
}
/* Repeat the real wait/signal/broadcast/timeout test using embedded
* condition state after its first IOMalloc fails. */
ready_lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
+41
View File
@@ -33,6 +33,7 @@
#include <drm/drm_ioctl.h>
#include <drm/amdgpu_drm.h>
#include <rt/lx_files.h>
#include <rt/lx_timing.h>
extern int usleep(unsigned int usec);
@@ -45,6 +46,20 @@ extern int usleep(unsigned int usec);
#define FIXTURE_BAR2_LEN (2ULL << 20)
static int opens, releases;
/* The device files the runtime adds (rt_lx_timing_register). */
static const struct device_attribute *timing_attr;
static struct device *timing_dev;
static int timing_registrations;
int device_create_file(struct device *dev, const struct device_attribute *attr)
{
if (timing_attr && timing_dev == dev && attr == timing_attr)
return -EEXIST;
timing_dev = dev;
timing_attr = attr;
timing_registrations++;
return 0;
}
static DECLARE_WAIT_QUEUE_HEAD(syncobj_wq);
static uint32_t signaled; /* syncobj handles <= this are signaled */
static int waiters;
@@ -550,6 +565,17 @@ int main(void)
}
CHECK(s->w[MLG_LX_MAX_ASYNC].first_signaled == 99);
CHECK(rt_lx_async_outstanding(a) == 0);
/* One worker per call that overlapped; they stay for later
* calls, which start no more. */
CHECK(rt_lx_async_workers(a) == MLG_LX_MAX_ASYNC);
for (unsigned i = 0; i < 4 * MLG_LX_MAX_ASYNC; ++i) {
struct async_call one = { 0 };
CHECK(!start_async(a, fd_a, DRM_IOCTL_SYNCOBJ_WAIT, &s->w[0], &one));
wait_done(&one);
CHECK(one.result == 0);
}
CHECK(rt_lx_async_workers(a) == MLG_LX_MAX_ASYNC && rt_lx_async_outstanding(a) == 0);
free(calls);
free(s);
__atomic_store_n(&signaled, 0, __ATOMIC_SEQ_CST);
@@ -630,6 +656,21 @@ int main(void)
CHECK(releases == 2); /* a's render file; b's stays open */
free(call);
}
/* Hop timing: the device's mlg_lx_timing file, added once, shows the
* requests made above by number with each hop's mean. */
{
static char text[16384];
char version[64];
CHECK(timing_registrations == 1 && timing_dev == &pdev.dev && timing_attr &&
!strcmp(timing_attr->attr.name, "mlg_lx_timing"));
CHECK(timing_attr->show(&pdev.dev, (struct device_attribute *)timing_attr, text) > 0);
CHECK(strstr(text, "# primitives ns: clock="));
snprintf(version, sizeof(version), "\n0x%02x ", (unsigned int)(DRM_IOCTL_VERSION & 0xff));
const char *line = strstr(text, version);
CHECK(line && strstr(line, " pages=") && strstr(line, " ioctl=") && strstr(line, " release="));
CHECK(rt_lx_timing_show(text, 8) < 8); /* bounded */
}
rt_lx_client_destroy(b);
CHECK(releases == 3 && opens == 3);
unregister_chrdev(DRM_MAJOR, "drm");
+79
View File
@@ -83,6 +83,84 @@ static void *producer(void *opaque)
return NULL;
}
/* Lockless readers against grace periods: readers dereference the
* published object while writers replace it and free the old one after a
* grace period (call_rcu or synchronize_rcu), poisoning it first. A reader
* that ever sees poison held a reference across a grace period. */
struct boxed {
struct rcu_head head;
uint64_t value;
};
#define BOX_POISON 0xdeaddeaddeaddeadull
static struct boxed *published_box;
static int stress_stop;
static uint64_t stress_reads;
static void box_free(struct rcu_head *head)
{
struct boxed *b = container_of(head, struct boxed, head);
__atomic_store_n(&b->value, BOX_POISON, __ATOMIC_SEQ_CST);
free(b);
}
static void *stress_reader(void *unused)
{
(void)unused;
while (!__atomic_load_n(&stress_stop, __ATOMIC_SEQ_CST)) {
rcu_read_lock();
struct boxed *b = rcu_dereference(published_box);
for (int spin = 0; spin < 64; spin++)
assert(__atomic_load_n(&b->value, __ATOMIC_SEQ_CST) != BOX_POISON);
rcu_read_lock(); /* nested sections keep the outer epoch */
assert(__atomic_load_n(&b->value, __ATOMIC_SEQ_CST) != BOX_POISON);
rcu_read_unlock();
rcu_read_unlock();
__atomic_add_fetch(&stress_reads, 1, __ATOMIC_RELAXED);
}
return NULL;
}
static void *stress_writer(void *arg)
{
const int synchronous = (int)(intptr_t)arg;
for (int i = 0; i < 2000; i++) {
struct boxed *b = calloc(1, sizeof(*b));
assert(b);
b->value = (uint64_t)i;
struct boxed *old = __atomic_exchange_n(&published_box, b, __ATOMIC_SEQ_CST);
if (synchronous) {
synchronize_rcu();
box_free(&old->head);
} else {
call_rcu(&old->head, box_free);
}
}
return NULL;
}
static void stress_lockless_readers(void)
{
pthread_t readers[6], writers[2];
struct boxed *first = calloc(1, sizeof(*first));
assert(first);
rcu_assign_pointer(published_box, first);
for (int i = 0; i < 6; i++)
assert(pthread_create(&readers[i], NULL, stress_reader, NULL) == 0);
for (int i = 0; i < 2; i++)
assert(pthread_create(&writers[i], NULL, stress_writer, (void *)(intptr_t)i) == 0);
for (int i = 0; i < 2; i++)
assert(pthread_join(writers[i], NULL) == 0);
__atomic_store_n(&stress_stop, 1, __ATOMIC_SEQ_CST);
for (int i = 0; i < 6; i++)
assert(pthread_join(readers[i], NULL) == 0);
rcu_barrier();
assert(__atomic_load_n(&stress_reads, __ATOMIC_RELAXED) > 0);
free(published_box);
}
int main(void)
{
pthread_t r, s, producers[4];
@@ -134,5 +212,6 @@ int main(void)
assert(__atomic_load_n(&freed, __ATOMIC_SEQ_CST) == 801);
rcu_barrier();
synchronize_rcu();
stress_lockless_readers();
return 0;
}
+1 -1
View File
@@ -32,7 +32,7 @@ amdkfd=third_party/linux/drivers/gpu/drm/amd/amdkfd
sources=(linuxu/tests/test_kfd_power.c linuxu/tests/kfd_session_fixture.c
linuxu/src/amdgpu-rt/power.c linuxu/src/amdgpu-rt/tmpring_gc12.c
linuxu/src/amdgpu-rt/kfd_session.c linuxu/src/amdgpu-rt/process_file.c
linuxu/src/amdgpu-rt/lx_files.c linuxu/src/amdgpu-rt/lx_frame.c linuxu/src/amdgpu-rt/lx_describe.c
linuxu/src/amdgpu-rt/lx_files.c linuxu/src/amdgpu-rt/lx_frame.c linuxu/src/amdgpu-rt/lx_describe.c linuxu/src/amdgpu-rt/lx_timing.c
$amdkfd/kfd_device.c
$amdkfd/kfd_chardev.c $amdkfd/kfd_process.c $amdkfd/kfd_process_queue_manager.c
$amdkfd/kfd_device_queue_manager.c $amdkfd/kfd_device_queue_manager_v12.c
+1 -1
View File
@@ -33,7 +33,7 @@ cflags=("${hostcflags[@]}" -DRT_KFD_COPY_TIMEOUT_MS=200 -DRT_KFD_SETTLE_MS=200
sources=(linuxu/tests/test_kfd_session.c linuxu/tests/kfd_session_fixture.c
linuxu/src/amdgpu-rt/tmpring_gc12.c
linuxu/src/amdgpu-rt/kfd_session.c linuxu/src/amdgpu-rt/process_file.c
linuxu/src/amdgpu-rt/lx_files.c linuxu/src/amdgpu-rt/lx_frame.c linuxu/src/amdgpu-rt/lx_describe.c
linuxu/src/amdgpu-rt/lx_files.c linuxu/src/amdgpu-rt/lx_frame.c linuxu/src/amdgpu-rt/lx_describe.c linuxu/src/amdgpu-rt/lx_timing.c
third_party/linux/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c third_party/linux/drivers/gpu/drm/amd/amdkfd/kfd_process.c
third_party/linux/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c
third_party/linux/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c third_party/linux/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager_v12.c
+1 -1
View File
@@ -10,7 +10,7 @@ trap 'rm -rf "$test_dir"' EXIT
flags=(-w -std=gnu11 -g -O1 -fsanitize=address,undefined
-fno-sanitize-recover=all -ffunction-sections -fdata-sections -Ilinuxu/headers)
clang "${flags[@]}" linuxu/tests/test_lx_files.c \
linuxu/src/amdgpu-rt/{lx_files,lx_frame,lx_describe}.c \
linuxu/src/amdgpu-rt/{lx_files,lx_frame,lx_describe,lx_timing}.c \
linuxu/src/shims/{process,task,kthread,fd,chrdev,module,dma_fence,timekeeping,printk}.c \
third_party/linux/lib/rbtree.c \
linuxu/src/mm/{mm,mmu_notifier,uaccess,page}.c linuxu/src/kmem/slab.c linuxu/src/dart/dart.c \