[PATCH 2/3] mm: kmemleak: do not hex dump objects once free tracking has stopped

From: Catalin Marinas

Date: Thu Dec 03 2026 - 10:25:30 EST


If kmemleak is disabled after reporting leaks, the metadata is kept so
that the report can still be read, but kmemleak_free_enabled is cleared.
kmemleak_free() then no longer clears OBJECT_ALLOCATED, which the report
checks under object->lock before hex dumping an object. Reading the
report can therefore hex dump memory that has been freed, or unmapped in
the case of vmalloc and percpu areas.

Skip the hex dump once kmemleak_free_enabled is cleared. The callers
hold scan_mutex, under which kmemleak_do_cleanup() clears the flag.

Fixes: 74341703edca ("kmemleak: Report previously found leaks even after an error")
Cc: <stable@xxxxxxxxxxxxxxx>
Signed-off-by: Catalin Marinas <catalin.marinas@xxxxxxx>
---
mm/kmemleak.c | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 9c2d01fd4113..0fd3ea174367 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -311,6 +311,13 @@ static void hex_dump_object(struct seq_file *seq,
if (WARN_ON_ONCE(object->flags & OBJECT_PHYS))
return;

+ /*
+ * Frees are no longer tracked, so the object may have been freed or
+ * unmapped. The callers hold scan_mutex, under which the flag is cleared.
+ */
+ if (!kmemleak_free_enabled)
+ return;
+
if (object->flags & OBJECT_PERCPU)
ptr = (const u8 *)this_cpu_ptr((void __percpu *)object->pointer);