mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: bot+bpf-ci@kernel.org
To: kmehltretter@gmail.com,bigeasy@linutronix.de,ast@kernel.org,vbabka@kernel.org
Cc: kmehltretter@gmail.com,harry@kernel.org,akpm@linux-foundation.org,peterz@infradead.org,tglx@linutronix.de,daniel@iogearbox.net,andrii@kernel.org,martin.lau@linux.dev,eddyz87@gmail.com,memxor@gmail.com,song@kernel.org,yonghong.song@linux.dev,jolsa@kernel.org,emil@etsalapatis.com,ihor.solodrai@linux.dev,ameryhung@gmail.com,david@kernel.org,ljs@kernel.org,liam@infradead.org,rppt@kernel.org,surenb@google.com,mhocko@suse.com,swarajgaikwad1925@gmail.com,clrkwllms@kernel.org,rostedt@goodmis.org,bpf@vger.kernel.org,linux-mm@kvack.org,linux-rt-devel@lists.linux.dev,linux-kernel@vger.kernel.org,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,mason@kernel.org,ihor.solodrai@linux.dev
Subject: Re: [RFC PATCH v3 1/2] bpf: Move arena allocations and frees outside arena->spinlock
Date: Wed,  2 Dec 2026 03:19:43 +0000 (UTC)	[thread overview]
Message-ID: <512e79b4496e5fce49ead171f27601bf869950e7ae54704276e47ff50a2963e2@mail.kernel.org> (raw)
In-Reply-To: <20261202023506.35176-2-kmehltretter@gmail.com>

[-- 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

  reply	other threads:[~2026-12-02  3:19 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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=512e79b4496e5fce49ead171f27601bf869950e7ae54704276e47ff50a2963e2@mail.kernel.org \
    --to=bot+bpf-ci@kernel.org \
    --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=kmehltretter@gmail.com \
    --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@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mason@kernel.org \
    --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®