Skip to content

RFC vfio: Adopt iommufd

Eric Auger kvm qemu-devel 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v3 RFC Latest18 patches 12 repliesIn review

Changes in v3 from the cover letter

- rebase on top of v7.2.0
- Fix the compilation with CONFIG_IOMMUFD unset by using true classes for VFIO backends
- Fix use after free in error path, reported by Alister
- Split common.c in several steps to ease the review

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/18scripts/update-linux-headers: Add iommufd.hscripts/update-linux-headers.sh+1 -10 replies
2/18linux-headers: Import vfio.h and iommufd.hlinux-headers/linux/iommufd.h linux-headers/linux/kvm.h linux-headers/linux/vfio.h+723 -280 replies
3/18vfio/common: Move IOMMU agnostic helpers to a separate filehw/vfio/common.c hw/vfio/helpers.c hw/vfio/meson.build +1 more+601 -5690 replies
4/18vfio/common: Introduce vfio_container_add|del_section_window()hw/vfio/common.c+89 -670 replies
5/18vfio/common: Move legacy VFIO backend code into separate container.chw/vfio/common.c hw/vfio/container.c hw/vfio/meson.build +1 more+1261 -11900 replies
6/18vfio/common: Rename into as.chw/vfio/as.c hw/vfio/meson.build+1 -10 replies
7/18vfio: Add base containerhw/vfio/as.c hw/vfio/container-base.c hw/vfio/container.c +6 more+517 -1910 replies
8/18vfio/container: Introduce vfio_[attach/detach]_devicehw/vfio/container.c hw/vfio/pci.c include/hw/vfio/vfio-common.h+72 -450 replies
9/18vfio/platform: Use vfio_[attach/detach]_devicehw/vfio/platform.c+2 -400 replies
10/18vfio/ap: Use vfio_[attach/detach]_devicehw/vfio/ap.c+9 -530 replies
11/18vfio/ccw: Use vfio_[attach/detach]_devicehw/vfio/ccw.c hw/vfio/container.c include/hw/vfio/vfio-common.h+32 -982 replies
12/18vfio/container-base: Introduce [attach/detach]_device container callbackshw/vfio/as.c hw/vfio/container.c hw/vfio/pci.c +2 more+32 -30 replies
13/18vfio/container-base: Introduce VFIOContainer reset callbackhw/vfio/as.c hw/vfio/container-base.c hw/vfio/container.c +1 more+44 -120 replies
14/18backends/iommufd: Introduce the iommufd objectMAINTAINERS backends/Kconfig backends/iommufd.c +6 more+400 -12 replies
15/18util/char_dev: Add open_cdev()MAINTAINERS include/qemu/char_dev.h util/chardev_open.c +1 more+84 -00 replies
16/18vfio/iommufd: Implement the iommufd backendhw/vfio/as.c hw/vfio/iommufd.c hw/vfio/meson.build +4 more+586 -22 replies
17/18vfio/iommufd: Add IOAS_COPY_DMA supporthw/vfio/as.c hw/vfio/container-base.c hw/vfio/container.c +3 more+169 -410 replies
18/18vfio/as: Allow the selection of a given iommu backendhw/vfio/pci.c+14 -53 replies

Reviews trailers per patch

None of the 18 patches has a Reviewed-by or Acked-by.

PatchEric Auger
1/18
2/18
3/18
4/18
5/18
6/18
7/18Co-authored-by
8/18
9/18
10/18
11/18
12/18
13/18
14/18
15/18
16/18Co-authored-by
17/18
18/18

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [RFC v3 00/18] vfio: Adopt iommufd

With the introduction of iommufd, the Linux kernel provides a generic interface for userspace drivers to propagate their DMA mappings to kernel for assigned devices. This series does the porting of the VFIO devices onto the /dev/iommu uapi and let it coexist with the legacy implementation.

This QEMU integration is the result of a collaborative work between Yi Liu, Yi Sun, Nicolin Chen and Eric Auger.

At QEMU level, interactions with the /dev/iommu are abstracted by a new iommufd object (compiled in with the CONFIG_IOMMUFD option).

Any QEMU device (e.g. vfio device) wishing to use /dev/iommu must be linked with an iommufd object. In this series, the vfio-pci device is granted with such capability (other VFIO devices are not yet ready):

