From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796323256; b=m1vvN3kzUNeanG+OQwDxp6relW2WOFLil7I10MSUgNwMyjjGsK0NYwjaACZrKxGrMsGw 6MY3748k1Vk3GJGWjAhqCDmYrJBEP43EE4ZPgALgI0GY2aso+DCbaZTEqlKwkFnWlRA+c qp2mpbW243SmWa/uqCVkcRKUEIPeUFlNrQxGrkrin74lHZEPJthkXeSalP0PqhAupZv44 H0AJwBN/kOemU/fMdWWnBXQ8Zq+IgpWQCVnPdV++hTouWDX2LkCzoW2AMZbGiHEy+LcG5 5UwgKZLrQMYSPgwet836dxE3pd+7H8WsvLMQf/BrWUMAOnIXeJDi5aghoCnQ57/Fbzg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796323256; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=YK1lbOGsZCYJ/6ukuE98iFqyg4q4uTgjzZKXPNFZVXU=; b=f5dR6+DNvKCUrWUxoX0wG7ygTU7sOmSDcb8CSLqHP4lOqEiUmLZ07hGf4p4l40tTwlfN flWkB0HkO40ytfl0lo4rAkqnLIUDLEA9kZUBUvESLwiwTx5W/4gSQz3jh/OFVnLANTlim VqR0cWASMmsi/zTDdYQ7dHsy4jGahVzu45WtkPlMMaZ1jE/39UwqfaGUXpx7HJiaX8T6O E3iIurIjholgk2YJo0h0kfLEUftnLDLjzozyAHBChBGJHwuJZtqw2vAf4ixDejgL7YGtj noZmHipbNYSRN3jkUAcE8jaj5PFXYdIAA5Mf9iB8wsR+b67Tzw5jfkGdOgSVV/fuDLg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=YdHGom4E; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 04D64606A322; Thu, 3 Dec 2026 18:40:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=YdHGom4E Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7D5581F000C1; Thu, 3 Dec 2026 18:40:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796323255; bh=YK1lbOGsZCYJ/6ukuE98iFqyg4q4uTgjzZKXPNFZVXU=; h=Date:To:From:In-Reply-To:References:Subject; b=YdHGom4EWl6nPNEq5fPxvezqYrrhMoEWNe55I5XGKhBV0clzJ24hUxE0lauYsjWFm YuXNUE14GzYH5UX5X5sZQrU7DaZOU/TvPcr3UDcme+faSQDsqIkmt+vOmq169dJNgF Bu8dmGwVZIr3DlhJeTwamL/PUC390p7IyQz+KKQ0= Date: Thu, 03 Dec 2026 10:40:55 -0800 To: mm-commits@vger.kernel.org,tongweilin@linux.alibaba.com, stable@vger.kernel.org,leitao@debian.org,david@kernel.org, catalin.marinas@arm.com,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261203152515.2270778-2-catalin.marinas@arm.com> References: <20261203152515.2270778-2-catalin.marinas@arm.com> Subject: + mm-kmemleak-take-kmemleak_lock-in-the-disable-time-object-cleanup.patch added to mm-new branch Message-Id: <20261203184055.7D5581F000C1@smtp.kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 Signed-off-by: Andrew Morton Reported-by: Weilin Tong Closes: https://lore.kernel.org/r/149d93f2cfe20d355f4d9882ce2bf99030217772.1793871596.git.tongweilin@linux.alibaba.com Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-d5496447-37ff-43f3-81ed-e1a5616df463 Cc: Breno Leitao Cc: David Hildenbrand Cc: 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