From: Karl Mehltretter <kmehltretter@gmail.com>
To: Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Alexei Starovoitov <ast@kernel.org>,
Vlastimil Babka <vbabka@kernel.org>
Cc: Karl Mehltretter <kmehltretter@gmail.com>,
Harry Yoo <harry@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@linutronix.de>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Amery Hung <ameryhung@gmail.com>,
David Hildenbrand <david@kernel.org>,
Lorenzo Stoakes <ljs@kernel.org>,
"Liam R . Howlett" <liam@infradead.org>,
Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>,
Swaraj Gaikwad <swarajgaikwad1925@gmail.com>,
Clark Williams <clrkwllms@kernel.org>,
Steven Rostedt <rostedt@goodmis.org>,
bpf@vger.kernel.org, linux-mm@kvack.org,
linux-rt-devel@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [RFC PATCH v3 2/2] mm: restrict can_spin_trylock() to preemptible context on PREEMPT_RT
Date: Wed, 2 Dec 2026 03:35:06 +0100 [thread overview]
Message-ID: <20261202023506.35176-3-kmehltretter@gmail.com> (raw)
In-Reply-To: <20261202023506.35176-1-kmehltretter@gmail.com>
A BPF program on the sched_waking tracepoint runs in try_to_wake_up()
with p->pi_lock held and IRQs disabled. bpf_task_storage_get() with
BPF_LOCAL_STORAGE_GET_F_CREATE calls kmalloc_nolock() from there. On
PREEMPT_RT can_spin_trylock() rejects only hardirq and NMI context, so
the allocation goes on to trylock the per-CPU sheaves lock and
n->list_lock. Both are rtmutex-based on PREEMPT_RT:
- rt_spin_trylock() takes the rtmutex wait_lock under p->pi_lock. A
task that blocks on the same lock holds wait_lock and takes a
pi_lock in try_to_take_rt_mutex(). Lockdep reports this circular
dependency on the first event.
- If a waiter queued up after the trylock succeeded, rt_spin_unlock()
wakes it. That waiter can be p:
try_to_wake_up(p) raw_spin_lock_irqsave(&p->pi_lock)
bpf_task_storage_get()
kmalloc_nolock()
get_from_partial_node()
spin_unlock_irqrestore(&n->list_lock)
rt_mutex_slowunlock()
try_to_wake_up(p) raw_spin_lock_irqsave(&p->pi_lock)
The CPU spins with IRQs disabled on a lock that it holds. The other
CPUs then hang in TLB shootdowns that wait for it.
Commit 99a3e3a1cfc9 ("slab: fix kmalloc_nolock() context check for
PREEMPT_RT") made kmalloc_nolock() refuse non-preemptible context on
PREEMPT_RT. Commit 073d5f156292 ("slab: simplify kmalloc_nolock()")
relaxed this to hardirq and NMI again. The check is now in
can_spin_trylock().
The allocator cannot know which locks its caller holds. A held raw
spinlock implies !preemptible(), so allow preemptible context only. This
restores the earlier RT restriction for kmalloc_nolock() and extends it
to alloc_pages_nolock(), whose zone and per-CPU page locks are
rtmutex-based on PREEMPT_RT as well. free_pages_nolock() falls back to
its deferred list in those contexts.
BPF programs on PREEMPT_RT normally run preemptible with migration
disabled, and they keep allocating. Programs that run with preemption or
IRQs disabled, such as kprobe programs, programs on sched_waking or
programs holding a bpf_spin_lock, now get NULL, as hardirq and NMI
callers already do.
This patch depends on "bpf: Move arena allocations and frees outside
arena->spinlock" to avoid breaking BPF arena allocations and faults on
PREEMPT_RT; both patches must be applied in order, including in any
stable backport.
Fixes: 073d5f156292 ("slab: simplify kmalloc_nolock()")
Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com>
---
mm/internal.h | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/mm/internal.h b/mm/internal.h
index 96cb55d7816..aacd8ab962b 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -1676,10 +1676,14 @@ static inline bool can_spin_trylock(void)
* confuse PI logic, so return immediately if called from hard IRQ or
* NMI.
*
- * Note, irqs_disabled() case is ok. spin_trylock() can be called
- * from raw_spin_lock_irqsave region.
+ * Task context with a raw spinlock held is not safe either. The
+ * caller may hold a pi_lock, like a BPF program on a tracepoint in
+ * try_to_wake_up(). rt_spin_trylock() takes the rtmutex wait_lock
+ * and can take a pi_lock under it. rt_spin_unlock() wakes a waiter
+ * if there is one, which takes a pi_lock again. The locks held by
+ * the caller are not known here, so allow preemptible context only.
*/
- if (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_nmi() || in_hardirq()))
+ if (IS_ENABLED(CONFIG_PREEMPT_RT) && !preemptible())
return false;
/* On UP, spin_trylock() always succeeds even when it is locked */
--
2.39.5 (Apple Git-154)
prev parent reply other threads:[~2026-12-02 2:39 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 2:35 [RFC PATCH v3 0/2] bpf, mm: Avoid allocator deadlocks " Karl Mehltretter
2026-12-02 2:35 ` [RFC PATCH v3 1/2] bpf: Move arena allocations and frees outside arena->spinlock Karl Mehltretter
2026-12-02 3:19 ` bot+bpf-ci
2026-12-02 2:35 ` Karl Mehltretter [this message]
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=20261202023506.35176-3-kmehltretter@gmail.com \
--to=kmehltretter@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=ameryhung@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bigeasy@linutronix.de \
--cc=bpf@vger.kernel.org \
--cc=clrkwllms@kernel.org \
--cc=daniel@iogearbox.net \
--cc=david@kernel.org \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=harry@kernel.org \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-rt-devel@lists.linux.dev \
--cc=ljs@kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mhocko@suse.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=song@kernel.org \
--cc=surenb@google.com \
--cc=swarajgaikwad1925@gmail.com \
--cc=tglx@linutronix.de \
--cc=vbabka@kernel.org \
--cc=yonghong.song@linux.dev \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®