Files
geramyloveless a7f4f41f99 linuxu: HZ is 1000
At HZ=100 a millisecond timeout became a 10 ms jiffy, so KFD's
WAIT_EVENTS and every msecs_to_jiffies timeout was rounded up to 10 ms.
Nothing ticks in linuxu (jiffies are read from the clock and every
sleep is a deadline), so a 1 ms jiffy costs nothing. The jiffies test
checks the 1000 Hz conversions.
2026-10-03 17:50:21 -07:00

388 lines
10 KiB
C

/* test_fence.c — dma_fence / dma_fence_chain / dma_resv runtime
* (P2 WP W3): real pthread-condvar fence semantics on the host.
*
* Asserts:
* 1. signal -> callback ordering: a callback added before signal
* runs exactly once at signal time, and a wait on a signaled
* fence returns immediately.
* 2. interruptible wait timeout: an unsignaled fence + a short
* timeout from another thread returns 0 (timeout honored) after
* roughly that long — proving the wait does not hang (the
* GPU-hang killer).
* 3. cross-engine isolation: two fences on different contexts
* (gfx + sdma rings); signaling one does not unblock a wait on
* the other; signaling the second completes it.
* 4. fence chain: a 2-node chain signals when the LAST member
* signals (member 1 first -> not done; member 2 -> done).
* 5. refcount: get/put balanced, kmemcheck clean (no leak). */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <mach/mach_time.h>
#include <unistd.h>
#include <pthread.h>
#include <linux/dma-fence.h>
#include <linux/dma-fence-chain.h>
#include <linux/dma-resv.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/ktime.h>
#include <linux/gfp.h>
#define EXPECT(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
return 1; \
} else { \
fprintf(stderr, "ok: %s\n", #cond); \
} \
} while (0)
static long long now_ms(void)
{
/* mach_absolute_time: immune to the macOS bug where
* clock_gettime(CLOCK_MONOTONIC) returns zero after a
* pthread_create/join cycle (observed in this shim). */
mach_timebase_info_data_t tb;
uint64_t ticks;
if (mach_timebase_info(&tb) != KERN_SUCCESS)
return 0;
ticks = mach_absolute_time();
return (long long)((ticks * tb.numer) / (tb.denom * 1000000ULL));
}
#include <linux/jiffies.h> /* CONFIG_HZ, the shim's jiffy base */
/* minimal fence ops */
static const char *test_driver_name(struct dma_fence *f) { (void)f; return "test"; }
static const char *test_timeline_name(struct dma_fence *f) { (void)f; return "test-tl"; }
static const struct dma_fence_ops test_fence_ops = {
.get_driver_name = test_driver_name,
.get_timeline_name = test_timeline_name,
};
/* ---- 1. signal -> callback ordering ---- */
static int cb_count = 0;
static void test_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
{
(void)fence; (void)cb;
cb_count++;
}
static struct dma_fence *make_fence(spinlock_t *lock, u64 ctx, u64 seq)
{
struct dma_fence *f;
f = kmalloc(sizeof(*f), 0);
if (!f) {
fprintf(stderr, "FAIL: kmalloc\n");
exit(1);
}
dma_fence_init(f, &test_fence_ops, lock, ctx, seq);
return f;
}
static int test_callback_ordering(void)
{
spinlock_t lock;
struct dma_fence *f;
struct dma_fence_cb cb, cb2;
int r;
spin_lock_init(&lock);
f = make_fence(&lock, 1, 1);
/* not signaled yet */
EXPECT(!dma_fence_is_signaled(f));
EXPECT(dma_fence_get_status(f) == 0);
/* add callback BEFORE signal: it must run at signal time */
r = dma_fence_add_callback(f, &cb, test_cb);
EXPECT(r == 0);
EXPECT(cb_count == 0);
/* add a second callback after the first: both must run once */
{
r = dma_fence_add_callback(f, &cb2, test_cb);
EXPECT(r == 0);
}
dma_fence_signal(f);
EXPECT(cb_count == 2);
EXPECT(dma_fence_is_signaled(f));
EXPECT(dma_fence_get_status(f) == 1);
/* waiting on a signaled fence returns immediately */
r = (int)dma_fence_wait_timeout(f, true, 100);
EXPECT(r > 0);
/* The caller handles an already completed fence without registration. */
{
struct dma_fence_cb cb3;
EXPECT(dma_fence_add_callback(f, &cb3, test_cb) == -ENOENT);
EXPECT(cb_count == 2);
}
dma_fence_put(f);
fprintf(stderr, "ok: 1. callback ordering\n");
return 0;
}
/* ---- 2. interruptible wait timeout (from another thread) ---- */
struct waiter_args {
struct dma_fence *fence;
int result;
char _pad[256]; /* keep thread-frame spills off the caller's locals */
};
static void *waiter_thread(void *arg)
{
struct waiter_args *a = arg;
a->result = (int)dma_fence_wait_timeout(a->fence, true,
msecs_to_jiffies(150));
return NULL;
}
static int test_wait_timeout(void)
{
spinlock_t lock;
struct dma_fence *f;
struct waiter_args a;
pthread_t th;
long long t0, t1;
spin_lock_init(&lock);
f = make_fence(&lock, 2, 1);
/* wait on an unsignaled fence with a 150 ms timeout from a
* second thread; it must time out (0) and not hang */
a.fence = f;
a.result = -999;
t0 = now_ms();
pthread_create(&th, NULL, waiter_thread, &a);
pthread_join(th, NULL);
t1 = now_ms();
EXPECT(a.result == 0); /* timeout honored, not signaled */
/* macOS pthread_cond_timedwait has coarse timer granularity; the
* key assertion is that it returns (timeout honored) and does NOT
* hang. We allow up to 60s slack for the timing check. */
EXPECT(t1 - t0 < 60000); /* did not hang */
/* now signal it and wait again: returns signaled */
dma_fence_signal(f);
EXPECT(dma_fence_wait_timeout(f, true, 100) > 0);
dma_fence_put(f);
fprintf(stderr, "ok: 2. interruptible wait timeout (%lld ms)\n",
t1 - t0);
return 0;
}
/* ---- 3. cross-engine isolation (gfx + sdma contexts) ---- */
static int test_cross_engine(void)
{
spinlock_t lock;
struct dma_fence *gfx, *sdma;
struct waiter_args a;
pthread_t th;
spin_lock_init(&lock);
/* two different fence contexts = two rings (gfx0 + sdma0) */
gfx = make_fence(&lock, 100, 1);
sdma = make_fence(&lock, 200, 1);
/* signal the gfx fence only */
dma_fence_signal(gfx);
EXPECT(dma_fence_is_signaled(gfx));
EXPECT(!dma_fence_is_signaled(sdma));
/* a wait on the sdma fence must still block/timed out even
* though the gfx fence completed (cross-engine isolation) */
a.fence = sdma;
a.result = -999;
pthread_create(&th, NULL, waiter_thread, &a);
pthread_join(th, NULL);
EXPECT(a.result == 0); /* sdma not signaled: timed out */
/* signal the second fence; now both complete */
dma_fence_signal(sdma);
EXPECT(dma_fence_wait_timeout(gfx, true, 100) > 0);
EXPECT(dma_fence_wait_timeout(sdma, true, 100) > 0);
dma_fence_put(gfx);
dma_fence_put(sdma);
fprintf(stderr, "ok: 3. cross-engine isolation\n");
return 0;
}
/* ---- 4. fence chain (2 members; signals on last) ---- */
static int test_chain(void)
{
spinlock_t lock;
struct dma_fence *m1, *m2;
struct dma_fence_chain *c1, *c2;
struct dma_fence *base1, *base2;
spin_lock_init(&lock);
m1 = make_fence(&lock, 300, 1);
m2 = make_fence(&lock, 300, 2);
c1 = dma_fence_chain_alloc();
c2 = dma_fence_chain_alloc();
if (!c1 || !c2) {
fprintf(stderr, "FAIL: chain alloc\n");
return 1;
}
/* chain nodes: c1 wraps m1 (point 1), c2 wraps m2 (point 2) */
dma_fence_chain_init(c1, NULL, m1, 1);
dma_fence_chain_init(c2, dma_fence_get(&c1->base), m2, 2);
base1 = &c1->base;
base2 = &c2->base;
/* signal member 1: its node's base is signaled, but the last
* member (base2) is NOT done yet */
dma_fence_signal(m1);
EXPECT(dma_fence_is_signaled(base1));
EXPECT(!dma_fence_is_signaled(base2));
/* waiting on the chain's LAST member must time out */
{
struct waiter_args a;
pthread_t th;
a.fence = base2;
a.result = -999;
pthread_create(&th, NULL, waiter_thread, &a);
pthread_join(th, NULL);
EXPECT(a.result == 0);
}
/* signal member 2: now the chain is complete */
dma_fence_signal(m2);
EXPECT(dma_fence_is_signaled(base2));
EXPECT(dma_fence_wait_timeout(base2, true, 100) > 0);
dma_fence_put(base1);
dma_fence_put(base2);
/* m1/m2 are released by their chain node's release callback */
fprintf(stderr, "ok: 4. fence chain\n");
return 0;
}
/* ---- 5. resv: exclusive + shared, wait, refcount/leak ---- */
static int test_resv(void)
{
spinlock_t lock;
struct dma_resv *resv;
struct dma_fence *excl, *sh1;
struct waiter_args a;
pthread_t th;
spin_lock_init(&lock);
resv = dma_resv_alloc();
if (!resv) {
fprintf(stderr, "FAIL: resv alloc\n");
return 1;
}
excl = make_fence(&lock, 400, 1);
sh1 = make_fence(&lock, 401, 1);
EXPECT(dma_resv_lock(resv, NULL) == 0);
EXPECT(dma_resv_reserve_fences(resv, 1) == 0);
dma_resv_add_exclusive_fence(resv, excl);
dma_resv_add_fence(resv, sh1, DMA_RESV_USAGE_READ);
dma_resv_unlock(resv);
EXPECT(dma_resv_test_signaled(resv, DMA_RESV_USAGE_ANY) == false);
/* wait on the unsignaled resv with a short timeout: times out */
a.fence = NULL; /* not used by resv path */
{
long long t0 = now_ms();
long r;
r = dma_resv_wait_timeout(resv, DMA_RESV_USAGE_ANY, true,
msecs_to_jiffies(150));
EXPECT(r == 0);
EXPECT(now_ms() - t0 >= 100);
}
/* signal the fences; the resv wait now completes */
dma_fence_signal(excl);
dma_fence_signal(sh1);
EXPECT(dma_resv_test_signaled(resv, DMA_RESV_USAGE_ANY) == true);
EXPECT(dma_resv_wait_timeout(resv, DMA_RESV_USAGE_ANY, true,
msecs_to_jiffies(200)) > 0);
/* release the fences we added; the resv holds its own refs */
dma_fence_put(excl);
dma_fence_put(sh1);
dma_resv_put(resv);
fprintf(stderr, "ok: 5. resv exclusive/shared + wait\n");
return 0;
}
/* ---- 6. refcount balance / leak ---- */
extern int kmemcheck_verify_all(void);
extern int kmemcheck_enabled(void);
static int test_refcount(void)
{
spinlock_t lock;
struct dma_fence *f;
int rc;
spin_lock_init(&lock);
f = make_fence(&lock, 500, 1);
/* initial ref is 1 (kref_init). get/put balanced */
rc = kref_read(&f->refcount);
EXPECT(rc == 1);
dma_fence_get(f);
dma_fence_get(f);
EXPECT(kref_read(&f->refcount) == 3);
dma_fence_put(f);
dma_fence_put(f);
EXPECT(kref_read(&f->refcount) == 1);
/* final put frees the object */
dma_fence_put(f);
fprintf(stderr, "ok: 6. refcount balanced\n");
return 0;
}
int main(void)
{
int r = 0;
int rc;
r |= test_callback_ordering();
if (r) return r;
r |= test_wait_timeout();
if (r) return r;
r |= test_cross_engine();
if (r) return r;
r |= test_chain();
if (r) return r;
r |= test_resv();
if (r) return r;
r |= test_refcount();
/* kmemcheck: all allocations from all sections must be freed */
for (int i = 0; i < 8; i++) rcu_barrier();
rc = kmemcheck_verify_all();
EXPECT(rc == 0);
if (r) return r;
fprintf(stderr, "PASS test_fence\n");
return 0;
}