mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 0/2] bpf, mm: Avoid allocator deadlocks on PREEMPT_RT
Date: Wed,  2 Dec 2026 03:35:04 +0100	[thread overview]
Message-ID: <20261202023506.35176-1-kmehltretter@gmail.com> (raw)

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)


             reply	other threads:[~2026-12-02  2:35 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  2:35 Karl Mehltretter [this message]
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

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-1-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®