Introduce vfio_dma_mapping_perf_test
Subsystems (4)
cover Introduce vfio_dma_mapping_perf_test
Add a new VFIO selftest, vfio_dma_mapping_perf_test, to provide a configurable tool for measuring DMA mapping and unmapping latencies.
This selftest introduces command-line parameters to provide a convenient way to tune the test for specific situations. For example, it can be used to observe the lengthy unmap times of the Type 1 IOMMU on large memory regions, or to compare DMA mapping performance between the Type 1 IOMMU and IOMMUFD. These scenarios can now be easily configured and run via the command line.
During development, I abandoned an approach where I initialized memfd globally in main() for all tests. While that allowed memfd to be initialized once for all test variants, it proved awkward because a different memfd would be needed for each supported page size (e.g., 4K, 2M, 1G). Also, If a user is only running a single test variant, pre-allocating all sizes is unnecessary overhead. Therefore, I dropped that approach in favor of test-local allocation. However, I'm open to ideas on a better way to handle this.
Aaron Lewis (4):
vfio: selftests: Introduce vfio_dma_mapping_perf_test
vfio: selftests: Add memfd test to vfio_dma_mapping_perf_test
vfio: selftests: Allow a size for vfio_dma_mapping_perf_test
vfio: selftests: Allow the flag MAP_POPULATE to be set on the cmdline
tools/testing/selftests/vfio/Makefile | 1 + .../vfio/lib/include/libvfio/iommu.h | 7 + tools/testing/selftests/vfio/lib/iommu.c | 24 ++ .../vfio/vfio_dma_mapping_perf_test.c | 380 ++++++++++++++++++ 4 files changed, 412 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
PATCH 1/4 vfio: selftests: Introduce vfio_dma_mapping_perf_test
Introduce a tool to aid in latency testing rather than a traditional pass/fail test. The goal is to make the test configurable to expose latency issues and make them easy to reproduce and share.
Start with a basic test that performs a simple DMA map/unmap and reports the duration of each operation. Report the following four metrics:
1. Time taken by mmap().
2. Time taken to map a DMA region.
3. Time taken to unmap a DMA region.
4. Time taken by munmap().
Subsequent commits in this series will introduce more features to allow for a wider variety of testing.
--- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/vfio_dma_mapping_perf_test.c | 124 ++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c
tools/testing/selftests/vfio/Makefile +1 -0
@@ -8,6 +8,7 @@ else CFLAGS = $(KHDR_INCLUDES) TEST_GEN_PROGS += vfio_dma_mapping_test TEST_GEN_PROGS += vfio_dma_mapping_mmio_test +TEST_GEN_PROGS += vfio_dma_mapping_perf_test TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test TEST_GEN_PROGS += vfio_pci_device_init_perf_test
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c new file +124 -0
124 lines not on this page · raw message
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
PATCH 2/4 vfio: selftests: Add memfd test to vfio_dma_mapping_perf_test
In reply to Aaron LewisAdd a second test to "vfio_dma_mapping_perf_test" to evaluate memfd latencies. A key motivator for adding this test is to demonstrate that the IOMMU_IOAS_MAP_FILE ioctl is significantly faster than other methods of mapping DMA regions. While this performance difference is not fully apparent with the test's current capabilities, it will become evident as more features are introduced later in the series.
To support this, add IOMMU_IOAS_MAP_FILE ioctl support to the VFIO selftest library via iommufd_map_file().
--- .../vfio/lib/include/libvfio/iommu.h | 7 ++ tools/testing/selftests/vfio/lib/iommu.c | 24 ++++ .../vfio/vfio_dma_mapping_perf_test.c | 118 ++++++++++++++++++ 3 files changed, 149 insertions(+)
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +7 -0
@@ -35,6 +35,13 @@ struct iommu {struct iommu *iommu_init (const char *iommu_mode);void iommu_cleanup (struct iommu *iommu); +int __iommufd_map_file (struct iommu *iommu,struct dma_region *region,int fd); + +static inline void iommufd_map_file (struct iommu *iommu,struct dma_region *region,int fd) +{ +VFIO_ASSERT_EQ (__iommufd_map_file (iommu, region, fd),0 ); +} +int __iommu_map (struct iommu *iommu,struct dma_region *region);static inline void iommu_map (struct iommu *iommu,struct dma_region *region)
tools/testing/selftests/vfio/lib/iommu.c +24 -0
@@ -97,6 +97,30 @@ iova_t iommu_hva2iova(struct iommu *iommu, void *vaddr)return iova; } +int __iommufd_map_file (struct iommu *iommu,struct dma_region *region,int fd) +{ +VFIO_ASSERT_TRUE (iommu->iommufd,"IOMMU_IOAS_MAP_FILE is an IOMMUFD IOCTL." ); + +struct iommu_ioas_map_file args = { + .size =sizeof (args), + .flags =IOMMU_IOAS_MAP_READABLE | +IOMMU_IOAS_MAP_WRITEABLE | +IOMMU_IOAS_MAP_FIXED_IOVA , + .ioas_id = iommu->ioas_id, + .fd = fd, + .start =0 , + .iova = region->iova, + .length = region->size, + }; + +if (ioctl (iommu->iommufd,IOMMU_IOAS_MAP_FILE , &args)) +return -errno; + +list_add (®ion->link, &iommu->dma_regions); + +return 0 ; +} +static int vfio_iommu_map (struct iommu *iommu,struct dma_region *region) {struct vfio_iommu_type1_dma_map args = {
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +118 -0
124 lines not on this page · raw message
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
PATCH 3/4 vfio: selftests: Allow a size for vfio_dma_mapping_perf_test
In reply to Aaron LewisAllow the user to specify a DMA region size via the command line for vfio_dma_mapping_perf_test.
Because the selftest harness also parses command-line parameters, sharing them directly is problematic. Adding options directly to the test could create conflicts with harness-defined options. Even without conflicts, the harness would need to be updated to recognize test-specific options to avoid failing on unknown parameters.
Resolve this by isolating the two sets of parameters. The standard command-line options are consumed by the test itself. To pass options through to the test harness, introduce a new '-a' option.
For example, both the test size and the test harness options can be set like this:
./vfio_dma_mapping_perf_test -b 16G -a "-v vfio_type1_iommu_memfd_hugetlb_1gb"
This invocation configures a 16G DMA region and restricts execution to the specified test variant, which is useful when debugging DMA mapping latency issues for a specific IOMMU type.
--- .../vfio/vfio_dma_mapping_perf_test.c | 132 +++++++++++++++++- 1 file changed, 129 insertions(+), 3 deletions(-)
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +129 -3
@@ -4,6 +4,7 @@#include <sys/mman.h> #include <time.h> #include <unistd.h> +#include <wordexp.h> #include <linux/iommufd.h> #include <linux/limits.h>
@@ -19,6 +20,12 @@static const char *device_bdf; +struct test_params { +u64 size; +}; + +struct test_params test_params; +struct iommu_mapping {u64 pgd;u64 p4d;
@@ -80,7 +87,7 @@ FIXTURE_TEARDOWN(vfio_dma_mapping_perf_test) TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap) { -const u64 size =SZ_1G ; +const u64 size = test_params.size;const int flags = variant->mmap_flags;struct dma_region region;struct timespec start;
@@ -204,7 +211,7 @@ static void teardown_memfd(int fd, u64 size, void *vaddr) TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) { -const u64 size =SZ_1G ; +const u64 size = test_params.size;struct dma_region region;struct timespec start;u64 unmapped;
@@ -235,8 +242,127 @@ TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file)teardown_memfd (fd, size, region.vaddr); } +/* +* Parses "[0-9]+[kmgt]?". +*/ +size_t parse_size (const char *size) +{ +size_t base; +char *scale; +int shift =0 ; + +VFIO_ASSERT_TRUE (size &&isdigit (size[0 ]), +"Need at least one digit in '%s'." , size); + + base =strtoull (size, &scale,0 ); + +VFIO_ASSERT_TRUE (base !=ULLONG_MAX ,"Overflow parsing size!" ); + +switch (tolower (*scale)) { +case 't' : + shift =40 ; +break ; +case 'g' : + shift =30 ; +break ; +case 'm' : + shift =20 ; +break ; +case 'k' : + shift =10 ; +break ; +case 'b' : +case '\0' : + shift =0 ; +break ; +default : +VFIO_FAIL ("Unknown size letter '%c'." , *scale); + } + +VFIO_ASSERT_TRUE ((base << shift) >> shift == base, +"Overflow scaling size!" ); + +return base << shift; +} + +static void help (char *name) +{ +puts ("" ); +printf ("usage: %s [-h] [-b bytes] [-a \"test harness args\"]\n" , name); +puts ("" ); +printf (" -h: Display this help message.\n" +" -b: Specify the size of the DMA region to be mapped\n" +" and unmapped. e.g. 16M or 8G, (default: 1G)\n" +" -a: Args that are forwarded to the test harness,\n" +" e.g. -a \"-t dma_map_unmap_from_file\"\n" ); +} + +struct harness_args +{ +int argc; +char **argv; +wordexp_t exp; +}; + +static void populate_harness_args (struct harness_args *args,const char *argv_0, +const char *cmdlne) +{ +if (wordexp (argv_0, &args->exp,WRDE_NOCMD ) ==0 && +wordexp (cmdlne, &args->exp,WRDE_APPEND |WRDE_NOCMD ) ==0 ) { + args->argc = args->exp.we_wordc; + args->argv = args->exp.we_wordv; + } +} + +static void setup_test (struct harness_args *args,int argc,char *argv[]) +{ +int opt; + + test_params = (struct test_params ) { + .size =SZ_1G , + }; + +while ((opt =getopt (argc, argv,"a:b:h" )) !=-1 ) { +switch (opt) { +case 'a' : +populate_harness_args (args, argv[0 ], optarg); +break ; +case 'b' : + test_params.size =parse_size (optarg); +break ; +case 'h' : +default : +help (argv[0 ]); + goto out; + } + } + +out: +// Reset getopt() state to allow the test harness to use it. + optind =1 ; +} + +static void teardown_test (struct harness_args *args) +{ +if (args->argv) { + args->argc =0 ; + args->argv =NULL ; +wordfree (&args->exp); + } +} +int main (int argc,char *argv[]) { +struct harness_args args = (struct harness_args ) { + .argc =0 , + .argv =NULL , + }; +int r; + +setup_test (&args, argc, argv); device_bdf =vfio_selftests_get_bdf (&argc, argv); -return test_harness_run (argc, argv); + r =test_harness_run (args.argc, args.argv); +teardown_test (&args); + +return r; }
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
PATCH 4/4 vfio: selftests: Allow the flag MAP_POPULATE to be set on the cmdline
In reply to Aaron LewisAdd a command-line option to vfio_dma_mapping_perf_test to set the MAP_POPULATE flag during mmap(). This ensures consistent and accurate DMA mapping latency measurements by forcing page prefaulting to occur during the mmap() phase, rather than during the DMA mapping ioctl.
For example, mapping a 32G region without the -p (MAP_POPULATE) flag results in:
$ ./vfio_dma_mapping_perf_test -b 32G -a "-v vfio_type1_iommu_memfd_hugetlb_1gb" Completed mmap() for memfd in 0.00ms Mapped HVA 0x7f1840000000 (size 32G) at IOVA 0x800000000 in 2887.56ms
Here, the mmap() call completes instantly, but the ioctl to map the DMA region incurs the ~3s cost of prefaulting the pages.
Running the same test with the -p flag results in:
$ ./vfio_dma_mapping_perf_test -p -b 32G -a "-v vfio_type1_iommu_memfd_hugetlb_1gb" Completed mmap() for memfd in 2887.41ms Mapped HVA 0x7f8000000000 (size 32G) at IOVA 0x800000000 in 11.51ms
By shifting the prefaulting overhead to mmap(), the actual DMA mapping latency is isolated and accurately reported as 11.51ms.
With user-defined mapping sizes and prefaulting in place, this test now clearly demonstrates the difference in mapping speeds between VFIO's Type 1 IOMMU and IOMMUFD (i.e., IOMMU_IOAS_MAP_FILE).
Running the same configuration with the "iommufd_memfd_hugetlb_1gb" variant highlights this difference:
$ ./vfio_dma_mapping_perf_test -p -b 32G -a "-v iommufd_memfd_hugetlb_1gb" Mapped HVA 0x7f4fc0000000 (size 32G) at IOVA 0x800000000 in 0.11ms
The DMA mapping time drops dramatically from 11.51ms to 0.11ms.
--- .../vfio/vfio_dma_mapping_perf_test.c | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +16 -4
@@ -22,6 +22,7 @@ static const char *device_bdf;struct test_params {u64 size; +int mmap_flags; };struct test_params test_params;
@@ -88,7 +89,7 @@ FIXTURE_TEARDOWN(vfio_dma_mapping_perf_test) TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap) {const u64 size = test_params.size; -const int flags = variant->mmap_flags; +const int flags = variant->mmap_flags | test_params.mmap_flags;struct dma_region region;struct timespec start;u64 unmapped;
@@ -217,7 +218,9 @@ TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file)u64 unmapped;int rc, fd; - region.vaddr =setup_memfd (&fd, size, variant->mmap_flags, variant->memfd_flags); + region.vaddr =setup_memfd (&fd, size, + variant->mmap_flags | test_params.mmap_flags, + variant->memfd_flags);ASSERT_NE (region.vaddr,MAP_FAILED ); region.iova =iova_allocator_alloc (self->iova_allocator, size);
@@ -288,11 +291,16 @@ size_t parse_size(const char *size)static void help (char *name) {puts ("" ); -printf ("usage: %s [-h] [-b bytes] [-a \"test harness args\"]\n" , name); +printf ("usage: %s [-h|-p] [-b bytes] [-a \"test harness args\"]\n" , name);puts ("" );printf (" -h: Display this help message.\n" " -b: Specify the size of the DMA region to be mapped\n" " and unmapped. e.g. 16M or 8G, (default: 1G)\n" +" -p: Append 'MAP_POPULATE' to the mmap() flags to avoid\n" +" prefaulting while mapping DMA regions. Instead, any\n" +" and all prefaulting needed will happen during the\n" +" mmap() call. This will make mapping DMA regions\n" +" more consistent.\n" " -a: Args that are forwarded to the test harness,\n" " e.g. -a \"-t dma_map_unmap_from_file\"\n" ); }
@@ -320,9 +328,10 @@ static void setup_test(struct harness_args *args, int argc, char *argv[]) test_params = (struct test_params ) { .size =SZ_1G , + .mmap_flags =0 , }; -while ((opt =getopt (argc, argv,"a:b:h" )) !=-1 ) { +while ((opt =getopt (argc, argv,"a:b:ph" )) !=-1 ) {switch (opt) {case 'a' :populate_harness_args (args, argv[0 ], optarg);
@@ -330,6 +339,9 @@ static void setup_test(struct harness_args *args, int argc, char *argv[])case 'b' : test_params.size =parse_size (optarg);break ; +case 'p' : + test_params.mmap_flags =MAP_POPULATE ; +break ;case 'h' :default :help (argv[0 ]);
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
[email protected]
AI reviewRe: [PATCH 2/4] vfio: selftests: Add memfd test to vfio_dma_mapping_perf_test
In reply to Aaron LewisThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] Missing SKIP fallback for hugepage allocation failures. - [Low] MAP_SHARED (an mmap flag) is…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Missing SKIP fallback for hugepage allocation failures.
- [Low] MAP_SHARED (an mmap flag) is incorrectly passed to memfd_create() via memfd_flags.
- [Low] memfd_create() return value is incorrectly validated against <= 0, which rejects fd 0 and leaks it. --
commit ffa43469ab562932001b32361e9549a362e44c3c
Author: Aaron Lewis <[email protected]>
vfio: selftests: Add memfd test to vfio_dma_mapping_perf_test
Add a second test to "vfio_dma_mapping_perf_test" to evaluate memfd latencies. A key motivator for adding this test is to demonstrate that the IOMMU_IOAS_MAP_FILE ioctl is significantly faster than other methods of mapping DMA regions.
Quoted 4 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +0
-0
[ ... ]
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap) · line 136
Quoted 5 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +5
-0+#define FIXTURE_VARIANT_ADD_IOMMU_MODE (_iommu_mode, _name, _mmap_flags, _memfd_flags) \ +FIXTURE_VARIANT_ADD(vfio_dma_mapping_perf_memfd_test, _iommu_mode ## _ ## _name) { \ + .iommu_mode = #_iommu_mode, \ + .mmap_flags = MAP_SHARED | (_mmap_flags), \ + .memfd_flags = MAP_SHARED | (_memfd_flags), \
[Severity: Low]
Does this incorrectly pass the mmap flag MAP_SHARED to memfd_create() via memfd_flags?
MAP_SHARED (0x01) aliases with MFD_CLOEXEC (0x0001U) in the memfd flag namespace, which silently sets close-on-exec rather than failing at runtime. Could conflating the namespaces here cause confusion?
Quoted 2 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +2
-0+} +
[ ... ]
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap) · line 172
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +10
-0+static void *setup_memfd (int *fd,u64 size,int mmap_flags,int mfd_flags) +{ +void *buf =MAP_FAILED ; +struct timespec start; + +timer_start (&start); + *fd =memfd_create ("vfio_dma_mapping_perf_memfd_test" , mfd_flags); +printf ("Completed memfd_create() in %.2lfms\n" ,timer_elapsed_ms (start)); +if (*fd <=0 ) +return MAP_FAILED ;
[Severity: Low]
Could this leak fd 0 if memfd_create() successfully returns it?
File descriptors can validly be 0 if the test is run with stdin closed. Would it be better to check if (*fd < 0) instead so it doesn't reject fd 0?
Quoted 3 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +3
-0+ +if (ftruncate (*fd, size)) + goto out;
[ ... ]
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap) · line 214
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +10
-0+TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) +{ +const u64 size =SZ_1G ; +struct dma_region region; +struct timespec start; +u64 unmapped; +int rc, fd; + + region.vaddr =setup_memfd (&fd, size, variant->mmap_flags, variant->memfd_flags); +ASSERT_NE (region.vaddr,MAP_FAILED );
[Severity: Medium]
Will this unconditional ASSERT_NE cause the test suite to fail on systems that do not have sufficient hugepages configured (e.g., for MFD_HUGE_1GB or MFD_HUGE_2MB) instead of gracefully skipping the unsupported variants?
Quoted 3 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +3
-0+ + region.iova =iova_allocator_alloc (self->iova_allocator, size); + region.size = size;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH 1/4] vfio: selftests: Introduce vfio_dma_mapping_perf_test
In reply to Aaron LewisThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Format String Mismatch (Undefined Behavior) for 64-bit `u64` values. - [Low] Unused struct…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Format String Mismatch (Undefined Behavior) for 64-bit u64 values.
- [Low] Unused struct iommu_mapping definition.
--
commit ed41e119412fd95d65a62de77183ae8dfc8f2449
Author: Aaron Lewis <[email protected]>
vfio: selftests: Introduce vfio_dma_mapping_perf_test
Introduce a tool to aid in latency testing rather than a traditional pass/fail test. Start with a basic test that performs a simple DMA map/unmap and reports the duration of each operation.
Quoted 7 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c new file +1
-0@@ -0,0 +1,124 @@ +// SPDX-License-Identifier: GPL-2.0-only
[ ... ]
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c line 28
Quoted 9 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c new file +9
-0+static const char *device_bdf; + +struct iommu_mapping { +u64 pgd; +u64 p4d; +u64 pud; +u64 pmd; +u64 pte; +};
[Severity: Low]
This isn't a bug, but struct iommu_mapping is defined and never used in this file. Is it needed, or is it left over from earlier iterations?
[ ... ]
Quoted 8 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c new file +8
-0+TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap) +{ +const u64 size =SZ_1G ; +const int flags = variant->mmap_flags; +struct dma_region region; +struct timespec start; +u64 unmapped; +int rc;
[ ... ]
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c line 106
Quoted 4 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c new file +4
-0+timer_start (&start); +iommu_map (self->iommu, ®ion); +printf ("Mapped HVA %p (size %luG) at IOVA 0x%lx in %.2lfms\n" , + region.vaddr, size /SZ_1G , region.iova,timer_elapsed_ms (start));
[Severity: Medium]
Does this printf format string mismatch cause stack misalignment on 32-bit platforms?
Because size / SZ_1G and region.iova are 64-bit u64 values, using %lu and %lx instead of %llu and %llx can misalign the variadic arguments. On 32-bit systems, %lu expects a 32-bit value, so printf will misread the 64-bit integer, corrupting the subsequent double argument and producing garbage output for timer_elapsed_ms().
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c line 111
Quoted 5 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c new file +5
-0+ASSERT_EQ (region.iova,to_iova (self->device, region.vaddr)); + +timer_start (&start); + rc =__iommu_unmap (self->iommu, ®ion, &unmapped); +printf ("Unmapped IOVA 0x%lx in %.2lfms\n" , region.iova,timer_elapsed_ms (start));
[Severity: Medium]
Similarly here, does region.iova need %llx to avoid stack misalignment issues with the subsequent double on 32-bit platforms?
Quoted 2 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c new file +2
-0+ASSERT_EQ (rc,0 ); +ASSERT_EQ (unmapped, region.size);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH 3/4] vfio: selftests: Allow a size for vfio_dma_mapping_perf_test
In reply to Aaron LewisThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] The -h option and invalid arguments fail to exit the program, causing unintended execution of…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] The -h option and invalid arguments fail to exit the program, causing unintended execution of all tests.
- [Medium] Multiple usages of the '-a' flag overwrite the wordexp structure without WRDE_REUSE, causing memory leaks and dropping previous arguments.
- [Medium] Using size_t for math and return type in parse_size() causes undefined behavior and silent truncation on 32-bit architectures. --
commit d938d0d26cc3c95a10e15ae011fce57f55a320c9
Author: Aaron Lewis <[email protected]>
vfio: selftests: Allow a size for vfio_dma_mapping_perf_test
Allow the user to specify a DMA region size via the command line for vfio_dma_mapping_perf_test.
Quoted 4 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +0
-0
[ ... ]
Quoted 5 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +5
-0+size_t parse_size (const char *size) +{ +size_t base; +char *scale; +int shift =0 ;
[ ... ]
Quoted 4 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +4
-0+switch (tolower (*scale)) { +case 't' : + shift =40 ; +break ;
[ ... ]
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 286
Quoted 5 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +5
-0+VFIO_ASSERT_TRUE ((base << shift) >> shift == base, +"Overflow scaling size!" ); + +return base << shift; +}
[Severity: Medium]
On 32-bit architectures where size_t is 32-bit, will shifting a 32-bit integer by 40 when parsing terabytes cause undefined behavior?
Because compilers can mask over-sized shift counts, the overflow check here might evaluate as true and silently pass while returning a drastically truncated size. Should this function use u64 internally and as its return type to match test_params.size?
[ ... ]
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 311
Quoted 5 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +5
-0+static void populate_harness_args (struct harness_args *args,const char *argv_0, +const char *cmdlne) +{ +if (wordexp (argv_0, &args->exp,WRDE_NOCMD ) ==0 && +wordexp (cmdlne, &args->exp,WRDE_APPEND |WRDE_NOCMD ) ==0 ) {
[Severity: Medium]
If a user passes multiple '-a' flags, does the first wordexp() call without WRDE_REUSE or WRDE_APPEND leak the previously allocated memory in args->exp?
This also looks like it would discard any arguments appended by earlier '-a' flags, and potentially leave args->argv pointing to leaked allocations if the second wordexp() fails.
Quoted 15 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +15
-0+ args->argc = args->exp.we_wordc; + args->argv = args->exp.we_wordv; + } +} + +static void setup_test (struct harness_args *args,int argc,char *argv[]) +{ +int opt; + + test_params = (struct test_params ) { + .size =SZ_1G , + }; + +while ((opt =getopt (argc, argv,"a:b:h" )) !=-1 ) { +switch (opt) {
[ ... ]
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 343
Quoted 11 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +11
-0+case 'h' : +default : +help (argv[0 ]); + goto out; + } + } + +out: +// Reset getopt() state to allow the test harness to use it. + optind =1 ; +}
[Severity: Medium]
When the '-h' option or an invalid argument is provided, does jumping to 'out' unintentionally allow the program to continue executing?
Because setup_test() returns normally to main() in this path, main() will subsequently execute test_harness_run(), which runs the full test suite immediately after printing the help message instead of terminating.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
David Matlack
maintainerRe: [PATCH 4/4] vfio: selftests: Allow the flag MAP_POPULATE to be set on the cmdline
In reply to Aaron LewisQuoted 31 lines · Aaron Lewis
Add a command-line option to
vfio_dma_mapping_perf_testto set theMAP_POPULATEflag duringmmap(). This ensures consistent and accurate DMA mapping latency measurements by forcing page prefaulting to occur during themmap()phase, rather than during the DMA mapping ioctl.For example, mapping a 32G region without the -p (
MAP_POPULATE) flag results in:$ ./vfio_dma_mapping_perf_test -b 32G -a "-v vfio_type1_iommu_memfd_hugetlb_1gb" Completed mmap() for memfd in 0.00ms Mapped HVA 0x7f1840000000 (size 32G) at IOVA 0x800000000 in 2887.56msHere, the
mmap()call completes instantly, but the ioctl to map the DMA region incurs the ~3s cost of prefaulting the pages.Running the same test with the -p flag results in:
$ ./vfio_dma_mapping_perf_test -p -b 32G -a "-v vfio_type1_iommu_memfd_hugetlb_1gb" Completed mmap() for memfd in 2887.41ms Mapped HVA 0x7f8000000000 (size 32G) at IOVA 0x800000000 in 11.51msBy shifting the prefaulting overhead to
mmap(), the actual DMA mapping latency is isolated and accurately reported as 11.51ms.With user-defined mapping sizes and prefaulting in place, this test now clearly demonstrates the difference in mapping speeds between VFIO's Type 1 IOMMU and IOMMUFD (i.e.,
IOMMU_IOAS_MAP_FILE).Running the same configuration with the "
iommufd_memfd_hugetlb_1gb" variant highlights this difference:
Quoted 6 lines
$ ./vfio_dma_mapping_perf_test -p -b 32G -a "-v iommufd_memfd_hugetlb_1gb" Mapped HVA 0x7f4fc0000000 (size 32G) at IOVA 0x800000000 in 0.11msThe DMA mapping time drops dramatically from 11.51ms to 0.11ms.
Signed-off-by Aaron Lewis <[email protected]>
IMO we should either:
1. Only support the MAP_POPULATE variant since that isolates the VFIO and IOMMUFD performance from allocating the backing memory and aligns with how a proper userspace should be designed.
or
2. Make MAP_POPULATE toggle a test fixture variant so that we automatically test both scenarios without needing a command line flag. If the user wants to test just one scenario they can do so via kselftest harness -t flag.
Either way, we shouldn't need a command line flag for this.
I don't have a strong opinion between the two but lean towards (2) until we have a compelling reason to care about the performance of VFIO and IOMMUFD triggering the backing memory allocations.
David Matlack
maintainerRe: [PATCH 4/4] vfio: selftests: Allow the flag MAP_POPULATE to be set on the cmdline
Quoted 53 lines · David Matlack
Quoted 31 lines · Aaron Lewis
Add a command-line option to
vfio_dma_mapping_perf_testto set theMAP_POPULATEflag duringmmap(). This ensures consistent and accurate DMA mapping latency measurements by forcing page prefaulting to occur during themmap()phase, rather than during the DMA mapping ioctl.For example, mapping a 32G region without the -p (
MAP_POPULATE) flag results in:$ ./vfio_dma_mapping_perf_test -b 32G -a "-v vfio_type1_iommu_memfd_hugetlb_1gb" Completed mmap() for memfd in 0.00ms Mapped HVA 0x7f1840000000 (size 32G) at IOVA 0x800000000 in 2887.56msHere, the
mmap()call completes instantly, but the ioctl to map the DMA region incurs the ~3s cost of prefaulting the pages.Running the same test with the -p flag results in:
$ ./vfio_dma_mapping_perf_test -p -b 32G -a "-v vfio_type1_iommu_memfd_hugetlb_1gb" Completed mmap() for memfd in 2887.41ms Mapped HVA 0x7f8000000000 (size 32G) at IOVA 0x800000000 in 11.51msBy shifting the prefaulting overhead to
mmap(), the actual DMA mapping latency is isolated and accurately reported as 11.51ms.With user-defined mapping sizes and prefaulting in place, this test now clearly demonstrates the difference in mapping speeds between VFIO's Type 1 IOMMU and IOMMUFD (i.e.,
IOMMU_IOAS_MAP_FILE).Running the same configuration with the "
iommufd_memfd_hugetlb_1gb" variant highlights this difference:Quoted 6 lines
$ ./vfio_dma_mapping_perf_test -p -b 32G -a "-v iommufd_memfd_hugetlb_1gb" Mapped HVA 0x7f4fc0000000 (size 32G) at IOVA 0x800000000 in 0.11msThe DMA mapping time drops dramatically from 11.51ms to 0.11ms.
Signed-off-by Aaron Lewis <[email protected]>IMO we should either:
1. Only support the
MAP_POPULATEvariant since that isolates the VFIO and IOMMUFD performance from allocating the backing memory and aligns with how a proper userspace should be designed.or
2. Make
MAP_POPULATEtoggle a test fixture variant so that we automatically test both scenarios without needing a command line flag. If the user wants to test just one scenario they can do so via kselftest harness -t flag.
Quoted 5 lines
Either way, we shouldn't need a command line flag for this.
I don't have a strong opinion between the two but lean towards (2) until we have a compelling reason to care about the performance of VFIO and IOMMUFD triggering the backing memory allocations.
Oops I meant "lean towards (1)" there.
David Matlack
maintainerQuoted 19 lines · Aaron Lewis
Add a new VFIO selftest,
vfio_dma_mapping_perf_test, to provide a configurable tool for measuring DMA mapping and unmapping latencies.This selftest introduces command-line parameters to provide a convenient way to tune the test for specific situations. For example, it can be used to observe the lengthy unmap times of the Type 1 IOMMU on large memory regions, or to compare DMA mapping performance between the Type 1 IOMMU and IOMMUFD. These scenarios can now be easily configured and run via the command line.
During development, I abandoned an approach where I initialized memfd globally in
main()for all tests. While that allowed memfd to be initialized once for all test variants, it proved awkward because a different memfd would be needed for each supported page size (e.g., 4K, 2M, 1G). Also, If a user is only running a single test variant, pre-allocating all sizes is unnecessary overhead. Therefore, I dropped that approach in favor of test-local allocation. However, I'm open to ideas on a better way to handle this.
Quoted 5 lines
Aaron Lewis (4):
vfio: selftests: Introducevfio_dma_mapping_perf_test
vfio: selftests: Add memfd test tovfio_dma_mapping_perf_test
vfio: selftests: Allow a size forvfio_dma_mapping_perf_test
vfio: selftests: Allow the flagMAP_POPULATEto be set on the cmdline
Please respond inline to the Sashiko feedback and address any findings in v2. I'll be doing a review as well shortly.
David Matlack
maintainerRe: [PATCH 1/4] vfio: selftests: Introduce vfio_dma_mapping_perf_test
In reply to Aaron LewisQuoted 26 lines · Aaron Lewis
Introduce a tool to aid in latency testing rather than a traditional pass/fail test. The goal is to make the test configurable to expose latency issues and make them easy to reproduce and share.
Start with a basic test that performs a simple DMA map/unmap and reports the duration of each operation. Report the following four metrics:
1. Time taken by
mmap().
2. Time taken to map a DMA region.
3. Time taken to unmap a DMA region.
4. Time taken bymunmap().Subsequent commits in this series will introduce more features to allow for a wider variety of testing.
Signed-off-by Aaron Lewis <[email protected]>tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c new file +3
-0+static void timer_start (struct timespec *start) { +clock_gettime (CLOCK_MONOTONIC , start); +}
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c line 42
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c new file +10
-0+ +static double timer_elapsed_ms (struct timespec start) +{ +struct timespec end; + +clock_gettime (CLOCK_MONOTONIC , &end); + +return (double )(end.tv_sec - start.tv_sec) *MSEC_PER_SEC + + (double )(end.tv_nsec - start.tv_nsec) /NSEC_PER_MSEC ; +}
Please add these to the library and let's exposed the full granularity (number of nanoseconds as a u64) and let caller decide how to format it.
Quoted 54 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c new file +54
-0+ +FIXTURE (vfio_dma_mapping_perf_test) { +struct iommu *iommu; +struct vfio_pci_device *device; +struct iova_allocator *iova_allocator; +}; + +FIXTURE_VARIANT (vfio_dma_mapping_perf_test) { +const char *iommu_mode; +int mmap_flags; +}; + +#define FIXTURE_VARIANT_ADD_IOMMU_MODE (_iommu_mode, _name, _mmap_flags) \ +FIXTURE_VARIANT_ADD(vfio_dma_mapping_perf_test, _iommu_mode ## _ ## _name) { \ + .iommu_mode = #_iommu_mode, \ + .mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | (_mmap_flags), \ +} + +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES (anonymous,0 ); +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES (anonymous_hugetlb_2mb,MAP_HUGETLB |MAP_HUGE_2MB ); +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES (anonymous_hugetlb_1gb,MAP_HUGETLB |MAP_HUGE_1GB ); + +#undef FIXTURE_VARIANT_ADD_IOMMU_MODE + +FIXTURE_SETUP (vfio_dma_mapping_perf_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->iommu); +} + +FIXTURE_TEARDOWN (vfio_dma_mapping_perf_test) +{ +iova_allocator_cleanup (self->iova_allocator); +vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu); +} + +TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap) +{ +const u64 size =SZ_1G ; +const int flags = variant->mmap_flags; +struct dma_region region; +struct timespec start; +u64 unmapped; +int rc; + +timer_start (&start); + region.vaddr =mmap (NULL , size,PROT_READ |PROT_WRITE , flags,-1 ,0 ); +printf ("Completed mmap() in %.2lfms\n" ,timer_elapsed_ms (start)); + +/* Skip the test if there aren't enough HugeTLB pages available. */ +if (flags &MAP_HUGETLB && region.vaddr ==MAP_FAILED ) +SKIP (return,"mmap() failed: %s (%d)\n" ,strerror (errno), errno);
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c line 106
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c new file +10
-0+else +ASSERT_NE (region.vaddr,MAP_FAILED ); + + region.iova =iova_allocator_alloc (self->iova_allocator, size); + region.size = size; + +timer_start (&start); +iommu_map (self->iommu, ®ion); +printf ("Mapped HVA %p (size %luG) at IOVA 0x%lx in %.2lfms\n" , + region.vaddr, size /SZ_1G , region.iova,timer_elapsed_ms (start));
To ease with parsing the test output for performance results maybe we should avoid including specific addresses/sizes mixed in? You can print out the configuration first and then all the performance data. e.g.
Test Config: size=1GB, backing=HugeTLB, page_size=1GB
mmap: XX.YYYYYs
IOMMU map: XX.YYYYYs
IOMMU unmap: XX.YYYYYs
munmap: XX.YYYYYs
I don't think logging the HVA or IOVAs are particularly useful.
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c line 111
Quoted 5 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c new file +5
-0+ASSERT_EQ (region.iova,to_iova (self->device, region.vaddr)); + +timer_start (&start); + rc =__iommu_unmap (self->iommu, ®ion, &unmapped); +printf ("Unmapped IOVA 0x%lx in %.2lfms\n" , region.iova,timer_elapsed_ms (start));
Consider generating the timer code. e.g.
+#define TIME(_name, _expression) do { \
+ struct timespec __start; \
+ \
+ timer_start(&__start); \
+ _expression; \+ printf(_name ": %.2lfms", timer_elapsed_ms(__start)); \
+} while (0)
+
TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap)
{
const u64 size = test_params.size;
const int flags = variant->mmap_flags | test_params.mmap_flags;
struct dma_region region;
- struct timespec start;
u64 unmapped;- int rc;
- timer_start(&start);
- region.vaddr = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);
- printf("Completed mmap() in %.2lfms\n", timer_elapsed_ms(start));
+ TIME("mmap", mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0)); /* Skip the test if there aren't enough HugeTLB pages available. */
if (flags & MAP_HUGETLB && region.vaddr == MAP_FAILED)
@@ -108,21 +112,13 @@ TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap)
region.iova = iova_allocator_alloc(self->iova_allocator, size);
region.size = size;- timer_start(&start);
- iommu_map(self->iommu, ®ion);
- printf("Mapped HVA %p (size %luG) at IOVA 0x%lx in %.2lfms\n",
- region.vaddr, size / SZ_1G, region.iova, timer_elapsed_ms(start));
+ TIME("IOMMU map", iommu_map(self->iommu, ®ion));
ASSERT_EQ(region.iova, to_iova(self->device, region.vaddr));- timer_start(&start);
- rc = __iommu_unmap(self->iommu, ®ion, &unmapped);
- printf("Unmapped IOVA 0x%lx in %.2lfms\n", region.iova, timer_elapsed_ms(start));
- ASSERT_EQ(rc, 0);
+ TIME("IOMMU unmap", ASSERT_EQ(0,__iommu_unmap(self->iommu, ®ion, &unmapped)));
ASSERT_EQ(unmapped, region.size);- timer_start(&start);
- ASSERT_TRUE(!munmap(region.vaddr, size));
- printf("Completed munmap() in %.2lfms\n", timer_elapsed_ms(start));
+ TIME("munmap", ASSERT_TRUE(!munmap(region.vaddr, size)));}
Quoted 16 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c new file +13
-0+ASSERT_EQ (rc,0 ); +ASSERT_EQ (unmapped, region.size); + +timer_start (&start); +ASSERT_TRUE (!munmap (region.vaddr, size)); +printf ("Completed munmap() in %.2lfms\n" ,timer_elapsed_ms (start)); +} + +int main (int argc,char *argv[]) +{ + device_bdf =vfio_selftests_get_bdf (&argc, argv); +return test_harness_run (argc, argv); +}Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
David Matlack
maintainerRe: [PATCH 2/4] vfio: selftests: Add memfd test to vfio_dma_mapping_perf_test
In reply to Aaron LewisQuoted 94 lines · Aaron Lewis
Add a second test to "
vfio_dma_mapping_perf_test" to evaluate memfd latencies. A key motivator for adding this test is to demonstrate that theIOMMU_IOAS_MAP_FILEioctl is significantly faster than other methods of mapping DMA regions. While this performance difference is not fully apparent with the test's current capabilities, it will become evident as more features are introduced later in the series.To support this, add
IOMMU_IOAS_MAP_FILEioctl support to the VFIO selftest library viaiommufd_map_file().Signed-off-by Aaron Lewis <[email protected]>--- .../vfio/lib/include/libvfio/iommu.h | 7 ++ tools/testing/selftests/vfio/lib/iommu.c | 24 ++++ .../vfio/vfio_dma_mapping_perf_test.c | 118 ++++++++++++++++++ 3 files changed, 149 insertions(+)tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +7
-0@@ -35,6 +35,13 @@ struct iommu {struct iommu *iommu_init (const char *iommu_mode);void iommu_cleanup (struct iommu *iommu); +int __iommufd_map_file (struct iommu *iommu,struct dma_region *region,int fd); + +static inline void iommufd_map_file (struct iommu *iommu,struct dma_region *region,int fd) +{ +VFIO_ASSERT_EQ (__iommufd_map_file (iommu, region, fd),0 ); +} +int __iommu_map (struct iommu *iommu,struct dma_region *region);static inline void iommu_map (struct iommu *iommu,struct dma_region *region)tools/testing/selftests/vfio/lib/iommu.c +24
-0@@ -97,6 +97,30 @@ iova_t iommu_hva2iova(struct iommu *iommu, void *vaddr)return iova; } +int __iommufd_map_file (struct iommu *iommu,struct dma_region *region,int fd) +{ +VFIO_ASSERT_TRUE (iommu->iommufd,"IOMMU_IOAS_MAP_FILE is an IOMMUFD IOCTL." ); + +struct iommu_ioas_map_file args = { + .size =sizeof (args), + .flags =IOMMU_IOAS_MAP_READABLE | +IOMMU_IOAS_MAP_WRITEABLE | +IOMMU_IOAS_MAP_FIXED_IOVA , + .ioas_id = iommu->ioas_id, + .fd = fd, + .start =0 , + .iova = region->iova, + .length = region->size, + }; + +if (ioctl (iommu->iommufd,IOMMU_IOAS_MAP_FILE , &args)) +return -errno; + +list_add (®ion->link, &iommu->dma_regions); + +return 0 ; +} +static int vfio_iommu_map (struct iommu *iommu,struct dma_region *region) {struct vfio_iommu_type1_dma_map args = {tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +16
-0@@ -117,6 +117,124 @@ TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap)printf ("Completed munmap() in %.2lfms\n" ,timer_elapsed_ms (start)); } +FIXTURE (vfio_dma_mapping_perf_memfd_test) { +struct iommu *iommu; +struct vfio_pci_device *device; +struct iova_allocator *iova_allocator; +}; + +FIXTURE_VARIANT (vfio_dma_mapping_perf_memfd_test) { +const char *iommu_mode; +int mmap_flags; +int memfd_flags; +}; + +#define FIXTURE_VARIANT_ADD_IOMMU_MODE (_iommu_mode, _name, _mmap_flags, _memfd_flags) \ +FIXTURE_VARIANT_ADD(vfio_dma_mapping_perf_memfd_test, _iommu_mode ## _ ## _name) { \ + .iommu_mode = #_iommu_mode, \ + .mmap_flags = MAP_SHARED | (_mmap_flags), \
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap) · line 145
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +10
-0+ .memfd_flags = MAP_SHARED | (_memfd_flags), \ +} + +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES (memfd,0 ,0 ); +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES (memfd_hugetlb_2mb, +MAP_HUGETLB |MAP_HUGE_2MB , +MFD_HUGETLB |MFD_HUGE_2MB ); +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES (memfd_hugetlb_1gb, +MAP_HUGETLB |MAP_HUGE_1GB , +MFD_HUGETLB |MFD_HUGE_1GB );
Only iommufd supports file-based mapping so not need to replicate across all other IOMMU modes. Just run with iommufd.
And then you can drop the special case below where you fallback to HVA-based mapping in the file-based test, which is largely duplicating the other test.
If you want to also have test coverage for HVA-based mapping of file-backed memory then you should add that separately, and include iommufd in it.
Quoted 78 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +78
-0+ +#undef FIXTURE_VARIANT_ADD_IOMMU_MODE + +FIXTURE_SETUP (vfio_dma_mapping_perf_memfd_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->iommu); +} + +FIXTURE_TEARDOWN (vfio_dma_mapping_perf_memfd_test) +{ +iova_allocator_cleanup (self->iova_allocator); +vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu); +} + +static void *setup_memfd (int *fd,u64 size,int mmap_flags,int mfd_flags) +{ +void *buf =MAP_FAILED ; +struct timespec start; + +timer_start (&start); + *fd =memfd_create ("vfio_dma_mapping_perf_memfd_test" , mfd_flags); +printf ("Completed memfd_create() in %.2lfms\n" ,timer_elapsed_ms (start)); +if (*fd <=0 ) +return MAP_FAILED ; + +if (ftruncate (*fd, size)) + goto out; + +timer_start (&start); + buf =mmap (NULL , size,PROT_READ |PROT_WRITE , mmap_flags, *fd,0 ); +printf ("Completed mmap() for memfd in %.2lfms\n" ,timer_elapsed_ms (start)); + +out: +if (buf ==MAP_FAILED ) +close (*fd); + +return buf; +} + +static void teardown_memfd (int fd,u64 size,void *vaddr) +{ +struct timespec start; + +if (vaddr !=MAP_FAILED ) { +timer_start (&start); +munmap (vaddr, size); +printf ("Completed munmap() in %.2lfms\n" ,timer_elapsed_ms (start)); + } + +if (fd !=-1 ) { +timer_start (&start); +close (fd); +printf ("Completed close() in %.2lfms\n" ,timer_elapsed_ms (start)); + } +} + +TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) +{ +const u64 size =SZ_1G ; +struct dma_region region; +struct timespec start; +u64 unmapped; +int rc, fd; + + region.vaddr =setup_memfd (&fd, size, variant->mmap_flags, variant->memfd_flags); +ASSERT_NE (region.vaddr,MAP_FAILED ); + + region.iova =iova_allocator_alloc (self->iova_allocator, size); + region.size = size; + +timer_start (&start); +if (strcmp (variant->iommu_mode,MODE_IOMMUFD ) ==0 ) { +iommufd_map_file (self->iommu, ®ion, fd); + }else { +iommu_map (self->iommu, ®ion);
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap) · line 233
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +10
-0+ } +printf ("Mapped HVA %p (size %luG) at IOVA 0x%lx in %.2lfms\n" , + region.vaddr, size /SZ_1G , region.iova,timer_elapsed_ms (start)); +ASSERT_EQ (region.iova,to_iova (self->device, region.vaddr)); + +timer_start (&start); + rc =__iommu_unmap (self->iommu, ®ion, &unmapped); +printf ("Unmapped IOVA 0x%lx in %.2lfms\n" , region.iova,timer_elapsed_ms (start)); +ASSERT_EQ (rc,0 ); +ASSERT_EQ (unmapped, region.size);
Maybe we should add this assert to iommu_unmap() and you can just use iommu_unmap() here and in the other test?
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +4
-0+ +teardown_memfd (fd, size, region.vaddr); +} +int main (int argc,char *argv[]) { device_bdf =vfio_selftests_get_bdf (&argc, argv);Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
David Matlack
maintainerRe: [PATCH 3/4] vfio: selftests: Allow a size for vfio_dma_mapping_perf_test
In reply to Aaron LewisQuoted 40 lines · Aaron Lewis
Allow the user to specify a DMA region size via the command line for
vfio_dma_mapping_perf_test.Because the selftest harness also parses command-line parameters, sharing them directly is problematic. Adding options directly to the test could create conflicts with harness-defined options. Even without conflicts, the harness would need to be updated to recognize test-specific options to avoid failing on unknown parameters.
Resolve this by isolating the two sets of parameters. The standard command-line options are consumed by the test itself. To pass options through to the test harness, introduce a new '-a' option.
For example, both the test size and the test harness options can be set like this:
./vfio_dma_mapping_perf_test -b 16G -a "-v
vfio_type1_iommu_memfd_hugetlb_1gb"This invocation configures a 16G DMA region and restricts execution to the specified test variant, which is useful when debugging DMA mapping latency issues for a specific IOMMU type.
Signed-off-by Aaron Lewis <[email protected]>--- .../vfio/vfio_dma_mapping_perf_test.c | 132 +++++++++++++++++- 1 file changed, 129 insertions(+), 3 deletions(-)tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +1
-0@@ -4,6 +4,7 @@#include <sys/mman.h> #include <time.h> #include <unistd.h> +#include <wordexp.h> #include <linux/iommufd.h> #include <linux/limits.h>
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c line 25
Quoted 6 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +3
-0@@ -19,6 +20,12 @@static const char *device_bdf; +struct test_params { +u64 size; +};
Move device_bdf into test_params as well.
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c line 27
Quoted 2 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +2
-0+ +struct test_params test_params;
static
Quoted 23 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +3
-2+struct iommu_mapping {u64 pgd;u64 p4d;@@ -80,7 +87,7 @@ FIXTURE_TEARDOWN(vfio_dma_mapping_perf_test) TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap) { -const u64 size =SZ_1G ; +const u64 size = test_params.size;const int flags = variant->mmap_flags;struct dma_region region;struct timespec start;@@ -204,7 +211,7 @@ static void teardown_memfd(int fd, u64 size, void *vaddr) TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) { -const u64 size =SZ_1G ; +const u64 size = test_params.size;struct dma_region region;struct timespec start;u64 unmapped;
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 248
Quoted 7 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +4
-0@@ -235,8 +242,127 @@ TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file)teardown_memfd (fd, size, region.vaddr); } +/* +* Parses "[0-9]+[kmgt]?". +*/ +size_t parse_size (const char *size)
nit: u64 aligns with the types used in the test.
Quoted 28 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +28
-0+{ +size_t base; +char *scale; +int shift =0 ; + +VFIO_ASSERT_TRUE (size &&isdigit (size[0 ]), +"Need at least one digit in '%s'." , size); + + base =strtoull (size, &scale,0 ); + +VFIO_ASSERT_TRUE (base !=ULLONG_MAX ,"Overflow parsing size!" ); + +switch (tolower (*scale)) { +case 't' : + shift =40 ; +break ; +case 'g' : + shift =30 ; +break ; +case 'm' : + shift =20 ; +break ; +case 'k' : + shift =10 ; +break ; +case 'b' : +case '\0' : + shift =0 ;
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 286
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +10
-0+break ; +default : +VFIO_FAIL ("Unknown size letter '%c'." , *scale); + } + +VFIO_ASSERT_TRUE ((base << shift) >> shift == base, +"Overflow scaling size!" ); + +return base << shift; +}
I'd like to put this in the library but it's going to conflict with KVM selftests parse_size() helper when we link libvfio into KVM selftests.
I'd like to unify the common helpers between libvfio and KVM selftests library but that is future work. In the meantime I guess we can keep this local.
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 297
Quoted 11 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +11
-0+ +static void help (char *name) +{ +puts ("" ); +printf ("usage: %s [-h] [-b bytes] [-a \"test harness args\"]\n" , name); +puts ("" ); +printf (" -h: Display this help message.\n" +" -b: Specify the size of the DMA region to be mapped\n" +" and unmapped. e.g. 16M or 8G, (default: 1G)\n" +" -a: Args that are forwarded to the test harness,\n" +" e.g. -a \"-t dma_map_unmap_from_file\"\n" );
Should this also call test_harness_run(1, "-h") to print its help message?
Quoted 7 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +7
-0+} + +struct harness_args +{ +int argc; +char **argv; +wordexp_t exp;
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 314
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +10
-0+}; + +static void populate_harness_args (struct harness_args *args,const char *argv_0, +const char *cmdlne) +{ +if (wordexp (argv_0, &args->exp,WRDE_NOCMD ) ==0 && +wordexp (cmdlne, &args->exp,WRDE_APPEND |WRDE_NOCMD ) ==0 ) { + args->argc = args->exp.we_wordc; + args->argv = args->exp.we_wordv; + }
Is this missing error handling? Also maybe add some comments since wordexp() is not a very commonly used helper.
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 323
Quoted 9 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +9
-0+} + +static void setup_test (struct harness_args *args,int argc,char *argv[]) +{ +int opt; + + test_params = (struct test_params ) { + .size =SZ_1G , + };
Do this where test_params is declared, not here.
static struct test_params test_params = {
.size = SZ_1G,
};BTW, this change will force all tests to use 1GiB instead of their current defaults (PAGE_SIZE, 2M, and 1G respectively).
So this change is not purely adding a command line option. I think this change should probably go into the first commit that introduces the test, not this one.
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 336
Quoted 13 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +13
-0+ +while ((opt =getopt (argc, argv,"a:b:h" )) !=-1 ) { +switch (opt) { +case 'a' : +populate_harness_args (args, argv[0 ], optarg); +break ; +case 'b' : + test_params.size =parse_size (optarg); +break ; +case 'h' : +default : +help (argv[0 ]); + goto out;
This should exit instead of continuing right?
Quoted 13 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +13
-0+ } + } + +out: +// Reset getopt() state to allow the test harness to use it. + optind =1 ; +} + +static void teardown_test (struct harness_args *args) +{ +if (args->argv) { + args->argc =0 ; + args->argv =NULL ;
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 359
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +8
-0+wordfree (&args->exp); + } +} +int main (int argc,char *argv[]) { +struct harness_args args = (struct harness_args ) { + .argc =0 , + .argv =NULL , + };
This can just be:
struct harness_args args = {};tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 360
Quoted 1 line
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +1
-0+int r;
Please be consistent about variable names for return codes. There's a mix of r and rc I've noticed.
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 363
Quoted 3 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +2
-0+ +setup_test (&args, argc, argv); device_bdf =vfio_selftests_get_bdf (&argc, argv);
Move this into setup_test() too.
Quoted 9 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +4
-1-return test_harness_run (argc, argv); + r =test_harness_run (args.argc, args.argv); +teardown_test (&args); + +return r; }Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
Re: [PATCH 3/4] vfio: selftests: Allow a size for vfio_dma_mapping_perf_test
Quoted 19 lines
Quoted 5 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +5
-0+ shift =10 ; +break ; +case 'b' : +case '\0' : + shift =0 ;tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 286
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +10
-0+break ; +default : +VFIO_FAIL ("Unknown size letter '%c'." , *scale); + } + +VFIO_ASSERT_TRUE ((base << shift) >> shift == base, +"Overflow scaling size!" ); + +return base << shift; +}I'd like to put this in the library but it's going to conflict with KVM selftests
parse_size()helper when we link libvfio into KVM selftests.
Quoted 4 lines
I'd like to unify the common helpers between libvfio and KVM selftests library but that is future work. In the meantime I guess we can keep this local.
Agreed, this would be good to do as a follow up.
Quoted 52 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 297
Quoted 11 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +11
-0+ +static void help (char *name) +{ +puts ("" ); +printf ("usage: %s [-h] [-b bytes] [-a \"test harness args\"]\n" , name); +puts ("" ); +printf (" -h: Display this help message.\n" +" -b: Specify the size of the DMA region to be mapped\n" +" and unmapped. e.g. 16M or 8G, (default: 1G)\n" +" -a: Args that are forwarded to the test harness,\n" +" e.g. -a \"-t dma_map_unmap_from_file\"\n" );Should this also call
test_harness_run(1, "-h")to print its help message?Quoted 7 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +7
-0+} + +struct harness_args +{ +int argc; +char **argv; +wordexp_t exp;tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 314
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +10
-0+}; + +static void populate_harness_args (struct harness_args *args,const char *argv_0, +const char *cmdlne) +{ +if (wordexp (argv_0, &args->exp,WRDE_NOCMD ) ==0 && +wordexp (cmdlne, &args->exp,WRDE_APPEND |WRDE_NOCMD ) ==0 ) { + args->argc = args->exp.we_wordc; + args->argv = args->exp.we_wordv; + }Is this missing error handling? Also maybe add some comments since
wordexp()is not a very commonly used helper.tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file) · line 323
Quoted 9 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +9
-0+} + +static void setup_test (struct harness_args *args,int argc,char *argv[]) +{ +int opt; + + test_params = (struct test_params ) { + .size =SZ_1G , + };Do this where
test_paramsis declared, not here.static struct test_params test_params = { .size = SZ_1G, };
Quoted 6 lines
BTW, this change will force all tests to use 1GiB instead of their current defaults (
PAGE_SIZE, 2M, and 1G respectively).So this change is not purely adding a command line option. I think this change should probably go into the first commit that introduces the test, not this one.
The tests were explicitly set to 1GB in the previous commits for all variants. This commit allows the user to change that to a user defined value, with the default still set to 1GB. So it really should just be additive.
I left this as its own commit because it is only additive and should be easier to review on its own. If you still think it should be a part of the first commit I can do that.
Quoted 11 lines
Quoted 11 lines
tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c +10
-0+ +while ((opt =getopt (argc, argv,"a:b:h" )) !=-1 ) { +switch (opt) { +case 'a' : +populate_harness_args (args, argv[0 ], optarg); +break ; +case 'b' : + test_params.size =parse_size (optarg); +break ; +case 'h' :