From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796349402; b=An9o8/6elgDslNmysrWoDUycYPKWK+j1V0pJT8fRNtxN7vcOtSKp8x4Ct9GE9LC9N93J DpGj2PO+X01KcFZo6ZnhjdAmiDs+BIWuTmxGtJN1SpYwy14TlmBsQM06tmFopCGdzKLEr dcaa72ryLtZVxWPy2ReIUqAa+gdBHZhkoj2dIy0JowWIvqZjrUdsQJpu6nYmxYcf9rHae kVrgR2ZgjIz0xPSbWA4Lj5pdN2T062r8RmV07/zWFAbvRZK6FzZCfyWEUvovQ8XBykF+W nrUznsh8MloZeIjh3nsMCzztJVv/BIUVbGFssehNrmyXkadiPThTwfG/fuFZfYlcu3Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796349402; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=WiIBj+TQ29F7QSkIqyChVCWsgfxHaidhXcn6XqZ9jh8=; b=39r4bSgbWXZYsl+zky5yNoQIqa/fvpSdgR+J+iaJe6PhC+uNffXVAB20KXu0h9pBZUH5 b9elCi33JW5VoJn3yGWG8PjrZFZGk2hkgRZwVMjUgQWlu+kR70+0588vEF4PK49vijBho wyl8sAIvJHXpuIfgQTYsl3AjtlOLDo4G9UjleMdSfwldHUyya/BfLH3uhtympi7HijhHf Ju+j2/CLKtnC37p6gBXACbof9CaXuDcBWulcGQJg6q44oEnRI80aPVAg6IcVu95qTvscY oJhPEbVWSIIVDtLTUH43dockk0799DTUAFwYXT61KW+K5LTEzWH+jXVQHkJZKBHIKpQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=XgFACAOx; arc=none smtp.remote-ip=74.125.82.70 Received: from mail-dl1-f70.google.com (mail-dl1-f70.google.com [74.125.82.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C7428606A32C for ; Fri, 4 Dec 2026 01:56:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C7428606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com 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=XgFACAOx Received: by mail-dl1-f70.google.com with SMTP id a92af1059eb24-1b588e4bd7fso418056c88.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=lists.linux.dev; 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=XgFACAOxWIQ7Rwoq05XXpGCdmHBSrGdqAexNeb2U2N/VgNbMkTOPNhN1FhyHmzx686 eQryxpbfhTRej6OG1bZ12EPnkcyt3rGaSUcTBd+OZqVn94CA6HdDP2m9GDyzgzICpMx7 rymLR/OmQ4KPvDd3gMcMW5VSItyBAFzZBvCJsxZ3o33fkomHyEXVce/IWScfjFd8IJUT VxZlSAAoEkM5NuXiDlaUQy2Mybq27R4csHedB2SliBx2HA1wKYAkU1N07pm3/T8d77kI QoGgIHCbbsVQpCaQdp7IsITNQ0rniefQmrKZ23kiEznP0U4vObevXVJ5Tn97BgAB39OS AGfg== 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=IM/sDH+OGCUBDNZu4KjeioPfWCmngFJg45/csGnvLWbik9pDSX0R7bew22pdMqeIzt 3vZ5qmxNYgDjO9c2jo9ClmKwSuG9+rruKuk7+Wnc0ib77yhDVbGSE2QCYFE2VGrJxVrw N8Mq2rvAwpMo4kboghuXeNcQdkmdaVLZKQK1DnsI5V44JhxBhKFU49kQb7Af7txTYYJS OlWoOsYnfwJq+D6bE+sTqs9ARiDR0wGWF7GGMp+Qv9iU0jakHG5vUsdsry+gPcQ2JClA uZRrS0i11lUe7R66yUGkfHlOs6IxyEPso0Qatq3AZ42U9QPmV+47IphHSYwTSKk5UOV7 yuig== X-Forwarded-Encrypted: i=1; AL+hErFSfujdDi+pRwXyHF20Lwia7SIogtEsw0FAAFpJsh9NRKWJK92CFLZP3qqZOZU175s9dWTSnhSJ1ZFZM9y0zg==@lists.linux.dev X-Gm-Message-State: APuU4Z8BJmvAJoOpaNLZjOChJQkeXRh3UUNEUe6o3r7HcMRzGEGmPMWv 4Ttypk3TpqzzX5zynfG8sDWwYNPO/7ghu350GmRr2K4lmXh1wXx9Whc13VgQ/xxJuB17dJBryV1 8URoGkg== 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-Mailing-List: linux-rt-devel@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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" 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