All of lore.kernel.org
 help / color / mirror / Atom feed
From: Catalin Marinas <catalin.marinas@arm.com>
To: sashiko-reviews@lists.linux.dev
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Weilin Tong <tongweilin@linux.alibaba.com>,
	Breno Leitao <leitao@debian.org>,
	David Hildenbrand <david@kernel.org>,
	linux-mm@kvack.org
Subject: Re: [PATCH 1/3] mm: kmemleak: take kmemleak_lock in the disable-time object cleanup
Date: Sat, 5 Dec 2026 10:37:02 +0000	[thread overview]
Message-ID: <axPpToUdk5qt-1ax@arm.com> (raw)
In-Reply-To: <sashiko-outbox-250094@kernel.org>

On Fri, Dec 04, 2026 at 03:25:49PM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] mm: kmemleak: double remove and UAF in
>   __kmemleak_do_cleanup()
> --
> 
> commit 48955aa5e1c8f3cd5cbd6cee7646e58ca94dc439
> Author: Catalin Marinas <catalin.marinas@arm.com>
> 
> mm: kmemleak: take kmemleak_lock in the disable-time object cleanup
> 
> This commit takes kmemleak_lock around removing each object from
> object_list in __kmemleak_do_cleanup() by repeatedly popping the first
> entry instead of traversing the list unlocked with
> list_for_each_entry_safe().
> 
> Link: https://lore.kernel.org/r/149d93f2cfe20d355f4d9882ce2bf99030217772.1793871596.git.tongweilin@linux.alibaba.com
> 
> > diff --git a/mm/kmemleak.c b/mm/kmemleak.c
> > index 20c0eb41c8730..9c2d01fd41134 100644
> > --- a/mm/kmemleak.c
> > +++ b/mm/kmemleak.c
> 
> [ ... ]
> 
> > @@ -2473,15 +2473,25 @@ static const struct file_operations kmemleak_fops = {
> >  
> >  static void __kmemleak_do_cleanup(void)
> >  {
> > -	struct kmemleak_object *object, *tmp;
> > +	struct kmemleak_object *object;
> > +	unsigned long flags;
> >  	unsigned int cnt = 0;
> >  
> >  	/*
> > -	 * Kmemleak has already been disabled, no need for RCU list traversal
> > -	 * or kmemleak_lock held.
> > +	 * Callbacks that passed their enabled checks before kmemleak was
> > +	 * disabled may still be adding or removing objects.
> >  	 */
> > -	list_for_each_entry_safe(object, tmp, &object_list, object_list) {
> > -		__remove_object(object);
> > +	while (true) {
> > +		raw_spin_lock_irqsave(&kmemleak_lock, flags);
> > +		object = list_first_entry_or_null(&object_list,
> > +						  struct kmemleak_object,
> > +						  object_list);
> > +		if (object)
> > +			__remove_object(object);
> > +		raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
> > +
> > +		if (!object)
> > +			break;
> >  		__delete_object(object);
> 
> [Severity: Medium]
> Can this loop remove and delete the same kmemleak_object twice when
> DELSTATE_NO_DELETE is set?
> 
> __remove_object() skips list_del_rcu(&object->object_list) when
> DELSTATE_NO_DELETE is set, leaving the object at the head of object_list
> while still erasing its rb_node and setting DELSTATE_REMOVED.

Thanks. I'm aware of this one (already saw the report on sashiko.dev).
I'll post a fix next week (life was simpler pre-AI, I thought all this
code was race-free ;)).

The race isn't new and Weilin's patch ("mm: kmemleak: serialize
disable-time cleanup"; now dropped) would have hidden this specific
report since the cleanup held scan_mutex and the restarted scan thread
had to wait. However, while digging (with Claude's help), I found two
more races in this area between the scan thread start/stop and kmemleak
disabling.

There's also an unrelated, pre-existing deadlock between "scan=off" and
the scan thread waiting on scan_mutex. I'll post it together with the
above.

Andrew, do you have a preference whether you want the fixes on top of
this series or just re-spin a v2 of 5 patches?

-- 
Catalin


  reply	other threads:[~2026-12-05 10:37 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 15:25 [PATCH 0/3] mm: kmemleak: fix the disable-time cleanup races Catalin Marinas
2026-12-03 15:25 ` [PATCH 1/3] mm: kmemleak: take kmemleak_lock in the disable-time object cleanup Catalin Marinas
2026-12-03 18:40   ` + mm-kmemleak-take-kmemleak_lock-in-the-disable-time-object-cleanup.patch added to mm-new branch Andrew Morton
2026-12-04 15:25   ` [PATCH 1/3] mm: kmemleak: take kmemleak_lock in the disable-time object cleanup sashiko-bot
2026-12-05 10:37     ` Catalin Marinas [this message]
2026-12-05 17:35       ` Andrew Morton
2026-12-03 15:25 ` [PATCH 2/3] mm: kmemleak: do not hex dump objects once free tracking has stopped Catalin Marinas
2026-12-03 18:40   ` + mm-kmemleak-do-not-hex-dump-objects-once-free-tracking-has-stopped.patch added to mm-new branch Andrew Morton
2026-12-03 15:25 ` [PATCH 3/3] mm: kmemleak: annotate the lockless accesses to the enable flags Catalin Marinas
2026-12-03 18:40   ` + mm-kmemleak-annotate-the-lockless-accesses-to-the-enable-flags.patch added to mm-new branch Andrew Morton

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=axPpToUdk5qt-1ax@arm.com \
    --to=catalin.marinas@arm.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=leitao@debian.org \
    --cc=linux-mm@kvack.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tongweilin@linux.alibaba.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.