From: Catalin Marinas <catalin.marinas@arm.com>
To: Breno Leitao <leitao@debian.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
kernel-team@meta.com
Subject: Re: [PATCH] mm: kmemleak: scan modified objects as soon as they are grayed
Date: Mon, 19 Oct 2026 16:56:53 +0100 [thread overview]
Message-ID: <atY9xT4MfPL7R8fo@arm.com> (raw)
In-Reply-To: <20261016-kmemleak-gray-drain-v1-1-3270256fdc8e@debian.org>
On Fri, Oct 16, 2026 at 09:07:24AM -0700, Breno Leitao wrote:
> diff --git a/mm/kmemleak.c b/mm/kmemleak.c
> index 8fa409a4f9fb26..6e12bcd95312d2 100644
> --- a/mm/kmemleak.c
> +++ b/mm/kmemleak.c
> @@ -1696,8 +1696,10 @@ static void scan_gray_list(void)
> * Conditionally call resched() in an object iteration loop while making sure
> * that the given object won't go away without RCU read lock by performing a
> * get_object() if necessaary.
> + * If @scan_gray is set, also scan the gray list while the RCU lock is dropped.
> */
> -static void kmemleak_cond_resched(struct kmemleak_object *object)
> +static void kmemleak_cond_resched(struct kmemleak_object *object,
> + bool scan_gray)
> {
> if (!get_object(object))
> return; /* Try next object */
> @@ -1709,6 +1711,8 @@ static void kmemleak_cond_resched(struct kmemleak_object *object)
> raw_spin_unlock_irq(&kmemleak_lock);
>
> rcu_read_unlock();
> + if (scan_gray)
> + scan_gray_list();
> cond_resched_tasks_rcu_qs();
> rcu_read_lock();
>
> @@ -1910,7 +1914,7 @@ static int __kmemleak_scan(bool full)
> raw_spin_unlock_irq(&object->lock);
>
> if (need_resched())
> - kmemleak_cond_resched(object);
> + kmemleak_cond_resched(object, false);
> }
> rcu_read_unlock();
>
> @@ -1981,8 +1985,13 @@ static int __kmemleak_scan(bool full)
> */
> rcu_read_lock();
> list_for_each_entry_rcu(object, &object_list, object_list) {
> - if (need_resched())
> - kmemleak_cond_resched(object);
> + /*
> + * Scan newly grayed objects now, not after the walk: one may
> + * hold the only reference to another and be freed by then,
> + * like a maple tree node that keeps being replaced.
> + */
> + if (need_resched() || !list_empty(&gray_list))
> + kmemleak_cond_resched(object, true);
I think that's a good way to reduce the false positives for such maple
tree scenario. Do you have any numbers on how much this extends the scan
time by? If it's too expensive, we could rate-limit it, say every jiffy.
>
> /*
> * This is racy but we can save the overhead of lock/unlock
> @@ -2082,7 +2091,7 @@ static void kmemleak_scan(void)
> bool dedup_print;
>
> if (need_resched())
> - kmemleak_cond_resched(object);
> + kmemleak_cond_resched(object, false);
Nitpick: instead of passing 'false' in a few places, rename
kmemleak_cond_resched() to kmemleak_resched_drain() (well, some better
name) call get kmemleak_cond_resched() to call that with 'false'. We
could also move need_resched() in such function, being a small wrapper
the compiler should inline it.
Either way,
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
prev parent reply other threads:[~2026-10-19 15:57 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-16 16:07 [PATCH] mm: kmemleak: scan modified objects as soon as they are grayed Breno Leitao
2026-10-19 15:56 ` Catalin Marinas [this message]
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=atY9xT4MfPL7R8fo@arm.com \
--to=catalin.marinas@arm.com \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=kernel-team@meta.com \
--cc=leitao@debian.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.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