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>,
Lorenzo Stoakes <ljs@kernel.org>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>, Shuah Khan <shuah@kernel.org>,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
linux-kselftest@vger.kernel.org, kernel-team@meta.com
Subject: Re: [PATCH 1/2] mm/kmemleak: dedupe verbose scan output by allocation backtrace
Date: Thu, 23 Apr 2026 15:29:08 +0100 [thread overview]
Message-ID: <aeostKcaHzmVBFjb@arm.com> (raw)
In-Reply-To: <20260421-kmemleak_dedup-v1-1-65e31c6cdf0c@debian.org>
On Tue, Apr 21, 2026 at 06:45:04AM -0700, Breno Leitao wrote:
> +/*
> + * Record a leaked object in the dedup table. The representative object's
> + * use_count is incremented so it can be safely dereferenced by dedup_flush()
> + * outside the RCU read section; dedup_flush() drops the reference. On
> + * allocation failure (or a concurrent insert) the object is printed
> + * immediately, preserving today's "always log every leak" guarantee.
> + * Caller must not hold object->lock and must hold rcu_read_lock().
> + */
> +static void dedup_record(struct xarray *dedup, struct kmemleak_object *object,
> + depot_stack_handle_t trace_handle)
> +{
> + struct kmemleak_dedup_entry *entry;
> +
> + entry = xa_load(dedup, trace_handle);
> + if (entry) {
> + /* This is a known beast, just increase the counter */
> + entry->count++;
> + return;
> + }
> +
> + /*
> + * A brand new report. Object will have object->use_count increased
> + * in here, and released put_object() at dedup_flush
> + */
> + entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Do we need to allocate a structure here? We could instead add a
dup_count member in the kmemleak_object and just link the object itself
into the xarray. Well, maybe the leak being a rare event is not that
bad.
> + if (entry && get_object(object)) {
> + if (xa_insert(dedup, trace_handle, entry, GFP_ATOMIC) == 0) {
I wonder if we need xa_insert() at all. Since it's indexed by
trace_handle, we could follow similar mechanism like stack_depot with a
large hash array, maybe gated by CONFIG_DEBUG_KMEMLEAK_VERBOSE.
> + entry->object = object;
> + entry->count = 1;
> + return;
> + }
> + put_object(object);
> + }
> + kfree(entry);
> +
> + /*
> + * Fallback for kmalloc/get_object(): Just print it straight away
> + */
> + raw_spin_lock_irq(&object->lock);
> + print_unreferenced(NULL, object);
> + raw_spin_unlock_irq(&object->lock);
> +}
> +
> +/*
> + * Drain the dedup table: print one full record per unique backtrace,
> + * followed by a summary line whenever more than one object shared it.
> + * Releases the reference dedup_record() took on each representative object.
> + */
> +static void dedup_flush(struct xarray *dedup)
> +{
> + struct kmemleak_dedup_entry *entry;
> + unsigned long idx;
> +
> + xa_for_each(dedup, idx, entry) {
> + raw_spin_lock_irq(&entry->object->lock);
> + print_unreferenced(NULL, entry->object);
> + raw_spin_unlock_irq(&entry->object->lock);
Sashiko has a good point here - while the kmemleak metadata is still
around due to an earlier get_object(), the object itself may have been
freed and the hex dump in print_unreferenced() could fault (e.g.
vunmap'ed object). Same with the print_unreferenced() above. It's
probably not worth printing the first bytes of the content anyway when
we do coalescing, the content would differ anyway. Also it's possible
that the size differs even if the stack trace is the same but I guess we
can ignore this.
https://sashiko.dev/#/patchset/20260421-kmemleak_dedup-v1-0-65e31c6cdf0c@debian.org
--
Catalin
next prev parent reply other threads:[~2026-04-23 14:29 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-21 13:45 [PATCH 0/2] mm/kmemleak: dedupe verbose scan output Breno Leitao
2026-04-21 13:45 ` [PATCH 1/2] mm/kmemleak: dedupe verbose scan output by allocation backtrace Breno Leitao
2026-04-23 14:29 ` Catalin Marinas [this message]
2026-04-24 9:26 ` Breno Leitao
2026-04-24 12:05 ` Catalin Marinas
2026-04-24 12:43 ` Breno Leitao
2026-04-21 13:45 ` [PATCH 2/2] selftests/mm: add kmemleak verbose dedup test Breno Leitao
2026-04-24 13:53 ` [PATCH 0/2] mm/kmemleak: dedupe verbose scan output Andrew Morton
2026-04-24 14:36 ` Breno Leitao
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=aeostKcaHzmVBFjb@arm.com \
--to=catalin.marinas@arm.com \
--cc=Liam.Howlett@oracle.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-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=rppt@kernel.org \
--cc=shuah@kernel.org \
--cc=surenb@google.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