Re: [PATCH] mm/slub: don't let kmemleak report rcu sheaves

From: Catalin Marinas

Date: Tue Nov 17 2026 - 18:05:37 EST


On Mon, Nov 16, 2026 at 11:54:37AM +0100, Vlastimil Babka (SUSE) wrote:
> +CC Breno
>
> On 11/13/26 16:18, Nguyen Ngoc Thang wrote:
> > kmemleak reports full rcu sheaves as leaked:
> >
> > unreferenced object 0xffff88810005f600 (size 512):
> > hex dump (first 32 bytes):
> > e0 79 f0 2b 81 88 ff ff 20 00 b4 81 ff ff ff ff
> > 00 12 04 00 81 88 ff ff 3c 00 00 00 00 00 00 00
> > backtrace:
> > __alloc_empty_sheaf mm/slub.c:2854 [inline]
> > alloc_empty_sheaf mm/slub.c:2874 [inline]
> > __pcs_replace_empty_main+0x260/0x2e0 mm/slub.c:4806
> >
> > The second word is rcu_free_sheaf and the size equals the capacity: the
> > sheaf was handed to call_rcu() by __kfree_rcu_sheaf() and is waiting for
> > a grace period. At that point the only reference to it is the ->next
> > pointer of the previous callback in the rcu callback list.
> >
> > That previous callback may live in memory kmemleak never scans, e.g. the
> > rcu_head of a struct slab queued by rcu_free_slab() for a
> > SLAB_TYPESAFE_BY_RCU cache. Slab pages are frozen (page_count() == 0),
> > so kmemleak skips their struct page. The chain from rcu_data is cut and
> > the sheaf, typically allocated long ago and recycled, looks unreferenced.
> > Since the sheaf is then not scanned either, the objects it is about to
> > free can be reported as well.
>
> I don't get this part. The previous object can't be that much gone, RCU
> still needs to get to this sheaf at some point, so there has to be a live
> pointer somewhere?

After some back and forth with claude, I think there are a couple of
places where kmemleak fails to track. Since commit 9aec2fb0fd5e ("slab:
allocate frozen pages"), kmemleak no longer scans the slab structures in
scan_zone_pages(). We could do something like below:

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 20c0eb41c873..47446af5fc9f 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -1875,9 +1875,13 @@ static int scan_zone_pages(struct zone *zone)
if (!(pfn & 63))
cond_resched_tasks_rcu_qs();

- /* only scan in-use pages belonging to this zone */
+ /*
+ * Only scan in-use pages belonging to this zone. Slab pages
+ * are frozen (zero refcount) but struct slab holds pointers
+ * such as the rcu_head of a slab pending rcu_free_slab().
+ */
if (page && (page_zone(page) != zone ||
- page_count(page) == 0))
+ (page_count(page) == 0 && !PageSlab(page))))
page = NULL;

if (page && first && page == last + 1 &&


I don't think this is specific to sheaves, SLAB_TYPESAFE_BY_RCU may have
a similar problem, though I don't remember to have seen such reports
yet. It should be covered by the above.

However, this only fixes the links through struct slab. The rcu list can
also go through a sheaf allocated with kmalloc_nolock(), which kmemleak
doesn't track. Any callback queued after it is missed (together with the
objects queued in that sheaf that may come from ordinary kfree_rcu()).
When kmemleak is enabled, could we simply avoid allocating sheaves with
kmalloc_nolock(), i.e.:

diff --git a/mm/slub.c b/mm/slub.c
index 45e077c7526c..13b8e57d6dae 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -6278,6 +6278,14 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, unsigned int free_flags)

local_unlock(&s->cpu_sheaves->lock);

+ /*
+ * A sheaf allocated with kmalloc_nolock() is not tracked by
+ * kmemleak, so neither its objects nor the rcu callback queued
+ * after it would be scanned. Fall back to defer_kfree_rcu().
+ */
+ if (IS_ENABLED(CONFIG_DEBUG_KMEMLEAK) && !allow_spin)
+ goto fail;
+
/* Don't wake up kswapd, it will cause deadlock under pi_lock */
empty = alloc_empty_sheaf(s, __GFP_NOWARN, alloc_flags);


Not tested and it's possible I misunderstood how all this works. But if
it works, I don't think the kmemleak_not_leak() annotation is needed.

--
Catalin