It gets a new optional parameter named iommufd which allows to pass an iommufd object:

    -object iommufd,id=iommufd0
    -device vfio-pci,host=0000:02:00.0,iommufd=iommufd0

Note the /dev/iommu can be externally opened by a management layer. In such a case the fd is passed along with the iommufd object:

    -object iommufd,id=iommufd0,fd=22
    -device vfio-pci,host=0000:02:00.0,iommufd=iommufd0

If the fd parameter is not passed, the fd (/dev/iommu) is opened by QEMU.

If no iommufd option is passed to the vfio-pci device, iommufd is not used and the end-user gets the behavior based on the legacy vfio iommu interfaces:

    -device vfio-pci,host=0000:02:00.0

While the legacy kernel interface is group-centric, the new iommufd interface is device-centric, relying on device fd and iommufd.

To support both interfaces in the QEMU VFIO device we reworked the vfio container abstraction so that the generic VFIO code can use either backend.

The VFIOContainer object becomes a base object derived into
a) the legacy VFIO container and
b) the new iommufd based container.

The base object implements generic code such as code related to memory_listener and address space management whereas the derived objects implement callbacks specific to either BE, legacy and iommufd. Indeed each backend has its own way to setup secure context and dma management interface. The below diagram shows how it looks like with both BEs.

VFIO AddressSpace/Memory
+-------+ +----------+ +-----+ +-----+
| pci | | platform | | ap | | ccw |
+---+---+ +----+-----+ +--+--+ +--+--+ +----------------------+
| | | | | AddressSpace |
| | | | +------------+---------+
+---V-----------V-----------V--------V----+ /
| VFIOAddressSpace | <------------+
| | | MemoryListener
| VFIOContainer list |
+-------+----------------------------+----+
| |
| |
+-------V------+ +--------V----------+
| iommufd | | vfio legacy |
| container | | container |
+-------+------+ +--------+----------+
| |
| /dev/iommu | /dev/vfio/vfio
| /dev/vfio/devices/vfioX | /dev/vfio/$group_id
Userspace | |
============+============================+===========================

Kernel      |  device fd                 |
            +---------------+            | group/container fd
            | (BIND_IOMMUFD |            | (SET_CONTAINER/SET_IOMMU)
            |  ATTACH_IOAS) |            | device fd
            |               |            |
            |       +-------V------------V-----------------+
    iommufd |       |                vfio                  |
(map/unmap  |       +---------+--------------------+-------+
ioas_copy)  |                 |                    | map/unmap
            |                 |                    |
     +------V------+    +-----V------+      +------V--------+
     | iommfd core |    |  device    |      |  vfio iommu   |
     +-------------+    +------------+      +---------------+

[Secure Context setup]
- iommufd BE: uses device fd and iommufd to setup secure context (bind_iommufd, attach_ioas)
- vfio legacy BE: uses group fd and container fd to setup secure context (set_container, set_iommu)
[Device access]
- iommufd BE: device fd is opened through /dev/vfio/devices/vfioX
- vfio legacy BE: device fd is retrieved from group fd ioctl [DMA Mapping flow]
- VFIOAddressSpace receives MemoryRegion add/del via MemoryListener
- VFIO populates DMA map/unmap via the container BEs
*) iommufd BE: uses iommufd
*) vfio legacy BE: uses container fd

This series depends on Yi's kernel series:
"[PATCH 00/13] Add vfio_device cdev for iommufd support"
https://lore.kernel.org/all/[email protected]/

which can be found at:
https://github.com/yiliu1765/iommufd/tree/vfio_device_cdev_v1

This qemu series can be found at:
https://github.com/eauger/qemu/tree/iommufd_rfcv3

Test done:
- PCI and Platform device were tested
- ccw and ap were only compile-tested
- limited device hotplug test
- vIOMMU test run for both legacy and iommufd backends (limited tests)

Given some iommufd kernel limitations, the iommufd backend is not yuet fully on par with the legacy backend w.r.t. features like:
- p2p mappings (you will see related error traces)
- coherency tracking
- live migration
- vfio pci device hot reset
- and etc.

TODOs:
- Add DMA alias check for iommufd BE (group level)
- Make pci.c to be BE agnostic. Needs kernel change as well to fix the VFIO_DEVICE_PCI_HOT_RESET gap
- Cleanup the VFIODevice fields as it's used in both backends
- Add device fd parameter to vfio-device in case the iommufd option is used
- Add locks
- Replace list with g_tree
- More tests

Change log:
v2 -> v3:
- rebase on top of v7.2.0
- Fix the compilation with CONFIG_IOMMUFD unset by using true classes for VFIO backends
- Fix use after free in error path, reported by Alister
- Split common.c in several steps to ease the review

v1 -> v2:
- remove the first three patches of rfcv1
- add open cdev helper suggested by Jason
- remove the QOMification of the VFIOContainer and simply use standard ops (David)
- add "-object iommufd" suggested by Alex

v1: https://lore.kernel.org/qemu-devel/[email protected]/

Thanks,
Yi, Yi, Eric

Eric Auger (10):
scripts/update-linux-headers: Add iommufd.h vfio/common: Introduce vfio_container_add|del_section_window() vfio/container: Introduce vfio_[attach/detach]_device vfio/platform: Use vfio_[attach/detach]_device vfio/ap: Use vfio_[attach/detach]_device
vfio/ccw: Use vfio_[attach/detach]_device vfio/container-base: Introduce [attach/detach]_device container
callbacks
vfio/container-base: Introduce VFIOContainer reset callback backends/iommufd: Introduce the iommufd object vfio/as: Allow the selection of a given iommu backend

Yi Liu (8):
linux-headers: Import vfio.h and iommufd.h
vfio/common: Move IOMMU agnostic helpers to a separate file vfio/common: Move legacy VFIO backend code into separate container.c vfio/common: Rename into as.c
vfio: Add base container
util/char_dev: Add open_cdev()
vfio/iommufd: Implement the iommufd backend
vfio/iommufd: Add IOAS_COPY_DMA support

 MAINTAINERS                           |   13 +
 qapi/qom.json                         |   16 +-
 include/hw/vfio/vfio-common.h         |   94 +-
 include/hw/vfio/vfio-container-base.h |  162 ++
 include/qemu/char_dev.h               |   16 +
 include/sysemu/iommufd.h              |   47 +
 linux-headers/linux/iommufd.h         |  349 ++++
 linux-headers/linux/kvm.h             |   58 +-
 linux-headers/linux/vfio.h            |  344 +++-
 backends/iommufd.c                    |  265 +++
 backends/iommufd_stub.c               |   35 +
 hw/vfio/ap.c                          |   62 +-
 hw/vfio/as.c                          |  993 ++++++++++
 hw/vfio/ccw.c                         |  118 +-
 hw/vfio/common.c                      | 2574 -------------------------
 hw/vfio/container-base.c              |  172 ++
 hw/vfio/container.c                   | 1349 +++++++++++++
 hw/vfio/helpers.c                     |  598 ++++++
 hw/vfio/iommufd.c                     |  565 ++++++
 hw/vfio/migration.c                   |    5 +-
 hw/vfio/pci.c                         |   83 +-
 hw/vfio/platform.c                    |   42 +-
 hw/vfio/spapr.c                       |   22 +-
 util/chardev_open.c                   |   61 +
 backends/Kconfig                      |    5 +
 backends/meson.build                  |    2 +
 backends/trace-events                 |   12 +
 hw/vfio/meson.build                   |    6 +-
 hw/vfio/trace-events                  |   11 +
 qemu-options.hx                       |   12 +
 scripts/update-linux-headers.sh       |    2 +-
 util/meson.build                      |    1 +
 32 files changed, 5192 insertions(+), 2902 deletions(-)
 create mode 100644 include/hw/vfio/vfio-container-base.h
 create mode 100644 include/qemu/char_dev.h
 create mode 100644 include/sysemu/iommufd.h
 create mode 100644 linux-headers/linux/iommufd.h
 create mode 100644 backends/iommufd.c
 create mode 100644 backends/iommufd_stub.c
 create mode 100644 hw/vfio/as.c
 delete mode 100644 hw/vfio/common.c
 create mode 100644 hw/vfio/container-base.c
 create mode 100644 hw/vfio/container.c
 create mode 100644 hw/vfio/helpers.c
 create mode 100644 hw/vfio/iommufd.c
 create mode 100644 util/chardev_open.c
Signature · 1 line
2.37.3