Skip to contentSkip to conversation

hw/vfio: recover from disabled-BAR mmap SIGBUS as Unsupported Request

RFC PATCH New [email protected] 1 participant 1 message Jul 16

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 hw/vfio: recover from disabled-BAR mmap SIGBUS as Unsupported Request

From: Mikhail Malyshev <[email protected]>

A guest that clears PCI_COMMAND.MEM on a passed-through device makes vfio-pci invalidate the device's BAR mmaps in the kernel: any access to them then faults with BUS_ADRERR. A vCPU MMIO access that races the resulting BAR teardown can reach memory_region_ram_device_{read,write}() against a mmap the kernel has already invalidated and take SIGBUS, which qemu turns into a fatal abort of the whole VM.

This is not a contrived race: real multi-vCPU guest drivers clear a device's PCI_COMMAND.MEM (memory decode) on one vCPU while other vCPUs are still issuing MMIO to that device's BAR, and this has been observed with Intel integrated-GPU drivers in a Windows guest under vfio passthrough, so the racing MMIO during teardown is ordinary guest behavior.

On real hardware an access to a BAR whose memory decode is disabled is answered by the PCIe layer with an Unsupported Request: reads return all-ones, writes are dropped, and the instruction completes. Emulate that instead of aborting.

Record each ram_device mmap host range as it is (un)mapped, arm a per-thread sigsetjmp checkpoint around the raw load/store in the ram_device ops, and have the SIGBUS handler longjmp back to it when a non-MCE fault address falls in one of those ranges. Mirrors the SIGBUS-recovery idiom already used by qemu_prealloc_mem(). MCE and non-vfio SIGBUS are unaffected.

Notes:
- A compiler barrier() brackets the guarded load/store so the ram_device_active guard is ordered around the faulting access rather than depending on codegen.
- Overflowing the mmap range table warns rather than silently leaving a range untracked (which would reintroduce the abort).
- sigsetjmp saves the signal mask (an rt_sigprocmask per trapped access); it cannot be dropped without leaving SIGBUS blocked after siglongjmp, so it is a deliberate cost on the trapped-MMIO slow path only, never the EPT fast path.

---

Sent as RFC because:

(a) This is the VMM/SIGBUS half of a two-part PCI_COMMAND.MEM teardown-edge race. The KVM -EFAULT half (the kernel-side arrival of the same race) is handled separately via KVM_EXIT_MEMORY_FAULT and discussed at the Link above; a coordinated qemu memory-fault handler for that half will follow.

(b) The mmap range table currently lives in the generic system/memory.c ram_device path. It could instead be relocated into vfio, since vfio is presently the only user of ram_device regions that hits this race in practice.

(c) The per-access sigsetjmp on the trapped-MMIO slow path, and the alternative of keeping the ram_device MemoryRegion's enabled state synced with PCI_COMMAND.MEM (so the race window is closed instead of recovered from), are both open to maintainer direction.

(d) There is no clean single qemu commit that introduced this fault (it is the kernel vfio-pci mmap invalidation on PCI_COMMAND.MEM clear that makes the mapping fault); commit 4a2e242bbb30 ("memory: Don't use memcpy for ram_device regions") is only where the now-recoverable ram_device dereference lives, so no Fixes:
tag is asserted.

 hw/vfio/region.c        |   5 ++
 include/system/memory.h |  13 ++++
 system/cpus.c           |   9 +++
 system/memory.c         | 131 +++++++++++++++++++++++++++++++++++++++-
 4 files changed, 157 insertions(+), 1 deletion(-)
hw/vfio/region.c +5 -0
index dbde339180..30f10e5857 100644
@@ -280,6 +280,7 @@ static void vfio_subregion_unmap(VFIORegion *region, int index)
                             region->mmaps[index].offset +
                             region->mmaps[index].size - 1);
     memory_region_del_subregion(region->mem, &region->mmaps[index].mem);
+    memory_region_ram_device_del_range(region->mmaps[index].mmap);
     munmap(region->mmaps[index].mmap, region->mmaps[index].size);
     object_unparent(OBJECT(&region->mmaps[index].mem));
     region->mmaps[index].mmap = NULL;
@@ -428,6 +429,9 @@ int vfio_region_mmap(VFIORegion *region)
                                           memory_region_owner(region->mem),
                                           name, region->mmaps[i].size,
                                           region->mmaps[i].mmap);
+        /* track the mmap host range for disabled-BAR SIGBUS -> UR recovery */
+        memory_region_ram_device_add_range(region->mmaps[i].mmap,
+                                           region->mmaps[i].size);
         g_free(name);
         memory_region_add_subregion(region->mem, region->mmaps[i].offset,
                                     &region->mmaps[i].mem);
@@ -495,6 +499,7 @@ void vfio_region_finalize(VFIORegion *region)
 
     for (i = 0; i < region->nr_mmaps; i++) {
         if (region->mmaps[i].mmap) {
+            memory_region_ram_device_del_range(region->mmaps[i].mmap);
             munmap(region->mmaps[i].mmap, region->mmaps[i].size);
         }
     }
include/system/memory.h +13 -0
index 47a0e06fbf..bcf1b4e735 100644
@@ -1278,6 +1278,19 @@ void memory_region_init_ram_device_ptr(MemoryRegion *mr,
                                        uint64_t size,
                                        void *ptr);
 
+/*
+ * vfio disabled-BAR SIGBUS recovery (see system/memory.c). vfio registers each
+ * BAR mmap host range so a racing MMIO into a just-invalidated mmap can be
+ * completed as an Unsupported Request instead of aborting qemu.
+ */
+void memory_region_ram_device_add_range(void *host, size_t size);
+void memory_region_ram_device_del_range(void *host);
+/*
+ * called from the SIGBUS handler; longjmps (no return) when it recovers.
+ * Recovers only for a BUS_ADRERR fault address in a tracked range.
+ */
+bool memory_region_ram_device_on_sigbus(void *addr, int si_code);
+
 /**
  * memory_region_init_alias: Initialize a memory region that aliases all or a
  *                           part of another memory region.
system/cpus.c +9 -0
index 97e5a5edee..22f77321b1 100644
@@ -33,6 +33,7 @@
 #include "exec/gdbstub.h"
 #include "accel/accel-cpu-ops.h"
 #include "system/hw_accel.h"
+#include "system/memory.h"
 #include "exec/cpu-common.h"
 #include "qemu/thread.h"
 #include "qemu/main-loop.h"
@@ -382,6 +383,14 @@ static void sigbus_reraise(void)
 static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
 {
     if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
+        /*
+         * Not an MCE. If this is a racing MMIO into a passed-through vfio BAR
+         * whose PCI_COMMAND.MEM was just cleared (mmap invalidated by the
+         * kernel), complete it as an Unsupported Request instead of aborting.
+         * memory_region_ram_device_on_sigbus() longjmps back into the
+         * ram_device access and does not return when it recovers.
+         */
+        memory_region_ram_device_on_sigbus(siginfo->si_addr, siginfo->si_code);
         sigbus_reraise();
     }
 
system/memory.c +130 -1

154 lines not on this page · raw message

Signature · 1 line
2.43.0