From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ABDFD606A322 for ; Fri, 4 Dec 2026 01:56:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=bB+DKUby Received: by kanga.kvack.org (Postfix) id E603C6B05B8; Thu, 3 Dec 2026 20:56:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DC2126B05B9; Thu, 3 Dec 2026 20:56:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD7FC6B05BA; Thu, 3 Dec 2026 20:56:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4BF1C6B05B8 for ; Thu, 3 Dec 2026 20:56:44 -0500 (EST) Received: from smtpin24.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id AB30DA0245 for ; Fri, 4 Dec 2026 01:56:43 +0000 (UTC) X-FDA: 85504636686.24.86E088D Received: from mail-dl1-f71.google.com (mail-dl1-f71.google.com [74.125.82.71]) by imf26.hostedemail.com (Postfix) with ESMTP id EB09514000F for ; Fri, 4 Dec 2026 01:56:41 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20260707 header.b=bB+DKUby; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 32B0SawYKCLstvsfochpphmf.dpnmjovy-nnlwbdl.psh@flex--surenb.bounces.google.com designates 74.125.82.71 as permitted sender) smtp.mailfrom=32B0SawYKCLstvsfochpphmf.dpnmjovy-nnlwbdl.psh@flex--surenb.bounces.google.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796349402; b=szCwNiPCZRDJbg2+bm4dP72aw0/YBiccMS19G5HG3IZ+2RkPn+xJBL/o33DMXHspKAwaoA olJLBLGmAzpy0pFYL9amfo909RUtinXhVmGVsLrI8atPYl43kIy8KOR+jtgL0KKlCfLu/i WVdcVWzjqmKHrdQewcD2qqkQHudYzEI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796349402; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=WiIBj+TQ29F7QSkIqyChVCWsgfxHaidhXcn6XqZ9jh8=; b=Fny5lFiKWiypnPkWxbxHeTE+0E8PCCzMZAkdDTHL6o27WHDNNFBFgrfCy1EUU3IdXiE8Vq UoTMdq4b8n3xOsXWzTNfBRNp0bCGiHdSBkorMJXqDbrBx7C12WILfOIXbxrnxUf0Hw3C9e 3fuz/kAvm60TfIoOqpbc4kBEF/c6Fow= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20260707 header.b=bB+DKUby; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 32B0SawYKCLstvsfochpphmf.dpnmjovy-nnlwbdl.psh@flex--surenb.bounces.google.com designates 74.125.82.71 as permitted sender) smtp.mailfrom=32B0SawYKCLstvsfochpphmf.dpnmjovy-nnlwbdl.psh@flex--surenb.bounces.google.com Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-1b588e4bd7fso418026c88.0 for ; Thu, 03 Dec 2026 17:56:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796349401; x=1796954201; darn=kvack.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=WiIBj+TQ29F7QSkIqyChVCWsgfxHaidhXcn6XqZ9jh8=; b=bB+DKUby/HQY0P1U2Y6Qe6R9zxdglrrG47lgGfM7w0fup62Wz82wKbeqLjrl36f66B 5juwF4vKpJHaaMfABbOOpb45grUBgBJTkzxsmiLYJq+QGzS7HchNEjzAGq291deWqz80 fa+OKpJN7IQjcygKitwoDOtb9FX3dI7+n/PvyS+XD3YD4QHY7e5rYocFIoanvbC6UCwV DAqnTWHUV1G+Xi9dt5b2Z26OqJkte2PkoIxIMY15iGLTUZQ90cC+y/hbBYvyMEoHw5wi BF/1QkGv0wh/VZYuxWyPIEOwBqtwUbXoSYYGlmFY0n+nBHZsEE2ePZkOUA4huex1/s59 SbYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796349401; x=1796954201; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=WiIBj+TQ29F7QSkIqyChVCWsgfxHaidhXcn6XqZ9jh8=; b=1zBhCZ9k31h+3sCD5JYKrN/OjPVCysKSRczwBx6jNrCyY38Ck+mkvP5YZ++vHM9bT5 SnWE28jcbU5P0HGkfvCTM6139pAnZG2DuKi8hP2hCC9GtmSvHwE2WELJtFR+aUc13ahM JRRgif48gi1trP591f8YBLTzhTG/WqPhMhdzNIxfb41v2LdghS/K4ag4P5BHF9nf0qoo Ybaz29Ck+8exHAmQwuvlonFY+kVRC7WWMIaBZvlWOS2diqsHSeKy6aMD+Own8CMWflHB EYe8iyY/RZZvfLxUnaZ+afuJly+wCKzp55GThPRNhKwnRqBB4NsdhWA5DBJGijo4/M0s U5SQ== X-Forwarded-Encrypted: i=1; AL+hErG9/hTB5+tPg8wCrnliOWtM/pXncnWb/6I1DztVg5AVLCsSDICmO0RKPtroIMicfBzOTdzwegYnvQ==@kvack.org X-Gm-Message-State: APuU4Z/VB06WUvK5jsF6FaIMY3KQe1u0mvXXmJHgsQftCIAWuRi6zwLU ta14ECtL4oxcqZwrfWpnh6qnbMaramvxdG6HrVesxXytg+VFYYHVST/PmVLUHUF0wXPY1k9uRCU OGIYK6g== X-Received: from dldyr5-n2.prod.google.com ([2002:a05:701b:4505:20b0:1b5:345d:ed30]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:450e:20b0:1b5:3878:a9d2 with SMTP id a92af1059eb24-1b5d7125d6dmr564104c88.20.1796349400011; Thu, 03 Dec 2026 17:56:40 -0800 (PST) Date: Thu, 3 Dec 2026 17:56:30 -0800 In-Reply-To: <20261204015630.4095988-1-surenb@google.com> X-Archived-By: Mime-Version: 1.0 References: <20261204015630.4095988-1-surenb@google.com> X-Mailer: git-send-email 2.56.1.987.g2ec0ab4260-goog Message-ID: <20261204015630.4095988-4-surenb@google.com> Subject: [PATCH 3/3] mm/show_mem: avoid unsafe locking when reporting allocation profiling From: Suren Baghdasaryan 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 Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: qeb7bcoxtgpf94nnonotxpspi1bgrmud X-Spam-Status: No, score=-26.5 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=4.0.1 X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: EB09514000F X-HE-Tag: 1796349401-143920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yuL4mOlfxgmIcAxWUrUAgGjTUai4ejas6Bx7hGaDuwUzpPVoCypI1KzBbypGwmiTktmWITMhn7LL3tGDpat1nCDPmjYYBojeIq9LYNWG6ZVXeG+iLJM6kZoNH68LKlawioI1F2rd7XcZn0B95c+YJ/mxGwjTDoyYb8eTas9TfF+dWWj9Cr32WGlfhzZOiTREzhinHmDG0rv/yj9zB93vTftiNZEUKZCRHA+kJ8O+b7EliEc5QMdAG4r8TGNWGRlV27BUzLtysYD5lOcHjE0wq0DKbP3WxLG3h3o6T+nR06XjYKkCIkiuC4Ziagq+pg1/++qC/Xbbqw6d6r2RBuM6ZBiWtTsd1x+DMzlDNXjUqz11Yb53WVUnI3oTLD6a71uyhAWEEP/fG3tAC9QHohmQEVti5xvadfUAbnYJHddI0bQTfqalpFdw/YiIry2uVijlaxPikOxekEZtDbqzga1AQQJt0vB4WaDTCmnZlIt9PIbFnIKE8MbsCUckHvM98U8XbSd+odDqB4OG0lHTUA+Hh9OztxNKJcw60BBuMGzckSAtGaOaE0Yr/mgITRet7hGvv0H4EGdTMu6J4jEem5uB51p33DeZ/35hjMX4tkcJjrRBfjb/yn1b9o/cMX9XMF1GBpY1FTpytVO2cKeBGk7DpK70eYR+FarmOPD7dB274Ce1TBE7PDIB9rBj5cyxktWc6TFBiDRIqV5TR2agjo1n/KMoaHL27Rq3ManTJFAPvlvjHhUn6BF3vqwMagYygdjJ2zMnB1iXZnMskH9kj0qxzoEZHc+CIo28Eg0I3f1hp7ResMY6Q== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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