From 7e0f2a9d6541e7f61de764b79f5b7113bc3a0329 Mon Sep 17 00:00:00 2001 From: morebrackets <19673572+morebrackets@users.noreply.github.com> Date: Thu, 3 Dec 2026 00:14:20 -0800 Subject: [PATCH] Address Greptile findings: dynamic phys-bits for multi-GPU, full SGEMM check, P4 setup docs --- tests/p4/p4_smoke.cu | 8 +++++- tests/p4/run_guest.sh | 20 +++++++++++++-- tests/vm/test_vfio_guest_helpers.py | 9 +++++++ tools/vm/run_vfio_guest.py | 39 +++++++++++++++++++++++------ 4 files changed, 66 insertions(+), 10 deletions(-) diff --git a/tests/p4/p4_smoke.cu b/tests/p4/p4_smoke.cu index a125afe5..9c275b76 100644 --- a/tests/p4/p4_smoke.cu +++ b/tests/p4/p4_smoke.cu @@ -89,7 +89,13 @@ int main(void) { printf("SMOKE: sgemm %dx%d time=%.3f ms %.2f GFLOPS expect=%.0f got=%.0f\n", g, g, ms, gflops, (double)g, (double)h_C[0]); if (!(gflops >= 1e-9)) return 1; - if (!(fabs((double)h_C[12345] - (double)g) <= 0.5)) { printf("SMOKE: FAIL sgemm result\n"); return 1; } + for (int i = 0; i < g * g; ++i) { + if (!(fabs((double)h_C[i] - (double)g) <= 0.5)) { + printf("SMOKE: FAIL sgemm result at index %d (got=%g expect=%g)\n", + i, (double)h_C[i], (double)g); + return 1; + } + } CK(cudaFree(d_a)); CK(cudaFree(d_b)); CK(cudaFree(d_c)); CK(cudaFree(d_A)); CK(cudaFree(d_B)); CK(cudaFree(d_C)); diff --git a/tests/p4/run_guest.sh b/tests/p4/run_guest.sh index 4ca46da6..2ce04c6d 100644 --- a/tests/p4/run_guest.sh +++ b/tests/p4/run_guest.sh @@ -1,6 +1,17 @@ #!/bin/sh -# Guest-side wrapper: stage the real /proc/driver/nvidia version nodes, dump nvdriver's -# telemetry surface, then run the sm_61 CUDA smoke test. +# Guest-side wrapper: stage optional /proc/driver/nvidia version/registry nodes, dump +# nvdriver's telemetry surface, then run the sm_61 CUDA smoke test. +# +# Host-side setup before launching via tools/vm/run_vfio_guest.py: +# 1. Build the sm_61 smoke binary (static cudart so it uses host /usr libcuda.so.1): +# mkdir -p target/p4 +# nvcc -arch=sm_61 --cudart=static tests/p4/p4_smoke.cu -o target/p4/p4_smoke +# 2. (Optional) Capture stock /proc/driver/nvidia/{version,registry} from a stock host +# or --driver stock VM run if you want matching version/registry files staged: +# mkdir -p target/p4/procdriver-files +# cp /proc/driver/nvidia/version /proc/driver/nvidia/registry target/p4/procdriver-files/ +# 3. Run inside the VFIO guest: +# python3 tools/vm/run_vfio_guest.py --bdf --driver rust --cmd /repo/tests/p4/run_guest.sh for f in /repo/target/p4/procdriver-files/*; do [ -e "$f" ] || continue cp -f "$f" /tmp/proc_driver/nvidia/ 2>/dev/null || true @@ -24,6 +35,11 @@ echo "--- EVIDENCE: nvidia-smi ---" /usr/bin/nvidia-smi --query-gpu=name,driver_version,memory.total --format=csv,noheader 2>&1 | head -4 echo "--- SMOKE ---" +if [ ! -x /repo/target/p4/p4_smoke ]; then + echo "SMOKE: FAIL missing /repo/target/p4/p4_smoke" + echo "Build on host first: mkdir -p target/p4 && nvcc -arch=sm_61 --cudart=static tests/p4/p4_smoke.cu -o target/p4/p4_smoke" + exit 1 +fi /repo/target/p4/p4_smoke RC=$? diff --git a/tests/vm/test_vfio_guest_helpers.py b/tests/vm/test_vfio_guest_helpers.py index c31c30a6..a11772b2 100644 --- a/tests/vm/test_vfio_guest_helpers.py +++ b/tests/vm/test_vfio_guest_helpers.py @@ -257,6 +257,15 @@ class GuestCpuTests(unittest.TestCase): with mock.patch.dict(os.environ, {"NV_RS_VM_CPU": "host,phys-bits=39"}, clear=False): self.assertEqual(vfio.guest_cpu(), "host,phys-bits=39") + def test_eight_gpu_window_expands_phys_bits_limit(self) -> None: + with mock.patch.dict(os.environ, {}, clear=False): + os.environ.pop("NV_RS_VM_CPU", None) + # 8 GPUs: 16 GiB RAM + 512 GiB 64-bit PCI hole cannot fit in 39 bits (512 GiB). + self.assertEqual( + vfio.guest_cpu(ram_mb=16384, hole64_gb=512), + "host,host-phys-bits=on,host-phys-bits-limit=40", + ) + class BuildQemuCmdTests(unittest.TestCase): """QEMU argument assembly is pure, so its shape can be asserted without a GPU.""" diff --git a/tools/vm/run_vfio_guest.py b/tools/vm/run_vfio_guest.py index ff071646..22819f28 100755 --- a/tools/vm/run_vfio_guest.py +++ b/tools/vm/run_vfio_guest.py @@ -658,18 +658,43 @@ poweroff -f sh(f"(cd {fs} && find . -print0 | cpio --null -o -H newc 2>/dev/null | gzip -n -1 > {initramfs_path})") -DEFAULT_GUEST_CPU = "host,host-phys-bits=on,host-phys-bits-limit=39" +DEFAULT_PHYS_BITS_LIMIT = 39 +DEFAULT_GUEST_CPU = f"host,host-phys-bits=on,host-phys-bits-limit={DEFAULT_PHYS_BITS_LIMIT}" -def guest_cpu() -> str: +def required_guest_phys_bits(*, ram_mb: int = 4096, hole64_gb: int = 64) -> int: + """Minimum guest physical address width needed for RAM + aligned 64-bit PCI hole. + + Clamped to at least 39 bits (512 GiB) so single/small-GPU guests fit inside a 39-bit + host DMAR aperture, while expanding automatically for large multi-GPU guests (e.g. + 8 GPUs -> 512 GiB PCI hole + 16 GiB RAM -> 40 bits / 1024 GiB).""" + ram_gb = (max(0, ram_mb) + 1023) // 1024 + hole = max(0, hole64_gb) + if hole > 0: + # QEMU aligns the 64-bit PCI hole above guest RAM + 4 GiB low MMIO. + hole_base = ((ram_gb + 4 + hole - 1) // hole) * hole + top_gb = hole_base + hole + else: + top_gb = max(1, ram_gb + 4) + bits = 30 + (top_gb - 1).bit_length() + return max(DEFAULT_PHYS_BITS_LIMIT, bits) + + +def guest_cpu(*, ram_mb: int = 4096, hole64_gb: int = 64) -> str: """Guest CPU model for the FW/VM. - The physical address width is clamped to the host IOMMU aperture by default. With - host-phys-bits=on alone, q35 places the GPU's 64-bit BARs far above the aperture a - 39-bit DMAR advertises, and every BAR map then fails with `VFIO_MAP_DMA ... EINVAL`. + The physical address width is clamped to the host IOMMU aperture by default (39 bits + for single/small-GPU guests), expanding only when the requested RAM + 64-bit PCI + window requires more address space (such as 8-GPU guests). With host-phys-bits=on + alone, q35 places the GPU's 64-bit BARs far above the aperture a 39-bit DMAR + advertises, and every BAR map then fails with `VFIO_MAP_DMA ... EINVAL`. `phys-bits=39` is not a substitute: QEMU only warns about the mismatch and leaves the 64-bit hole where it was. Override with NV_RS_VM_CPU.""" - return os.environ.get("NV_RS_VM_CPU", DEFAULT_GUEST_CPU) + override = os.environ.get("NV_RS_VM_CPU") + if override is not None: + return override + limit_bits = required_guest_phys_bits(ram_mb=ram_mb, hole64_gb=hole64_gb) + return f"host,host-phys-bits=on,host-phys-bits-limit={limit_bits}" def build_qemu_cmd( @@ -689,7 +714,7 @@ def build_qemu_cmd( qemu_cmd = [ "sudo", "-n", "qemu-system-x86_64", "-machine", "q35,accel=kvm", - "-cpu", cpu or guest_cpu(), + "-cpu", cpu or guest_cpu(ram_mb=ram_mb, hole64_gb=hole64_gb), "-m", str(ram_mb), "-smp", "4", "-nodefaults",