* [PATCH v4 4/5] selftests: harness: Support custom command-line options
[not found] <20261114132200.113697-1-aaronlewis@google.com>
@ 2026-11-14 13:21 ` Aaron Lewis
2026-11-25 19:40 ` David Matlack
2026-11-14 13:22 ` [PATCH v4 5/5] vfio: selftests: Allow a size for vfio_dma_mapping_perf_test Aaron Lewis
1 sibling, 1 reply; 4+ messages in thread
From: Aaron Lewis @ 2026-11-14 13:21 UTC (permalink / raw)
To: kvm
Cc: alex, dmatlack, jgg, Aaron Lewis, Kees Cook, Andy Lutomirski,
Will Drewry, Shuah Khan, linux-kselftest
Because the selftest harness parses command-line parameters directly,
adding custom options to individual tests requires the test harness to
recognize them.
Add support to kselftest_harness.h for custom command-line options via
struct test_harness_cli_opts and test_harness_run_opts(), allowing tests
to register a custom getopt optstring, option handler, and optional help
callback while keeping test_harness_run() unchanged for existing callers.
Assert if the custom options string is problematic (e.g. conflicting
with built-in options or exceeding the buffer limit). This should only
happen when developing a test, and asserting makes the issue immediately
obvious to the developer.
Also update test_enabled() to match exact option characters rather than
tolower(opt) so custom options that differ only by case (such as 'R') do
not mistakenly match built-in single-case options like '-r'.
Signed-off-by: Aaron Lewis <aaronlewis@google.com>
---
tools/testing/selftests/kselftest_harness.h | 127 +++++++++++++++-----
1 file changed, 94 insertions(+), 33 deletions(-)
diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h
index 29a19bc87084..562d4c0edac5 100644
--- a/tools/testing/selftests/kselftest_harness.h
+++ b/tools/testing/selftests/kselftest_harness.h
@@ -54,6 +54,7 @@
#define _GNU_SOURCE
#endif
#include <asm/types.h>
+#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <linux/unistd.h>
@@ -76,6 +77,18 @@ static inline void __kselftest_memset_safe(void *s, int c, size_t n)
memset(s, c, n);
}
+/**
+ * struct test_harness_cli_opts - Custom command-line options for test harness
+ * @optstring: getopt option string; must not conflict with harness options.
+ * @handler: Callback to handle custom options; returns KSFT_PASS or KSFT_FAIL.
+ * @help: Optional callback to print custom option help text.
+ */
+struct test_harness_cli_opts {
+ const char *optstring;
+ int (*handler)(int opt, char *optarg);
+ void (*help)(void);
+};
+
#define KSELFTEST_PRIO_TEST 20000
#define KSELFTEST_PRIO_XFAIL 20001
@@ -1097,11 +1110,60 @@ static void test_harness_list_tests(void)
}
}
-static int test_harness_argv_check(int argc, char **argv)
+#define OPTSTRING_LEN 128
+
+static void optstring_append_custom(char *optstring, size_t size,
+ const struct test_harness_cli_opts *opts)
+{
+ const char *c;
+
+ if (!opts || !opts->optstring)
+ return;
+
+ assert(strlen(optstring) + strlen(opts->optstring) < size);
+
+ for (c = opts->optstring; *c; c++) {
+ if (isalnum(*c))
+ assert(!strchr(optstring, *c));
+ }
+
+ strncat(optstring, opts->optstring, size - strlen(optstring) - 1);
+}
+
+static void test_harness_usage(const char *prog,
+ const struct test_harness_cli_opts *opts)
+{
+ fprintf(stderr,
+ "Usage: %s [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name]\n"
+ "\t-h print help\n"
+ "\t-l list all tests\n"
+ "\t-d enable debug prints\n"
+ "\n"
+ "\t-t name include test\n"
+ "\t-T name exclude test\n"
+ "\t-v name include variant\n"
+ "\t-V name exclude variant\n"
+ "\t-f name include fixture\n"
+ "\t-F name exclude fixture\n"
+ "\t-r name run specified test\n"
+ "\n"
+ "Test filter options can be specified multiple times. The filtering stops\n"
+ "at the first match. For example to include all tests from variant 'bla'\n"
+ "but not test 'foo' specify '-T foo -v bla'.\n", prog);
+
+ if (opts && opts->help) {
+ fprintf(stderr, "\nCustom options:\n");
+ opts->help();
+ }
+}
+
+static int test_harness_argv_check(int argc, char **argv, const char *optstring,
+ const struct test_harness_cli_opts *opts)
{
int opt;
- while ((opt = getopt(argc, argv, "dhlF:f:V:v:t:T:r:")) != -1) {
+ optind = 1;
+ while ((opt = getopt(argc, argv, optstring)) != -1) {
switch (opt) {
case 'f':
case 'F':
@@ -1118,35 +1180,21 @@ static int test_harness_argv_check(int argc, char **argv)
ksft_debug_enabled = true;
break;
case 'h':
+ test_harness_usage(argv[0], opts);
+ return KSFT_SKIP;
default:
- fprintf(stderr,
- "Usage: %s [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name]\n"
- "\t-h print help\n"
- "\t-l list all tests\n"
- "\t-d enable debug prints\n"
- "\n"
- "\t-t name include test\n"
- "\t-T name exclude test\n"
- "\t-v name include variant\n"
- "\t-V name exclude variant\n"
- "\t-f name include fixture\n"
- "\t-F name exclude fixture\n"
- "\t-r name run specified test\n"
- "\n"
- "Test filter options can be specified "
- "multiple times. The filtering stops\n"
- "at the first match. For example to "
- "include all tests from variant 'bla'\n"
- "but not test 'foo' specify '-T foo -v bla'.\n"
- "", argv[0]);
- return opt == 'h' ? KSFT_SKIP : KSFT_FAIL;
+ if (opts && opts->handler &&
+ opts->handler(opt, optarg) == KSFT_PASS)
+ break;
+ test_harness_usage(argv[0], opts);
+ return KSFT_FAIL;
}
}
return KSFT_PASS;
}
-static bool test_enabled(int argc, char **argv,
+static bool test_enabled(int argc, char **argv, const char *optstring,
struct __fixture_metadata *f,
struct __fixture_variant_metadata *v,
struct __test_metadata *t)
@@ -1156,24 +1204,28 @@ static bool test_enabled(int argc, char **argv,
int opt;
optind = 1;
- while ((opt = getopt(argc, argv, "dF:f:V:v:t:T:r:")) != -1) {
- if (opt != 'd')
- has_positive |= islower(opt);
-
- switch (tolower(opt)) {
+ while ((opt = getopt(argc, argv, optstring)) != -1) {
+ switch (opt) {
case 't':
+ case 'T':
+ has_positive |= islower(opt);
if (!strcmp(t->name, optarg))
return islower(opt);
break;
case 'f':
+ case 'F':
+ has_positive |= islower(opt);
if (!strcmp(f->name, optarg))
return islower(opt);
break;
case 'v':
+ case 'V':
+ has_positive |= islower(opt);
if (!strcmp(v->name, optarg))
return islower(opt);
break;
case 'r':
+ has_positive = true;
if (!tlen) {
flen = strlen(f->name);
vlen = strlen(v->name);
@@ -1262,8 +1314,10 @@ static void __run_test(struct __fixture_metadata *f,
diagnostic ? "%s" : NULL, diagnostic);
}
-static int test_harness_run(int argc, char **argv)
+static int test_harness_run_opts(int argc, char **argv,
+ const struct test_harness_cli_opts *opts)
{
+ char optstring[OPTSTRING_LEN] = "dhlF:f:V:v:t:T:r:";
struct __fixture_variant_metadata no_variant = { .name = "", };
struct __fixture_variant_metadata *v;
struct __fixture_metadata *f;
@@ -1274,7 +1328,9 @@ static int test_harness_run(int argc, char **argv)
unsigned int count = 0;
unsigned int pass_count = 0;
- ret = test_harness_argv_check(argc, argv);
+ optstring_append_custom(optstring, sizeof(optstring), opts);
+
+ ret = test_harness_argv_check(argc, argv, optstring, opts);
if (ret != KSFT_PASS)
return ret;
@@ -1283,7 +1339,7 @@ static int test_harness_run(int argc, char **argv)
unsigned int old_tests = test_count;
for (t = f->tests; t; t = t->next)
- if (test_enabled(argc, argv, f, v, t))
+ if (test_enabled(argc, argv, optstring, f, v, t))
test_count++;
if (old_tests != test_count)
@@ -1301,7 +1357,7 @@ static int test_harness_run(int argc, char **argv)
for (f = __fixture_list; f; f = f->next) {
for (v = f->variant ?: &no_variant; v; v = v->next) {
for (t = f->tests; t; t = t->next) {
- if (!test_enabled(argc, argv, f, v, t))
+ if (!test_enabled(argc, argv, optstring, f, v, t))
continue;
count++;
t->results = results;
@@ -1324,6 +1380,11 @@ static int test_harness_run(int argc, char **argv)
return KSFT_FAIL;
}
+static inline int test_harness_run(int argc, char **argv)
+{
+ return test_harness_run_opts(argc, argv, NULL);
+}
+
static void __attribute__((constructor(KSELFTEST_PRIO_TEST))) __constructor_order_first(void)
{
__constructor_order_forward = true;
--
2.56.0.611.g1030f0b940-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v4 5/5] vfio: selftests: Allow a size for vfio_dma_mapping_perf_test
[not found] <20261114132200.113697-1-aaronlewis@google.com>
2026-11-14 13:21 ` [PATCH v4 4/5] selftests: harness: Support custom command-line options Aaron Lewis
@ 2026-11-14 13:22 ` Aaron Lewis
2026-11-25 19:56 ` David Matlack
1 sibling, 1 reply; 4+ messages in thread
From: Aaron Lewis @ 2026-11-14 13:22 UTC (permalink / raw)
To: kvm
Cc: alex, dmatlack, jgg, Aaron Lewis, Kees Cook, Andy Lutomirski,
Will Drewry, Shuah Khan, linux-kselftest
Allow the user to specify a DMA region size via the command line for
vfio_dma_mapping_perf_test.
Use test_harness_run_opts() to introduce a '-b' option to specify the
DMA region size so that both the test size and test harness options can
be passed together naturally:
./vfio_dma_mapping_perf_test -b 16G -v vfio_type1_iommu_anonymous_hugetlb_1gb
Signed-off-by: Aaron Lewis <aaronlewis@google.com>
---
.../vfio/vfio_dma_mapping_perf_test.c | 144 ++++++++++++++----
1 file changed, 117 insertions(+), 27 deletions(-)
diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c b/tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c
index 086a63a50df8..6e38cc134a49 100644
--- a/tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c
+++ b/tools/testing/selftests/vfio/vfio_dma_mapping_perf_test.c
@@ -1,10 +1,12 @@
// SPDX-License-Identifier: GPL-2.0-only
+#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
+#include <linux/align.h>
#include <linux/iommufd.h>
#include <linux/limits.h>
#include <linux/memfd.h>
@@ -17,7 +19,81 @@
#include "kselftest_harness.h"
-static const char *device_bdf;
+static struct {
+ u64 size;
+ const char *size_str;
+ const char *device_bdf;
+} test_params = {
+ .size = SZ_1G,
+ .size_str = "1G",
+};
+
+/*
+ * Parses "[0-9]+[kmgt]?".
+ */
+static u64 parse_size(const char *size)
+{
+ int shift = 0;
+ char *scale;
+ u64 base;
+
+ VFIO_ASSERT_TRUE(size && isdigit(size[0]),
+ "Need at least one digit in '%s'.", size);
+
+ base = strtoull(size, &scale, 0);
+
+ VFIO_ASSERT_GT(base, 0, "Size must be greater than 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 opts_help(void)
+{
+ fprintf(stderr,
+ "\t-b bytes Specify the size of the DMA region to be mapped\n"
+ "\t and unmapped. e.g. 16M or 8G, (default: 1G)\n");
+}
+
+static int opts_handler(int opt, char *optarg)
+{
+ if (opt == 'b') {
+ test_params.size = parse_size(optarg);
+ test_params.size_str = optarg;
+ return KSFT_PASS;
+ }
+ return KSFT_FAIL;
+}
+
+static const struct test_harness_cli_opts opts = {
+ .optstring = "b:",
+ .handler = opts_handler,
+ .help = opts_help,
+};
FIXTURE(vfio_dma_mapping_perf_test) {
struct iommu *iommu;
@@ -27,25 +103,27 @@ FIXTURE(vfio_dma_mapping_perf_test) {
FIXTURE_VARIANT(vfio_dma_mapping_perf_test) {
const char *iommu_mode;
+ u64 page_size;
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 | MAP_POPULATE | (_mmap_flags), \
+#define FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode, _name, _page_size, _mmap_flags) \
+FIXTURE_VARIANT_ADD(vfio_dma_mapping_perf_test, _iommu_mode ## _ ## _name) { \
+ .iommu_mode = #_iommu_mode, \
+ .page_size = (_page_size), \
+ .mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_POPULATE | (_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);
+FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous, 0, 0);
+FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_2mb, SZ_2M, MAP_HUGETLB | MAP_HUGE_2MB);
+FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, 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->device = vfio_pci_device_init(test_params.device_bdf, self->iommu);
self->iova_allocator = iova_allocator_init(self->iommu);
}
@@ -58,11 +136,16 @@ FIXTURE_TEARDOWN(vfio_dma_mapping_perf_test)
TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap)
{
- const u64 size = SZ_1G;
+ const u64 page_size = variant->page_size ?: getpagesize();
+ const u64 size = test_params.size;
const int flags = variant->mmap_flags;
struct dma_region region;
- ksft_print_msg("mmap size = %lluG\n", (unsigned long long)(size / SZ_1G));
+ if (!IS_ALIGNED(size, page_size))
+ SKIP(return, "Size 0x%llx is not aligned to page size 0x%llx",
+ (unsigned long long)size, (unsigned long long)page_size);
+
+ ksft_print_msg("mmap size = %s\n", test_params.size_str);
TIME("mmap",
region.vaddr = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0));
@@ -90,30 +173,32 @@ FIXTURE(vfio_dma_mapping_perf_memfd_test) {
};
FIXTURE_VARIANT(vfio_dma_mapping_perf_memfd_test) {
+ u64 page_size;
int mmap_flags;
int memfd_flags;
};
-#define FIXTURE_VARIANT_ADD_MEMFD_MODE(_name, _mmap_flags, _memfd_flags) \
-FIXTURE_VARIANT_ADD(vfio_dma_mapping_perf_memfd_test, iommufd ## _ ## _name) {\
- .mmap_flags = MAP_SHARED | MAP_POPULATE | (_mmap_flags), \
- .memfd_flags = (_memfd_flags), \
+#define FIXTURE_VARIANT_ADD_MEMFD_MODE(_name, _page_size, _mmap_flags, _memfd_flags) \
+FIXTURE_VARIANT_ADD(vfio_dma_mapping_perf_memfd_test, iommufd ## _ ## _name) { \
+ .page_size = (_page_size), \
+ .mmap_flags = MAP_SHARED | MAP_POPULATE | (_mmap_flags), \
+ .memfd_flags = (_memfd_flags), \
}
-FIXTURE_VARIANT_ADD_MEMFD_MODE(memfd, 0, 0);
-FIXTURE_VARIANT_ADD_MEMFD_MODE(memfd_hugetlb_2mb,
- MAP_HUGETLB | MAP_HUGE_2MB,
- MFD_HUGETLB | MFD_HUGE_2MB);
-FIXTURE_VARIANT_ADD_MEMFD_MODE(memfd_hugetlb_1gb,
- MAP_HUGETLB | MAP_HUGE_1GB,
- MFD_HUGETLB | MFD_HUGE_1GB);
+FIXTURE_VARIANT_ADD_MEMFD_MODE(memfd, 0, 0, 0);
+FIXTURE_VARIANT_ADD_MEMFD_MODE(memfd_hugetlb_2mb, SZ_2M,
+ MAP_HUGETLB | MAP_HUGE_2MB,
+ MFD_HUGETLB | MFD_HUGE_2MB);
+FIXTURE_VARIANT_ADD_MEMFD_MODE(memfd_hugetlb_1gb, SZ_1G,
+ MAP_HUGETLB | MAP_HUGE_1GB,
+ MFD_HUGETLB | MFD_HUGE_1GB);
#undef FIXTURE_VARIANT_ADD_MEMFD_MODE
FIXTURE_SETUP(vfio_dma_mapping_perf_memfd_test)
{
self->iommu = iommu_init(MODE_IOMMUFD);
- self->device = vfio_pci_device_init(device_bdf, self->iommu);
+ self->device = vfio_pci_device_init(test_params.device_bdf, self->iommu);
self->iova_allocator = iova_allocator_init(self->iommu);
}
@@ -154,12 +239,17 @@ 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 page_size = variant->page_size ?: getpagesize();
+ const u64 size = test_params.size;
const int mmap_flags = variant->mmap_flags;
struct dma_region region;
int fd;
- ksft_print_msg("mmap size = %lluG\n", (unsigned long long)(size / SZ_1G));
+ if (!IS_ALIGNED(size, page_size))
+ SKIP(return, "Size 0x%llx is not aligned to page size 0x%llx",
+ (unsigned long long)size, (unsigned long long)page_size);
+
+ ksft_print_msg("mmap size = %s\n", test_params.size_str);
region.vaddr = setup_memfd(&fd, size, mmap_flags, variant->memfd_flags);
@@ -184,6 +274,6 @@ TEST_F(vfio_dma_mapping_perf_memfd_test, dma_map_unmap_from_file)
int main(int argc, char *argv[])
{
- device_bdf = vfio_selftests_get_bdf(&argc, argv);
- return test_harness_run(argc, argv);
+ test_params.device_bdf = vfio_selftests_get_bdf(&argc, argv);
+ return test_harness_run_opts(argc, argv, &opts);
}
--
2.56.0.611.g1030f0b940-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v4 4/5] selftests: harness: Support custom command-line options
2026-11-14 13:21 ` [PATCH v4 4/5] selftests: harness: Support custom command-line options Aaron Lewis
@ 2026-11-25 19:40 ` David Matlack
0 siblings, 0 replies; 4+ messages in thread
From: David Matlack @ 2026-11-25 19:40 UTC (permalink / raw)
To: Aaron Lewis, kvm
Cc: alex, dmatlack, jgg, Kees Cook, Andy Lutomirski, Will Drewry,
Shuah Khan, linux-kselftest
On Sat Nov 14, 2026 at 5:21 AM PST, Aaron Lewis wrote:
> Also update test_enabled() to match exact option characters rather than
> tolower(opt) so custom options that differ only by case (such as 'R') do
> not mistakenly match built-in single-case options like '-r'.
Please also explain the has_positive change, since it's not obvious
from the diff. How about replacing this paragraph with something like:
Pass the full optstring (including custom options) to test_enabled()
so that getopt() correctly parses custom options that take an
argument. Since test_enabled() now sees custom options, only update
has_positive for the filter options (-t, -f, -v, -r). Otherwise a
lowercase custom option would be treated as a positive filter that
matches nothing, and every test would get filtered out. Similarly,
match exact option characters rather than tolower(opt) so that custom
options that differ only by case (such as -R) do not get treated as
built-in options (such as -r).
[...]
> +/**
> + * struct test_harness_cli_opts - Custom command-line options for test harness
> + * @optstring: getopt option string; must not conflict with harness options.
> + * @handler: Callback to handle custom options; returns KSFT_PASS or KSFT_FAIL.
> + * @help: Optional callback to print custom option help text.
> + */
> +struct test_harness_cli_opts {
> + const char *optstring;
> + int (*handler)(int opt, char *optarg);
> + void (*help)(void);
> +};
KSFT_PASS and KSFT_FAIL are test result codes, which seems like an odd
fit for a callback that handles a command-line option. Please follow the
usual kernel convention instead and have the handler return 0 on success
and non-zero on error, e.g. in test_harness_argv_check():
default:
if (opts && opts->handler && !opts->handler(opt, optarg))
break;
and in the next patch:
static int opts_handler(int opt, char *optarg)
{
if (opt == 'b') {
...
return 0;
}
return -EINVAL;
}
The @handler kernel-doc above will need to be updated to match.
Also, kselftest.rst explicitly lists which kernel-doc comments to pull
in from kselftest_harness.h, so this comment won't show up in the
rendered docs as-is. Please add kernel-doc for test_harness_run_opts()
too, and add both to the "Helpers" list in
Documentation/dev-tools/kselftest.rst so test authors can discover how
to add custom options.
[...]
> +#define OPTSTRING_LEN 128
kselftest_harness.h is included by a lot of tests, so please use a more
specific name (e.g. KSFT_OPTSTRING_LEN) to avoid clashing with something
a test defines, or hard-code it locally.
> +
> +static void optstring_append_custom(char *optstring, size_t size,
> + const struct test_harness_cli_opts *opts)
> +{
> + const char *c;
> +
> + if (!opts || !opts->optstring)
> + return;
> +
> + assert(strlen(optstring) + strlen(opts->optstring) < size);
> +
> + for (c = opts->optstring; *c; c++) {
> + if (isalnum(*c))
> + assert(!strchr(optstring, *c));
> + }
assert() compiles out if a test is built with -DNDEBUG, in which case
these checks silently disappear, and nothing else in kselftest_harness.h
uses assert(). Please follow what the harness already does for misuse
it detects at runtime (e.g. "Illegal usage of low-level ksft APIs in
harness test") and for bad command-line options: print an error with
ksft_print_msg() and fail. e.g. have optstring_append_custom() return an
error and test_harness_run_opts() return KSFT_FAIL, the same way it
handles a failure from test_harness_argv_check(). That also avoids
pulling <assert.h> into every test that includes the harness.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v4 5/5] vfio: selftests: Allow a size for vfio_dma_mapping_perf_test
2026-11-14 13:22 ` [PATCH v4 5/5] vfio: selftests: Allow a size for vfio_dma_mapping_perf_test Aaron Lewis
@ 2026-11-25 19:56 ` David Matlack
0 siblings, 0 replies; 4+ messages in thread
From: David Matlack @ 2026-11-25 19:56 UTC (permalink / raw)
To: Aaron Lewis, kvm
Cc: alex, dmatlack, jgg, Kees Cook, Andy Lutomirski, Will Drewry,
Shuah Khan, linux-kselftest, Sean Christopherson
On Sat Nov 14, 2026 at 5:22 AM PST, Aaron Lewis wrote:
> +/*
> + * Parses "[0-9]+[kmgt]?".
> + */
> +static u64 parse_size(const char *size)
+Sean
This is a copy of parse_size() from KVM selftests
(tools/testing/selftests/kvm/lib/test_util.c), which points to a broader
problem: VFIO and KVM selftests keep growing duplicate copies of the
same basic helpers. Besides parse_size(), this series adds
timer_get()/timer_elapsed_ns(), which overlap with KVM's
timespec_elapsed()/timespec_to_ns(), and VFIO selftests have their own
VFIO_ASSERT*() macros that serve the same purpose as KVM's
TEST_ASSERT().
This isn't just about the tests themselves, it's also about the
libraries. libvfio uses VFIO_ASSERT*() internally and gets linked into
KVM selftests, so a KVM test that uses libvfio (e.g. irq_test) ends up
with two different assert implementations depending on which library
the failure came from. And libvfio can't use any of KVM's helpers, since
VFIO selftests don't link against libkvm. We need to figure out a common
place for these helpers that both libvfio and libkvm (and probably other
selftests too) can use.
That doesn't need to block this series though.
[...]
> TEST_F(vfio_dma_mapping_perf_test, dma_map_unmap)
> {
> - const u64 size = SZ_1G;
> + const u64 page_size = variant->page_size ?: getpagesize();
> + const u64 size = test_params.size;
> const int flags = variant->mmap_flags;
> struct dma_region region;
>
> - ksft_print_msg("mmap size = %lluG\n", (unsigned long long)(size / SZ_1G));
> + if (!IS_ALIGNED(size, page_size))
> + SKIP(return, "Size 0x%llx is not aligned to page size 0x%llx",
> + (unsigned long long)size, (unsigned long long)page_size);
> +
> + ksft_print_msg("mmap size = %s\n", test_params.size_str);
The page size calculation, alignment check, and ksft_print_msg() are
all duplicated in dma_map_unmap_from_file(). How about:
- Print the size once in main(), since it's the same for every test.
- Move the alignment check into a helper and call it at the top of
both FIXTURE_SETUP()s. Roughly:
static bool skip_if_size_unaligned(struct __test_metadata *_metadata,
u64 page_size)
{
page_size = page_size ?: getpagesize();
if (IS_ALIGNED(test_params.size, page_size))
return false;
SKIP(return true, "Size 0x%llx is not aligned to page size 0x%llx",
(unsigned long long)test_params.size,
(unsigned long long)page_size);
}
FIXTURE_SETUP(vfio_dma_mapping_perf_test)
{
if (skip_if_size_unaligned(_metadata, variant->page_size))
return;
...
}
The harness won't run the test if setup skips (see
vfio_dma_mapping_mmio_test.c for an example), and doing the check
first avoids setting up the IOMMU and device for tests that are going
to be skipped anyway.
With that,
Reviewed-by: David Matlack <dmatlack@google.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-25 19:56 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261114132200.113697-1-aaronlewis@google.com>
2026-11-14 13:21 ` [PATCH v4 4/5] selftests: harness: Support custom command-line options Aaron Lewis
2026-11-25 19:40 ` David Matlack
2026-11-14 13:22 ` [PATCH v4 5/5] vfio: selftests: Allow a size for vfio_dma_mapping_perf_test Aaron Lewis
2026-11-25 19:56 ` David Matlack
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox