[PATCH v3 05/13] iommupt/kunit: Skip test configs without supported features

Nicolin Chen nicolinc at nvidia.com
Mon Nov 23 21:29:52 PST 2026


On Sun, Nov 15, 2026 at 07:30:35PM -0400, Jason Gunthorpe wrote:
> Some of the tests configs can test features we don't currently enable,
> like TTBR1 on ARMv8. Instead of blowing up, skip the test. When
> CONFIG_DEBUG_GENERIC_PT is set all features are force enabled during
> the build so everything can be tested. That is the normal mode for the
> kunit.py runner.
> 
> Reviewed-by: Pranjal Shrivastava <praan at google.com>
> Signed-off-by: Jason Gunthorpe <jgg at nvidia.com>
> ---
>  drivers/iommu/generic_pt/kunit_iommu.h | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/drivers/iommu/generic_pt/kunit_iommu.h b/drivers/iommu/generic_pt/kunit_iommu.h
> index 79d7f9c8e7868f..558e161eacb05f 100644
> --- a/drivers/iommu/generic_pt/kunit_iommu.h
> +++ b/drivers/iommu/generic_pt/kunit_iommu.h
> @@ -134,6 +134,16 @@ static int pt_kunit_priv_init(struct kunit *test, struct kunit_iommu_priv *priv)
>  
>  #ifdef kunit_fmt_cfgs
>  	priv->cfg = kunit_fmt_cfgs[((uintptr_t)test->param_value) - 1];
> +	/*
> +	 * Without CONFIG_DEBUG_GENERIC_PT only the format's compiled-in
> +	 * PT_SUPPORTED_FEATURES are available, so cfgs requesting bits outside
> +	 * that set cannot be tested.
> +	 */
> +	if (priv->cfg.common.features & ~KUNIT_PT_SUPPORTED_FEATURES)
> +		kunit_skip(
> +			test,
> +			"cfg requires features not built into this format (enable CONFIG_DEBUG_GENERIC_PT)");

The patch looks fine.

But I tried iommu_pt_kunit_test on Grace with CONFIG_PAGE_SIZE_64KB
and got this assertion:

------------------------------------------------------------
 KTAP version 1
 1..6
     KTAP version 1
     # Subtest: amdv1_mock_iommu_test
     # module: kunit_iommu_mock
     1..7
         KTAP version 1
         # Subtest: test_increase_level
         ok 1 amdv1_mock_cfg_0
         ok 2 amdv1_mock_cfg_1 # SKIP max_vasz_lg2 fits in starting level, no growth possible
     # test_increase_level: pass:1 fail:0 skip:1 total:2
     ok 1 test_increase_level
         KTAP version 1
         # Subtest: test_map_simple
         ok 1 amdv1_mock_cfg_0
         ok 2 amdv1_mock_cfg_1
     # test_map_simple: pass:2 fail:0 skip:0 total:2
     ok 2 test_map_simple
         KTAP version 1
         # Subtest: test_map_table_to_oa
     # test_map_table_to_oa: Test should be marked slow (runtime: 4.997639265s)
     # test_map_table_to_oa: ASSERTION FAILED at drivers/iommu/generic_pt/fmt/../kunit_iommu_pt.h:473
                   Expected priv->orig_nr_secondary_pagetable == global_node_page_state(NR_SECONDARY_PAGETABLE), but
                       priv->orig_nr_secondary_pagetable == 157 (0x9d)
                       global_node_page_state(NR_SECONDARY_PAGETABLE) == 174 (0xae)
     # test_map_table_to_oa: test aborted during cleanup. continuing without cleaning up
 [...and all other tests continued but all failed...]
------------------------------------------------------------

I asked AI to fix the issue, and it came up with a patch (attached).
I wonder if it makes sense to you.

Nicolin

>From c7e0332ce5c2854e4ff23ac7cb024ad837d11986 Mon Sep 17 00:00:00 2001
From: Nicolin Chen <nicolinc at nvidia.com>
Date: Tue, 24 Nov 2026 04:41:11 +0000
Subject: [PATCH] iommupt/kunit: Include per-CPU deltas in the leak check

The leak check reads NR_SECONDARY_PAGETABLE with global_node_page_state().
That only returns the global counter. Each CPU also keeps a small delta of
page updates that is folded into the global counter only after it exceeds
a threshold. So the value can be off by a few pages without any leak.

