[PATCH 0/3] mm: kmemleak: fix the disable-time cleanup races

From: Catalin Marinas

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


Hi Andrew,

Patch 1 here supersedes Weilin's "mm: kmemleak: serialize disable-time
cleanup" in mm-unstable, so I think we can drop that one. I reviewed it
but Sashiko found more problems in the area, so here's a more
comprehensive fix. If you'd rather keep it (it's harmless), I can rebase
on top. This series is based on v7.4-rc1.

When kmemleak is disabled at runtime, __kmemleak_do_cleanup() removes
the objects without kmemleak_lock. A "clear" written to debugfs can run
a second cleanup concurrently with cleanup_work's (reported by Weilin
[1]) and callbacks such as kmemleak_free() that passed their enabled
checks before the disable can still update the object tree and list
(reported by Sashiko [2]). Both corrupt the tree and list.

Patch 1 takes kmemleak_lock when removing each object in the cleanup,
covering both cases. Patch 2 skips the hex dump in the report once frees
are no longer tracked since the reported objects may have been freed.
Patch 3 annotates the lockless accesses to the enable flags for
completeness. I couldn't get KCSAN to trigger a warning.

Tested on arm64 under Qemu with KASAN. On v7.4-rc1, writing "off" and
then "clear" crashes reliably and patch 1 fixes it. The concurrent
callback race needed some delays inserted to trigger a KASAN
use-after-free in rb_erase() (also fixed by patch 1).

LLM help (Claude): race analysis, testing, code and commit log
checks/suggestions, identifying the hex dump problem fixed by patch 2.

[1] https://lore.kernel.org/r/149d93f2cfe20d355f4d9882ce2bf99030217772.1793871596.git.tongweilin@xxxxxxxxxxxxxxxxx
[2] https://sashiko.dev/#/bug/linux-d5496447-37ff-43f3-81ed-e1a5616df463

Catalin Marinas (3):
mm: kmemleak: take kmemleak_lock in the disable-time object cleanup
mm: kmemleak: do not hex dump objects once free tracking has stopped
mm: kmemleak: annotate the lockless accesses to the enable flags

mm/kmemleak.c | 73 +++++++++++++++++++++++++++++++--------------------
1 file changed, 45 insertions(+), 28 deletions(-)