From: Mostafa Saleh <smostafa@google.com>
To: iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org
Cc: robin.murphy@arm.com, will@kernel.org, joro@8bytes.org,
jgg@ziepe.ca, praan@google.com,
Mostafa Saleh <smostafa@google.com>
Subject: [PATCH 2/2] iommu/io-pgtable-arm-selftest: Use KUnit
Date: Wed, 17 Sep 2025 14:02:03 +0000 [thread overview]
Message-ID: <20250917140216.2199055-3-smostafa@google.com> (raw)
In-Reply-To: <20250917140216.2199055-1-smostafa@google.com>
Integrate the selftests as part of kunit, this makes the results of
the test available through debugfs later.
Suggested-by: Jason Gunthorpe <jgg@ziepe.ca>
Signed-off-by: Mostafa Saleh <smostafa@google.com>
---
drivers/iommu/Kconfig | 2 +-
drivers/iommu/io-pgtable-arm-selftests.c | 90 ++++++++++++++----------
2 files changed, 52 insertions(+), 40 deletions(-)
diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
index 70d29b14d851..52c2fcb0e77f 100644
--- a/drivers/iommu/Kconfig
+++ b/drivers/iommu/Kconfig
@@ -42,7 +42,7 @@ config IOMMU_IO_PGTABLE_LPAE
config IOMMU_IO_PGTABLE_LPAE_SELFTEST
bool "LPAE selftests"
- depends on IOMMU_IO_PGTABLE_LPAE
+ depends on IOMMU_IO_PGTABLE_LPAE && KUNIT=y
help
Enable self-tests for LPAE page table allocator. This performs
a series of page-table consistency checks during boot.
diff --git a/drivers/iommu/io-pgtable-arm-selftests.c b/drivers/iommu/io-pgtable-arm-selftests.c
index f7746ff2c7a0..3775936d07c7 100644
--- a/drivers/iommu/io-pgtable-arm-selftests.c
+++ b/drivers/iommu/io-pgtable-arm-selftests.c
@@ -6,6 +6,8 @@
*
* Author: Will Deacon <will.deacon@arm.com>
*/
+#include <kunit/device.h>
+#include <kunit/test.h>
#include <linux/device/faux.h>
#include <linux/kernel.h>
#include <linux/slab.h>
@@ -39,25 +41,26 @@ static const struct iommu_flush_ops dummy_tlb_ops __initconst = {
.tlb_add_page = dummy_tlb_add_page,
};
-static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
+static void __init arm_lpae_dump_ops(struct kunit *test, struct io_pgtable_ops *ops)
{
struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
struct io_pgtable_cfg *cfg = &data->iop.cfg;
- pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
- cfg->pgsize_bitmap, cfg->ias);
- pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n",
- ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data),
- ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd);
+ kunit_err(test, "cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
+ cfg->pgsize_bitmap, cfg->ias);
+ kunit_err(test, "data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n",
+ ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data),
+ ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd);
}
-#define __FAIL(ops, i) ({ \
- WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
- arm_lpae_dump_ops(ops); \
- -EFAULT; \
+#define __FAIL(test, ops, i) ({ \
+ KUNIT_FAIL(test, ""); \
+ kunit_err(test, "selftest: test failed for fmt idx %d\n", (i)); \
+ arm_lpae_dump_ops(test, ops); \
+ -EFAULT; \
})
-static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
+static int __init arm_lpae_run_tests(struct kunit *test, struct io_pgtable_cfg *cfg)
{
static const enum io_pgtable_fmt fmts[] __initconst = {
ARM_64_LPAE_S1,
@@ -73,7 +76,7 @@ static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
cfg_cookie = cfg;
ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
if (!ops) {
- pr_err("selftest: failed to allocate io pgtable ops\n");
+ kunit_err(test, "selftest: failed to allocate io pgtable ops\n");
return -ENOMEM;
}
@@ -82,13 +85,13 @@ static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
* Empty page tables shouldn't provide any translations.
*/
if (ops->iova_to_phys(ops, 42))
- return __FAIL(ops, i);
+ return __FAIL(test, ops, i);
if (ops->iova_to_phys(ops, SZ_1G + 42))
- return __FAIL(ops, i);
+ return __FAIL(test, ops, i);
if (ops->iova_to_phys(ops, SZ_2G + 42))
- return __FAIL(ops, i);
+ return __FAIL(test, ops, i);
/*
* Distinct mappings of different granule sizes.
@@ -101,16 +104,16 @@ static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
IOMMU_READ | IOMMU_WRITE |
IOMMU_NOEXEC | IOMMU_CACHE,
GFP_KERNEL, &mapped))
- return __FAIL(ops, i);
+ return __FAIL(test, ops, i);
/* Overlapping mappings */
if (!ops->map_pages(ops, iova, iova + size, size, 1,
IOMMU_READ | IOMMU_NOEXEC,
GFP_KERNEL, &mapped))
- return __FAIL(ops, i);
+ return __FAIL(test, ops, i);
if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
- return __FAIL(ops, i);
+ return __FAIL(test, ops, i);
iova += SZ_1G;
}
@@ -121,18 +124,18 @@ static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
size = 1UL << j;
if (ops->unmap_pages(ops, iova, size, 1, NULL) != size)
- return __FAIL(ops, i);
+ return __FAIL(test, ops, i);
if (ops->iova_to_phys(ops, iova + 42))
- return __FAIL(ops, i);
+ return __FAIL(test, ops, i);
/* Remap full block */
if (ops->map_pages(ops, iova, iova, size, 1,
IOMMU_WRITE, GFP_KERNEL, &mapped))
- return __FAIL(ops, i);
+ return __FAIL(test, ops, i);
if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
- return __FAIL(ops, i);
+ return __FAIL(test, ops, i);
iova += SZ_1G;
}
@@ -148,11 +151,11 @@ static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
IOMMU_READ | IOMMU_WRITE |
IOMMU_NOEXEC | IOMMU_CACHE,
GFP_KERNEL, &mapped))
- return __FAIL(ops, i);
+ return __FAIL(test, ops, i);
if (mapped != size)
- return __FAIL(ops, i);
+ return __FAIL(test, ops, i);
if (ops->unmap_pages(ops, iova, size, 1, NULL) != size)
- return __FAIL(ops, i);
+ return __FAIL(test, ops, i);
free_io_pgtable_ops(ops);
}
@@ -160,7 +163,7 @@ static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
return 0;
}
-static int __init arm_lpae_do_selftests(void)
+static void __init io_pgtable_arm_test_run(struct kunit *test)
{
static const unsigned long pgsize[] __initconst = {
SZ_4K | SZ_2M | SZ_1G,
@@ -173,18 +176,19 @@ static int __init arm_lpae_do_selftests(void)
};
int i, j, k, pass = 0, fail = 0;
- struct faux_device *dev;
+ struct device *dev;
struct io_pgtable_cfg cfg = {
.tlb = &dummy_tlb_ops,
.coherent_walk = true,
.quirks = IO_PGTABLE_QUIRK_NO_WARN,
};
- dev = faux_device_create("io-pgtable-test", NULL, 0);
- if (!dev)
- return -ENOMEM;
+ dev = kunit_device_register(test, "io-pgtable-test");
+ KUNIT_EXPECT_NOT_ERR_OR_NULL(test, dev);
+ if (IS_ERR_OR_NULL(dev))
+ return;
- cfg.iommu_dev = &dev->dev;
+ cfg.iommu_dev = dev;
for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
for (j = 0; j < ARRAY_SIZE(address_size); ++j) {
@@ -193,9 +197,9 @@ static int __init arm_lpae_do_selftests(void)
cfg.pgsize_bitmap = pgsize[i];
cfg.ias = address_size[k];
cfg.oas = address_size[j];
- pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u OAS %u\n",
- pgsize[i], cfg.ias, cfg.oas);
- if (arm_lpae_run_tests(&cfg))
+ kunit_info(test, "selftest: pgsize_bitmap 0x%08lx, IAS %u OAS %u\n",
+ pgsize[i], cfg.ias, cfg.oas);
+ if (arm_lpae_run_tests(test, &cfg))
fail++;
else
pass++;
@@ -203,9 +207,17 @@ static int __init arm_lpae_do_selftests(void)
}
}
- pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
- faux_device_destroy(dev);
-
- return fail ? -EFAULT : 0;
+ kunit_info(test, "selftest: completed with %d PASS %d FAIL\n", pass, fail);
}
-subsys_initcall(arm_lpae_do_selftests);
+
+static struct kunit_case io_pgtable_arm_test_cases[] __refdata = {
+ KUNIT_CASE(io_pgtable_arm_test_run),
+ {},
+};
+
+static struct kunit_suite io_pgtable_arm_test = {
+ .name = "io-pgtable-arm-test",
+ .test_cases = io_pgtable_arm_test_cases,
+};
+
+kunit_test_init_section_suite(io_pgtable_arm_test);
--
2.51.0.384.g4c02a37b29-goog
next prev parent reply other threads:[~2025-09-17 14:04 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-17 14:02 [PATCH 0/2] Move io-pgtable-arm selftest to KUnit Mostafa Saleh
2025-09-17 14:02 ` [PATCH 1/2] iommu/io-pgtable-arm: Move selftests to a separate file Mostafa Saleh
2025-09-17 14:02 ` Mostafa Saleh [this message]
2025-09-17 14:44 ` [PATCH 0/2] Move io-pgtable-arm selftest to KUnit Jason Gunthorpe
2025-09-17 15:00 ` Mostafa Saleh
2025-09-17 15:11 ` Jason Gunthorpe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250917140216.2199055-3-smostafa@google.com \
--to=smostafa@google.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--cc=joro@8bytes.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=praan@google.com \
--cc=robin.murphy@arm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.