RFC vfio: Adopt iommufd
Get series with b4
b4 am [email protected]Versions newest first
- 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 files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/18 | scripts/update-linux-headers: Add iommufd.hscripts/ | +1 | 0 replies | |
| 2/18 | linux-headers: Import vfio.h and iommufd.hlinux-headers/ linux-headers/ linux-headers/ | +723 | 0 replies | |
| 3/18 | vfio/common: Move IOMMU agnostic helpers to a separate filehw/ hw/ hw/ +1 more | +601 | 0 replies | |
| 4/18 | vfio/common: Introduce vfio_container_add|del_section_window()hw/ | +89 | 0 replies | |
| 5/18 | vfio/common: Move legacy VFIO backend code into separate container.chw/ hw/ hw/ +1 more | +1261 | 0 replies | |
| 6/18 | vfio/common: Rename into as.chw/ hw/ | +1 | 0 replies | |
| 7/18 | vfio: Add base containerhw/ hw/ hw/ +6 more | +517 | 0 replies | |
| 8/18 | vfio/container: Introduce vfio_[attach/detach]_devicehw/ hw/ include/ | +72 | 0 replies | |
| 9/18 | vfio/platform: Use vfio_[attach/detach]_devicehw/ | +2 | 0 replies | |
| 10/18 | vfio/ap: Use vfio_[attach/detach]_devicehw/ | +9 | 0 replies | |
| 11/18 | vfio/ccw: Use vfio_[attach/detach]_devicehw/ hw/ include/ | +32 | 2 replies | |
| 12/18 | vfio/container-base: Introduce [attach/detach]_device container callbackshw/ hw/ hw/ +2 more | +32 | 0 replies | |
| 13/18 | vfio/container-base: Introduce VFIOContainer reset callbackhw/ hw/ hw/ +1 more | +44 | 0 replies | |
| 14/18 | backends/iommufd: Introduce the iommufd objectMAINTAINERS backends/ backends/ +6 more | +400 | 2 replies | |
| 15/18 | util/char_dev: Add open_cdev()MAINTAINERS include/ util/ +1 more | +84 | 0 replies | |
| 16/18 | vfio/iommufd: Implement the iommufd backendhw/ hw/ hw/ +4 more | +586 | 2 replies | |
| 17/18 | vfio/iommufd: Add IOAS_COPY_DMA supporthw/ hw/ hw/ +3 more | +169 | 0 replies | |
| 18/18 | vfio/as: Allow the selection of a given iommu backendhw/ | +14 | 3 replies |
Reviews trailers per patch
None of the 18 patches has a Reviewed-by or Acked-by.
| Patch | Eric Auger |
|---|---|
| 1/18 | |
| 2/18 | |
| 3/18 | |
| 4/18 | |
| 5/18 | |
| 6/18 | |
| 7/18 | Co-authored-by |
| 8/18 | |
| 9/18 | |
| 10/18 | |
| 11/18 | |
| 12/18 | |
| 13/18 | |
| 14/18 | |
| 15/18 | |
| 16/18 | Co-authored-by |
| 17/18 | |
| 18/18 |
Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.
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=iommufd0Note 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=iommufd0If 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