[RFC PATCH v3 0/2] bpf, mm: Avoid allocator deadlocks on PREEMPT_RT

From: Karl Mehltretter

Date: Tue Dec 01 2026 - 21:35:26 EST


On PREEMPT_RT, a BPF program on sched_waking that creates task storage
can hang the machine.

kmalloc_nolock() runs in try_to_wake_up(p) with p->pi_lock held and IRQs
off, and trylocks rtmutex-based slab locks. Unlocking one can wake p
and re-enter try_to_wake_up(p), deadlocking on p->pi_lock (confirmed in
GDB). Patch 2 has the details.

v1 restored v6.19's !preemptible() check in kmalloc_nolock(). Alexei
pointed out that this broke BPF arena on RT, which allocated range-tree
nodes (and a fallback page on fault) under arena->spinlock. v2 tried to
make rtmutex trylocks usable from there instead. Following Sebastian's
feedback, that approach is dropped.

This version moves the arena allocations and range-node frees outside
arena->spinlock first (patch 1), then restricts can_spin_trylock() to
preemptible context on PREEMPT_RT (patch 2). A held raw spinlock implies
!preemptible(), so the check does not need to identify specific caller
locks. This is deliberately a conservative RT-only guard rather than an
attempt to classify tracepoint contexts; non-RT is unchanged.

On PREEMPT_RT, the nolock allocation helpers now return NULL when called
with preemption or IRQs disabled. This restores the earlier RT
restriction for kmalloc_nolock() and extends it to alloc_pages_nolock()
(whose zone and PCP locks are also rtmutex-backed on RT);
free_pages_nolock() falls back to its deferred list. Sleepable arena
use, arena faults and preemptible BPF programs keep working. New
task-storage allocations from sched_waking and kprobes fail on RT, as do
arena allocations (and reservations, returning -ENOMEM) under an outer
bpf_spin_lock.

Patch 2 depends on patch 1 so arena does not break on RT. Both should
be applied in order, ideally through the same tree with subsystem acks.

Open questions:

- Is !preemptible() in can_spin_trylock() on RT an acceptable tradeoff?

- Non-sleepable bpf_arena_free_pages() under a bpf_spin_lock on RT now
always fails to allocate the deferred-free span, retaining the pages
until map destruction. Hardirq and NMI callers already behave this
way, but because bpf_arena_free_pages() returns void, callers cannot
detect or retry it. What would be the preferred way to preserve
deferred freeing from these contexts?

- kfree_nolock() on RT still trylocks the sheaves and memcg stock locks
in non-preemptible context. This is outside the scope of the
allocation-side fix here; would Harry's recent kmalloc_nolock()
series be a suitable basis for addressing it separately?

Tested on x86-64 QEMU/KVM (4 vCPUs, v7.4-rc5):

- RT + lockdep, sched_waking: circular lockdep splats in 2/2 unpatched
v7.4-rc5 boots; none in 4 patched boots (2 targeted, 2 with the v1
reproducer). No task-storage allocations succeeded on RT, as
expected.
- RT without lockdep, sched_waking + kmalloc-cg-192 churn (40 s): 0/16
hangs on patched v7.4-rc5 (unpatched v7.4-rc3 hung 3/21 KVM and 1/12
TCG).
- Arena test (alloc, free, reserve, faults; single-thread and 4-thread
adjacent ranges): passes on RT (4 prod, 2 lockdep) and !RT + lockdep
(2 runs).
- !RT + lockdep, sched_waking: 2/2 clean, every allocation succeeds.
- Both commits build cleanly on their own with W=1 (RT lockdep, RT prod
and !RT lockdep).

Assisted by an LLM.

v1: https://lore.kernel.org/r/20260919171443.90512-1-kmehltretter@xxxxxxxxx/
v2: https://lore.kernel.org/r/20261005070625.8871-1-kmehltretter@xxxxxxxxx/
pi_lock thread: https://lore.kernel.org/r/awbvek-HS3OJYJGF@thinkstation/
Harry's kmalloc_nolock series: https://lore.kernel.org/r/20261115-kmalloc-nolock-fixes-v3-0-9034dd4ecaab@xxxxxxxxxx/

Karl Mehltretter (2):
bpf: Move arena allocations and frees outside arena->spinlock
mm: restrict can_spin_trylock() to preemptible context on PREEMPT_RT

kernel/bpf/arena.c | 129 +++++++++++++++++++++++++---------------
kernel/bpf/range_tree.c | 80 ++++++++++++++++++-------
kernel/bpf/range_tree.h | 15 ++++-
mm/internal.h | 10 +++-
4 files changed, 159 insertions(+), 75 deletions(-)


base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
--
2.39.5 (Apple Git-154)