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 --- 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 #include #include +#include +#include #include #include #include @@ -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