On an arm64 KVM guest with 64 KiB pages, test_map_simple frees every page
but still sees 5 extra pages. The assertion fails in the exit callback and
aborts the cleanup. Then the dummy device stays registered, and every later
test case fails to init with -EBUSY.

Add pt_kunit_nr_secondary_pagetable(). It adds the per-CPU deltas of every
node to the global counter. Use it for both the initial and the final read.
The result is still a best-effort snapshot, because a concurrent fold can
move a delta from a CPU to the global counter between the two reads.

Also change the assertion into an expectation. A mismatch still fails the
test case, but the exit callback now finishes its cleanup. So a mismatch no
longer breaks the test cases that run after it.

Fixes: bc5233c0904e ("iommupt: Add a kunit test for the IOMMU implementation")
Signed-off-by: Nicolin Chen <nicolinc at nvidia.com>
---
 drivers/iommu/generic_pt/kunit_iommu.h    |  2 +-
 drivers/iommu/generic_pt/kunit_iommu_pt.h | 29 +++++++++++++++++++----
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/drivers/iommu/generic_pt/kunit_iommu.h b/drivers/iommu/generic_pt/kunit_iommu.h
index 558e161eacb05..3c098be0d8f3f 100644
--- a/drivers/iommu/generic_pt/kunit_iommu.h
+++ b/drivers/iommu/generic_pt/kunit_iommu.h
@@ -84,7 +84,7 @@ struct kunit_iommu_priv {
 	unsigned int largest_pgsz_lg2;
 	pt_oaddr_t test_oa;
 	pt_vaddr_t safe_pgsize_bitmap;
-	unsigned long orig_nr_secondary_pagetable;
+	long orig_nr_secondary_pagetable;
 
 };
 PT_IOMMU_CHECK_DOMAIN(struct kunit_iommu_priv, fmt_table.iommu, domain);
diff --git a/drivers/iommu/generic_pt/kunit_iommu_pt.h b/drivers/iommu/generic_pt/kunit_iommu_pt.h
index 9fc71db74234d..29b882dd5b0ed 100644
--- a/drivers/iommu/generic_pt/kunit_iommu_pt.h
+++ b/drivers/iommu/generic_pt/kunit_iommu_pt.h
@@ -438,6 +438,28 @@ static struct kunit_case iommu_test_cases[] = {
 	{},
 };
 
+/*
+ * global_node_page_state() only reads the global counter. Each CPU also keeps
+ * a small delta that might not be folded into the global counter yet. Add the
+ * deltas of every CPU on every node to get a closer count. This is still just
+ * a best-effort snapshot. A concurrent fold can move a delta between the reads.
+ */
+static long pt_kunit_nr_secondary_pagetable(void)
+{
+	const enum node_stat_item item = NR_SECONDARY_PAGETABLE;
+	long nr = atomic_long_read(&vm_node_stat[item]);
+	int nid, cpu;
+
+	for_each_online_node(nid) {
+		struct per_cpu_nodestat __percpu *stats =
+			NODE_DATA(nid)->per_cpu_nodestats;
+
+		for_each_online_cpu(cpu)
+			nr += per_cpu_ptr(stats, cpu)->vm_node_stat_diff[item];
+	}
+	return nr;
+}
+
 static int pt_kunit_iommu_init(struct kunit *test)
 {
 	struct kunit_iommu_priv *priv;
@@ -447,8 +469,7 @@ static int pt_kunit_iommu_init(struct kunit *test)
 	if (!priv)
 		return -ENOMEM;
 
-	priv->orig_nr_secondary_pagetable =
-		global_node_page_state(NR_SECONDARY_PAGETABLE);
+	priv->orig_nr_secondary_pagetable = pt_kunit_nr_secondary_pagetable();
 	ret = pt_kunit_priv_init(test, priv);
 	if (ret) {
 		kunit_kfree(test, priv);
@@ -470,8 +491,8 @@ static void pt_kunit_iommu_exit(struct kunit *test)
 	 * Look for memory leaks, assumes kunit is running isolated and nothing
 	 * else is using secondary page tables.
 	 */
-	KUNIT_ASSERT_EQ(test, priv->orig_nr_secondary_pagetable,
-			global_node_page_state(NR_SECONDARY_PAGETABLE));
+	KUNIT_EXPECT_EQ(test, priv->orig_nr_secondary_pagetable,
+			pt_kunit_nr_secondary_pagetable());
 	kunit_kfree(test, test->priv);
 }
 
-- 
2.43.0



More information about the linux-arm-kernel mailing list