All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: mm-commits@vger.kernel.org,tongweilin@linux.alibaba.com,leitao@debian.org,
	david@kernel.org,catalin.marinas@arm.com,akpm@linux-foundation.org
Subject: + mm-kmemleak-annotate-the-lockless-accesses-to-the-enable-flags.patch added to mm-new branch
Date: Thu, 03 Dec 2026 10:40:59 -0800	[thread overview]
Message-ID: <20261203184100.51B1E1F000C1@smtp.kernel.org> (raw)
In-Reply-To: <20261203152515.2270778-4-catalin.marinas@arm.com>


The patch titled
     Subject: mm: kmemleak: annotate the lockless accesses to the enable flags
has been added to the -mm mm-new branch.  Its filename is
     mm-kmemleak-annotate-the-lockless-accesses-to-the-enable-flags.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-kmemleak-annotate-the-lockless-accesses-to-the-enable-flags.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: annotate the lockless accesses to the enable flags
Date: Thu, 03 Dec 2026 15:25:14 +0000

kmemleak_enabled and kmemleak_free_enabled are read without a lock in
the kmemleak callbacks while kmemleak_disable() and
kmemleak_do_cleanup() clear them. Use READ_ONCE() and WRITE_ONCE() for
these accesses. No functional change.

Link: https://patch.msgid.link/20261203152515.2270778-4-catalin.marinas@arm.com
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Weilin Tong <tongweilin@linux.alibaba.com>
Cc: Breno Leitao <leitao@debian.org>
Cc: David Hildenbrand <david@kernel.org>

 mm/kmemleak.c |   46 +++++++++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 23 deletions(-)

--- a/mm/kmemleak.c~mm-kmemleak-annotate-the-lockless-accesses-to-the-enable-flags
+++ a/mm/kmemleak.c
@@ -316,7 +316,7 @@ static void hex_dump_object(struct seq_f
 	 * 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)
+	if (!READ_ONCE(kmemleak_free_enabled))
 		return;
 
 	if (object->flags & OBJECT_PERCPU)
@@ -1122,7 +1122,7 @@ void __ref kmemleak_alloc(const void *pt
 {
 	pr_debug("%s(0x%px, %zu, %d)\n", __func__, ptr, size, min_count);
 
-	if (kmemleak_enabled && ptr && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR(ptr))
 		create_object((unsigned long)ptr, size, min_count, gfp);
 }
 EXPORT_SYMBOL_GPL(kmemleak_alloc);
@@ -1141,7 +1141,7 @@ void __ref kmemleak_alloc_percpu(const v
 {
 	pr_debug("%s(0x%px, %zu)\n", __func__, ptr, size);
 
-	if (kmemleak_enabled && ptr && !IS_ERR_PCPU(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR_PCPU(ptr))
 		create_object_percpu((__force unsigned long)ptr, size, 1, gfp);
 }
 EXPORT_SYMBOL_GPL(kmemleak_alloc_percpu);
@@ -1163,7 +1163,7 @@ void __ref kmemleak_vmalloc(const struct
 	 * A min_count = 2 is needed because vm_struct contains a reference to
 	 * the virtual address of the vmalloc'ed block.
 	 */
-	if (kmemleak_enabled) {
+	if (READ_ONCE(kmemleak_enabled)) {
 		create_object((unsigned long)area->addr, size, 2, gfp);
 		object_set_excess_ref((unsigned long)area,
 				      (unsigned long)area->addr);
@@ -1182,7 +1182,7 @@ void __ref kmemleak_free(const void *ptr
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_free_enabled && ptr && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_free_enabled) && ptr && !IS_ERR(ptr))
 		delete_object_full((unsigned long)ptr, 0);
 }
 EXPORT_SYMBOL_GPL(kmemleak_free);
@@ -1200,7 +1200,7 @@ void __ref kmemleak_free_part(const void
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_enabled && ptr && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR(ptr))
 		delete_object_part((unsigned long)ptr, size, 0);
 }
 EXPORT_SYMBOL_GPL(kmemleak_free_part);
@@ -1216,7 +1216,7 @@ void __ref kmemleak_free_percpu(const vo
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_free_enabled && ptr && !IS_ERR_PCPU(ptr))
+	if (READ_ONCE(kmemleak_free_enabled) && ptr && !IS_ERR_PCPU(ptr))
 		delete_object_full((__force unsigned long)ptr, OBJECT_PERCPU);
 }
 EXPORT_SYMBOL_GPL(kmemleak_free_percpu);
@@ -1236,7 +1236,7 @@ void __ref kmemleak_update_trace(const v
 
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (!kmemleak_enabled || IS_ERR_OR_NULL(ptr))
+	if (!READ_ONCE(kmemleak_enabled) || IS_ERR_OR_NULL(ptr))
 		return;
 
 	object = find_and_get_object((unsigned long)ptr, 1);
@@ -1268,7 +1268,7 @@ void __ref kmemleak_not_leak(const void
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_enabled && ptr && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR(ptr))
 		make_gray_object((unsigned long)ptr);
 }
 EXPORT_SYMBOL(kmemleak_not_leak);
@@ -1285,7 +1285,7 @@ void __ref kmemleak_transient_leak(const
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_enabled && ptr && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR(ptr))
 		reset_checksum((unsigned long)ptr);
 }
 EXPORT_SYMBOL(kmemleak_transient_leak);
@@ -1299,7 +1299,7 @@ void __ref kmemleak_ignore_percpu(const
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_enabled && ptr && !IS_ERR_PCPU(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR_PCPU(ptr))
 		make_black_object((unsigned long)ptr, OBJECT_PERCPU);
 }
 EXPORT_SYMBOL_GPL(kmemleak_ignore_percpu);
@@ -1317,7 +1317,7 @@ void __ref kmemleak_ignore(const void *p
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_enabled && ptr && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR(ptr))
 		make_black_object((unsigned long)ptr, 0);
 }
 EXPORT_SYMBOL(kmemleak_ignore);
