From 3e17966353ec2afc6cfdc8339d116c1d915dd553 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sun, 27 Sep 2026 07:05:15 -0500 Subject: [PATCH] fix(m19r): add nvrm_free with VA reuse and monotonic completion wait PHYSICS had no way to release GPU memory (nvrm_alloc but no nvrm_free), so every allocation permanently consumed VA space and RM/UVM/CPU mappings for the life of the process. Completion waits also used strict equality (*marker == expected), which is wrong once a marker wraps past 2^32. Adds, additively, without changing any existing function's behavior: - nvrm_free(Nvrm *rm, NvrmMem *m): reverses nvrm_alloc (UVM_FREE of the external range, rm_free of the RM memory object, munmap of the CPU range), returns the VA range to a sorted, coalesced free list that va_take() now checks first (first-fit with splitting) before bumping va_next. Idempotent on an already-zeroed NvrmMem; rejects a stale/ duplicate copy with -1 + rm->err, without touching the driver, via a small live-allocation table (NVRM_MAX_LIVE / NVRM_MAX_FREE = 4096). - m16_native_wait_marker_ge(): waits for (int32_t)(*marker - target) >= 0, a monotonic serial-number compare (RFC 1982 style) that is correct across uint32_t wraparound, instead of exact equality. m16_native_wait_marker() is unchanged. New test-only files: nvrm/lifecycle_gates.c + tests/run_nvrm_lifecycle_gates.sh. Verified on DGX Spark GB10 hardware (under flock /tmp/aien-gb10.lock): tests/run_nvrm_lifecycle_gates.sh [PASS] M16_WAIT_GE_SERIAL [PASS] NVRM_FREE_CHURN (20,000 alloc/touch/free cycles, 442.06 GiB churned, VA high-water delta 117.07 MiB) [PASS] NVRM_FREE_UNMAPS [PASS] NVRM_FREE_IDEMPOTENT_AND_STALE [PASS] NVRM_ALLOC_AFTER_FREE_REUSES_VA [PASS] GPU_SUBMIT_AFTER_CHURN TOTAL: 6 PASSED: 6 FAILED: 0 tests/run_m15_gates.sh -> ALL 10 GATES PASSED tests/run_m16_requalification.sh -> ALL GATES PASS (12/12) omega @ ae2476d2c16beedff6507cbfc36701bb740203de built against this branch (PHYSICS_DIR=...) and run against real hardware: ./build/omegatool --run-m17-gates -> TOTAL GATES: 18 | PASSED: 18 | FAILED: 0 ./build/omegatool --run-m18-gates -> TOTAL GATES: 18 | PASSED: 18 | FAILED: 0 (157 total gates evaluated, 0 failed) Co-authored-by: Drake Stapleton Co-Authored-By: Claude Opus 5.5 --- m16/m16_native.c | 11 ++ m16/m16_native.h | 5 + nvrm/lifecycle_gates.c | 304 ++++++++++++++++++++++++++++++ nvrm/nvrm.c | 112 +++++++++++ nvrm/nvrm.h | 31 +++ tests/run_nvrm_lifecycle_gates.sh | 29 +++ 6 files changed, 492 insertions(+) create mode 100644 nvrm/lifecycle_gates.c create mode 100755 tests/run_nvrm_lifecycle_gates.sh diff --git a/m16/m16_native.c b/m16/m16_native.c index 06e4ad8..d6df905 100644 --- a/m16/m16_native.c +++ b/m16/m16_native.c @@ -86,6 +86,17 @@ int m16_native_wait_marker(volatile uint32_t *marker, uint32_t expected, uint64_ return (*marker == expected) ? 0 : -1; } +int m16_native_wait_marker_ge(volatile uint32_t *marker, uint32_t target, uint64_t timeout_ms) { + if (!marker) return -1; + uint64_t end = now_ns() + timeout_ms * 1000000ull; + while (now_ns() < end) { + if ((int32_t)(*marker - target) >= 0) return 0; + usleep(50); + __asm__ volatile("yield"); + } + return ((int32_t)(*marker - target) >= 0) ? 0 : -1; +} + void m16_native_close(M16NativeContext *ctx) { if (ctx) { nvrm_close(&ctx->rm); diff --git a/m16/m16_native.h b/m16/m16_native.h index 92b92e4..b01d601 100644 --- a/m16/m16_native.h +++ b/m16/m16_native.h @@ -41,6 +41,11 @@ int m16_native_submit_methods(M16NativeContext *ctx, const uint32_t *methods, s /* Poll coherent marker until it equals expected value or timeout_ms expires */ int m16_native_wait_marker(volatile uint32_t *marker, uint32_t expected, uint64_t timeout_ms); +/* Poll coherent marker until (int32_t)(*marker - target) >= 0 (monotonic, + * serial-number compare per RFC 1982: correct across uint32_t wraparound) + * or timeout_ms expires. Returns 0 once satisfied, -1 on timeout. */ +int m16_native_wait_marker_ge(volatile uint32_t *marker, uint32_t target, uint64_t timeout_ms); + /* Free all RM objects, mappings, and close file descriptors */ void m16_native_close(M16NativeContext *ctx); diff --git a/nvrm/lifecycle_gates.c b/nvrm/lifecycle_gates.c new file mode 100644 index 0000000..88817fe --- /dev/null +++ b/nvrm/lifecycle_gates.c @@ -0,0 +1,304 @@ +/* + * lifecycle_gates.c -- M19R NVRM lifecycle qualification gates. + * + * Validates nvrm_free() (VA reuse, live/idempotent/stale detection, CPU + * unmap) and m16_native_wait_marker_ge() (monotonic serial-number wait) + * against the real GB10 hardware and kernel driver. New file: does not + * modify m16_requalify.c / m16_concurrent.c or any existing test. + */ +#include "nvrm.h" +#include "m16_native.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static int g_total = 0, g_passed = 0, g_failed = 0; + +static void gate_result(const char *name, int ok) { + g_total++; + if (ok) { g_passed++; printf("[PASS] %s\n", name); } + else { g_failed++; printf("[FAIL] %s\n", name); } +} + +static uint64_t now_ms(void) { + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return (uint64_t)t.tv_sec * 1000ull + (uint64_t)t.tv_nsec / 1000000ull; +} + +/* ---- NVRM_FREE_CHURN --------------------------------------------------- + * 20,000 alloc/touch/free cycles, sizes cycling 4 KiB..64 MiB, total bytes + * churned must exceed 256 GiB (2x the machine's 128 GiB unified memory -- + * impossible without real nvrm_free() release), and va_next (the VA + * high-water mark) must stay bounded, proving reuse rather than a bump leak. + */ +#define CHURN_CYCLES 20000 + +static int gate_free_churn(Nvrm *rm, uint64_t *out_bytes, uint64_t *out_high_water_delta) { + static const uint64_t sizes[] = { + 4ull * 1024, /* 4 KiB */ + 64ull * 1024, /* 64 KiB */ + 1024ull * 1024, /* 1 MiB */ + 4ull * 1024 * 1024, /* 4 MiB */ + 16ull * 1024 * 1024, /* 16 MiB */ + 32ull * 1024 * 1024, /* 32 MiB */ + 64ull * 1024 * 1024, /* 64 MiB */ + 64ull * 1024 * 1024, /* 64 MiB */ + }; + const int nsizes = (int)(sizeof(sizes) / sizeof(sizes[0])); + uint64_t va_start = rm->va_next; + uint64_t va_high = rm->va_next; + uint64_t total_bytes = 0; + + for (int i = 0; i < CHURN_CYCLES; i++) { + uint64_t size = sizes[i % nsizes]; + NvrmMem m; + if (nvrm_alloc(rm, size, &m) != 0) { + fprintf(stderr, " churn: nvrm_alloc failed at cycle %d (size 0x%lx): %s\n", + i, (unsigned long)size, rm->err); + return -1; + } + if (rm->va_next > va_high) va_high = rm->va_next; + + volatile uint8_t *p = (volatile uint8_t *)m.cpu; + uint8_t first = (uint8_t)(i & 0xff); + uint8_t last = (uint8_t)((i ^ 0xff) & 0xff); + p[0] = first; + p[m.size - 1] = last; + if (p[0] != first || p[m.size - 1] != last) { + fprintf(stderr, " churn: readback mismatch at cycle %d\n", i); + nvrm_free(rm, &m); + return -1; + } + + total_bytes += m.size; + if (nvrm_free(rm, &m) != 0) { + fprintf(stderr, " churn: nvrm_free failed at cycle %d: %s\n", i, rm->err); + return -1; + } + if ((i % 2000) == 0) { + fprintf(stderr, " churn: cycle %d/%d, %" PRIu64 " bytes so far\n", i, CHURN_CYCLES, total_bytes); + } + } + + *out_bytes = total_bytes; + *out_high_water_delta = va_high - va_start; + return 0; +} + +/* ---- NVRM_FREE_UNMAPS --------------------------------------------------- + * After nvrm_free(), the CPU range must no longer be mapped: msync() on it + * must fail with ENOMEM. + */ +static int gate_free_unmaps(Nvrm *rm) { + NvrmMem m; + if (nvrm_alloc(rm, 0x4000, &m) != 0) { + fprintf(stderr, " unmaps: alloc failed: %s\n", rm->err); + return -1; + } + void *va = m.cpu; + size_t size = (size_t)m.size; + + if (nvrm_free(rm, &m) != 0) { + fprintf(stderr, " unmaps: free failed: %s\n", rm->err); + return -1; + } + + errno = 0; + int rc = msync(va, size, MS_ASYNC); + if (rc == 0) { + fprintf(stderr, " unmaps: msync unexpectedly succeeded on freed range %p\n", va); + return -1; + } + if (errno != ENOMEM) { + fprintf(stderr, " unmaps: msync failed with errno %d (%s), expected ENOMEM\n", errno, strerror(errno)); + return -1; + } + return 0; +} + +/* ---- NVRM_FREE_IDEMPOTENT_AND_STALE ------------------------------------ + * Freeing an already-zeroed NvrmMem (double free) is a silent no-op (0, no + * syscalls). Freeing a saved stale copy (pre-free snapshot of a struct that + * has since been freed through another handle) must fail with -1 and must + * not touch the driver. + */ +static int gate_idempotent_and_stale(Nvrm *rm) { + NvrmMem m; + if (nvrm_alloc(rm, 0x2000, &m) != 0) { + fprintf(stderr, " idempotent: alloc failed: %s\n", rm->err); + return -1; + } + NvrmMem stale = m; /* stale copy, snapshotted before the real free */ + + if (nvrm_free(rm, &m) != 0) { + fprintf(stderr, " idempotent: first free failed: %s\n", rm->err); + return -1; + } + if (m.handle != 0 || m.va != 0 || m.size != 0 || m.cpu != NULL) { + fprintf(stderr, " idempotent: nvrm_free did not zero the NvrmMem on success\n"); + return -1; + } + + /* Double free of the now-zeroed struct: must return 0. */ + if (nvrm_free(rm, &m) != 0) { + fprintf(stderr, " idempotent: double free of zeroed struct returned nonzero\n"); + return -1; + } + + /* Free of the stale (already-freed-elsewhere) copy: must return -1. */ + if (nvrm_free(rm, &stale) == 0) { + fprintf(stderr, " idempotent: freeing stale copy unexpectedly succeeded\n"); + return -1; + } + return 0; +} + +/* ---- NVRM_ALLOC_AFTER_FREE_REUSES_VA ------------------------------------ + * Allocate, free, allocate the same size again: must get the same VA back. + */ +static int gate_alloc_after_free_reuses_va(Nvrm *rm) { + NvrmMem a, b; + if (nvrm_alloc(rm, 0x8000, &a) != 0) { + fprintf(stderr, " reuse: first alloc failed: %s\n", rm->err); + return -1; + } + uint64_t va = a.va; + if (nvrm_free(rm, &a) != 0) { + fprintf(stderr, " reuse: free failed: %s\n", rm->err); + return -1; + } + if (nvrm_alloc(rm, 0x8000, &b) != 0) { + fprintf(stderr, " reuse: second alloc failed: %s\n", rm->err); + return -1; + } + int ok = (b.va == va); + if (!ok) { + fprintf(stderr, " reuse: expected va 0x%lx, got 0x%lx\n", + (unsigned long)va, (unsigned long)b.va); + } + nvrm_free(rm, &b); + return ok ? 0 : -1; +} + +/* ---- M16_WAIT_GE_SERIAL -------------------------------------------------- + * Pure CPU unit test, no hardware dependency: synthetic marker + serial + * (RFC 1982 style) comparison against a target that has wrapped past 0. + */ +static int gate_wait_ge_serial(void) { + uint32_t marker; + + /* target 0xFFFFFFF0 satisfied by marker 0x00000005 (wrapped past). */ + marker = 0x00000005u; + if (m16_native_wait_marker_ge(&marker, 0xFFFFFFF0u, 50) != 0) { + fprintf(stderr, " wait_ge: wrapped-past case unexpectedly failed/timed out\n"); + return -1; + } + + /* Not satisfied by 0xFFFFFFE0. Must time out, within timeout_ms + 50ms. */ + marker = 0xFFFFFFE0u; + uint64_t t0 = now_ms(); + int rc = m16_native_wait_marker_ge(&marker, 0xFFFFFFF0u, 100); + uint64_t elapsed = now_ms() - t0; + if (rc == 0) { + fprintf(stderr, " wait_ge: not-yet-satisfied case unexpectedly returned success\n"); + return -1; + } + if (elapsed > 150) { + fprintf(stderr, " wait_ge: timeout took %" PRIu64 "ms, expected <= 150ms\n", elapsed); + return -1; + } + return 0; +} + +/* ---- GPU_SUBMIT_AFTER_CHURN ---------------------------------------------- + * After churn, open a channel and do one real submit + wait (same pattern + * as m16/m16_native.c + m16/m16_requalify.c) to prove the device context + * is still healthy after 20,000 alloc/free cycles. + */ +static int gate_gpu_submit_after_churn(Nvrm *rm) { + if (nvrm_channel(rm) != 0) { + fprintf(stderr, " submit: nvrm_channel failed: %s\n", rm->err); + return -1; + } + NvrmMem pb, marker_mem; + if (nvrm_alloc(rm, 0x1000, &pb) != 0) { + fprintf(stderr, " submit: pushbuffer alloc failed: %s\n", rm->err); + return -1; + } + if (nvrm_alloc(rm, 0x1000, &marker_mem) != 0) { + fprintf(stderr, " submit: marker alloc failed: %s\n", rm->err); + return -1; + } + volatile uint32_t *marker = (volatile uint32_t *)marker_mem.cpu; + *marker = 0; + + uint32_t methods[32]; + int nwords = m16_native_build_release(methods, marker_mem.va, 0x19c0ffee, rm->compute_class); + if (nwords <= 0) { + fprintf(stderr, " submit: build_release failed\n"); + return -1; + } + memcpy(pb.cpu, methods, (size_t)nwords * sizeof(uint32_t)); + __asm__ volatile("dsb sy" ::: "memory"); + + if (nvrm_submit(rm, &pb, 0, (uint32_t)nwords) != 0) { + fprintf(stderr, " submit: nvrm_submit failed: %s\n", rm->err); + return -1; + } + if (m16_native_wait_marker_ge(marker, 0x19c0ffee, 2000) != 0) { + fprintf(stderr, " submit: timeout waiting for marker; actual=0x%08x\n", *marker); + return -1; + } + if (*marker != 0x19c0ffee) { + fprintf(stderr, " submit: unexpected final marker value 0x%08x\n", *marker); + return -1; + } + nvrm_retire(rm, rm->put); + return 0; +} + +int main(void) { + printf("=== M19R NVRM LIFECYCLE GATES ===\n"); + + gate_result("M16_WAIT_GE_SERIAL", gate_wait_ge_serial() == 0); + + Nvrm rm; + if (nvrm_open(&rm) != 0) { + fprintf(stderr, "FATAL: nvrm_open failed: %s\n", rm.err); + gate_result("NVRM_FREE_CHURN", 0); + gate_result("NVRM_FREE_UNMAPS", 0); + gate_result("NVRM_FREE_IDEMPOTENT_AND_STALE", 0); + gate_result("NVRM_ALLOC_AFTER_FREE_REUSES_VA", 0); + gate_result("GPU_SUBMIT_AFTER_CHURN", 0); + printf("TOTAL: %d PASSED: %d FAILED: %d\n", g_total, g_passed, g_failed); + return 1; + } + + uint64_t churn_bytes = 0, high_water_delta = 0; + int churn_rc = gate_free_churn(&rm, &churn_bytes, &high_water_delta); + uint64_t required_bytes = 256ull * 1024 * 1024 * 1024; /* 256 GiB */ + uint64_t max_high_water = 2ull * 1024 * 1024 * 1024; /* 2 GiB */ + int churn_ok = (churn_rc == 0) && (churn_bytes > required_bytes) && (high_water_delta < max_high_water); + printf(" churn: bytes=%" PRIu64 " (%.2f GiB) high_water_delta=%" PRIu64 " (%.2f MiB)\n", + churn_bytes, (double)churn_bytes / (1024.0 * 1024.0 * 1024.0), + high_water_delta, (double)high_water_delta / (1024.0 * 1024.0)); + gate_result("NVRM_FREE_CHURN", churn_ok); + + gate_result("NVRM_FREE_UNMAPS", gate_free_unmaps(&rm) == 0); + gate_result("NVRM_FREE_IDEMPOTENT_AND_STALE", gate_idempotent_and_stale(&rm) == 0); + gate_result("NVRM_ALLOC_AFTER_FREE_REUSES_VA", gate_alloc_after_free_reuses_va(&rm) == 0); + gate_result("GPU_SUBMIT_AFTER_CHURN", gate_gpu_submit_after_churn(&rm) == 0); + + printf("TOTAL: %d PASSED: %d FAILED: %d\n", g_total, g_passed, g_failed); + + nvrm_close(&rm); + return (g_failed == 0) ? 0 : 1; +} diff --git a/nvrm/nvrm.c b/nvrm/nvrm.c index 487fa93..0b52221 100644 --- a/nvrm/nvrm.c +++ b/nvrm/nvrm.c @@ -248,7 +248,71 @@ int nvrm_open(Nvrm *rm) { return 0; } +/* Return a freed VA range to the sorted, coalesced free list. */ +static void va_release(Nvrm *rm, uint64_t va, uint64_t size) { + uint32_t pos = 0; + while (pos < rm->free_count && rm->free_list[pos].va < va) pos++; + + if (rm->free_count >= NVRM_MAX_FREE) { + /* Free-list table full: leak this VA range (address space only -- + * no RM object, CPU mapping, or UVM range remains associated with it). */ + return; + } + for (uint32_t i = rm->free_count; i > pos; i--) rm->free_list[i] = rm->free_list[i - 1]; + rm->free_list[pos].va = va; + rm->free_list[pos].size = size; + rm->free_count++; + + /* Coalesce with the following entry. */ + if (pos + 1 < rm->free_count && rm->free_list[pos].va + rm->free_list[pos].size == rm->free_list[pos + 1].va) { + rm->free_list[pos].size += rm->free_list[pos + 1].size; + for (uint32_t i = pos + 1; i + 1 < rm->free_count; i++) rm->free_list[i] = rm->free_list[i + 1]; + rm->free_count--; + } + /* Coalesce with the preceding entry. */ + if (pos > 0 && rm->free_list[pos - 1].va + rm->free_list[pos - 1].size == rm->free_list[pos].va) { + rm->free_list[pos - 1].size += rm->free_list[pos].size; + for (uint32_t i = pos; i + 1 < rm->free_count; i++) rm->free_list[i] = rm->free_list[i + 1]; + rm->free_count--; + } +} + static uint64_t va_take(Nvrm *rm, uint64_t size, uint64_t align) { + /* First-fit reuse from the free list (sorted ascending by va), splitting + * off any leading alignment pad or trailing remainder as smaller free + * entries. Falls back to the bump allocator when nothing fits. */ + for (uint32_t i = 0; i < rm->free_count; i++) { + uint64_t base = rm->free_list[i].va; + uint64_t sz = rm->free_list[i].size; + uint64_t aligned = (base + align - 1) & ~(align - 1); + uint64_t pad = aligned - base; + if (sz < pad + size) continue; + uint64_t used_end = aligned + size; + uint64_t range_end = base + sz; + + if (pad == 0 && used_end == range_end) { + /* Exact fit: remove entry i. */ + for (uint32_t j = i; j + 1 < rm->free_count; j++) rm->free_list[j] = rm->free_list[j + 1]; + rm->free_count--; + } else if (pad == 0) { + /* Consume from the front: shrink entry i. */ + rm->free_list[i].va = used_end; + rm->free_list[i].size = range_end - used_end; + } else { + /* Leading pad remains free at entry i. */ + rm->free_list[i].size = pad; + if (used_end < range_end && rm->free_count < NVRM_MAX_FREE) { + for (uint32_t j = rm->free_count; j > i + 1; j--) rm->free_list[j] = rm->free_list[j - 1]; + rm->free_list[i + 1].va = used_end; + rm->free_list[i + 1].size = range_end - used_end; + rm->free_count++; + } + /* If the table is full, the trailing remainder is leaked (address + * space only), matching va_release()'s degenerate-case handling. */ + } + return aligned; + } + uint64_t va = (rm->va_next + align - 1) & ~(align - 1); rm->va_next = va + size; return va; @@ -290,6 +354,7 @@ static int uvm_map(Nvrm *rm, uint64_t va, uint64_t size, uint32_t hmem) { /* Coherent, CPU-mapped, GPU-mapped buffer at one shared virtual address. */ int nvrm_alloc(Nvrm *rm, uint64_t size, NvrmMem *out) { size = (size + 0xfff) & ~0xfffull; + if (rm->live_count >= NVRM_MAX_LIVE) return fail(rm, "live allocation table full (%d)", NVRM_MAX_LIVE); uint64_t va = va_take(rm, size, 0x1000); /* Reserve the CPU range first so the fixed mapping can never clobber anything. */ void *res = mmap((void *)va, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0); @@ -336,6 +401,53 @@ int nvrm_alloc(Nvrm *rm, uint64_t size, NvrmMem *out) { out->size = size; out->cpu = cpu; memset(cpu, 0, size); + + rm->live[rm->live_count].handle = h; + rm->live[rm->live_count].va = va; + rm->live[rm->live_count].size = size; + rm->live_count++; + return 0; +} + +/* Reverse of nvrm_alloc(): drop the UVM external range, free the RM memory + * object, unmap the CPU range, and return the VA range to the free list. */ +int nvrm_free(Nvrm *rm, NvrmMem *m) { + if (!rm || !m) return -1; + + /* Idempotent: freeing an already-zeroed NvrmMem is a silent no-op. */ + if (m->handle == 0 && m->va == 0 && m->size == 0 && m->cpu == NULL) return 0; + + /* Reject anything that is not a currently-live allocation (e.g. a stale + * copy of an NvrmMem that was already freed elsewhere) without touching + * the driver at all. */ + int idx = -1; + for (uint32_t i = 0; i < rm->live_count; i++) { + if (rm->live[i].va == m->va && rm->live[i].handle == m->handle && rm->live[i].size == m->size) { + idx = (int)i; + break; + } + } + if (idx < 0) + return fail(rm, "nvrm_free: va 0x%lx handle 0x%x size 0x%lx is not a live allocation", + (unsigned long)m->va, m->handle, (unsigned long)m->size); + + UVM_FREE_PARAMS uf; + memset(&uf, 0, sizeof uf); + uf.base = m->va; + uf.length = m->size; + (void)ioctl(rm->fd_uvm, UVM_FREE, &uf); + + rm_free(rm, rm->device, m->handle); + + munmap(m->cpu, m->size); + + /* Remove from the live table (swap with last). */ + rm->live[idx] = rm->live[rm->live_count - 1]; + rm->live_count--; + + va_release(rm, m->va, m->size); + + memset(m, 0, sizeof *m); return 0; } diff --git a/nvrm/nvrm.h b/nvrm/nvrm.h index d9c1dcf..e29407a 100644 --- a/nvrm/nvrm.h +++ b/nvrm/nvrm.h @@ -14,6 +14,12 @@ #define NVRM_MAX_MEM 16 +/* Capacity of the free-VA-range table (nvrm_free / va_take reuse) and the + * live-allocation table (nvrm_free validation). Separate from NVRM_MAX_MEM, + * which no caller currently uses, so existing behavior is unaffected. */ +#define NVRM_MAX_FREE 4096 +#define NVRM_MAX_LIVE 4096 + typedef struct { uint32_t handle; uint64_t va; /* identical GPU and CPU virtual address */ @@ -21,6 +27,20 @@ typedef struct { void *cpu; /* CPU mapping (== (void *)va) */ } NvrmMem; +/* One coalesced free VA range available for reuse by va_take(). */ +typedef struct { + uint64_t va; + uint64_t size; +} NvrmVaRange; + +/* One outstanding allocation, tracked so nvrm_free() can tell a real, + * still-live NvrmMem apart from a stale/duplicate copy. */ +typedef struct { + uint32_t handle; + uint64_t va; + uint64_t size; +} NvrmLiveAlloc; + typedef struct { int fd_ctl, fd_dev, fd_uvm, fd_uvm2; uint32_t root, device, subdevice, virtmem, usermode, vaspace; @@ -40,11 +60,22 @@ typedef struct { uint32_t put; /* entries enqueued (monotonic) */ uint32_t retired; /* entries the caller has seen complete */ uint64_t va_next; + NvrmVaRange free_list[NVRM_MAX_FREE]; /* sorted ascending by va, coalesced */ + uint32_t free_count; + NvrmLiveAlloc live[NVRM_MAX_LIVE]; + uint32_t live_count; char err[256]; } Nvrm; int nvrm_open(Nvrm *rm); int nvrm_alloc(Nvrm *rm, uint64_t size, NvrmMem *out); +/* Reverse of nvrm_alloc: drops the UVM external range, frees the RM memory + * object, unmaps the CPU range, and returns the VA range for reuse by a + * later nvrm_alloc. Idempotent on an already-zeroed NvrmMem (returns 0, + * no syscalls). Returns -1 and sets rm->err, without touching the driver, + * if *m does not match a currently-live allocation (e.g. a stale copy). + * Zeroes *m on success. */ +int nvrm_free(Nvrm *rm, NvrmMem *m); int nvrm_channel(Nvrm *rm); /* Write a GP entry and advance USERD GPPut; does NOT ring the doorbell. */ int nvrm_enqueue(Nvrm *rm, const NvrmMem *pb, uint32_t off_bytes, uint32_t nwords); diff --git a/tests/run_nvrm_lifecycle_gates.sh b/tests/run_nvrm_lifecycle_gates.sh new file mode 100755 index 0000000..8be08ec --- /dev/null +++ b/tests/run_nvrm_lifecycle_gates.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# tests/run_nvrm_lifecycle_gates.sh -- M19R nvrm_free()/wait_marker_ge() lifecycle gates. +set -euo pipefail + +SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P) +REPO_ROOT=$(cd "$SCRIPT_DIR/.." && pwd -P) +LOCK_FILE=/tmp/aien-gb10.lock + +cd "$REPO_ROOT" + +echo "=== M19R NVRM Lifecycle Gates ===" +echo "Timestamp: $(date -u +"%Y-%m-%dT%H:%M:%SZ")" +echo "Host: $(uname -n) ($(uname -m))" + +echo -e "\n[*] Building lifecycle_gates..." +gcc -O2 -Wall -Wextra -Werror \ + -I./nvrm -I./m16 \ + -I./third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc \ + -I./third_party/nvidia-open-580.173.02/kernel-open/common/inc \ + -I./third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm \ + -I./third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include \ + nvrm/nvrm.c m16/m16_native.c nvrm/lifecycle_gates.c -o nvrm/lifecycle_gates + +echo " lifecycle_gates built cleanly." + +echo -e "\n[*] Running lifecycle_gates under GPU lock ($LOCK_FILE)..." +flock "$LOCK_FILE" ./nvrm/lifecycle_gates + +echo -e "\n=== M19R NVRM Lifecycle Gates: done ==="