BPF List
 help / color / mirror / Atom feed
From: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>
To: "Emil Tsalapatis" <emil@etsalapatis.com>, "Harry Yoo" <harry@kernel.org>
Cc: <bpf@vger.kernel.org>, <ast@kernel.org>, <andrii@kernel.org>,
	<eddyz87@gmail.com>, <daniel@iogearbox.net>
Subject: Re: [PATCH bpf-next v2 3/7] selftests/bpf: libarena: Add buddy per-CPU caches
Date: Fri, 23 Oct 2026 01:52:59 +0200	[thread overview]
Message-ID: <DMBRS988UZQ5.OJ7ZMBFZHJDN@gmail.com> (raw)
In-Reply-To: <DMBRIUEJD3IW.3AXK142IA7TBH@etsalapatis.com>

On Fri Oct 23, 2026 at 1:40 AM CEST, Emil Tsalapatis wrote:
> On Thu Oct 22, 2026 at 12:53 PM UTC, Harry Yoo wrote:
>> On Wed, Oct 21, 2026 at 11:26:37PM +0000, Emil Tsalapatis wrote:
>>> Every small allocation currently reaches the central buddy allocator and
>>> takes its lock. This serializes otherwise independent callers and touches
>>> the buddy metadata on every allocation and free.
>>>
>>> Add a fixed-size LIFO array for each cached order and CPU. Keep the
>>> per-CPU cache pointers in struct buddy and allocate each CPU's packed
>>> cache from the buddy during initialization. Cache the eight smallest
>>> allocation orders, up to 2 KiB. Each array holds 16 objects, bounding each
>>> CPU's cache to about 1.5 KiB.
>>
>> TIL the min alloc size is not PAGE_SIZE in BPF buddy allocator :)
>>
>>> Allocations pop from the local array and frees push onto it without taking
>>> the buddy lock. Empty and full arrays fall through to the central buddy
>>> allocator. Packed arrays keep the fast path to a bounds check and a single
>>> slot access.
>>>
>>> BPF programs only disable migration, so another task can preempt a cache
>>> operation and run on the same CPU, and an IRQ or NMI program can interrupt
>>> one. Claim the CPU with a cmpxchg on a per-CPU flag before touching its
>>> arrays, following the model used by the main BPF memory allocator. An
>>> operation that finds the CPU already claimed falls through to the central
>>> buddy allocator.
>>>
>>> Signed-off-by: Emil Tsalapatis <emil@etsalapatis.com>
>>> ---
> [...]
>
> We don't really have cmpxchg_local() available in BPF. I hacked up a prototype
> and the benefits were actually not that significant, about 2ns for hot alloc
> free loops.
>
>>
>> On x86, cmpxchg instruction should be cheaper without LOCK prefix.
>>
>>> +
>>> +	return cache;
>>> +}
>>> +
>>> +static __always_inline void buddy_cache_exit(struct buddy_cache __arena *cache)
>>> +{
>>> +	smp_store_release(&cache->active, 0);
>>> +}
>>
>> We don't need this barrier because this is percpu?
>
>
> ... this, however did matter significantly for hot loop frees. I will add it
> to v3 and respin.

What architecture are you measuring it on? Because on x86 smp_store_release() is
equivalent to a WRITE_ONCE(), so unless this was suppressing compiler
optimizations and the code changed by doing a simple store, I don't follow how
it would help.

  reply	other threads:[~2026-10-22 23:53 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-21 23:26 [PATCH bpf-next v2 0/7] bpf libarena allocator improvements Emil Tsalapatis
2026-10-21 23:26 ` [PATCH bpf-next v2 1/7] selftests/bpf: libarena: Add bulk buddy allocation helpers Emil Tsalapatis
2026-10-21 23:26 ` [PATCH bpf-next v2 2/7] selftests/bpf: Add a parallel buddy allocator benchmark Emil Tsalapatis
2026-10-21 23:26 ` [PATCH bpf-next v2 3/7] selftests/bpf: libarena: Add buddy per-CPU caches Emil Tsalapatis
2026-10-22 12:53   ` Harry Yoo
2026-10-22 23:40     ` Emil Tsalapatis
2026-10-22 23:52       ` Kumar Kartikeya Dwivedi [this message]
2026-10-21 23:26 ` [PATCH bpf-next v2 4/7] selftests/bpf: libarena: Handle NMI reentrancy Emil Tsalapatis
2026-10-21 23:26 ` [PATCH bpf-next v2 5/7] selftests/bpf: libarena: Refill local caches in batches Emil Tsalapatis
2026-10-21 23:26 ` [PATCH bpf-next v2 6/7] selftests/bpf: libarena: Add a transfer cache Emil Tsalapatis
2026-10-21 23:26 ` [PATCH bpf-next v2 7/7] selftests/bpf: libarena: Cover buddy cache fast paths Emil Tsalapatis

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=DMBRS988UZQ5.OJ7ZMBFZHJDN@gmail.com \
    --to=memxor@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=harry@kernel.org \
    /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