@@ -1337,7 +1337,7 @@ void __ref kmemleak_scan_area(const void
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_enabled && ptr && size && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && size && !IS_ERR(ptr))
 		add_scan_area((unsigned long)ptr, size, gfp);
 }
 EXPORT_SYMBOL(kmemleak_scan_area);
@@ -1355,7 +1355,7 @@ void __ref kmemleak_no_scan(const void *
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_enabled && ptr && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR(ptr))
 		object_no_scan((unsigned long)ptr);
 }
 EXPORT_SYMBOL(kmemleak_no_scan);
@@ -1371,7 +1371,7 @@ void __ref kmemleak_alloc_phys(phys_addr
 {
 	pr_debug("%s(0x%px, %zu)\n", __func__, &phys, size);
 
-	if (kmemleak_enabled)
+	if (READ_ONCE(kmemleak_enabled))
 		/*
 		 * Create object with OBJECT_PHYS flag and
 		 * assume min_count 0.
@@ -1391,7 +1391,7 @@ void __ref kmemleak_free_part_phys(phys_
 {
 	pr_debug("%s(0x%px)\n", __func__, &phys);
 
-	if (kmemleak_enabled)
+	if (READ_ONCE(kmemleak_enabled))
 		delete_object_part((unsigned long)phys, size, OBJECT_PHYS);
 }
 EXPORT_SYMBOL(kmemleak_free_part_phys);
@@ -1405,7 +1405,7 @@ void __ref kmemleak_ignore_phys(phys_add
 {
 	pr_debug("%s(0x%px)\n", __func__, &phys);
 
-	if (kmemleak_enabled)
+	if (READ_ONCE(kmemleak_enabled))
 		make_black_object((unsigned long)phys, OBJECT_PHYS);
 }
 EXPORT_SYMBOL(kmemleak_ignore_phys);
@@ -1534,7 +1534,7 @@ static void pointer_update_refs(struct k
  */
 static int scan_should_stop(void)
 {
-	if (!kmemleak_enabled)
+	if (!READ_ONCE(kmemleak_enabled))
 		return 1;
 
 	/*
@@ -2459,14 +2459,14 @@ static ssize_t kmemleak_write(struct fil
 		return ret;
 
 	if (strncmp(buf, "clear", 5) == 0) {
-		if (kmemleak_enabled)
+		if (READ_ONCE(kmemleak_enabled))
 			kmemleak_clear();
 		else
 			__kmemleak_do_cleanup();
 		goto out;
 	}
 
-	if (!kmemleak_enabled) {
+	if (!READ_ONCE(kmemleak_enabled)) {
 		ret = -EPERM;
 		goto out;
 	}
@@ -2569,7 +2569,7 @@ static void kmemleak_do_cleanup(struct w
 	 * the memory accesses below is guaranteed by the kthread_stop()
 	 * function.
 	 */
-	kmemleak_free_enabled = 0;
+	WRITE_ONCE(kmemleak_free_enabled, 0);
 	mutex_unlock(&scan_mutex);
 
 	if (!kmemleak_found_leaks)
@@ -2591,13 +2591,13 @@ static void kmemleak_disable(void)
 		return;
 
 	/* stop any memory operation tracing */
-	kmemleak_enabled = 0;
+	WRITE_ONCE(kmemleak_enabled, 0);
 
 	/* check whether it is too early for a kernel thread */
 	if (kmemleak_late_initialized)
 		schedule_work(&cleanup_work);
 	else
-		kmemleak_free_enabled = 0;
+		WRITE_ONCE(kmemleak_free_enabled, 0);
 
 	pr_info("Kernel memory leak detector disabled\n");
 }
_

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


      reply	other threads:[~2026-12-03 18:41 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 15:25 [PATCH 0/3] mm: kmemleak: fix the disable-time cleanup races Catalin Marinas
2026-12-03 15:25 ` [PATCH 1/3] mm: kmemleak: take kmemleak_lock in the disable-time object cleanup Catalin Marinas
2026-12-03 18:40   ` + mm-kmemleak-take-kmemleak_lock-in-the-disable-time-object-cleanup.patch added to mm-new branch Andrew Morton
2026-12-04 15:25   ` [PATCH 1/3] mm: kmemleak: take kmemleak_lock in the disable-time object cleanup sashiko-bot
2026-12-05 10:37     ` Catalin Marinas
2026-12-05 17:35       ` Andrew Morton
2026-12-03 15:25 ` [PATCH 2/3] mm: kmemleak: do not hex dump objects once free tracking has stopped Catalin Marinas
2026-12-03 18:40   ` + mm-kmemleak-do-not-hex-dump-objects-once-free-tracking-has-stopped.patch added to mm-new branch Andrew Morton
2026-12-03 15:25 ` [PATCH 3/3] mm: kmemleak: annotate the lockless accesses to the enable flags Catalin Marinas
2026-12-03 18:40   ` Andrew Morton [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203184100.51B1E1F000C1@smtp.kernel.org \
    --to=akpm@linux-foundation.org \
    --cc=catalin.marinas@arm.com \
    --cc=david@kernel.org \
    --cc=leitao@debian.org \
    --cc=mm-commits@vger.kernel.org \
    --cc=tongweilin@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.