mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] iommupt/kunit: Skip the page-table leak check on SMP
@ 2026-10-31  8:55 fangyu.yu
  2026-11-16 19:21 ` Jason Gunthorpe
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: fangyu.yu @ 2026-10-31  8:55 UTC (permalink / raw)
  To: joro, will, robin.murphy, joerg.roedel, jgg, jgg, dan,
	vasant.hegde, kevin.tian, Ankit.Soni
  Cc: iommu, linux-kernel, guoren, Fangyu Yu

From: Fangyu Yu <fangyu.yu@linux.alibaba.com>

NR_SECONDARY_PAGETABLE is batched per-CPU on SMP, so the global counter
can change as pending updates are folded, even when a test has freed all
its page tables. Comparing the global counter before and after a test
can therefore report a false leak. A failed teardown assertion also
aborts cleanup, causing subsequent tests to fail because the test driver
is still registered.

Only perform the leak-check assertion on non-SMP configurations, where
there are no per-CPU vmstat deltas. Keep the tests and resource cleanup
enabled on SMP without accessing MM's internal vmstat data structures.

Fixes: bc5233c0904e ("iommupt: Add a kunit test for the IOMMU implementation")
Signed-off-by: Fangyu Yu <fangyu.yu@linux.alibaba.com>
---
Changes in v2:
  - Replace the hand-written vmstat snapshot with a non-SMP-only
    assertion, as suggested by Jason.

Link to v1:
  https://lore.kernel.org/linux-iommu/20261015132024.43998-1-fangyu.yu@linux.alibaba.com/
---
 drivers/iommu/generic_pt/kunit_iommu_pt.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/generic_pt/kunit_iommu_pt.h b/drivers/iommu/generic_pt/kunit_iommu_pt.h
index 9fc71db74234..d90e57b2cd7b 100644
--- a/drivers/iommu/generic_pt/kunit_iommu_pt.h
+++ b/drivers/iommu/generic_pt/kunit_iommu_pt.h
@@ -470,8 +470,10 @@ 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));
+	/* Per-CPU vmstat batching can cause false leak reports on SMP. */
+	if (!IS_ENABLED(CONFIG_SMP))
+		KUNIT_ASSERT_EQ(test, priv->orig_nr_secondary_pagetable,
+				global_node_page_state(NR_SECONDARY_PAGETABLE));
 	kunit_kfree(test, test->priv);
 }
 
-- 
2.50.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] iommupt/kunit: Skip the page-table leak check on SMP
  2026-10-31  8:55 [PATCH v2] iommupt/kunit: Skip the page-table leak check on SMP fangyu.yu
@ 2026-11-16 19:21 ` Jason Gunthorpe
  2026-11-19  5:19 ` desobei
  2026-11-23  9:25 ` Jörg Rödel
  2 siblings, 0 replies; 4+ messages in thread
From: Jason Gunthorpe @ 2026-11-16 19:21 UTC (permalink / raw)
  To: fangyu.yu
  Cc: joro, will, robin.murphy, joerg.roedel, dan, vasant.hegde,
	kevin.tian, Ankit.Soni, iommu, linux-kernel, guoren

On Sat, Oct 31, 2026 at 04:55:36PM +0800, fangyu.yu@linux.alibaba.com wrote:
> From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
> 
> NR_SECONDARY_PAGETABLE is batched per-CPU on SMP, so the global counter
> can change as pending updates are folded, even when a test has freed all
> its page tables. Comparing the global counter before and after a test
> can therefore report a false leak. A failed teardown assertion also
> aborts cleanup, causing subsequent tests to fail because the test driver
> is still registered.
> 
> Only perform the leak-check assertion on non-SMP configurations, where
> there are no per-CPU vmstat deltas. Keep the tests and resource cleanup
> enabled on SMP without accessing MM's internal vmstat data structures.
> 
> Fixes: bc5233c0904e ("iommupt: Add a kunit test for the IOMMU implementation")
> Signed-off-by: Fangyu Yu <fangyu.yu@linux.alibaba.com>
> ---
> Changes in v2:
>   - Replace the hand-written vmstat snapshot with a non-SMP-only
>     assertion, as suggested by Jason.
> 
> Link to v1:
>   https://lore.kernel.org/linux-iommu/20261015132024.43998-1-fangyu.yu@linux.alibaba.com/
> ---
>  drivers/iommu/generic_pt/kunit_iommu_pt.h | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>

