mm/kmemleak: dedupe verbose scan output
Subsystems (4)
v3 cover mm/kmemleak: dedupe verbose scan output
In this version, I am not touching the kernel code, but, just fixing the selftest, as sashiko reported some issues, and I am addressing them in here.
https://sashiko.dev/#/patchset/20260424-kmemleak_dedup-v2-0-8bea649b2a92%40debian.org
NOTE: Additional testing has revealed that lockdep detects a potential lock inversion issue on kmemleak and legacy console.
The problem occurs because the function holds &object->lock (a raw spinlock) while invoking printk to report the leak. This printk requires the legacy console_owner lock, which remains in active use despite the ongoing transition to the nbcon framework.
Concurrently, console drivers such as hvc (hypervisor virtual console) acquire the console_owner lock and may subsequently free memory. This kfree() operation calls into kmemleak's __delete_object(), which in turn acquires object->lock.
Although these code paths hold locks from different object instances, lockdep operates on lock classes rather than individual instances. Since all kmemleak_object->lock instances belong to the same lock class, lockdep identifies this as a circular dependency, even though the actual deadlock scenario cannot occur in practice (?!).
This problem was not introduced by this patchset/selftest, but, it is exposing it. I plan to solve it once this patchset is done.
Breno Leitao (2):
mm/kmemleak: dedupe verbose scan output by allocation backtrace selftests/mm: add kmemleak verbose dedup test
mm/kmemleak.c | 102 +++++++++++++++++-
.../selftests/mm/test_kmemleak_dedup.sh | 78 ++++++++++++++ 2 files changed, 175 insertions(+), 5 deletions(-) create mode 100755 tools/testing/selftests/mm/test_kmemleak_dedup.sh
Signature · 39 lines
2.52.0 Signed-off-by: Breno Leitao <[email protected]> --- Changes in v3: - No changes to the first patch (Kernel changes). All changes below are for the selftest. - Pre-cleanup before modprobe — rmmod "$MODULE" 2>/dev/null added before modprobe so a stale load doesn't cause modprobe to be a no-op. - dmesg -C between the two scans — isolates printed count to the second (reporting) scan only. - Link to v2: https://patch.msgid.link/[email protected] Changes in v2: - Drop struct kmemleak_dedup_entry and its kmalloc. (Catalin) - Handle trace_handle == 0 instead of dropping it. - Skip hex dump for coalesced entries (dup_count > 1) — bytes would differ across objects sharing a trace anyway, and it removes the only object->pointer read left in the deferred path. - Counter narrowed from unsigned long count to unsigned int dup_count. - Link to v1: https://patch.msgid.link/[email protected] --- Breno Leitao (2): mm/kmemleak: dedupe verbose scan output by allocation backtrace selftests/mm: add kmemleak verbose dedup test mm/kmemleak.c | 148 ++++++++++++++- tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/ksft_kmemleak_dedup.sh | 222 ++++++++++++++++++++++ 3 files changed, 363 insertions(+), 8 deletions(-) --- base-commit: 4cd074ae20bbcc293bbbce9163abe99d68ae6ae0 change-id: 20260420-kmemleak_dedup-bee54ffa65e7 Best regards, -- Breno Leitao <[email protected]>
PATCH v3 1/2 mm/kmemleak: dedupe verbose scan output by allocation backtrace
In kmemleak's verbose mode, every unreferenced object found during a scan is logged with its full header, hex dump and 16-frame backtrace. Workloads that leak many objects from a single allocation site flood dmesg with byte-for-byte identical backtraces, drowning out distinct leaks and other kernel messages.
Dedupe within each scan using stackdepot's trace_handle as the key: for every leaked object with a recorded stack trace, look up the representative kmemleak_object in a per-scan xarray keyed by trace_handle. The first sighting stores the object pointer (with a get_object() reference) and sets object->dup_count to 1; later sightings just bump dup_count on the representative. After the scan, walk the xarray once and emit each unique backtrace, followed by a single summary line when more than one object shares it.
Leaks whose trace_handle is 0 (early-boot allocations tracked before kmemleak_init() set up object_cache, or stack_depot_save() failures under memory pressure) cannot be deduped, so they are still printed inline via the same locked OBJECT_ALLOCATED-checked helper. The contents of /sys/kernel/debug/kmemleak are unchanged - only the verbose console output is collapsed.
Safety notes:
- The xarray store happens outside object->lock: object->lock is a raw spinlock, while xa_store() may grab xa_node slab locks at a higher wait-context level which lockdep flags as invalid. trace_handle is captured under object->lock (which serialises with kmemleak_update_trace()'s writer), so it is safe to use after dropping the lock.
- get_object() pins the kmemleak_object metadata across rcu_read_unlock(), but the underlying tracked allocation can still be freed concurrently. The deferred print path therefore re-acquires object->lock and re-checks OBJECT_ALLOCATED via print_leak_locked() before touching object->pointer; __delete_object() clears that flag under the same lock before the user memory goes away. The same helper is used by the trace_handle == 0 and xa_store() failure fallbacks, so every printer in the new path has identical safety guarantees.
- If get_object() fails after we set OBJECT_REPORTED, the object is already being torn down (use_count hit zero); the leak count is still accurate but the verbose line is dropped, which is correct
- the memory was freed concurrently and is no longer a leak.
- If xa_store() fails to allocate an xa_node under memory pressure, we fall back to printing inline via print_leak_locked() instead of silently dropping the leak.
- The hex dump is skipped for coalesced entries (dup_count > 1):
bytes would differ across objects sharing a backtrace anyway, and skipping it removes the only remaining read of object->pointer's contents in the deferred path. The representative's reported size may also differ from the coalesced objects' sizes; the printed trace_handle reflects the representative's current value rather than the value used as the dedup key, which is normally - but not strictly - identical.
--- mm/kmemleak.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 140 insertions(+), 8 deletions(-)
mm/kmemleak.c +140 -8
@@ -92,6 +92,7 @@#include <linux/nodemask.h> #include <linux/mm.h> #include <linux/workqueue.h> +#include <linux/xarray.h> #include <linux/crc32.h> #include <asm/sections.h>
@@ -157,6 +158,8 @@ struct kmemleak_object {struct hlist_head area_list;unsigned long jiffies;/* creation timestamp */ pid_t pid;/* pid of the current task */ +/* per-scan dedup count, valid only while in scan-local dedup xarray */ +unsigned int dup_count;char comm[TASK_COMM_LEN ];/* executable name */ };
@@ -360,8 +363,9 @@ static const char *__object_type_str(struct kmemleak_object *object)* Printing of the unreferenced objects information to the seq file. The * print_unreferenced function must be called with the object->lock held. */ -static void print_unreferenced (struct seq_file *seq, -struct kmemleak_object *object) +static void __print_unreferenced (struct seq_file *seq, +struct kmemleak_object *object, +bool hex_dump) {int i;unsigned long *entries;
@@ -373,7 +377,8 @@ static void print_unreferenced(struct seq_file *seq, object->pointer, object->size);warn_or_seq_printf (seq," comm \"%s\", pid %d, jiffies %lu\n" , object->comm, object->pid, object->jiffies); -hex_dump_object (seq, object); +if (hex_dump) +hex_dump_object (seq, object);warn_or_seq_printf (seq," backtrace (crc %x):\n" , object->checksum);for (i =0 ; i < nr_entries; i++) {
@@ -382,6 +387,12 @@ static void print_unreferenced(struct seq_file *seq, } } +static void print_unreferenced (struct seq_file *seq, +struct kmemleak_object *object) +{ +__print_unreferenced (seq, object, true); +} +/* * Print the kmemleak_object information. This function is used mainly for * debugging special cases when kmemleak operations. It must be called with
@@ -1684,6 +1695,103 @@ static void kmemleak_cond_resched(struct kmemleak_object *object)put_object (object); } +/* +* Print one leak inline. The hex dump is gated on OBJECT_ALLOCATED so it +* does not touch user memory that was freed concurrently; the rest of the +* report (backtrace, comm, pid) is always emitted since the kmemleak_object +* metadata is pinned by the caller. +*/ +static void print_leak_locked (struct kmemleak_object *object,bool hex_dump) +{ +raw_spin_lock_irq (&object->lock); +__print_unreferenced (NULL , object, + hex_dump && (object->flags &OBJECT_ALLOCATED )); +raw_spin_unlock_irq (&object->lock); +} + +/* +* Per-scan dedup table for verbose leak printing. The xarray is keyed by +* stackdepot trace_handle and stores a pointer to the representative +* kmemleak_object. The per-scan repeat count lives in object->dup_count. +* +* dedup_record() must run outside object->lock: xa_store() may take +* mutexes (xa_node slab allocation) which lockdep would flag against the +* raw spinlock object->lock. +*/ +static void dedup_record (struct xarray *dedup,struct kmemleak_object *object, +depot_stack_handle_t trace_handle) +{ +struct kmemleak_object *rep; +void *old; + +/* +* No stack trace to dedup against: early-boot allocation tracked +* before kmemleak_init() set up object_cache, or stack_depot_save() +* failure under memory pressure. +*/ +if (!trace_handle) { +print_leak_locked (object, true); +return ; + } + +/* stack is available, now we can de-dup */ + rep =xa_load (dedup, trace_handle); +if (rep) { + rep->dup_count++; +return ; + } + +/* +* Object is being torn down (use_count already hit zero); the +* tracked memory at object->pointer is unsafe to read, so skip. +*/ +if (!get_object (object)) +return ; + + object->dup_count =1 ; + old =xa_store (dedup, trace_handle, object,GFP_ATOMIC ); +if (xa_is_err (old)) { +/* xa_node allocation failed; fall back to inline print. */ +print_leak_locked (object, true); +put_object (object); +return ; + } +/* +* scan_mutex serialises all writers to the dedup xarray, so xa_store() +* after a NULL xa_load() must always overwrite an empty slot. +*/ +WARN_ON_ONCE (old); +} + +/* +* Drain the dedup table. Re-acquires object->lock and re-checks +* OBJECT_ALLOCATED before printing: while get_object() pins the +* kmemleak_object metadata, the underlying tracked allocation may have +* been freed since the scan walked it (kmemleak_free clears +* OBJECT_ALLOCATED under object->lock before the user memory goes away). +* The hex dump is skipped for coalesced entries since the bytes would +* differ across objects anyway. +*/ +static void dedup_flush (struct xarray *dedup) +{ +struct kmemleak_object *object; +unsigned long idx; +unsigned int dup; +bool coalesced; + +xa_for_each (dedup, idx, object) { + dup = object->dup_count; + coalesced = dup >1 ; + +print_leak_locked (object, !coalesced); +if (coalesced) +pr_warn (" ... and %u more object(s) with the same backtrace\n" , + dup -1 ); +put_object (object); +xa_erase (dedup, idx); + } +} +/* * Scan data sections and all the referenced memory blocks allocated via the * kernel's standard allocators. This function must be called with the
@@ -1694,6 +1802,7 @@ static void kmemleak_scan(void)struct kmemleak_object *object;struct zone *zone;int __maybe_unused i; +struct xarray dedup;int new_leaks =0 ; jiffies_last_scan = jiffies;
@@ -1834,10 +1943,18 @@ static void kmemleak_scan(void)return ;/* -* Scanning result reporting. +* Scanning result reporting. When verbose printing is enabled, dedupe +* by stackdepot trace_handle so each unique backtrace is logged once +* per scan, annotated with the number of objects that share it. The +* per-leak count below still reflects every object, and +* /sys/kernel/debug/kmemleak still lists them individually. */ +xa_init (&dedup);rcu_read_lock ();list_for_each_entry_rcu (object, &object_list, object_list) { +depot_stack_handle_t trace_handle; +bool dedup_print; +if (need_resched ())kmemleak_cond_resched (object);
@@ -1849,18 +1966,33 @@ static void kmemleak_scan(void)if (!color_white (object))continue ;raw_spin_lock_irq (&object->lock); + trace_handle =0 ; + dedup_print = false;if (unreferenced_object (object) && !(object->flags &OBJECT_REPORTED )) { object->flags |=OBJECT_REPORTED ; - -if (kmemleak_verbose) -print_unreferenced (NULL , object); - +if (kmemleak_verbose) { + trace_handle = object->trace_handle; + dedup_print = true; + } new_leaks++; }raw_spin_unlock_irq (&object->lock); + +/* +* Defer the verbose print outside object->lock: xa_store() +* may take xa_node slab locks at a higher wait-context level +* which lockdep would flag against the raw_spinlock_t +* object->lock. rcu_read_lock() keeps the kmemleak_object +* alive across the call. +*/ +if (dedup_print) +dedup_record (&dedup, object, trace_handle); }rcu_read_unlock (); +/* Flush'em all */ +dedup_flush (&dedup); +xa_destroy (&dedup);if (new_leaks) { kmemleak_found_leaks = true;
Signature · 1 line
2.52.0
Re: [PATCH v3 1/2] mm/kmemleak: dedupe verbose scan output by allocation backtrace
Quoted 4 lines · Breno Leitao
Dedupe within each scan using stackdepot's
trace_handleas the key: for every leaked object with a recorded stack trace, look up the representativekmemleak_objectin a per-scan xarray keyed bytrace_handle.
Just to circle back to this patch that now it is upstream and deployed to production.
I've got this feature enabled in Meta's kernel "debug" tier (which runs with kmemleak enabled), and this is being very helpful. For instance, this is the kmemleak reports (instead of 330 similar reports).
kmemleak: unreferenced object 0xffff0000c3b5ca00 (size 96):
kmemleak: comm "swapper/0", pid 1, jiffies 4294748185
kmemleak: backtrace (crc 22d8f576):
kmemleak: kmemleak_alloc+0x3c/0xc8
kmemleak: __kmalloc_node_noprof+0x42c/0x7c0
kmemleak: __alloc+0x160/0x3a8
kmemleak: alloc_bulk+0x3d8/0x7b0
kmemleak: bpf_mem_alloc_init+0x3f8/0xd48
kmemleak: cpumask_kfunc_init+0x88/0x158
kmemleak: __initstub__kmod_cpumask__654_534_cpumask_kfunc_init7+0x14/0x28
kmemleak: do_one_initcall+0xe0/0x9f8
kmemleak: do_initcall_level+0x12c/0x1d0
kmemleak: do_initcalls+0x60/0x108
kmemleak: do_basic_setup+0x80/0x98
kmemleak: kernel_init_freeable+0x280/0x400
kmemleak: kernel_init+0x24/0x170
kmemleak: ret_from_fork+0x10/0x20
kmemleak: ... and 1 more object(s) with the same backtrace
kmemleak: unreferenced object 0xffff0000c3b57080 (size 96):
kmemleak: comm "hardirq", pid 0, jiffies 4298468258
kmemleak: backtrace (crc 85619951):
kmemleak: kmemleak_alloc+0x3c/0xc8
kmemleak: __kmalloc_node_noprof+0x42c/0x7c0
kmemleak: __alloc+0x160/0x3a8
kmemleak: alloc_bulk+0x3d8/0x7b0
kmemleak: bpf_mem_refill+0xb4/0x8a0
kmemleak: irq_work_single+0x9c/0x178
kmemleak: irq_work_run+0x12c/0x208
kmemleak: ipi_handler+0x1cc/0x900
kmemleak: handle_percpu_devid_irq+0x3bc/0x4f8
kmemleak: generic_handle_domain_irq+0xa0/0x100
kmemleak: gic_handle_irq+0x364/0x688
kmemleak: ... and 331 more object(s) with the same backtraceI've haven't investigate the leak itself, and that is not the reason of this email, it is just to share that this is being quite useful at real production environments.
PATCH v3 2/2 selftests/mm: add kmemleak verbose dedup test
In reply to Breno LeitaoAdd a regression test for the per-scan verbose dedup added in the preceding commit. The test loads samples/kmemleak's helper module (CONFIG_SAMPLE_KMEMLEAK=m) to generate orphan allocations, several of which share an allocation backtrace, runs four kmemleak scans with verbose printing enabled, then walks dmesg looking for two "unreferenced object" reports within a single scan that share an identical backtrace - which would mean dedup failed to collapse them.
The test is intentionally permissive on detection but strict on regressions:
- PASS when no duplicates are observed, regardless of whether the dedup summary line ("... and N more object(s) with the same backtrace") was actually emitted. Per-CPU chunk reuse, slab freelist pointers, kernel stack residue and CONFIG_DEBUG_KMEMLEAK_ AUTO_SCAN can all keep most of the orphans "still referenced" or reported across many separate scans, so the dedup path may have nothing to fold within one scan. That is not a regression.
- PASS reports whether dedup actually fired, so a passing run on a well-behaved environment is still informative.
- FAIL when two same-backtrace reports land in a single scan (clear dedup regression).
- FAIL when kmemleak's own per-scan tally counts leaks but the verbose path emits zero "unreferenced object" lines - that catches a regression in the verbose printer itself, which would otherwise pass the duplicate check trivially.
- SKIP when kmemleak is absent, disabled at runtime, or the helper module is not built.
The dmesg parser anchors stack-frame matching to the indentation kmemleak uses for them (4+ spaces under "kmemleak: ") so unrelated kmemleak warnings landing between reports do not get lumped into the backtrace key and mask a duplicate.
--- tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/ksft_kmemleak_dedup.sh | 222 ++++++++++++++++++++++ 2 files changed, 223 insertions(+)
tools/testing/selftests/mm/Makefile +1 -0
@@ -151,6 +151,7 @@ TEST_PROGS += ksft_gup_test.sh TEST_PROGS += ksft_hmm.sh TEST_PROGS += ksft_hugetlb.sh TEST_PROGS += ksft_hugevm.sh +TEST_PROGS += ksft_kmemleak_dedup.sh TEST_PROGS += ksft_ksm.sh TEST_PROGS += ksft_ksm_numa.sh TEST_PROGS += ksft_madv_guard.sh
tools/testing/selftests/mm/ksft_kmemleak_dedup.sh new file +222 -0
222 lines not on this page · raw message
Signature · 1 line
2.52.0
Quoted 18 lines · On Wed, 06 May 2026 05:58:23 -0700 Breno Leitao <[email protected]> wrote:
In this version, I am not touching the kernel code, but, just fixing the selftest, as sashiko reported some issues, and I am addressing them in here.
https://sashiko.dev/#/patchset/20260424-kmemleak_dedup-v2-0-8bea649b2a92%40debian.org
NOTE: Additional testing has revealed that lockdep detects a potential lock inversion issue on kmemleak and legacy console.
The problem occurs because the function holds
&object->lock(a raw spinlock) while invoking printk to report the leak. This printk requires the legacyconsole_ownerlock, which remains in active use despite the ongoing transition to the nbcon framework.Concurrently, console drivers such as hvc (hypervisor virtual console) acquire the
console_ownerlock and may subsequently free memory. Thiskfree()operation calls into kmemleak's__delete_object(), which in turn acquiresobject->lock.
Quoted 8 lines
Although these code paths hold locks from different object instances, lockdep operates on lock classes rather than individual instances. Since all
kmemleak_object->lockinstances belong to the same lock class, lockdep identifies this as a circular dependency, even though the actual deadlock scenario cannot occur in practice (?!).This problem was not introduced by this patchset/selftest, but, it is exposing it. I plan to solve it once this patchset is done.
None of the above is usable for a [0/N] - it all pertains to the ongoing development process and should formally be below the ^---$ separator. Because it isn't relevant to the permanent kernel record.
The v2 series had a nice cover letter, so I stole that. Please check that the below remains the truth, the whole truth, etc.
From: Breno Leitao <[email protected]>
Subject: mm/kmemleak: dedupe verbose scan output by allocation backtrace
Date: Wed, 06 May 2026 05:58:24 -0700
Patch series "mm/kmemleak: dedupe verbose scan output", v3.
I am starting to run with kmemleak in verbose enabled in some "probe points" across the my employers fleet so that suspected leaks land in dmesg without needing a separate read of /sys/kernel/debug/kmemleak.
The downside is that workloads which leak many objects from a single allocation site flood the console with byte-for-byte identical backtraces. Hundreds of duplicates per scan are common, drowning out distinct leaks and unrelated kernel messages, while adding no signal beyond the first occurrence.
This series collapses those duplicates inside kmemleak itself. Each unique stackdepot trace_handle prints once per scan, followed by a short summary line when more than one object shares it:
kmemleak: unreferenced object 0xff110001083beb00 (size 192):
kmemleak: comm "modprobe", pid 974, jiffies 4294754196 kmemleak: ... kmemleak: backtrace (crc 6f361828):
kmemleak: __kmalloc_cache_noprof+0x1af/0x650
kmemleak: ...
kmemleak: ... and 71 more object(s) with the same backtrace
The "N new suspected memory leaks" tally and the contents of /sys/kernel/debug/kmemleak are unchanged - the per-object detail is still available on demand, only the verbose (dmesg) output is collapsed.
Patch 1 is the kmemleak change.
Patch 2 adds a selftest that loads samples/kmemleak's CONFIG_SAMPLE kmemleak-test module to generate ten leaks sharing one call site and checks that the printed count is strictly less than the reported leak total. Not sure if Patch 2 is useful or not, if not, it is easier to discard.
This patch (of 2):
In kmemleak's verbose mode, every unreferenced object found during a scan is logged with its full header, hex dump and 16-frame backtrace. Workloads that leak many objects from a single allocation site flood dmesg with byte-for-byte identical backtraces, drowning out distinct leaks and other kernel messages.
Dedupe within each scan using stackdepot's trace_handle as the key: for every leaked object with a recorded stack trace, look up the representative kmemleak_object in a per-scan xarray keyed by trace_handle. The first sighting stores the object pointer (with a get_object() reference) and sets object->dup_count to 1; later sightings just bump dup_count on the representative. After the scan, walk the xarray once and emit each unique backtrace, followed by a single summary line when more than one object shares it.
Leaks whose trace_handle is 0 (early-boot allocations tracked before kmemleak_init() set up object_cache, or stack_depot_save() failures under memory pressure) cannot be deduped, so they are still printed inline via the same locked OBJECT_ALLOCATED-checked helper. The contents of /sys/kernel/debug/kmemleak are unchanged - only the verbose console output is collapsed.
Safety notes:
- The xarray store happens outside object->lock: object->lock is a raw spinlock, while xa_store() may grab xa_node slab locks at a higher wait-context level which lockdep flags as invalid. trace_handle is captured under object->lock (which serialises with kmemleak_update_trace()'s writer), so it is safe to use after dropping the lock.
- get_object() pins the kmemleak_object metadata across rcu_read_unlock(), but the underlying tracked allocation can still be freed concurrently. The deferred print path therefore re-acquires object->lock and re-checks OBJECT_ALLOCATED via print_leak_locked() before touching object->pointer; __delete_object() clears that flag under the same lock before the user memory goes away. The same helper is used by the trace_handle == 0 and xa_store() failure fallbacks, so every printer in the new path has identical safety guarantees.
- If get_object() fails after we set OBJECT_REPORTED, the object is already being torn down (use_count hit zero); the leak count is still accurate but the verbose line is dropped, which is correct
- the memory was freed concurrently and is no longer a leak.
- If xa_store() fails to allocate an xa_node under memory pressure, we fall back to printing inline via print_leak_locked() instead of silently dropping the leak.
- The hex dump is skipped for coalesced entries (dup_count > 1):
bytes would differ across objects sharing a backtrace anyway, and skipping it removes the only remaining read of object->pointer's contents in the deferred path. The representative's reported size may also differ from the coalesced objects' sizes; the printed trace_handle reflects the representative's current value rather than the value used as the dedup key, which is normally - but not strictly - identical.
---
mm/kmemleak.c | 148 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 140 insertions(+), 8 deletions(-)
mm/kmemleak.c renamed from mm/kmemleak.c~mm-kmemleak-dedupe-verbose-scan-output-by-allocation-backtrace +140 -8
@@ -92,6 +92,7 @@#include <linux/nodemask.h> #include <linux/mm.h> #include <linux/workqueue.h> +#include <linux/xarray.h> #include <linux/crc32.h> #include <asm/sections.h>
@@ -157,6 +158,8 @@ struct kmemleak_object {struct hlist_head area_list;unsigned long jiffies;/* creation timestamp */ pid_t pid;/* pid of the current task */ +/* per-scan dedup count, valid only while in scan-local dedup xarray */ +unsigned int dup_count;char comm[TASK_COMM_LEN ];/* executable name */ };
@@ -360,8 +363,9 @@ static const char *__object_type_str(str* Printing of the unreferenced objects information to the seq file. The * print_unreferenced function must be called with the object->lock held. */ -static void print_unreferenced (struct seq_file *seq, -struct kmemleak_object *object) +static void __print_unreferenced (struct seq_file *seq, +struct kmemleak_object *object, +bool hex_dump) {int i;unsigned long *entries;
@@ -373,7 +377,8 @@ static void print_unreferenced(struct se object->pointer, object->size);warn_or_seq_printf (seq," comm \"%s\", pid %d, jiffies %lu\n" , object->comm, object->pid, object->jiffies); -hex_dump_object (seq, object); +if (hex_dump) +hex_dump_object (seq, object);warn_or_seq_printf (seq," backtrace (crc %x):\n" , object->checksum);for (i =0 ; i < nr_entries; i++) {
@@ -382,6 +387,12 @@ static void print_unreferenced(struct se } } +static void print_unreferenced (struct seq_file *seq, +struct kmemleak_object *object) +{ +__print_unreferenced (seq, object, true); +} +/* * Print the kmemleak_object information. This function is used mainly for * debugging special cases when kmemleak operations. It must be called with
@@ -1685,6 +1696,103 @@ unlock_put: }/* +* Print one leak inline. The hex dump is gated on OBJECT_ALLOCATED so it +* does not touch user memory that was freed concurrently; the rest of the +* report (backtrace, comm, pid) is always emitted since the kmemleak_object +* metadata is pinned by the caller. +*/ +static void print_leak_locked (struct kmemleak_object *object,bool hex_dump) +{ +raw_spin_lock_irq (&object->lock); +__print_unreferenced (NULL , object, + hex_dump && (object->flags &OBJECT_ALLOCATED )); +raw_spin_unlock_irq (&object->lock); +} + +/* +* Per-scan dedup table for verbose leak printing. The xarray is keyed by +* stackdepot trace_handle and stores a pointer to the representative +* kmemleak_object. The per-scan repeat count lives in object->dup_count. +* +* dedup_record() must run outside object->lock: xa_store() may take +* mutexes (xa_node slab allocation) which lockdep would flag against the +* raw spinlock object->lock. +*/ +static void dedup_record (struct xarray *dedup,struct kmemleak_object *object, +depot_stack_handle_t trace_handle) +{ +struct kmemleak_object *rep; +void *old; + +/* +* No stack trace to dedup against: early-boot allocation tracked +* before kmemleak_init() set up object_cache, or stack_depot_save() +* failure under memory pressure. +*/ +if (!trace_handle) { +print_leak_locked (object, true); +return ; + } + +/* stack is available, now we can de-dup */ + rep =xa_load (dedup, trace_handle); +if (rep) { + rep->dup_count++; +return ; + } + +/* +* Object is being torn down (use_count already hit zero); the +* tracked memory at object->pointer is unsafe to read, so skip. +*/ +if (!get_object (object)) +return ; + + object->dup_count =1 ; + old =xa_store (dedup, trace_handle, object,GFP_ATOMIC ); +if (xa_is_err (old)) { +/* xa_node allocation failed; fall back to inline print. */ +print_leak_locked (object, true); +put_object (object); +return ; + } +/* +* scan_mutex serialises all writers to the dedup xarray, so xa_store() +* after a NULL xa_load() must always overwrite an empty slot. +*/ +WARN_ON_ONCE (old); +} + +/* +* Drain the dedup table. Re-acquires object->lock and re-checks +* OBJECT_ALLOCATED before printing: while get_object() pins the +* kmemleak_object metadata, the underlying tracked allocation may have +* been freed since the scan walked it (kmemleak_free clears +* OBJECT_ALLOCATED under object->lock before the user memory goes away). +* The hex dump is skipped for coalesced entries since the bytes would +* differ across objects anyway. +*/ +static void dedup_flush (struct xarray *dedup) +{ +struct kmemleak_object *object; +unsigned long idx; +unsigned int dup; +bool coalesced; + +xa_for_each (dedup, idx, object) { + dup = object->dup_count; + coalesced = dup >1 ; + +print_leak_locked (object, !coalesced); +if (coalesced) +pr_warn (" ... and %u more object(s) with the same backtrace\n" , + dup -1 ); +put_object (object); +xa_erase (dedup, idx); + } +} + +/* * Scan data sections and all the referenced memory blocks allocated via the * kernel's standard allocators. This function must be called with the * scan_mutex held.
@@ -1694,6 +1802,7 @@ static void kmemleak_scan(void)struct kmemleak_object *object;struct zone *zone;int __maybe_unused i; +struct xarray dedup;int new_leaks =0 ; jiffies_last_scan = jiffies;
@@ -1834,10 +1943,18 @@ static void kmemleak_scan(void)return ;/* -* Scanning result reporting. +* Scanning result reporting. When verbose printing is enabled, dedupe +* by stackdepot trace_handle so each unique backtrace is logged once +* per scan, annotated with the number of objects that share it. The +* per-leak count below still reflects every object, and +* /sys/kernel/debug/kmemleak still lists them individually. */ +xa_init (&dedup);rcu_read_lock ();list_for_each_entry_rcu (object, &object_list, object_list) { +depot_stack_handle_t trace_handle; +bool dedup_print; +if (need_resched ())kmemleak_cond_resched (object);
@@ -1849,18 +1966,33 @@ static void kmemleak_scan(void)if (!color_white (object))continue ;raw_spin_lock_irq (&object->lock); + trace_handle =0 ; + dedup_print = false;if (unreferenced_object (object) && !(object->flags &OBJECT_REPORTED )) { object->flags |=OBJECT_REPORTED ; - -if (kmemleak_verbose) -print_unreferenced (NULL , object); - +if (kmemleak_verbose) { + trace_handle = object->trace_handle; + dedup_print = true; + } new_leaks++; }raw_spin_unlock_irq (&object->lock); + +/* +* Defer the verbose print outside object->lock: xa_store() +* may take xa_node slab locks at a higher wait-context level +* which lockdep would flag against the raw_spinlock_t +* object->lock. rcu_read_lock() keeps the kmemleak_object +* alive across the call. +*/ +if (dedup_print) +dedup_record (&dedup, object, trace_handle); }rcu_read_unlock (); +/* Flush'em all */ +dedup_flush (&dedup); +xa_destroy (&dedup);if (new_leaks) { kmemleak_found_leaks = true;
_
Quoted 29 lines · Andrew Morton
Quoted 18 lines · On Wed, 06 May 2026 05:58:23 -0700 Breno Leitao <[email protected]> wrote:
In this version, I am not touching the kernel code, but, just fixing the selftest, as sashiko reported some issues, and I am addressing them in here.
https://sashiko.dev/#/patchset/20260424-kmemleak_dedup-v2-0-8bea649b2a92%40debian.org
NOTE: Additional testing has revealed that lockdep detects a potential lock inversion issue on kmemleak and legacy console.
The problem occurs because the function holds
&object->lock(a raw spinlock) while invoking printk to report the leak. This printk requires the legacyconsole_ownerlock, which remains in active use despite the ongoing transition to the nbcon framework.Concurrently, console drivers such as hvc (hypervisor virtual console) acquire the
console_ownerlock and may subsequently free memory. Thiskfree()operation calls into kmemleak's__delete_object(), which in turn acquiresobject->lock.Quoted 8 lines
Although these code paths hold locks from different object instances, lockdep operates on lock classes rather than individual instances. Since all
kmemleak_object->lockinstances belong to the same lock class, lockdep identifies this as a circular dependency, even though the actual deadlock scenario cannot occur in practice (?!).This problem was not introduced by this patchset/selftest, but, it is exposing it. I plan to solve it once this patchset is done.
Quoted 6 lines
None of the above is usable for a [0/N] - it all pertains to the ongoing development process and should formally be below the ^---$ separator. Because it isn't relevant to the permanent kernel record.
The v2 series had a nice cover letter, so I stole that. Please check that the below remains the truth, the whole truth, etc.
Ack, That remains correct.
I just updated the cover letter to reflect the changes, but in the future I will keep the overall goal of the patchset.