interplane.Get Omega
Read in your language

physics-mv

FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.

Brought by drakestapleton

branch main Branches Tags History

physics-mv / nvrm / lifecycle_gates.c

428 lines · 17207 bytes Raw Blame History
1/*
2 * lifecycle_gates.c -- M19R NVRM lifecycle qualification gates.
3 *
4 * Validates nvrm_free() (VA reuse, live/idempotent/stale detection, CPU
5 * unmap) and m16_native_wait_marker_ge() (monotonic serial-number wait)
6 * against the real GB10 hardware and kernel driver. New file: does not
7 * modify m16_requalify.c / m16_concurrent.c or any existing test.
8 */
9#include "nvrm.h"
10#include "m16_native.h"
11
12#include <errno.h>
13#include <inttypes.h>
14#include <stdint.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <sys/mman.h>
19#include <time.h>
20#include <unistd.h>
21
22static int g_total = 0, g_passed = 0, g_failed = 0;
23
24static void gate_result(const char *name, int ok) {
25 g_total++;
26 if (ok) { g_passed++; printf("[PASS] %s\n", name); }
27 else { g_failed++; printf("[FAIL] %s\n", name); }
28}
29
30static uint64_t now_ms(void) {
31 struct timespec t;
32 clock_gettime(CLOCK_MONOTONIC, &t);
33 return (uint64_t)t.tv_sec * 1000ull + (uint64_t)t.tv_nsec / 1000000ull;
34}
35
36/* ---- NVRM_FREE_CHURN ---------------------------------------------------
37 * 20,000 alloc/touch/free cycles, sizes cycling 4 KiB..64 MiB, total bytes
38 * churned must exceed 256 GiB (2x the machine's 128 GiB unified memory --
39 * impossible without real nvrm_free() release), and va_next (the VA
40 * high-water mark) must stay bounded, proving reuse rather than a bump leak.
41 */
42#define CHURN_CYCLES 20000
43
44static int gate_free_churn(Nvrm *rm, uint64_t *out_bytes, uint64_t *out_high_water_delta) {
45 static const uint64_t sizes[] = {
46 4ull * 1024, /* 4 KiB */
47 64ull * 1024, /* 64 KiB */
48 1024ull * 1024, /* 1 MiB */
49 4ull * 1024 * 1024, /* 4 MiB */
50 16ull * 1024 * 1024, /* 16 MiB */
51 32ull * 1024 * 1024, /* 32 MiB */
52 64ull * 1024 * 1024, /* 64 MiB */
53 64ull * 1024 * 1024, /* 64 MiB */
54 };
55 const int nsizes = (int)(sizeof(sizes) / sizeof(sizes[0]));
56 uint64_t va_start = rm->va_next;
57 uint64_t va_high = rm->va_next;
58 uint64_t total_bytes = 0;
59
60 for (int i = 0; i < CHURN_CYCLES; i++) {
61 uint64_t size = sizes[i % nsizes];
62 NvrmMem m;
63 if (nvrm_alloc(rm, size, &m) != 0) {
64 fprintf(stderr, " churn: nvrm_alloc failed at cycle %d (size 0x%lx): %s\n",
65 i, (unsigned long)size, rm->err);
66 return -1;
67 }
68 if (rm->va_next > va_high) va_high = rm->va_next;
69
70 volatile uint8_t *p = (volatile uint8_t *)m.cpu;
71 uint8_t first = (uint8_t)(i & 0xff);
72 uint8_t last = (uint8_t)((i ^ 0xff) & 0xff);
73 p[0] = first;
74 p[m.size - 1] = last;
75 if (p[0] != first || p[m.size - 1] != last) {
76 fprintf(stderr, " churn: readback mismatch at cycle %d\n", i);
77 nvrm_free(rm, &m);
78 return -1;
79 }
80
81 total_bytes += m.size;
82 if (nvrm_free(rm, &m) != 0) {
83 fprintf(stderr, " churn: nvrm_free failed at cycle %d: %s\n", i, rm->err);
84 return -1;
85 }
86 if ((i % 2000) == 0) {
87 fprintf(stderr, " churn: cycle %d/%d, %" PRIu64 " bytes so far\n", i, CHURN_CYCLES, total_bytes);
88 }
89 }
90
91 *out_bytes = total_bytes;
92 *out_high_water_delta = va_high - va_start;
93 return 0;
94}
95
96/* ---- NVRM_FREE_UNMAPS ---------------------------------------------------
97 * After nvrm_free(), the CPU range must no longer be mapped: msync() on it
98 * must fail with ENOMEM.
99 */
100static int gate_free_unmaps(Nvrm *rm) {
101 NvrmMem m;
102 if (nvrm_alloc(rm, 0x4000, &m) != 0) {
103 fprintf(stderr, " unmaps: alloc failed: %s\n", rm->err);
104 return -1;
105 }
106 void *va = m.cpu;
107 size_t size = (size_t)m.size;
108
109 if (nvrm_free(rm, &m) != 0) {
110 fprintf(stderr, " unmaps: free failed: %s\n", rm->err);
111 return -1;
112 }
113
114 errno = 0;
115 int rc = msync(va, size, MS_ASYNC);
116 if (rc == 0) {
117 fprintf(stderr, " unmaps: msync unexpectedly succeeded on freed range %p\n", va);
118 return -1;
119 }
120 if (errno != ENOMEM) {
121 fprintf(stderr, " unmaps: msync failed with errno %d (%s), expected ENOMEM\n", errno, strerror(errno));
122 return -1;
123 }
124 return 0;
125}
126
127/* ---- NVRM_FREE_IDEMPOTENT_AND_STALE ------------------------------------
128 * Freeing an already-zeroed NvrmMem (double free) is a silent no-op (0, no
129 * syscalls). Freeing a saved stale copy (pre-free snapshot of a struct that
130 * has since been freed through another handle) must fail with -1 and must
131 * not touch the driver.
132 */
133static int gate_idempotent_and_stale(Nvrm *rm) {
134 NvrmMem m;
135 if (nvrm_alloc(rm, 0x2000, &m) != 0) {
136 fprintf(stderr, " idempotent: alloc failed: %s\n", rm->err);
137 return -1;
138 }
139 NvrmMem stale = m; /* stale copy, snapshotted before the real free */
140
141 if (nvrm_free(rm, &m) != 0) {
142 fprintf(stderr, " idempotent: first free failed: %s\n", rm->err);
143 return -1;
144 }
145 if (m.handle != 0 || m.va != 0 || m.size != 0 || m.cpu != NULL) {
146 fprintf(stderr, " idempotent: nvrm_free did not zero the NvrmMem on success\n");
147 return -1;
148 }
149
150 /* Double free of the now-zeroed struct: must return 0. */
151 if (nvrm_free(rm, &m) != 0) {
152 fprintf(stderr, " idempotent: double free of zeroed struct returned nonzero\n");
153 return -1;
154 }
155
156 /* Free of the stale (already-freed-elsewhere) copy: must return -1. */
157 if (nvrm_free(rm, &stale) == 0) {
158 fprintf(stderr, " idempotent: freeing stale copy unexpectedly succeeded\n");
159 return -1;
160 }
161 return 0;
162}
163
164/* ---- NVRM_ALLOC_AFTER_FREE_REUSES_VA ------------------------------------
165 * Allocate, free, allocate the same size again: must get the same VA back.
166 */
167static int gate_alloc_after_free_reuses_va(Nvrm *rm) {
168 NvrmMem a, b;
169 if (nvrm_alloc(rm, 0x8000, &a) != 0) {
170 fprintf(stderr, " reuse: first alloc failed: %s\n", rm->err);
171 return -1;
172 }
173 uint64_t va = a.va;
174 if (nvrm_free(rm, &a) != 0) {
175 fprintf(stderr, " reuse: free failed: %s\n", rm->err);
176 return -1;
177 }
178 if (nvrm_alloc(rm, 0x8000, &b) != 0) {
179 fprintf(stderr, " reuse: second alloc failed: %s\n", rm->err);
180 return -1;
181 }
182 int ok = (b.va == va);
183 if (!ok) {
184 fprintf(stderr, " reuse: expected va 0x%lx, got 0x%lx\n",
185 (unsigned long)va, (unsigned long)b.va);
186 }
187 nvrm_free(rm, &b);
188 return ok ? 0 : -1;
189}
190
191static int gate_injected_free_failure(int uvm_failure) {
192 Nvrm rm;
193 if (nvrm_open(&rm) != 0) return -1;
194 NvrmMem m;
195 if (nvrm_alloc(&rm, 0x4000, &m) != 0) { nvrm_close(&rm); return -1; }
196 uint64_t va = m.va;
197 uint32_t free_before = rm.free_count;
198 if (uvm_failure) rm.inject_uvm_free_failure = 1;
199 else rm.inject_rm_free_failure = 1;
200 int refused = nvrm_free(&rm, &m) != 0 && rm.faulted &&
201 m.handle != 0 && rm.live_count == 1 &&
202 rm.free_count == free_before;
203 NvrmMem other;
204 refused = refused && nvrm_alloc(&rm, 0x4000, &other) != 0;
205 int recovered_release = nvrm_free(&rm, &m) == 0 &&
206 rm.free_count > free_before && rm.free_list[rm.free_count - 1].va <= va;
207 nvrm_close(&rm);
208 return refused && recovered_release ? 0 : -1;
209}
210
211static int gate_corrupt_table_refusal(void) {
212 Nvrm rm;
213 if (nvrm_open(&rm) != 0) return -1;
214 NvrmMem m;
215 if (nvrm_alloc(&rm, 0x4000, &m) != 0) { nvrm_close(&rm); return -1; }
216 void *cpu = rm.live[0].cpu;
217 rm.live[0].cpu = NULL;
218 int refused = nvrm_free(&rm, &m) != 0 && rm.faulted && rm.live_count == 1 && m.handle;
219 rm.live[0].cpu = cpu;
220 int released = nvrm_free(&rm, &m) == 0;
221 nvrm_close(&rm);
222 return refused && released ? 0 : -1;
223}
224
225static int gate_allocation_overflow(Nvrm *rm) {
226 NvrmMem m = {0};
227 uint32_t before = rm->live_count;
228 uint64_t va_before = rm->va_next;
229 return (nvrm_alloc(rm, UINT64_MAX, &m) != 0 &&
230 nvrm_alloc(rm, 0, &m) != 0 &&
231 rm->live_count == before && rm->va_next == va_before) ? 0 : -1;
232}
233
234/* ---- M16_WAIT_GE_SERIAL --------------------------------------------------
235 * Pure CPU unit test, no hardware dependency: synthetic marker + serial
236 * (RFC 1982 style) comparison against a target that has wrapped past 0.
237 */
238static int gate_wait_ge_serial(void) {
239 uint32_t marker;
240
241 /* target 0xFFFFFFF0 satisfied by marker 0x00000005 (wrapped past). */
242 marker = 0x00000005u;
243 if (m16_native_wait_marker_ge(&marker, 0xFFFFFFF0u, 50) != 0) {
244 fprintf(stderr, " wait_ge: wrapped-past case unexpectedly failed/timed out\n");
245 return -1;
246 }
247
248 /* Not satisfied by 0xFFFFFFE0. Must time out, within timeout_ms + 50ms. */
249 marker = 0xFFFFFFE0u;
250 uint64_t t0 = now_ms();
251 int rc = m16_native_wait_marker_ge(&marker, 0xFFFFFFF0u, 100);
252 uint64_t elapsed = now_ms() - t0;
253 if (rc == 0) {
254 fprintf(stderr, " wait_ge: not-yet-satisfied case unexpectedly returned success\n");
255 return -1;
256 }
257 if (elapsed > 150) {
258 fprintf(stderr, " wait_ge: timeout took %" PRIu64 "ms, expected <= 150ms\n", elapsed);
259 return -1;
260 }
261 return 0;
262}
263
264/* ---- GPU_SUBMIT_AFTER_CHURN ----------------------------------------------
265 * After churn, open a channel and do one real submit + wait (same pattern
266 * as m16/m16_native.c + m16/m16_requalify.c) to prove the device context
267 * is still healthy after 20,000 alloc/free cycles.
268 */
269static int gate_gpu_submit_after_churn(Nvrm *rm) {
270 if (nvrm_channel(rm) != 0) {
271 fprintf(stderr, " submit: nvrm_channel failed: %s\n", rm->err);
272 return -1;
273 }
274 NvrmMem pb, marker_mem;
275 if (nvrm_alloc(rm, 0x1000, &pb) != 0) {
276 fprintf(stderr, " submit: pushbuffer alloc failed: %s\n", rm->err);
277 return -1;
278 }
279 /* The CPU polls this marker, so it must be GPU-uncached: a cacheable
280 * (NVOS32_ATTR2_GPU_CACHEABLE_YES) marker's store sits in the chip's L2 and
281 * the CPU can read 0 forever (the same bug fixed for M16 in 9e7dd87). */
282 if (nvrm_alloc_gpu_uncached(rm, 0x1000, &marker_mem) != 0) {
283 fprintf(stderr, " submit: marker alloc failed: %s\n", rm->err);
284 return -1;
285 }
286 volatile uint32_t *marker = (volatile uint32_t *)marker_mem.cpu;
287 *marker = 0;
288
289 uint32_t methods[32];
290 int nwords = m16_native_build_release(methods, marker_mem.va, 0x19c0ffee, rm->compute_class);
291 if (nwords <= 0) {
292 fprintf(stderr, " submit: build_release failed\n");
293 return -1;
294 }
295 memcpy(pb.cpu, methods, (size_t)nwords * sizeof(uint32_t));
296 __asm__ volatile("dsb sy" ::: "memory");
297
298 if (nvrm_submit(rm, &pb, 0, (uint32_t)nwords) != 0) {
299 fprintf(stderr, " submit: nvrm_submit failed: %s\n", rm->err);
300 return -1;
301 }
302 if (m16_native_wait_marker_ge(marker, 0x19c0ffee, 2000) != 0) {
303 fprintf(stderr, " submit: timeout waiting for marker; actual=0x%08x\n", *marker);
304 return -1;
305 }
306 if (*marker != 0x19c0ffee) {
307 fprintf(stderr, " submit: unexpected final marker value 0x%08x\n", *marker);
308 return -1;
309 }
310 nvrm_retire(rm, rm->put);
311 return 0;
312}
313
314
315/* omega#523: the compute-object RM_ALLOC is refused with NV_ERR_NO_MEMORY when
316 * the host has no free 2 MiB block at that instant. nvrm_channel must retry that
317 * one allocation, bounded, instead of failing the channel on the first refusal. */
318static int gate_channel_no_memory_retried(void) {
319 Nvrm rm;
320 if (nvrm_open(&rm) != 0) return -1;
321 rm.inject_rm_alloc_no_memory = 3;
322 rm.channel_no_memory_retry_ms = 1;
323 int rc = nvrm_channel(&rm);
324 int ok = rc == 0 && rm.compute_obj != 0 && rm.channel_no_memory_retries == 3 &&
325 rm.inject_rm_alloc_no_memory == 0;
326 if (!ok) printf(" channel after 3 injected NV_ERR_NO_MEMORY: rc=%d retries=%u err=\"%s\"\n",
327 rc, rm.channel_no_memory_retries, rm.err);
328 nvrm_close(&rm);
329 return ok ? 0 : -1;
330}
331
332/* The retry is bounded: one refusal more than the budget is final, the channel
333 * has no compute object, and the error names the attempts and the host's supply
334 * of 2 MiB blocks in the Normal zone and in all zones (DRA-351), so the next
335 * reader does not have to guess. */
336static int gate_channel_no_memory_bounded(void) {
337 Nvrm rm;
338 if (nvrm_open(&rm) != 0) return -1;
339 rm.inject_rm_alloc_no_memory = (uint8_t)(NVRM_CHANNEL_NO_MEMORY_RETRIES + 1);
340 rm.channel_no_memory_retry_ms = 1;
341 int rc = nvrm_channel(&rm);
342 int ok = rc != 0 && rm.compute_obj == 0 &&
343 rm.channel_no_memory_retries == NVRM_CHANNEL_NO_MEMORY_RETRIES &&
344 strstr(rm.err, "NV_ERR_NO_MEMORY") != NULL && strstr(rm.err, "attempts") != NULL &&
345 strstr(rm.err, "2 MiB blocks in the Normal zone") != NULL && strstr(rm.err, "(all zones ") != NULL;
346 if (!ok) printf(" bounded retry: rc=%d retries=%u err=\"%s\"\n", rc, rm.channel_no_memory_retries, rm.err);
347 int closed = nvrm_close(&rm) == 0;
348 if (!closed) printf(" bounded retry: close after refused channel failed: %s\n", rm.err);
349 return ok && closed ? 0 : -1;
350}
351
352/* The extra channel owns a subcontext as well as a FIFO. Rebuild each supported
353 * configuration on one open client and check teardown returns all tracked buffers. */
354static void gate_extra_channel_contexts(void) {
355 for (uint32_t n = 0; n <= NVRM_MAX_EXTRA_CHANNELS; n++) {
356 Nvrm rm;
357 int opened = nvrm_open(&rm) == 0;
358 int ok = opened;
359 uint32_t baseline = opened ? rm.live_count : 0;
360 for (int round = 0; ok && round < 3; round++) {
361 ok = nvrm_channel_n(&rm, n) == 0 && rm.n_extra == n;
362 for (uint32_t i = 0; ok && i < n; i++) {
363 ok = rm.extra[i].ctxshare != 0 && rm.extra[i].ctxshare != rm.ctxshare;
364 for (uint32_t j = 0; ok && j < i; j++)
365 ok = rm.extra[i].ctxshare != rm.extra[j].ctxshare;
366 }
367 if (nvrm_channel_destroy(&rm) != 0) ok = 0;
368 ok = ok && rm.n_extra == 0 && rm.ctxshare == 0 && rm.live_count == baseline;
369 for (uint32_t i = 0; ok && i < n; i++)
370 ok = rm.extra[i].ctxshare == 0 && rm.extra[i].gpfifo == 0;
371 }
372 if (opened && nvrm_close(&rm) != 0) ok = 0;
373 char name[80];
374 snprintf(name, sizeof name, "EXTRA_CHANNEL_CONTEXTS_%u_REOPEN_AND_RELEASE", n);
375 if (!ok) fprintf(stderr, "extra channels %u: %s\n", n, rm.err);
376 gate_result(name, ok);
377 }
378}
379
380int main(int argc, char **argv) {
381 if (argc == 2 && strcmp(argv[1], "--channels-only") == 0) {
382 gate_extra_channel_contexts();
383 printf("TOTAL: %d PASSED: %d FAILED: %d\n", g_total, g_passed, g_failed);
384 return g_failed ? 1 : 0;
385 }
386 if (argc != 1) { fprintf(stderr, "usage: %s [--channels-only]\n", argv[0]); return 2; }
387 printf("=== M19R NVRM LIFECYCLE GATES ===\n");
388
389 gate_result("M16_WAIT_GE_SERIAL", gate_wait_ge_serial() == 0);
390
391 Nvrm rm;
392 if (nvrm_open(&rm) != 0) {
393 fprintf(stderr, "FATAL: nvrm_open failed: %s\n", rm.err);
394 gate_result("NVRM_FREE_CHURN", 0);
395 gate_result("NVRM_FREE_UNMAPS", 0);
396 gate_result("NVRM_FREE_IDEMPOTENT_AND_STALE", 0);
397 gate_result("NVRM_ALLOC_AFTER_FREE_REUSES_VA", 0);
398 gate_result("GPU_SUBMIT_AFTER_CHURN", 0);
399 printf("TOTAL: %d PASSED: %d FAILED: %d\n", g_total, g_passed, g_failed);
400 return 1;
401 }
402
403 uint64_t churn_bytes = 0, high_water_delta = 0;
404 int churn_rc = gate_free_churn(&rm, &churn_bytes, &high_water_delta);
405 uint64_t required_bytes = 256ull * 1024 * 1024 * 1024; /* 256 GiB */
406 uint64_t max_high_water = 2ull * 1024 * 1024 * 1024; /* 2 GiB */
407 int churn_ok = (churn_rc == 0) && (churn_bytes > required_bytes) && (high_water_delta < max_high_water);
408 printf(" churn: bytes=%" PRIu64 " (%.2f GiB) high_water_delta=%" PRIu64 " (%.2f MiB)\n",
409 churn_bytes, (double)churn_bytes / (1024.0 * 1024.0 * 1024.0),
410 high_water_delta, (double)high_water_delta / (1024.0 * 1024.0));
411 gate_result("NVRM_FREE_CHURN", churn_ok);
412
413 gate_result("NVRM_FREE_UNMAPS", gate_free_unmaps(&rm) == 0);
414 gate_result("NVRM_FREE_IDEMPOTENT_AND_STALE", gate_idempotent_and_stale(&rm) == 0);
415 gate_result("NVRM_ALLOC_AFTER_FREE_REUSES_VA", gate_alloc_after_free_reuses_va(&rm) == 0);
416 gate_result("NVRM_ALLOCATION_OVERFLOW_REFUSED", gate_allocation_overflow(&rm) == 0);
417 gate_result("GPU_SUBMIT_AFTER_CHURN", gate_gpu_submit_after_churn(&rm) == 0);
418
419 nvrm_close(&rm);
420 gate_result("UVM_FREE_FAILURE_QUARANTINES", gate_injected_free_failure(1) == 0);
421 gate_result("RM_FREE_FAILURE_QUARANTINES", gate_injected_free_failure(0) == 0);
422 gate_result("CORRUPT_TABLE_REFUSED", gate_corrupt_table_refusal() == 0);
423 gate_result("CHANNEL_NO_MEMORY_RETRIED", gate_channel_no_memory_retried() == 0);
424 gate_result("CHANNEL_NO_MEMORY_BOUNDED", gate_channel_no_memory_bounded() == 0);
425 gate_extra_channel_contexts();
426 printf("TOTAL: %d PASSED: %d FAILED: %d\n", g_total, g_passed, g_failed);
427 return (g_failed == 0) ? 0 : 1;
428}