Skip to contentSkip to conversation

um: Add VFIO-based PCI passthrough support

RFC PATCH New Tiwei Bie 1 participant 4 messages Mar 10, 2025 User-mode Linux (UML)

lore
Share & tools
lore

Tiwei Bie

author
#0 lore raw

cover um: Add VFIO-based PCI passthrough support

This patchset adds a new virt-pci driver to UML that allows users to pass through host PCI devices to UML via VFIO. Currently, only MSI-X capable devices are supported, and drivers are assumed to use MSI-X.

This driver has been tested with virtio-net-pci, virtio-blk-pci and nvme in a QEMU virtual machine with virtual IOMMU enabled. Regression testing for virtio_pcidev has been limited as I haven't been able to find a virtio_pcidev vhost-user backend implementation.

This patchset is based on the following patchset:
https://lore.kernel.org/all/[email protected]/

Tiwei Bie (3):
um: Rewrite the sigio workaround based on epoll and tgkill
um: virt-pci: Refactor virtio_pcidev into its own module
um: Add VFIO-based virtual PCI driver

 arch/um/drivers/Kconfig         |  20 +-
 arch/um/drivers/Makefile        |   5 +-
 arch/um/drivers/random.c        |   2 +-
 arch/um/drivers/rtc_user.c      |   2 +-
 arch/um/drivers/vfio_kern.c     | 648 +++++++++++++++++++++++++++++
 arch/um/drivers/vfio_user.c     | 323 +++++++++++++++
 arch/um/drivers/vfio_user.h     |  44 ++
 arch/um/drivers/virt-pci.c      | 699 ++++++--------------------------
 arch/um/drivers/virt-pci.h      |  41 ++
 arch/um/drivers/virtio_pcidev.c | 628 ++++++++++++++++++++++++++++
 arch/um/include/shared/os.h     |   2 +-
 arch/um/include/shared/sigio.h  |   1 -
 arch/um/kernel/sigio.c          |  26 --
 arch/um/os-Linux/sigio.c        | 330 ++-------------
 14 files changed, 1866 insertions(+), 905 deletions(-)
 create mode 100644 arch/um/drivers/vfio_kern.c
 create mode 100644 arch/um/drivers/vfio_user.c
 create mode 100644 arch/um/drivers/vfio_user.h
 create mode 100644 arch/um/drivers/virt-pci.h
 create mode 100644 arch/um/drivers/virtio_pcidev.c
Signature · 1 line
2.34.1
author
#1 lore raw

RFC PATCH 1/3 um: Rewrite the sigio workaround based on epoll and tgkill

The current implementation is somewhat convoluted and inefficient, and can lead to race conditions when updating sigio fds. This change rewrites the implementation based on epoll and tgkill for improved scalability and reliability.

---
 arch/um/drivers/random.c       |   2 +-
 arch/um/drivers/rtc_user.c     |   2 +-
 arch/um/include/shared/os.h    |   2 +-
 arch/um/include/shared/sigio.h |   1 -
 arch/um/kernel/sigio.c         |  26 ---
 arch/um/os-Linux/sigio.c       | 330 +++++----------------------------
 6 files changed, 47 insertions(+), 316 deletions(-)
arch/um/drivers/random.c +1 -1
index da985e0dc69a..ca08c91f47a3 100644
@@ -79,7 +79,7 @@ static int __init rng_init (void)
 	if (err < 0)
 		goto err_out_cleanup_hw;
 
-	sigio_broken(random_fd);
+	sigio_broken();
 	hwrng.name = RNG_MODULE_NAME;
 	hwrng.read = rng_dev_read;
 
arch/um/drivers/rtc_user.c +1 -1
index 7c3cec4c68cf..51e79f3148cd 100644
@@ -39,7 +39,7 @@ int uml_rtc_start(bool timetravel)
 		}
 
 		/* apparently timerfd won't send SIGIO, use workaround */
