From 5d4d26f8e773f2871c1b5c9266b82495d390970e Mon Sep 17 00:00:00 2001 From: Arthur Hanson Rasmusson Date: Mon, 21 Sep 2026 12:37:46 -0400 Subject: kernel: Fix guest-hang root cause (VFIO mmap garbage PFN) + rebuild mdev remove path VFIO mmap (main.c): virt_to_phys() was used on vmalloc-family addresses (vzalloc'd BAR0 shadow, ioremap'd BAR2/3), producing garbage PFNs. Guest BAR0 access then hit a KVM make_spte reserved-bits WARN and an EPT-violation livelock, hard-wedging the guest on every nvidia driver probe (confirmed via QEMU GDB stub). BAR0 now maps per-page via vmalloc_to_pfn(); BAR2/3/4/5 map bars[N].gpa directly (like BAR1); config space uses new nv_vgpu_mmap_virt_to_pfn() helper. mdev remove path (main.c): old path never list_del'd the device (available_instances leaked), never ran cleanup (leaked gsp_emu kthread), never unregistered VFIO (leaked /dev/vfio/N + iommu group), never put pdev, never freed the struct. New kvmgt-style path: cancel init_work -> wait fds -> vfio_unregister_group_dev -> cleanup work -> list_del -> pci_dev_put -> vfio_put_device. gsp_rpc_client.c: fix dma_map_page dma_addr_t->int truncation in gsp_rpc_alloc_emul (corrupts >=4GB IOVAs); export gsp_client_init_emulation. gsp_emulation.c: export gsp_emu_process_rpc and gsp_emu_get_stats. main.c: remove wrong MODULE_SOFTDEP("pre: nvidia") - module has zero nvidia imports, loads standalone, and must load BEFORE nvidia.ko (it exports nvidia_vgpu_vfio_* for nvidia.ko to import). nvidia-vgpu-vfio.h: NULL-safe nv_vgpu_{dbg,info,warn,err} macros. tests/gsp_emulation_test.c: rewritten against current API - drives gsp_emu_process_rpc() with crafted shared-memory messages (protocol validation, SCHEDULE/YIELD/NOTIFY/DEBUG dispatch, stress). Builds with KBUILD_EXTRA_SYMBOLS; depends: open-iov-vfio. stack_manager.sh: vfio_mdev doesn't exist on >=6.4 (probe per-module); load-nvidia detects foreign nvidia via /sys/module/nvidia/version and errors with guidance instead of silently skipping. --- kernel/include/nvidia-vgpu-vfio.h | 40 ++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) (limited to 'kernel/include') diff --git a/kernel/include/nvidia-vgpu-vfio.h b/kernel/include/nvidia-vgpu-vfio.h index 3ca5cf6..02c2c13 100644 --- a/kernel/include/nvidia-vgpu-vfio.h +++ b/kernel/include/nvidia-vgpu-vfio.h @@ -191,16 +191,34 @@ static inline struct nv_vgpu_device *mdev_to_vgpu(struct mdev_device *mdev) return dev_get_drvdata(&mdev->dev); } -#define nv_vgpu_dbg(vgpu, fmt, ...) \ - dev_dbg(&(vgpu)->dev, "%s: " fmt, __func__, ##__VA_ARGS__) - -#define nv_vgpu_info(vgpu, fmt, ...) \ - dev_info(&(vgpu)->dev, "%s: " fmt, __func__, ##__VA_ARGS__) - -#define nv_vgpu_warn(vgpu, fmt, ...) \ - dev_warn(&(vgpu)->dev, "%s: " fmt, __func__, ##__VA_ARGS__) - -#define nv_vgpu_err(vgpu, fmt, ...) \ - dev_err(&(vgpu)->dev, "%s: " fmt, __func__, ##__VA_ARGS__) +/* NULL-tolerant logging: emulation paths may run with a NULL vgpu + * (e.g. unit tests, early init) - fall back to plain pr_*. */ +#define nv_vgpu_dbg(vgpu, fmt, ...) do { \ + if (vgpu) \ + dev_dbg(&(vgpu)->dev, "%s: " fmt, __func__, ##__VA_ARGS__); \ + else \ + pr_debug("%s: " fmt, __func__, ##__VA_ARGS__); \ +} while (0) + +#define nv_vgpu_info(vgpu, fmt, ...) do { \ + if (vgpu) \ + dev_info(&(vgpu)->dev, "%s: " fmt, __func__, ##__VA_ARGS__); \ + else \ + pr_info("%s: " fmt, __func__, ##__VA_ARGS__); \ +} while (0) + +#define nv_vgpu_warn(vgpu, fmt, ...) do { \ + if (vgpu) \ + dev_warn(&(vgpu)->dev, "%s: " fmt, __func__, ##__VA_ARGS__); \ + else \ + pr_warn("%s: " fmt, __func__, ##__VA_ARGS__); \ +} while (0) + +#define nv_vgpu_err(vgpu, fmt, ...) do { \ + if (vgpu) \ + dev_err(&(vgpu)->dev, "%s: " fmt, __func__, ##__VA_ARGS__); \ + else \ + pr_err("%s: " fmt, __func__, ##__VA_ARGS__); \ +} while (0) #endif \ No newline at end of file -- cgit v1.2.3