Jason

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] iommupt/kunit: Skip the page-table leak check on SMP
  2026-10-31  8:55 [PATCH v2] iommupt/kunit: Skip the page-table leak check on SMP fangyu.yu
  2026-11-16 19:21 ` Jason Gunthorpe
@ 2026-11-19  5:19 ` desobei
  2026-11-23  9:25 ` Jörg Rödel
  2 siblings, 0 replies; 4+ messages in thread
From: desobei @ 2026-11-19  5:19 UTC (permalink / raw)
  To: fangyu.yu
  Cc: joro, will, robin.murphy, joerg.roedel, jgg, jgg, dan,
	vasant.hegde, kevin.tian, Ankit.Soni, iommu, linux-kernel,
	guoren

On Sat, Oct 31, 2026 at 04:55:36PM +0800, fangyu.yu@linux.alibaba.com wrote:
> NR_SECONDARY_PAGETABLE is batched per-CPU on SMP, so the global counter
> can change as pending updates are folded, even when a test has freed all
> its page tables. Comparing the global counter before and after a test
> can therefore report a false leak. A failed teardown assertion also
> aborts cleanup, causing subsequent tests to fail because the test driver
> is still registered.

I hit this independently on next-20261117, with the KUnit tests built
in and booted in x86_64 QEMU/KVM guests with KASAN, lockdep, UBSAN and
kmemleak enabled. The first false report aborted the cleanup, and every
later test case of the amdv1, vtdss and x86_64 iommu_test and fmt_test
suites then failed with "Driver 'pt_kunit_dev' is already registered".

With this patch applied, all six suites pass with 1 and 8 vCPUs, and
there are no KASAN, UBSAN or lockdep reports.

Tested-by: desobei <desobei@outlook.com>

>  	 * 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));
> +	/* Per-CPU vmstat batching can cause false leak reports on SMP. */
> +	if (!IS_ENABLED(CONFIG_SMP))
> +		KUNIT_ASSERT_EQ(test, priv->orig_nr_secondary_pagetable,
> +				global_node_page_state(NR_SECONDARY_PAGETABLE));

A small follow-up thought, not a blocker: on !SMP this is still an
assertion in the exit handler, so a real leak there would again skip the
cleanup and break the suites that run afterwards. KUNIT_EXPECT_EQ()
would still fail the test case but let the cleanup finish. In my testing
on SMP, switching the original check to KUNIT_EXPECT_EQ() was enough to
stop the "already registered" failures.

I debugged this with the help of an AI coding assistant
(Claude Opus 5.5).

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] iommupt/kunit: Skip the page-table leak check on SMP
  2026-10-31  8:55 [PATCH v2] iommupt/kunit: Skip the page-table leak check on SMP fangyu.yu
  2026-11-16 19:21 ` Jason Gunthorpe
  2026-11-19  5:19 ` desobei
@ 2026-11-23  9:25 ` Jörg Rödel
  2 siblings, 0 replies; 4+ messages in thread
From: Jörg Rödel @ 2026-11-23  9:25 UTC (permalink / raw)
  To: fangyu.yu
  Cc: will, robin.murphy, joerg.roedel, jgg, jgg, dan, vasant.hegde,
	kevin.tian, Ankit.Soni, iommu, linux-kernel, guoren

On Sat, Oct 31, 2026 at 04:55:36PM +0800, fangyu.yu@linux.alibaba.com wrote:
>  drivers/iommu/generic_pt/kunit_iommu_pt.h | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)

Applied, thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-23  9:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-31  8:55 [PATCH v2] iommupt/kunit: Skip the page-table leak check on SMP fangyu.yu
2026-11-16 19:21 ` Jason Gunthorpe
2026-11-19  5:19 ` desobei
2026-11-23  9:25 ` Jörg Rödel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®