iommupt/kunit: Read an exact NR_SECONDARY_PAGETABLE snapshot for the leak check
PATCH 1/1 iommupt/kunit: Read an exact NR_SECONDARY_PAGETABLE snapshot for the leak check
From: Fangyu Yu <[email protected]>
NR_SECONDARY_PAGETABLE is per-CPU batched and only folded into the global counter about once per second. The tests complete within a few seconds, so the init/exit checks can read stale values and randomly fail when a fold lands mid-test, even though no page table is leaked:
# test_map_simple: ASSERTION FAILED at .../kunit_iommu_pt.h:473
Expected priv->orig_nr_secondary_pagetable ==
global_node_page_state(NR_SECONDARY_PAGETABLE), but
priv->orig_nr_secondary_pagetable == 68 (0x44)
global_node_page_state(NR_SECONDARY_PAGETABLE) == 70 (0x46)Sum the per-CPU batches together with the global counter to get an exact snapshot before and after each test case, which makes the check deterministic without any settling delay.
--- drivers/iommu/generic_pt/kunit_iommu_pt.h | 40 ++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-)
drivers/iommu/generic_pt/kunit_iommu_pt.h +36 -4
@@ -4,6 +4,7 @@*/ #include "kunit_iommu.h" #include "pt_iter.h" +#include <linux/cpu.h> #include <linux/generic_pt/iommu.h> #include <linux/iommu.h>
@@ -438,6 +439,36 @@ static struct kunit_case iommu_test_cases[] = { {}, }; +/* +* The global NR_SECONDARY_PAGETABLE counter is batched per-CPU and folded +* into the global value asynchronously, sum the global value and the per-CPU +* deltas to get an exact snapshot. +* +* The global value must be read without the clamp of global_node_page_state(): +* the raw counter is transiently negative whenever a folded free batch +* outweighs allocation deltas still pending on other CPUs, and clamping +* before adding the deltas back would overestimate and report phantom leaks. +* global_node_page_state_monotonic() is normally reserved for monotonic +* counters because a raw negative read is garbage as a page count, but the +* sum with the pending deltas always restores the exact non-negative value. +*/ +static unsigned long pt_kunit_nr_secondary_pagetable (void ) +{ +unsigned long nr =global_node_page_state_monotonic (NR_SECONDARY_PAGETABLE ); + +#ifdef CONFIG_SMP +int nid, cpu; + +cpus_read_lock (); +for_each_online_cpu (cpu) +for_each_online_node (nid) + nr +=per_cpu_ptr (NODE_DATA (nid)->per_cpu_nodestats, + cpu)->vm_node_stat_diff[NR_SECONDARY_PAGETABLE ]; +cpus_read_unlock (); +#endif +return nr; +} +static int pt_kunit_iommu_init (struct kunit *test) {struct kunit_iommu_priv *priv;
@@ -447,8 +478,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);
@@ -461,17 +491,19 @@ static int pt_kunit_iommu_init(struct kunit *test)static void pt_kunit_iommu_exit (struct kunit *test) {struct kunit_iommu_priv *priv = test->priv; +unsigned long nr;if (!test->priv)return ;pt_iommu_deinit (priv->iommu); + nr =pt_kunit_nr_secondary_pagetable (); +/* * 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_ASSERT_EQ (test, priv->orig_nr_secondary_pagetable, nr);kunit_kfree (test, test->priv); }
Signature · 1 line
2.50.1
Quoted 17 lines · On Thu, Oct 15, 2026 at 09:20:24PM +0800, [email protected] wrote:
drivers/iommu/generic_pt/kunit_iommu_pt.h +17
-0+/* +* The global NR_SECONDARY_PAGETABLE counter is batched per-CPU and folded +* into the global value asynchronously, sum the global value and the per-CPU +* deltas to get an exact snapshot. +* +* The global value must be read without the clamp of global_node_page_state(): +* the raw counter is transiently negative whenever a folded free batch +* outweighs allocation deltas still pending on other CPUs, and clamping +* before adding the deltas back would overestimate and report phantom leaks. +* global_node_page_state_monotonic() is normally reserved for monotonic +* counters because a raw negative read is garbage as a page count, but the +* sum with the pending deltas always restores the exact non-negative value. +*/ +static unsigned long pt_kunit_nr_secondary_pagetable (void ) +{ +unsigned long nr =global_node_page_state_monotonic (NR_SECONDARY_PAGETABLE ); +
drivers/iommu/generic_pt/kunit_iommu_pt.h static struct kunit_case iommu_test_cases[] = { · line 468
Quoted 10 lines
drivers/iommu/generic_pt/kunit_iommu_pt.h +10
-0+#ifdef CONFIG_SMP +int nid, cpu; + +cpus_read_lock (); +for_each_online_cpu (cpu) +for_each_online_node (nid) + nr +=per_cpu_ptr (NODE_DATA (nid)->per_cpu_nodestats, + cpu)->vm_node_stat_diff[NR_SECONDARY_PAGETABLE ]; +cpus_read_unlock (); +#endif
This should not be outside the mm code that manages this data structure.
I think the kunit runner executes this in some environment that doesn't trigger this delay? At least I've never seen this assert?
Maybe the test should just be ifdef'd off if that isn't the case?
Jason
Quoted 22 lines
Quoted 22 lines
drivers/iommu/generic_pt/kunit_iommu_pt.h +22
-0+/* +* The global NR_SECONDARY_PAGETABLE counter is batched per-CPU and folded +* into the global value asynchronously, sum the global value and the per-CPU +* deltas to get an exact snapshot. +* +* The global value must be read without the clamp of global_node_page_state(): +* the raw counter is transiently negative whenever a folded free batch +* outweighs allocation deltas still pending on other CPUs, and clamping +* before adding the deltas back would overestimate and report phantom leaks. +* global_node_page_state_monotonic() is normally reserved for monotonic +* counters because a raw negative read is garbage as a page count, but the +* sum with the pending deltas always restores the exact non-negative value. +*/ +static unsigned long pt_kunit_nr_secondary_pagetable (void ) +{ +unsigned long nr =global_node_page_state_monotonic (NR_SECONDARY_PAGETABLE ); + +#ifdef CONFIG_SMP +int nid, cpu; + +cpus_read_lock (); +for_each_online_cpu (cpu)
Quoted 9 lines
drivers/iommu/generic_pt/kunit_iommu_pt.h static struct kunit_case iommu_test_cases[] = { · line 468
Quoted 5 lines
drivers/iommu/generic_pt/kunit_iommu_pt.h +5
-0+for_each_online_node (nid) + nr +=per_cpu_ptr (NODE_DATA (nid)->per_cpu_nodestats, + cpu)->vm_node_stat_diff[NR_SECONDARY_PAGETABLE ]; +cpus_read_unlock (); +#endif This should not be outside the mm code that manages this data structure.
You're right. However, I couldn't find a way to solve this by modifying only the iommupt KUnit code.
Quoted 3 lines
I think the kunit runner executes this in some environment that doesn't trigger this delay? At least I've never seen this assert?
I can reproduce the assert stably on an x86 machine by booting a 4-core riscv64 QEMU.
Quoted 3 lines
Maybe the test should just be ifdef'd off if that isn't the case?
Jason
Quoted 35 lines · On Fri, Oct 16, 2026 at 09:12:29AM +0800, [email protected] wrote:
Quoted 22 lines
Quoted 22 lines
drivers/iommu/generic_pt/kunit_iommu_pt.h +22
-0+/* +* The global NR_SECONDARY_PAGETABLE counter is batched per-CPU and folded +* into the global value asynchronously, sum the global value and the per-CPU +* deltas to get an exact snapshot. +* +* The global value must be read without the clamp of global_node_page_state(): +* the raw counter is transiently negative whenever a folded free batch +* outweighs allocation deltas still pending on other CPUs, and clamping +* before adding the deltas back would overestimate and report phantom leaks. +* global_node_page_state_monotonic() is normally reserved for monotonic +* counters because a raw negative read is garbage as a page count, but the +* sum with the pending deltas always restores the exact non-negative value. +*/ +static unsigned long pt_kunit_nr_secondary_pagetable (void ) +{ +unsigned long nr =global_node_page_state_monotonic (NR_SECONDARY_PAGETABLE ); + +#ifdef CONFIG_SMP +int nid, cpu; + +cpus_read_lock (); +for_each_online_cpu (cpu)Quoted 9 lines
drivers/iommu/generic_pt/kunit_iommu_pt.h static struct kunit_case iommu_test_cases[] = { · line 468
Quoted 5 lines
drivers/iommu/generic_pt/kunit_iommu_pt.h +5
-0+for_each_online_node (nid) + nr +=per_cpu_ptr (NODE_DATA (nid)->per_cpu_nodestats, + cpu)->vm_node_stat_diff[NR_SECONDARY_PAGETABLE ]; +cpus_read_unlock (); +#endif This should not be outside the mm code that manages this data structure.
You're right. However, I couldn't find a way to solve this by modifying only the iommupt KUnit code.
Quoted 6 lines
Quoted 3 lines
I think the kunit runner executes this in some environment that doesn't trigger this delay? At least I've never seen this assert?
I can reproduce the assert stably on an x86 machine by booting a 4-core riscv64 QEMU.
Possibly kunit runner doesn't enable smp. Can you investigate? Maybe we can solve this by disabling the check on !SMP
Jason
Quoted 39 lines
Quoted 39 lines
Quoted 22 lines
Quoted 22 lines
drivers/iommu/generic_pt/kunit_iommu_pt.h +22
-0+/* +* The global NR_SECONDARY_PAGETABLE counter is batched per-CPU and folded +* into the global value asynchronously, sum the global value and the per-CPU +* deltas to get an exact snapshot. +* +* The global value must be read without the clamp of global_node_page_state(): +* the raw counter is transiently negative whenever a folded free batch +* outweighs allocation deltas still pending on other CPUs, and clamping +* before adding the deltas back would overestimate and report phantom leaks. +* global_node_page_state_monotonic() is normally reserved for monotonic +* counters because a raw negative read is garbage as a page count, but the +* sum with the pending deltas always restores the exact non-negative value. +*/ +static unsigned long pt_kunit_nr_secondary_pagetable (void ) +{ +unsigned long nr =global_node_page_state_monotonic (NR_SECONDARY_PAGETABLE ); + +#ifdef CONFIG_SMP +int nid, cpu; + +cpus_read_lock (); +for_each_online_cpu (cpu)Quoted 9 lines
drivers/iommu/generic_pt/kunit_iommu_pt.h static struct kunit_case iommu_test_cases[] = { · line 468
Quoted 5 lines
drivers/iommu/generic_pt/kunit_iommu_pt.h +5
-0+for_each_online_node (nid) + nr +=per_cpu_ptr (NODE_DATA (nid)->per_cpu_nodestats, + cpu)->vm_node_stat_diff[NR_SECONDARY_PAGETABLE ]; +cpus_read_unlock (); +#endif This should not be outside the mm code that manages this data structure.
You're right. However, I couldn't find a way to solve this by modifying only the iommupt KUnit code.
Quoted 3 lines
I think the kunit runner executes this in some environment that doesn't trigger this delay? At least I've never seen this assert?
Quoted 6 lines
Quoted 2 lines
I can reproduce the assert stably on an x86 machine by booting a 4-core riscv64 QEMU.
Possibly kunit runner doesn't enable smp. Can you investigate? Maybe we can solve this by disabling the check on !SMP
Hi Jason,
I looked into this, and you're right: the default KUnit config does not enable SMP. All runs below were done on the current tree without the proposed fix.
1) Default KUnit command, without SMP - the tests pass:
$ ./tools/testing/kunit/kunit.py run 'riscv_64_iommu*' \
--arch x86_64 --kunitconfig drivers/iommu/generic_ptThe generated .config contains "# CONFIG_SMP is not set", and the run completes with:
Testing complete. Ran 21 tests: passed: 18, skipped: 3
2) The same command with SMP enabled - the tests fail:
$ ./tools/testing/kunit/kunit.py run 'riscv_64_iommu*' \
--arch x86_64 --kunitconfig drivers/iommu/generic_pt \
--kconfig_add CONFIG_SMP=y --qemu_args="-smp 4"The run fails consistently. In the example above, 14 of 21 tests failed. With the proposed fix, the same command passes with 18 passed and 3 skipped.
3) Could you clarify what you meant by "disabling the check on !SMP"?
Thanks,
Fangyu
Quoted 1 line
Jason
Quoted 5 lines · On Mon, Oct 19, 2026 at 11:24:55AM +0800, [email protected] wrote:
The run fails consistently. In the example above, 14 of 21 tests failed. With the proposed fix, the same command passes with 18 passed and 3 skipped.
3) Could you clarify what you meant by "disabling the check on !SMP"?
Just
if (!IS_ENABLED(CONFIG_SMP))
With a comment explaining it
Jason
Quoted 4 lines
Quoted 4 lines
The run fails consistently. In the example above, 14 of 21 tests failed. With the proposed fix, the same command passes with 18 passed and 3 skipped.
Quoted 8 lines
Quoted 1 line
3) Could you clarify what you meant by "disabling the check on !SMP"?
Just
if (!IS_ENABLED(CONFIG_SMP))
With a comment explaining it
Got it, v2 will guard the assertion instead of open-coding the vmstat snapshot:
│ if (!IS_ENABLED(CONFIG_SMP)) │ KUNIT_ASSERT_EQ(test, priv->orig_nr_secondary_pagetable, │ global_node_page_state(NR_SECONDARY_PAGETABLE));
Thanks,
Fangyu
Quoted 1 line
Jason