vfio: Adopt iommufd
Subsystems (2)
v3 cover 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
RFC v3 1/18 scripts/update-linux-headers: Add iommufd.h
Update the script to import iommufd.h
--- scripts/update-linux-headers.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/update-linux-headers.sh +1 -1
@@ -160,7 +160,7 @@ done rm -rf "$output/linux-headers/linux" mkdir -p "$output/linux-headers/linux" -for header in kvm.h vfio.h vfio_ccw.h vfio_zdev.h vhost.h \ +for header in kvm.h vfio.h iommufd.h vfio_ccw.h vfio_zdev.h vhost.h \ psci.h psp-sev.h userfaultfd.h mman.h vduse.h; do cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux" done
Signature · 1 line
2.37.3
RFC v3 2/18 linux-headers: Import vfio.h and iommufd.h
In reply to Eric AugerFrom: Yi Liu <[email protected]>
Partial header update against the following branch:
https://github.com/yiliu1765/iommufd/tree/vfio_device_cdev_v1
featuring [PATCH 00/13] Add vfio_device cdev for iommufd support
This pulls header updates for kvm.h, iommufd.h and vfio.h.
--- linux-headers/linux/iommufd.h | 349 ++++++++++++++++++++++++++++++++++ linux-headers/linux/kvm.h | 58 +++--- linux-headers/linux/vfio.h | 344 ++++++++++++++++++++++++++++++++- 3 files changed, 723 insertions(+), 28 deletions(-) create mode 100644 linux-headers/linux/iommufd.h
linux-headers/linux/iommufd.h new file +349 -0
@@ -0,0 +1,349 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES. +*/ +#ifndef _IOMMUFD_H +#define _IOMMUFD_H + +#include <linux/types.h> +#include <linux/ioctl.h> + +#define IOMMUFD_TYPE (';') + +#define IOMMUFD_INVALID_ID 0/* valid ID starts from 1 */ + +/** +* DOC: General ioctl format +* +* The ioctl interface follows a general format to allow for extensibility. Each +* ioctl is passed in a structure pointer as the argument providing the size of +* the structure in the first u32. The kernel checks that any structure space +* beyond what it understands is 0. This allows userspace to use the backward +* compatible portion while consistently using the newer, larger, structures. +* +* ioctls use a standard meaning for common errnos: +* +* - ENOTTY: The IOCTL number itself is not supported at all +* - E2BIG: The IOCTL number is supported, but the provided structure has +* non-zero in a part the kernel does not understand. +* - EOPNOTSUPP: The IOCTL number is supported, and the structure is +* understood, however a known field has a value the kernel does not +* understand or support. +* - EINVAL: Everything about the IOCTL was understood, but a field is not +* correct. +* - ENOENT: An ID or IOVA provided does not exist. +* - ENOMEM: Out of memory. +* - EOVERFLOW: Mathematics overflowed. +* +* As well as additional errnos, within specific ioctls. +*/ +enum { +IOMMUFD_CMD_BASE =0x80 , +IOMMUFD_CMD_DESTROY =IOMMUFD_CMD_BASE , +IOMMUFD_CMD_IOAS_ALLOC , +IOMMUFD_CMD_IOAS_ALLOW_IOVAS , +IOMMUFD_CMD_IOAS_COPY , +IOMMUFD_CMD_IOAS_IOVA_RANGES , +IOMMUFD_CMD_IOAS_MAP , +IOMMUFD_CMD_IOAS_UNMAP , +IOMMUFD_CMD_OPTION , +IOMMUFD_CMD_VFIO_IOAS , +}; + +/** +* struct iommu_destroy - ioctl(IOMMU_DESTROY) +* @size: sizeof(struct iommu_destroy) +* @id: iommufd object ID to destroy. Can be any destroyable object type. +* +* Destroy any object held within iommufd. +*/ +struct iommu_destroy { +__u32 size; +__u32 id; +}; +#define IOMMU_DESTROY _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DESTROY) + +/** +* struct iommu_ioas_alloc - ioctl(IOMMU_IOAS_ALLOC) +* @size: sizeof(struct iommu_ioas_alloc) +* @flags: Must be 0 +* @out_ioas_id: Output IOAS ID for the allocated object +* +* Allocate an IO Address Space (IOAS) which holds an IO Virtual Address (IOVA) +* to memory mapping. +*/ +struct iommu_ioas_alloc { +__u32 size; +__u32 flags; +__u32 out_ioas_id; +}; +#define IOMMU_IOAS_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_ALLOC) + +/** +* struct iommu_iova_range - ioctl(IOMMU_IOVA_RANGE) +* @start: First IOVA +* @last: Inclusive last IOVA +* +* An interval in IOVA space. +*/ +struct iommu_iova_range { +__aligned_u64 start; +__aligned_u64 last; +}; + +/** +* struct iommu_ioas_iova_ranges - ioctl(IOMMU_IOAS_IOVA_RANGES) +* @size: sizeof(struct iommu_ioas_iova_ranges) +* @ioas_id: IOAS ID to read ranges from +* @num_iovas: Input/Output total number of ranges in the IOAS +* @__reserved: Must be 0 +* @allowed_iovas: Pointer to the output array of struct iommu_iova_range +* @out_iova_alignment: Minimum alignment required for mapping IOVA +* +* Query an IOAS for ranges of allowed IOVAs. Mapping IOVA outside these ranges +* is not allowed. num_iovas will be set to the total number of iovas and +* the allowed_iovas[] will be filled in as space permits. +* +* The allowed ranges are dependent on the HW path the DMA operation takes, and +* can change during the lifetime of the IOAS. A fresh empty IOAS will have a +* full range, and each attached device will narrow the ranges based on that +* device's HW restrictions. Detaching a device can widen the ranges. Userspace +* should query ranges after every attach/detach to know what IOVAs are valid +* for mapping. +* +* On input num_iovas is the length of the allowed_iovas array. On output it is +* the total number of iovas filled in. The ioctl will return -EMSGSIZE and set +* num_iovas to the required value if num_iovas is too small. In this case the +* caller should allocate a larger output array and re-issue the ioctl. +* +* out_iova_alignment returns the minimum IOVA alignment that can be given +* to IOMMU_IOAS_MAP/COPY. IOVA's must satisfy:: +* +* starting_iova % out_iova_alignment == 0 +* (starting_iova + length) % out_iova_alignment == 0 +* +* out_iova_alignment can be 1 indicating any IOVA is allowed. It cannot +* be higher than the system PAGE_SIZE. +*/ +struct iommu_ioas_iova_ranges { +__u32 size; +__u32 ioas_id; +__u32 num_iovas; +__u32 __reserved; +__aligned_u64 allowed_iovas; +__aligned_u64 out_iova_alignment; +}; +#define IOMMU_IOAS_IOVA_RANGES _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_IOVA_RANGES) + +/** +* struct iommu_ioas_allow_iovas - ioctl(IOMMU_IOAS_ALLOW_IOVAS) +* @size: sizeof(struct iommu_ioas_allow_iovas) +* @ioas_id: IOAS ID to allow IOVAs from +* @num_iovas: Input/Output total number of ranges in the IOAS +* @__reserved: Must be 0 +* @allowed_iovas: Pointer to array of struct iommu_iova_range +* +* Ensure a range of IOVAs are always available for allocation. If this call +* succeeds then IOMMU_IOAS_IOVA_RANGES will never return a list of IOVA ranges +* that are narrower than the ranges provided here. This call will fail if +* IOMMU_IOAS_IOVA_RANGES is currently narrower than the given ranges. +* +* When an IOAS is first created the IOVA_RANGES will be maximally sized, and as +* devices are attached the IOVA will narrow based on the device restrictions. +* When an allowed range is specified any narrowing will be refused, ie device +* attachment can fail if the device requires limiting within the allowed range. +* +* Automatic IOVA allocation is also impacted by this call. MAP will only +* allocate within the allowed IOVAs if they are present. +* +* This call replaces the entire allowed list with the given list. +*/ +struct iommu_ioas_allow_iovas { +__u32 size; +__u32 ioas_id; +__u32 num_iovas; +__u32 __reserved; +__aligned_u64 allowed_iovas; +}; +#define IOMMU_IOAS_ALLOW_IOVAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_ALLOW_IOVAS) + +/** +* enum iommufd_ioas_map_flags - Flags for map and copy +* @IOMMU_IOAS_MAP_FIXED_IOVA: If clear the kernel will compute an appropriate +* IOVA to place the mapping at +* @IOMMU_IOAS_MAP_WRITEABLE: DMA is allowed to write to this mapping +* @IOMMU_IOAS_MAP_READABLE: DMA is allowed to read from this mapping +*/ +enum iommufd_ioas_map_flags { +IOMMU_IOAS_MAP_FIXED_IOVA =1 <<0 , +IOMMU_IOAS_MAP_WRITEABLE =1 <<1 , +IOMMU_IOAS_MAP_READABLE =1 <<2 , +}; + +/** +* struct iommu_ioas_map - ioctl(IOMMU_IOAS_MAP) +* @size: sizeof(struct iommu_ioas_map) +* @flags: Combination of enum iommufd_ioas_map_flags +* @ioas_id: IOAS ID to change the mapping of +* @__reserved: Must be 0 +* @user_va: Userspace pointer to start mapping from +* @length: Number of bytes to map +* @iova: IOVA the mapping was placed at. If IOMMU_IOAS_MAP_FIXED_IOVA is set +* then this must be provided as input. +* +* Set an IOVA mapping from a user pointer. If FIXED_IOVA is specified then the +* mapping will be established at iova, otherwise a suitable location based on +* the reserved and allowed lists will be automatically selected and returned in +* iova. +* +* If IOMMU_IOAS_MAP_FIXED_IOVA is specified then the iova range must currently +* be unused, existing IOVA cannot be replaced. +*/ +struct iommu_ioas_map { +__u32 size; +__u32 flags; +__u32 ioas_id; +__u32 __reserved; +__aligned_u64 user_va; +__aligned_u64 length; +__aligned_u64 iova; +}; +#define IOMMU_IOAS_MAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_MAP) + +/** +* struct iommu_ioas_copy - ioctl(IOMMU_IOAS_COPY) +* @size: sizeof(struct iommu_ioas_copy) +* @flags: Combination of enum iommufd_ioas_map_flags +* @dst_ioas_id: IOAS ID to change the mapping of +* @src_ioas_id: IOAS ID to copy from +* @length: Number of bytes to copy and map +* @dst_iova: IOVA the mapping was placed at. If IOMMU_IOAS_MAP_FIXED_IOVA is +* set then this must be provided as input. +* @src_iova: IOVA to start the copy +* +* Copy an already existing mapping from src_ioas_id and establish it in +* dst_ioas_id. The src iova/length must exactly match a range used with +* IOMMU_IOAS_MAP. +* +* This may be used to efficiently clone a subset of an IOAS to another, or as a +* kind of 'cache' to speed up mapping. Copy has an efficiency advantage over +* establishing equivalent new mappings, as internal resources are shared, and +* the kernel will pin the user memory only once. +*/ +struct iommu_ioas_copy { +__u32 size; +__u32 flags; +__u32 dst_ioas_id; +__u32 src_ioas_id; +__aligned_u64 length; +__aligned_u64 dst_iova; +__aligned_u64 src_iova; +}; +#define IOMMU_IOAS_COPY _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_COPY) + +/** +* struct iommu_ioas_unmap - ioctl(IOMMU_IOAS_UNMAP) +* @size: sizeof(struct iommu_ioas_unmap) +* @ioas_id: IOAS ID to change the mapping of +* @iova: IOVA to start the unmapping at +* @length: Number of bytes to unmap, and return back the bytes unmapped +* +* Unmap an IOVA range. The iova/length must be a superset of a previously +* mapped range used with IOMMU_IOAS_MAP or IOMMU_IOAS_COPY. Splitting or +* truncating ranges is not allowed. The values 0 to U64_MAX will unmap +* everything. +*/ +struct iommu_ioas_unmap { +__u32 size; +__u32 ioas_id; +__aligned_u64 iova; +__aligned_u64 length; +}; +#define IOMMU_IOAS_UNMAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_UNMAP) + +/** +* enum iommufd_option - ioctl(IOMMU_OPTION_RLIMIT_MODE) and +* ioctl(IOMMU_OPTION_HUGE_PAGES) +* @IOMMU_OPTION_RLIMIT_MODE: +* Change how RLIMIT_MEMLOCK accounting works. The caller must have privilege +* to invoke this. Value 0 (default) is user based accouting, 1 uses process +* based accounting. Global option, object_id must be 0 +* @IOMMU_OPTION_HUGE_PAGES: +* Value 1 (default) allows contiguous pages to be combined when generating +* iommu mappings. Value 0 disables combining, everything is mapped to +* PAGE_SIZE. This can be useful for benchmarking. This is a per-IOAS +* option, the object_id must be the IOAS ID. +*/ +enum iommufd_option { +IOMMU_OPTION_RLIMIT_MODE =0 , +IOMMU_OPTION_HUGE_PAGES =1 , +}; + +/** +* enum iommufd_option_ops - ioctl(IOMMU_OPTION_OP_SET) and +* ioctl(IOMMU_OPTION_OP_GET) +* @IOMMU_OPTION_OP_SET: Set the option's value +* @IOMMU_OPTION_OP_GET: Get the option's value +*/ +enum iommufd_option_ops { +IOMMU_OPTION_OP_SET =0 , +IOMMU_OPTION_OP_GET =1 , +}; + +/** +* struct iommu_option - iommu option multiplexer +* @size: sizeof(struct iommu_option) +* @option_id: One of enum iommufd_option +* @op: One of enum iommufd_option_ops +* @__reserved: Must be 0 +* @object_id: ID of the object if required +* @val64: Option value to set or value returned on get +* +* Change a simple option value. This multiplexor allows controlling options +* on objects. IOMMU_OPTION_OP_SET will load an option and IOMMU_OPTION_OP_GET +* will return the current value. +*/ +struct iommu_option { +__u32 size; +__u32 option_id; +__u16 op; +__u16 __reserved; +__u32 object_id; +__aligned_u64 val64; +}; +#define IOMMU_OPTION _IO(IOMMUFD_TYPE, IOMMUFD_CMD_OPTION) + +/** +* enum iommufd_vfio_ioas_op - IOMMU_VFIO_IOAS_* ioctls +* @IOMMU_VFIO_IOAS_GET: Get the current compatibility IOAS +* @IOMMU_VFIO_IOAS_SET: Change the current compatibility IOAS +* @IOMMU_VFIO_IOAS_CLEAR: Disable VFIO compatibility +*/ +enum iommufd_vfio_ioas_op { +IOMMU_VFIO_IOAS_GET =0 , +IOMMU_VFIO_IOAS_SET =1 , +IOMMU_VFIO_IOAS_CLEAR =2 , +}; + +/** +* struct iommu_vfio_ioas - ioctl(IOMMU_VFIO_IOAS) +* @size: sizeof(struct iommu_vfio_ioas) +* @ioas_id: For IOMMU_VFIO_IOAS_SET the input IOAS ID to set +* For IOMMU_VFIO_IOAS_GET will output the IOAS ID +* @op: One of enum iommufd_vfio_ioas_op +* @__reserved: Must be 0 +* +* The VFIO compatibility support uses a single ioas because VFIO APIs do not +* support the ID field. Set or Get the IOAS that VFIO compatibility will use. +* When VFIO_GROUP_SET_CONTAINER is used on an iommufd it will get the +* compatibility ioas, either by taking what is already set, or auto creating +* one. From then on VFIO will continue to use that ioas and is not effected by +* this ioctl. SET or CLEAR does not destroy any auto-created IOAS. +*/ +struct iommu_vfio_ioas { +__u32 size; +__u32 ioas_id; +__u16 op; +__u16 __reserved; +}; +#define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS) +#endif
linux-headers/linux/kvm.h +35 -23
141 lines not on this page · raw message
linux-headers/linux/vfio.h +339 -5
418 lines not on this page · raw message
Signature · 1 line
2.37.3
RFC v3 3/18 vfio/common: Move IOMMU agnostic helpers to a separate file
In reply to Eric AugerFrom: Yi Liu <[email protected]>
Move low-level IOMMU iommu agnostic helpers to a separate helpers.c file. They relate to regions, interrupts and device/region capabilities.
--- include/hw/vfio/vfio-common.h | 2 + hw/vfio/common.c | 569 -------------------------------- hw/vfio/helpers.c | 598 ++++++++++++++++++++++++++++++++++ hw/vfio/meson.build | 1 + 4 files changed, 601 insertions(+), 569 deletions(-) create mode 100644 hw/vfio/helpers.c
include/hw/vfio/vfio-common.h +2 -0
@@ -232,6 +232,8 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info, unsigned int *avail);struct vfio_info_cap_header *vfio_get_device_info_cap (struct vfio_device_info *info,uint16_t id); +struct vfio_info_cap_header * +vfio_get_cap (void *ptr,uint32_t cap_offset,uint16_t id);#endif extern const MemoryListener vfio_prereg_listener;
hw/vfio/common.c +0 -569
605 lines not on this page · raw message
hw/vfio/helpers.c new file +598 -0
598 lines not on this page · raw message
hw/vfio/meson.build +1 -0
@@ -1,5 +1,6 @@ vfio_ss = ss.source_set() vfio_ss.add(files( + 'helpers.c', 'common.c', 'spapr.c', 'migration.c',
Signature · 1 line
2.37.3
RFC v3 4/18 vfio/common: Introduce vfio_container_add|del_section_window()
In reply to Eric AugerIntroduce helper functions that isolate the code used for VFIO_SPAPR_TCE_v2_IOMMU. This code relies is IOMMU backend specific whereas the rest of the code in the callers, ie. vfio_listener_region_add|del is not.
--- hw/vfio/common.c | 156 +++++++++++++++++++++++++++-------------------- 1 file changed, 89 insertions(+), 67 deletions(-)
hw/vfio/common.c +89 -67
@@ -590,6 +590,92 @@ static bool vfio_known_safe_misalignment(MemoryRegionSection *section)return true; } +static int vfio_container_add_section_window (VFIOContainer *container, +MemoryRegionSection *section, +Error **errp) +{ +VFIOHostDMAWindow *hostwin; +hwaddr pgsize =0 ; +int ret; + +if (container->iommu_type != VFIO_SPAPR_TCE_v2_IOMMU) { +return 0 ; + } + +/* For now intersections are not allowed, we may relax this later */ + QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) { +if (ranges_overlap (hostwin->min_iova, + hostwin->max_iova - hostwin->min_iova +1 , + section->offset_within_address_space, +int128_get64 (section->size))) { +error_setg (errp, +"region [0x%" PRIx64",0x%" PRIx64"] overlaps with existing" +"host DMA window [0x%" PRIx64",0x%" PRIx64"]" , + section->offset_within_address_space, + section->offset_within_address_space + +int128_get64 (section->size) -1 , + hostwin->min_iova, hostwin->max_iova); +return -EINVAL ; + } + } + + ret =vfio_spapr_create_window (container, section, &pgsize); +if (ret) { +error_setg_errno (errp, -ret,"Failed to create SPAPR window" ); +return ret; + } + +vfio_host_win_add (container, section->offset_within_address_space, + section->offset_within_address_space + +int128_get64 (section->size) -1 , pgsize); +#ifdef CONFIG_KVM +if (kvm_enabled ()) { +VFIOGroup *group; +IOMMUMemoryRegion *iommu_mr =IOMMU_MEMORY_REGION (section->mr); +struct kvm_vfio_spapr_tce param; +struct kvm_device_attr attr = { + .group =KVM_DEV_VFIO_GROUP , + .attr =KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE , + .addr = (uint64_t )(unsigned long )¶m, + }; + +if (!memory_region_iommu_get_attr (iommu_mr,IOMMU_ATTR_SPAPR_TCE_FD , + ¶m.tablefd)) { + QLIST_FOREACH(group, &container->group_list, container_next) { + param.groupfd = group->fd; +if (ioctl (vfio_kvm_device_fd,KVM_SET_DEVICE_ATTR , &attr)) { +error_report ("vfio: failed to setup fd %d " +"for a group with fd %d: %s" , + param.tablefd, param.groupfd, +strerror (errno)); +return 0 ; + } +trace_vfio_spapr_group_attach (param.groupfd, param.tablefd); + } + } + } +#endif +return 0 ; +} + +static void vfio_container_del_section_window (VFIOContainer *container, +MemoryRegionSection *section) +{ +if (container->iommu_type != VFIO_SPAPR_TCE_v2_IOMMU) { +return ; + } + +vfio_spapr_remove_window (container, + section->offset_within_address_space); +if (vfio_host_win_del (container, + section->offset_within_address_space, + section->offset_within_address_space + +int128_get64 (section->size) -1 ) <0 ) { +hw_error ("%s: Cannot delete missing window at %" HWADDR_PRIx, + __func__, section->offset_within_address_space); + } +} +static void vfio_listener_region_add (MemoryListener *listener,MemoryRegionSection *section) {
@@ -642,62 +728,8 @@ static void vfio_listener_region_add(MemoryListener *listener, } end =int128_get64 (int128_sub (llend,int128_one ())); -if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { -hwaddr pgsize =0 ; - -/* For now intersections are not allowed, we may relax this later */ - QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) { -if (ranges_overlap (hostwin->min_iova, - hostwin->max_iova - hostwin->min_iova +1 , - section->offset_within_address_space, -int128_get64 (section->size))) { -error_setg (&err, -"region [0x%" PRIx64",0x%" PRIx64"] overlaps with existing" -"host DMA window [0x%" PRIx64",0x%" PRIx64"]" , - section->offset_within_address_space, - section->offset_within_address_space + -int128_get64 (section->size) -1 , - hostwin->min_iova, hostwin->max_iova); - goto fail; - } - } - - ret =vfio_spapr_create_window (container, section, &pgsize); -if (ret) { -error_setg_errno (&err, -ret,"Failed to create SPAPR window" ); - goto fail; - } - -vfio_host_win_add (container, section->offset_within_address_space, - section->offset_within_address_space + -int128_get64 (section->size) -1 , pgsize); -#ifdef CONFIG_KVM -if (kvm_enabled ()) { -VFIOGroup *group; -IOMMUMemoryRegion *iommu_mr =IOMMU_MEMORY_REGION (section->mr); -struct kvm_vfio_spapr_tce param; -struct kvm_device_attr attr = { - .group =KVM_DEV_VFIO_GROUP , - .attr =KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE , - .addr = (uint64_t )(unsigned long )¶m, - }; - -if (!memory_region_iommu_get_attr (iommu_mr,IOMMU_ATTR_SPAPR_TCE_FD , - ¶m.tablefd)) { - QLIST_FOREACH(group, &container->group_list, container_next) { - param.groupfd = group->fd; -if (ioctl (vfio_kvm_device_fd,KVM_SET_DEVICE_ATTR , &attr)) { -error_report ("vfio: failed to setup fd %d " -"for a group with fd %d: %s" , - param.tablefd, param.groupfd, -strerror (errno)); -return ; - } -trace_vfio_spapr_group_attach (param.groupfd, param.tablefd); - } - } - } -#endif +if (vfio_container_add_section_window (container, section, &err)) { + goto fail; } hostwin_found = false;
@@ -949,17 +981,7 @@ static void vfio_listener_region_del(MemoryListener *listener,memory_region_unref (section->mr); -if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { -vfio_spapr_remove_window (container, - section->offset_within_address_space); -if (vfio_host_win_del (container, - section->offset_within_address_space, - section->offset_within_address_space + -int128_get64 (section->size) -1 ) <0 ) { -hw_error ("%s: Cannot delete missing window at %" HWADDR_PRIx, - __func__, section->offset_within_address_space); - } - } +vfio_container_del_section_window (container, section); }static void vfio_set_dirty_page_tracking (VFIOContainer *container,bool start)
Signature · 1 line
2.37.3
RFC v3 5/18 vfio/common: Move legacy VFIO backend code into separate container.c
In reply to Eric AugerFrom: Yi Liu <[email protected]>
Move all the code really dependent on the legacy VFIO container/group into a separate file: container.c. What does remain in common.c is the code related to VFIOAddressSpace and MemoryListeners.
---
- vfio_group_list init before int vfio_kvm_device_fd = -1;
- vfio_kvm_device_fd is not static since used in common.c/vfio_listener_region_add
- vfio_ram_block_discard_disable comes fisrt before vfio_mig_active following upstream order
- vfio_get_iommu_type1_info_cap and vfio_get_info_dma_avail now in container.c because they use vfio iommu type1 structs
- vfio_container_add_section_window and vfio_container_del_section_window does not exist anymore
--- include/hw/vfio/vfio-common.h | 28 + hw/vfio/common.c | 1197 +------------------------------- hw/vfio/container.c | 1225 +++++++++++++++++++++++++++++++++ hw/vfio/meson.build | 1 + 4 files changed, 1261 insertions(+), 1190 deletions(-) create mode 100644 hw/vfio/container.c
include/hw/vfio/vfio-common.h +28 -0
@@ -33,6 +33,8 @@#define VFIO_MSG_PREFIX "vfio %s: " +extern const MemoryListener vfio_memory_listener; +enum {VFIO_DEVICE_TYPE_PCI =0 ,VFIO_DEVICE_TYPE_PLATFORM =1 ,
@@ -190,6 +192,32 @@ typedef struct VFIODisplay { } dmabuf; } VFIODisplay; +void vfio_host_win_add (VFIOContainer *container, +hwaddr min_iova,hwaddr max_iova, +uint64_t iova_pgsizes); +int vfio_host_win_del (VFIOContainer *container,hwaddr min_iova, +hwaddr max_iova); +VFIOAddressSpace *vfio_get_address_space (AddressSpace *as); +void vfio_put_address_space (VFIOAddressSpace *space); +bool vfio_devices_all_running_and_saving (VFIOContainer *container); +bool vfio_devices_all_dirty_tracking (VFIOContainer *container); + +/* container->fd */ +int vfio_dma_unmap (VFIOContainer *container, +hwaddr iova,ram_addr_t size, +IOMMUTLBEntry *iotlb); +int vfio_dma_map (VFIOContainer *container,hwaddr iova, +ram_addr_t size,void *vaddr,bool readonly); +void vfio_set_dirty_page_tracking (VFIOContainer *container,bool start); +int vfio_get_dirty_bitmap (VFIOContainer *container,uint64_t iova, +uint64_t size,ram_addr_t ram_addr); + +int vfio_container_add_section_window (VFIOContainer *container, +MemoryRegionSection *section, +Error **errp); +void vfio_container_del_section_window (VFIOContainer *container, +MemoryRegionSection *section); +void vfio_put_base_device (VFIODevice *vbasedev);void vfio_disable_irqindex (VFIODevice *vbasedev,int index);void vfio_unmask_single_irqindex (VFIODevice *vbasedev,int index);
hw/vfio/common.c +7 -1190
1,251 lines not on this page · raw message
hw/vfio/container.c new file +1225 -0
1,225 lines not on this page · raw message
hw/vfio/meson.build +1 -0
@@ -2,6 +2,7 @@ vfio_ss = ss.source_set() vfio_ss.add(files( 'helpers.c', 'common.c', + 'container.c', 'spapr.c', 'migration.c', ))
Signature · 1 line
2.37.3
RFC v3 6/18 vfio/common: Rename into as.c
In reply to Eric AugerFrom: Yi Liu <[email protected]>
As the file mostly contains code related to VFIOAddressSpaces and MemoryListeners, let's rename it into as.c
---
hw/vfio/{common.c => as.c} | 0
hw/vfio/meson.build | 2 +-
2 files changed, 1 insertion(+), 1 deletion(-)
rename hw/vfio/{common.c => as.c} (100%)hw/vfio/as.c renamed from hw/vfio/common.c +0 -0
hw/vfio/meson.build +1 -1
@@ -1,7 +1,7 @@ vfio_ss = ss.source_set() vfio_ss.add(files( 'helpers.c', - 'common.c', + 'as.c', 'container.c', 'spapr.c', 'migration.c',
Signature · 1 line
2.37.3
RFC v3 7/18 vfio: Add base container
In reply to Eric AugerFrom: Yi Liu <[email protected]>
Abstract the VFIOContainer to be a base object. It is supposed to be embedded by legacy VFIO container and later on, into the new iommufd based container.
The base container implements generic code such as code related to memory_listener and address space management. The VFIOContainerOps implements callbacks that depend on the kernel user space being used.
'as.c' only manipulates the base container with wrapper functions that calls the functions defined in VFIOContainerOps. Existing 'container.c' code is converted to implement the legacy container ops functions.
Existing migration code only works with the legacy container. Also 'spapr.c' isn't BE agnostic.
Below is the base container. It's named as VFIOContainer, old VFIOContainer is replaced with VFIOLegacyContainer.
struct VFIOContainer {
VFIOContainerOps *ops;
VFIOAddressSpace *space;
MemoryListener listener;
Error *error;
bool initialized;
bool dirty_pages_supported;
uint64_t dirty_pgsizes;
uint64_t max_dirty_bitmap_size;
unsigned long pgsizes;
unsigned int dma_max_mappings;
QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
QLIST_HEAD(, VFIORamDiscardListener) vrdl_list;
QLIST_ENTRY(VFIOContainer) next;
};struct VFIOLegacyContainer {
VFIOContainer bcontainer;
int fd; /* /dev/vfio/vfio, empowered by the attached groups */
MemoryListener prereg_listener;
unsigned iommu_type;
QLIST_HEAD(, VFIOGroup) group_list;
};---
v2 -> v3:
- Use OpsClass instead of a simple ops struct
v1 -> v2:
- Remove QOM for VFIOContainer object, use callback instead per David's comment.
- Rename container-obj.c/.h to be container-base.c/.h
--- include/hw/vfio/vfio-common.h | 78 +-------- include/hw/vfio/vfio-container-base.h | 151 +++++++++++++++++ hw/vfio/as.c | 61 ++++--- hw/vfio/container-base.c | 154 +++++++++++++++++ hw/vfio/container.c | 232 ++++++++++++++++---------- hw/vfio/migration.c | 5 +- hw/vfio/pci.c | 4 +- hw/vfio/spapr.c | 22 +-- hw/vfio/meson.build | 1 + 9 files changed, 517 insertions(+), 191 deletions(-) create mode 100644 include/hw/vfio/vfio-container-base.h create mode 100644 hw/vfio/container-base.c
include/hw/vfio/vfio-common.h +9 -69
@@ -30,6 +30,7 @@#include <linux/vfio.h> #endif #include "sysemu/sysemu.h" +#include "hw/vfio/vfio-container-base.h" #define VFIO_MSG_PREFIX "vfio %s: "
@@ -70,58 +71,15 @@ typedef struct VFIOMigration {uint64_t pending_bytes; } VFIOMigration; -typedef struct VFIOAddressSpace { -AddressSpace *as; - QLIST_HEAD(, VFIOContainer) containers; -QLIST_ENTRY (VFIOAddressSpace) list; -}VFIOAddressSpace ; -struct VFIOGroup ; -typedef struct VFIOContainer { -VFIOAddressSpace *space; +typedef struct VFIOLegacyContainer { +VFIOContainer bcontainer;int fd;/* /dev/vfio/vfio, empowered by the attached groups */ -MemoryListener listener;MemoryListener prereg_listener;unsigned iommu_type; -Error *error; -bool initialized; -bool dirty_pages_supported; -uint64_t dirty_pgsizes; -uint64_t max_dirty_bitmap_size; -unsigned long pgsizes; -unsigned int dma_max_mappings; - QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; - QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list; QLIST_HEAD(, VFIOGroup) group_list; - QLIST_HEAD(, VFIORamDiscardListener) vrdl_list; -QLIST_ENTRY (VFIOContainer) next; -}VFIOContainer ; - -typedef struct VFIOGuestIOMMU { -VFIOContainer *container; -IOMMUMemoryRegion *iommu_mr; -hwaddr iommu_offset; -IOMMUNotifier n; -QLIST_ENTRY (VFIOGuestIOMMU) giommu_next; -}VFIOGuestIOMMU ; - -typedef struct VFIORamDiscardListener { -VFIOContainer *container; -MemoryRegion *mr; -hwaddr offset_within_address_space; -hwaddr size; -uint64_t granularity; -RamDiscardListener listener; -QLIST_ENTRY (VFIORamDiscardListener) next; -}VFIORamDiscardListener ; - -typedef struct VFIOHostDMAWindow { -hwaddr min_iova; -hwaddr max_iova; -uint64_t iova_pgsizes; -QLIST_ENTRY (VFIOHostDMAWindow) hostwin_next; -}VFIOHostDMAWindow ; +}VFIOLegacyContainer ;typedef struct VFIODeviceOps VFIODeviceOps ;
@@ -159,7 +117,7 @@ struct VFIODeviceOps { typedef struct VFIOGroup { int fd; int groupid; - VFIOContainer *container; + VFIOLegacyContainer *container; QLIST_HEAD(, VFIODevice) device_list; QLIST_ENTRY(VFIOGroup) next; QLIST_ENTRY(VFIOGroup) container_next;
@@ -192,31 +150,13 @@ typedef struct VFIODisplay { } dmabuf; } VFIODisplay; -void vfio_host_win_add (VFIOContainer *container, +void vfio_host_win_add (VFIOContainer *bcontainer,hwaddr min_iova,hwaddr max_iova,uint64_t iova_pgsizes); -int vfio_host_win_del (VFIOContainer *container,hwaddr min_iova, +int vfio_host_win_del (VFIOContainer *bcontainer,hwaddr min_iova,hwaddr max_iova);VFIOAddressSpace *vfio_get_address_space (AddressSpace *as);void vfio_put_address_space (VFIOAddressSpace *space); -bool vfio_devices_all_running_and_saving (VFIOContainer *container); -bool vfio_devices_all_dirty_tracking (VFIOContainer *container); - -/* container->fd */ -int vfio_dma_unmap (VFIOContainer *container, -hwaddr iova,ram_addr_t size, -IOMMUTLBEntry *iotlb); -int vfio_dma_map (VFIOContainer *container,hwaddr iova, -ram_addr_t size,void *vaddr,bool readonly); -void vfio_set_dirty_page_tracking (VFIOContainer *container,bool start); -int vfio_get_dirty_bitmap (VFIOContainer *container,uint64_t iova, -uint64_t size,ram_addr_t ram_addr); - -int vfio_container_add_section_window (VFIOContainer *container, -MemoryRegionSection *section, -Error **errp); -void vfio_container_del_section_window (VFIOContainer *container, -MemoryRegionSection *section);void vfio_put_base_device (VFIODevice *vbasedev);void vfio_disable_irqindex (VFIODevice *vbasedev,int index);
@@ -265,10 +205,10 @@ vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id);#endif extern const MemoryListener vfio_prereg_listener; -int vfio_spapr_create_window (VFIOContainer *container, +int vfio_spapr_create_window (VFIOLegacyContainer *container,MemoryRegionSection *section,hwaddr *pgsize); -int vfio_spapr_remove_window (VFIOContainer *container, +int vfio_spapr_remove_window (VFIOLegacyContainer *container,hwaddr offset_within_address_space);int vfio_migration_probe (VFIODevice *vbasedev,Error **errp);
include/hw/vfio/vfio-container-base.h new file +151 -0
151 lines not on this page · raw message
hw/vfio/as.c +41 -20
155 lines not on this page · raw message
hw/vfio/container-base.c new file +154 -0
154 lines not on this page · raw message
hw/vfio/container.c +144 -88
525 lines not on this page · raw message
hw/vfio/migration.c +3 -2
@@ -859,11 +859,12 @@ int64_t vfio_mig_bytes_transferred(void)int vfio_migration_probe (VFIODevice *vbasedev,Error **errp) { -VFIOContainer *container = vbasedev->group->container; +VFIOLegacyContainer *container = vbasedev->group->container;struct vfio_region_info *info =NULL ;int ret = -ENOTSUP ; -if (!vbasedev->enable_migration || !container->dirty_pages_supported) { +if (!vbasedev->enable_migration || + !container->bcontainer.dirty_pages_supported) { goto add_blocker; }
hw/vfio/pci.c +3 -1
@@ -3144,7 +3144,9 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) } } -if (!pdev->failover_pair_id) { +if (!pdev->failover_pair_id && +vfio_container_check_extension (&vbasedev->group->container->bcontainer, +VFIO_FEAT_LIVE_MIGRATION )) { ret =vfio_migration_probe (vbasedev, errp);if (ret) {error_report ("%s: Migration disabled" , vbasedev->name);
hw/vfio/spapr.c +11 -11
@@ -39,8 +39,8 @@ static void *vfio_prereg_gpa_to_vaddr(MemoryRegionSection *section, hwaddr gpa)static void vfio_prereg_listener_region_add (MemoryListener *listener,MemoryRegionSection *section) { -VFIOContainer *container =container_of (listener, VFIOContainer, - prereg_listener); +VFIOLegacyContainer *container =container_of (listener, VFIOLegacyContainer, + prereg_listener);const hwaddr gpa = section->offset_within_address_space;hwaddr end;int ret;
@@ -83,9 +83,9 @@ static void vfio_prereg_listener_region_add(MemoryListener *listener,* can gracefully fail. Runtime, there's not much we can do other * than throw a hardware error. */ -if (!container->initialized) { -if (!container->error) { -error_setg_errno (&container->error, -ret, +if (!container->bcontainer.initialized) { +if (!container->bcontainer.error) { +error_setg_errno (&container->bcontainer.error, -ret,"Memory registering failed" ); } }else {
@@ -97,8 +97,8 @@ static void vfio_prereg_listener_region_add(MemoryListener *listener,static void vfio_prereg_listener_region_del (MemoryListener *listener,MemoryRegionSection *section) { -VFIOContainer *container =container_of (listener, VFIOContainer, - prereg_listener); +VFIOLegacyContainer *container =container_of (listener, VFIOLegacyContainer, + prereg_listener);const hwaddr gpa = section->offset_within_address_space;hwaddr end;int ret;
@@ -141,7 +141,7 @@ const MemoryListener vfio_prereg_listener = { .region_del = vfio_prereg_listener_region_del, }; -int vfio_spapr_create_window (VFIOContainer *container, +int vfio_spapr_create_window (VFIOLegacyContainer *container,MemoryRegionSection *section,hwaddr *pgsize) {
@@ -159,13 +159,13 @@ int vfio_spapr_create_window(VFIOContainer *container,if (pagesize > rampagesize) { pagesize = rampagesize; } - pgmask = container->pgsizes & (pagesize | (pagesize -1 )); + pgmask = container->bcontainer.pgsizes & (pagesize | (pagesize -1 )); pagesize = pgmask ? (1ULL << (63 -clz64 (pgmask))) :0 ;if (!pagesize) {error_report ("Host doesn't support page size 0x%" PRIx64", the supported mask is 0x%lx" ,memory_region_iommu_get_min_page_size (iommu_mr), - container->pgsizes); + container->bcontainer.pgsizes);return -EINVAL ; }
@@ -233,7 +233,7 @@ int vfio_spapr_create_window(VFIOContainer *container,return 0 ; } -int vfio_spapr_remove_window (VFIOContainer *container, +int vfio_spapr_remove_window (VFIOLegacyContainer *container,hwaddr offset_within_address_space) {struct vfio_iommu_spapr_tce_remove remove = {
hw/vfio/meson.build +1 -0
@@ -2,6 +2,7 @@ vfio_ss = ss.source_set() vfio_ss.add(files( 'helpers.c', 'as.c', + 'container-base.c', 'container.c', 'spapr.c', 'migration.c',
Signature · 1 line
2.37.3
RFC v3 8/18 vfio/container: Introduce vfio_[attach/detach]_device
In reply to Eric AugerWe want the VFIO devices to be able to use two different IOMMU callbacks, the legacy VFIO one and the new iommufd one.
Introduce vfio_[attach/detach]_device which aim at hiding the underlying IOMMU backend (IOCTLs, datatypes, ...).
Once vfio_attach_device completes, the device is attached to a security context and its fd can be used. Conversely When vfio_detach_device completes, the device has been detached to the security context.
In this patch, only the vfio-pci device gets converted to use the new API. Subsequent patches will handle other devices.
--- include/hw/vfio/vfio-common.h | 2 ++ hw/vfio/container.c | 65 +++++++++++++++++++++++++++++++++++ hw/vfio/pci.c | 50 +++------------------------ 3 files changed, 72 insertions(+), 45 deletions(-)
include/hw/vfio/vfio-common.h +2 -0
@@ -180,6 +180,8 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);void vfio_put_group (VFIOGroup *group);int vfio_get_device (VFIOGroup *group,const char *name,VFIODevice *vbasedev,Error **errp); +int vfio_attach_device (VFIODevice *vbasedev,AddressSpace *as,Error **errp); +void vfio_detach_device (VFIODevice *vbasedev);extern const MemoryRegionOps vfio_region_ops; typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
hw/vfio/container.c +65 -0
71 lines not on this page · raw message
hw/vfio/pci.c +5 -45
99 lines not on this page · raw message
Signature · 1 line
2.37.3
RFC v3 9/18 vfio/platform: Use vfio_[attach/detach]_device
In reply to Eric AugerLet the vfio-platform device use vfio_attach_device() and vfio_detach_device(), hence hiding the details of the used IOMMU backend.
--- hw/vfio/platform.c | 42 ++---------------------------------------- 1 file changed, 2 insertions(+), 40 deletions(-)
hw/vfio/platform.c +2 -40
@@ -529,12 +529,7 @@ static VFIODeviceOps vfio_platform_ops = {*/ static int vfio_base_device_init (VFIODevice *vbasedev,Error **errp) { -VFIOGroup *group; -VFIODevice *vbasedev_iter; -char *tmp, group_path[PATH_MAX ], *group_name; -ssize_t len;struct stat st; -int groupid;int ret;/* @sysfsdev takes precedence over @host */
@@ -557,47 +552,14 @@ static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp)return -errno; } - tmp =g_strdup_printf ("%s/iommu_group" , vbasedev->sysfsdev); - len =readlink (tmp, group_path,sizeof (group_path)); -g_free (tmp); - -if (len <0 || len >=sizeof (group_path)) { - ret = len <0 ? -errno : -ENAMETOOLONG ; -error_setg_errno (errp, -ret,"no iommu_group found" ); -return ret; - } - - group_path[len] =0 ; - - group_name =basename (group_path); -if (sscanf (group_name,"%d" , &groupid) !=1 ) { -error_setg_errno (errp, errno,"failed to read %s" , group_path); -return -errno; - } - -trace_vfio_platform_base_device_init (vbasedev->name, groupid); - - group =vfio_get_group (groupid, &address_space_memory, errp); -if (!group) { -return -ENOENT ; - } - - QLIST_FOREACH(vbasedev_iter, &group->device_list, next) { -if (strcmp (vbasedev_iter->name, vbasedev->name) ==0 ) { -error_setg (errp,"device is already attached" ); -vfio_put_group (group); -return -EBUSY ; - } - } - ret =vfio_get_device (group, vbasedev->name, vbasedev, errp); + ret =vfio_attach_device (vbasedev, &address_space_memory, errp);if (ret) { -vfio_put_group (group);return ret; } ret =vfio_populate_device (vbasedev, errp);if (ret) { -vfio_put_group (group); +vfio_detach_device (vbasedev); }return ret;
Signature · 1 line
2.37.3
RFC v3 10/18 vfio/ap: Use vfio_[attach/detach]_device
In reply to Eric AugerLet the vfio-ap device use vfio_attach_device() and vfio_detach_device(), hence hiding the details of the used IOMMU backend.
--- hw/vfio/ap.c | 62 ++++++++-------------------------------------------- 1 file changed, 9 insertions(+), 53 deletions(-)
hw/vfio/ap.c +9 -53
@@ -50,58 +50,17 @@ struct VFIODeviceOps vfio_ap_ops = { .vfio_compute_needs_reset = vfio_ap_compute_needs_reset, }; -static void vfio_ap_put_device (VFIOAPDevice *vapdev) -{ -g_free (vapdev->vdev.name); -vfio_put_base_device (&vapdev->vdev); -} - -static VFIOGroup *vfio_ap_get_group (VFIOAPDevice *vapdev,Error **errp) -{ -GError *gerror =NULL ; -char *symlink, *group_path; -int groupid; - - symlink =g_strdup_printf ("%s/iommu_group" , vapdev->vdev.sysfsdev); - group_path =g_file_read_link (symlink, &gerror); -g_free (symlink); - -if (!group_path) { -error_setg (errp,"%s: no iommu_group found for %s: %s" , -TYPE_VFIO_AP_DEVICE , vapdev->vdev.sysfsdev, gerror->message); -g_error_free (gerror); -return NULL ; - } - -if (sscanf (basename (group_path),"%d" , &groupid) !=1 ) { -error_setg (errp,"vfio: failed to read %s" , group_path); -g_free (group_path); -return NULL ; - } - -g_free (group_path); - -return vfio_get_group (groupid, &address_space_memory, errp); -} -static void vfio_ap_realize (DeviceState *dev,Error **errp) { -int ret; -char *mdevid; -VFIOGroup *vfio_group;APDevice *apdev =AP_DEVICE (dev);VFIOAPDevice *vapdev =VFIO_AP_DEVICE (apdev); +VFIODevice *vbasedev = &vapdev->vdev; +int ret; - vfio_group =vfio_ap_get_group (vapdev, errp); -if (!vfio_group) { -return ; - } - - vapdev->vdev.ops = &vfio_ap_ops; - vapdev->vdev.type =VFIO_DEVICE_TYPE_AP ; - mdevid =basename (vapdev->vdev.sysfsdev); - vapdev->vdev.name =g_strdup_printf ("%s" , mdevid); - vapdev->vdev.dev = dev; + vbasedev->name =g_path_get_basename (vbasedev->sysfsdev); + vbasedev->ops = &vfio_ap_ops; + vbasedev->type =VFIO_DEVICE_TYPE_AP ; + vbasedev->dev = dev;/* * vfio-ap devices operate in a way compatible with discarding of
@@ -111,7 +70,7 @@ static void vfio_ap_realize(DeviceState *dev, Error **errp)*/ vapdev->vdev.ram_block_discard_allowed = true; - ret =vfio_get_device (vfio_group, mdevid, &vapdev->vdev, errp); + ret =vfio_attach_device (vbasedev, &address_space_memory, errp);if (ret) { goto out_get_dev_err; }
@@ -119,18 +78,15 @@ static void vfio_ap_realize(DeviceState *dev, Error **errp)return ; out_get_dev_err: -vfio_ap_put_device (vapdev); -vfio_put_group (vfio_group); +vfio_detach_device (vbasedev); }static void vfio_ap_unrealize (DeviceState *dev) {APDevice *apdev =AP_DEVICE (dev);VFIOAPDevice *vapdev =VFIO_AP_DEVICE (apdev); -VFIOGroup *group = vapdev->vdev.group; -vfio_ap_put_device (vapdev); -vfio_put_group (group); +vfio_detach_device (&vapdev->vdev); }static Property vfio_ap_properties[] = {
Signature · 1 line
2.37.3
RFC v3 11/18 vfio/ccw: Use vfio_[attach/detach]_device
In reply to Eric AugerLet the vfio-ccw device use vfio_attach_device() and vfio_detach_device(), hence hiding the details of the used IOMMU backend.
Also now all the devices have been migrated to use the new vfio_attach_device/vfio_detach_device API, let's turn the legacy functions into static functions, local to container.c.
--- include/hw/vfio/vfio-common.h | 4 -- hw/vfio/ccw.c | 118 ++++++++-------------------------- hw/vfio/container.c | 8 +-- 3 files changed, 32 insertions(+), 98 deletions(-)
include/hw/vfio/vfio-common.h +0 -4
@@ -176,10 +176,6 @@ void vfio_region_unmap(VFIORegion *region);void vfio_region_exit (VFIORegion *region);void vfio_region_finalize (VFIORegion *region);void vfio_reset_handler (void *opaque); -VFIOGroup *vfio_get_group (int groupid,AddressSpace *as,Error **errp); -void vfio_put_group (VFIOGroup *group); -int vfio_get_device (VFIOGroup *group,const char *name, -VFIODevice *vbasedev,Error **errp);int vfio_attach_device (VFIODevice *vbasedev,AddressSpace *as,Error **errp);void vfio_detach_device (VFIODevice *vbasedev);
hw/vfio/ccw.c +28 -90
160 lines not on this page · raw message
hw/vfio/container.c +4 -4
@@ -993,7 +993,7 @@ static void vfio_disconnect_container(VFIOGroup *group) } } -VFIOGroup *vfio_get_group (int groupid,AddressSpace *as,Error **errp) +static VFIOGroup *vfio_get_group (int groupid,AddressSpace *as,Error **errp) {VFIOGroup *group;VFIOContainer *bcontainer;
@@ -1062,7 +1062,7 @@ free_group_exit:return NULL ; } -void vfio_put_group (VFIOGroup *group) +static void vfio_put_group (VFIOGroup *group) {if (!group || !QLIST_EMPTY (&group->device_list)) {return ;
@@ -1083,8 +1083,8 @@ void vfio_put_group(VFIOGroup *group) } } -int vfio_get_device (VFIOGroup *group,const char *name, -VFIODevice *vbasedev,Error **errp) +static int vfio_get_device (VFIOGroup *group,const char *name, +VFIODevice *vbasedev,Error **errp) {struct vfio_device_info dev_info = { .argsz =sizeof (dev_info) };int ret, fd;
Signature · 1 line
2.37.3
Re: [RFC v3 11/18] vfio/ccw: Use vfio_[attach/detach]_device
Quoted 10 lines · Eric Auger
Let the vfio-ccw device use
vfio_attach_device()andvfio_detach_device(), hence hiding the details of the used IOMMU backend.Also now all the devices have been migrated to use the new vfio_attach_device/vfio_detach_device API, let's turn the legacy functions into static functions, local to
container.c.Signed-off-by Eric Auger <[email protected]>Signed-off-by Yi Liu <[email protected]>
Hi Eric,
While testing the cdev series on s390 I ran into a couple of issues with this patch, see below.
Quoted 38 lines
--- include/hw/vfio/vfio-common.h | 4 -- hw/vfio/ccw.c | 118 ++++++++-------------------------- hw/vfio/container.c | 8 +-- 3 files changed, 32 insertions(+), 98 deletions(-)include/hw/vfio/vfio-common.h +0
-4@@ -176,10 +176,6 @@ void vfio_region_unmap(VFIORegion *region);void vfio_region_exit (VFIORegion *region);void vfio_region_finalize (VFIORegion *region);void vfio_reset_handler (void *opaque); -VFIOGroup *vfio_get_group (int groupid,AddressSpace *as,Error **errp); -void vfio_put_group (VFIOGroup *group); -int vfio_get_device (VFIOGroup *group,const char *name, -VFIODevice *vbasedev,Error **errp);int vfio_attach_device (VFIODevice *vbasedev,AddressSpace *as,Error **errp);void vfio_detach_device (VFIODevice *vbasedev);hw/vfio/ccw.c +1
-8@@ -579,27 +579,32 @@ static void vfio_ccw_put_region(VFIOCCWDevice *vcdev)g_free (vcdev->io_region); } -static void vfio_ccw_put_device (VFIOCCWDevice *vcdev) -{ -g_free (vcdev->vdev.name); -vfio_put_base_device (&vcdev->vdev); -} - -static void vfio_ccw_get_device (VFIOGroup *group,VFIOCCWDevice *vcdev, -Error **errp) +static void vfio_ccw_realize (DeviceState *dev,Error **errp)
hw/vfio/ccw.c static void vfio_ccw_put_region(VFIOCCWDevice *vcdev) · line 592
Quoted 10 lines
hw/vfio/ccw.c +6
-0{ +CcwDevice *ccw_dev =DO_UPCAST (CcwDevice, parent_obj, dev); +S390CCWDevice *cdev =DO_UPCAST (S390CCWDevice, parent_obj, ccw_dev); +VFIOCCWDevice *vcdev =DO_UPCAST (VFIOCCWDevice, cdev, cdev); +S390CCWDeviceClass *cdc =S390_CCW_DEVICE_GET_CLASS (cdev); +VFIODevice *vbasedev = &vcdev->vdev; +Error *err =NULL ;char *name =g_strdup_printf ("%x.%x.%04x" , vcdev->cdev.hostid.cssid, vcdev->cdev.hostid.ssid, vcdev->cdev.hostid.devid);
We can't get these cssid, ssid, devid values quite yet, they are currently 0s. That has to happen after cdc->realize()
Quoted 10 lines
hw/vfio/ccw.c +3
-6-VFIODevice *vbasedev; +int ret; - QLIST_FOREACH(vbasedev, &group->device_list, next) { -if (strcmp (vbasedev->name, name) ==0 ) { -error_setg (errp,"vfio: subchannel %s has already been attached" , - name); - goto out_err; +/* Call the class init function for subchannel. */ +if (cdc->realize) {
hw/vfio/ccw.c static void vfio_ccw_put_region(VFIOCCWDevice *vcdev) · line 606
Quoted 10 lines
hw/vfio/ccw.c +8
-0+ cdc->realize (cdev, vcdev->vdev.sysfsdev, &err); +if (err) { + goto out_err_propagate; } } + vbasedev->sysfsdev =g_strdup_printf ("/sys/bus/css/devices/%s/%s" , + name, cdev->mdevid); + vbasedev->ops = &vfio_ccw_ops; + vbasedev->type =VFIO_DEVICE_TYPE_CCW ; + vbasedev->name = name;
vbasedev->name is being set to the wrong value here, it needs to be the uuid.
See below for a suggested diff on top of this patch that solves the issue for me.
Thanks,
Matt
hw/vfio/ccw.c +6 -6
@@ -587,9 +587,6 @@ static void vfio_ccw_realize(DeviceState *dev, Error **errp)S390CCWDeviceClass *cdc =S390_CCW_DEVICE_GET_CLASS (cdev);VFIODevice *vbasedev = &vcdev->vdev;Error *err =NULL ; -char *name =g_strdup_printf ("%x.%x.%04x" , vcdev->cdev.hostid.cssid, - vcdev->cdev.hostid.ssid, - vcdev->cdev.hostid.devid);int ret;/* Call the class init function for subchannel. */
@@ -599,11 +596,14 @@ static void vfio_ccw_realize(DeviceState *dev, Error **errp) goto out_err_propagate; } } - vbasedev->sysfsdev =g_strdup_printf ("/sys/bus/css/devices/%s/%s" , - name, cdev->mdevid); + vbasedev->sysfsdev =g_strdup_printf ("/sys/bus/css/devices/%x.%x.%04x/%s" , + vcdev->cdev.hostid.cssid, + vcdev->cdev.hostid.ssid, + vcdev->cdev.hostid.devid, + cdev->mdevid); vbasedev->ops = &vfio_ccw_ops; vbasedev->type =VFIO_DEVICE_TYPE_CCW ; - vbasedev->name = name; + vbasedev->name =g_strdup (cdev->mdevid); vbasedev->dev = &vcdev->cdev.parent_obj.parent_obj;/*
Re: [RFC v3 11/18] vfio/ccw: Use vfio_[attach/detach]_device
Hi Matthew,
Quoted 118 lines · Matthew Rosato
Quoted 10 lines · Eric Auger
Let the vfio-ccw device use
vfio_attach_device()andvfio_detach_device(), hence hiding the details of the used IOMMU backend.Also now all the devices have been migrated to use the new vfio_attach_device/vfio_detach_device API, let's turn the legacy functions into static functions, local to
container.c.Signed-off-by Eric Auger <[email protected]>Signed-off-by Yi Liu <[email protected]>Hi Eric,
While testing the cdev series on s390 I ran into a couple of issues with this patch, see below.
Quoted 38 lines
--- include/hw/vfio/vfio-common.h | 4 -- hw/vfio/ccw.c | 118 ++++++++-------------------------- hw/vfio/container.c | 8 +-- 3 files changed, 32 insertions(+), 98 deletions(-)include/hw/vfio/vfio-common.h +0
-4@@ -176,10 +176,6 @@ void vfio_region_unmap(VFIORegion *region);void vfio_region_exit (VFIORegion *region);void vfio_region_finalize (VFIORegion *region);void vfio_reset_handler (void *opaque); -VFIOGroup *vfio_get_group (int groupid,AddressSpace *as,Error **errp); -void vfio_put_group (VFIOGroup *group); -int vfio_get_device (VFIOGroup *group,const char *name, -VFIODevice *vbasedev,Error **errp);int vfio_attach_device (VFIODevice *vbasedev,AddressSpace *as,Error **errp);void vfio_detach_device (VFIODevice *vbasedev);hw/vfio/ccw.c +1
-8@@ -579,27 +579,32 @@ static void vfio_ccw_put_region(VFIOCCWDevice *vcdev)g_free (vcdev->io_region); } -static void vfio_ccw_put_device (VFIOCCWDevice *vcdev) -{ -g_free (vcdev->vdev.name); -vfio_put_base_device (&vcdev->vdev); -} - -static void vfio_ccw_get_device (VFIOGroup *group,VFIOCCWDevice *vcdev, -Error **errp) +static void vfio_ccw_realize (DeviceState *dev,Error **errp)hw/vfio/ccw.c static void vfio_ccw_put_region(VFIOCCWDevice *vcdev) · line 592
Quoted 10 lines
hw/vfio/ccw.c +6
-0{ +CcwDevice *ccw_dev =DO_UPCAST (CcwDevice, parent_obj, dev); +S390CCWDevice *cdev =DO_UPCAST (S390CCWDevice, parent_obj, ccw_dev); +VFIOCCWDevice *vcdev =DO_UPCAST (VFIOCCWDevice, cdev, cdev); +S390CCWDeviceClass *cdc =S390_CCW_DEVICE_GET_CLASS (cdev); +VFIODevice *vbasedev = &vcdev->vdev; +Error *err =NULL ;char *name =g_strdup_printf ("%x.%x.%04x" , vcdev->cdev.hostid.cssid, vcdev->cdev.hostid.ssid, vcdev->cdev.hostid.devid);We can't get these cssid, ssid, devid values quite yet, they are currently 0s. That has to happen after
cdc->realize()Quoted 10 lines
hw/vfio/ccw.c +3
-6-VFIODevice *vbasedev; +int ret; - QLIST_FOREACH(vbasedev, &group->device_list, next) { -if (strcmp (vbasedev->name, name) ==0 ) { -error_setg (errp,"vfio: subchannel %s has already been attached" , - name); - goto out_err; +/* Call the class init function for subchannel. */ +if (cdc->realize) {hw/vfio/ccw.c static void vfio_ccw_put_region(VFIOCCWDevice *vcdev) · line 606
Quoted 10 lines
hw/vfio/ccw.c +8
-0+ cdc->realize (cdev, vcdev->vdev.sysfsdev, &err); +if (err) { + goto out_err_propagate; } } + vbasedev->sysfsdev =g_strdup_printf ("/sys/bus/css/devices/%s/%s" , + name, cdev->mdevid); + vbasedev->ops = &vfio_ccw_ops; + vbasedev->type =VFIO_DEVICE_TYPE_CCW ; + vbasedev->name = name;
vbasedev->nameis being set to the wrong value here, it needs to be the uuid.See below for a suggested diff on top of this patch that solves the issue for me.
Thanks,
Matthw/vfio/ccw.c +2
-5@@ -587,9 +587,6 @@ static void vfio_ccw_realize(DeviceState *dev, Error **errp)S390CCWDeviceClass *cdc =S390_CCW_DEVICE_GET_CLASS (cdev);VFIODevice *vbasedev = &vcdev->vdev;Error *err =NULL ; -char *name =g_strdup_printf ("%x.%x.%04x" , vcdev->cdev.hostid.cssid, - vcdev->cdev.hostid.ssid, - vcdev->cdev.hostid.devid);int ret;/* Call the class init function for subchannel. */ @@ -599,11 +596,14 @@ static void vfio_ccw_realize(DeviceState *dev, Error **errp) goto out_err_propagate; } } - vbasedev->sysfsdev =g_strdup_printf ("/sys/bus/css/devices/%s/%s" , - name, cdev->mdevid); + vbasedev->sysfsdev = g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s", + vcdev->cdev.hostid.cssid,
hw/vfio/ccw.c static void vfio_ccw_realize(DeviceState *dev, Error **errp) · line 609
Quoted 10 lines
hw/vfio/ccw.c +4
-1+ vcdev->cdev.hostid.ssid, + vcdev->cdev.hostid.devid, + cdev->mdevid); vbasedev->ops = &vfio_ccw_ops; vbasedev->type =VFIO_DEVICE_TYPE_CCW ; - vbasedev->name = name; + vbasedev->name =g_strdup (cdev->mdevid); vbasedev->dev = &vcdev->cdev.parent_obj.parent_obj;/*
Thank you very much for your report.
This will be handled in the next version.
Eric
RFC v3 12/18 vfio/container-base: Introduce [attach/detach]_device container callbacks
In reply to Eric AugerLet's turn attach/detach_device as container callbacks. That way, their implementation can be easily customized for a given backend.
For the time being, only the legacy container is supported.
--- include/hw/vfio/vfio-common.h | 1 + include/hw/vfio/vfio-container-base.h | 2 ++ hw/vfio/as.c | 21 +++++++++++++++++++++ hw/vfio/container.c | 9 +++++++-- hw/vfio/pci.c | 2 +- 5 files changed, 32 insertions(+), 3 deletions(-)
include/hw/vfio/vfio-common.h +1 -0
@@ -86,6 +86,7 @@ typedef struct VFIODeviceOps VFIODeviceOps; typedef struct VFIODevice { QLIST_ENTRY(VFIODevice) next; struct VFIOGroup *group; + VFIOContainer *container; char *sysfsdev; char *name; DeviceState *dev;
include/hw/vfio/vfio-container-base.h +2 -0
@@ -133,6 +133,8 @@ struct VFIOIOMMUBackendOpsClass {int (*dma_unmap)(VFIOContainer *container,hwaddr iova,ram_addr_t size,IOMMUTLBEntry *iotlb); +int (*attach_device)(VFIODevice *vbasedev,AddressSpace *as,Error **errp); +void (*detach_device)(VFIODevice *vbasedev);/* migration feature */ bool (*devices_all_dirty_tracking)(VFIOContainer *container);void (*set_dirty_page_tracking)(VFIOContainer *container,bool start);
hw/vfio/as.c +21 -0
@@ -851,6 +851,27 @@ void vfio_put_address_space(VFIOAddressSpace *space) } } +int vfio_attach_device (VFIODevice *vbasedev,AddressSpace *as,Error **errp) +{ +const VFIOIOMMUBackendOpsClass *ops; + + ops =VFIO_IOMMU_BACKEND_OPS_CLASS ( +object_class_by_name (TYPE_VFIO_IOMMU_BACKEND_LEGACY_OPS )); +if (!ops) { +error_setg (errp,"VFIO IOMMU Backend not found!" ); +return -ENODEV ; + } +return ops->attach_device (vbasedev, as, errp); +} + +void vfio_detach_device (VFIODevice *vbasedev) +{ +if (!vbasedev->container) { +return ; + } + vbasedev->container->ops->detach_device (vbasedev); +} +static const TypeInfo vfio_iommu_backend_ops_type_info = { .name =TYPE_VFIO_IOMMU_BACKEND_OPS , .parent =TYPE_OBJECT ,
hw/vfio/container.c +7 -2
@@ -1278,7 +1278,8 @@ static int vfio_device_groupid(VFIODevice *vbasedev, Error **errp)return groupid; } -int vfio_attach_device (VFIODevice *vbasedev,AddressSpace *as,Error **errp) +static int +vfio_legacy_attach_device (VFIODevice *vbasedev,AddressSpace *as,Error **errp) {int groupid =vfio_device_groupid (vbasedev, errp);VFIODevice *vbasedev_iter;
@@ -1307,14 +1308,16 @@ int vfio_attach_device(VFIODevice *vbasedev, AddressSpace *as, Error **errp)vfio_put_group (group);return -1 ; } + vbasedev->container = &group->container->bcontainer;return 0 ; } -void vfio_detach_device (VFIODevice *vbasedev) +static void vfio_legacy_detach_device (VFIODevice *vbasedev) {vfio_put_base_device (vbasedev);vfio_put_group (vbasedev->group); + vbasedev->container =NULL ; } static void vfio_iommu_backend_legacy_ops_class_init(ObjectClass *oc,
@@ -1329,6 +1332,8 @@ static void vfio_iommu_backend_legacy_ops_class_init(ObjectClass *oc, ops->add_window = vfio_legacy_container_add_section_window; ops->del_window = vfio_legacy_container_del_section_window; ops->check_extension = vfio_legacy_container_check_extension; + ops->attach_device = vfio_legacy_attach_device; + ops->detach_device = vfio_legacy_detach_device; }static const TypeInfo vfio_iommu_backend_legacy_ops_type = {
hw/vfio/pci.c +1 -1
@@ -3106,7 +3106,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) }if (!pdev->failover_pair_id && -vfio_container_check_extension (&vbasedev->group->container->bcontainer, +vfio_container_check_extension (vbasedev->container,VFIO_FEAT_LIVE_MIGRATION )) { ret =vfio_migration_probe (vbasedev, errp);if (ret) {
Signature · 1 line
2.37.3
RFC v3 13/18 vfio/container-base: Introduce VFIOContainer reset callback
In reply to Eric AugerReset implementation depends on the container backend. Let's introduce a VFIOContainer class function and register a generic reset handler that will be able to call the right reset function depending on the container type. Also, let's move the registration/unregistration to a place that is not backend-specific (first vfio address space created instead of the first group).
--- include/hw/vfio/vfio-container-base.h | 2 ++ hw/vfio/as.c | 18 ++++++++++++++++++ hw/vfio/container-base.c | 9 +++++++++ hw/vfio/container.c | 27 +++++++++++++++------------ 4 files changed, 44 insertions(+), 12 deletions(-)
include/hw/vfio/vfio-container-base.h +2 -0
@@ -97,6 +97,7 @@ int vfio_container_dma_map(VFIOContainer *container,int vfio_container_dma_unmap (VFIOContainer *container,hwaddr iova,ram_addr_t size,IOMMUTLBEntry *iotlb); +int vfio_container_reset (VFIOContainer *container);bool vfio_container_devices_all_dirty_tracking (VFIOContainer *container);void vfio_container_set_dirty_page_tracking (VFIOContainer *container,bool start);
@@ -135,6 +136,7 @@ struct VFIOIOMMUBackendOpsClass { IOMMUTLBEntry *iotlb);int (*attach_device)(VFIODevice *vbasedev,AddressSpace *as,Error **errp);void (*detach_device)(VFIODevice *vbasedev); +int (*reset)(VFIOContainer *container);/* migration feature */ bool (*devices_all_dirty_tracking)(VFIOContainer *container);void (*set_dirty_page_tracking)(VFIOContainer *container,bool start);
hw/vfio/as.c +18 -0
@@ -823,6 +823,18 @@ const MemoryListener vfio_memory_listener = { .log_sync = vfio_listener_log_sync, }; +void vfio_reset_handler (void *opaque) +{ +VFIOAddressSpace *space; +VFIOContainer *bcontainer; + + QLIST_FOREACH(space, &vfio_address_spaces, list) { + QLIST_FOREACH(bcontainer, &space->containers, next) { +vfio_container_reset (bcontainer); + } + } +} +VFIOAddressSpace *vfio_get_address_space (AddressSpace *as) {VFIOAddressSpace *space;
@@ -838,6 +850,9 @@ VFIOAddressSpace *vfio_get_address_space(AddressSpace *as) space->as = as;QLIST_INIT (&space->containers); +if (QLIST_EMPTY (&vfio_address_spaces)) { +qemu_register_reset (vfio_reset_handler,NULL ); + }QLIST_INSERT_HEAD (&vfio_address_spaces, space, list);return space;
@@ -849,6 +864,9 @@ void vfio_put_address_space(VFIOAddressSpace *space)QLIST_REMOVE (space, list);g_free (space); } +if (QLIST_EMPTY (&vfio_address_spaces)) { +qemu_unregister_reset (vfio_reset_handler,NULL ); + } }int vfio_attach_device (VFIODevice *vbasedev,AddressSpace *as,Error **errp)
hw/vfio/container-base.c +9 -0
@@ -58,6 +58,15 @@ int vfio_container_dma_unmap(VFIOContainer *container,return container->ops->dma_unmap (container, iova, size, iotlb); } +int vfio_container_reset (VFIOContainer *container) +{ +if (!container->ops->reset) { +return -ENOENT ; + } + +return container->ops->reset (container); +} +void vfio_container_set_dirty_page_tracking (VFIOContainer *container,bool start) {
hw/vfio/container.c +15 -12
@@ -520,12 +520,16 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,return true; } -void vfio_reset_handler (void *opaque) +static int vfio_legacy_container_reset (VFIOContainer *bcontainer) { +VFIOLegacyContainer *container =container_of (bcontainer, + VFIOLegacyContainer, + bcontainer);VFIOGroup *group;VFIODevice *vbasedev; +int ret, final_ret =0 ; - QLIST_FOREACH(group, &vfio_group_list, next) { + QLIST_FOREACH(group, &container->group_list, container_next) { QLIST_FOREACH(vbasedev, &group->device_list, next) {if (vbasedev->dev->realized) { vbasedev->ops->vfio_compute_needs_reset (vbasedev);
@@ -533,13 +537,19 @@ void vfio_reset_handler(void *opaque) } } - QLIST_FOREACH(group, &vfio_group_list, next) { + QLIST_FOREACH(group, &container->group_list, next) { QLIST_FOREACH(vbasedev, &group->device_list, next) {if (vbasedev->dev->realized && vbasedev->needs_reset) { - vbasedev->ops->vfio_hot_reset_multi (vbasedev); + ret = vbasedev->ops->vfio_hot_reset_multi (vbasedev); +if (ret) { +error_report ("failed to reset %s (%d)" , + vbasedev->name, ret); + final_ret = ret; + } } } } +return final_ret; }static void vfio_kvm_device_add_group (VFIOGroup *group)
@@ -1045,10 +1055,6 @@ static VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp) goto close_fd_exit; } -if (QLIST_EMPTY (&vfio_group_list)) { -qemu_register_reset (vfio_reset_handler,NULL ); - } -QLIST_INSERT_HEAD (&vfio_group_list, group, next);return group;
@@ -1077,10 +1083,6 @@ static void vfio_put_group(VFIOGroup *group)trace_vfio_put_group (group->fd);close (group->fd);g_free (group); - -if (QLIST_EMPTY (&vfio_group_list)) { -qemu_unregister_reset (vfio_reset_handler,NULL ); - } } static int vfio_get_device(VFIOGroup *group, const char *name,
@@ -1334,6 +1336,7 @@ static void vfio_iommu_backend_legacy_ops_class_init(ObjectClass *oc, ops->check_extension = vfio_legacy_container_check_extension; ops->attach_device = vfio_legacy_attach_device; ops->detach_device = vfio_legacy_detach_device; + ops->reset = vfio_legacy_container_reset; }static const TypeInfo vfio_iommu_backend_legacy_ops_type = {
Signature · 1 line
2.37.3
RFC v3 14/18 backends/iommufd: Introduce the iommufd object
In reply to Eric AugerIntroduce an iommufd object which allows the interaction with the host /dev/iommu device.
The /dev/iommu can have been already pre-opened outside of qemu, in which case the fd can be passed directly along with the iommufd object:
This allows the iommufd object to be shared accross several subsystems (VFIO, VDPA, ...). For example, libvirt would open the /dev/iommu once.
If no fd is passed along with the iommufd object, the /dev/iommu is opened by the qemu code.
The CONFIG_IOMMUFD option must be set to compile this new object.
---
v2 -> v3:
- add stub file
--- MAINTAINERS | 7 ++ qapi/qom.json | 16 ++- include/sysemu/iommufd.h | 47 +++++++ backends/iommufd.c | 265 +++++++++++++++++++++++++++++++++++++++ backends/iommufd_stub.c | 35 ++++++ backends/Kconfig | 5 + backends/meson.build | 2 + backends/trace-events | 12 ++ qemu-options.hx | 12 ++ 9 files changed, 400 insertions(+), 1 deletion(-) create mode 100644 include/sysemu/iommufd.h create mode 100644 backends/iommufd.c create mode 100644 backends/iommufd_stub.c
MAINTAINERS +7 -0
@@ -2020,6 +2020,13 @@ F: hw/vfio/ap.c F: docs/system/s390x/vfio-ap.rst L: [email protected] +iommufd +M: Yi Liu <[email protected]> +M: Eric Auger <[email protected]> +S: Supported +F: backends/iommufd.c +F: include/sysemu/iommufd.h + vhost M: Michael S. Tsirkin <[email protected]> S: Supported
qapi/qom.json +15 -1
@@ -752,6 +752,18 @@ { 'struct': 'VfioUserServerProperties', 'data': { 'socket': 'SocketAddress', 'device': 'str' } } +## +# @IOMMUFDProperties: +# +# Properties for IOMMUFDbackend objects. +# +# fd: file descriptor name +# +# Since: 7.2 +## +{ 'struct': 'IOMMUFDProperties', + 'data': { '*fd': 'str' } } + ## # @RngProperties: #
@@ -903,6 +915,7 @@ 'qtest', 'rng-builtin', 'rng-egd', + 'iommufd', { 'name': 'rng-random', 'if': 'CONFIG_POSIX' }, 'secret',
@@ -984,7 +997,8 @@ 'tls-creds-x509': 'TlsCredsX509Properties', 'tls-cipher-suites': 'TlsCredsProperties', 'x-remote-object': 'RemoteObjectProperties', - 'x-vfio-user-server': 'VfioUserServerProperties' + 'x-vfio-user-server': 'VfioUserServerProperties', + 'iommufd': 'IOMMUFDProperties' } } ##
include/sysemu/iommufd.h new file +47 -0
47 lines not on this page · raw message
backends/iommufd.c new file +265 -0
265 lines not on this page · raw message
backends/iommufd_stub.c new file +35 -0
@@ -0,0 +1,35 @@ +/* +* iommufd container backend stub +* +* Copyright (C) 2022 Intel Corporation. +* Copyright Red Hat, Inc. 2022 +* +* Authors: Yi Liu <[email protected]> +* Eric Auger <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. + +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. + +* You should have received a copy of the GNU General Public License along +* with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +#include "qemu/osdep.h" +#include "sysemu/iommufd.h" +#include "qemu/error-report.h" + +int iommufd_backend_connect (IOMMUFDBackend *be,Error **errp) +{ +return 0 ; +} + +void iommufd_backend_disconnect (IOMMUFDBackend *be) +{ +}
backends/Kconfig +5 -0
@@ -1 +1,6 @@ source tpm/Kconfig + +config IOMMUFD + bool + default y + depends on LINUX
backends/meson.build +2 -0
@@ -18,6 +18,8 @@ endif if have_vhost_user softmmu_ss.add(when: 'CONFIG_VIRTIO', if_true: files('vhost-user.c')) endif +specific_ss.add(when: 'CONFIG_IOMMUFD', if_true: files('iommufd.c'), + if_false: files('iommufd_stub.c')) softmmu_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-vhost.c')) if have_vhost_user_crypto softmmu_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-vhost-user.c'))
backends/trace-events +12 -0
@@ -5,3 +5,15 @@ dbus_vmstate_pre_save(void) dbus_vmstate_post_load(int version_id) "version_id: %d" dbus_vmstate_loading(const char *id) "id: %s" dbus_vmstate_saving(const char *id) "id: %s" + +# iommufd.c +iommufd_backend_connect(int fd, bool owned, uint32_t users, int ret) "fd=%d owned=%d users=%d (%d)" +iommufd_backend_disconnect(int fd, uint32_t users) "fd=%d users=%d" +iommu_backend_set_fd(int fd) "pre-opened /dev/iommu fd=%d" +iommufd_backend_get_ioas(int iommufd, uint32_t ioas, int ret) " iommufd=%d ioas=%d (%d)" +iommufd_backend_put_ioas(int iommufd, uint32_t ioas) " iommufd=%d ioas=%d" +iommufd_backend_unmap_dma(int iommufd, uint32_t ioas, uint64_t iova, uint64_t size, int ret) " iommufd=%d ioas=%d iova=0x%"PRIx64" size=0x%"PRIx64" (%d)" +iommufd_backend_map_dma(int iommufd, uint32_t ioas, uint64_t iova, uint64_t size, void *vaddr, bool readonly, int ret) " iommufd=%d ioas=%d iova=0x%"PRIx64" size=0x%"PRIx64" addr=%p readonly=%d (%d)" +iommufd_backend_copy_dma(int iommufd, uint32_t src_ioas, uint32_t dst_ioas, uint64_t iova, uint64_t size, bool readonly, int ret) " iommufd=%d src_ioas=%d dst_ioas=%d iova=0x%"PRIx64" size=0x%"PRIx64" readonly=%d (%d)" +iommufd_backend_alloc_ioas(int iommufd, uint32_t ioas, int ret) " iommufd=%d ioas=%d (%d)" +iommufd_backend_free_ioas(int iommufd, uint32_t ioas, int ret) " iommufd=%d ioas=%d (%d)"
qemu-options.hx +12 -0
@@ -4993,6 +4993,18 @@ SRST The ``share`` boolean option is on by default with memfd. + ``-object iommufd,id=id[,fd=fd]`` + Creates an iommufd backend which allows control of DMA mapping + through the /dev/iommu device. + + The ``id`` parameter is a unique ID which frontends (such as + vfio-pci of vdpa) will use to connect withe the iommufd backend. + + The ``fd`` parameter is an optional pre-opened file descriptor + resulting from /dev/iommu opening. Usually the iommufd is shared + accross all subsystems, bringing the benefit of centralized + reference counting. + ``-object rng-builtin,id=id`` Creates a random number generator backend which obtains entropy from QEMU builtin functions. The ``id`` parameter is a unique ID
Signature · 1 line
2.37.3
Re: [RFC v3 14/18] backends/iommufd: Introduce the iommufd object
Hi Eric,
include/sysemu/iommufd.h line 7
Quoted 13 lines · Eric Auger
include/sysemu/iommufd.h new file +7
-0@@ -0,0 +1,47 @@ +#ifndef SYSEMU_IOMMUFD_H +#define SYSEMU_IOMMUFD_H + +#include "qom/object.h" +#include "qemu/thread.h" +#include "exec/hwaddr.h" +#include "exec/ram_addr.h"
After rebasing nesting patches on top of this, I see a build error:
====================================================================
[47/876] Compiling C object libcommon.fa.p/hw_arm_smmu-common.c.o
FAILED: libcommon.fa.p/hw_arm_smmu-common.c.o
cc -Ilibcommon.fa.p -I../src/3rdparty/qemu/dtc/libfdt -I/usr/include/pixman-1 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/glib-2.0 -I/usr/lib/aarch64-linux-gnu/glib-2.0/include -I/usr/include/gio-unix-2.0 -fdiagnostics-color=auto -Wall -Winvalid-pch -std=gnu11 -O2 -g -isystem /src/3rdparty/qemu/linux-headers -isystem linux-headers -iquote . -iquote /src/3rdparty/qemu -iquote /src/3rdparty/qemu/include -iquote /src/3rdparty/qemu/tcg/aarch64 -pthread -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fno-common -fwrapv -Wundef -Wwrite-strings -Wmissing-prototypes -Wstrict-prototypes -Wredundant-decls -Wold-style-declaration -Wold-style-definition -Wtype-limits -Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers -Wempty-body -Wnested-externs -Wendif-labels -Wexpansion-to-defined -Wimplicit-fallthrough=2 -Wmissing-format-attribute -Wno-missing-include-dirs -Wno-shift-negative-value -Wno-psabi -fstack-protector-strong -fPIE -MD -MQ libcommon.fa.p/hw_arm_smmu-common.c.o -MF libcommon.fa.p/hw_arm_smmu-common.c.o.d -o libcommon.fa.p/hw_arm_smmu-common.c.o -c ../src/3rdparty/qemu/hw/arm/smmu-common.c In file included from /src/3rdparty/qemu/include/sysemu/iommufd.h:7,
from ../src/3rdparty/qemu/hw/arm/smmu-common.c:29:
/src/3rdparty/qemu/include/exec/ram_addr.h:23:10: fatal error: cpu.h: No such file or directory
23 | #include "cpu.h"
| ^~~~~~~compilation terminated.
====================================================================
I guess it's resulted from the module inter-dependency. Though our nesting patches aren't finalized yet, the possibility of including iommufd.h is still there. Meanwhile, the ram_addr.h here is added for "ram_addr_t" type, I think. So, could we include "cpu-common.h" instead, where the "ram_addr_t" type is actually defined?
The build error is gone after this replacement:
====================================================================
include/sysemu/iommufd.h +1 -1
@@ -4,7 +4,7 @@#include "qom/object.h" #include "qemu/thread.h" #include "exec/hwaddr.h" -#include "exec/ram_addr.h" +#include "exec/cpu-common.h" #include <linux/iommufd.h> #define TYPE_IOMMUFD_BACKEND "iommufd"
====================================================================
Thanks
Nic
Re: [RFC v3 14/18] backends/iommufd: Introduce the iommufd object
Hi Nicolin,
Quoted 28 lines · Nicolin Chen
Hi Eric,
include/sysemu/iommufd.h line 7
Quoted 13 lines · Eric Auger
include/sysemu/iommufd.h new file +7
-0@@ -0,0 +1,47 @@ +#ifndef SYSEMU_IOMMUFD_H +#define SYSEMU_IOMMUFD_H + +#include "qom/object.h" +#include "qemu/thread.h" +#include "exec/hwaddr.h" +#include "exec/ram_addr.h" After rebasing nesting patches on top of this, I see a build error:
====================================================================
[47/876] Compiling C objectlibcommon.fa.p/hw_arm_smmu-common.c.o
FAILED:libcommon.fa.p/hw_arm_smmu-common.c.o
cc -Ilibcommon.fa.p -I../src/3rdparty/qemu/dtc/libfdt -I/usr/include/pixman-1 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/glib-2.0 -I/usr/lib/aarch64-linux-gnu/glib-2.0/include -I/usr/include/gio-unix-2.0 -fdiagnostics-color=auto -Wall -Winvalid-pch -std=gnu11 -O2 -g -isystem /src/3rdparty/qemu/linux-headers -isystem linux-headers -iquote . -iquote /src/3rdparty/qemu -iquote /src/3rdparty/qemu/include -iquote /src/3rdparty/qemu/tcg/aarch64 -pthread -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fno-common -fwrapv -Wundef -Wwrite-strings -Wmissing-prototypes -Wstrict-prototypes -Wredundant-decls -Wold-style-declaration -Wold-style-definition -Wtype-limits -Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers -Wempty-body -Wnested-externs -Wendif-labels -Wexpansion-to-defined -Wimplicit-fallthrough=2 -Wmissing-format-attribute -Wno-missing-include-dirs -Wno-shift-negative-value -Wno-psabi -fstack-protector-strong -fPIE -MD -MQlibcommon.fa.p/hw_arm_smmu-common.c.o-MF libcommon.fa.p/hw_arm_smmu-common.c.o.d -olibcommon.fa.p/hw_arm_smmu-common.c.o-c../src/3rdparty/qemu/hw/arm/smmu-common.cIn file included from /src/3rdparty/qemu/include/sysemu/iommufd.h:7,from ../src/3rdparty/qemu/hw/arm/smmu-common.c:29: /src/3rdparty/qemu/include/exec/ram_addr.h:23:10: fatal error: cpu.h: No such file or directory 23 | #include "cpu.h" | ^~~~~~~
Quoted 8 lines
compilation terminated.
====================================================================I guess it's resulted from the module inter-dependency. Though our nesting patches aren't finalized yet, the possibility of including
iommufd.his still there. Meanwhile, theram_addr.hhere is added for "ram_addr_t" type, I think. So, could we include "cpu-common.h" instead, where the "ram_addr_t" type is actually defined?
Sure. We will fix that on the next iteration
Eric
Quoted 21 lines
The build error is gone after this replacement:
====================================================================include/sysemu/iommufd.h +1
-1@@ -4,7 +4,7 @@#include "qom/object.h" #include "qemu/thread.h" #include "exec/hwaddr.h" -#include "exec/ram_addr.h" +#include "exec/cpu-common.h" #include <linux/iommufd.h> #define TYPE_IOMMUFD_BACKEND "iommufd"====================================================================
Thanks
Nic
RFC v3 15/18 util/char_dev: Add open_cdev()
In reply to Eric AugerFrom: Yi Liu <[email protected]>
/dev/vfio/devices/vfioX may not exist. In that case it is still possible to open /dev/char/$major:$minor instead. Add helper function to abstract the cdev open.
--- MAINTAINERS | 6 ++++ include/qemu/char_dev.h | 16 +++++++++++ util/chardev_open.c | 61 +++++++++++++++++++++++++++++++++++++++++ util/meson.build | 1 + 4 files changed, 84 insertions(+) create mode 100644 include/qemu/char_dev.h create mode 100644 util/chardev_open.c
MAINTAINERS +6 -0
@@ -3247,6 +3247,12 @@ S: Maintained F: include/qemu/iova-tree.h F: util/iova-tree.c +cdev Open +M: Yi Liu <[email protected]> +S: Maintained +F: include/qemu/char_dev.h +F: util/chardev_open.c + elf2dmp M: Viktor Prutyanov <[email protected]> S: Maintained
include/qemu/char_dev.h new file +16 -0
@@ -0,0 +1,16 @@ +/* +* QEMU Chardev Helper +* +* Copyright (C) 2022 Intel Corporation. +* +* Authors: Yi Liu <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +*/ + +#ifndef QEMU_CHARDEV_HELPERS_H +#define QEMU_CHARDEV_HELPERS_H + +int open_cdev (const char *devpath,dev_t cdev); +#endif
util/chardev_open.c new file +61 -0
61 lines not on this page · raw message
util/meson.build +1 -0
@@ -104,4 +104,5 @@ if have_block util_ss.add(files('filemonitor-stub.c')) endif util_ss.add(when: 'CONFIG_LINUX', if_true: files('vfio-helpers.c')) + util_ss.add(when: 'CONFIG_LINUX', if_true: files('chardev_open.c')) endif
Signature · 1 line
2.37.3
RFC v3 16/18 vfio/iommufd: Implement the iommufd backend
In reply to Eric AugerFrom: Yi Liu <[email protected]>
Add the iommufd backend. The IOMMUFD container class is implemented based on the new /dev/iommu user API. This backend obviously depends on CONFIG_IOMMUFD.
So far, the iommufd backend doesn't support live migration and cache coherency yet due to missing support in the host kernel meaning that only a subset of the container class callbacks is implemented.
---
v2 -> v3:
- fixed use after free reported by Alistair
v1 -> v2:
- arbitrarily set bcontainer->pgsizes to 4K to fix interoperability with virtio-iommu (Nicolin)
- add tear down in iommufd_attach_device error path
- use cdev open for /dev/vfio/devices/vfioX open
--- include/hw/vfio/vfio-common.h | 21 + include/hw/vfio/vfio-container-base.h | 1 + hw/vfio/as.c | 9 +- hw/vfio/iommufd.c | 535 ++++++++++++++++++++++++++ hw/vfio/pci.c | 10 + hw/vfio/meson.build | 1 + hw/vfio/trace-events | 11 + 7 files changed, 586 insertions(+), 2 deletions(-) create mode 100644 hw/vfio/iommufd.c
include/hw/vfio/vfio-common.h +21 -0
@@ -81,10 +81,29 @@ typedef struct VFIOLegacyContainer { QLIST_HEAD(, VFIOGroup) group_list; } VFIOLegacyContainer; +typedef struct VFIOIOASHwpt { +uint32_t hwpt_id; + QLIST_HEAD(, VFIODevice) device_list; +QLIST_ENTRY (VFIOIOASHwpt) next; +}VFIOIOASHwpt ; + +typedef struct IOMMUFDBackend IOMMUFDBackend ; + +typedef struct VFIOIOMMUFDContainer { +VFIOContainer bcontainer; +IOMMUFDBackend *be; +uint32_t ioas_id; + QLIST_HEAD(, VFIOIOASHwpt) hwpt_list; +}VFIOIOMMUFDContainer ; + +typedef QLIST_HEAD(VFIOAddressSpaceList, VFIOAddressSpace) VFIOAddressSpaceList; +extern VFIOAddressSpaceList vfio_address_spaces; +typedef struct VFIODeviceOps VFIODeviceOps ; typedef struct VFIODevice { QLIST_ENTRY(VFIODevice) next; + QLIST_ENTRY(VFIODevice) hwpt_next; struct VFIOGroup *group; VFIOContainer *container; char *sysfsdev;
@@ -92,6 +111,7 @@ typedef struct VFIODevice {DeviceState *dev;int fd;int type; +int devid;bool reset_works;bool needs_reset;bool no_mmap;
@@ -104,6 +124,7 @@ typedef struct VFIODevice {VFIOMigration *migration;Error *migration_blocker;OnOffAuto pre_copy_dirty_page_tracking; +IOMMUFDBackend *iommufd; } VFIODevice;struct VFIODeviceOps {
include/hw/vfio/vfio-container-base.h +1 -0
@@ -115,6 +115,7 @@ void vfio_container_init(VFIOContainer *container,void vfio_container_destroy (VFIOContainer *container);#define TYPE_VFIO_IOMMU_BACKEND_LEGACY_OPS "vfio-iommu-backend-legacy-ops" +#define TYPE_VFIO_IOMMU_BACKEND_IOMMUFD_OPS "vfio-iommu-backend-iommufd-ops"#define TYPE_VFIO_IOMMU_BACKEND_OPS "vfio-iommu-backend-ops" DECLARE_CLASS_CHECKERS(VFIOIOMMUBackendOpsClass,
hw/vfio/as.c +7 -2
@@ -42,7 +42,7 @@#include "migration/migration.h" #include "sysemu/tpm.h" -static QLIST_HEAD(, VFIOAddressSpace) vfio_address_spaces = +VFIOAddressSpaceList vfio_address_spaces =QLIST_HEAD_INITIALIZER (vfio_address_spaces); void vfio_host_win_add(VFIOContainer *container, hwaddr min_iova,
@@ -873,8 +873,13 @@ int vfio_attach_device(VFIODevice *vbasedev, AddressSpace *as, Error **errp) {const VFIOIOMMUBackendOpsClass *ops; - ops =VFIO_IOMMU_BACKEND_OPS_CLASS ( +if (vbasedev->iommufd) { + ops =VFIO_IOMMU_BACKEND_OPS_CLASS ( +object_class_by_name (TYPE_VFIO_IOMMU_BACKEND_IOMMUFD_OPS )); + }else { + ops =VFIO_IOMMU_BACKEND_OPS_CLASS (object_class_by_name (TYPE_VFIO_IOMMU_BACKEND_LEGACY_OPS )); + }if (!ops) {error_setg (errp,"VFIO IOMMU Backend not found!" );return -ENODEV ;
hw/vfio/iommufd.c new file +535 -0
535 lines not on this page · raw message
hw/vfio/pci.c +10 -0
@@ -3191,6 +3191,16 @@ static void vfio_pci_reset(DeviceState *dev) goto post_reset; } +/* +* This is a temporary check, long term iommufd should +* support hot reset as well +*/ +if (vdev->vbasedev.iommufd) { +error_report ("Dangerous: iommufd BE doesn't support hot " +"reset, please stop the VM" ); + goto post_reset; + } +/* See if we can do our own bus reset */ if (!vfio_pci_hot_reset_one (vdev)) { goto post_reset;
hw/vfio/meson.build +1 -0
@@ -7,6 +7,7 @@ vfio_ss.add(files( 'spapr.c', 'migration.c', )) +vfio_ss.add(when: 'CONFIG_IOMMUFD', if_true: files( 'iommufd.c')) vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files( 'display.c', 'pci-quirks.c',
hw/vfio/trace-events +11 -0
@@ -166,3 +166,14 @@ vfio_load_state_device_data(const char *name, uint64_t data_offset, uint64_t dat vfio_load_cleanup(const char *name) " (%s)" vfio_get_dirty_bitmap(int fd, uint64_t iova, uint64_t size, uint64_t bitmap_size, uint64_t start) "container fd=%d, iova=0x%"PRIx64" size= 0x%"PRIx64" bitmap_size=0x%"PRIx64" start=0x%"PRIx64 vfio_iommu_map_dirty_notify(uint64_t iova_start, uint64_t iova_end) "iommu dirty @ 0x%"PRIx64" - 0x%"PRIx64 + +#iommufd.c + +vfio_iommufd_get_devicefd(const char *dev, int devfd) " %s (fd=%d)" +vfio_iommufd_bind_device(int iommufd, const char *name, int devfd, int devid) " [iommufd=%d] Succesfully bound device %s (fd=%d): output devid=%d" +vfio_iommufd_attach_device(int iommufd, const char *name, int devfd, int ioasid, int hwptid) " [iommufd=%d] Succesfully attached device %s (%d) to ioasid=%d: output hwptd=%d" +vfio_iommufd_detach_device(int iommufd, const char *name, int ioasid) " [iommufd=%d] Detached %s from ioasid=%d" +vfio_iommufd_alloc_ioas(int iommufd, int ioas_id) " [iommufd=%d] new IOMMUFD container with ioasid=%d" +vfio_iommufd_device_info(char *name, int devfd, int num_irqs, int num_regions, int flags) " %s (%d) num_irqs=%d num_regions=%d flags=%d" +vfio_iommufd_fail_attach_existing_container(const char *msg) " %s" +vfio_iommufd_container_reset(char *name) " Successfully reset %s"
Signature · 1 line
2.37.3
Re: [RFC v3 16/18] vfio/iommufd: Implement the iommufd backend
Quoted 9 lines · Eric Auger
From: Yi Liu <[email protected]>
Add the iommufd backend. The IOMMUFD container class is implemented based on the new
/dev/iommuuser API. This backend obviously depends onCONFIG_IOMMUFD.So far, the iommufd backend doesn't support live migration and cache coherency yet due to missing support in the host kernel meaning that only a subset of the container class callbacks is implemented.
What is missing for cache coherency? I spent lots of time on that already, I thought I got everything..
Jason
Re: [RFC v3 16/18] vfio/iommufd: Implement the iommufd backend
Hi Jason,
Quoted 7 lines · Jason Gunthorpe
Quoted 6 lines · Eric Auger
From: Yi Liu <[email protected]>
Add the iommufd backend. The IOMMUFD container class is implemented based on the new
/dev/iommuuser API. This backend obviously depends onCONFIG_IOMMUFD.
Quoted 5 lines
Quoted 3 lines
So far, the iommufd backend doesn't support live migration and cache coherency yet due to missing support in the host kernel meaning that only a subset of the container class callbacks is implemented.
What is missing for cache coherency? I spent lots of time on that already, I thought I got everything..
I don't think there is anything missing anymore. This was about KVM_DEV_VFIO_GROUP_ADD/DEL. With the advent of KVM_DEV_VFIO_FILE_ADD/DEL we should be able to bridge the gap. However This RFCv3 does not support that yet in the IOMMUFD backend. We will add this support in the next version.
Thanks
Eric
Quoted 3 lines
Jason
RFC v3 17/18 vfio/iommufd: Add IOAS_COPY_DMA support
In reply to Eric AugerFrom: Yi Liu <[email protected]>
Compared with legacy vfio container BE, one of the benefits provided by iommufd is to reduce the redundant page pinning on kernel side through the usage of IOAS_COPY_DMA. For iommufd containers within the same address space, IOVA mappings can be copied from a source container to destination container.
To achieve this, move the vfio_memory_listener to be per address space. In the memory listener callbacks, all the containers within the address space will be looped. For the iommufd containers, QEMU uses IOAS_MAP_DMA on the first one, and then uses IOAS_COPY_DMA to copy the IOVA mappings from the first iommufd container to other iommufd containers within the address space. For legacy containers, IOVA mapping is done by VFIO_IOMMU_MAP_DMA.
--- include/hw/vfio/vfio-common.h | 4 + include/hw/vfio/vfio-container-base.h | 8 +- hw/vfio/as.c | 118 ++++++++++++++++++++++---- hw/vfio/container-base.c | 13 ++- hw/vfio/container.c | 19 ++--- hw/vfio/iommufd.c | 48 +++++++++-- 6 files changed, 169 insertions(+), 41 deletions(-)
include/hw/vfio/vfio-common.h +4 -0
@@ -178,6 +178,10 @@ void vfio_host_win_add(VFIOContainer *bcontainer,int vfio_host_win_del (VFIOContainer *bcontainer,hwaddr min_iova,hwaddr max_iova);VFIOAddressSpace *vfio_get_address_space (AddressSpace *as); +void vfio_as_add_container (VFIOAddressSpace *space, +VFIOContainer *bcontainer); +void vfio_as_del_container (VFIOAddressSpace *space, +VFIOContainer *container);void vfio_put_address_space (VFIOAddressSpace *space);void vfio_put_base_device (VFIODevice *vbasedev);
include/hw/vfio/vfio-container-base.h +7 -1
@@ -31,12 +31,15 @@typedef enum VFIOContainerFeature {VFIO_FEAT_LIVE_MIGRATION , +VFIO_FEAT_DMA_COPY , }VFIOContainerFeature ;typedef struct VFIOContainer VFIOContainer ;typedef struct VFIOAddressSpace {AddressSpace *as; +MemoryListener listener; +bool listener_initialized; QLIST_HEAD(, VFIOContainer) containers;QLIST_ENTRY (VFIOAddressSpace) list; }VFIOAddressSpace ;
@@ -75,7 +78,6 @@ typedef struct VFIOIOMMUBackendOpsClass VFIOIOMMUBackendOpsClass;struct VFIOContainer {VFIOIOMMUBackendOpsClass *ops;VFIOAddressSpace *space; -MemoryListener listener;Error *error;bool initialized;bool dirty_pages_supported;
@@ -94,6 +96,8 @@ bool vfio_container_check_extension(VFIOContainer *container,int vfio_container_dma_map (VFIOContainer *container,hwaddr iova,ram_addr_t size,void *vaddr,bool readonly); +int vfio_container_dma_copy (VFIOContainer *src,VFIOContainer *dst, +hwaddr iova,ram_addr_t size,bool readonly);int vfio_container_dma_unmap (VFIOContainer *container,hwaddr iova,ram_addr_t size,IOMMUTLBEntry *iotlb);
@@ -132,6 +136,8 @@ struct VFIOIOMMUBackendOpsClass {int (*dma_map)(VFIOContainer *container,hwaddr iova,ram_addr_t size,void *vaddr,bool readonly); +int (*dma_copy)(VFIOContainer *src,VFIOContainer *dst, +hwaddr iova,ram_addr_t size,bool readonly);int (*dma_unmap)(VFIOContainer *container,hwaddr iova,ram_addr_t size,IOMMUTLBEntry *iotlb);
hw/vfio/as.c +101 -17
186 lines not on this page · raw message
hw/vfio/container-base.c +11 -2
@@ -47,6 +47,17 @@ int vfio_container_dma_map(VFIOContainer *container,return container->ops->dma_map (container, iova, size, vaddr, readonly); } +int vfio_container_dma_copy (VFIOContainer *src,VFIOContainer *dst, +hwaddr iova,ram_addr_t size,bool readonly) +{ +if (!src->ops->dma_copy || src->ops->dma_copy != dst->ops->dma_copy) { +error_report ("Incompatible container: unable to copy dma" ); +return -EINVAL ; + } + +return src->ops->dma_copy (src, dst, iova, size, readonly); +} +int vfio_container_dma_unmap (VFIOContainer *container,hwaddr iova,ram_addr_t size,IOMMUTLBEntry *iotlb)
@@ -137,8 +148,6 @@ void vfio_container_destroy(VFIOContainer *container)VFIOGuestIOMMU *giommu, *tmp;VFIOHostDMAWindow *hostwin, *next; -QLIST_SAFE_REMOVE (container, next); - QLIST_FOREACH_SAFE(vrdl, &container->vrdl_list, next, vrdl_tmp) {RamDiscardManager *rdm;
hw/vfio/container.c +7 -12
@@ -386,9 +386,6 @@ err_out:static void vfio_listener_release (VFIOLegacyContainer *container) { -VFIOContainer *bcontainer = &container->bcontainer; - -memory_listener_unregister (&bcontainer->listener);if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {memory_listener_unregister (&container->prereg_listener); }
@@ -929,14 +926,11 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,vfio_kvm_device_add_group (group);QLIST_INIT (&container->group_list); -QLIST_INSERT_HEAD (&space->containers, bcontainer, next); group->container = container;QLIST_INSERT_HEAD (&container->group_list, group, container_next); - bcontainer->listener = vfio_memory_listener; - -memory_listener_register (&bcontainer->listener, bcontainer->space->as); +vfio_as_add_container (space, bcontainer);if (bcontainer->error) { ret =-1 ;
@@ -949,8 +943,8 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,return 0 ; listener_release_exit: +vfio_as_del_container (space, bcontainer);QLIST_REMOVE (group, container_next); -QLIST_REMOVE (bcontainer, next);vfio_kvm_device_del_group (group);vfio_listener_release (container);
@@ -973,6 +967,7 @@ static void vfio_disconnect_container(VFIOGroup *group) {VFIOLegacyContainer *container = group->container;VFIOContainer *bcontainer = &container->bcontainer; +VFIOAddressSpace *space = bcontainer->space;QLIST_REMOVE (group, container_next); group->container =NULL ;
@@ -980,10 +975,12 @@ static void vfio_disconnect_container(VFIOGroup *group)/* * Explicitly release the listener first before unset container, * since unset may destroy the backend container if it's the last -* group. +* group. By removing container from the list, container is disconnected +* with address space memory listener. */ if (QLIST_EMPTY (&container->group_list)) {vfio_listener_release (container); +vfio_as_del_container (space, bcontainer); }if (ioctl (group->fd,VFIO_GROUP_UNSET_CONTAINER , &container->fd)) {
@@ -992,10 +989,8 @@ static void vfio_disconnect_container(VFIOGroup *group) }if (QLIST_EMPTY (&container->group_list)) { -VFIOAddressSpace *space = bcontainer->space; - -vfio_container_destroy (bcontainer);trace_vfio_disconnect_container (container->fd); +vfio_container_destroy (bcontainer);close (container->fd);g_free (container);
hw/vfio/iommufd.c +39 -9
98 lines not on this page · raw message
Signature · 1 line
2.37.3
RFC v3 18/18 vfio/as: Allow the selection of a given iommu backend
In reply to Eric AugerNow we support two types of iommu backends, let's add the capability to select one of them. This depends on whether an iommufd object has been linked with the vfio-pci device:
if the user wants to use the legacy backend, it shall not link the vfio-pci device with any iommufd object:
-device vfio-pci,host=0000:02:00.0
This is called the legacy mode/backend.
If the user wants to use the iommufd backend (/dev/iommu) it shall pass an iommufd object id in the vfio-pci device options:
-object iommufd,id=iommufd0
-device vfio-pci,host=0000:02:00.0,iommufd=iommufd0
Note the /dev/iommu device may have been pre-opened by a management tool such as libvirt. This mode is no more considered for the legacy backend. So let's remove the "TODO" comment.
--- hw/vfio/pci.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
hw/vfio/pci.c +14 -5
@@ -42,6 +42,7 @@#include "qapi/error.h" #include "migration/blocker.h" #include "migration/qemu-file.h" +#include "sysemu/iommufd.h" #define TYPE_VFIO_PCI_NOHOTPLUG "vfio-pci-nohotplug"
@@ -2852,6 +2853,13 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)int i, ret;bool is_mdev; +if (vbasedev->iommufd) { +iommufd_backend_connect (vbasedev->iommufd, errp); +if (*errp) { +return ; + } + } +if (!vbasedev->sysfsdev) {if (!(~vdev->host.domain || ~vdev->host.bus || ~vdev->host.slot || ~vdev->host.function)) {
@@ -3134,6 +3142,7 @@ error:static void vfio_instance_finalize (Object *obj) {VFIOPCIDevice *vdev =VFIO_PCI (obj); +VFIODevice *vbasedev = &vdev->vbasedev;vfio_display_finalize (vdev);vfio_bars_finalize (vdev);
@@ -3146,6 +3155,9 @@ static void vfio_instance_finalize(Object *obj)* * g_free(vdev->igd_opregion); */ +if (vbasedev->iommufd) { +iommufd_backend_disconnect (vbasedev->iommufd); + }vfio_put_device (vdev); }
@@ -3281,11 +3293,8 @@ static Property vfio_pci_dev_properties[] = { qdev_prop_nv_gpudirect_clique, uint8_t),DEFINE_PROP_OFF_AUTO_PCIBAR ("x-msix-relocation" , VFIOPCIDevice, msix_relo,OFF_AUTOPCIBAR_OFF ), -/* -* TODO - support passed fds... is this necessary? -* DEFINE_PROP_STRING("vfiofd", VFIOPCIDevice, vfiofd_name), -* DEFINE_PROP_STRING("vfiogroupfd, VFIOPCIDevice, vfiogroupfd_name), -*/ +DEFINE_PROP_LINK ("iommufd" , VFIOPCIDevice, vbasedev.iommufd, + TYPE_IOMMUFD_BACKEND, IOMMUFDBackend *), DEFINE_PROP_END_OF_LIST(), };
Signature · 1 line
2.37.3
Re: [RFC v3 18/18] vfio/as: Allow the selection of a given iommu backend
Quoted 14 lines · Eric Auger
Now we support two types of iommu backends, let's add the capability to select one of them. This depends on whether an iommufd object has been linked with the vfio-pci device:
if the user wants to use the legacy backend, it shall not link the vfio-pci device with any iommufd object:
-device vfio-pci,host=0000:02:00.0
This is called the legacy mode/backend.
If the user wants to use the iommufd backend (
/dev/iommu) it shall pass an iommufd object id in the vfio-pci device options:
Quoted 6 lines
-object iommufd,id=iommufd0
-device vfio-pci,host=0000:02:00.0,iommufd=iommufd0Note the
/dev/iommudevice may have been pre-opened by a management tool such as libvirt. This mode is no more considered for the legacy backend. So let's remove the "TODO" comment.
The vfio cdev should also be pre-openable like iommufd?
Jason
Re: [RFC v3 18/18] vfio/as: Allow the selection of a given iommu backend
Hi Jason,
Quoted 15 lines · Jason Gunthorpe
Quoted 14 lines · Eric Auger
Now we support two types of iommu backends, let's add the capability to select one of them. This depends on whether an iommufd object has been linked with the vfio-pci device:
if the user wants to use the legacy backend, it shall not link the vfio-pci device with any iommufd object:
-device vfio-pci,host=0000:02:00.0
This is called the legacy mode/backend.
If the user wants to use the iommufd backend (
/dev/iommu) it shall pass an iommufd object id in the vfio-pci device options:
Quoted 7 lines
Quoted 6 lines
-object iommufd,id=iommufd0
-device vfio-pci,host=0000:02:00.0,iommufd=iommufd0Note the
/dev/iommudevice may have been pre-opened by a management tool such as libvirt. This mode is no more considered for the legacy backend. So let's remove the "TODO" comment.The vfio cdev should also be pre-openable like iommufd?
where does the requirement come from?
Thanks
Eric
Quoted 3 lines
Jason
Re: [RFC v3 18/18] vfio/as: Allow the selection of a given iommu backend
Quoted 21 lines · Eric Auger
Hi Jason,
Quoted 18 lines · Jason Gunthorpe
Quoted 17 lines · Eric Auger
Now we support two types of iommu backends, let's add the capability to select one of them. This depends on whether an iommufd object has been linked with the vfio-pci device:
if the user wants to use the legacy backend, it shall not link the vfio-pci device with any iommufd object:
-device vfio-pci,host=0000:02:00.0
This is called the legacy mode/backend.
If the user wants to use the iommufd backend (
/dev/iommu) it shall pass an iommufd object id in the vfio-pci device options:-object iommufd,id=iommufd0
-device vfio-pci,host=0000:02:00.0,iommufd=iommufd0
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines
Note the
/dev/iommudevice may have been pre-opened by a management tool such as libvirt. This mode is no more considered for the legacy backend. So let's remove the "TODO" comment.The vfio cdev should also be pre-openable like iommufd?
where does the requirement come from?
I would expect it helps sandbox security.
We couldn't do this with the original VFIO model, but now we can have libvirt open the vfio with privilege and FD pass it to qemu.
This way qemu never needs to have privilege to open a VFIO or iommu cdev node.
Jason
Quoted 4 lines · Eric Auger
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
You said this was a qemu side limitation?
Quoted 1 line
- live migration
The vfio kernel interfaces are deprecated, Avihai's series here adds live migration support:
https://lore.kernel.org/qemu-devel/[email protected]/
And there will be another series for iommufd system iommu based live migration
Quoted 1 line
- vfio pci device hot reset
What is needed here?
Jason
Quoted 6 lines · Jason Gunthorpe
Quoted 4 lines · Eric Auger
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 trackingYou said this was a qemu side limitation?
yes that's correct. This comment will be removed.
Quoted 9 lines
Quoted 2 lines
- live migration
The vfio kernel interfaces are deprecated, Avihai's series here adds live migration support:
https://lore.kernel.org/qemu-devel/[email protected]/
And there will be another series for iommufd system iommu based live migration
OK thanks for the pointer.
Quoted 3 lines
Quoted 2 lines
- vfio pci device hot reset
What is needed here?
we need to revisit the vfio_pci_hot_reset() implementation in hw/vfio/pci.c It uses VFIO_DEVICE_GET_PCI_HOT_RESET_INFO and VFIO_DEVICE_PCI_HOT_RESET uapis
which retrieves/passes the list of iommu groups involved in the reset. The notion of group had initially disappeared from the the iommufd BE but I am afraid that's not that simple.
Thanks
Eric
Quoted 3 lines
Jason
Quoted 9 lines · Eric Auger
Quoted 2 lines
Quoted 1 line
- vfio pci device hot reset
What is needed here?
we need to revisit the
vfio_pci_hot_reset()implementation inhw/vfio/pci.cIt usesVFIO_DEVICE_GET_PCI_HOT_RESET_INFOandVFIO_DEVICE_PCI_HOT_RESETuapis
which retrieves/passes the list of iommu groups involved in the reset. The notion of group had initially disappeared from the the iommufd BE but I am afraid that's not that simple.
From the kernel side I expected that the device FD would be passed in place of the group FD and like in KVM we'd transform that into a group and run exactly the same algorithm. The kernel cdev series should be doing this.
So from the qemu side I would expect it to keep track of all the open VFIO device FD and pass them all into the host rest functions when working in cdev mode.
Jason