All of lore.kernel.org
 help / color / mirror / Atom feed
From: Suren Baghdasaryan <surenb@google.com>
To: akpm@linux-foundation.org
Cc: david@kernel.org, hao.ge@linux.dev, liam@infradead.org,
	vbabka@kernel.org,  hannes@cmpxchg.org,
	brendan.jackman@linux.dev, ziy@nvidia.com,
	 bigeasy@linutronix.de, clrkwllms@kernel.org,
	rostedt@goodmis.org,  pasha.tatashin@soleen.com,
	pyyjason@gmail.com, usamaarif642@gmail.com,  surenb@google.com,
	linux-mm@kvack.org, linux-rt-devel@lists.linux.dev
Subject: [PATCH 3/3] mm/show_mem: avoid unsafe locking when reporting allocation profiling
Date: Thu,  3 Dec 2026 17:56:30 -0800	[thread overview]
Message-ID: <20261204015630.4095988-4-surenb@google.com> (raw)
In-Reply-To: <20261204015630.4095988-1-surenb@google.com>

With CONFIG_MEM_ALLOC_PROFILING=y, __show_mem() reports the top memory
allocation tags. __show_mem() can be reached from any context, including
hardirq, NMI and panic, and the locking used for the report is not safe
in all of them:

1. __show_mem() takes mem_alloc_profiling_spinlock with spin_trylock().
   On PREEMPT_RT spinlock_t is based on rtmutex and must not be acquired,
   even with a trylock, from hardirq or NMI context.

2. alloc_tag_top_users() takes alloc_tag_cttype->mod_lock with
   down_read_trylock() and drops it with up_read(). The trylock never
   blocks, but if a writer (module load/unload) queues up in the
   meantime, up_read() has to take the rwsem's wait_lock and wake the
   writer, which takes scheduler locks. From NMI this can deadlock if
   the NMI interrupted a holder of one of those locks, and during panic
   such a holder might have been stopped on another CPU. On PREEMPT_RT,
   __rwbase_read_unlock() additionally uses raw_spin_lock_irq() and
   raw_spin_unlock_irq(), enabling interrupts for a caller which had
   them disabled.

Replace mem_alloc_profiling_spinlock with an atomic flag, which is safe
in every context.

Skip the report instead of trylocking the alloc_tag_cttype->mod_lock
when dropping the lock would not be safe: in NMI, during panic, and on
PREEMPT_RT in hardirq context or with interrupts disabled.

As a result the "Memory allocations" section is no longer printed by
__show_mem() calls made from those contexts. Reports printed on OOM are
not affected, including with panic_on_oom, since dump_header() runs
before panic().

Fixes: 8147bc15b409 ("mm/show_mem: add trylock while printing alloc info")
Fixes: 1438d349d16b ("lib: add memory allocations report in show_mem()")
Closes: https://sashiko.dev/#/bug/linux-852c758c-0935-4ef7-b76a-6a9c8cd55b2a
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
---
 mm/alloc_tag.c | 30 +++++++++++++++++++++++++++++-
 mm/show_mem.c  | 11 ++++++++---
 2 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/mm/alloc_tag.c b/mm/alloc_tag.c
index 7c12053445fe..b04c987f15c4 100644
--- a/mm/alloc_tag.c
+++ b/mm/alloc_tag.c
@@ -6,6 +6,8 @@
 #include <linux/kallsyms.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
+#include <linux/panic.h>
+#include <linux/preempt.h>
 #include <linux/compat.h>
 #include <linux/page_ext.h>
 #include <linux/pgalloc_tag.h>
@@ -610,6 +612,31 @@ void alloc_tag_trace_mem_free(const void *ptr, struct alloc_tag *tag,
 }
 EXPORT_SYMBOL(alloc_tag_trace_mem_free);
 
+/*
+ * Trylocking the module list never blocks, but unlocking it can: if a writer
+ * queued up after a successful trylock, up_read() has to take the rwsem's
+ * wait_lock to wake the writer.
+ */
+static bool alloc_tag_can_trylock_module_list(void)
+{
+	/*
+	 * Not safe from NMI (might have interrupted the lock holder).
+	 * Not safe during panic (the lock holder might have been stopped
+	 * on another CPU).
+	 */
+	if (in_nmi() || panic_in_progress())
+		return false;
+
+	/*
+	 * On PREEMPT_RT the unlock slow path uses raw_spin_lock_irq() and
+	 * raw_spin_unlock_irq() which would enable interrupts unconditionally.
+	 */
+	if (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_hardirq() || irqs_disabled()))
+		return false;
+
+	return true;
+}
+
 size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep)
 {
 	struct codetag_iterator iter;
@@ -622,7 +649,8 @@ size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sl
 
 	if (can_sleep)
 		codetag_lock_module_list(alloc_tag_cttype);
-	else if (!codetag_trylock_module_list(alloc_tag_cttype))
+	else if (!alloc_tag_can_trylock_module_list() ||
+		 !codetag_trylock_module_list(alloc_tag_cttype))
 		return 0;
 
 	iter = codetag_get_ct_iter(alloc_tag_cttype);
diff --git a/mm/show_mem.c b/mm/show_mem.c
index b938cbcd774a..1c369c539665 100644
--- a/mm/show_mem.c
+++ b/mm/show_mem.c
@@ -433,9 +433,14 @@ void __show_mem(unsigned int filter, const nodemask_t *nodemask,
 	printk("%ld pages hwpoisoned\n", atomic_long_read(&num_poisoned_pages));
 #endif
 #ifdef CONFIG_MEM_ALLOC_PROFILING
-	static DEFINE_SPINLOCK(mem_alloc_profiling_spinlock);
+	/*
+	 * Prevents concurrent reports from interleaving. Use an atomic instead
+	 * of a lock to keep it valid in any context, including hardirq, NMI,
+	 * and on PREEMPT_RT.
+	 */
+	static atomic_t mem_alloc_profiling_busy;
 
-	if (spin_trylock(&mem_alloc_profiling_spinlock)) {
+	if (!atomic_cmpxchg(&mem_alloc_profiling_busy, 0, 1)) {
 		struct codetag_bytes tags[10];
 		size_t i, nr;
 
@@ -462,7 +467,7 @@ void __show_mem(unsigned int filter, const nodemask_t *nodemask,
 						  ct->lineno, ct->function);
 			}
 		}
-		spin_unlock(&mem_alloc_profiling_spinlock);
+		atomic_set(&mem_alloc_profiling_busy, 0);
 	}
 #endif
 }
-- 
2.56.1.987.g2ec0ab4260-goog


  parent reply	other threads:[~2026-12-04  1:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  1:56 [PATCH 0/3] alloc_tag: fixes for module tag area handling and the show_mem() report Suren Baghdasaryan
2026-12-04  1:56 ` [PATCH 1/3] alloc_tag: preserve the error when reserving a module tag area fails Suren Baghdasaryan
2026-12-04  2:06   ` Liam R. Howlett
2026-12-04  7:47   ` Hao Ge
2026-12-04  1:56 ` [PATCH 2/3] alloc_tag: do not leak module tag areas when removing them fails Suren Baghdasaryan
2026-12-04  2:29   ` Liam R. Howlett
2026-12-04 18:31     ` Suren Baghdasaryan
2026-12-04  1:56 ` Suren Baghdasaryan [this message]
2026-12-04  2:48   ` [PATCH 3/3] mm/show_mem: avoid unsafe locking when reporting allocation profiling Liam R. Howlett
2026-12-04 18:44     ` Suren Baghdasaryan

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=20261204015630.4095988-4-surenb@google.com \
    --to=surenb@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=bigeasy@linutronix.de \
    --cc=brendan.jackman@linux.dev \
    --cc=clrkwllms@kernel.org \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=hao.ge@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=pasha.tatashin@soleen.com \
    --cc=pyyjason@gmail.com \
    --cc=rostedt@goodmis.org \
    --cc=usamaarif642@gmail.com \
    --cc=vbabka@kernel.org \
    --cc=ziy@nvidia.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.