CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tests / test_nvrm_va_slots.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* test_nvrm_va_slots: the driver client's process-wide GPU VA slots and its close path, on the host. Opens no |
| 3 | * device: nvrm_open is never called; the Nvrm structs are built by hand and only va_take, acquire_va_slot and |
| 4 | * nvrm_close run (nvrm_close with no RM objects does no ioctl). |
| 5 | * |
| 6 | * VA_SLOT_NO_OVERLAP (CudaAuditFable REPORT.md RtMem F5): every Nvrm gets a 4 GiB slot from a shared counter, and |
| 7 | * the bump allocator used to run past the slot end into the next client's slot. A client may now grow past 4 GiB |
| 8 | * only by claiming the address space after its slot from the counter, which works when nothing was handed out |
| 9 | * after it; otherwise the allocation is refused. |
| 10 | * CLOSE_TWICE_LEAVES_REUSED_FDS (RtMem F12): nvrm_close closed the four fds and kept their numbers, so a second |
| 11 | * close closed whatever file had since reused the number. |
| 12 | */ |
| 13 | #include "nvrm.c" |
| 14 | |
| 15 | #include <stdlib.h> |
| 16 | |
| 17 | static int n_pass, n_fail; |
| 18 | static void check(const char *id, int ok) { |
| 19 | printf("[%s] %s\n", ok ? "PASS" : "FAIL", id); |
| 20 | if (ok) n_pass++; else n_fail++; |
| 21 | } |
| 22 | |
| 23 | static void slot_rm(Nvrm *rm, uint64_t slot) { |
| 24 | memset(rm, 0, sizeof *rm); |
| 25 | rm->va_next = rm->va_slot_base = slot; |
| 26 | } |
| 27 | |
| 28 | static void case_va_slots(void) { |
| 29 | const uint64_t four = 0x100000000ull; |
| 30 | const uint64_t a = acquire_va_slot(), b = acquire_va_slot(); |
| 31 | check("va_two_slots_adjacent", a != 0 && b == a + four); |
| 32 | Nvrm ra, rb; |
| 33 | slot_rm(&ra, a); |
| 34 | slot_rm(&rb, b); |
| 35 | check("va_whole_slot_fits", va_take(&ra, four, 0x1000) == a); |
| 36 | const uint64_t past = va_take(&ra, 0x1000, 0x1000); |
| 37 | printf(" slot A full, next take: 0x%llx (slot B starts at 0x%llx)\n", (unsigned long long)past, |
| 38 | (unsigned long long)b); |
| 39 | check("va_slot_full_refused_not_into_next_slot", past == 0); |
| 40 | const uint64_t big = va_take(&rb, 2 * four, 0x1000); |
| 41 | check("va_last_slot_grows_past_4gib", big == b); |
| 42 | const uint64_t c = acquire_va_slot(); |
| 43 | printf(" after B took 8 GiB, next slot: 0x%llx (B ends at 0x%llx)\n", (unsigned long long)c, |
| 44 | (unsigned long long)(b + 2 * four)); |
| 45 | check("va_next_slot_after_grown_slot", c >= b + 2 * four); |
| 46 | check("va_grown_slot_cannot_grow_into_later_slot", va_take(&rb, 0x1000, 0x1000) == 0); |
| 47 | Nvrm rc; |
| 48 | slot_rm(&rc, c); |
| 49 | check("va_later_slot_still_usable", va_take(&rc, 0x1000, 0x1000) == c); |
| 50 | } |
| 51 | |
| 52 | static int fd_open(int fd) { return fcntl(fd, F_GETFD) != -1; } |
| 53 | |
| 54 | static void case_close_twice(void) { |
| 55 | int p[2]; |
| 56 | if (pipe(p) != 0) { check("close_pipe", 0); return; } |
| 57 | Nvrm rm; |
| 58 | memset(&rm, 0, sizeof rm); |
| 59 | rm.fd_ctl = dup(p[0]); |
| 60 | rm.fd_uvm = dup(p[0]); |
| 61 | rm.fd_uvm2 = dup(p[0]); |
| 62 | rm.fd_dev = dup(p[0]); |
| 63 | const int ctl = rm.fd_ctl; |
| 64 | check("close_first", nvrm_close(&rm) == 0 && !fd_open(ctl)); |
| 65 | check("close_fds_cleared", rm.fd_ctl <= 0 && rm.fd_uvm <= 0 && rm.fd_uvm2 <= 0 && rm.fd_dev <= 0); |
| 66 | const int victim = dup(p[1]); /* lowest free number: one the first close released */ |
| 67 | printf(" reused fd %d (fd_ctl was %d)\n", victim, ctl); |
| 68 | (void)nvrm_close(&rm); |
| 69 | check("close_twice_leaves_reused_fd_open", victim >= 0 && fd_open(victim)); |
| 70 | close(victim); |
| 71 | close(p[0]); |
| 72 | close(p[1]); |
| 73 | } |
| 74 | |
| 75 | int main(void) { |
| 76 | case_va_slots(); |
| 77 | case_close_twice(); |
| 78 | printf("SUMMARY passed=%d failed=%d\n", n_pass, n_fail); |
| 79 | return n_fail ? 1 : 0; |
| 80 | } |