-		sigio_broken(uml_rtc_irq_fds[0]);
+		sigio_broken();
 		err = add_sigio_fd(uml_rtc_irq_fds[0]);
 		if (err < 0) {
 			close(uml_rtc_irq_fds[0]);
arch/um/include/shared/os.h +1 -1
index c4f8f990ffb8..9a146912dd75 100644
@@ -315,7 +315,7 @@ extern void um_irqs_resume(void);
 extern int add_sigio_fd(int fd);
 extern int ignore_sigio_fd(int fd);
 extern void maybe_sigio_broken(int fd);
-extern void sigio_broken(int fd);
+extern void sigio_broken(void);
 /*
  * unlocked versions for IRQ controller code.
  *
arch/um/include/shared/sigio.h +0 -1
index e60c8b227844..c6c2edce1f6d 100644
@@ -6,7 +6,6 @@
 #ifndef __SIGIO_H__
 #define __SIGIO_H__
 
-extern int write_sigio_irq(int fd);
 extern void sigio_lock(void);
 extern void sigio_unlock(void);
 
arch/um/kernel/sigio.c +0 -26
index 5085a50c3b8c..4fc04742048a 100644
@@ -8,32 +8,6 @@
 #include <os.h>
 #include <sigio.h>
 
-/* Protected by sigio_lock() called from write_sigio_workaround */
-static int sigio_irq_fd = -1;
-
-static irqreturn_t sigio_interrupt(int irq, void *data)
-{
-	char c;
-
-	os_read_file(sigio_irq_fd, &c, sizeof(c));
-	return IRQ_HANDLED;
-}
-
-int write_sigio_irq(int fd)
-{
-	int err;
-
-	err = um_request_irq(SIGIO_WRITE_IRQ, fd, IRQ_READ, sigio_interrupt,
-			     0, "write sigio", NULL);
-	if (err < 0) {
-		printk(KERN_ERR "write_sigio_irq : um_request_irq failed, "
-		       "err = %d\n", err);
-		return -1;
-	}
-	sigio_irq_fd = fd;
-	return 0;
-}
-
 /* These are called from os-Linux/sigio.c to protect its pollfds arrays. */
 static DEFINE_MUTEX(sigio_mutex);
 
arch/um/os-Linux/sigio.c +44 -286

397 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#2 lore raw

RFC PATCH 2/3 um: virt-pci: Refactor virtio_pcidev into its own module

In reply to Tiwei Bie

Decouple virt-pci and virtio_pcidev, refactoring virtio_pcidev into its own module. Define a set of APIs for virt-pci. This allows for future addition of more PCI emulation implementations.

---
 arch/um/drivers/Kconfig         |  12 +-
 arch/um/drivers/Makefile        |   3 +-
 arch/um/drivers/virt-pci.c      | 699 ++++++--------------------------
 arch/um/drivers/virt-pci.h      |  41 ++
 arch/um/drivers/virtio_pcidev.c | 628 ++++++++++++++++++++++++++++
 5 files changed, 794 insertions(+), 589 deletions(-)
 create mode 100644 arch/um/drivers/virt-pci.h
 create mode 100644 arch/um/drivers/virtio_pcidev.c
arch/um/drivers/Kconfig +8 -4
index ede40a160c5e..9cb196070614 100644
@@ -345,16 +345,20 @@ config UML_RTC
 	  by providing a fake RTC clock that causes a wakeup at the right
 	  time.
 
-config UML_PCI_OVER_VIRTIO
-	bool "Enable PCI over VIRTIO device simulation"
-	# in theory, just VIRTIO is enough, but that causes recursion
-	depends on VIRTIO_UML
+config UML_PCI
+	bool
 	select FORCE_PCI
 	select UML_IOMEM_EMULATION
 	select UML_DMA_EMULATION
 	select PCI_MSI
 	select PCI_LOCKLESS_CONFIG
 
+config UML_PCI_OVER_VIRTIO
+	bool "Enable PCI over VIRTIO device simulation"
+	# in theory, just VIRTIO is enough, but that causes recursion
+	depends on VIRTIO_UML
+	select UML_PCI
+
 config UML_PCI_OVER_VIRTIO_DEVICE_ID
 	int "set the virtio device ID for PCI emulation"
 	default -1
arch/um/drivers/Makefile +2 -1
index 57882e6bc215..0a5820343ad3 100644
@@ -60,7 +60,8 @@ obj-$(CONFIG_BLK_DEV_COW_COMMON) += cow_user.o
 obj-$(CONFIG_UML_RANDOM) += random.o
 obj-$(CONFIG_VIRTIO_UML) += virtio_uml.o
 obj-$(CONFIG_UML_RTC) += rtc.o
-obj-$(CONFIG_UML_PCI_OVER_VIRTIO) += virt-pci.o
+obj-$(CONFIG_UML_PCI) += virt-pci.o
+obj-$(CONFIG_UML_PCI_OVER_VIRTIO) += virtio_pcidev.o
 
 # pcap_user.o must be added explicitly.
 USER_OBJS := fd.o null.o pty.o tty.o xterm.o slip_common.o vde_user.o vector_user.o
arch/um/drivers/virt-pci.c +115 -584

860 lines not on this page · raw message

arch/um/drivers/virt-pci.h new file +41 -0

41 lines not on this page · raw message

arch/um/drivers/virtio_pcidev.c new file +628 -0

628 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#3 lore raw

RFC PATCH 3/3 um: Add VFIO-based virtual PCI driver

In reply to Tiwei Bie

Implement a new virtual PCI driver based on the VFIO framework. This driver allows users to pass through PCI devices to UML via VFIO. Currently, only MSI-X capable devices are supported, and it is assumed that drivers will use MSI-X.

---
 arch/um/drivers/Kconfig     |   8 +
 arch/um/drivers/Makefile    |   2 +
 arch/um/drivers/vfio_kern.c | 648 ++++++++++++++++++++++++++++++++++++
 arch/um/drivers/vfio_user.c | 323 ++++++++++++++++++
 arch/um/drivers/vfio_user.h |  44 +++
 5 files changed, 1025 insertions(+)
 create mode 100644 arch/um/drivers/vfio_kern.c
 create mode 100644 arch/um/drivers/vfio_user.c
 create mode 100644 arch/um/drivers/vfio_user.h
arch/um/drivers/Kconfig +8 -0
index 9cb196070614..d7bb447ff958 100644
@@ -367,3 +367,11 @@ config UML_PCI_OVER_VIRTIO_DEVICE_ID
 	  There's no official device ID assigned (yet), set the one you
 	  wish to use for experimentation here. The default of -1 is
 	  not valid and will cause the driver to fail at probe.
+
+config UML_PCI_OVER_VFIO
+	bool "Enable VFIO-based PCI passthrough"
+	select UML_PCI
+	help
+	  This driver provides support for VFIO-based PCI passthrough.
+	  Currently, only MSI-X capable devices are supported, and it
+	  is assumed that drivers will use MSI-X.
arch/um/drivers/Makefile +2 -0
index 0a5820343ad3..336be56b8975 100644
@@ -19,6 +19,7 @@ port-objs := port_kern.o port_user.o
 harddog-objs := harddog_kern.o
 harddog-builtin-$(CONFIG_UML_WATCHDOG) := harddog_user.o harddog_user_exp.o
 rtc-objs := rtc_kern.o rtc_user.o
+vfio_uml-objs := vfio_kern.o vfio_user.o
 
 LDFLAGS_vde.o = $(shell $(CC) $(CFLAGS) -print-file-name=libvdeplug.a)
 
@@ -62,6 +63,7 @@ obj-$(CONFIG_VIRTIO_UML) += virtio_uml.o
 obj-$(CONFIG_UML_RTC) += rtc.o
 obj-$(CONFIG_UML_PCI) += virt-pci.o
 obj-$(CONFIG_UML_PCI_OVER_VIRTIO) += virtio_pcidev.o
+obj-$(CONFIG_UML_PCI_OVER_VFIO) += vfio_uml.o
 
 # pcap_user.o must be added explicitly.
 USER_OBJS := fd.o null.o pty.o tty.o xterm.o slip_common.o vde_user.o vector_user.o
arch/um/drivers/vfio_kern.c new file +648 -0

648 lines not on this page · raw message

arch/um/drivers/vfio_user.c new file +323 -0

323 lines not on this page · raw message

arch/um/drivers/vfio_user.h new file +44 -0

44 lines not on this page · raw message

Signature · 1 line
2.34.1