vfio: selftests: Support for multi-device tests
Subsystems (4)
David Matlack
maintainerv3 cover vfio: selftests: Support for multi-device tests
This series adds support for tests that use multiple devices, and adds one new test, vfio_pci_device_init_perf_test, which measures parallel device initialization time to demonstrate the improvement from commit e908f58b6beb ("vfio/pci: Separate SR-IOV VF dev_set").
This series also breaks apart the monolithic vfio_util.h and vfio_pci_device.c into separate files, to account for all the new code. This required quite a bit of code motion so the diffstat looks large. The final layout is more granular and provides a better separation of the IOMMU code from the device code.
Final layout:
C files:
- tools/testing/selftests/vfio/lib/libvfio.c
- tools/testing/selftests/vfio/lib/iommu.c
- tools/testing/selftests/vfio/lib/iova_allocator.c
- tools/testing/selftests/vfio/lib/vfio_pci_device.c
- tools/testing/selftests/vfio/lib/vfio_pci_driver.c
H files:
- tools/testing/selftests/vfio/lib/include/libvfio.h
- tools/testing/selftests/vfio/lib/include/libvfio/assert.h
- tools/testing/selftests/vfio/lib/include/libvfio/iommu.h
- tools/testing/selftests/vfio/lib/include/libvfio/iova_allocator.h
- tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h
- tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_driver.h
Notably, vfio_util.h is now gone and replaced with libvfio.h.
This series is based on vfio/next plus Alex Mastro's series to add the IOVA allocator [1]. It should apply cleanly to vfio/next once Alex's series is merged by Linus into the next 6.18 rc and then merged into vfio/next.
This series can be found on GitHub:
https://github.com/dmatlack/linux/tree/vfio/selftests/init_perf_test/v3
[1] https://lore.kernel.org/kvm/[email protected]/
v3:
- Replace literal with NSEC_PER_SEC (Alex Mastro)
- Fix Makefile accumulate vs. assignment (Alex Mastro)
v2: https://lore.kernel.org/kvm/[email protected]/
v1: https://lore.kernel.org/kvm/[email protected]/
David Matlack (18):
vfio: selftests: Move run.sh into scripts directory
vfio: selftests: Split run.sh into separate scripts
vfio: selftests: Allow passing multiple BDFs on the command line
vfio: selftests: Rename struct vfio_iommu_mode to iommu_mode
vfio: selftests: Introduce struct iommu
vfio: selftests: Support multiple devices in the same
container/iommufd
vfio: selftests: Eliminate overly chatty logging
vfio: selftests: Prefix logs with device BDF where relevant
vfio: selftests: Upgrade driver logging to dev_err()
vfio: selftests: Rename struct vfio_dma_region to dma_region
vfio: selftests: Move IOMMU library code into iommu.c
vfio: selftests: Move IOVA allocator into iova_allocator.c
vfio: selftests: Stop passing device for IOMMU operations
vfio: selftests: Rename vfio_util.h to libvfio.h
vfio: selftests: Move vfio_selftests_*() helpers into libvfio.c
vfio: selftests: Split libvfio.h into separate header files
vfio: selftests: Eliminate INVALID_IOVA
vfio: selftests: Add vfio_pci_device_init_perf_test
tools/testing/selftests/vfio/Makefile | 10 +- .../selftests/vfio/lib/drivers/dsa/dsa.c | 36 +- .../selftests/vfio/lib/drivers/ioat/ioat.c | 18 +- .../selftests/vfio/lib/include/libvfio.h | 26 + .../vfio/lib/include/libvfio/assert.h | 54 ++ .../vfio/lib/include/libvfio/iommu.h | 76 +++ .../vfio/lib/include/libvfio/iova_allocator.h | 23 + .../lib/include/libvfio/vfio_pci_device.h | 125 ++++ .../lib/include/libvfio/vfio_pci_driver.h | 97 +++ .../selftests/vfio/lib/include/vfio_util.h | 331 ----------- tools/testing/selftests/vfio/lib/iommu.c | 465 +++++++++++++++ .../selftests/vfio/lib/iova_allocator.c | 94 +++ tools/testing/selftests/vfio/lib/libvfio.c | 78 +++ tools/testing/selftests/vfio/lib/libvfio.mk | 5 +- .../selftests/vfio/lib/vfio_pci_device.c | 555 +----------------- .../selftests/vfio/lib/vfio_pci_driver.c | 16 +- tools/testing/selftests/vfio/run.sh | 109 ---- .../testing/selftests/vfio/scripts/cleanup.sh | 41 ++ tools/testing/selftests/vfio/scripts/lib.sh | 42 ++ tools/testing/selftests/vfio/scripts/run.sh | 16 + tools/testing/selftests/vfio/scripts/setup.sh | 48 ++ .../selftests/vfio/vfio_dma_mapping_test.c | 46 +- .../selftests/vfio/vfio_iommufd_setup_test.c | 2 +- .../vfio/vfio_pci_device_init_perf_test.c | 168 ++++++ .../selftests/vfio/vfio_pci_device_test.c | 12 +- .../selftests/vfio/vfio_pci_driver_test.c | 51 +- 26 files changed, 1481 insertions(+), 1063 deletions(-) create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio.h create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/assert.h create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/iommu.h create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/iova_allocator.h create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_driver.h delete mode 100644 tools/testing/selftests/vfio/lib/include/vfio_util.h create mode 100644 tools/testing/selftests/vfio/lib/iommu.c create mode 100644 tools/testing/selftests/vfio/lib/iova_allocator.c create mode 100644 tools/testing/selftests/vfio/lib/libvfio.c delete mode 100755 tools/testing/selftests/vfio/run.sh create mode 100755 tools/testing/selftests/vfio/scripts/cleanup.sh create mode 100755 tools/testing/selftests/vfio/scripts/lib.sh create mode 100755 tools/testing/selftests/vfio/scripts/run.sh create mode 100755 tools/testing/selftests/vfio/scripts/setup.sh create mode 100644 tools/testing/selftests/vfio/vfio_pci_device_init_perf_test.c
base-commit: fa804aa4ac1b091ef2ec2981f08a1c28aaeba8e7
prerequisite-patch-id: dcf23dcc1198960bda3102eefaa21df60b2e4c54
prerequisite-patch-id: e32e56d5bf7b6c7dd40d737aa3521560407e00f5
prerequisite-patch-id: 4f79a41bf10a4c025ba5f433551b46035aa15878
prerequisite-patch-id: f903a45f0c32319138cd93a007646ab89132b18c
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerPATCH v3 1/18 vfio: selftests: Move run.sh into scripts directory
Move run.sh in a new sub-directory scripts/. This directory will be used to house various helper scripts to be used by humans and automation for running VFIO selftests.
Opportunistically also switch run.sh from TEST_PROGS_EXTENDED to TEST_FILES. The former is for actual test executables that are just not run by default. TEST_FILES is a better fit for helper scripts.
No functional change intended.
---
tools/testing/selftests/vfio/Makefile | 4 +++-
tools/testing/selftests/vfio/{ => scripts}/run.sh | 0
2 files changed, 3 insertions(+), 1 deletion(-)
rename tools/testing/selftests/vfio/{ => scripts}/run.sh (100%)tools/testing/selftests/vfio/Makefile +3 -1
@@ -3,7 +3,9 @@ TEST_GEN_PROGS += vfio_dma_mapping_test TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test TEST_GEN_PROGS += vfio_pci_driver_test -TEST_PROGS_EXTENDED := run.sh + +TEST_FILES += scripts/run.sh + include ../lib.mk include lib/libvfio.mk
tools/testing/selftests/vfio/scripts/run.sh renamed from tools/testing/selftests/vfio/run.sh +0 -0
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerPATCH v3 2/18 vfio: selftests: Split run.sh into separate scripts
In reply to David MatlackSplit run.sh into separate scripts (setup.sh, run.sh, cleanup.sh) to enable multi-device testing, and prepare for VFIO selftests automatically detecting which devices to use for testing by storing device metadata on the filesystem.
- setup.sh takes one or more BDFs as arguments and sets up each device. Metadata about each device is stored on the filesystem in the directory:
${TMPDIR:-/tmp}/vfio-selftests-devices Within this directory is a directory for each BDF, and then files in those directories that cleanup.sh uses to cleanup the device.
- run.sh runs a selftest by passing it the BDFs of all set up devices.
- cleanup.sh takes zero or more BDFs as arguments and cleans up each device. If no BDFs are provided, it cleans up all devices.
This split enables multi-device testing by allowing multiple BDFs to be set up and passed into tests:
For example:
$ tools/testing/selftests/vfio/scripts/setup.sh <BDF1> <BDF2> $ tools/testing/selftests/vfio/scripts/setup.sh <BDF3> $ tools/testing/selftests/vfio/scripts/run.sh echo <BDF1> <BDF2> <BDF3> $ tools/testing/selftests/vfio/scripts/cleanup.sh
In the future, VFIO selftests can automatically detect set up devices by inspecting ${TMPDIR:-/tmp}/vfio-selftests-devices. This will avoid the need for the run.sh script.
--- tools/testing/selftests/vfio/Makefile | 3 + .../testing/selftests/vfio/scripts/cleanup.sh | 41 +++++++ tools/testing/selftests/vfio/scripts/lib.sh | 42 +++++++ tools/testing/selftests/vfio/scripts/run.sh | 105 +----------------- tools/testing/selftests/vfio/scripts/setup.sh | 48 ++++++++ 5 files changed, 140 insertions(+), 99 deletions(-) create mode 100755 tools/testing/selftests/vfio/scripts/cleanup.sh create mode 100755 tools/testing/selftests/vfio/scripts/lib.sh create mode 100755 tools/testing/selftests/vfio/scripts/setup.sh
tools/testing/selftests/vfio/Makefile +3 -0
@@ -4,7 +4,10 @@ TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test TEST_GEN_PROGS += vfio_pci_driver_test +TEST_FILES += scripts/cleanup.sh +TEST_FILES += scripts/lib.sh TEST_FILES += scripts/run.sh +TEST_FILES += scripts/setup.sh include ../lib.mk include lib/libvfio.mk
tools/testing/selftests/vfio/scripts/cleanup.sh new file +41 -0
41 lines not on this page · raw message
tools/testing/selftests/vfio/scripts/lib.sh new file +42 -0
42 lines not on this page · raw message
tools/testing/selftests/vfio/scripts/run.sh +6 -99
115 lines not on this page · raw message
tools/testing/selftests/vfio/scripts/setup.sh new file +48 -0
48 lines not on this page · raw message
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerPATCH v3 3/18 vfio: selftests: Allow passing multiple BDFs on the command line
In reply to David MatlackAdd support for passing multiple device BDFs to a test via the command line. This is a prerequisite for multi-device tests.
Single-device tests can continue using vfio_selftests_get_bdf(), which will continue to return argv[argc - 1] (if it is a BDF string), or the environment variable $VFIO_SELFTESTS_BDF otherwise.
For multi-device tests, a new helper called vfio_selftests_get_bdfs() is introduced which will return an array of all BDFs found at the end of argv[], as well as the number of BDFs found (passed back to the caller via argument). The array of BDFs returned does not need to be freed by the caller.
The environment variable VFIO_SELFTESTS_BDF continues to support only a single BDF for the time being.
--- .../selftests/vfio/lib/include/vfio_util.h | 2 + .../selftests/vfio/lib/vfio_pci_device.c | 57 +++++++++++++++---- 2 files changed, 48 insertions(+), 11 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +2 -0
@@ -208,6 +208,8 @@ struct iova_allocator {* If BDF cannot be determined then the test will exit with KSFT_SKIP. */ const char *vfio_selftests_get_bdf (int *argc,char *argv[]); +char **vfio_selftests_get_bdfs (int *argc,char *argv[],int *nr_bdfs); +const char *vfio_pci_get_cdev_path (const char *bdf);extern const char *default_iommu_mode;
tools/testing/selftests/vfio/lib/vfio_pci_device.c +46 -11
75 lines not on this page · raw message
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerPATCH v3 4/18 vfio: selftests: Rename struct vfio_iommu_mode to iommu_mode
In reply to David MatlackRename struct vfio_iommu_mode to struct iommu_mode since the mode can include iommufd. This also prepares for splitting out all the IOMMU code into its own structs/helpers/files which are independent from the vfio_pci_device code.
No function change intended.
--- tools/testing/selftests/vfio/lib/include/vfio_util.h | 4 ++-- tools/testing/selftests/vfio/lib/vfio_pci_device.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +2 -2
@@ -50,7 +50,7 @@ VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__); \ }while (0 ) -struct vfio_iommu_mode { +struct iommu_mode {const char *name;const char *container_path;unsigned long iommu_type;
@@ -166,7 +166,7 @@ struct vfio_pci_driver {struct vfio_pci_device {int fd; -const struct vfio_iommu_mode *iommu_mode; +const struct iommu_mode *iommu_mode;int group_fd;int container_fd;
tools/testing/selftests/vfio/lib/vfio_pci_device.c +2 -2
@@ -713,7 +713,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) }/* Reminder: Keep in sync with FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(). */ -static const struct vfio_iommu_mode iommu_modes[] = { +static const struct iommu_mode iommu_modes[] = { { .name ="vfio_type1_iommu" , .container_path ="/dev/vfio/vfio" ,
@@ -741,7 +741,7 @@ static const struct vfio_iommu_mode iommu_modes[] = {const char *default_iommu_mode ="iommufd" ; -static const struct vfio_iommu_mode *lookup_iommu_mode (const char *iommu_mode) +static const struct iommu_mode *lookup_iommu_mode (const char *iommu_mode) {int i;
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerPATCH v3 5/18 vfio: selftests: Introduce struct iommu
In reply to David MatlackIntroduce struct iommu, which logically represents either a VFIO container or an iommufd IOAS, depending on which IOMMU mode is used by the test.
This will be used in a subsequent commit to allow devices to be added to the same container/iommufd.
--- .../selftests/vfio/lib/include/vfio_util.h | 16 ++-- .../selftests/vfio/lib/vfio_pci_device.c | 75 ++++++++++--------- 2 files changed, 49 insertions(+), 42 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +9 -7
@@ -163,15 +163,19 @@ struct vfio_pci_driver {int msi; }; +struct iommu { +const struct iommu_mode *mode; +int container_fd; +int iommufd; +u32 ioas_id; +struct list_head dma_regions; +}; +struct vfio_pci_device {int fd; - -const struct iommu_mode *iommu_mode;int group_fd; -int container_fd; -int iommufd; -u32 ioas_id; +struct iommu *iommu;struct vfio_device_info info;struct vfio_region_info config_space;
@@ -180,8 +184,6 @@ struct vfio_pci_device {struct vfio_irq_info msi_info;struct vfio_irq_info msix_info; -struct list_head dma_regions; -/* eventfds for MSI and MSI-x interrupts */ int msi_eventfds[PCI_MSIX_FLAGS_QSIZE +1 ];
tools/testing/selftests/vfio/lib/vfio_pci_device.c +40 -35
172 lines not on this page · raw message
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerPATCH v3 6/18 vfio: selftests: Support multiple devices in the same container/iommufd
In reply to David MatlackSupport tests that want to add multiple devices to the same container/iommufd by decoupling struct vfio_pci_device from struct iommu.
Multi-devices tests can now put multiple devices in the same container/iommufd like so:
iommu = iommu_init(iommu_mode); device1 = vfio_pci_device_init(bdf1, iommu); device2 = vfio_pci_device_init(bdf2, iommu); device3 = vfio_pci_device_init(bdf3, iommu);
...
vfio_pci_device_cleanup(device3); vfio_pci_device_cleanup(device2); vfio_pci_device_cleanup(device1); iommu_cleanup(iommu);
To account for the new separation of vfio_pci_device and iommu, update existing tests to initialize and cleanup a struct iommu.
--- .../selftests/vfio/lib/include/vfio_util.h | 6 +- .../selftests/vfio/lib/vfio_pci_device.c | 102 +++++++++++------- .../selftests/vfio/vfio_dma_mapping_test.c | 10 +- .../selftests/vfio/vfio_pci_device_test.c | 10 +- .../selftests/vfio/vfio_pci_driver_test.c | 26 ++++- 5 files changed, 105 insertions(+), 49 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +5 -1
@@ -216,8 +216,12 @@ const char *vfio_pci_get_cdev_path(const char *bdf);extern const char *default_iommu_mode; -struct vfio_pci_device *vfio_pci_device_init (const char *bdf,const char *iommu_mode); +struct iommu *iommu_init (const char *iommu_mode); +void iommu_cleanup (struct iommu *iommu); + +struct vfio_pci_device *vfio_pci_device_init (const char *bdf,struct iommu *iommu);void vfio_pci_device_cleanup (struct vfio_pci_device *device); +void vfio_pci_device_reset (struct vfio_pci_device *device); struct iommu_iova_range *vfio_pci_iova_ranges(struct vfio_pci_device *device,
tools/testing/selftests/vfio/lib/vfio_pci_device.c +63 -39
172 lines not on this page · raw message
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +8 -2
@@ -94,6 +94,7 @@ static int iommu_mapping_get(const char *bdf, u64 iova, }FIXTURE (vfio_dma_mapping_test) { +struct iommu *iommu;struct vfio_pci_device *device;struct iova_allocator *iova_allocator; };
@@ -119,7 +120,8 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB |FIXTURE_SETUP (vfio_dma_mapping_test) { - self->device =vfio_pci_device_init (device_bdf, variant->iommu_mode); + self->iommu =iommu_init (variant->iommu_mode); + self->device =vfio_pci_device_init (device_bdf, self->iommu); self->iova_allocator =iova_allocator_init (self->device); }
@@ -127,6 +129,7 @@ FIXTURE_TEARDOWN(vfio_dma_mapping_test) {iova_allocator_cleanup (self->iova_allocator);vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu); }TEST_F (vfio_dma_mapping_test, dma_map_unmap)
@@ -203,6 +206,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap) }FIXTURE (vfio_dma_map_limit_test) { +struct iommu *iommu;struct vfio_pci_device *device;struct vfio_dma_region region;size_t mmap_size;
@@ -235,7 +239,8 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)*/ self->mmap_size =2 * region_size; - self->device =vfio_pci_device_init (device_bdf, variant->iommu_mode); + self->iommu =iommu_init (variant->iommu_mode); + self->device =vfio_pci_device_init (device_bdf, self->iommu); region->vaddr =mmap (NULL , self->mmap_size,PROT_READ |PROT_WRITE ,MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );ASSERT_NE (region->vaddr,MAP_FAILED );
@@ -253,6 +258,7 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)FIXTURE_TEARDOWN (vfio_dma_map_limit_test) {vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu);ASSERT_EQ (munmap (self->region.vaddr, self->mmap_size),0 ); }
tools/testing/selftests/vfio/vfio_pci_device_test.c +8 -2
@@ -23,17 +23,20 @@ static const char *device_bdf;#define MAX_TEST_MSI 16UFIXTURE (vfio_pci_device_test) { +struct iommu *iommu;struct vfio_pci_device *device; };FIXTURE_SETUP (vfio_pci_device_test) { - self->device =vfio_pci_device_init (device_bdf, default_iommu_mode); + self->iommu =iommu_init (default_iommu_mode); + self->device =vfio_pci_device_init (device_bdf, self->iommu); }FIXTURE_TEARDOWN (vfio_pci_device_test) {vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu); }#define read_pci_id_from_sysfs (_file) ({ \
@@ -99,6 +102,7 @@ TEST_F(vfio_pci_device_test, validate_bars) }FIXTURE (vfio_pci_irq_test) { +struct iommu *iommu;struct vfio_pci_device *device; };
@@ -116,12 +120,14 @@ FIXTURE_VARIANT_ADD(vfio_pci_irq_test, msix) {FIXTURE_SETUP (vfio_pci_irq_test) { - self->device =vfio_pci_device_init (device_bdf, default_iommu_mode); + self->iommu =iommu_init (default_iommu_mode); + self->device =vfio_pci_device_init (device_bdf, self->iommu); }FIXTURE_TEARDOWN (vfio_pci_irq_test) {vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu); }TEST_F (vfio_pci_irq_test, enable_trigger_disable)
tools/testing/selftests/vfio/vfio_pci_driver_test.c +21 -5
@@ -44,6 +44,7 @@ static void region_teardown(struct vfio_pci_device *device, }FIXTURE (vfio_pci_driver_test) { +struct iommu *iommu;struct vfio_pci_device *device;struct iova_allocator *iova_allocator;struct vfio_dma_region memcpy_region;
@@ -73,7 +74,8 @@ FIXTURE_SETUP(vfio_pci_driver_test) {struct vfio_pci_driver *driver; - self->device =vfio_pci_device_init (device_bdf, variant->iommu_mode); + self->iommu =iommu_init (variant->iommu_mode); + self->device =vfio_pci_device_init (device_bdf, self->iommu); self->iova_allocator =iova_allocator_init (self->device); driver = &self->device->driver;
@@ -113,6 +115,7 @@ FIXTURE_TEARDOWN(vfio_pci_driver_test)iova_allocator_cleanup (self->iova_allocator);vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu); }TEST_F (vfio_pci_driver_test, init_remove)
@@ -231,18 +234,31 @@ TEST_F_TIMEOUT(vfio_pci_driver_test, memcpy_storm, 60)ASSERT_NO_MSI (self->msi_fd); } -int main (int argc,char *argv[]) +static bool device_has_selftests_driver (const char *bdf) {struct vfio_pci_device *device; +struct iommu *iommu; +bool has_driver; + + iommu =iommu_init (default_iommu_mode); + device =vfio_pci_device_init (device_bdf, iommu); + + has_driver = !!device->driver.ops; + +vfio_pci_device_cleanup (device); +iommu_cleanup (iommu); +return has_driver; +} + +int main (int argc,char *argv[]) +{ device_bdf =vfio_selftests_get_bdf (&argc, argv); - device =vfio_pci_device_init (device_bdf, default_iommu_mode); -if (!device->driver.ops) { +if (!device_has_selftests_driver (device_bdf)) {fprintf (stderr,"No driver found for device %s\n" , device_bdf);return KSFT_SKIP ; } -vfio_pci_device_cleanup (device);return test_harness_run (argc, argv); }
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Re: [PATCH v3 06/18] vfio: selftests: Support multiple devices in the same container/iommufd
Quoted 27 lines · David Matlack
Support tests that want to add multiple devices to the same container/iommufd by decoupling struct
vfio_pci_devicefrom struct iommu.Multi-devices tests can now put multiple devices in the same container/iommufd like so:
iommu = iommu_init(iommu_mode); device1 = vfio_pci_device_init(bdf1, iommu); device2 = vfio_pci_device_init(bdf2, iommu); device3 = vfio_pci_device_init(bdf3, iommu);...
vfio_pci_device_cleanup(device3); vfio_pci_device_cleanup(device2); vfio_pci_device_cleanup(device1); iommu_cleanup(iommu);tools/testing/selftests/vfio/lib/vfio_pci_device.c +1
-0+struct vfio_pci_device *vfio_pci_device_init (const char *bdf,struct iommu *iommu) {struct vfio_pci_device *device;
tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *b · line 844
Quoted 10 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +1
-6device =calloc (1 ,sizeof (*device));VFIO_ASSERT_NOT_NULL (device); - device->iommu =calloc (1 ,sizeof (*device->iommu)); -VFIO_ASSERT_NOT_NULL (device->iommu); - -INIT_LIST_HEAD (&device->iommu->dma_regions); - - device->iommu->mode =lookup_iommu_mode (iommu_mode); + device->iommu = iommu;
nit: Since we now depend on the caller to follow the right order, should we have a VFIO_ASSERT_NOT_NULL(iommu), or something along the lines of 'Is iommu initialized?" before this function starts using it, and fail with an appropriate error message?
Quoted 2 lines
if (device->iommu->mode->container_path)minor nit: if there's a v4, simply use iommu->mode->container_path.
Thank you.
Raghavendra
Quoted 205 lines
vfio_pci_container_setup(device, bdf);tools/testing/selftests/vfio/lib/vfio_pci_device.c +11
-6@@ -853,17 +872,22 @@ void vfio_pci_device_cleanup(struct vfio_pci_device *device)VFIO_ASSERT_EQ (close (device->msi_eventfds[i]),0 ); } -if (device->iommu->iommufd) { -VFIO_ASSERT_EQ (close (device->iommu->iommufd),0 ); - }else { +if (device->group_fd)VFIO_ASSERT_EQ (close (device->group_fd),0 ); -VFIO_ASSERT_EQ (close (device->iommu->container_fd),0 ); - } -free (device->iommu);free (device); } +void iommu_cleanup (struct iommu *iommu) +{ +if (iommu->iommufd) +VFIO_ASSERT_EQ (close (iommu->iommufd),0 ); +else +VFIO_ASSERT_EQ (close (iommu->container_fd),0 ); + +free (iommu); +} +static bool is_bdf (const char *str) {unsigned int s, b, d, f;tools/testing/selftests/vfio/vfio_dma_mapping_test.c +8
-2@@ -94,6 +94,7 @@ static int iommu_mapping_get(const char *bdf, u64 iova, }FIXTURE (vfio_dma_mapping_test) { +struct iommu *iommu;struct vfio_pci_device *device;struct iova_allocator *iova_allocator; };@@ -119,7 +120,8 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB |FIXTURE_SETUP (vfio_dma_mapping_test) { - self->device =vfio_pci_device_init (device_bdf, variant->iommu_mode); + self->iommu =iommu_init (variant->iommu_mode); + self->device =vfio_pci_device_init (device_bdf, self->iommu); self->iova_allocator =iova_allocator_init (self->device); }@@ -127,6 +129,7 @@ FIXTURE_TEARDOWN(vfio_dma_mapping_test) {iova_allocator_cleanup (self->iova_allocator);vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu); }TEST_F (vfio_dma_mapping_test, dma_map_unmap)@@ -203,6 +206,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap) }FIXTURE (vfio_dma_map_limit_test) { +struct iommu *iommu;struct vfio_pci_device *device;struct vfio_dma_region region;size_t mmap_size;@@ -235,7 +239,8 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)*/ self->mmap_size =2 * region_size; - self->device =vfio_pci_device_init (device_bdf, variant->iommu_mode); + self->iommu =iommu_init (variant->iommu_mode); + self->device =vfio_pci_device_init (device_bdf, self->iommu); region->vaddr =mmap (NULL , self->mmap_size,PROT_READ |PROT_WRITE ,MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );ASSERT_NE (region->vaddr,MAP_FAILED );@@ -253,6 +258,7 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)FIXTURE_TEARDOWN (vfio_dma_map_limit_test) {vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu);ASSERT_EQ (munmap (self->region.vaddr, self->mmap_size),0 ); }tools/testing/selftests/vfio/vfio_pci_device_test.c +8
-2@@ -23,17 +23,20 @@ static const char *device_bdf;#define MAX_TEST_MSI 16UFIXTURE (vfio_pci_device_test) { +struct iommu *iommu;struct vfio_pci_device *device; };FIXTURE_SETUP (vfio_pci_device_test) { - self->device =vfio_pci_device_init (device_bdf, default_iommu_mode); + self->iommu =iommu_init (default_iommu_mode); + self->device =vfio_pci_device_init (device_bdf, self->iommu); }FIXTURE_TEARDOWN (vfio_pci_device_test) {vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu); }#define read_pci_id_from_sysfs (_file) ({ \@@ -99,6 +102,7 @@ TEST_F(vfio_pci_device_test, validate_bars) }FIXTURE (vfio_pci_irq_test) { +struct iommu *iommu;struct vfio_pci_device *device; };@@ -116,12 +120,14 @@ FIXTURE_VARIANT_ADD(vfio_pci_irq_test, msix) {FIXTURE_SETUP (vfio_pci_irq_test) { - self->device =vfio_pci_device_init (device_bdf, default_iommu_mode); + self->iommu =iommu_init (default_iommu_mode); + self->device =vfio_pci_device_init (device_bdf, self->iommu); }FIXTURE_TEARDOWN (vfio_pci_irq_test) {vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu); }TEST_F (vfio_pci_irq_test, enable_trigger_disable)tools/testing/selftests/vfio/vfio_pci_driver_test.c +21
-6@@ -44,6 +44,7 @@ static void region_teardown(struct vfio_pci_device *device, }FIXTURE (vfio_pci_driver_test) { +struct iommu *iommu;struct vfio_pci_device *device;struct iova_allocator *iova_allocator;struct vfio_dma_region memcpy_region;@@ -73,7 +74,8 @@ FIXTURE_SETUP(vfio_pci_driver_test) {struct vfio_pci_driver *driver; - self->device =vfio_pci_device_init (device_bdf, variant->iommu_mode); + self->iommu =iommu_init (variant->iommu_mode); + self->device =vfio_pci_device_init (device_bdf, self->iommu); self->iova_allocator =iova_allocator_init (self->device); driver = &self->device->driver;@@ -113,6 +115,7 @@ FIXTURE_TEARDOWN(vfio_pci_driver_test)iova_allocator_cleanup (self->iova_allocator);vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu); }TEST_F (vfio_pci_driver_test, init_remove)@@ -231,18 +234,31 @@ TEST_F_TIMEOUT(vfio_pci_driver_test, memcpy_storm, 60)ASSERT_NO_MSI (self->msi_fd); } -int main (int argc,char *argv[]) +static bool device_has_selftests_driver (const char *bdf) {struct vfio_pci_device *device; +struct iommu *iommu; +bool has_driver; + + iommu =iommu_init (default_iommu_mode); + device =vfio_pci_device_init (device_bdf, iommu); + + has_driver = !!device->driver.ops; + +vfio_pci_device_cleanup (device); +iommu_cleanup (iommu); +return has_driver; +} + +int main (int argc,char *argv[]) +{ device_bdf =vfio_selftests_get_bdf (&argc, argv); - device =vfio_pci_device_init (device_bdf, default_iommu_mode); -if (!device->driver.ops) { +if (!device_has_selftests_driver (device_bdf)) {fprintf (stderr,"No driver found for device %s\n" , device_bdf);return KSFT_SKIP ; } -vfio_pci_device_cleanup (device);return test_harness_run (argc, argv); } --2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerRe: [PATCH v3 06/18] vfio: selftests: Support multiple devices in the same container/iommufd
Quoted 8 lines · Raghavendra Rao Ananta
Quoted 7 lines · David Matlack
+struct
vfio_pci_device*vfio_pci_device_init(const char *bdf, struct iommu *iommu){ struct vfio_pci_device *device; device = calloc(1, sizeof(*device)); VFIO_ASSERT_NOT_NULL(device);
Quoted 11 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *b · line 844
Quoted 7 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +1
-6- device->iommu =calloc (1 ,sizeof (*device->iommu)); -VFIO_ASSERT_NOT_NULL (device->iommu); - -INIT_LIST_HEAD (&device->iommu->dma_regions); - - device->iommu->mode =lookup_iommu_mode (iommu_mode); + device->iommu = iommu;nit: Since we now depend on the caller to follow the right order, should we have a
VFIO_ASSERT_NOT_NULL(iommu), or something along the lines of 'Is iommu initialized?" before this function starts using it, and fail with an appropriate error message?
I think the compiler and type system largely enforce the order now. The compiler will complain if a user passes in an uninitialized struct iommu *. And the only way to initialize it is with iommu_init(). I guess someone could pass in NULL, so having an explicit assert for non-null would be easier to debug than a SIGSEGV. I'll add that in v4.
Quoted 4 lines
Quoted 3 lines
if (device->iommu->mode->container_path)minor nit: if there's a v4, simply use
iommu->mode->container_path.
Yes, thanks, will do!
David Matlack
maintainerPATCH v3 7/18 vfio: selftests: Eliminate overly chatty logging
In reply to David MatlackEliminate overly chatty logs that are printed during almost every test. These logs are adding more noise than value. If a test cares about this information it can log it itself. This is especially true as the VFIO selftests gains support for multiple devices in a single test (which multiplies all these logs).
--- tools/testing/selftests/vfio/lib/vfio_pci_driver.c | 14 -------------- 1 file changed, 14 deletions(-)
tools/testing/selftests/vfio/lib/vfio_pci_driver.c +0 -14
@@ -1,6 +1,4 @@// SPDX-License-Identifier: GPL-2.0-only -#include <stdio.h> -#include "../../../kselftest.h" #include <vfio_util.h>
@@ -29,7 +27,6 @@ void vfio_pci_driver_probe(struct vfio_pci_device *device)if (ops->probe (device))continue ; -printf ("Driver found: %s\n" , ops->name); device->driver.ops = ops; } }
@@ -58,17 +55,6 @@ void vfio_pci_driver_init(struct vfio_pci_device *device) driver->ops->init (device); driver->initialized = true; - -printf ("%s: region: vaddr %p, iova 0x%lx, size 0x%lx\n" , - driver->ops->name, - driver->region.vaddr, - driver->region.iova, - driver->region.size); - -printf ("%s: max_memcpy_size 0x%lx, max_memcpy_count 0x%lx\n" , - driver->ops->name, - driver->max_memcpy_size, - driver->max_memcpy_count); }void vfio_pci_driver_remove (struct vfio_pci_device *device)
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
kernel test robot
build botRe: [PATCH v3 07/18] vfio: selftests: Eliminate overly chatty logging
kernel test robot noticed the following build errors:
Hi David,
kernel test robot noticed the following build errors:
[auto build test ERROR on fa804aa4ac1b091ef2ec2981f08a1c28aaeba8e7]
url: https://github.com/intel-lab-lkp/linux/commits/David-Matlack/vfio-selftests-Move-run-sh-into-scripts-directory/20251122-022109 base: fa804aa4ac1b091ef2ec2981f08a1c28aaeba8e7 patch link: https://lore.kernel.org/r/20251121181429.1421717-8-dmatlack%40google.com patch subject: [PATCH v3 07/18] vfio: selftests: Eliminate overly chatty logging config: riscv-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20251122/[email protected]/config) compiler: riscv64-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251122/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/
All errors (new ones prefixed by >>):
In file included from tools/testing/selftests/vfio/lib/vfio_pci_driver.c:2:
>> tools/testing/selftests/vfio/lib/include/../../../kselftest.h:53:10: fatal error: errno.h: No such file or directory
53 | #include <errno.h>
| ^~~~~~~~~compilation terminated.
vim +53 tools/testing/selftests/vfio/lib/include/../../../kselftest.h
7fb2c3ea28bf7b7 Shuah Khan 2014-10-03 51 9e38be732da8fe3 Mark Brown 2023-04-06 52 #ifndef NOLIBC fc2e634e997d84f Aleksa Sarai 2019-07-20 @53 #include <errno.h> 7fb2c3ea28bf7b7 Shuah Khan 2014-10-03 54 #include <stdlib.h> 7fb2c3ea28bf7b7 Shuah Khan 2014-10-03 55 #include <unistd.h> 151b2732111f074 Paul Elder 2017-06-28 56 #include <stdarg.h> f2662ec26b26adb André Almeida 2025-09-17 57 #include <stdbool.h> 907f33028871fa7 Mark Brown 2023-09-28 58 #include <string.h> a18261d71bbc625 Tycho Andersen 2019-01-18 59 #include <stdio.h> 6d029c25b71f2de Oleg Nesterov 2024-04-09 60 #include <sys/utsname.h> 9e38be732da8fe3 Mark Brown 2023-04-06 61 #endif 7fb2c3ea28bf7b7 Shuah Khan 2014-10-03 62
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
David Matlack
maintainerPATCH v3 8/18 vfio: selftests: Prefix logs with device BDF where relevant
In reply to David MatlackPrefix log messages with the device's BDF where relevant. This will help understanding VFIO selftests logs when tests are run with multiple devices.
--- .../selftests/vfio/lib/drivers/dsa/dsa.c | 34 +++++++++---------- .../selftests/vfio/lib/drivers/ioat/ioat.c | 16 ++++----- .../selftests/vfio/lib/include/vfio_util.h | 4 +++ .../selftests/vfio/lib/vfio_pci_device.c | 1 + 4 files changed, 30 insertions(+), 25 deletions(-)
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c +17 -17
@@ -70,7 +70,7 @@ static int dsa_probe(struct vfio_pci_device *device)return -EINVAL ;if (dsa_int_handle_request_required (device)) { -printf ("Device requires requesting interrupt handles\n" ); +dev_info (device,"Device requires requesting interrupt handles\n" );return -EINVAL ; }
@@ -91,23 +91,23 @@ static void dsa_check_sw_err(struct vfio_pci_device *device)return ; } -fprintf (stderr,"SWERR: 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n" , +dev_err (device,"SWERR: 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n" , err.bits[0 ], err.bits[1 ], err.bits[2 ], err.bits[3 ]); -fprintf (stderr," valid: 0x%x\n" , err.valid); -fprintf (stderr," overflow: 0x%x\n" , err.overflow); -fprintf (stderr," desc_valid: 0x%x\n" , err.desc_valid); -fprintf (stderr," wq_idx_valid: 0x%x\n" , err.wq_idx_valid); -fprintf (stderr," batch: 0x%x\n" , err.batch); -fprintf (stderr," fault_rw: 0x%x\n" , err.fault_rw); -fprintf (stderr," priv: 0x%x\n" , err.priv); -fprintf (stderr," error: 0x%x\n" , err.error); -fprintf (stderr," wq_idx: 0x%x\n" , err.wq_idx); -fprintf (stderr," operation: 0x%x\n" , err.operation); -fprintf (stderr," pasid: 0x%x\n" , err.pasid); -fprintf (stderr," batch_idx: 0x%x\n" , err.batch_idx); -fprintf (stderr," invalid_flags: 0x%x\n" , err.invalid_flags); -fprintf (stderr," fault_addr: 0x%lx\n" , err.fault_addr); +dev_err (device," valid: 0x%x\n" , err.valid); +dev_err (device," overflow: 0x%x\n" , err.overflow); +dev_err (device," desc_valid: 0x%x\n" , err.desc_valid); +dev_err (device," wq_idx_valid: 0x%x\n" , err.wq_idx_valid); +dev_err (device," batch: 0x%x\n" , err.batch); +dev_err (device," fault_rw: 0x%x\n" , err.fault_rw); +dev_err (device," priv: 0x%x\n" , err.priv); +dev_err (device," error: 0x%x\n" , err.error); +dev_err (device," wq_idx: 0x%x\n" , err.wq_idx); +dev_err (device," operation: 0x%x\n" , err.operation); +dev_err (device," pasid: 0x%x\n" , err.pasid); +dev_err (device," batch_idx: 0x%x\n" , err.batch_idx); +dev_err (device," invalid_flags: 0x%x\n" , err.invalid_flags); +dev_err (device," fault_addr: 0x%lx\n" , err.fault_addr);VFIO_FAIL ("Software Error Detected!\n" ); }
@@ -256,7 +256,7 @@ static int dsa_completion_wait(struct vfio_pci_device *device,if (status ==DSA_COMP_SUCCESS )return 0 ; -printf ("Error detected during memcpy operation: 0x%x\n" , status); +dev_info (device,"Error detected during memcpy operation: 0x%x\n" , status);return -1 ; }
tools/testing/selftests/vfio/lib/drivers/ioat/ioat.c +8 -8
@@ -51,7 +51,7 @@ static int ioat_probe(struct vfio_pci_device *device) r =0 ;break ;default : -printf ("ioat: Unsupported version: 0x%x\n" , version); +dev_info (device,"ioat: Unsupported version: 0x%x\n" , version); r = -EINVAL ; }return r;
@@ -135,13 +135,13 @@ static void ioat_handle_error(struct vfio_pci_device *device) {void *registers =ioat_channel_registers (device); -printf ("Error detected during memcpy operation!\n" -" CHANERR: 0x%x\n" -" CHANERR_INT: 0x%x\n" -" DMAUNCERRSTS: 0x%x\n" , -readl (registers +IOAT_CHANERR_OFFSET ), -vfio_pci_config_readl (device,IOAT_PCI_CHANERR_INT_OFFSET ), -vfio_pci_config_readl (device,IOAT_PCI_DMAUNCERRSTS_OFFSET )); +dev_info (device,"Error detected during memcpy operation!\n" +" CHANERR: 0x%x\n" +" CHANERR_INT: 0x%x\n" +" DMAUNCERRSTS: 0x%x\n" , +readl (registers +IOAT_CHANERR_OFFSET ), +vfio_pci_config_readl (device,IOAT_PCI_CHANERR_INT_OFFSET ), +vfio_pci_config_readl (device,IOAT_PCI_DMAUNCERRSTS_OFFSET ));ioat_reset (device); }
tools/testing/selftests/vfio/lib/include/vfio_util.h +4 -0
@@ -50,6 +50,9 @@ VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__); \ } while (0) +#define dev_info (_dev, _fmt, ...) printf("%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__) +#define dev_err (_dev, _fmt, ...) fprintf(stderr, "%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__) +struct iommu_mode {const char *name;const char *container_path;
@@ -172,6 +175,7 @@ struct iommu { };struct vfio_pci_device { +const char *bdf;int fd;int group_fd;
tools/testing/selftests/vfio/lib/vfio_pci_device.c +1 -0
@@ -841,6 +841,7 @@ struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iomm device =calloc (1 ,sizeof (*device));VFIO_ASSERT_NOT_NULL (device); + device->bdf = bdf; device->iommu = iommu;if (device->iommu->mode->container_path)
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerPATCH v3 9/18 vfio: selftests: Upgrade driver logging to dev_err()
In reply to David MatlackUpgrade various logging in the VFIO selftests drivers from dev_info() to dev_err(). All of these logs indicate scenarios that may be unexpected. For example, the logging during probing indicates matching devices but that aren't supported by the driver. And the memcpy errors can indicate a problem if the caller was not trying to do something like exercise I/O fault handling. Exercising I/O fault handling is certainly a valid thing to do, but the driver can't infer the caller's expectations, so better to just log with dev_err().
--- tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c | 4 ++-- tools/testing/selftests/vfio/lib/drivers/ioat/ioat.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c +2 -2
@@ -70,7 +70,7 @@ static int dsa_probe(struct vfio_pci_device *device)return -EINVAL ;if (dsa_int_handle_request_required (device)) { -dev_info (device,"Device requires requesting interrupt handles\n" ); +dev_err (device,"Device requires requesting interrupt handles\n" );return -EINVAL ; }
@@ -256,7 +256,7 @@ static int dsa_completion_wait(struct vfio_pci_device *device,if (status ==DSA_COMP_SUCCESS )return 0 ; -dev_info (device,"Error detected during memcpy operation: 0x%x\n" , status); +dev_err (device,"Error detected during memcpy operation: 0x%x\n" , status);return -1 ; }
tools/testing/selftests/vfio/lib/drivers/ioat/ioat.c +2 -2
@@ -51,7 +51,7 @@ static int ioat_probe(struct vfio_pci_device *device) r =0 ;break ;default : -dev_info (device,"ioat: Unsupported version: 0x%x\n" , version); +dev_err (device,"ioat: Unsupported version: 0x%x\n" , version); r = -EINVAL ; }return r;
@@ -135,7 +135,7 @@ static void ioat_handle_error(struct vfio_pci_device *device) {void *registers =ioat_channel_registers (device); - dev_info(device, "Error detected during memcpy operation!\n" + dev_err(device, "Error detected during memcpy operation!\n" " CHANERR: 0x%x\n" " CHANERR_INT: 0x%x\n" " DMAUNCERRSTS: 0x%x\n",
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerPATCH v3 10/18 vfio: selftests: Rename struct vfio_dma_region to dma_region
In reply to David MatlackRename struct vfio_dma_region to dma_region. This is in preparation for separating the VFIO PCI device library code from the IOMMU library code. This name change also better reflects the fact that DMA mappings can be managed by either VFIO or IOMMUFD. i.e. the "vfio_" prefix is misleading.
--- tools/testing/selftests/vfio/lib/include/vfio_util.h | 12 ++++++------ tools/testing/selftests/vfio/lib/vfio_pci_device.c | 12 ++++++------ tools/testing/selftests/vfio/vfio_dma_mapping_test.c | 12 ++++++------ tools/testing/selftests/vfio/vfio_pci_driver_test.c | 6 +++--- 4 files changed, 21 insertions(+), 21 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +6 -6
@@ -80,7 +80,7 @@ typedef u64 iova_t;#define INVALID_IOVA UINT64_MAX -struct vfio_dma_region { +struct dma_region {struct list_head link;void *vaddr;iova_t iova;
@@ -154,7 +154,7 @@ struct vfio_pci_driver {bool memcpy_in_progress;/* Region to be used by the driver (e.g. for in-memory descriptors) */ -struct vfio_dma_region region; +struct dma_region region;/* The maximum size that can be passed to memcpy_start(). */ u64 max_memcpy_size;
@@ -236,20 +236,20 @@ void iova_allocator_cleanup(struct iova_allocator *allocator);iova_t iova_allocator_alloc (struct iova_allocator *allocator,size_t size);int __vfio_pci_dma_map (struct vfio_pci_device *device, -struct vfio_dma_region *region); +struct dma_region *region);int __vfio_pci_dma_unmap (struct vfio_pci_device *device, -struct vfio_dma_region *region, +struct dma_region *region,u64 *unmapped);int __vfio_pci_dma_unmap_all (struct vfio_pci_device *device,u64 *unmapped);static inline void vfio_pci_dma_map (struct vfio_pci_device *device, -struct vfio_dma_region *region) +struct dma_region *region) {VFIO_ASSERT_EQ (__vfio_pci_dma_map (device, region),0 ); }static inline void vfio_pci_dma_unmap (struct vfio_pci_device *device, -struct vfio_dma_region *region) +struct dma_region *region) {VFIO_ASSERT_EQ (__vfio_pci_dma_unmap (device, region,NULL ),0 ); }
tools/testing/selftests/vfio/lib/vfio_pci_device.c +6 -6
@@ -275,7 +275,7 @@ iova_t iova_allocator_alloc(struct iova_allocator *allocator, size_t size)iova_t __to_iova (struct vfio_pci_device *device,void *vaddr) { -struct vfio_dma_region *region; +struct dma_region *region;list_for_each_entry (region, &device->iommu->dma_regions, link) {if (vaddr < region->vaddr)
@@ -387,7 +387,7 @@ static void vfio_pci_irq_get(struct vfio_pci_device *device, u32 index, }static int vfio_iommu_dma_map (struct vfio_pci_device *device, -struct vfio_dma_region *region) +struct dma_region *region) {struct vfio_iommu_type1_dma_map args = { .argsz =sizeof (args),
@@ -404,7 +404,7 @@ static int vfio_iommu_dma_map(struct vfio_pci_device *device, }static int iommufd_dma_map (struct vfio_pci_device *device, -struct vfio_dma_region *region) +struct dma_region *region) {struct iommu_ioas_map args = { .size =sizeof (args),
@@ -424,7 +424,7 @@ static int iommufd_dma_map(struct vfio_pci_device *device, }int __vfio_pci_dma_map (struct vfio_pci_device *device, -struct vfio_dma_region *region) +struct dma_region *region) {int ret;
@@ -480,7 +480,7 @@ static int iommufd_dma_unmap(int fd, u64 iova, u64 length, u32 ioas_id, }int __vfio_pci_dma_unmap (struct vfio_pci_device *device, -struct vfio_dma_region *region,u64 *unmapped) +struct dma_region *region,u64 *unmapped) {int ret;
@@ -504,7 +504,7 @@ int __vfio_pci_dma_unmap(struct vfio_pci_device *device,int __vfio_pci_dma_unmap_all (struct vfio_pci_device *device,u64 *unmapped) {int ret; -struct vfio_dma_region *curr, *next; +struct dma_region *curr, *next;if (device->iommu->iommufd) ret = iommufd_dma_unmap(device->iommu->iommufd, 0, UINT64_MAX,
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +6 -6
@@ -136,7 +136,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap) {const u64 size = variant->size ?:getpagesize ();const int flags = variant->mmap_flags; -struct vfio_dma_region region; +struct dma_region region;struct iommu_mapping mapping;u64 mapping_size = size;u64 unmapped;
@@ -208,7 +208,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)FIXTURE (vfio_dma_map_limit_test) {struct iommu *iommu;struct vfio_pci_device *device; -struct vfio_dma_region region; +struct dma_region region;size_t mmap_size; };
@@ -227,7 +227,7 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES();FIXTURE_SETUP (vfio_dma_map_limit_test) { -struct vfio_dma_region *region = &self->region; +struct dma_region *region = &self->region;struct iommu_iova_range *ranges;u64 region_size =getpagesize ();iova_t last_iova;
@@ -264,7 +264,7 @@ FIXTURE_TEARDOWN(vfio_dma_map_limit_test) TEST_F(vfio_dma_map_limit_test, unmap_range) { -struct vfio_dma_region *region = &self->region; +struct dma_region *region = &self->region;u64 unmapped;int rc;
@@ -278,7 +278,7 @@ TEST_F(vfio_dma_map_limit_test, unmap_range) TEST_F(vfio_dma_map_limit_test, unmap_all) { -struct vfio_dma_region *region = &self->region; +struct dma_region *region = &self->region;u64 unmapped;int rc;
@@ -292,7 +292,7 @@ TEST_F(vfio_dma_map_limit_test, unmap_all) TEST_F(vfio_dma_map_limit_test, overflow) { -struct vfio_dma_region *region = &self->region; +struct dma_region *region = &self->region;int rc; region->iova = ~(iova_t )0 & ~(region->size -1 );
tools/testing/selftests/vfio/vfio_pci_driver_test.c +3 -3
@@ -20,7 +20,7 @@ static const char *device_bdf;static void region_setup (struct vfio_pci_device *device,struct iova_allocator *iova_allocator, -struct vfio_dma_region *region,u64 size) +struct dma_region *region,u64 size) {const int flags =MAP_SHARED |MAP_ANONYMOUS ;const int prot =PROT_READ |PROT_WRITE ;
@@ -37,7 +37,7 @@ static void region_setup(struct vfio_pci_device *device, }static void region_teardown (struct vfio_pci_device *device, -struct vfio_dma_region *region) +struct dma_region *region) {vfio_pci_dma_unmap (device, region);VFIO_ASSERT_EQ (munmap (region->vaddr, region->size),0 );
@@ -47,7 +47,7 @@ FIXTURE(vfio_pci_driver_test) {struct iommu *iommu;struct vfio_pci_device *device;struct iova_allocator *iova_allocator; -struct vfio_dma_region memcpy_region; +struct dma_region memcpy_region;void *vaddr;int msi_fd;
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerPATCH v3 11/18 vfio: selftests: Move IOMMU library code into iommu.c
In reply to David MatlackMove all the IOMMU related library code into their own file iommu.c. This provides a better separation between the vfio_pci_device helper code and the iommu code.
No function change intended.
--- .../selftests/vfio/lib/include/vfio_util.h | 74 ++- tools/testing/selftests/vfio/lib/iommu.c | 461 ++++++++++++++++++ tools/testing/selftests/vfio/lib/libvfio.mk | 3 +- .../selftests/vfio/lib/vfio_pci_device.c | 442 ----------------- 4 files changed, 527 insertions(+), 453 deletions(-) create mode 100644 tools/testing/selftests/vfio/lib/iommu.c
tools/testing/selftests/vfio/lib/include/vfio_util.h +64 -10
@@ -50,6 +50,12 @@ VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__); \ } while (0) +#define ioctl_assert (_fd, _op, _arg) do { \ + void *__arg = (_arg); \ + int __ret = ioctl((_fd), (_op), (__arg)); \ + VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #_arg, __ret); \ +} while (0) +#define dev_info (_dev, _fmt, ...) printf("%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)#define dev_err (_dev, _fmt, ...) fprintf(stderr, "%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)
@@ -223,24 +229,52 @@ extern const char *default_iommu_mode;struct iommu *iommu_init (const char *iommu_mode);void iommu_cleanup (struct iommu *iommu); +int __iommu_map (struct iommu *iommu,struct dma_region *region); + +static inline void iommu_map (struct iommu *iommu,struct dma_region *region) +{ +VFIO_ASSERT_EQ (__iommu_map (iommu, region),0 ); +} + +int __iommu_unmap (struct iommu *iommu,struct dma_region *region,u64 *unmapped); + +static inline void iommu_unmap (struct iommu *iommu,struct dma_region *region) +{ +VFIO_ASSERT_EQ (__iommu_unmap (iommu, region,NULL ),0 ); +} + +int __iommu_unmap_all (struct iommu *iommu,u64 *unmapped); + +static inline void iommu_unmap_all (struct iommu *iommu) +{ +VFIO_ASSERT_EQ (__iommu_unmap_all (iommu,NULL ),0 ); +} + +iova_t __iommu_hva2iova (struct iommu *iommu,void *vaddr); +iova_t iommu_hva2iova (struct iommu *iommu,void *vaddr); + +struct iommu_iova_range *iommu_iova_ranges (struct iommu *iommu,u32 *nranges); +struct vfio_pci_device *vfio_pci_device_init (const char *bdf,struct iommu *iommu);void vfio_pci_device_cleanup (struct vfio_pci_device *device);void vfio_pci_device_reset (struct vfio_pci_device *device); -struct iommu_iova_range *vfio_pci_iova_ranges (struct vfio_pci_device *device, -u32 *nranges); +static inline struct iommu_iova_range *vfio_pci_iova_ranges (struct vfio_pci_device *device, +u32 *nranges) +{ +return iommu_iova_ranges (device->iommu, nranges); +}struct iova_allocator *iova_allocator_init (struct vfio_pci_device *device);void iova_allocator_cleanup (struct iova_allocator *allocator);iova_t iova_allocator_alloc (struct iova_allocator *allocator,size_t size); -int __vfio_pci_dma_map (struct vfio_pci_device *device, -struct dma_region *region); -int __vfio_pci_dma_unmap (struct vfio_pci_device *device, -struct dma_region *region, -u64 *unmapped); -int __vfio_pci_dma_unmap_all (struct vfio_pci_device *device,u64 *unmapped); +static inline int __vfio_pci_dma_map (struct vfio_pci_device *device, +struct dma_region *region) +{ +return __iommu_map (device->iommu, region); +}static inline void vfio_pci_dma_map (struct vfio_pci_device *device,struct dma_region *region)
@@ -248,12 +282,25 @@ static inline void vfio_pci_dma_map(struct vfio_pci_device *device,VFIO_ASSERT_EQ (__vfio_pci_dma_map (device, region),0 ); } +static inline int __vfio_pci_dma_unmap (struct vfio_pci_device *device, +struct dma_region *region, +u64 *unmapped) +{ +return __iommu_unmap (device->iommu, region, unmapped); +} +static inline void vfio_pci_dma_unmap (struct vfio_pci_device *device,struct dma_region *region) {VFIO_ASSERT_EQ (__vfio_pci_dma_unmap (device, region,NULL ),0 ); } +static inline int __vfio_pci_dma_unmap_all (struct vfio_pci_device *device, +u64 *unmapped) +{ +return __iommu_unmap_all (device->iommu, unmapped); +} +static inline void vfio_pci_dma_unmap_all (struct vfio_pci_device *device) {VFIO_ASSERT_EQ (__vfio_pci_dma_unmap_all (device,NULL ),0 );
@@ -319,8 +366,15 @@ static inline void vfio_pci_msix_disable(struct vfio_pci_device *device)vfio_pci_irq_disable (device,VFIO_PCI_MSIX_IRQ_INDEX ); } -iova_t __to_iova (struct vfio_pci_device *device,void *vaddr); -iova_t to_iova (struct vfio_pci_device *device,void *vaddr); +static inline iova_t __to_iova (struct vfio_pci_device *device,void *vaddr) +{ +return __iommu_hva2iova (device->iommu, vaddr); +} + +static inline iova_t to_iova (struct vfio_pci_device *device,void *vaddr) +{ +return iommu_hva2iova (device->iommu, vaddr); +}static inline bool vfio_pci_device_match (struct vfio_pci_device *device,u16 vendor_id,u16 device_id)
tools/testing/selftests/vfio/lib/iommu.c new file +461 -0
461 lines not on this page · raw message
tools/testing/selftests/vfio/lib/libvfio.mk +2 -1
@@ -3,7 +3,8 @@ ARCH ?= $(SUBARCH) LIBVFIO_SRCDIR := $(selfdir)/vfio/lib -LIBVFIO_C := vfio_pci_device.c +LIBVFIO_C := iommu.c +LIBVFIO_C += vfio_pci_device.c LIBVFIO_C += vfio_pci_driver.c ifeq ($(ARCH:x86_64=x86),x86)
tools/testing/selftests/vfio/lib/vfio_pci_device.c +0 -442
484 lines not on this page · raw message
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
kernel test robot
build botRe: [PATCH v3 11/18] vfio: selftests: Move IOMMU library code into iommu.c
kernel test robot noticed the following build errors:
Hi David,
kernel test robot noticed the following build errors:
[auto build test ERROR on fa804aa4ac1b091ef2ec2981f08a1c28aaeba8e7]
url: https://github.com/intel-lab-lkp/linux/commits/David-Matlack/vfio-selftests-Move-run-sh-into-scripts-directory/20251122-022109 base: fa804aa4ac1b091ef2ec2981f08a1c28aaeba8e7 patch link: https://lore.kernel.org/r/20251121181429.1421717-12-dmatlack%40google.com patch subject: [PATCH v3 11/18] vfio: selftests: Move IOMMU library code into iommu.c config: riscv-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20251123/[email protected]/config) compiler: riscv64-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251123/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/
All errors (new ones prefixed by >>):
>> tools/testing/selftests/vfio/lib/iommu.c:2:10: fatal error: dirent.h: No such file or directory
2 | #include <dirent.h>
| ^~~~~~~~~~compilation terminated.
vim +2 tools/testing/selftests/vfio/lib/iommu.c
4391647129942c David Matlack 2025-11-21 @2 #include <dirent.h> 4391647129942c David Matlack 2025-11-21 3 #include <fcntl.h> 4391647129942c David Matlack 2025-11-21 4 #include <libgen.h> 4391647129942c David Matlack 2025-11-21 5 #include <stdint.h> 4391647129942c David Matlack 2025-11-21 6 #include <stdlib.h> 4391647129942c David Matlack 2025-11-21 7 #include <string.h> 4391647129942c David Matlack 2025-11-21 8 #include <unistd.h>
4391647129942c David Matlack 2025-11-21 9
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
David Matlack
maintainerPATCH v3 12/18 vfio: selftests: Move IOVA allocator into iova_allocator.c
In reply to David MatlackMove the IOVA allocator into its own file, to provide better separation between the allocator and the struct vfio_pci_device helper code.
The allocator could go into iommu.c, but it is standalone enough that a separate file seems cleaner. This also continues the trend of having a .c for every major object in VFIO selftests (vfio_pci_device.c, vfio_pci_driver.c, iommu.c, and now iova_allocator.c).
No functional change intended.
--- .../selftests/vfio/lib/iova_allocator.c | 94 +++++++++++++++++++ tools/testing/selftests/vfio/lib/libvfio.mk | 1 + .../selftests/vfio/lib/vfio_pci_device.c | 71 -------------- 3 files changed, 95 insertions(+), 71 deletions(-) create mode 100644 tools/testing/selftests/vfio/lib/iova_allocator.c
tools/testing/selftests/vfio/lib/iova_allocator.c new file +94 -0
@@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <dirent.h> +#include <fcntl.h> +#include <libgen.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <sys/eventfd.h> +#include <sys/ioctl.h> +#include <sys/mman.h> + +#include <uapi/linux/types.h> +#include <linux/iommufd.h> +#include <linux/limits.h> +#include <linux/mman.h> +#include <linux/overflow.h> +#include <linux/types.h> +#include <linux/vfio.h> + +#include <vfio_util.h> + +struct iova_allocator *iova_allocator_init (struct vfio_pci_device *device) +{ +struct iova_allocator *allocator; +struct iommu_iova_range *ranges; +u32 nranges; + + ranges =vfio_pci_iova_ranges (device, &nranges); +VFIO_ASSERT_NOT_NULL (ranges); + + allocator =malloc (sizeof (*allocator)); +VFIO_ASSERT_NOT_NULL (allocator); + + *allocator = (struct iova_allocator ){ + .ranges = ranges, + .nranges = nranges, + .range_idx =0 , + .range_offset =0 , + }; + +return allocator; +} + +void iova_allocator_cleanup (struct iova_allocator *allocator) +{ +free (allocator->ranges); +free (allocator); +} + +iova_t iova_allocator_alloc (struct iova_allocator *allocator,size_t size) +{ +VFIO_ASSERT_GT (size,0 ,"Invalid size arg, zero\n" ); +VFIO_ASSERT_EQ (size & (size -1 ),0 ,"Invalid size arg, non-power-of-2\n" ); + +for (;;) { +struct iommu_iova_range *range; +iova_t iova, last; + +VFIO_ASSERT_LT (allocator->range_idx, allocator->nranges, +"IOVA allocator out of space\n" ); + + range = &allocator->ranges[allocator->range_idx]; + iova = range->start + allocator->range_offset; + +/* Check for sufficient space at the current offset */ +if (check_add_overflow (iova, size -1 , &last) || + last > range->last) + goto next_range; + +/* Align iova to size */ + iova = last & ~(size -1 ); + +/* Check for sufficient space at the aligned iova */ +if (check_add_overflow (iova, size -1 , &last) || + last > range->last) + goto next_range; + +if (last == range->last) { + allocator->range_idx++; + allocator->range_offset =0 ; + }else { + allocator->range_offset = last - range->start +1 ; + } + +return iova; + +next_range: + allocator->range_idx++; + allocator->range_offset =0 ; + } +} +
tools/testing/selftests/vfio/lib/libvfio.mk +1 -0
@@ -4,6 +4,7 @@ ARCH ?= $(SUBARCH) LIBVFIO_SRCDIR := $(selfdir)/vfio/lib LIBVFIO_C := iommu.c +LIBVFIO_C += iova_allocator.c LIBVFIO_C += vfio_pci_device.c LIBVFIO_C += vfio_pci_driver.c
tools/testing/selftests/vfio/lib/vfio_pci_device.c +0 -71
77 lines not on this page · raw message
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
kernel test robot
build botRe: [PATCH v3 12/18] vfio: selftests: Move IOVA allocator into iova_allocator.c
kernel test robot noticed the following build errors:
Hi David,
kernel test robot noticed the following build errors:
[auto build test ERROR on fa804aa4ac1b091ef2ec2981f08a1c28aaeba8e7]
url: https://github.com/intel-lab-lkp/linux/commits/David-Matlack/vfio-selftests-Move-run-sh-into-scripts-directory/20251122-022109 base: fa804aa4ac1b091ef2ec2981f08a1c28aaeba8e7 patch link: https://lore.kernel.org/r/20251121181429.1421717-13-dmatlack%40google.com patch subject: [PATCH v3 12/18] vfio: selftests: Move IOVA allocator into iova_allocator.c config: riscv-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20251123/[email protected]/config) compiler: riscv64-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251123/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/
All errors (new ones prefixed by >>):
>> tools/testing/selftests/vfio/lib/iova_allocator.c:2:10: fatal error: dirent.h: No such file or directory
2 | #include <dirent.h>
| ^~~~~~~~~~compilation terminated.
vim +2 tools/testing/selftests/vfio/lib/iova_allocator.c
a1ab4208a86852 David Matlack 2025-11-21 @2 #include <dirent.h> a1ab4208a86852 David Matlack 2025-11-21 3 #include <fcntl.h> a1ab4208a86852 David Matlack 2025-11-21 4 #include <libgen.h> a1ab4208a86852 David Matlack 2025-11-21 5 #include <stdint.h> a1ab4208a86852 David Matlack 2025-11-21 6 #include <stdlib.h> a1ab4208a86852 David Matlack 2025-11-21 7 #include <string.h> a1ab4208a86852 David Matlack 2025-11-21 8 #include <unistd.h>
a1ab4208a86852 David Matlack 2025-11-21 9
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
David Matlack
maintainerPATCH v3 13/18 vfio: selftests: Stop passing device for IOMMU operations
In reply to David MatlackDrop the struct vfio_pci_device wrappers for IOMMU map/unmap functions and require tests to directly call iommu_map(), iommu_unmap(), etc. This results in more concise code, and also makes it clear the map operations are happening on a struct iommu, not necessarily on a specific device, especially when multi-device tests are introduced.
Do the same for iova_allocator_init() as that function only needs the struct iommu, not struct vfio_pci_device.
--- .../selftests/vfio/lib/include/vfio_util.h | 44 +------------------ .../selftests/vfio/lib/iova_allocator.c | 4 +- .../selftests/vfio/vfio_dma_mapping_test.c | 20 ++++----- .../selftests/vfio/vfio_pci_driver_test.c | 19 ++++---- 4 files changed, 22 insertions(+), 65 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +1 -43
@@ -260,52 +260,10 @@ void vfio_pci_device_cleanup(struct vfio_pci_device *device);void vfio_pci_device_reset (struct vfio_pci_device *device); -static inline struct iommu_iova_range *vfio_pci_iova_ranges (struct vfio_pci_device *device, -u32 *nranges) -{ -return iommu_iova_ranges (device->iommu, nranges); -} - -struct iova_allocator *iova_allocator_init (struct vfio_pci_device *device); +struct iova_allocator *iova_allocator_init (struct iommu *iommu);void iova_allocator_cleanup (struct iova_allocator *allocator);iova_t iova_allocator_alloc (struct iova_allocator *allocator,size_t size); -static inline int __vfio_pci_dma_map (struct vfio_pci_device *device, -struct dma_region *region) -{ -return __iommu_map (device->iommu, region); -} - -static inline void vfio_pci_dma_map (struct vfio_pci_device *device, -struct dma_region *region) -{ -VFIO_ASSERT_EQ (__vfio_pci_dma_map (device, region),0 ); -} - -static inline int __vfio_pci_dma_unmap (struct vfio_pci_device *device, -struct dma_region *region, -u64 *unmapped) -{ -return __iommu_unmap (device->iommu, region, unmapped); -} - -static inline void vfio_pci_dma_unmap (struct vfio_pci_device *device, -struct dma_region *region) -{ -VFIO_ASSERT_EQ (__vfio_pci_dma_unmap (device, region,NULL ),0 ); -} - -static inline int __vfio_pci_dma_unmap_all (struct vfio_pci_device *device, -u64 *unmapped) -{ -return __iommu_unmap_all (device->iommu, unmapped); -} - -static inline void vfio_pci_dma_unmap_all (struct vfio_pci_device *device) -{ -VFIO_ASSERT_EQ (__vfio_pci_dma_unmap_all (device,NULL ),0 ); -} -void vfio_pci_config_access (struct vfio_pci_device *device,bool write,size_t config,size_t size,void *data);
tools/testing/selftests/vfio/lib/iova_allocator.c +2 -2
@@ -21,13 +21,13 @@#include <vfio_util.h> -struct iova_allocator *iova_allocator_init (struct vfio_pci_device *device) +struct iova_allocator *iova_allocator_init (struct iommu *iommu) {struct iova_allocator *allocator;struct iommu_iova_range *ranges;u32 nranges; - ranges =vfio_pci_iova_ranges (device, &nranges); + ranges =iommu_iova_ranges (iommu, &nranges);VFIO_ASSERT_NOT_NULL (ranges); allocator =malloc (sizeof (*allocator));
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +10 -10
@@ -122,7 +122,7 @@ FIXTURE_SETUP(vfio_dma_mapping_test) { self->iommu =iommu_init (variant->iommu_mode); self->device =vfio_pci_device_init (device_bdf, self->iommu); - self->iova_allocator =iova_allocator_init (self->device); + self->iova_allocator =iova_allocator_init (self->iommu); }FIXTURE_TEARDOWN (vfio_dma_mapping_test)
@@ -153,7 +153,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap) region.iova =iova_allocator_alloc (self->iova_allocator, size); region.size = size; -vfio_pci_dma_map (self->device, ®ion); +iommu_map (self->iommu, ®ion);printf ("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n" , region.vaddr, size, region.iova);ASSERT_EQ (region.iova,to_iova (self->device, region.vaddr));
@@ -195,7 +195,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap) } unmap: - rc =__vfio_pci_dma_unmap (self->device, ®ion, &unmapped); + rc =__iommu_unmap (self->iommu, ®ion, &unmapped);ASSERT_EQ (rc,0 );ASSERT_EQ (unmapped, region.size);printf ("Unmapped IOVA 0x%lx\n" , region.iova);
@@ -245,7 +245,7 @@ FIXTURE_SETUP(vfio_dma_map_limit_test) MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);ASSERT_NE (region->vaddr,MAP_FAILED ); - ranges =vfio_pci_iova_ranges (self->device, &nranges); + ranges =iommu_iova_ranges (self->iommu, &nranges);VFIO_ASSERT_NOT_NULL (ranges); last_iova = ranges[nranges -1 ].last;free (ranges);
@@ -268,10 +268,10 @@ TEST_F(vfio_dma_map_limit_test, unmap_range)u64 unmapped;int rc; -vfio_pci_dma_map (self->device, region); +iommu_map (self->iommu, region);ASSERT_EQ (region->iova,to_iova (self->device, region->vaddr)); - rc =__vfio_pci_dma_unmap (self->device, region, &unmapped); + rc =__iommu_unmap (self->iommu, region, &unmapped);ASSERT_EQ (rc,0 );ASSERT_EQ (unmapped, region->size); }
@@ -282,10 +282,10 @@ TEST_F(vfio_dma_map_limit_test, unmap_all)u64 unmapped;int rc; -vfio_pci_dma_map (self->device, region); +iommu_map (self->iommu, region);ASSERT_EQ (region->iova,to_iova (self->device, region->vaddr)); - rc =__vfio_pci_dma_unmap_all (self->device, &unmapped); + rc =__iommu_unmap_all (self->iommu, &unmapped);ASSERT_EQ (rc,0 );ASSERT_EQ (unmapped, region->size); }
@@ -298,10 +298,10 @@ TEST_F(vfio_dma_map_limit_test, overflow) region->iova = ~(iova_t )0 & ~(region->size -1 ); region->size = self->mmap_size; - rc =__vfio_pci_dma_map (self->device, region); + rc =__iommu_map (self->iommu, region);ASSERT_EQ (rc, -EOVERFLOW ); - rc =__vfio_pci_dma_unmap (self->device, region,NULL ); + rc =__iommu_unmap (self->iommu, region,NULL );ASSERT_EQ (rc, -EOVERFLOW ); }
tools/testing/selftests/vfio/vfio_pci_driver_test.c +9 -10
@@ -18,7 +18,7 @@ static const char *device_bdf;ASSERT_EQ (EAGAIN , errno); \ }while (0 ) -static void region_setup(struct vfio_pci_device *device, +static void region_setup(struct iommu *iommu,struct iova_allocator *iova_allocator,struct dma_region *region,u64 size) {
@@ -33,13 +33,12 @@ static void region_setup(struct vfio_pci_device *device, region->iova =iova_allocator_alloc (iova_allocator, size); region->size = size; -vfio_pci_dma_map (device, region); +iommu_map (iommu, region); } -static void region_teardown (struct vfio_pci_device *device, -struct dma_region *region) +static void region_teardown (struct iommu *iommu,struct dma_region *region) { -vfio_pci_dma_unmap (device, region); +iommu_unmap (iommu, region);VFIO_ASSERT_EQ (munmap (region->vaddr, region->size),0 ); }
@@ -76,12 +75,12 @@ FIXTURE_SETUP(vfio_pci_driver_test) self->iommu =iommu_init (variant->iommu_mode); self->device =vfio_pci_device_init (device_bdf, self->iommu); - self->iova_allocator =iova_allocator_init (self->device); + self->iova_allocator =iova_allocator_init (self->iommu); driver = &self->device->driver; -region_setup (self->device, self->iova_allocator, &self->memcpy_region,SZ_1G ); -region_setup (self->device, self->iova_allocator, &driver->region,SZ_2M ); +region_setup (self->iommu, self->iova_allocator, &self->memcpy_region,SZ_1G ); +region_setup (self->iommu, self->iova_allocator, &driver->region,SZ_2M );/* Any IOVA that doesn't overlap memcpy_region and driver->region. */ self->unmapped_iova =iova_allocator_alloc (self->iova_allocator,SZ_1G );
@@ -110,8 +109,8 @@ FIXTURE_TEARDOWN(vfio_pci_driver_test)vfio_pci_driver_remove (self->device); -region_teardown (self->device, &self->memcpy_region); -region_teardown (self->device, &driver->region); +region_teardown (self->iommu, &self->memcpy_region); +region_teardown (self->iommu, &driver->region);iova_allocator_cleanup (self->iova_allocator);vfio_pci_device_cleanup (self->device);
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerPATCH v3 14/18 vfio: selftests: Rename vfio_util.h to libvfio.h
In reply to David MatlackRename vfio_util.h to libvfio.h to match the name of libvfio.mk.
No functional change intended.
---
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c | 2 +-
tools/testing/selftests/vfio/lib/drivers/ioat/ioat.c | 2 +-
.../selftests/vfio/lib/include/{vfio_util.h => libvfio.h} | 6 +++---
tools/testing/selftests/vfio/lib/iommu.c | 2 +-
tools/testing/selftests/vfio/lib/iova_allocator.c | 2 +-
tools/testing/selftests/vfio/lib/vfio_pci_device.c | 2 +-
tools/testing/selftests/vfio/lib/vfio_pci_driver.c | 2 +-
tools/testing/selftests/vfio/vfio_dma_mapping_test.c | 2 +-
tools/testing/selftests/vfio/vfio_iommufd_setup_test.c | 2 +-
tools/testing/selftests/vfio/vfio_pci_device_test.c | 2 +-
tools/testing/selftests/vfio/vfio_pci_driver_test.c | 2 +-
11 files changed, 13 insertions(+), 13 deletions(-)
rename tools/testing/selftests/vfio/lib/include/{vfio_util.h => libvfio.h} (98%)tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c +1 -1
@@ -9,7 +9,7 @@#include <linux/pci_ids.h> #include <linux/sizes.h> -#include <vfio_util.h> +#include <libvfio.h> #include "registers.h"
tools/testing/selftests/vfio/lib/drivers/ioat/ioat.c +1 -1
@@ -7,7 +7,7 @@#include <linux/pci_ids.h> #include <linux/sizes.h> -#include <vfio_util.h> +#include <libvfio.h> #include "hw.h" #include "registers.h"
tools/testing/selftests/vfio/lib/include/libvfio.h renamed from tools/testing/selftests/vfio/lib/include/vfio_util.h +3 -3
@@ -1,6 +1,6 @@/* SPDX-License-Identifier: GPL-2.0-only */ -#ifndef SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H -#define SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H +#ifndef SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H +#define SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H #include <fcntl.h> #include <string.h>
@@ -352,4 +352,4 @@ void vfio_pci_driver_memcpy_start(struct vfio_pci_device *device,int vfio_pci_driver_memcpy_wait (struct vfio_pci_device *device);void vfio_pci_driver_send_msi (struct vfio_pci_device *device); -#endif /* SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H */ +#endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H */
tools/testing/selftests/vfio/lib/iommu.c +1 -1
@@ -19,7 +19,7 @@#include <linux/iommufd.h> #include "../../../kselftest.h" -#include <vfio_util.h> +#include <libvfio.h> const char *default_iommu_mode ="iommufd" ;
tools/testing/selftests/vfio/lib/iova_allocator.c +1 -1
@@ -19,7 +19,7 @@#include <linux/types.h> #include <linux/vfio.h> -#include <vfio_util.h> +#include <libvfio.h> struct iova_allocator *iova_allocator_init (struct iommu *iommu) {
tools/testing/selftests/vfio/lib/vfio_pci_device.c +1 -1
@@ -20,7 +20,7 @@#include <linux/vfio.h> #include "../../../kselftest.h" -#include <vfio_util.h> +#include <libvfio.h> #define PCI_SYSFS_PATH "/sys/bus/pci/devices"
tools/testing/selftests/vfio/lib/vfio_pci_driver.c +1 -1
@@ -1,6 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only #include "../../../kselftest.h" -#include <vfio_util.h> +#include <libvfio.h> #ifdef __x86_64__extern struct vfio_pci_driver_ops dsa_ops;
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +1 -1
@@ -10,7 +10,7 @@#include <linux/sizes.h> #include <linux/vfio.h> -#include <vfio_util.h> +#include <libvfio.h> #include "../kselftest_harness.h"
tools/testing/selftests/vfio/vfio_iommufd_setup_test.c +1 -1
@@ -10,7 +10,7 @@#include <sys/ioctl.h> #include <unistd.h> -#include <vfio_util.h> +#include <libvfio.h> #include "../kselftest_harness.h" static const char iommu_dev_path[] ="/dev/iommu" ;
tools/testing/selftests/vfio/vfio_pci_device_test.c +1 -1
@@ -10,7 +10,7 @@#include <linux/sizes.h> #include <linux/vfio.h> -#include <vfio_util.h> +#include <libvfio.h> #include "../kselftest_harness.h"
tools/testing/selftests/vfio/vfio_pci_driver_test.c +1 -1
@@ -5,7 +5,7 @@#include <linux/sizes.h> #include <linux/vfio.h> -#include <vfio_util.h> +#include <libvfio.h> #include "../kselftest_harness.h"
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerPATCH v3 15/18 vfio: selftests: Move vfio_selftests_*() helpers into libvfio.c
In reply to David MatlackMove the vfio_selftests_*() helpers into their own file libvfio.c. These helpers have nothing to do with struct vfio_pci_device, so they don't make sense in vfio_pci_device.c.
No functional change intended.
--- tools/testing/selftests/vfio/lib/libvfio.c | 78 +++++++++++++++++++ tools/testing/selftests/vfio/lib/libvfio.mk | 1 + .../selftests/vfio/lib/vfio_pci_device.c | 71 ----------------- 3 files changed, 79 insertions(+), 71 deletions(-) create mode 100644 tools/testing/selftests/vfio/lib/libvfio.c
tools/testing/selftests/vfio/lib/libvfio.c new file +78 -0
@@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <stdio.h> +#include <stdlib.h> + +#include "../../../kselftest.h" +#include <libvfio.h> + +static bool is_bdf (const char *str) +{ +unsigned int s, b, d, f; +int length, count; + + count =sscanf (str,"%4x:%2x:%2x.%2x%n" , &s, &b, &d, &f, &length); +return count ==4 && length ==strlen (str); +} + +static char **get_bdfs_cmdline (int *argc,char *argv[],int *nr_bdfs) +{ +int i; + +for (i = *argc -1 ; i >0 &&is_bdf (argv[i]); i--) +continue ; + + i++; + *nr_bdfs = *argc - i; + *argc -= *nr_bdfs; + +return *nr_bdfs ? &argv[i] :NULL ; +} + +static char *get_bdf_env (void ) +{ +char *bdf; + + bdf =getenv ("VFIO_SELFTESTS_BDF" ); +if (!bdf) +return NULL ; + +VFIO_ASSERT_TRUE (is_bdf (bdf),"Invalid BDF: %s\n" , bdf); +return bdf; +} + +char **vfio_selftests_get_bdfs (int *argc,char *argv[],int *nr_bdfs) +{ +static char *env_bdf; +char **bdfs; + + bdfs =get_bdfs_cmdline (argc, argv, nr_bdfs); +if (bdfs) +return bdfs; + + env_bdf =get_bdf_env (); +if (env_bdf) { + *nr_bdfs =1 ; +return &env_bdf; + } + +fprintf (stderr,"Unable to determine which device(s) to use, skipping test.\n" ); +fprintf (stderr,"\n" ); +fprintf (stderr,"To pass the device address via environment variable:\n" ); +fprintf (stderr,"\n" ); +fprintf (stderr," export VFIO_SELFTESTS_BDF=\"segment:bus:device.function\"\n" ); +fprintf (stderr," %s [options]\n" , argv[0 ]); +fprintf (stderr,"\n" ); +fprintf (stderr,"To pass the device address(es) via argv:\n" ); +fprintf (stderr,"\n" ); +fprintf (stderr," %s [options] segment:bus:device.function ...\n" , argv[0 ]); +fprintf (stderr,"\n" ); +exit (KSFT_SKIP ); +} + +const char *vfio_selftests_get_bdf (int *argc,char *argv[]) +{ +int nr_bdfs; + +return vfio_selftests_get_bdfs (argc, argv, &nr_bdfs)[0 ]; +}
tools/testing/selftests/vfio/lib/libvfio.mk +1 -0
@@ -5,6 +5,7 @@ LIBVFIO_SRCDIR := $(selfdir)/vfio/lib LIBVFIO_C := iommu.c LIBVFIO_C += iova_allocator.c +LIBVFIO_C += libvfio.c LIBVFIO_C += vfio_pci_device.c LIBVFIO_C += vfio_pci_driver.c
tools/testing/selftests/vfio/lib/vfio_pci_device.c +0 -71
74 lines not on this page · raw message
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerPATCH v3 16/18 vfio: selftests: Split libvfio.h into separate header files
In reply to David MatlackSplit out the contents of libvfio.h into separate header files, but keep libvfio.h as the top-level include that all tests can use.
Put all new header files into a libvfio/ subdirectory to avoid future name conflicts in include paths when libvfio is used by other selftests like KVM.
No functional change intended.
--- .../selftests/vfio/lib/include/libvfio.h | 339 +----------------- .../vfio/lib/include/libvfio/assert.h | 54 +++ .../vfio/lib/include/libvfio/iommu.h | 77 ++++ .../vfio/lib/include/libvfio/iova_allocator.h | 23 ++ .../lib/include/libvfio/vfio_pci_device.h | 125 +++++++ .../lib/include/libvfio/vfio_pci_driver.h | 97 +++++ 6 files changed, 381 insertions(+), 334 deletions(-) create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/assert.h create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/iommu.h create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/iova_allocator.h create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_driver.h
tools/testing/selftests/vfio/lib/include/libvfio.h +5 -334
@@ -2,210 +2,11 @@#ifndef SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H #define SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H -#include <fcntl.h> -#include <string.h> - -#include <uapi/linux/types.h> -#include <linux/iommufd.h> -#include <linux/list.h> -#include <linux/pci_regs.h> -#include <linux/vfio.h> - -#include "../../../kselftest.h" - -#define VFIO_LOG_AND_EXIT (...) do { \ - fprintf(stderr, " " __VA_ARGS__); \ - fprintf(stderr, "\n"); \ - exit(KSFT_FAIL); \ -} while (0) - -#define VFIO_ASSERT_OP (_lhs, _rhs, _op, ...) do { \ - typeof(_lhs) __lhs = (_lhs); \ - typeof(_rhs) __rhs = (_rhs); \ - \ - if (__lhs _op __rhs) \ - break; \ - \ - fprintf(stderr, "%s:%u: Assertion Failure\n\n", __FILE__, __LINE__); \ - fprintf(stderr, " Expression: " #_lhs " " #_op " " #_rhs "\n"); \ - fprintf(stderr, " Observed: %#lx %s %#lx\n", \ - (u64)__lhs, #_op, (u64)__rhs); \ - fprintf(stderr, " [errno: %d - %s]\n", errno, strerror(errno)); \ - VFIO_LOG_AND_EXIT(__VA_ARGS__); \ -} while (0) - -#define VFIO_ASSERT_EQ (_a, _b, ...) VFIO_ASSERT_OP(_a, _b, ==, ##__VA_ARGS__) -#define VFIO_ASSERT_NE (_a, _b, ...) VFIO_ASSERT_OP(_a, _b, !=, ##__VA_ARGS__) -#define VFIO_ASSERT_LT (_a, _b, ...) VFIO_ASSERT_OP(_a, _b, <, ##__VA_ARGS__) -#define VFIO_ASSERT_LE (_a, _b, ...) VFIO_ASSERT_OP(_a, _b, <=, ##__VA_ARGS__) -#define VFIO_ASSERT_GT (_a, _b, ...) VFIO_ASSERT_OP(_a, _b, >, ##__VA_ARGS__) -#define VFIO_ASSERT_GE (_a, _b, ...) VFIO_ASSERT_OP(_a, _b, >=, ##__VA_ARGS__) -#define VFIO_ASSERT_TRUE (_a, ...) VFIO_ASSERT_NE(false, (_a), ##__VA_ARGS__) -#define VFIO_ASSERT_FALSE (_a, ...) VFIO_ASSERT_EQ(false, (_a), ##__VA_ARGS__) -#define VFIO_ASSERT_NULL (_a, ...) VFIO_ASSERT_EQ(NULL, _a, ##__VA_ARGS__) -#define VFIO_ASSERT_NOT_NULL (_a, ...) VFIO_ASSERT_NE(NULL, _a, ##__VA_ARGS__) - -#define VFIO_FAIL (_fmt, ...) do { \ - fprintf(stderr, "%s:%u: FAIL\n\n", __FILE__, __LINE__); \ - VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__); \ -} while (0) - -#define ioctl_assert (_fd, _op, _arg) do { \ - void *__arg = (_arg); \ - int __ret = ioctl((_fd), (_op), (__arg)); \ - VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #_arg, __ret); \ -} while (0) - -#define dev_info (_dev, _fmt, ...) printf("%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__) -#define dev_err (_dev, _fmt, ...) fprintf(stderr, "%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__) - -struct iommu_mode { -const char *name; -const char *container_path; -unsigned long iommu_type; -}; - -/* -* Generator for VFIO selftests fixture variants that replicate across all -* possible IOMMU modes. Tests must define FIXTURE_VARIANT_ADD_IOMMU_MODE() -* which should then use FIXTURE_VARIANT_ADD() to create the variant. -*/ -#define FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES (...) \ -FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1_iommu, ##__VA_ARGS__); \ -FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1v2_iommu, ##__VA_ARGS__); \ -FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd_compat_type1, ##__VA_ARGS__); \ -FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd_compat_type1v2, ##__VA_ARGS__); \ -FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd, ##__VA_ARGS__) - -struct vfio_pci_bar { -struct vfio_region_info info; -void *vaddr; -}; - -typedef u64 iova_t ; - -#define INVALID_IOVA UINT64_MAX - -struct dma_region { -struct list_head link; -void *vaddr; -iova_t iova; -u64 size; -}; - -struct vfio_pci_device ; - -struct vfio_pci_driver_ops { -const char *name; - -/** -* @probe() - Check if the driver supports the given device. -* -* Return: 0 on success, non-0 on failure. -*/ -int (*probe)(struct vfio_pci_device *device); - -/** -* @init() - Initialize the driver for @device. -* -* Must be called after device->driver.region has been initialized. -*/ -void (*init)(struct vfio_pci_device *device); - -/** -* remove() - Deinitialize the driver for @device. -*/ -void (*remove)(struct vfio_pci_device *device); - -/** -* memcpy_start() - Kick off @count repeated memcpy operations from -* [@src, @src + @size) to [@dst, @dst + @size). -* -* Guarantees: -* - The device will attempt DMA reads on [src, src + size). -* - The device will attempt DMA writes on [dst, dst + size). -* - The device will not generate any interrupts. -* -* memcpy_start() returns immediately, it does not wait for the -* copies to complete. -*/ -void (*memcpy_start)(struct vfio_pci_device *device, -iova_t src,iova_t dst,u64 size,u64 count); - -/** -* memcpy_wait() - Wait until the memcpy operations started by -* memcpy_start() have finished. -* -* Guarantees: -* - All in-flight DMAs initiated by memcpy_start() are fully complete -* before memcpy_wait() returns. -* -* Returns non-0 if the driver detects that an error occurred during the -* memcpy, 0 otherwise. -*/ -int (*memcpy_wait)(struct vfio_pci_device *device); - -/** -* send_msi() - Make the device send the MSI device->driver.msi. -* -* Guarantees: -* - The device will send the MSI once. -*/ -void (*send_msi)(struct vfio_pci_device *device); -}; - -struct vfio_pci_driver { -const struct vfio_pci_driver_ops *ops; -bool initialized; -bool memcpy_in_progress; - -/* Region to be used by the driver (e.g. for in-memory descriptors) */ -struct dma_region region; - -/* The maximum size that can be passed to memcpy_start(). */ -u64 max_memcpy_size; - -/* The maximum count that can be passed to memcpy_start(). */ -u64 max_memcpy_count; - -/* The MSI vector the device will signal in ops->send_msi(). */ -int msi; -}; - -struct iommu { -const struct iommu_mode *mode; -int container_fd; -int iommufd; -u32 ioas_id; -struct list_head dma_regions; -}; - -struct vfio_pci_device { -const char *bdf; -int fd; -int group_fd; - -struct iommu *iommu; - -struct vfio_device_info info; -struct vfio_region_info config_space; -struct vfio_pci_bar bars[PCI_STD_NUM_BARS ]; - -struct vfio_irq_info msi_info; -struct vfio_irq_info msix_info; - -/* eventfds for MSI and MSI-x interrupts */ -int msi_eventfds[PCI_MSIX_FLAGS_QSIZE +1 ]; - -struct vfio_pci_driver driver; -}; - -struct iova_allocator { -struct iommu_iova_range *ranges; -u32 nranges; -u32 range_idx; -u64 range_offset; -}; +#include <libvfio/assert.h> +#include <libvfio/iommu.h> +#include <libvfio/iova_allocator.h> +#include <libvfio/vfio_pci_device.h> +#include <libvfio/vfio_pci_driver.h> /* * Return the BDF string of the device that the test should use.
@@ -222,134 +23,4 @@ struct iova_allocator {const char *vfio_selftests_get_bdf (int *argc,char *argv[]);char **vfio_selftests_get_bdfs (int *argc,char *argv[],int *nr_bdfs); -const char *vfio_pci_get_cdev_path (const char *bdf); - -extern const char *default_iommu_mode; - -struct iommu *iommu_init (const char *iommu_mode); -void iommu_cleanup (struct iommu *iommu); - -int __iommu_map (struct iommu *iommu,struct dma_region *region); - -static inline void iommu_map (struct iommu *iommu,struct dma_region *region) -{ -VFIO_ASSERT_EQ (__iommu_map (iommu, region),0 ); -} - -int __iommu_unmap (struct iommu *iommu,struct dma_region *region,u64 *unmapped); - -static inline void iommu_unmap (struct iommu *iommu,struct dma_region *region) -{ -VFIO_ASSERT_EQ (__iommu_unmap (iommu, region,NULL ),0 ); -} - -int __iommu_unmap_all (struct iommu *iommu,u64 *unmapped); - -static inline void iommu_unmap_all (struct iommu *iommu) -{ -VFIO_ASSERT_EQ (__iommu_unmap_all (iommu,NULL ),0 ); -} - -iova_t __iommu_hva2iova (struct iommu *iommu,void *vaddr); -iova_t iommu_hva2iova (struct iommu *iommu,void *vaddr); - -struct iommu_iova_range *iommu_iova_ranges (struct iommu *iommu,u32 *nranges); - -struct vfio_pci_device *vfio_pci_device_init (const char *bdf,struct iommu *iommu); -void vfio_pci_device_cleanup (struct vfio_pci_device *device); - -void vfio_pci_device_reset (struct vfio_pci_device *device); - -struct iova_allocator *iova_allocator_init (struct iommu *iommu); -void iova_allocator_cleanup (struct iova_allocator *allocator); -iova_t iova_allocator_alloc (struct iova_allocator *allocator,size_t size); - -void vfio_pci_config_access (struct vfio_pci_device *device,bool write, -size_t config,size_t size,void *data); - -#define vfio_pci_config_read (_device, _offset, _type) ({ \ - _type __data; \ - vfio_pci_config_access((_device), false, _offset, sizeof(__data), &__data); \ - __data; \ -}) - -#define vfio_pci_config_readb (_d, _o) vfio_pci_config_read(_d, _o, u8) -#define vfio_pci_config_readw (_d, _o) vfio_pci_config_read(_d, _o, u16) -#define vfio_pci_config_readl (_d, _o) vfio_pci_config_read(_d, _o, u32) - -#define vfio_pci_config_write (_device, _offset, _value, _type) do { \ - _type __data = (_value); \ - vfio_pci_config_access((_device), true, _offset, sizeof(_type), &__data); \ -} while (0) - -#define vfio_pci_config_writeb (_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u8) -#define vfio_pci_config_writew (_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u16) -#define vfio_pci_config_writel (_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u32) - -void vfio_pci_irq_enable (struct vfio_pci_device *device,u32 index, -u32 vector,int count); -void vfio_pci_irq_disable (struct vfio_pci_device *device,u32 index); -void vfio_pci_irq_trigger (struct vfio_pci_device *device,u32 index,u32 vector); - -static inline void fcntl_set_nonblock (int fd) -{ -int r; - - r =fcntl (fd,F_GETFL ,0 ); -VFIO_ASSERT_NE (r,-1 ,"F_GETFL failed for fd %d\n" , fd); - - r =fcntl (fd,F_SETFL , r |O_NONBLOCK ); -VFIO_ASSERT_NE (r,-1 ,"F_SETFL O_NONBLOCK failed for fd %d\n" , fd); -} - -static inline void vfio_pci_msi_enable (struct vfio_pci_device *device, -u32 vector,int count) -{ -vfio_pci_irq_enable (device,VFIO_PCI_MSI_IRQ_INDEX , vector, count); -} - -static inline void vfio_pci_msi_disable (struct vfio_pci_device *device) -{ -vfio_pci_irq_disable (device,VFIO_PCI_MSI_IRQ_INDEX ); -} - -static inline void vfio_pci_msix_enable (struct vfio_pci_device *device, -u32 vector,int count) -{ -vfio_pci_irq_enable (device,VFIO_PCI_MSIX_IRQ_INDEX , vector, count); -} - -static inline void vfio_pci_msix_disable (struct vfio_pci_device *device) -{ -vfio_pci_irq_disable (device,VFIO_PCI_MSIX_IRQ_INDEX ); -} - -static inline iova_t __to_iova (struct vfio_pci_device *device,void *vaddr) -{ -return __iommu_hva2iova (device->iommu, vaddr); -} - -static inline iova_t to_iova (struct vfio_pci_device *device,void *vaddr) -{ -return iommu_hva2iova (device->iommu, vaddr); -} - -static inline bool vfio_pci_device_match (struct vfio_pci_device *device, -u16 vendor_id,u16 device_id) -{ -return (vendor_id ==vfio_pci_config_readw (device,PCI_VENDOR_ID )) && - (device_id ==vfio_pci_config_readw (device,PCI_DEVICE_ID )); -} - -void vfio_pci_driver_probe (struct vfio_pci_device *device); -void vfio_pci_driver_init (struct vfio_pci_device *device); -void vfio_pci_driver_remove (struct vfio_pci_device *device); -int vfio_pci_driver_memcpy (struct vfio_pci_device *device, -iova_t src,iova_t dst,u64 size); -void vfio_pci_driver_memcpy_start (struct vfio_pci_device *device, -iova_t src,iova_t dst,u64 size, -u64 count); -int vfio_pci_driver_memcpy_wait (struct vfio_pci_device *device); -void vfio_pci_driver_send_msi (struct vfio_pci_device *device); -#endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H */
tools/testing/selftests/vfio/lib/include/libvfio/assert.h new file +54 -0
54 lines not on this page · raw message
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h new file +77 -0
77 lines not on this page · raw message
tools/testing/selftests/vfio/lib/include/libvfio/iova_allocator.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_IOVA_ALLOCATOR_H +#define SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_IOVA_ALLOCATOR_H + +#include <uapi/linux/types.h> +#include <linux/list.h> +#include <linux/types.h> +#include <linux/iommufd.h> + +#include <libvfio/iommu.h> + +struct iova_allocator { +struct iommu_iova_range *ranges; +u32 nranges; +u32 range_idx; +u64 range_offset; +}; + +struct iova_allocator *iova_allocator_init (struct iommu *iommu); +void iova_allocator_cleanup (struct iova_allocator *allocator); +iova_t iova_allocator_alloc (struct iova_allocator *allocator,size_t size); + +#endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_IOVA_ALLOCATOR_H */
tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h new file +125 -0
125 lines not on this page · raw message
tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_driver.h new file +97 -0
97 lines not on this page · raw message
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerPATCH v3 17/18 vfio: selftests: Eliminate INVALID_IOVA
In reply to David MatlackEliminate INVALID_IOVA as there are platforms where UINT64_MAX is a valid iova.
--- .../selftests/vfio/lib/include/libvfio/iommu.h | 3 +-- .../vfio/lib/include/libvfio/vfio_pci_device.h | 4 ++-- tools/testing/selftests/vfio/lib/iommu.c | 14 +++++++++----- .../testing/selftests/vfio/vfio_dma_mapping_test.c | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +1 -2
@@ -8,7 +8,6 @@#include <libvfio/assert.h> typedef u64 iova_t ; -#define INVALID_IOVA UINT64_MAXstruct iommu_mode {const char *name;
@@ -57,7 +56,7 @@ static inline void iommu_unmap_all(struct iommu *iommu)VFIO_ASSERT_EQ (__iommu_unmap_all (iommu,NULL ),0 ); } -iova_t __iommu_hva2iova (struct iommu *iommu,void *vaddr); +int __iommu_hva2iova (struct iommu *iommu,void *vaddr,iova_t *iova);iova_t iommu_hva2iova (struct iommu *iommu,void *vaddr);struct iommu_iova_range *iommu_iova_ranges (struct iommu *iommu,u32 *nranges);
tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +2 -2
@@ -103,9 +103,9 @@ static inline void vfio_pci_msix_disable(struct vfio_pci_device *device)vfio_pci_irq_disable (device,VFIO_PCI_MSIX_IRQ_INDEX ); } -static inline iova_t __to_iova (struct vfio_pci_device *device,void *vaddr) +static inline int __to_iova (struct vfio_pci_device *device,void *vaddr,iova_t *iova) { -return __iommu_hva2iova (device->iommu, vaddr); +return __iommu_hva2iova (device->iommu, vaddr, iova); }static inline iova_t to_iova (struct vfio_pci_device *device,void *vaddr)
tools/testing/selftests/vfio/lib/iommu.c +9 -5
@@ -67,7 +67,7 @@ static const struct iommu_mode *lookup_iommu_mode(const char *iommu_mode)VFIO_FAIL ("Unrecognized IOMMU mode: %s\n" , iommu_mode); } -iova_t __iommu_hva2iova (struct iommu *iommu,void *vaddr) +int __iommu_hva2iova (struct iommu *iommu,void *vaddr,iova_t *iova) {struct dma_region *region;
@@ -78,18 +78,22 @@ iova_t __iommu_hva2iova(struct iommu *iommu, void *vaddr)if (vaddr >= region->vaddr + region->size)continue ; -return region->iova + (vaddr - region->vaddr); +if (iova) + *iova = region->iova + (vaddr - region->vaddr); + +return 0 ; } -return INVALID_IOVA ; +return -ENOENT ; }iova_t iommu_hva2iova (struct iommu *iommu,void *vaddr) {iova_t iova; +int ret; - iova =__iommu_hva2iova (iommu, vaddr); -VFIO_ASSERT_NE (iova,INVALID_IOVA ,"%p is not mapped into IOMMU\n" , vaddr); + ret =__iommu_hva2iova (iommu, vaddr, &iova); +VFIO_ASSERT_EQ (ret,0 ,"%p is not mapped into the iommu\n" , vaddr);return iova; }
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +1 -1
@@ -199,7 +199,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)ASSERT_EQ (rc,0 );ASSERT_EQ (unmapped, region.size);printf ("Unmapped IOVA 0x%lx\n" , region.iova); -ASSERT_EQ (INVALID_IOVA ,__to_iova (self->device, region.vaddr)); +ASSERT_NE (0 ,__to_iova (self->device, region.vaddr,NULL ));ASSERT_NE (0 ,iommu_mapping_get (device_bdf, region.iova, &mapping));ASSERT_TRUE (!munmap (region.vaddr, size));
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
David Matlack
maintainerPATCH v3 18/18 vfio: selftests: Add vfio_pci_device_init_perf_test
In reply to David MatlackAdd a new VFIO selftest for measuring the time it takes to run vfio_pci_device_init() in parallel for one or more devices.
This test serves as manual regression test for the performance improvement of commit e908f58b6beb ("vfio/pci: Separate SR-IOV VF dev_set"). For example, when running this test with 64 VFs under the same PF:
Before:
$ ./vfio_pci_device_init_perf_test -r vfio_pci_device_init_perf_test.iommufd.init 0000:1a:00.0 0000:1a:00.1 ... ... Wall time: 6.653234463s Min init time (per device): 0.101215344s Max init time (per device): 6.652755941s Avg init time (per device): 3.377609608s
After:
$ ./vfio_pci_device_init_perf_test -r vfio_pci_device_init_perf_test.iommufd.init 0000:1a:00.0 0000:1a:00.1 ... ... Wall time: 0.122978332s Min init time (per device): 0.108121915s Max init time (per device): 0.122762761s Avg init time (per device): 0.113816748s
This test does not make any assertions about performance, since any such assertion is likely to be flaky due to system differences and random noise. However this test can be fed into automation to detect regressions, and can be used by developers in the future to measure performance optimizations.
--- tools/testing/selftests/vfio/Makefile | 3 + .../vfio/vfio_pci_device_init_perf_test.c | 168 ++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_pci_device_init_perf_test.c
tools/testing/selftests/vfio/Makefile +3 -0
@@ -2,6 +2,7 @@ CFLAGS = $(KHDR_INCLUDES) TEST_GEN_PROGS += vfio_dma_mapping_test TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test +TEST_GEN_PROGS += vfio_pci_device_init_perf_test TEST_GEN_PROGS += vfio_pci_driver_test TEST_FILES += scripts/cleanup.sh
@@ -16,6 +17,8 @@ CFLAGS += -I$(top_srcdir)/tools/include CFLAGS += -MD CFLAGS += $(EXTRA_CFLAGS) +LDFLAGS += -pthread + $(TEST_GEN_PROGS): %: %.o $(LIBVFIO_O) $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIBVFIO_O) $(LDLIBS) -o $@
tools/testing/selftests/vfio/vfio_pci_device_init_perf_test.c new file +168 -0
168 lines not on this page · raw message
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Quoted 122 lines · David Matlack
This series adds support for tests that use multiple devices, and adds one new test,
vfio_pci_device_init_perf_test, which measures parallel device initialization time to demonstrate the improvement from commite908f58b6beb("vfio/pci: Separate SR-IOV VFdev_set").This series also breaks apart the monolithic
vfio_util.handvfio_pci_device.cinto separate files, to account for all the new code. This required quite a bit of code motion so the diffstat looks large. The final layout is more granular and provides a better separation of the IOMMU code from the device code.Final layout:
C files:
-tools/testing/selftests/vfio/lib/libvfio.c
-tools/testing/selftests/vfio/lib/iommu.c
-tools/testing/selftests/vfio/lib/iova_allocator.c
-tools/testing/selftests/vfio/lib/vfio_pci_device.c
-tools/testing/selftests/vfio/lib/vfio_pci_driver.cH files:
-tools/testing/selftests/vfio/lib/include/libvfio.h
-tools/testing/selftests/vfio/lib/include/libvfio/assert.h
-tools/testing/selftests/vfio/lib/include/libvfio/iommu.h
-tools/testing/selftests/vfio/lib/include/libvfio/iova_allocator.h
-tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h
-tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_driver.hNotably,
vfio_util.his now gone and replaced withlibvfio.h.This series is based on vfio/next plus Alex Mastro's series to add the IOVA allocator [1]. It should apply cleanly to vfio/next once Alex's series is merged by Linus into the next 6.18 rc and then merged into vfio/next.
This series can be found on GitHub:
https://github.com/dmatlack/linux/tree/vfio/selftests/init_perf_test/v3
[1] https://lore.kernel.org/kvm/[email protected]/
Cc Alex Mastro <[email protected]>Cc Jason Gunthorpe <[email protected]>Cc Josh Hilke <[email protected]>Cc Raghavendra Rao Ananta <[email protected]>Cc Vipin Sharma <[email protected]>v3:
- Replace literal withNSEC_PER_SEC(Alex Mastro)
- FixMakefileaccumulate vs. assignment (Alex Mastro)v2: https://lore.kernel.org/kvm/[email protected]/
v1: https://lore.kernel.org/kvm/[email protected]/
David Matlack (18):
vfio: selftests: Moverun.shinto scripts directory
vfio: selftests: Splitrun.shinto separate scripts
vfio: selftests: Allow passing multiple BDFs on the command line
vfio: selftests: Rename structvfio_iommu_modetoiommu_mode
vfio: selftests: Introduce struct iommu
vfio: selftests: Support multiple devices in the same
container/iommufd
vfio: selftests: Eliminate overly chatty logging
vfio: selftests: Prefix logs with device BDF where relevant
vfio: selftests: Upgrade driver logging todev_err()
vfio: selftests: Rename structvfio_dma_regiontodma_region
vfio: selftests: Move IOMMU library code intoiommu.c
vfio: selftests: Move IOVA allocator intoiova_allocator.c
vfio: selftests: Stop passing device for IOMMU operations
vfio: selftests: Renamevfio_util.htolibvfio.h
vfio: selftests: Move vfio_selftests_*() helpers intolibvfio.c
vfio: selftests: Splitlibvfio.hinto separate header files
vfio: selftests: EliminateINVALID_IOVA
vfio: selftests: Addvfio_pci_device_init_perf_testtools/testing/selftests/vfio/Makefile | 10 +- .../selftests/vfio/lib/drivers/dsa/dsa.c | 36 +- .../selftests/vfio/lib/drivers/ioat/ioat.c | 18 +- .../selftests/vfio/lib/include/libvfio.h | 26 + .../vfio/lib/include/libvfio/assert.h | 54 ++ .../vfio/lib/include/libvfio/iommu.h | 76 +++ .../vfio/lib/include/libvfio/iova_allocator.h | 23 + .../lib/include/libvfio/vfio_pci_device.h | 125 ++++ .../lib/include/libvfio/vfio_pci_driver.h | 97 +++ .../selftests/vfio/lib/include/vfio_util.h | 331 ----------- tools/testing/selftests/vfio/lib/iommu.c | 465 +++++++++++++++ .../selftests/vfio/lib/iova_allocator.c | 94 +++ tools/testing/selftests/vfio/lib/libvfio.c | 78 +++ tools/testing/selftests/vfio/lib/libvfio.mk | 5 +- .../selftests/vfio/lib/vfio_pci_device.c | 555 +----------------- .../selftests/vfio/lib/vfio_pci_driver.c | 16 +- tools/testing/selftests/vfio/run.sh | 109 ---- .../testing/selftests/vfio/scripts/cleanup.sh | 41 ++ tools/testing/selftests/vfio/scripts/lib.sh | 42 ++ tools/testing/selftests/vfio/scripts/run.sh | 16 + tools/testing/selftests/vfio/scripts/setup.sh | 48 ++ .../selftests/vfio/vfio_dma_mapping_test.c | 46 +- .../selftests/vfio/vfio_iommufd_setup_test.c | 2 +- .../vfio/vfio_pci_device_init_perf_test.c | 168 ++++++ .../selftests/vfio/vfio_pci_device_test.c | 12 +- .../selftests/vfio/vfio_pci_driver_test.c | 51 +- 26 files changed, 1481 insertions(+), 1063 deletions(-) create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio.h create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/assert.h create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/iommu.h create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/iova_allocator.h create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_driver.h delete mode 100644 tools/testing/selftests/vfio/lib/include/vfio_util.h create mode 100644 tools/testing/selftests/vfio/lib/iommu.c create mode 100644 tools/testing/selftests/vfio/lib/iova_allocator.c create mode 100644 tools/testing/selftests/vfio/lib/libvfio.c delete mode 100755 tools/testing/selftests/vfio/run.sh create mode 100755 tools/testing/selftests/vfio/scripts/cleanup.sh create mode 100755 tools/testing/selftests/vfio/scripts/lib.sh create mode 100755 tools/testing/selftests/vfio/scripts/run.sh create mode 100755 tools/testing/selftests/vfio/scripts/setup.sh create mode 100644 tools/testing/selftests/vfio/vfio_pci_device_init_perf_test.c
Quoted 8 lines
base-commit:
fa804aa4ac1b091ef2ec2981f08a1c28aaeba8e7
prerequisite-patch-id:dcf23dcc1198960bda3102eefaa21df60b2e4c54
prerequisite-patch-id:e32e56d5bf7b6c7dd40d737aa3521560407e00f5
prerequisite-patch-id:4f79a41bf10a4c025ba5f433551b46035aa15878
prerequisite-patch-id:f903a45f0c32319138cd93a007646ab89132b18c--
2.52.0.rc2.455.g230fcf2819-goog
Apart from a couple of minor nits in patch-6:
David Matlack
maintainerQuoted 10 lines · Raghavendra Rao Ananta
Quoted 6 lines · David Matlack
This series adds support for tests that use multiple devices, and adds one new test,
vfio_pci_device_init_perf_test, which measures parallel device initialization time to demonstrate the improvement from commite908f58b6beb("vfio/pci: Separate SR-IOV VFdev_set").Apart from a couple of minor nits in patch-6:
Reviewed-by Raghavendra Rao Ananta <[email protected]>
Thanks Raghu!
I'll send a v4 addressing your comments and the kernel-test-robot issues on riscv and i386. And then hopefully we can get this into 6.19.