* [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
` (2 more replies)
0 siblings, 3 replies; 6+ 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] 6+ messages in thread
* [RFC PATCH v3 1/2] bpf: Move arena allocations and frees outside arena->spinlock
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 ` 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
2026-12-02 4:11 ` [RFC PATCH v3 0/2] bpf, mm: Avoid allocator deadlocks " Alexei Starovoitov
2 siblings, 1 reply; 6+ 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
Some arena paths allocate and free with arena->spinlock held and IRQs
disabled. range_tree_clear() and range_tree_set() call kmalloc_nolock()
when they split a free interval or add one, and kfree_nolock() when they
remove or merge intervals. arena_vm_fault() calls bpf_alloc_page(), that
is alloc_pages_nolock(), when the page it found before taking the lock
was freed meanwhile.
On PREEMPT_RT the locks behind both allocators are rtmutex-based. The
following patch, "mm: restrict can_spin_trylock() to preemptible context
on PREEMPT_RT", makes both fail when preemption is disabled. Arena
allocations at a fixed address, reservations and faults that split a
free interval would then fail on PREEMPT_RT, and frees that add an
interval would leave the range allocated.
Allocate a spare range_node before taking the lock and pass it to
range_tree_clear() and range_tree_set(). One node per call is enough: a
clear splits at most one interval and a set adds at most one. Queue the
nodes that a call removes and free them after the lock is dropped.
Reuse rb_range_size as the list link, so that struct range_node does not
grow.
arena_free_range() drops the lock between its clear and its set. It
allocates the node for the set after the pages are freed. Its clear gets
no spare: it needs one only when the whole range is free already, and
then there is nothing to do, as before. If the node for the set cannot
be allocated, the range stays allocated, as it did when kmalloc_nolock()
failed under the lock.
arena_vm_fault() now allocates the page and the node before taking the
lock. When the page that it found before taking the lock is gone, it
drops the lock, allocates and retries.
Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com>
---
kernel/bpf/arena.c | 129 +++++++++++++++++++++++++---------------
kernel/bpf/range_tree.c | 80 ++++++++++++++++++-------
kernel/bpf/range_tree.h | 15 ++++-
3 files changed, 152 insertions(+), 72 deletions(-)
diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
index 70a49b1200a..56ba6540618 100644
--- a/kernel/bpf/arena.c
+++ b/kernel/bpf/arena.c
@@ -282,6 +282,7 @@ static struct bpf_map *arena_map_alloc(union bpf_attr *attr)
struct vm_struct *kern_vm;
int numa_node = bpf_map_attr_numa_node(attr);
struct bpf_arena *arena;
+ struct range_tree_nodes range_nodes = {};
u64 vm_range;
int err = -ENOMEM;
@@ -331,7 +332,10 @@ static struct bpf_map *arena_map_alloc(union bpf_attr *attr)
goto err_free_arena;
range_tree_init(&arena->rt);
- err = range_tree_set(&arena->rt, 0, attr->max_entries);
+ if (range_tree_nodes_prepare(&range_nodes))
+ goto err_free_scratch;
+ err = range_tree_set(&arena->rt, 0, attr->max_entries, &range_nodes);
+ range_tree_nodes_release(&range_nodes);
if (err)
goto err_free_scratch;
mutex_init(&arena->lock);
@@ -495,6 +499,7 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
struct bpf_map *map = vmf->vma->vm_file->private_data;
struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
struct mem_cgroup *new_memcg, *old_memcg;
+ struct range_tree_nodes range_nodes = {};
LLIST_HEAD(pages);
struct page *page, *new_page = NULL;
struct apply_range_data data;
@@ -507,28 +512,18 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
kaddr = kbase + (u32)(vmf->address);
page = vmalloc_to_page((void *)kaddr);
- if (!page && !(arena->map.map_flags & BPF_F_SEGV_ON_FAULT)) {
- /*
- * Preallocate outside the lock so the allocation can reclaim;
- * __GFP_RETRY_MAYFAIL keeps the OOM killer out of it.
- */
- bpf_map_memcg_enter(&arena->map, &old_memcg, &new_memcg);
- new_page = alloc_pages_node(map->numa_node,
- GFP_KERNEL | __GFP_ZERO |
- __GFP_ACCOUNT | __GFP_NOWARN |
- __GFP_RETRY_MAYFAIL, 0);
- bpf_map_memcg_exit(old_memcg, new_memcg);
- }
+ if (!page && !(arena->map.map_flags & BPF_F_SEGV_ON_FAULT))
+ goto prepare;
+lock:
if (raw_res_spin_lock_irqsave(&arena->spinlock, flags)) {
/*
* A failed lock means a possible deadlock was detected. Don't
* return VM_FAULT_RETRY: this handler never took mmap_lock, but
* the fault path would re-take it on retry and deadlock. Fail.
*/
- if (new_page)
- free_pages_nolock(new_page, 0);
- return VM_FAULT_SIGBUS;
+ fault_ret = VM_FAULT_SIGBUS;
+ goto out_free;
}
page = vmalloc_to_page((void *)kaddr);
@@ -558,24 +553,16 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
goto out_err_locked;
}
- bpf_map_memcg_enter(&arena->map, &old_memcg, &new_memcg);
-
- if (!new_page) {
- /*
- * The probed page was freed meanwhile or preallocation failed;
- * try the non-blocking allocator, we cannot sleep here.
- */
- new_page = bpf_alloc_page(map->numa_node, false);
- if (!new_page) {
- fault_ret = VM_FAULT_SIGBUS;
- goto out_err_locked_memcg;
- }
+ if (!new_page || !range_nodes.spare) {
+ /* The page found before locking was freed. Prepare and retry. */
+ raw_res_spin_unlock_irqrestore(&arena->spinlock, flags);
+ goto prepare;
}
- ret = range_tree_clear(&arena->rt, vmf->pgoff, 1);
+ ret = range_tree_clear(&arena->rt, vmf->pgoff, 1, &range_nodes);
if (ret) {
fault_ret = VM_FAULT_SIGBUS;
- goto out_err_locked_memcg;
+ goto out_err_locked;
}
llist_add(&new_page->pcp_llist, &pages);
data.arena = arena;
@@ -586,12 +573,12 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
ret = apply_to_page_range(&init_mm, kaddr, PAGE_SIZE, apply_range_set_cb, &data);
if (ret) {
llist_del_first(&pages);
- range_tree_set(&arena->rt, vmf->pgoff, 1);
+ WARN_ON_ONCE(range_tree_set(&arena->rt, vmf->pgoff, 1,
+ &range_nodes));
fault_ret = VM_FAULT_SIGBUS;
- goto out_err_locked_memcg;
+ goto out_err_locked;
}
flush_vmap_cache(kaddr, PAGE_SIZE);
- bpf_map_memcg_exit(old_memcg, new_memcg);
/* new_page was consumed */
page = new_page;
new_page = NULL;
@@ -600,6 +587,7 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
raw_res_spin_unlock_irqrestore(&arena->spinlock, flags);
if (new_page)
free_pages_nolock(new_page, 0);
+ range_tree_nodes_release(&range_nodes);
/*
* A free clears the kernel pte and then takes the page lock before it
@@ -617,13 +605,32 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
put_page(page);
return fault_ret;
-out_err_locked_memcg:
- bpf_map_memcg_exit(old_memcg, new_memcg);
out_err_locked:
raw_res_spin_unlock_irqrestore(&arena->spinlock, flags);
+out_free:
if (new_page)
free_pages_nolock(new_page, 0);
+ range_tree_nodes_release(&range_nodes);
return fault_ret;
+
+prepare:
+ /*
+ * Preallocate outside the lock so the allocation can reclaim;
+ * __GFP_RETRY_MAYFAIL keeps the OOM killer out of it.
+ */
+ bpf_map_memcg_enter(&arena->map, &old_memcg, &new_memcg);
+ if (!new_page)
+ new_page = alloc_pages_node(map->numa_node,
+ GFP_KERNEL | __GFP_ZERO |
+ __GFP_ACCOUNT | __GFP_NOWARN |
+ __GFP_RETRY_MAYFAIL, 0);
+ range_tree_nodes_prepare(&range_nodes);
+ bpf_map_memcg_exit(old_memcg, new_memcg);
+ if (!new_page || !range_nodes.spare) {
+ fault_ret = VM_FAULT_SIGBUS;
+ goto out_free;
+ }
+ goto lock;
}
static const struct vm_operations_struct arena_vm_ops = {
@@ -749,7 +756,8 @@ static u64 clear_lo32(u64 val)
return val & ~(u64)~0U;
}
-static int arena_adjust_tree(struct bpf_arena *arena, long uaddr, long page_cnt, long *pgoff)
+static int arena_adjust_tree(struct bpf_arena *arena, long uaddr, long page_cnt, long *pgoff,
+ struct range_tree_nodes *range_nodes)
{
int ret;
@@ -758,7 +766,7 @@ static int arena_adjust_tree(struct bpf_arena *arena, long uaddr, long page_cnt,
ret = is_range_tree_set(&arena->rt, *pgoff, page_cnt);
if (ret)
return ret;
- return range_tree_clear(&arena->rt, *pgoff, page_cnt);
+ return range_tree_clear(&arena->rt, *pgoff, page_cnt, range_nodes);
}
ret = range_tree_find(&arena->rt, page_cnt);
@@ -767,7 +775,7 @@ static int arena_adjust_tree(struct bpf_arena *arena, long uaddr, long page_cnt,
*pgoff = ret;
- return range_tree_clear(&arena->rt, *pgoff, page_cnt);
+ return range_tree_clear(&arena->rt, *pgoff, page_cnt, range_nodes);
}
/*
@@ -781,6 +789,7 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt
long page_cnt_max = (arena->user_vm_end - arena->user_vm_start) >> PAGE_SHIFT;
u64 kern_vm_start = bpf_arena_get_kern_vm_start(arena);
struct mem_cgroup *new_memcg, *old_memcg;
+ struct range_tree_nodes range_nodes = {};
struct apply_range_data data;
LLIST_HEAD(pages);
long mapped = 0;
@@ -806,6 +815,8 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt
return 0;
}
bpf_map_memcg_enter(&arena->map, &old_memcg, &new_memcg);
+ if (range_tree_nodes_prepare(&range_nodes))
+ goto out_memcg;
ret = bpf_alloc_pages(node_id, page_cnt, &pages, sleepable);
if (ret)
@@ -820,7 +831,7 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt
if (raw_res_spin_lock_irqsave(&arena->spinlock, flags))
goto out_free_pages;
- ret = arena_adjust_tree(arena, uaddr, page_cnt, &pgoff);
+ ret = arena_adjust_tree(arena, uaddr, page_cnt, &pgoff, &range_nodes);
if (ret) {
raw_res_spin_unlock_irqrestore(&arena->spinlock, flags);
goto out_free_pages;
@@ -862,6 +873,7 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt
bpf_free_pages(&pages);
out_memcg:
+ range_tree_nodes_release(&range_nodes);
bpf_map_memcg_exit(old_memcg, new_memcg);
return addr;
@@ -939,6 +951,7 @@ static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt)
u64 full_uaddr = clear_lo32(arena->user_vm_start) + uaddr;
long kaddr = bpf_arena_get_kern_vm_start(arena) + uaddr;
long pgoff = compute_pgoff(arena, uaddr);
+ struct range_tree_nodes range_nodes = {};
struct clear_range_data cdata;
struct llist_node *pos, *t;
LLIST_HEAD(free_pages);
@@ -954,11 +967,12 @@ static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt)
return ret;
/*
- * Take the free pages of the range too. It allocates, and can fail,
- * only when the whole range is free. Then there is nothing to do.
+ * Take the free pages of the range too. Without a spare node this
+ * fails only when the whole range is free. Then there is nothing to do.
*/
- if (range_tree_clear(&arena->rt, pgoff, page_cnt)) {
+ if (range_tree_clear(&arena->rt, pgoff, page_cnt, &range_nodes)) {
raw_res_spin_unlock_irqrestore(&arena->spinlock, flags);
+ range_tree_nodes_release(&range_nodes);
return 0;
}
@@ -970,6 +984,7 @@ static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt)
/* drop the lock to do the tlb flush and zap pages */
raw_res_spin_unlock_irqrestore(&arena->spinlock, flags);
+ range_tree_nodes_release(&range_nodes);
/* wait for arena_vm_fault() that found the page before its pte was cleared */
llist_for_each_entry(page, free_pages.first, pcp_llist) {
@@ -996,11 +1011,18 @@ static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt)
__free_page(page);
}
+ /*
+ * The set needs a node when neither neighbour is free. Allocate it now
+ * that the pages are back. If that fails, the range stays allocated.
+ */
+ range_tree_nodes_prepare(&range_nodes);
+
/* The callers hold no other rqspinlock, so this one can only time out */
while (raw_res_spin_lock_irqsave(&arena->spinlock, flags))
cond_resched();
- range_tree_set(&arena->rt, pgoff, page_cnt);
+ range_tree_set(&arena->rt, pgoff, page_cnt, &range_nodes);
raw_res_spin_unlock_irqrestore(&arena->spinlock, flags);
+ range_tree_nodes_release(&range_nodes);
return 0;
}
@@ -1057,6 +1079,7 @@ static int arena_reserve_pages(struct bpf_arena *arena, long uaddr, u32 page_cnt
{
long page_cnt_max = (arena->user_vm_end - arena->user_vm_start) >> PAGE_SHIFT;
struct mem_cgroup *new_memcg, *old_memcg;
+ struct range_tree_nodes range_nodes = {};
unsigned long flags;
long pgoff;
int ret;
@@ -1068,8 +1091,16 @@ static int arena_reserve_pages(struct bpf_arena *arena, long uaddr, u32 page_cnt
if (pgoff + page_cnt > page_cnt_max)
return -EINVAL;
- if (raw_res_spin_lock_irqsave(&arena->spinlock, flags))
- return -EBUSY;
+ bpf_map_memcg_enter(&arena->map, &old_memcg, &new_memcg);
+ if (range_tree_nodes_prepare(&range_nodes)) {
+ ret = -ENOMEM;
+ goto out_memcg;
+ }
+
+ if (raw_res_spin_lock_irqsave(&arena->spinlock, flags)) {
+ ret = -EBUSY;
+ goto out_free_node;
+ }
/* Cannot guard already allocated pages. */
ret = is_range_tree_set(&arena->rt, pgoff, page_cnt);
@@ -1079,11 +1110,13 @@ static int arena_reserve_pages(struct bpf_arena *arena, long uaddr, u32 page_cnt
}
/* "Allocate" the region to prevent it from being allocated. */
- bpf_map_memcg_enter(&arena->map, &old_memcg, &new_memcg);
- ret = range_tree_clear(&arena->rt, pgoff, page_cnt);
- bpf_map_memcg_exit(old_memcg, new_memcg);
+ ret = range_tree_clear(&arena->rt, pgoff, page_cnt, &range_nodes);
out:
raw_res_spin_unlock_irqrestore(&arena->spinlock, flags);
+out_free_node:
+ range_tree_nodes_release(&range_nodes);
+out_memcg:
+ bpf_map_memcg_exit(old_memcg, new_memcg);
return ret;
}
diff --git a/kernel/bpf/range_tree.c b/kernel/bpf/range_tree.c
index 2460e78d6a5..db8cf5dce94 100644
--- a/kernel/bpf/range_tree.c
+++ b/kernel/bpf/range_tree.c
@@ -20,27 +20,61 @@
* in commit 6772fcc8890a ("xfs: convert xbitmap to interval tree").
*
* The implementation relies on external lock to protect rbtree-s.
- * The alloc/free of range_node-s is done via kmalloc_nolock().
+ * Callers allocate a spare range_node before taking that lock. Removed nodes
+ * are queued and freed after dropping the lock.
*
* bpf arena is using range_tree to represent unallocated slots.
* At init time:
- * range_tree_set(rt, 0, max);
+ * range_tree_set(rt, 0, max, nodes);
* Then:
* start = range_tree_find(rt, len);
* if (start >= 0)
- * range_tree_clear(rt, start, len);
+ * range_tree_clear(rt, start, len, nodes);
* to find free range and mark slots as allocated and later:
- * range_tree_set(rt, start, len);
+ * range_tree_set(rt, start, len, nodes);
* to mark as unallocated after use.
*/
struct range_node {
struct rb_node rn_rbnode;
- struct rb_node rb_range_size;
+ union {
+ struct rb_node rb_range_size;
+ /* Out of both trees, waiting to be freed */
+ struct range_node *next;
+ };
u32 rn_start;
u32 rn_last; /* inclusive */
u32 __rn_subtree_last;
};
+/* Make sure @nodes holds a spare node. Call it without the tree lock held. */
+int range_tree_nodes_prepare(struct range_tree_nodes *nodes)
+{
+ if (!nodes->spare)
+ nodes->spare = kmalloc_nolock(sizeof(struct range_node),
+ __GFP_ACCOUNT, NUMA_NO_NODE);
+ return nodes->spare ? 0 : -ENOMEM;
+}
+
+void range_tree_nodes_release(struct range_tree_nodes *nodes)
+{
+ struct range_node *node, *next;
+
+ kfree_nolock(nodes->spare);
+ for (node = nodes->to_free; node; node = next) {
+ next = node->next;
+ kfree_nolock(node);
+ }
+ nodes->spare = NULL;
+ nodes->to_free = NULL;
+}
+
+static void range_tree_node_defer_free(struct range_tree_nodes *nodes,
+ struct range_node *node)
+{
+ node->next = nodes->to_free;
+ nodes->to_free = node;
+}
+
static struct range_node *rb_to_range_node(struct rb_node *rb)
{
return rb_entry(rb, struct range_node, rb_range_size);
@@ -133,7 +167,8 @@ range_it_iter_first(struct range_tree *rt, u32 start, u32 last)
}
/* Clear the range in this range tree */
-int range_tree_clear(struct range_tree *rt, u32 start, u32 len)
+int range_tree_clear(struct range_tree *rt, u32 start, u32 len,
+ struct range_tree_nodes *nodes)
{
u32 last = start + len - 1;
struct range_node *new_rn;
@@ -144,14 +179,13 @@ int range_tree_clear(struct range_tree *rt, u32 start, u32 len)
u32 old_last = rn->rn_last;
/*
- * Pre-allocate the right-half node before modifying
- * the tree. If allocation fails we return -ENOMEM
- * without altering the range tree.
+ * Use the node allocated before the caller took the lock.
+ * Without it, leave the tree unchanged.
*/
- new_rn = kmalloc_nolock(sizeof(struct range_node),
- __GFP_ACCOUNT, NUMA_NO_NODE);
- if (!new_rn)
+ if (!nodes->spare)
return -ENOMEM;
+ new_rn = nodes->spare;
+ nodes->spare = NULL;
/* Overlaps with the entire clearing range */
range_it_remove(rn, rt);
@@ -176,7 +210,7 @@ int range_tree_clear(struct range_tree *rt, u32 start, u32 len)
} else {
/* in the middle of the clearing range */
range_it_remove(rn, rt);
- kfree_nolock(rn);
+ range_tree_node_defer_free(nodes, rn);
}
}
return 0;
@@ -196,7 +230,8 @@ int is_range_tree_set(struct range_tree *rt, u32 start, u32 len)
}
/* Set the range in this range tree */
-int range_tree_set(struct range_tree *rt, u32 start, u32 len)
+int range_tree_set(struct range_tree *rt, u32 start, u32 len,
+ struct range_tree_nodes *nodes)
{
u32 last = start + len - 1;
struct range_node *new_rn = NULL;
@@ -212,16 +247,16 @@ int range_tree_set(struct range_tree *rt, u32 start, u32 len)
left = range_it_iter_first(rt, start - 1, start - 1);
right = range_it_iter_first(rt, last + 1, last + 1);
if (!left && !right) {
- new_rn = kmalloc_nolock(sizeof(struct range_node),
- __GFP_ACCOUNT, NUMA_NO_NODE);
- if (!new_rn)
+ if (!nodes->spare)
return -ENOMEM;
+ new_rn = nodes->spare;
+ nodes->spare = NULL;
}
/* Clear out everything in the range we want to set. */
- err = range_tree_clear(rt, start, len);
+ err = range_tree_clear(rt, start, len, nodes);
if (err)
- goto out_free_new;
+ goto out_restore_spare;
if (left && right) {
/* Combine left and right adjacent ranges */
@@ -229,7 +264,7 @@ int range_tree_set(struct range_tree *rt, u32 start, u32 len)
range_it_remove(right, rt);
left->rn_last = right->rn_last;
range_it_insert(left, rt);
- kfree_nolock(right);
+ range_tree_node_defer_free(nodes, right);
} else if (left) {
/* Combine with the left range */
range_it_remove(left, rt);
@@ -248,8 +283,9 @@ int range_tree_set(struct range_tree *rt, u32 start, u32 len)
}
return 0;
-out_free_new:
- kfree_nolock(new_rn);
+out_restore_spare:
+ if (new_rn)
+ nodes->spare = new_rn;
return err;
}
diff --git a/kernel/bpf/range_tree.h b/kernel/bpf/range_tree.h
index ff0b9110eb7..e028c37c557 100644
--- a/kernel/bpf/range_tree.h
+++ b/kernel/bpf/range_tree.h
@@ -3,6 +3,13 @@
#ifndef _RANGE_TREE_H
#define _RANGE_TREE_H 1
+struct range_node;
+
+struct range_tree_nodes {
+ struct range_node *spare;
+ struct range_node *to_free;
+};
+
struct range_tree {
/* root of interval tree */
struct rb_root_cached it_root;
@@ -13,8 +20,12 @@ struct range_tree {
void range_tree_init(struct range_tree *rt);
void range_tree_destroy(struct range_tree *rt);
-int range_tree_clear(struct range_tree *rt, u32 start, u32 len);
-int range_tree_set(struct range_tree *rt, u32 start, u32 len);
+int range_tree_nodes_prepare(struct range_tree_nodes *nodes);
+void range_tree_nodes_release(struct range_tree_nodes *nodes);
+int range_tree_clear(struct range_tree *rt, u32 start, u32 len,
+ struct range_tree_nodes *nodes);
+int range_tree_set(struct range_tree *rt, u32 start, u32 len,
+ struct range_tree_nodes *nodes);
int is_range_tree_set(struct range_tree *rt, u32 start, u32 len);
s64 range_tree_find(struct range_tree *rt, u32 len);
--
2.39.5 (Apple Git-154)
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [RFC PATCH v3 2/2] mm: restrict can_spin_trylock() to preemptible context on PREEMPT_RT
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 2:35 ` Karl Mehltretter
2026-12-02 4:11 ` [RFC PATCH v3 0/2] bpf, mm: Avoid allocator deadlocks " Alexei Starovoitov
2 siblings, 0 replies; 6+ 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
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)
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [RFC PATCH v3 1/2] bpf: Move arena allocations and frees outside arena->spinlock
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
0 siblings, 0 replies; 6+ messages in thread
From: bot+bpf-ci @ 2026-12-02 3:19 UTC (permalink / raw)
To: kmehltretter, bigeasy, ast, vbabka
Cc: kmehltretter, harry, akpm, peterz, tglx, daniel, andrii,
martin.lau, eddyz87, memxor, song, yonghong.song, jolsa, emil,
ihor.solodrai, ameryhung, david, ljs, liam, rppt, surenb, mhocko,
swarajgaikwad1925, clrkwllms, rostedt, bpf, linux-mm,
linux-rt-devel, linux-kernel, ast, andrii, daniel, martin.lau,
eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 4743 bytes --]
> bpf: Move arena allocations and frees outside arena->spinlock
TL;DR:
- arena_alloc_pages() and arena_reserve_pages() now preallocate a spare node,
so they may return -ENOMEM or 0 on allocation failure where they previously
succeeded, including on PREEMPT_RT.
- Possible extra alloc/free per arena allocation.
> diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
> --- a/kernel/bpf/arena.c
> +++ b/kernel/bpf/arena.c
[ ... ]
> @@ -886,6 +895,8 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt
> return 0;
> }
> bpf_map_memcg_enter(&arena->map, &old_memcg, &new_memcg);
> + if (range_tree_nodes_prepare(&range_nodes))
> + goto out_memcg;
>
> ret = bpf_alloc_pages(node_id, page_cnt, &pages, sleepable);
> if (ret)
When uaddr == 0, arena_adjust_tree() takes the range_tree_find() path and
range_tree_clear() starts at the found node's rn_start, so the node is
only trimmed or removed and the spare is never used:
ret = range_tree_find(&arena->rt, page_cnt);
...
*pgoff = ret;
return range_tree_clear(&arena->rt, *pgoff, page_cnt, range_nodes);
Before this patch that path allocated no range_node. Now
arena_alloc_pages() fails and returns 0 if kmalloc_nolock() fails here.
Can this regress a sleepable bpf_arena_alloc_pages(&arena, NULL, 1,
NUMA_NO_NODE, 0) call? If the map's memcg is at memory.max and the
per-CPU obj stock has no bytes for this objcg, the kmalloc_nolock() charge
goes through __memcg_slab_post_alloc_hook() -> __obj_cgroup_charge() ->
try_charge_memcg(), where !gfpflags_allow_blocking() goes to nomem and the
allocation returns NULL.
Previously the same call reclaimed through bpf_alloc_page() with
GFP_KERNEL | __GFP_ACCOUNT | __GFP_RETRY_MAYFAIL and then cleared the
range without any kmalloc. kmalloc_nolock() can also return NULL on
trylock contention.
This also adds a kmalloc_nolock()/kfree_nolock() pair to every arena
allocation, including the arena stack allocation done by
bpf_arena_stack_get(). Would it work to prepare the node only when
uaddr != 0, since only the fixed-address path can split an interval?
[ ... ]
> @@ -1262,8 +1285,16 @@ static int arena_reserve_pages(struct bpf_arena *arena, long uaddr, u32 page_cnt
> if (pgoff + page_cnt > page_cnt_max)
> return -EINVAL;
>
> - if (raw_res_spin_lock_irqsave(&arena->spinlock, flags))
> - return -EBUSY;
> + bpf_map_memcg_enter(&arena->map, &old_memcg, &new_memcg);
> + if (range_tree_nodes_prepare(&range_nodes)) {
> + ret = -ENOMEM;
> + goto out_memcg;
> + }
> +
> + if (raw_res_spin_lock_irqsave(&arena->spinlock, flags)) {
> + ret = -EBUSY;
> + goto out_free_node;
> + }
>
> /* Cannot guard already allocated pages. */
> ret = is_range_tree_set(&arena->rt, pgoff, page_cnt);
Does this make arena_reserve_pages() fail with -ENOMEM for reservations
that need no spare node?
range_tree_clear() only consumes the spare when it splits an interval
(rn->rn_start < start && rn->rn_last > last). A reservation at either
edge of a free interval only trims it. For example, reserving the first N
pages of a fresh arena, whose tree holds [0, max-1], takes the right side
branch. Before this patch such a reservation allocated nothing and could
not fail for lack of memory.
kmalloc_nolock() returns NULL without trying when can_spin_trylock() is
false. With the next patch in the series, e1c6badba891 ("mm: restrict
can_spin_trylock() to preemptible context on PREEMPT_RT"), that covers
every non-preemptible context on PREEMPT_RT. bpf_arena_reserve_pages() is
registered KF_SPINLOCK_SAFE, so a program may call it under bpf_spin_lock
(IRQs off), and it can also be called from kprobe or tracepoint programs.
On PREEMPT_RT in those contexts every reservation would then return
-ENOMEM, including the edge-aligned ones, where without this patch only
splitting reservations would fail. On other configurations the same check
adds a failure when the nolock trylock or the non-reclaiming __GFP_ACCOUNT
charge fails.
arena_free_range() in the same patch handles this differently: it calls
range_tree_nodes_prepare() and ignores the result, so range_tree_set()
fails only when it really needs the node. Would doing the same here,
dropping the check and letting range_tree_clear() report -ENOMEM, keep the
old behaviour for non-splitting reservations?
The same pattern in arena_alloc_pages() (the issue above) also puts a
non-reclaiming kmalloc_nolock() ahead of the reclaiming bpf_alloc_page()
for sleepable callers.
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/46798506165
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [RFC PATCH v3 0/2] bpf, mm: Avoid allocator deadlocks on PREEMPT_RT
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 2:35 ` [RFC PATCH v3 2/2] mm: restrict can_spin_trylock() to preemptible context on PREEMPT_RT Karl Mehltretter
@ 2026-12-02 4:11 ` Alexei Starovoitov
2026-12-02 6:09 ` Karl Mehltretter
2 siblings, 1 reply; 6+ messages in thread
From: Alexei Starovoitov @ 2026-12-02 4:11 UTC (permalink / raw)
To: Karl Mehltretter, Sebastian Andrzej Siewior, Vlastimil Babka
Cc: 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 Tue, Dec 01, 2026 at 06:35 PM Karl Mehltretter <kmehltretter@gmail.com> wrote:
> locks. This is deliberately a conservative RT-only guard rather than an
> attempt to classify tracepoint contexts; non-RT is unchanged.
It's not. Patch 1 adds new failures on !RT.
> - Is !preemptible() in can_spin_trylock() on RT an acceptable tradeoff?
No.
Pls do not send stuff straight out of LLM that you don't understand.
pw-bot: cr
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [RFC PATCH v3 0/2] bpf, mm: Avoid allocator deadlocks on PREEMPT_RT
2026-12-02 4:11 ` [RFC PATCH v3 0/2] bpf, mm: Avoid allocator deadlocks " Alexei Starovoitov
@ 2026-12-02 6:09 ` Karl Mehltretter
0 siblings, 0 replies; 6+ messages in thread
From: Karl Mehltretter @ 2026-12-02 6:09 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: Sebastian Andrzej Siewior, Vlastimil Babka, 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 Wed, Dec 02, 2026 at 04:11:47AM +0100, Alexei Starovoitov wrote:
> On Tue, Dec 01, 2026 at 06:35 PM Karl Mehltretter <kmehltretter@gmail.com> wrote:
> > locks. This is deliberately a conservative RT-only guard rather than an
> > attempt to classify tracepoint contexts; non-RT is unchanged.
>
> It's not. Patch 1 adds new failures on !RT.
>
> > - Is !preemptible() in can_spin_trylock() on RT an acceptable tradeoff?
>
> No.
> Pls do not send stuff straight out of LLM that you don't understand.
>
Sorry I tried to revisit this after the stalled v2 discussion and got it
wrong.
Thanks for the review.
Karl
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-02 6:09 UTC | newest]
Thread overview: 6+ 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
2026-12-02 4:11 ` [RFC PATCH v3 0/2] bpf, mm: Avoid allocator deadlocks " Alexei Starovoitov
2026-12-02 6:09 ` Karl Mehltretter
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.