[PATCH v3 0/2] mm/kmemleak: dedupe verbose scan output
From: Breno Leitao
Date: Wed May 06 2026 - 08:59:38 EST
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
--
2.52.0
Signed-off-by: Breno Leitao <leitao@xxxxxxxxxx>
---
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/20260424-kmemleak_dedup-v2-0-8bea649b2a92@xxxxxxxxxx
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/20260421-kmemleak_dedup-v1-0-65e31c6cdf0c@xxxxxxxxxx
---
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 <leitao@xxxxxxxxxx>