mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] radix-tree: fix kmemleak false positives on tree head reassignment
@ 2026-07-03 15:22 Breno Leitao
  0 siblings, 0 replies; only message in thread
From: Breno Leitao @ 2026-07-03 15:22 UTC (permalink / raw)
  To: Andrew Morton, catalin.marinas
  Cc: linux-kernel, bpf, kent.overstreet, arnd, bigeasy, kangas,
	kernel-team, Breno Leitao

Kmemleak periodically reports transient false positives for radix tree
nodes allocated through the IDR, for example:

  unreferenced object 0xffff0004d6ac4200 (size 576):
    comm "tcpeventd", pid 6412
    backtrace (crc 335d668a):
      kmem_cache_alloc_noprof
      radix_tree_node_alloc
      radix_tree_extend
      idr_get_free
      idr_alloc_cyclic
      map_create
      __sys_bpf

radix_tree_extend() (grow) and radix_tree_shrink() (shrink) repoint
root->xa_head to a new node.  If a kmemleak scan has already walked past
root->xa_head, the new head is not reachable from any scanned pointer
until the following scan, so kmemleak reports it as leaked even though it
is live.

This is the same race fixed for the XArray API in commit a1a029bcea59
("XArray: fix kmemleak false positive in xas_shrink()").  The IDR uses
the radix tree API directly and hits it on both the grow and the shrink
path, so mark the new head as a transient leak in both.

Add a matching kmemleak_transient_leak() stub to the radix tree test
harness so the userspace lib/radix-tree.c build keeps building.

Signed-off-by: Breno Leitao <leitao@debian.org>
---
Changes in v2:
- Add a kmemleak_transient_leak() stub to the radix tree test harness
  (tools/testing/shared/linux/kmemleak.h) so the userspace
  lib/radix-tree.c build keeps working; reported by the kernel test robot.
- Link to v1: https://lore.kernel.org/r/20260702-radix-tree-v1-1-a87c125b0f96@debian.org

To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org
Cc: bpf@vger.kernel.org
---
 lib/radix-tree.c                      | 7 ++++++-
 tools/testing/shared/linux/kmemleak.h | 1 +
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/radix-tree.c b/lib/radix-tree.c
index 976b9bd02a1b5..cb5ca920fddb6 100644
--- a/lib/radix-tree.c
+++ b/lib/radix-tree.c
@@ -455,6 +455,8 @@ static int radix_tree_extend(struct radix_tree_root *root, gfp_t gfp,
 		node->slots[0] = (void __rcu *)entry;
 		entry = node_to_entry(node);
 		rcu_assign_pointer(root->xa_head, entry);
+		/* new head may be missed by an in-progress kmemleak scan */
+		kmemleak_transient_leak(node);
 		shift += RADIX_TREE_MAP_SHIFT;
 	} while (shift <= maxshift);
 out:
@@ -495,8 +497,11 @@ static inline bool radix_tree_shrink(struct radix_tree_root *root)
 		if (!node->shift && is_idr(root))
 			break;
 
-		if (radix_tree_is_internal_node(child))
+		if (radix_tree_is_internal_node(child)) {
 			entry_to_node(child)->parent = NULL;
+			/* new head may be missed by an in-progress kmemleak scan */
+			kmemleak_transient_leak(entry_to_node(child));
+		}
 
 		/*
 		 * We don't need rcu_assign_pointer(), since we are simply
diff --git a/tools/testing/shared/linux/kmemleak.h b/tools/testing/shared/linux/kmemleak.h
index 155f112786c4f..65a383090fb51 100644
--- a/tools/testing/shared/linux/kmemleak.h
+++ b/tools/testing/shared/linux/kmemleak.h
@@ -1 +1,2 @@
 static inline void kmemleak_update_trace(const void *ptr) { }
+static inline void kmemleak_transient_leak(const void *ptr) { }

---
base-commit: 840ef6c78e6a2f694b578ecb9063241c992aaa9e
change-id: 20260702-radix-tree-8ae41d324101

Best regards,
--  
Breno Leitao <leitao@debian.org>


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-07-03 15:22 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-03 15:22 [PATCH v2] radix-tree: fix kmemleak false positives on tree head reassignment Breno Leitao

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®