mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC PATCH v3 0/2] bpf, mm: Avoid allocator deadlocks on PREEMPT_RT
@ 2026-12-02  2:35 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  2:35 ` [RFC PATCH v3 2/2] mm: restrict can_spin_trylock() to preemptible context on PREEMPT_RT Karl Mehltretter
  0 siblings, 2 replies; 4+ messages in thread
From: Karl Mehltretter @ 2026-12-02  2:35 UTC (permalink / raw)
  To: Sebastian Andrzej Siewior, Alexei Starovoitov, Vlastimil Babka
  Cc: Karl Mehltretter, Harry Yoo, Andrew Morton, Peter Zijlstra,
	Thomas Gleixner, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
	Ihor Solodrai, Amery Hung, David Hildenbrand, Lorenzo Stoakes,
	Liam R . Howlett, Mike Rapoport, Suren Baghdasaryan,
	Michal Hocko, Swaraj Gaikwad, Clark Williams, Steven Rostedt,
	bpf, linux-mm, linux-rt-devel, linux-kernel

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@gmail.com/
v2: https://lore.kernel.org/r/20261005070625.8871-1-kmehltretter@gmail.com/
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@kernel.org/

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)


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-02  3:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02  2:35 [RFC PATCH v3 0/2] bpf, mm: Avoid allocator deadlocks on PREEMPT_RT 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 ` [RFC PATCH v3 2/2] mm: restrict can_spin_trylock() to preemptible context on PREEMPT_RT Karl Mehltretter

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®