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 / docs / share / README.md
| 1 | <!-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception --> |
| 2 | # Zero-copy buffer sharing between processes |
| 3 | |
| 4 | `native/share/share.om` lets one process give another process its GPU buffer |
| 5 | without copying it. Both processes then read and write the same physical pages |
| 6 | through the CPU, and the GPU sees the buffer at the same virtual address in the |
| 7 | importer as the CPU does. It is the CUDA++ form of ROADMAP section 3 |
| 8 | ("Zero-copy shared memory"). It is written in Omega and composes after |
| 9 | `native/nvrm/context.om`, reusing that file's descriptors, handles and mapping |
| 10 | slots. `context.om` is unchanged. |
| 11 | |
| 12 | Status: IMPLEMENTED and EXERCISED against a fake RM on the host. It has not |
| 13 | run on a GB10. The chip tier is NOT_RUN; the runbook is below. |
| 14 | |
| 15 | ## Calls |
| 16 | |
| 17 | | Function | Does | Returns | |
| 18 | |---|---|---| |
| 19 | | `nvexport m` | Grants `DUP_OBJECT` on the memory behind mapping `m` to any client of the same user (`RM_SHARE`, NVOS57) and fills the 24-byte record `nvshrec`. | `true`, or `false` with `nverr` set | |
| 20 | | `nvimport want ro` | Reads `nvshrec`, duplicates the owner's memory into this process's client (`RM_DUP_OBJECT`, NVOS55), proves the size, maps it for the CPU, reserves a UVM external range at the CPU address, maps it for the GPU there (`RM_MAP_MEMORY_DMA`, NVOS46) and adds the UVM external mapping (`UVM_MAP_EXTERNAL_ALLOCATION`). | CPU mapping handle; `mapaddr` of it is also the GPU VA. `-1` with `nverr` on refusal | |
| 21 | |
| 22 | An import is released with `nvdrop` like any `nvalloc` mapping. The duplicate |
| 23 | holds its own reference to the pages, so an import keeps working after the |
| 24 | owner frees its buffer or exits. |
| 25 | |
| 26 | With `ro` true every access flag is read-only: CPU map `NVOS33` access field |
| 27 | `READ_ONLY` (1), CPU mapping bound with access 1, DMA flags 33041 (`33040` |
| 28 | plus `NVOS46` access `READ_ONLY`), UVM mapping type 3 (`READ_ONLY`). A CPU |
| 29 | write through a read-only import traps with exit 103. |
| 30 | |
| 31 | An imported buffer cannot be exported again (`EPERM`). Only the owner exports. |
| 32 | |
| 33 | ## Record |
| 34 | |
| 35 | Little endian, 24 bytes, produced by `nvexport`, consumed by `nvimport`. The |
| 36 | transport between processes (pipe, socket, file) is the caller's. |
| 37 | |
| 38 | | Offset | Width | Field | Accepted | |
| 39 | |---|---|---|---| |
| 40 | | 0 | u32 | magic `"OMZC"` = 1129991503 | exactly that value | |
| 41 | | 4 | u32 | version | 1 | |
| 42 | | 8 | u32 | owner RM client handle | nonzero | |
| 43 | | 12 | u32 | owner memory handle | nonzero | |
| 44 | | 16 | u64 | size in bytes | 1 .. 68719476736 (2^36), multiple of 4096, equal to `want` | |
| 45 | |
| 46 | ## Refusals |
| 47 | |
| 48 | Before any RM call (no slot, no RM object, context not faulted): |
| 49 | |
| 50 | | `nverr` | Cause | |
| 51 | |---|---| |
| 52 | | -5 | context faulted, closed, or UVM not registered | |
| 53 | | -22 | malformed record: magic, version, zero handle, size 0, over 2^36, not page aligned | |
| 54 | | -34 | record size differs from `want` | |
| 55 | | -24 | no free mapping slot | |
| 56 | | -75 | handle sequence exhausted | |
| 57 | |
| 58 | From RM, after the duplicate exists (the duplicate is freed, no fault): |
| 59 | |
| 60 | | `nverr` | Cause | |
| 61 | |---|---| |
| 62 | | RM status (positive) | `RM_DUP_OBJECT` refused: wrong handle or client, not shared, other user | |
| 63 | | -34 | the object is not `size` bytes (size proof below) | |
| 64 | |
| 65 | GPU stage (UVM range, DMA map, UVM mapping): the slot stays quarantined and |
| 66 | the context is faulted, exactly as in `nvalloc`. `nvmclose` or `nvmforce` |
| 67 | releases it. |
| 68 | |
| 69 | ### Size proof |
| 70 | |
| 71 | `RM_DUP_OBJECT` does not report the object's size. Before mapping, the |
| 72 | importer asks RM to map one page at offset `size`. RM must refuse it with |
| 73 | `INVALID_BASE` (0x20), which means the object is not larger than `size`. The |
| 74 | full map of `size` bytes must then succeed; `INVALID_LIMIT` (0x2e) means the |
| 75 | object is smaller. Either mismatch returns -34 and the duplicate is freed, |
| 76 | which also retires any RM mapping record the probe created. The RM behavior |
| 77 | is read from `src/nvidia/src/kernel/rmapi/mapping_cpu.c` lines 245-269 of |
| 78 | NVIDIA open-gpu-kernel-modules 580.173.02 (not vendored here; the vendored |
| 79 | tree holds headers only). It is not yet confirmed on the chip. |
| 80 | |
| 81 | ## Constants |
| 82 | |
| 83 | Request numbers, parameter sizes, field offsets and statuses are not typed by |
| 84 | hand in the tests. `tests/share/abi-oracle.c` prints them from the pinned |
| 85 | headers in `third_party/nvidia-open-580.173.02/` as Omega functions, and the |
| 86 | fake RM checks every call against them. The literals in `share.om` were |
| 87 | computed with Wolfram: |
| 88 | |
| 89 | ``` |
| 90 | WolframLanguageEvaluator: {3*2^30+28*2^16+70*2^8+52, 3*2^30+24*2^16+70*2^8+53, |
| 91 | 2^15+2^8+2^4, BitOr[2^15+2^8+2^4,1], FromDigits[Reverse[ToCharacterCode["OMZC"]],256], |
| 92 | 2^36, FromDigits["1b",16], FromDigits["20",16], FromDigits["2e",16], FromDigits["57",16], |
| 93 | 2^40, 2^49-2^26} |
| 94 | -> {3223078452, 3222816309, 33040, 33041, 1129991503, 68719476736, 27, 32, 46, 87, |
| 95 | 1099511627776, 562949886312448} |
| 96 | WolframLanguageEvaluator: {2^36 + 2^12, 2^37, 2^14, 2^13, FromDigits[{67,90,77,79},256]} |
| 97 | -> {68719480832, 137438953472, 16384, 8192, 1129991503} |
| 98 | WolframLanguageEvaluator: {2^16, 2^16/4096, 10*10^9} -> {65536, 16, 10000000000} |
| 99 | WolframLanguageEvaluator: {FactorInteger[2^49 - 562949869535232], 2^49 - 5*2^24 == 562949869535232} |
| 100 | -> {{{2, 24}, {5, 1}}, True} |
| 101 | ``` |
| 102 | |
| 103 | The GPU VA bound `562949869535232` in `nvshgpu` is `2^49 - 5*2^24`, the |
| 104 | `vaSize` that `nvfull` in `context.om` gives the VA space |
| 105 | (`NV_VASPACE_ALLOCATION_PARAMETERS` byte 8). `nvshgpu` checks an import's CPU |
| 106 | address against it exactly as `nvalloc` does, with the same literal. (The |
| 107 | `2^49-2^26` in the first query above is not that bound.) |
| 108 | |
| 109 | ## Host test |
| 110 | |
| 111 | ``` |
| 112 | OMEGA_OMC=<pinned omc> make test-zero-copy-share |
| 113 | ``` |
| 114 | |
| 115 | `tests/share/run-share-host.sh` runs four tiers. None opens a device file. |
| 116 | |
| 117 | 1. Host reference: build and run `abi-oracle.c` against the pinned NVIDIA |
| 118 | headers, producing `abi.om`. |
| 119 | 2. Simulation: rewrite a private copy of `context.om` + `share.om` so every |
| 120 | device word (`devcall`, `devctl`, `uvmctl`, device `fopen`, `fclose`, |
| 121 | `mapbind`, `mapnew`) calls `tests/share/fake-rm.om`, and refuse the run if |
| 122 | any raw device word survives. The fake keeps an RM object table per client |
| 123 | and backs each memory object with a real memfd, so an import really aliases |
| 124 | the owner's bytes. `tests/share/share-test.om` forges records byte by byte |
| 125 | and checks: |
| 126 | - owner export and record bytes; export refusals; |
| 127 | - same-process and foreign-client imports, read-write and read-only, by |
| 128 | reading and writing the shared bytes; |
| 129 | - an import cannot be re-exported; |
| 130 | - the owner frees first and both imports still read the bytes; |
| 131 | - negative controls: wrong handle, wrong client, unshared object, size |
| 132 | mismatch in both directions, `want` mismatch, seven malformed records, |
| 133 | faulted context, and a UVM failure that must quarantine the slot; |
| 134 | - a separate binary writes through a read-only import and must exit 103. |
| 135 | 3. Mutants: `tests/share/mutants.sh` applies 20 one-site changes to |
| 136 | `share.om` (each read-only flag, both bind accesses, the size proof, the |
| 137 | share policy, the record checks, the release on refusal, the GPU stage). |
| 138 | Each mutant must compile and the suite must fail on it. |
| 139 | 4. Chip gate: `tests/share/chip-share.om` is compiled from the unrewritten |
| 140 | sources and never executed. |
| 141 | |
| 142 | ## Hosted native library unit |
| 143 | |
| 144 | `native/share/share_native.om` is the same export and import in OM-2 |
| 145 | function mode, for the one combined hosted native library (omega-bridge ABI v2, |
| 146 | `tools/native --host`; Integration generates and links that library). Function |
| 147 | mode has no globals, so it does not compose with `context.om`. All state lives |
| 148 | in caller buffers, and the CPU mapping token lives in the explicit native |
| 149 | context the caller passes. |
| 150 | |
| 151 | | Export (C, `omega_native.h`) | Arguments after `ctx, uint64_t *result` | |
| 152 | |---|---| |
| 153 | | `omega2_ctx_nvshare_export` | `uint8_t *s` (`zcshare`), `uint8_t *rec` (`zcrec`), `uint8_t *p, uint64_t p_len` | |
| 154 | | `omega2_ctx_nvshare_import` | `uint8_t *s`, `const uint8_t *rec`, `uint8_t *p, uint64_t p_len`, `const uint8_t *path, uint64_t path_len` | |
| 155 | | `omega2_ctx_nvshare_release` | `uint8_t *s`, `uint8_t *p, uint64_t p_len` | |
| 156 | |
| 157 | `*result` is a signed 64-bit value, also stored in `zcshare.err`: 0, a negative |
| 158 | errno (the table above, plus -16 for a block that already holds an import and |
| 159 | -1 for re-exporting an import), or the positive RM status of a refused request. |
| 160 | `p` is a 9264-byte scratch (the size of `UVM_MAP_EXTERNAL_ALLOCATION_PARAMS`); |
| 161 | any other `p_len` is `OMEGA_BAD_ARG` before any request. `path` names the |
| 162 | control device (`/dev/nvidiactl` on the chip) without a trailing NUL; import |
| 163 | opens it for the descriptor `RM_MAP_MEMORY` binds and closes it afterwards. |
| 164 | |
| 165 | `zcshare` (96 bytes): inputs `ctlfd`@0, `uvmfd`@4, `client`@8, `device`@12, |
| 166 | `vaspace`@16 and `uuid`@72 (16 bytes) from the NVRM client; export reads |
| 167 | `obj`@20 and `size`@32; import reads `newh`@24 (handle for the duplicate), |
| 168 | `mode`@28 (0 read/write, 1 read-only), `size` and `addr`@40 (CPU and GPU |
| 169 | address, 4 KiB aligned, inside [65536, 2^40 - size]). Outputs: `obj` (the live |
| 170 | duplicate), `map`@48 (CPU mapping token), `live`@56 (bits: 1 duplicate, 2 CPU |
| 171 | mapping, 4 UVM range, 8 DMA mapping, 16 completed import), `stage`@60 |
| 172 | (51..57), `err`@64 and `fault`@88. `zcrec` is the 24-byte record above. |
| 173 | |
| 174 | Rules the callers keep: |
| 175 | |
| 176 | - Import and release use the same native context. A release through another |
| 177 | context runs the UVM, DMA and RM frees, then traps in `mapdrop` (bridge |
| 178 | status 3, result unwritten); the block still shows `live` 18, and the right |
| 179 | context finishes the release. |
| 180 | - Each live import holds one slot of that context; a full context refuses the |
| 181 | import with -24 before the duplicate survives (it is freed). |
| 182 | `omega_native_context_destroy` returns `OMEGA_CONTEXT_BUSY` while an import |
| 183 | is live. |
| 184 | - Release frees in order UVM range, DMA mapping, RM duplicate, CPU mapping. |
| 185 | Each `live` bit clears only when its request succeeded, so a refused step is |
| 186 | retried by calling release again. |
| 187 | - `fault` is sticky. A faulted block refuses export and import with -5; release |
| 188 | it and discard it. |
| 189 | |
| 190 | `tests/share/run-share-native.sh` (`make test-zero-copy-share-native`) builds |
| 191 | a single-unit test library from this file into the build directory, never |
| 192 | linked into the product, and runs `tests/share/native-share-test.c`. That test |
| 193 | traps `ioctl` on two unopened descriptors with seccomp and answers as RM and |
| 194 | UVM, decoding each request with the pinned NVIDIA headers. Memory objects are |
| 195 | memfds, so imports alias the owner's bytes in both directions. It checks |
| 196 | export, read/write and read-only imports (a child's write to a read-only import |
| 197 | gets `SIGSEGV`), every refusal, size lies in both directions, the faults of a |
| 198 | zero duplicate handle, a refused DMA or UVM mapping and a moved DMA address, a |
| 199 | refused RM free, context slots, and the wrong-context trap. Then it rebuilds |
| 200 | the library from each of the 20 mutants in `tests/share/native-mutants.txt`; |
| 201 | each must build and the test must fail. Pins (checked by sha256): |
| 202 | `OMEGA_NATIVE_OMC2` b8f8f137… (omega-mv dbedb2ca), `OMEGA_NATIVE_OMC` |
| 203 | 734f0a39…, and `ZERO_COPY_NATIVE_BRIDGE` an omega-bridge checkout with the |
| 204 | explicit native context (omega-bridge#69 at 248a1984). Without them the target |
| 205 | reports NOT_RUN and exits 2. |
| 206 | |
| 207 | The chip tier of this unit is NOT_RUN. It needs the combined library with an |
| 208 | NVRM client bring-up export (client, device, VA space, UVM registration, GPU |
| 209 | UUID) to fill `zcshare`; the program-mode chip runbook below sends the same |
| 210 | requests. |
| 211 | |
| 212 | ## Chip runbook (NOT_RUN) |
| 213 | |
| 214 | Only in a Drake-authorized exclusive GPU window, following the current |
| 215 | window procedure. It opens `/dev/nvidiactl`, `/dev/nvidia0` and |
| 216 | `/dev/nvidia-uvm` from two processes. |
| 217 | |
| 218 | ``` |
| 219 | cd <CUDApp checkout at the PR head> |
| 220 | OMEGA_OMC=<pinned omc> ZERO_COPY_OUT=build/zero-copy sh tests/share/run-share-host.sh --no-mutants |
| 221 | sha256sum build/zero-copy/chip-share # record next to the receipt |
| 222 | chmod 0755 build/zero-copy/chip-share |
| 223 | taskset -c 10-14 build/zero-copy/chip-share; echo rc=$? |
| 224 | ``` |
| 225 | |
| 226 | Pass is rc 0 and stdout `ZERO_COPY_SHARE_CHIP_PASS`. The program forks before |
| 227 | any device is opened. The parent allocates 65536 bytes, fills them, exports |
| 228 | and sends the record over a pipe. The child imports the buffer read-write and |
| 229 | read-only, checks the owner's bytes through both, writes a marker byte through |
| 230 | the read-write import, and checks that the read-only import sees it. The parent |
| 231 | checks that its own mapping sees the child's marker, frees its buffer, and |
| 232 | tells the child. The child checks both imports again, drops them, and stops its |
| 233 | context. A failure prints `ZERO_COPY_SHARE_CHIP_FAIL step N`; steps 20..32 are |
| 234 | the owner's, 40..53 the importer's, 60..64 pipe, fork and reaping (see the source). |
| 235 | |
| 236 | Negative control for the window: run it once with the record's handle |
| 237 | corrupted (`nvshput 12 add nvshget 12 1` before the send, in a scratch copy), |
| 238 | which must fail at step 42 with an RM status in `nverr`. |