[RFC PATCH v3 2/2] mm: restrict can_spin_trylock() to preemptible context on PREEMPT_RT
From: Karl Mehltretter
Date: Tue Dec 01 2026 - 21:39:54 EST
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@xxxxxxxxx>
---
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)