Files
nvdriver/tools/check_uvm_ioctl_layout.py
nvidia-driver-rs 4526e6a816 Baseline: validated 30/30 tree with passing Level 7 hardware regression
- Updated tools/post_reboot.sh to check kernel oops taint bit and stuck driver tasks instead of <86400s uptime
- Level 7 hardware regression passed on 0000:b3:00.0 (module 37e6fa430835eee2b426019db3ad70eeb7a9e9070c09c84bd9b973cf56cbe529, 107 inventory words, 0 static mismatches, CUDA baseline verified before and after)
- Updated .gitignore to exclude build archives (*.a, *.rlib), __pycache__, and raw trace dumps
2026-11-23 23:48:38 +00:00

114 lines
5.6 KiB
Python

#!/usr/bin/env python3
"""Structure layouts of the /dev/nvidia-uvm ioctl interface, computed by the C compiler
from the open-source nvidia-uvm headers of 580.178.04 (evidence class [S]).
Reads the routing table of uvm.c (UVM_ROUTE_CMD_* lines of uvm_ioctl), compiles a
program that prints the size of every parameter structure, the offset of its rmStatus
and, for the commands the libcuda workload issues, the offset and size of every field.
Writes target/uvm/ioctl-layout.txt, which `uvm::ioctl` is tested against.
Two source trees are compiled and must agree: the reference tree of the analysis and, when
present, the tree the loaded module was built from (/usr/src/nvidia-srv-580.178.04).
"""
import hashlib
import json
from pathlib import Path
import re
import subprocess
import sys
import tempfile
ROOT = Path(__file__).resolve().parents[1]
REFERENCE = Path("/home/geodd/nvidia-driver/analysis-580.178.04/reference/"
"open-gpu-kernel-modules-580.178.04/kernel-open")
INSTALLED = Path("/usr/src/nvidia-srv-580.178.04")
FIELDS = {
"UVM_INITIALIZE": ("flags", "rmStatus"),
"UVM_MM_INITIALIZE": ("uvmFd", "rmStatus"),
"UVM_PAGEABLE_MEM_ACCESS": ("pageableMemAccess", "rmStatus"),
"UVM_REGISTER_GPU": ("gpu_uuid", "numaEnabled", "numaNodeId", "rmCtrlFd", "hClient",
"hSmcPartRef", "rmStatus"),
"UVM_UNREGISTER_GPU": ("gpu_uuid", "rmStatus"),
"UVM_CREATE_RANGE_GROUP": ("rangeGroupId", "rmStatus"),
"UVM_DESTROY_RANGE_GROUP": ("rangeGroupId", "rmStatus"),
"UVM_REGISTER_GPU_VASPACE": ("gpuUuid", "rmCtrlFd", "hClient", "hVaSpace", "rmStatus"),
"UVM_UNREGISTER_GPU_VASPACE": ("gpuUuid", "rmStatus"),
"UVM_REGISTER_CHANNEL": ("gpuUuid", "rmCtrlFd", "hClient", "hChannel", "base", "length",
"rmStatus"),
"UVM_UNREGISTER_CHANNEL": ("gpuUuid", "hClient", "hChannel", "rmStatus"),
"UVM_CREATE_EXTERNAL_RANGE": ("base", "length", "rmStatus"),
"UVM_MAP_EXTERNAL_ALLOCATION": ("base", "length", "offset", "perGpuAttributes",
"gpuAttributesCount", "rmCtrlFd", "hClient", "hMemory",
"rmStatus"),
"UVM_FREE": ("base", "length", "rmStatus"),
"UVM_MAP_DYNAMIC_PARALLELISM_REGION": ("base", "length", "gpuUuid", "rmStatus"),
"UVM_ALLOC_SEMAPHORE_POOL": ("base", "length", "perGpuAttributes", "gpuAttributesCount",
"rmStatus"),
"UVM_VALIDATE_VA_RANGE": ("base", "length", "rmStatus"),
}
ATTRIBUTES = ("gpuUuid", "gpuMappingType", "gpuCachingType", "gpuFormatType", "gpuElementBits",
"gpuCompressionType")
def routes(uvm_c):
"""(name, init check, heap-allocated block) in the order of uvm_ioctl."""
found = re.findall(r"UVM_ROUTE_CMD_(STACK|ALLOC)_(NO_INIT|INIT)_CHECK\((UVM_[A-Z0-9_]+),",
uvm_c)
return [(name, check == "INIT", storage == "ALLOC") for storage, check, name in found]
def layout(uvm, include):
table = routes((uvm / "uvm.c").read_text())
assert len(table) == 45, len(table)
program = ['#include <stdio.h>', '#include <stddef.h>', '#include "uvm_linux_ioctl.h"',
'int main(void) {']
for name, check, heap in table:
program.append(
f'printf("ioctl %x {name} %zx %zx {int(check)} {int(heap)}\\n", (unsigned){name}, '
f'sizeof({name}_PARAMS), offsetof({name}_PARAMS, rmStatus));')
for field in FIELDS.get(name, ()):
program.append(
f'printf("field {name} {field} %zx %zx\\n", offsetof({name}_PARAMS, {field}), '
f'sizeof((({name}_PARAMS *)0)->{field}));')
program.append('printf("attributes %zx\\n", sizeof(UvmGpuMappingAttributes));')
for field in ATTRIBUTES:
program.append(
f'printf("attribute {field} %zx %zx\\n", offsetof(UvmGpuMappingAttributes, {field}), '
f'sizeof(((UvmGpuMappingAttributes *)0)->{field}));')
program += ['printf("constant UVM_DEINITIALIZE %x\\n", (unsigned)UVM_DEINITIALIZE);',
'printf("constant UVM_MAX_GPUS %x\\n", (unsigned)UVM_MAX_GPUS);',
'printf("constant UVM_INIT_FLAGS_MASK %llx\\n", '
'(unsigned long long)UVM_INIT_FLAGS_MASK);',
'return 0; }']
with tempfile.TemporaryDirectory() as directory:
source = Path(directory) / "layout.c"
source.write_text("\n".join(program))
binary = Path(directory) / "layout"
subprocess.run(["gcc", f"-I{uvm}", f"-I{include}", str(source), "-o", str(binary)],
check=True)
return subprocess.run([str(binary)], check=True, capture_output=True, text=True).stdout
def main():
text = layout(REFERENCE / "nvidia-uvm", REFERENCE / "common/inc")
trees = ["reference"]
if INSTALLED.exists():
assert layout(INSTALLED / "nvidia-uvm", INSTALLED / "common/inc") == text, \
"the installed and the reference source disagree"
trees.append("installed")
text = ("# /dev/nvidia-uvm ioctl layouts from the public source; see "
"tools/check_uvm_ioctl_layout.py\n" + text)
target = ROOT / "target/uvm/ioctl-layout.txt"
target.parent.mkdir(parents=True, exist_ok=True)
if "--check" in sys.argv:
assert target.read_text() == text, "layout record differs from the checked copy"
else:
target.write_text(text)
print(json.dumps(dict(record=str(target.relative_to(ROOT)), trees=trees,
sha256=hashlib.sha256(text.encode()).hexdigest(),
ioctls=text.count("\nioctl "), fields=text.count("\nfield "))))
if __name__ == "__main__":
main()