Re: [RFC PATCH v3 1/2] bpf: Move arena allocations and frees outside arena->spinlock

From: bot+bpf-ci

Date: Tue Dec 01 2026 - 22:20:31 EST


> 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