Linux kernel -stable discussions
 help / color / mirror / Atom feed
* + mm-kmemleak-take-kmemleak_lock-in-the-disable-time-object-cleanup.patch added to mm-new branch
       [not found] <20261203152515.2270778-2-catalin.marinas@arm.com>
@ 2026-12-03 18:40 ` Andrew Morton
  0 siblings, 0 replies; only message in thread
From: Andrew Morton @ 2026-12-03 18:40 UTC (permalink / raw)
  To: mm-commits, tongweilin, stable, leitao, david, catalin.marinas,
	akpm


The patch titled
     Subject: mm: kmemleak: take kmemleak_lock in the disable-time object cleanup
has been added to the -mm mm-new branch.  Its filename is
     mm-kmemleak-take-kmemleak_lock-in-the-disable-time-object-cleanup.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-kmemleak-take-kmemleak_lock-in-the-disable-time-object-cleanup.patch

This patch will later appear in the mm-new branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Note, mm-new is a provisional staging ground for work-in-progress
patches, and acceptance into mm-new is a notification for others take
notice and to finish up reviews.  Please do not hesitate to respond to
review feedback and post updated versions to replace or incrementally
fixup patches in mm-new.

The mm-new branch of mm.git is not included in linux-next

If a few days of testing in mm-new is successful, the patch will me moved
into mm.git's mm-unstable branch, which is included in linux-next

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Catalin Marinas <catalin.marinas@arm.com>
Subject: mm: kmemleak: take kmemleak_lock in the disable-time object cleanup
Date: Thu, 03 Dec 2026 15:25:12 +0000

Patch series "mm: kmemleak: fix the disable-time cleanup races".

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.


This patch (of 3):

__kmemleak_do_cleanup() removes objects from object_list and the object
tree without kmemleak_lock, assuming nothing else changes them once
kmemleak is disabled. This is not the case:

- a "clear" written to the debugfs file before cleanup_work has cleared
  kmemleak_free_enabled runs the cleanup while frees are still tracked;

- a "clear" written while cleanup_work's own cleanup is still running
  starts a second, concurrent cleanup;

- when kmemleak disables itself, e.g. on an __alloc_object() failure, a
  kmemleak_free() or kmemleak_alloc() that passed its enabled check
  before the flag was cleared can still update the tree and list.

The concurrent updates corrupt the tree and list and lead to a
use-after-free in rb_erase().

Take kmemleak_lock when removing each object in the cleanup.

Link: https://lore.kernel.org/20261203152515.2270778-1-catalin.marinas@arm.com
Link: https://patch.msgid.link/20261203152515.2270778-2-catalin.marinas@arm.com
Link: https://lore.kernel.org/r/149d93f2cfe20d355f4d9882ce2bf99030217772.1793871596.git.tongweilin@linux.alibaba.com [1]
Link: https://sashiko.dev/#/bug/linux-d5496447-37ff-43f3-81ed-e1a5616df463 [2]
Fixes: 2abd839aa7e6 ("kmemleak: Do not corrupt the object_list during clean-up")
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reported-by: Weilin Tong <tongweilin@linux.alibaba.com>
Closes: https://lore.kernel.org/r/149d93f2cfe20d355f4d9882ce2bf99030217772.1793871596.git.tongweilin@linux.alibaba.com
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-d5496447-37ff-43f3-81ed-e1a5616df463
Cc: Breno Leitao <leitao@debian.org>
Cc: David Hildenbrand <david@kernel.org>
Cc: <stable@vger.kernel.org>

 mm/kmemleak.c |   22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

--- a/mm/kmemleak.c~mm-kmemleak-take-kmemleak_lock-in-the-disable-time-object-cleanup
+++ a/mm/kmemleak.c
@@ -620,7 +620,7 @@ static struct kmemleak_object *find_and_
 
 /*
  * Remove an object from its object tree and object_list. Must be called with
- * the kmemleak_lock held _if_ kmemleak is still enabled.
+ * the kmemleak_lock held.
  */
 static void __remove_object(struct kmemleak_object *object)
 {
@@ -2519,15 +2519,25 @@ static const struct file_operations kmem
 
 static void __kmemleak_do_cleanup(void)
 {
-	struct kmemleak_object *object, *tmp;
+	struct kmemleak_object *object;
+	unsigned long flags;
 	unsigned int cnt = 0;
 
 	/*
-	 * Kmemleak has already been disabled, no need for RCU list traversal
-	 * or kmemleak_lock held.
+	 * Callbacks that passed their enabled checks before kmemleak was
+	 * disabled may still be adding or removing objects.
 	 */
-	list_for_each_entry_safe(object, tmp, &object_list, object_list) {
-		__remove_object(object);
+	while (true) {
+		raw_spin_lock_irqsave(&kmemleak_lock, flags);
+		object = list_first_entry_or_null(&object_list,
+						  struct kmemleak_object,
+						  object_list);
+		if (object)
+			__remove_object(object);
+		raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
+
+		if (!object)
+			break;
 		__delete_object(object);
 
 		/* Call cond_resched() once per 64 iterations to avoid soft lockup */
_

Patches currently in -mm which might be from catalin.marinas@arm.com are

mm-kmemleak-take-kmemleak_lock-in-the-disable-time-object-cleanup.patch
mm-kmemleak-do-not-hex-dump-objects-once-free-tracking-has-stopped.patch
mm-kmemleak-annotate-the-lockless-accesses-to-the-enable-flags.patch


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-03 18:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261203152515.2270778-2-catalin.marinas@arm.com>
2026-12-03 18:40 ` + mm-kmemleak-take-kmemleak_lock-in-the-disable-time-object-cleanup.patch added to mm-new branch Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox