um: Add VFIO-based PCI passthrough support
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 only covered compile tests and partial virtio probe/remove tests with a dummy vhost-user backend.
Here are some steps to try out this driver:
1. Enable IOMMU and bind PCI devices to the vfio-pci driver.
2. Launch a UML instance directly (assuming that the PCI devices to be passed through to UML are 0000:02:00.0 and 0000:03:00.0).
$ ./linux mem=2G init=/bin/sh ubd0=$your_rootfs_image \
vfio_uml.device=0000:02:00.0 \
vfio_uml.device=0000:03:00.0Note that, currently, it's not possible to allocate a large amount of "physical" memory to the UML instance; otherwise, it may overlap with IOMMU-reserved IOVA ranges (e.g. [0xfee00000, 0xfef00000)), causing DMA mapping failures.
This patchset is based on the following patchset:
https://lore.kernel.org/all/[email protected]/
v1:
- Add more details in the commit log;
RFC: 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
PATCH 1/3 um: Rewrite the sigio workaround based on epoll and tgkill
The existing sigio workaround implementation removes FDs from the poll when events are triggered, requiring users to re-add them via add_sigio_fd() after processing. This introduces a potential race condition between FD removal in write_sigio_thread() and next_poll update in __add_sigio_fd(), and is inefficient due to frequent FD removal and re-addition. Rewrite the implementation based on epoll and tgkill for improved efficiency 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH 2/3 um: virt-pci: Refactor virtio_pcidev into its own module
In reply to Tiwei BieDecouple 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
@@ -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
@@ -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
PATCH 3/3 um: Add VFIO-based virtual PCI driver
In reply to Tiwei BieImplement 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
@@ -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
@@ -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
Johannes Berg
maintainerRe: [PATCH 3/3] um: Add VFIO-based virtual PCI driver
Quoted 4 lines · 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.
Seems nice, but I haven't tested it now :)
Quoted 9 lines
arch/um/drivers/vfio_kern.c new file +9
-0+static irqreturn_t uml_vfio_interrupt (int unused,void *opaque) +{ +struct uml_vfio_intr_ctx *ctx = opaque; +struct uml_vfio_device *dev = ctx->dev; +int index = ctx - dev->intr_ctx; +int irqfd = dev->udev.irqfd[index]; +int irq = dev->msix_data[index]; +uint64_t v; +int r;
arch/um/drivers/vfio_kern.c line 188
Quoted 10 lines
arch/um/drivers/vfio_kern.c new file +10
-0+ +do { + r =os_read_file (irqfd, &v,sizeof (v)); +if (r ==sizeof (v)) +generic_handle_irq (irq); + }while (r ==sizeof (v) || r == -EINTR ); +WARN (r != -EAGAIN ,"read returned %d\n" , r); + +return IRQ_HANDLED ; +}
This seems mostly right - looks like it's an eventfd.
arch/um/drivers/vfio_kern.c line 204
Quoted 7 lines
arch/um/drivers/vfio_kern.c new file +7
-0+ irqfd =uml_vfio_user_activate_irq (&dev->udev, index); +if (irqfd <0 ) +return irqfd; + + ctx->irq =um_request_irq (UM_IRQ_ALLOC , irqfd,IRQ_READ , + uml_vfio_interrupt,IRQF_SHARED , +"vfio-uml" , ctx);
However I'm not sure it's right with IRQF_SHARED since you don't detect if it was actually not your interrupt that fired? We don't really have any good reason to have shared interrupts anyway though, and I'm not sure we even *can* share interrupts in the lower layers in ARCH=um, so I'm not sure it matters?
arch/um/drivers/vfio_kern.c line 212
Quoted 3 lines
arch/um/drivers/vfio_kern.c new file +3
-0+ err =add_sigio_fd (irqfd); +if (err) + goto free_irq;
doesn't the irq framework do that automatically?
arch/um/drivers/vfio_kern.c line 230
Quoted 7 lines
arch/um/drivers/vfio_kern.c new file +7
-0+static int uml_vfio_deactivate_irq (struct uml_vfio_device *dev,int index) +{ +struct uml_vfio_intr_ctx *ctx = &dev->intr_ctx[index]; + +if (ctx->irq >=0 ) { +ignore_sigio_fd (dev->udev.irqfd[index]); +um_free_irq (ctx->irq, ctx);
and here too?
Quoted 6 lines
arch/um/drivers/vfio_kern.c new file +6
-0+static int uml_vfio_update_msix_cap (struct uml_vfio_device *dev, +unsigned int offset,int size, +unsigned long val) +{ +int err =0 ; +
arch/um/drivers/vfio_kern.c line 252
Quoted 10 lines
arch/um/drivers/vfio_kern.c new file +10
-0+if (size ==2 && offset == dev->msix_cap +PCI_MSIX_FLAGS ) { +switch (val & ~PCI_MSIX_FLAGS_QSIZE ) { +case PCI_MSIX_FLAGS_ENABLE : +case 0 : + err =uml_vfio_user_update_irqs (&dev->udev); +break ; + } + } + +return err;
do you really want to ignore and return 0 if it wasn't supported?
arch/um/drivers/vfio_kern.c line 264
Quoted 10 lines
arch/um/drivers/vfio_kern.c new file +10
-0+static int uml_vfio_update_msix_table (struct uml_vfio_device *dev, +unsigned int offset,int size, +unsigned long val) +{ +int index; + + offset -= dev->msix_offset +PCI_MSIX_ENTRY_DATA ; + +if (size !=4 || offset %PCI_MSIX_ENTRY_SIZE !=0 ) +return 0 ;
here too?
arch/um/drivers/vfio_kern.c line 343
Quoted 8 lines
arch/um/drivers/vfio_kern.c new file +8
-0+static void uml_vfio_cfgspace_write (struct um_pci_device *pdev, +unsigned int offset,int size, +unsigned long val) +{ +struct uml_vfio_device *dev =to_vdev (pdev); + +if (offset < dev->msix_cap +PCI_CAP_MSIX_SIZEOF && + offset + size > dev->msix_cap)
I'd probably indent the second line just to after "if ("?
arch/um/drivers/vfio_kern.c line 399
Quoted 3 lines
arch/um/drivers/vfio_kern.c new file +3
-0+if (bar == dev->msix_bar && offset + size > dev->msix_offset && + offset < dev->msix_offset + dev->msix_size) +WARN_ON (uml_vfio_update_msix_table (dev, offset, size, val));
likewise
arch/um/drivers/vfio_kern.c line 445
Quoted 5 lines
arch/um/drivers/vfio_kern.c new file +5
-0+static u8 uml_vfio_find_capability (struct uml_vfio_device *dev,u8 cap) +{ +u8 id, pos; +u16 ent; +int ttl =48 ;
any particular significance in that value?
arch/um/drivers/vfio_user.c line 59
Quoted 10 lines
arch/um/drivers/vfio_user.c new file +10
-0+int uml_vfio_user_setup_iommu (int container) +{ +unsigned long reserved = uml_reserved - uml_physmem; +struct vfio_iommu_type1_dma_map dma_map = { + .argsz =sizeof (dma_map), + .flags =VFIO_DMA_MAP_FLAG_READ |VFIO_DMA_MAP_FLAG_WRITE , + .vaddr = uml_reserved, + .iova = reserved, + .size = physmem_size - reserved, + };
maybe point over to the big comment in vhost_user_set_mem_table() from virtio_uml.c? Same things apply here I guess.
johannes
Re: [PATCH 3/3] um: Add VFIO-based virtual PCI driver
Quoted 7 lines · Johannes Berg
Quoted 4 lines · 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.
Seems nice, but I haven't tested it now :)
Thanks! :)
Quoted 14 lines
Quoted 14 lines
arch/um/drivers/vfio_kern.c new file +13
-0+static irqreturn_t uml_vfio_interrupt (int unused,void *opaque) +{ +struct uml_vfio_intr_ctx *ctx = opaque; +struct uml_vfio_device *dev = ctx->dev; +int index = ctx - dev->intr_ctx; +int irqfd = dev->udev.irqfd[index]; +int irq = dev->msix_data[index]; +uint64_t v; +int r; + + do { + r = os_read_file(irqfd, &v, sizeof(v)); + if (r == sizeof(v))
Quoted 8 lines
arch/um/drivers/vfio_kern.c line 188
Quoted 6 lines
arch/um/drivers/vfio_kern.c new file +6
-0+generic_handle_irq (irq); + }while (r ==sizeof (v) || r == -EINTR ); +WARN (r != -EAGAIN ,"read returned %d\n" , r); + +return IRQ_HANDLED ; +}This seems mostly right - looks like it's an eventfd.
Yeah, it's an eventfd.
Quoted 14 lines
arch/um/drivers/vfio_kern.c line 204
Quoted 8 lines
arch/um/drivers/vfio_kern.c new file +7
-0+ irqfd =uml_vfio_user_activate_irq (&dev->udev, index); +if (irqfd <0 ) +return irqfd; + + ctx->irq =um_request_irq (UM_IRQ_ALLOC , irqfd,IRQ_READ , + uml_vfio_interrupt,IRQF_SHARED , +"vfio-uml" , ctx);However I'm not sure it's right with
IRQF_SHAREDsince you don't detect if it was actually not your interrupt that fired? We don't really have any good reason to have shared interrupts anyway though, and I'm not sure we even *can* share interrupts in the lower layers in ARCH=um, so I'm not sure it matters?
Makes sense. Will fix it. Thanks!
Quoted 6 lines
arch/um/drivers/vfio_kern.c line 212
Quoted 4 lines
arch/um/drivers/vfio_kern.c new file +3
-0+ err =add_sigio_fd (irqfd); +if (err) + goto free_irq;doesn't the irq framework do that automatically?
Currently it's not supported. Users need to add it manually, e.g.:
https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/um/drivers/rtc_user.c?h=v6.14-rc7#n43
https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/um/drivers/rtc_kern.c?h=v6.14-rc7#n127
Quoted 21 lines
arch/um/drivers/vfio_kern.c line 230
Quoted 8 lines
arch/um/drivers/vfio_kern.c new file +7
-0+static int uml_vfio_deactivate_irq (struct uml_vfio_device *dev,int index) +{ +struct uml_vfio_intr_ctx *ctx = &dev->intr_ctx[index]; + +if (ctx->irq >=0 ) { +ignore_sigio_fd (dev->udev.irqfd[index]); +um_free_irq (ctx->irq, ctx);and here too?
Quoted 10 lines
arch/um/drivers/vfio_kern.c new file +10
-0+static int uml_vfio_update_msix_cap (struct uml_vfio_device *dev, +unsigned int offset,int size, +unsigned long val) +{ +int err =0 ; + +if (size ==2 && offset == dev->msix_cap +PCI_MSIX_FLAGS ) { +switch (val & ~PCI_MSIX_FLAGS_QSIZE ) { +case PCI_MSIX_FLAGS_ENABLE : +case 0 :
Quoted 8 lines
arch/um/drivers/vfio_kern.c line 252
Quoted 6 lines
arch/um/drivers/vfio_kern.c new file +6
-0+ err =uml_vfio_user_update_irqs (&dev->udev); +break ; + } + } + +return err;do you really want to ignore and return 0 if it wasn't supported?
I just realized that this part could be confusing. It handles only some essential cmds and ignores others. I'll add some comments to clarify.
Quoted 14 lines
arch/um/drivers/vfio_kern.c line 264
Quoted 11 lines
arch/um/drivers/vfio_kern.c new file +10
-0+static int uml_vfio_update_msix_table (struct uml_vfio_device *dev, +unsigned int offset,int size, +unsigned long val) +{ +int index; + + offset -= dev->msix_offset +PCI_MSIX_ENTRY_DATA ; + +if (size !=4 || offset %PCI_MSIX_ENTRY_SIZE !=0 ) +return 0 ;here too?
Quoted 10 lines
arch/um/drivers/vfio_kern.c line 343
Quoted 8 lines
arch/um/drivers/vfio_kern.c new file +8
-0+static void uml_vfio_cfgspace_write (struct um_pci_device *pdev, +unsigned int offset,int size, +unsigned long val) +{ +struct uml_vfio_device *dev =to_vdev (pdev); + +if (offset < dev->msix_cap +PCI_CAP_MSIX_SIZEOF && + offset + size > dev->msix_cap)I'd probably indent the second line just to after "if ("?
Sure. Will do.
Quoted 7 lines
arch/um/drivers/vfio_kern.c line 399
Quoted 4 lines
arch/um/drivers/vfio_kern.c new file +3
-0+if (bar == dev->msix_bar && offset + size > dev->msix_offset && + offset < dev->msix_offset + dev->msix_size) +WARN_ON (uml_vfio_update_msix_table (dev, offset, size, val));likewise
Quoted 7 lines
arch/um/drivers/vfio_kern.c line 445
Quoted 5 lines
arch/um/drivers/vfio_kern.c new file +5
-0+static u8 uml_vfio_find_capability (struct uml_vfio_device *dev,u8 cap) +{ +u8 id, pos; +u16 ent; +int ttl =48 ;any particular significance in that value?
I borrowed the value of PCI_FIND_CAP_TTL from drivers/pci/pci.h, which is an internal header and can't be included from here. I'll add a comment to it.
Quoted 14 lines
arch/um/drivers/vfio_user.c line 59
Quoted 11 lines
arch/um/drivers/vfio_user.c new file +10
-0+int uml_vfio_user_setup_iommu (int container) +{ +unsigned long reserved = uml_reserved - uml_physmem; +struct vfio_iommu_type1_dma_map dma_map = { + .argsz =sizeof (dma_map), + .flags =VFIO_DMA_MAP_FLAG_READ |VFIO_DMA_MAP_FLAG_WRITE , + .vaddr = uml_reserved, + .iova = reserved, + .size = physmem_size - reserved, + };maybe point over to the big comment in
vhost_user_set_mem_table()fromvirtio_uml.c? Same things apply here I guess.
Good idea. Will do.
Thanks again for the review! :)
Regards,
Tiwei