Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Catalin Marinas <catalin.marinas@arm.com>
To: "Vlastimil Babka (SUSE)" <vbabka@kernel.org>
Cc: Nguyen Ngoc Thang <ngocthang2710.1999@gmail.com>,
	Harry Yoo <harry@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Hao Li <hao.li@linux.dev>, Christoph Lameter <cl@gentwo.org>,
	David Rientjes <rientjes@google.com>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Suren Baghdasaryan <surenb@google.com>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	syzbot+527fed49e5b988882c9a@syzkaller.appspotmail.com,
	Breno Leitao <leitao@debian.org>
Subject: Re: [PATCH] mm/slub: don't let kmemleak report rcu sheaves
Date: Wed, 18 Nov 2026 16:06:29 +0000	[thread overview]
Message-ID: <av3NBYxYXyYk_tjS@arm.com> (raw)
In-Reply-To: <avzduIJralOKBgxK@arm.com>

On Tue, Nov 17, 2026 at 11:05:28PM +0000, Catalin Marinas wrote:
> 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.

This still leaves a hole. Scanning the slab descriptor recovers the link
through slab->rcu_head.next, but the preceding callback can also be
free_object_rcu() whose rcu_head is embedded in a kmemleak metadata
object allocated from a SLAB_NOLEAKTRACE cache. This would break the
chain.

So I think we should keep the original kmemleak_not_leak() patch at
allocation.

> 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);
>  

For the objects queued in a sheaf, rather than the !allow_spin fallback
above, it's simpler to move the kmemleak_ignore() call in
kvfree_call_rcu() before kfree_rcu_sheaf(), so they are treated the same
as on the non-sheaf path. kfree_rcu_nolock() currently only has a kunit
user, freeing kmalloc_nolock() objects that kmemleak doesn't track
anyway.

So this second hunk isn't needed either.

To Vlastimil's earlier point, kmemleak could follow these rcu chains
instead of relying on kmemleak_not_leak(). However, with chain breaks at
any _nolock or SLAB_NOLEAKTRACE object (including kmemleak's own
metadata) and walking the rcu callback lists directly means digging
into rcu internals. The annotation is easier to maintain.

(got lost in all the slub code, it was much simpler 17 years ago when we
merged kmemleak ;))

-- 
Catalin


  reply	other threads:[~2026-11-18 16:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 15:18 [PATCH] mm/slub: don't let kmemleak report rcu sheaves Nguyen Ngoc Thang
2026-11-16 10:54 ` Vlastimil Babka (SUSE)
2026-11-17 23:05   ` Catalin Marinas
2026-11-18 16:06     ` Catalin Marinas [this message]
2026-11-18 16:10 ` Catalin Marinas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=av3NBYxYXyYk_tjS@arm.com \
    --to=catalin.marinas@arm.com \
    --cc=akpm@linux-foundation.org \
    --cc=cl@gentwo.org \
    --cc=hao.li@linux.dev \
    --cc=harry@kernel.org \
    --cc=leitao@debian.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ngocthang2710.1999@gmail.com \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=surenb@google.com \
    --cc=syzbot+527fed49e5b988882c9a@syzkaller.appspotmail.com \
    --cc=vbabka@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox