* [PATCH 0/3] alloc_tag: fixes for module tag area handling and the show_mem() report
@ 2026-12-04 1:56 Suren Baghdasaryan
2026-12-04 1:56 ` [PATCH 1/3] alloc_tag: preserve the error when reserving a module tag area fails Suren Baghdasaryan
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Suren Baghdasaryan @ 2026-12-04 1:56 UTC (permalink / raw)
To: akpm
Cc: david, hao.ge, liam, vbabka, hannes, brendan.jackman, ziy,
bigeasy, clrkwllms, rostedt, pasha.tatashin, pyyjason,
usamaarif642, surenb, linux-mm, linux-rt-devel
This series fixes three issues in memory allocation profiling which were
reported by Sashiko.
Patches 1 and 2 fix the error handling while operating on the maple tree
which tracks the areas reserved for module allocation tags. Patch 3 fixes
the locking used by the "Memory allocations" report in __show_mem(), which
can be called from any context, including hardirq or NMI as well as on
PREEMPT_RT.
The series is based on mm-unstable.
Suren Baghdasaryan (3):
alloc_tag: preserve the error when reserving a module tag area fails
alloc_tag: do not leak module tag areas when removing them fails
mm/show_mem: avoid unsafe locking when reporting allocation profiling
mm/alloc_tag.c | 47 +++++++++++++++++++++++++++++++++++++++++++----
mm/show_mem.c | 11 ++++++++---
2 files changed, 51 insertions(+), 7 deletions(-)
base-commit: e685fdf67323bade713c5f81015eda5094b3261b
--
2.56.1.987.g2ec0ab4260-goog
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 1/3] alloc_tag: preserve the error when reserving a module tag area fails
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 ` 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 1:56 ` [PATCH 3/3] mm/show_mem: avoid unsafe locking when reporting allocation profiling Suren Baghdasaryan
2 siblings, 2 replies; 8+ messages in thread
From: Suren Baghdasaryan @ 2026-12-04 1:56 UTC (permalink / raw)
To: akpm
Cc: david, hao.ge, liam, vbabka, hannes, brendan.jackman, ziy,
bigeasy, clrkwllms, rostedt, pasha.tatashin, pyyjason,
usamaarif642, surenb, linux-mm, linux-rt-devel
When storing the module area fails after the padding in front of it was
stored, reserve_module_tags() removes the padding with mas_erase() and
only then reads the error from the maple state. mas_erase() walks the
tree and overrides the error stored in mas.node, therefore the correct
error code gets cleared and later treated as area reservation success.
This leaves the same memory area available for allocation to subsequent
modules, causing silent memory corruption of allocation tags.
Store the error before removing the padding.
Fixes: 0db6f8d7820a ("alloc_tag: load module tags into separate contiguous memory")
Closes: https://sashiko.dev/#/bug/linux-64e7eab3-9791-43b6-a55e-fc05f931983c
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
---
mm/alloc_tag.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/mm/alloc_tag.c b/mm/alloc_tag.c
index 18bec85b6220..b4e55bbf57f6 100644
--- a/mm/alloc_tag.c
+++ b/mm/alloc_tag.c
@@ -1010,9 +1010,10 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,
mas.last = offset + size - 1;
mas_store(&mas, mod);
if (mas_is_err(&mas)) {
+ /* mas_erase() resets the error in the maple state */
+ ret = ERR_PTR(xa_err(mas.node));
mas.index = pad_start;
mas_erase(&mas);
- ret = ERR_PTR(xa_err(mas.node));
}
} else {
mas.last = offset + size - 1;
--
2.56.1.987.g2ec0ab4260-goog
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 2/3] alloc_tag: do not leak module tag areas when removing them fails
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 1:56 ` Suren Baghdasaryan
2026-12-04 2:29 ` Liam R. Howlett
2026-12-04 1:56 ` [PATCH 3/3] mm/show_mem: avoid unsafe locking when reporting allocation profiling Suren Baghdasaryan
2 siblings, 1 reply; 8+ messages in thread
From: Suren Baghdasaryan @ 2026-12-04 1:56 UTC (permalink / raw)
To: akpm
Cc: david, hao.ge, liam, vbabka, hannes, brendan.jackman, ziy,
bigeasy, clrkwllms, rostedt, pasha.tatashin, pyyjason,
usamaarif642, surenb, linux-mm, linux-rt-devel
release_module_tags() removes the area of a module, and the padding in
front of it, from mod_area_mt with mas_store(&mas, NULL). Storing NULL can
merge the range with its neighbours and rebalance the tree, which needs
new maple tree node allocations (with GFP_NOWAIT) and therefore can fail.
If the allocation fails, the area stays in the tree and still points to
the module that is going away. The maple state is left in the error state,
so mas_prev_range() returns NULL and the padding is not removed either.
If the allocation fails when the padding is removed, the padding is
leaked.
Use mas_erase() to remove the entries. It retries the allocation with the
tree lock dropped and cannot fail. Dropping the lock is fine because all
the changes to mod_area_mt are serialized by codetag_lock. Marking the
area with unloaded_mod replaces the entry in place using mas_store()
which does not allocate and therefore can't fail.
Fixes: 0db6f8d7820a ("alloc_tag: load module tags into separate contiguous memory")
Closes: https://sashiko.dev/#/bug/linux-55c2bd22-02fc-49f6-9ee7-e43117224340
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
---
mm/alloc_tag.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/mm/alloc_tag.c b/mm/alloc_tag.c
index b4e55bbf57f6..7c12053445fe 100644
--- a/mm/alloc_tag.c
+++ b/mm/alloc_tag.c
@@ -1087,10 +1087,20 @@ static void release_module_tags(struct module *mod, bool used)
used = false;
}
release_area:
- mas_store(&mas, used ? &unloaded_mod : NULL);
+ /*
+ * Replacing the entry in place does not need new maple tree nodes and
+ * cannot fail. Removing it might, and mas_store() allocates them with
+ * GFP_NOWAIT. A failure would leave the area reserved forever, with a
+ * stale module pointer in it. Use mas_erase(), which does not fail.
+ */
+ if (used)
+ mas_store(&mas, &unloaded_mod);
+ else
+ mas_erase(&mas);
+
val = mas_prev_range(&mas, 0);
if (val == &prepend_mod)
- mas_store(&mas, NULL);
+ mas_erase(&mas);
out:
mas_unlock(&mas);
}
--
2.56.1.987.g2ec0ab4260-goog
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 3/3] mm/show_mem: avoid unsafe locking when reporting allocation profiling
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 1:56 ` [PATCH 2/3] alloc_tag: do not leak module tag areas when removing them fails Suren Baghdasaryan
@ 2026-12-04 1:56 ` Suren Baghdasaryan
2026-12-04 2:48 ` Liam R. Howlett
2 siblings, 1 reply; 8+ messages in thread
From: Suren Baghdasaryan @ 2026-12-04 1:56 UTC (permalink / raw)
To: akpm
Cc: david, hao.ge, liam, vbabka, hannes, brendan.jackman, ziy,
bigeasy, clrkwllms, rostedt, pasha.tatashin, pyyjason,
usamaarif642, surenb, linux-mm, linux-rt-devel
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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH 1/3] alloc_tag: preserve the error when reserving a module tag area fails
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
1 sibling, 0 replies; 8+ messages in thread
From: Liam R. Howlett @ 2026-12-04 2:06 UTC (permalink / raw)
To: Suren Baghdasaryan
Cc: akpm, david, hao.ge, vbabka, hannes, brendan.jackman, ziy,
bigeasy, clrkwllms, rostedt, pasha.tatashin, pyyjason,
usamaarif642, linux-mm, linux-rt-devel
On 26/12/03 05:56PM, Suren Baghdasaryan wrote:
> When storing the module area fails after the padding in front of it was
> stored, reserve_module_tags() removes the padding with mas_erase() and
> only then reads the error from the maple state. mas_erase() walks the
> tree and overrides the error stored in mas.node, therefore the correct
> error code gets cleared and later treated as area reservation success.
> This leaves the same memory area available for allocation to subsequent
> modules, causing silent memory corruption of allocation tags.
>
> Store the error before removing the padding.
>
> Fixes: 0db6f8d7820a ("alloc_tag: load module tags into separate contiguous memory")
> Closes: https://sashiko.dev/#/bug/linux-64e7eab3-9791-43b6-a55e-fc05f931983c
> Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Reviewed-by: Liam R. Howlett (Google) <liam@infradead.org>
> ---
> mm/alloc_tag.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/mm/alloc_tag.c b/mm/alloc_tag.c
> index 18bec85b6220..b4e55bbf57f6 100644
> --- a/mm/alloc_tag.c
> +++ b/mm/alloc_tag.c
> @@ -1010,9 +1010,10 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,
> mas.last = offset + size - 1;
> mas_store(&mas, mod);
> if (mas_is_err(&mas)) {
> + /* mas_erase() resets the error in the maple state */
> + ret = ERR_PTR(xa_err(mas.node));
> mas.index = pad_start;
> mas_erase(&mas);
> - ret = ERR_PTR(xa_err(mas.node));
> }
> } else {
> mas.last = offset + size - 1;
> --
> 2.56.1.987.g2ec0ab4260-goog
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 2/3] alloc_tag: do not leak module tag areas when removing them fails
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
0 siblings, 0 replies; 8+ messages in thread
From: Liam R. Howlett @ 2026-12-04 2:29 UTC (permalink / raw)
To: Suren Baghdasaryan
Cc: akpm, david, hao.ge, vbabka, hannes, brendan.jackman, ziy,
bigeasy, clrkwllms, rostedt, pasha.tatashin, pyyjason,
usamaarif642, linux-mm, linux-rt-devel
On 26/12/03 05:56PM, Suren Baghdasaryan wrote:
> release_module_tags() removes the area of a module, and the padding in
> front of it, from mod_area_mt with mas_store(&mas, NULL). Storing NULL can
> merge the range with its neighbours and rebalance the tree, which needs
> new maple tree node allocations (with GFP_NOWAIT) and therefore can fail.
> If the allocation fails, the area stays in the tree and still points to
> the module that is going away. The maple state is left in the error state,
> so mas_prev_range() returns NULL and the padding is not removed either.
> If the allocation fails when the padding is removed, the padding is
> leaked.
>
> Use mas_erase() to remove the entries. It retries the allocation with the
> tree lock dropped and cannot fail. Dropping the lock is fine because all
> the changes to mod_area_mt are serialized by codetag_lock. Marking the
> area with unloaded_mod replaces the entry in place using mas_store()
> which does not allocate and therefore can't fail.
>
> Fixes: 0db6f8d7820a ("alloc_tag: load module tags into separate contiguous memory")
> Closes: https://sashiko.dev/#/bug/linux-55c2bd22-02fc-49f6-9ee7-e43117224340
> Signed-off-by: Suren Baghdasaryan <surenb@google.com>
This works.. but mas_erase() will drop the spinlock and reacquire it.
If you use mas_store_gfp(&mas, used ? &unloaded_mod : NULL, GFP_KERNEL);
then it will retry the allocation with GFP_KERNEL after a GFP_NOWAIT.
The mas_erase() will use __GFP_NOFAIL, so it should always work.
I'm happy with either way.
Reviewed-by: Liam R. Howlett (Google) <liam@infradead.org>
> ---
> mm/alloc_tag.c | 14 ++++++++++++--
> 1 file changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/mm/alloc_tag.c b/mm/alloc_tag.c
> index b4e55bbf57f6..7c12053445fe 100644
> --- a/mm/alloc_tag.c
> +++ b/mm/alloc_tag.c
> @@ -1087,10 +1087,20 @@ static void release_module_tags(struct module *mod, bool used)
> used = false;
> }
> release_area:
> - mas_store(&mas, used ? &unloaded_mod : NULL);
> + /*
> + * Replacing the entry in place does not need new maple tree nodes and
> + * cannot fail. Removing it might, and mas_store() allocates them with
> + * GFP_NOWAIT. A failure would leave the area reserved forever, with a
> + * stale module pointer in it. Use mas_erase(), which does not fail.
> + */
> + if (used)
> + mas_store(&mas, &unloaded_mod);
> + else
> + mas_erase(&mas);
> +
> val = mas_prev_range(&mas, 0);
> if (val == &prepend_mod)
> - mas_store(&mas, NULL);
> + mas_erase(&mas);
> out:
> mas_unlock(&mas);
> }
> --
> 2.56.1.987.g2ec0ab4260-goog
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3] mm/show_mem: avoid unsafe locking when reporting allocation profiling
2026-12-04 1:56 ` [PATCH 3/3] mm/show_mem: avoid unsafe locking when reporting allocation profiling Suren Baghdasaryan
@ 2026-12-04 2:48 ` Liam R. Howlett
0 siblings, 0 replies; 8+ messages in thread
From: Liam R. Howlett @ 2026-12-04 2:48 UTC (permalink / raw)
To: Suren Baghdasaryan
Cc: akpm, david, hao.ge, vbabka, hannes, brendan.jackman, ziy,
bigeasy, clrkwllms, rostedt, pasha.tatashin, pyyjason,
usamaarif642, linux-mm, linux-rt-devel
On 26/12/03 05:56PM, Suren Baghdasaryan wrote:
> 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()")
This probably needs to be split, if not now then when it's applied to
6.12.
> 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);
atomic_set_release(), I think?
> }
> #endif
> }
> --
> 2.56.1.987.g2ec0ab4260-goog
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 1/3] alloc_tag: preserve the error when reserving a module tag area fails
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
1 sibling, 0 replies; 8+ messages in thread
From: Hao Ge @ 2026-12-04 7:47 UTC (permalink / raw)
To: Suren Baghdasaryan
Cc: david, liam, vbabka, hannes, brendan.jackman, ziy, bigeasy,
clrkwllms, rostedt, pasha.tatashin, pyyjason, usamaarif642,
linux-mm, linux-rt-devel, akpm
On 2026/12/4 09:56, Suren Baghdasaryan wrote:
> When storing the module area fails after the padding in front of it was
> stored, reserve_module_tags() removes the padding with mas_erase() and
> only then reads the error from the maple state. mas_erase() walks the
> tree and overrides the error stored in mas.node, therefore the correct
> error code gets cleared and later treated as area reservation success.
> This leaves the same memory area available for allocation to subsequent
> modules, causing silent memory corruption of allocation tags.
>
> Store the error before removing the padding.
>
> Fixes: 0db6f8d7820a ("alloc_tag: load module tags into separate contiguous memory")
> Closes: https://sashiko.dev/#/bug/linux-64e7eab3-9791-43b6-a55e-fc05f931983c
> Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Reviewed-by: Hao Ge <hao.ge@linux.dev>
> ---
> mm/alloc_tag.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/mm/alloc_tag.c b/mm/alloc_tag.c
> index 18bec85b6220..b4e55bbf57f6 100644
> --- a/mm/alloc_tag.c
> +++ b/mm/alloc_tag.c
> @@ -1010,9 +1010,10 @@ static void *reserve_module_tags(struct module *mod, unsigned long size,
> mas.last = offset + size - 1;
> mas_store(&mas, mod);
> if (mas_is_err(&mas)) {
> + /* mas_erase() resets the error in the maple state */
> + ret = ERR_PTR(xa_err(mas.node));
> mas.index = pad_start;
> mas_erase(&mas);
> - ret = ERR_PTR(xa_err(mas.node));
> }
> } else {
> mas.last = offset + size - 1;
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-12-04 7:47 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 1:56 ` [PATCH 3/3] mm/show_mem: avoid unsafe locking when reporting allocation profiling Suren Baghdasaryan
2026-12-04 2:48 ` Liam R. Howlett
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox