From: Emil Tsalapatis <emil@etsalapatis.com>
To: bpf@vger.kernel.org
Cc: ast@kernel.org, andrii@kernel.org, eddyz87@gmail.com,
memxor@gmail.com, daniel@iogearbox.net,
Emil Tsalapatis <emil@etsalapatis.com>
Subject: [PATCH bpf-next v2 5/7] selftests/bpf: libarena: Refill local caches in batches
Date: Wed, 21 Oct 2026 23:26:39 +0000 [thread overview]
Message-ID: <20261021232641.10063-6-emil@etsalapatis.com> (raw)
In-Reply-To: <20261021232641.10063-1-emil@etsalapatis.com>
Cache hits avoid the central lock, but every cache miss still asks the
buddy allocator for one object. A sequence of misses therefore repeats
the same locking and metadata work.
Allocate eight objects with buddy_alloc_bulk() on a local-cache miss.
Return one object directly and retain the other seven in the per-CPU LIFO
array. This amortizes buddy locking and metadata updates over subsequent
allocations. Skip the refill in NMI context, where the backend allocator
cannot safely take its lock.
Express the array capacity as twice the batch size, so that a refilled
array can still absorb a full batch of frees before it overflows.
Signed-off-by: Emil Tsalapatis <emil@etsalapatis.com>
---
.../bpf/libarena/include/libarena/buddy.h | 6 +++-
.../selftests/bpf/libarena/src/buddy.bpf.c | 28 +++++++++++++++++--
2 files changed, 30 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h b/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h
index de8d29ea4673..0c984952042a 100644
--- a/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h
+++ b/tools/testing/selftests/bpf/libarena/include/libarena/buddy.h
@@ -81,10 +81,14 @@ enum buddy_cache_consts {
*/
BUDDY_CACHE_MAX_CPUS = 1024,
+ /* Objects moved to and from the buddy allocator at once, as an order. */
+ BUDDY_CACHE_BATCH_ORDER = 3,
+ BUDDY_CACHE_BATCH_SIZE = 1 << BUDDY_CACHE_BATCH_ORDER,
+
/*
* Objects retained locally for each allocation order.
*/
- BUDDY_CACHE_CAPACITY = 16,
+ BUDDY_CACHE_CAPACITY = BUDDY_CACHE_BATCH_SIZE * 2,
};
struct buddy_cache_order {
diff --git a/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c b/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
index e19104ad7422..d7c3defeb0a8 100644
--- a/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
+++ b/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
@@ -126,10 +126,28 @@ static __always_inline int buddy_cache_push(struct buddy_cache __arena *cache,
return 0;
}
-void __arena *buddy_cache_alloc(struct buddy __arena *buddy, size_t order)
+/*
+ * Refill an empty cache with one trip to the buddy allocator. Kept out of line
+ * so that cache hits do not pay for the refill path.
+ */
+static __noinline void __arena *
+buddy_cache_refill(struct buddy __arena *buddy, struct buddy_cache __arena *cache,
+ size_t size, int order)
+{
+ struct buddy_cache_order __arena *slots = &cache->orders[order];
+
+ if (buddy_alloc_bulk(buddy, size, BUDDY_CACHE_BATCH_ORDER,
+ (void __arena * __arena *)slots->objects))
+ return NULL;
+
+ WRITE_ONCE(slots->elems, BUDDY_CACHE_BATCH_SIZE - 1);
+ return slots->objects[BUDDY_CACHE_BATCH_SIZE - 1];
+}
+
+void __arena *buddy_cache_alloc(struct buddy __arena *buddy, size_t size, size_t order)
{
struct buddy_cache __arena *cache;
- void __arena *address = NULL;
+ void __arena *address;
if (!buddy_order_cached(order))
return NULL;
@@ -138,7 +156,11 @@ void __arena *buddy_cache_alloc(struct buddy __arena *buddy, size_t order)
if (!cache)
return NULL;
+ /* NMIs cannot take the buddy lock, so they only use what is already cached. */
address = buddy_cache_pop(cache, order);
+ if (!address && !bpf_in_nmi())
+ address = buddy_cache_refill(buddy, cache, size, order);
+
buddy_cache_exit(cache);
return address;
@@ -1055,7 +1077,7 @@ void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size)
return NULL;
order = size_to_order(size);
- address = buddy_cache_alloc(buddy, order);
+ address = buddy_cache_alloc(buddy, size, order);
if (address)
return address;
--
2.52.0
next prev parent reply other threads:[~2026-10-21 23:27 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
2026-10-21 23:26 ` [PATCH bpf-next v2 4/7] selftests/bpf: libarena: Handle NMI reentrancy Emil Tsalapatis
2026-10-21 23:26 ` Emil Tsalapatis [this message]
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=20261021232641.10063-6-emil@etsalapatis.com \
--to=emil@etsalapatis.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=memxor@gmail.com \
/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 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.