Skip to contentSkip to conversation

bpf libarena allocator improvements

PATCH Superseded Emil Tsalapatis 3 participants 17 messages Oct 23 – Oct 28

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Emil Tsalapatis

author reviewer
#0 lore raw

v3 cover bpf libarena allocator improvements

The libarena buddy allocator's design has up until now prioritized ease of verification. With new verifier features like unlimited global stack frames and a large stack size we can now improve the allocator's performance and robustness.

This patch introduces per-CPU caches for the allocator. The overall caching design is inspired by tcmalloc. Each CPU has its own static object LIFOs, one for each allocator order. This speeds up allocation and freeing for BPF programs that have some degree of locality. The CPU caches are lockless. The caches are replenished from and spilled into "transfer caches", per-order caches shared between the CPUs that amortize the cost of spilling/refilling. Data from the transfer caches is in turn moved from and to the allocator.

The patchset additionally includes code for allocator reentrancy and bulk allocations. The reentrancy code lets us safely allocate from an NMI context in general, and from a BPF syscall program context locklessly. The bulk allocation API is useful for replenishing the allocator caches and also on its own right for speeding up allocations during initialization.

Benchmark results from patch 2, run with the options from run_bench_libarena.sh (-a, 64-byte allocations) in a 16-vCPU VM, as the mean of three runs. Rows are the number of consecutive allocations, and then frees, in each benchmark iteration. Columns are the number of producers. Each entry shows the latency in ns before and with this series, followed by the speedup: the latency before divided by the latency with this series, so higher is better.

libarena-malloc-free, where each worker frees its own objects:

Allocation latency (before/after ns, speedup):

allocs 1 prod 2 prod 4 prod 8 prod

     1    269/68 3.9x    641/68 9.4x  1568/68 22.9x  4550/69 66.4x
     4    251/63 4.0x    557/63 8.9x  2326/67 34.5x  5221/74 70.4x
    16    247/61 4.0x  1027/64 16.0x  2570/67 38.1x  5315/74 71.8x
    64    242/75 3.2x  1129/146 7.8x 2469/190 13.0x 5521/374 14.8x

256 242/102 2.4x 1314/184 7.1x 2664/249 10.7x 5542/457 12.1x

Free latency (before/after ns, speedup):

allocs 1 prod 2 prod 4 prod 8 prod

     1    243/55 4.4x    273/56 4.9x  1428/55 25.9x  4371/56 78.2x
     4    242/51 4.7x    414/52 8.0x  2303/52 44.5x 5266/51 103.1x
    16    241/50 4.8x  1056/50 20.9x  2580/50 51.6x 5330/51 105.4x
    64    240/64 3.8x  1079/86 12.5x 2454/131 18.8x 5538/309 17.9x

256 238/124 1.9x 1206/107 11.2x 2681/178 15.0x 5546/406 13.7x

libarena-malloc-xfree, where each worker frees the objects allocated by the previous invocation, so with more than one producer most frees happen on a different CPU from the allocation:

Allocation latency (before/after ns, speedup):

allocs 1 prod 2 prod 4 prod 8 prod

     1    291/65 4.5x   857/65 13.2x  1165/67 17.5x  4172/70 59.8x
     4    243/63 3.9x   754/64 11.7x  2170/66 32.6x  4931/79 62.2x
    16    231/61 3.8x  1163/63 18.5x  2572/71 36.2x  5438/93 58.7x
    64    229/75 3.1x  1249/158 7.9x 2824/213 13.3x 5760/411 14.0x

256 228/100 2.3x 1269/210 6.0x 2746/264 10.4x 5669/466 12.2x

Free latency (before/after ns, speedup):

allocs 1 prod 2 prod 4 prod 8 prod

     1    266/55 4.8x   558/56 10.0x  1052/56 18.8x  4050/56 72.3x
     4    231/51 4.5x   583/52 11.2x  2032/51 39.6x  4875/52 93.9x
    16    223/50 4.5x  1019/50 20.3x  2492/50 49.6x 5396/51 106.5x
    64    222/63 3.5x  1156/84 13.8x 2801/122 23.0x 5735/320 17.9x

256 221/124 1.8x 1202/113 10.7x 2736/160 17.1x 5656/387 14.6x

Cc Harry Yoo <[email protected]>
Cc Kumar Kartikeya Dwivedi <[email protected]>

v2 -> v3 (https://lore.kernel.org/bpf/[email protected]/)

- Release the per-CPU cache claim with barrier() and WRITE_ONCE() instead of smp_store_release(), since only contexts on the same CPU claim the cache (Harry).

v1 -> v2 (https://lore.kernel.org/bpf/[email protected]/)

- In the transfer cache, avoid taking two locks per free when it is full:
check the transfer array's count before taking its lock, and spill the local batch with buddy_free_bulk() when the array is full or busy (Alexei).
- Simplify the per-CPU caches: keep the guard in struct buddy_cache, claim the cache once per miss or full cache with the refill and spill paths out of line, keep the cache state in struct buddy, and move the definitions into buddy.h (Alexei).
- Make buddy_free_bulk() fail only when nothing was freed (Alexei).
- Time frees in the benchmark, add a cross-CPU variant, and add before and after numbers to the cover letter (Alexei).
- Addressed bot feedback.

Emil Tsalapatis (7):
selftests/bpf: libarena: Add bulk buddy allocation helpers selftests/bpf: Add a parallel buddy allocator benchmark selftests/bpf: libarena: Add buddy per-CPU caches selftests/bpf: libarena: Handle NMI reentrancy selftests/bpf: libarena: Refill local caches in batches selftests/bpf: libarena: Add a transfer cache selftests/bpf: libarena: Cover buddy cache fast paths

 tools/testing/selftests/bpf/bench.c           |   4 +
 .../selftests/bpf/benchs/bench_libarena.c     | 118 +++-
 .../bpf/benchs/run_bench_libarena.sh          |  16 +
 .../bpf/libarena/benchs/bench_malloc.bpf.c    | 110 +++
 .../bpf/libarena/include/libarena/buddy.h     |  56 ++
 .../bpf/libarena/include/libarena/common.h    |   4 +
 .../bpf/libarena/selftests/test_buddy.bpf.c   | 273 ++++++++
 .../selftests/bpf/libarena/src/buddy.bpf.c    | 635 +++++++++++++++++-
 8 files changed, 1171 insertions(+), 45 deletions(-)
Signature · 1 line
2.52.0
author reviewer
#1 lore raw

PATCH v3 1/7 selftests/bpf: libarena: Add bulk buddy allocation helpers

The buddy allocator currently allocates and frees one object at a time. Callers that need a group of equal-sized objects consequently take the allocator lock and repeat the same split operations for every object.

Add buddy_alloc_bulk(), which obtains one block and divides it into a power-of-two number of independently freeable objects. Add buddy_free_bulk() to free an arbitrary array of objects while holding the allocator lock once. Bulk frees cannot avoid coalescing each object independently, but they still amortize lock acquisition.

buddy_free_bulk() fails only when it cannot take the allocator lock, in which case nothing was freed. Objects that cannot be freed are reported individually, so a caller can tell from the return value alone whether the array still owns its objects.

The per-CPU caches added later use this interface to populate a cache with one trip through the central allocator.

---
 .../bpf/libarena/include/libarena/buddy.h     |   4 +
 .../selftests/bpf/libarena/src/buddy.bpf.c    | 142 ++++++++++++++----
 2 files changed, 118 insertions(+), 28 deletions(-)
tools/testing/selftests/bpf/libarena/include/libarena/buddy.h +4 -0
index 528c69a1f38e..f150e1c37e3d 100644
@@ -76,6 +76,10 @@ struct buddy {
 int buddy_init(struct buddy __arena *buddy);
 int buddy_destroy(struct buddy __arena *buddy);
 int buddy_free(struct buddy __arena *buddy, void __arena *free);
+int buddy_free_bulk(struct buddy __arena *buddy, void __arena * __arena *addrs,
+	size_t naddrs);
 void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size);
+int buddy_alloc_bulk(struct buddy __arena *buddy, size_t size, size_t norder,
+	void __arena * __arena *out);
 
 #endif /* __BPF__  */
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +114 -28

239 lines not on this page · raw message

Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH bpf-next v3 1/7] selftests/bpf: libarena: Add bulk buddy allocation helpers

TL;DR: Nit only: the comment and commit message say buddy_free_bulk() fails only when the lock can't be taken, but it also returns -EINVAL for NULL arguments. No functional problem.
Quoted 1 line

selftests/bpf: libarena: Add bulk buddy allocation helpers

TL;DR: Nit only: the comment and commit message say buddy_free_bulk() fails only when the lock can't be taken, but it also returns -EINVAL for NULL arguments. No functional problem.

Quoted 3 lines
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +0 -0

[ ... ]

Quoted 17 lines
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +13 -0
@@ -900,4 +950,40 @@ __weak int buddy_free(struct buddy __arena *buddy, void __arena *addr)
 	return ret;
 }
 
+/*
+ * Fails only when the buddy lock cannot be taken, in which case nothing was
+ * freed. Objects that buddy_free_unlocked() rejects are reported there.
+ */
+__weak int buddy_free_bulk(struct buddy __arena *buddy, void __arena * __arena *addrs, size_t naddrs)
+{
+	unsigned long flags;
+	unsigned int i;
+	int ret;
+
+	if (!buddy)
+		return -EINVAL;
+

tools/testing/selftests/bpf/libarena/src/buddy.bpf.c __weak int buddy_free(struct buddy __arena *buddy, void __arena *addr) · line 975

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +10 -0
+	if (!naddrs)
+		return 0;
+
+	if (!addrs)
+		return -EINVAL;
+
+	ret = buddy_lock(buddy, flags);
+	if (ret)
+		return ret;
+

[ ... ]

This isn't a bug, but is the comment above buddy_free_bulk() accurate?

It says the function "fails only when the buddy lock cannot be taken", but buddy_free_bulk() also returns -EINVAL for a NULL buddy and for a NULL addrs with a non-zero naddrs, before the lock is ever attempted.

Nothing is freed on any of those paths either, so the contract that a non-zero return means the array still owns its objects does hold. The later transfer cache patch relies on this: buddy_cache_spill() in f23ed100d6eb ("selftests/bpf: libarena: Add a transfer cache") treats a non-zero return that way.

Could the comment say something like "Fails only when the arguments are invalid or the buddy lock cannot be taken; in either case nothing was freed"?

The commit message has the same sentence:

Quoted 2 lines

buddy_free_bulk() fails only when it cannot take the allocator lock, in which case nothing was freed.

and could be adjusted the same way.

---
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/39878206535

author reviewer
#3 lore raw

PATCH v3 2/7 selftests/bpf: Add a parallel buddy allocator benchmark

In reply to Emil Tsalapatis

Add a benchmark for measuring buddy allocator latency under concurrent load. Each worker performs a configurable number of allocations followed by the corresponding frees, and the summary reports the mean latency of allocations and frees separately. The frees chain the objects through their first word, so the allocation size must be large enough to hold a pointer.

Add a cross-CPU variant in which each worker publishes its list in a shared slot, takes the list left there by the previous invocation and frees it. With more than one pinned worker, most objects are then freed on a different CPU from the one that allocated them.

Add both benchmarks to the libarena runner alongside the existing single-threaded cases, sweeping the producer count from 1 to 8 with a fixed allocation size and count to expose lock contention.

Report the existing libarena-malloc and libarena-calloc results in ns/allocation and summarize them by total allocation count, so that all libarena benchmarks share one output format. This replaces the previous ns/op unit and the invocation and allocations-per-invocation fields.

---
 tools/testing/selftests/bpf/bench.c           |   4 +
 .../selftests/bpf/benchs/bench_libarena.c     | 118 ++++++++++++++++--
 .../bpf/benchs/run_bench_libarena.sh          |  16 +++
 .../bpf/libarena/benchs/bench_malloc.bpf.c    | 110 ++++++++++++++++
 4 files changed, 237 insertions(+), 11 deletions(-)
tools/testing/selftests/bpf/bench.c +4 -0
index d5f494184f9d..fbbd55399019 100644
@@ -589,6 +589,8 @@ extern const struct bench bench_bpf_nop;
 extern const struct bench bench_xdp_lb;
 extern const struct bench bench_libarena_malloc;
 extern const struct bench bench_libarena_calloc;
+extern const struct bench bench_libarena_malloc_free;
+extern const struct bench bench_libarena_malloc_xfree;
 
 static const struct bench *benchs[] = {
 	&bench_count_global,
@@ -675,6 +677,8 @@ static const struct bench *benchs[] = {
 	&bench_xdp_lb,
 	&bench_libarena_malloc,
 	&bench_libarena_calloc,
+	&bench_libarena_malloc_free,
+	&bench_libarena_malloc_xfree,
 };
 
 static void find_benchmark(void)
tools/testing/selftests/bpf/benchs/bench_libarena.c +107 -11

201 lines not on this page · raw message

tools/testing/selftests/bpf/benchs/run_bench_libarena.sh +16 -0
index 10afe4d52ebf..2dfa32cd95cc 100755
@@ -29,3 +29,19 @@ printf "\t-------------------\n"
 			"$($RUN_BENCH --alloc_size "$size" --nallocs "$nallocs" libarena-calloc)"
 	done
 done
+
+header "libarena parallel malloc/free\n"
+
+for producers in 1 2 4 8; do
+	summarize_libarena "$producers producers:" \
+		"$($RUN_BENCH -a -p "$producers" --alloc_size 64 --nallocs 1000 \
+		libarena-malloc-free)"
+done
+
+header "libarena parallel cross-CPU malloc/free\n"
+
+for producers in 1 2 4 8; do
+	summarize_libarena "$producers producers:" \
+		"$($RUN_BENCH -a -p "$producers" --alloc_size 64 --nallocs 1000 \
+		libarena-malloc-xfree)"
+done
tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c +110 -0

119 lines not on this page · raw message

Signature · 1 line
2.52.0
author reviewer
#4 lore raw

PATCH v3 3/7 selftests/bpf: libarena: Add buddy per-CPU caches

In reply to Emil Tsalapatis

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.

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.

---
 .../bpf/libarena/include/libarena/buddy.h     |  34 +++
 .../bpf/libarena/include/libarena/common.h    |   4 +
 .../bpf/libarena/selftests/test_buddy.bpf.c   |   9 +
 .../selftests/bpf/libarena/src/buddy.bpf.c    | 220 +++++++++++++++++-
 4 files changed, 262 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/libarena/include/libarena/buddy.h +34 -0
index f150e1c37e3d..66349915745b 100644
@@ -65,10 +65,44 @@ struct buddy_chunk {
 	struct buddy_chunk __arena	*next;
 };
 
+/* Constants related to the buddy allocator's object caches. */
+enum buddy_cache_consts {
+	/*
+	 * Do not cache larger allocations to avoid excessive
+	 * memory consumption from the per-cpu lists.
+	 */
+	BUDDY_CACHE_NUM_ORDERS = 1 << 3,
+
+	/*
+	 * Just like arena spinlocks, hardcode percpu state to a maximum number of CPUs.
+	 * Since arena spinlocks are a hard dependency, use the exact same limit.
+	 */
+	BUDDY_CACHE_MAX_CPUS = 1024,
+
+	/*
+	 * Objects retained locally for each allocation order.
+	 */
+	BUDDY_CACHE_CAPACITY = 16,
+};
+
+struct buddy_cache_order {
+	u32 elems;
+	u32 __pad;
+	void __arena *objects[BUDDY_CACHE_CAPACITY];
+} __attribute__((aligned(64)));
+
+/* Dynamically allocated packed per-CPU slot slab. */
+struct buddy_cache {
+	/* Claimed by an in-flight cache operation on this CPU. */
+	unsigned int active;
+	struct buddy_cache_order orders[BUDDY_CACHE_NUM_ORDERS];
+};
+
 struct buddy {
 	struct buddy_chunk __arena *first_chunk;		/* Pointer to the chunk linked list. */
 	arena_spinlock_t lock;			/* Allocator lock */
 	u64 vaddr;				/* Allocation into reserved vaddr */
+	struct buddy_cache __arena *percpu[BUDDY_CACHE_MAX_CPUS];
 };
 
 #ifdef __BPF__
tools/testing/selftests/bpf/libarena/include/libarena/common.h +4 -0
index a6220355a697..a20947911d34 100644
@@ -22,6 +22,10 @@
 #define __maybe_unused __attribute__((__unused__))
 #endif
 
+#ifndef min
+#define min(a, b) ((a) < (b) ? (a) : (b))
+#endif
+
 #define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8)))
 
 #define ARENA_PAGES (1UL << (32 - __builtin_ffs(__PAGE_SIZE) + 1))
tools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c +9 -0
index 5628f0987012..85a7ea8b2e97 100644
@@ -34,9 +34,18 @@ __weak int test_buddy_create(void)
 		if (ret)
 			return ret;
 
+#ifndef BPF_ARENA_ASAN
+		if (!buddy.percpu[0]) {
+			buddy_destroy(&buddy);
+			return -EINVAL;
+		}
+#endif
+
 		ret = buddy_destroy(&buddy);
 		if (ret)
 			return ret;
+		if (buddy.percpu[0])
+			return -EINVAL;
 	}
 
 	return 0;
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +215 -5

273 lines not on this page · raw message

Signature · 1 line
2.52.0
#5 lore raw

Re: [PATCH bpf-next v3 3/7] selftests/bpf: libarena: Add buddy per-CPU caches

TL;DR: Commit message only: the claimed 1.5 KiB per-CPU cache bound seems to cover just the metadata; cached objects could hold about 64 KiB per CPU (roughly 64 MiB at 1024 CPUs), so the changelog…
Quoted 1 line

selftests/bpf: libarena: Add buddy per-CPU caches

TL;DR: Commit message only: the claimed 1.5 KiB per-CPU cache bound seems to cover just the metadata; cached objects could hold about 64 KiB per CPU (roughly 64 MiB at 1024 CPUs), so the changelog should state that.

Quoted 17 lines

selftests/bpf: libarena: Add buddy per-CPU caches

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.

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.

Quoted 8 lines

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.

The changelog says "Each array holds 16 objects, bounding each CPU's cache to about 1.5 KiB." Is that the bound on what a CPU's cache can hold, or only the size of the slot arrays?

The 1.5 KiB figure matches the cache container. struct buddy_cache_order is 136 bytes padded to 192 by __attribute__((aligned(64))), so sizeof(struct buddy_cache) is 64 + 8 * 192 = 1600 bytes, and buddy_cache_init() allocates it from the buddy as an order-7 (2 KiB) block per CPU.

The objects held in the cache are bounded separately, by BUDDY_CACHE_CAPACITY for each of the BUDDY_CACHE_NUM_ORDERS cached orders:

    elems = READ_ONCE(slots->elems);
    if (elems >= BUDDY_CACHE_CAPACITY)
        return -ENOMEM;

    WRITE_ONCE(slots->objects[elems], obj);

That allows up to 16 * (16 + 32 + 64 + 128 + 256 + 512 + 1024 + 2048) = 65280 bytes (about 64 KiB) of cached objects per CPU, plus the 2 KiB cache block, which is roughly 40 times the figure in the changelog. Cached objects stay marked allocated in the chunk metadata, so they cannot be coalesced or handed out to other CPUs.

With BUDDY_CACHE_MAX_CPUS = 1024 that is about 64 MiB of arena memory that can sit in the per-CPU caches, plus 2 MiB of cache structures allocated up front in buddy_init(). The comment on BUDDY_CACHE_NUM_ORDERS in buddy.h ("avoid excessive memory consumption from the per-cpu lists") suggests retained memory is the quantity being limited. The later patches in this series leave BUDDY_CACHE_CAPACITY at 16.

Could the changelog state the real bound on cached memory, or say that 1.5 KiB is only the per-CPU metadata size?

---
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/39878206535

author reviewer
#6 lore raw

PATCH v3 4/7 selftests/bpf: libarena: Handle NMI reentrancy

In reply to Emil Tsalapatis

An NMI can interrupt the buddy allocator while it holds the buddy lock, so code running in NMI context must not take that lock.

Allow an allocation from NMI context only when the local cache can satisfy it without taking the buddy lock.

When an NMI free cannot use the local cache, link the object onto an intrusive deferred-free list. A later allocator operation from a safe context drains that list while holding the buddy lock.

---
 .../bpf/libarena/include/libarena/buddy.h     |   3 +
 .../bpf/libarena/selftests/test_buddy.bpf.c   |   2 +
 .../selftests/bpf/libarena/src/buddy.bpf.c    | 129 +++++++++++++++++-
 3 files changed, 133 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/libarena/include/libarena/buddy.h +3 -0
index 66349915745b..de8d29ea4673 100644
@@ -49,6 +49,8 @@ struct buddy_header {
 	u32 next_index; /* Same for the next allocation. */
 };
 
+struct buddy_deferred_free;
+
 /*
  * We bring memory into the allocator 1 MiB at a time.
  */
@@ -103,6 +105,7 @@ struct buddy {
 	arena_spinlock_t lock;			/* Allocator lock */
 	u64 vaddr;				/* Allocation into reserved vaddr */
 	struct buddy_cache __arena *percpu[BUDDY_CACHE_MAX_CPUS];
+	struct buddy_deferred_free __arena *deferred_free;
 };
 
 #ifdef __BPF__
tools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c +2 -0
index 85a7ea8b2e97..e7c6ac76433c 100644
@@ -44,6 +44,8 @@ __weak int test_buddy_create(void)
 		ret = buddy_destroy(&buddy);
 		if (ret)
 			return ret;
+		if (buddy.deferred_free)
+			return -EINVAL;
 		if (buddy.percpu[0])
 			return -EINVAL;
 	}
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +128 -1

195 lines not on this page · raw message

Signature · 1 line
2.52.0
#7 lore raw

Re: [PATCH bpf-next v3 4/7] selftests/bpf: libarena: Handle NMI reentrancy

Quoted 5 lines · Emil Tsalapatis

An NMI can interrupt the buddy allocator while it holds the buddy lock, so code running in NMI context must not take that lock.

Allow an allocation from NMI context only when the local cache can satisfy it without taking the buddy lock.

can we take the buddy and transfer locks with arena_spin_trylock() in NMI instead ?
It succeeds only when the lock has no owner and no waiters, so it's safe in NMI. Every caller handles a lock failure already.

With this patch nothing refills the caches in NMI, and buddy_cache_init() leaves them empty. A prog that runs only in NMI gets NULL from every buddy_alloc().

With trylock an empty cache gets a refill and a full cache spills. When trylock fails, buddy_alloc() returns NULL and buddy_free() defers the object.
Then bpf_in_nmi() is in one place instead of five by the end of the series, and no need for buddy_deferred_free_start_bulk().

Something like this (not even compile tested):

static __always_inline int buddy_spin_lock(arena_spinlock_t __arena *lock)

{
	if (!bpf_in_nmi())
		return arena_spin_lock(lock);

	/* NMI could have interrupted the owner of the lock on this cpu */
	bpf_preempt_disable();
	if (arena_spin_trylock(lock))
		return 0;
	bpf_preempt_enable();

	return -EBUSY;
}
#define buddy_spin_lock_irqsave(lock, flags)			\
	({							\
		int __ret;					\
		bpf_local_irq_save(&(flags));			\
		__ret = buddy_spin_lock(lock);			\
		if (__ret)					\
			bpf_local_irq_restore(&(flags));	\
		(__ret);					\
	})

#define buddy_lock(buddy, flags) buddy_spin_lock_irqsave(&(buddy)->lock, flags)

and in buddy_free():

	ret = buddy_lock(buddy, flags);
	if (ret) {
		buddy_deferred_free_start(buddy, addr);
		return 0;
	}

[...]

Quoted 4 lines
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +4 -0
+	do {
+		head = buddy->deferred_free;
+		if (!head)
+			break;

tools/testing/selftests/bpf/libarena/src/buddy.bpf.c static int buddy_free_unlocked(struct buddy __arena *buddy, u64 addr) · line 1230

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +10 -0
+		old = head;
+
+		/*
+		 * The cmpxchg is safe because we never pop from the list
+		 * without holding the buddy lock. The next pointer of
+		 * the element then cannot have been gotten invalidated.
+		 */
+
+	} while (cmpxchg((void __arena * __arena *)&buddy->deferred_free,
+			 old, NULL) != old && can_loop);

Also one __sync_lock_test_and_set() takes the whole list. No need for the loop.

author reviewer
#8 lore raw

Re: [PATCH bpf-next v3 4/7] selftests/bpf: libarena: Handle NMI reentrancy

Quoted 17 lines · Alexei Starovoitov
Quoted 5 lines · Emil Tsalapatis

An NMI can interrupt the buddy allocator while it holds the buddy lock, so code running in NMI context must not take that lock.

Allow an allocation from NMI context only when the local cache can satisfy it without taking the buddy lock.

can we take the buddy and transfer locks with arena_spin_trylock() in NMI instead ?
It succeeds only when the lock has no owner and no waiters, so it's safe in NMI. Every caller handles a lock failure already.

With this patch nothing refills the caches in NMI, and buddy_cache_init() leaves them empty. A prog that runs only in NMI gets NULL from every buddy_alloc().

Quoted 8 lines

With trylock an empty cache gets a refill and a full cache spills. When trylock fails, buddy_alloc() returns NULL and buddy_free() defers the object.
Then bpf_in_nmi() is in one place instead of five by the end of the series, and no need for buddy_deferred_free_start_bulk().

Something like this (not even compile tested):

This makes sense, I will try it out when respinning.

<SNIP>

Quoted 12 lines

[...]

Quoted 9 lines
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +9 -0
+     do {
+             head = buddy->deferred_free;
+             if (!head)
+                     break;
+             old = head;
+
+             /*
+              * The cmpxchg is safe because we never pop from the list
+              * without holding the buddy lock. The next pointer of
Quoted 8 lines

tools/testing/selftests/bpf/libarena/src/buddy.bpf.c static int buddy_free_unlocked(struct buddy __arena *buddy, u64 addr) · line 1230

Quoted 5 lines
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +5 -0
+              * the element then cannot have been gotten invalidated.
+              */
+
+     } while (cmpxchg((void __arena * __arena *)&buddy->deferred_free,
+                      old, NULL) != old && can_loop);

Also one __sync_lock_test_and_set() takes the whole list. No need for the loop.

Ack, will do.

author reviewer
#9 lore raw

PATCH v3 5/7 selftests/bpf: libarena: Refill local caches in batches

In reply to Emil Tsalapatis

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.

---
 .../bpf/libarena/include/libarena/buddy.h     |  6 +++-
 .../selftests/bpf/libarena/src/buddy.bpf.c    | 28 +++++++++++++++++--
 2 files changed, 30 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/libarena/include/libarena/buddy.h +5 -1
index de8d29ea4673..0c984952042a 100644
@@ -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 {
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +25 -3
index 6f98044518ed..837d5cf70185 100644
@@ -131,10 +131,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;
@@ -143,7 +161,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;
@@ -1060,7 +1082,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;
 
Signature · 1 line
2.52.0
#10 lore raw

Re: [PATCH bpf-next v3 5/7] selftests/bpf: libarena: Refill local caches in batches

tools/testing/selftests/bpf/libarena/src/buddy.bpf.c static __always_inline int buddy_cache_push(struct buddy_cache __arena *cache, · line 146

Quoted 3 lines · Emil Tsalapatis
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +3 -0
+	if (buddy_alloc_bulk(buddy, size, BUDDY_CACHE_BATCH_ORDER,
+			     (void __arena * __arena *)slots->objects))
+		return NULL;

This asks for one block of order + 3. When the existing chunks have free objects of this order, but no such block, it goes to buddy_chunk_get(). Before this patch the existing chunks served such buddy_alloc().

A sleepable prog takes another 1 MiB chunk.
A non-sleepable prog loses 1 MiB of buddy->vaddr in every buddy_alloc():

buddy_alloc() the cache is empty
buddy_cache_refill()
buddy_alloc_bulk() no free block of order + 3
buddy_chunk_get()
buddy_alloc_arena_vaddr() buddy->vaddr += 1 MiB
bpf_arena_free_pages() deferred to a worker
bpf_arena_alloc_pages() the range is still reserved: NULL
buddy_alloc_memory() one object from the existing chunks
the cache is still empty

After 1024 of them buddy_alloc_arena_vaddr() fails for sleepable progs too.

Should the refill look at the existing chunks only, like buddy_alloc_nogrow() in your other series?

author reviewer
#11 lore raw

Re: [PATCH bpf-next v3 5/7] selftests/bpf: libarena: Refill local caches in batches

Quoted 23 lines · Alexei Starovoitov

tools/testing/selftests/bpf/libarena/src/buddy.bpf.c static __always_inline int buddy_cache_push(struct buddy_cache __arena *cache, · line 146

Quoted 3 lines · Emil Tsalapatis
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +3 -0
+     if (buddy_alloc_bulk(buddy, size, BUDDY_CACHE_BATCH_ORDER,
+                          (void __arena * __arena *)slots->objects))
+             return NULL;

This asks for one block of order + 3. When the existing chunks have free objects of this order, but no such block, it goes to buddy_chunk_get(). Before this patch the existing chunks served such buddy_alloc().

A sleepable prog takes another 1 MiB chunk.
A non-sleepable prog loses 1 MiB of buddy->vaddr in every buddy_alloc():

buddy_alloc() the cache is empty
buddy_cache_refill()
buddy_alloc_bulk() no free block of order + 3
buddy_chunk_get()
buddy_alloc_arena_vaddr() buddy->vaddr += 1 MiB
bpf_arena_free_pages() deferred to a worker
bpf_arena_alloc_pages() the range is still reserved: NULL
buddy_alloc_memory() one object from the existing chunks
the cache is still empty

Quoted 5 lines

After 1024 of them buddy_alloc_arena_vaddr() fails for sleepable progs too.

Should the refill look at the existing chunks only, like buddy_alloc_nogrow() in your other series?

I think that'd be a fair policy, the downside would be that consecutive allocations
would fall through the cache, but that's a fair tradeoff. It's easier for a user to
preallocate memory into the allocator from the mapping as opposed to having to deal with the fragmentation you're describing.

#12 lore raw

Re: [PATCH bpf-next v3 5/7] selftests/bpf: libarena: Refill local caches in batches

Quoted 31 lines · Emil Tsalapatis
Quoted 23 lines · Alexei Starovoitov

tools/testing/selftests/bpf/libarena/src/buddy.bpf.c static __always_inline int buddy_cache_push(struct buddy_cache __arena *cache, · line 146

Quoted 3 lines · Emil Tsalapatis
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +3 -0
+     if (buddy_alloc_bulk(buddy, size, BUDDY_CACHE_BATCH_ORDER,
+                          (void __arena * __arena *)slots->objects))
+             return NULL;

This asks for one block of order + 3. When the existing chunks have free objects of this order, but no such block, it goes to buddy_chunk_get(). Before this patch the existing chunks served such buddy_alloc().

A sleepable prog takes another 1 MiB chunk.
A non-sleepable prog loses 1 MiB of buddy->vaddr in every buddy_alloc():

buddy_alloc() the cache is empty
buddy_cache_refill()
buddy_alloc_bulk() no free block of order + 3
buddy_chunk_get()
buddy_alloc_arena_vaddr() buddy->vaddr += 1 MiB
bpf_arena_free_pages() deferred to a worker
bpf_arena_alloc_pages() the range is still reserved: NULL
buddy_alloc_memory() one object from the existing chunks
the cache is still empty

Quoted 5 lines

After 1024 of them buddy_alloc_arena_vaddr() fails for sleepable progs too.

Should the refill look at the existing chunks only, like buddy_alloc_nogrow() in your other series?

Quoted 6 lines

I think that'd be a fair policy, the downside would be that consecutive allocations
would fall through the cache, but that's a fair tradeoff. It's easier for a user to
preallocate memory into the allocator from the mapping as opposed to having to deal with the fragmentation you're describing.

Thanks. Makes sense to me.

author reviewer
#13 lore raw

PATCH v3 6/7 selftests/bpf: libarena: Add a transfer cache

In reply to Emil Tsalapatis

Batch refills amortize local-cache misses, but every batch that refills an empty local array, and every free into a full one, still goes through the buddy lock. Under contention the buddy lock and its metadata updates dominate, and spare objects on one CPU cannot serve another.

Add a fixed-size shared transfer array for each cached order, protected by a per-order lock. A full local cache moves one batch into the transfer array, and an empty one takes a batch from it before falling back to the buddy allocator. Embed the transfer arrays in struct buddy so this layer requires no dynamically allocated containers.

Check the transfer array's count before taking its lock, so that a full or empty array does not cost a lock acquisition that cannot succeed. When the array is full or its lock is busy, spill the local batch to the buddy allocator with buddy_free_bulk(), so the next frees fit in the local cache instead of each taking the transfer and buddy locks. NMI context never takes the transfer lock and spills directly. Keep both slow paths out of line so that cache hits do not pay for them.

---
 .../bpf/libarena/include/libarena/buddy.h     |  11 ++
 .../selftests/bpf/libarena/src/buddy.bpf.c    | 123 +++++++++++++++++-
 2 files changed, 131 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/libarena/include/libarena/buddy.h +11 -0
index 0c984952042a..1c62ae6e3fd0 100644
@@ -89,6 +89,9 @@ enum buddy_cache_consts {
 	 * Objects retained locally for each allocation order.
 	 */
 	BUDDY_CACHE_CAPACITY = BUDDY_CACHE_BATCH_SIZE * 2,
+
+	/* Number of objects shared between per-CPU caches for each order. */
+	BUDDY_TRANSFER_CAPACITY = BUDDY_CACHE_BATCH_SIZE * 8,
 };
 
 struct buddy_cache_order {
@@ -104,11 +107,19 @@ struct buddy_cache {
 	struct buddy_cache_order orders[BUDDY_CACHE_NUM_ORDERS];
 };
 
+/* Per-order array shared between the per-CPU caches. */
+struct buddy_transfer_order {
+	arena_spinlock_t lock;
+	u32 elems;
+	void __arena *objects[BUDDY_TRANSFER_CAPACITY];
+} __attribute__((aligned(64)));
+
 struct buddy {
 	struct buddy_chunk __arena *first_chunk;		/* Pointer to the chunk linked list. */
 	arena_spinlock_t lock;			/* Allocator lock */
 	u64 vaddr;				/* Allocation into reserved vaddr */
 	struct buddy_cache __arena *percpu[BUDDY_CACHE_MAX_CPUS];
+	struct buddy_transfer_order transfer[BUDDY_CACHE_NUM_ORDERS];
 	struct buddy_deferred_free __arena *deferred_free;
 };
 
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +120 -3

163 lines not on this page · raw message

Signature · 1 line
2.52.0
#14 lore raw

Re: [PATCH bpf-next v3 6/7] selftests/bpf: libarena: Add a transfer cache

Quoted 3 lines · Emil Tsalapatis

When the array is full or its lock is busy, spill the local batch to the buddy allocator with buddy_free_bulk(), so the next frees fit in the local cache instead of each taking the transfer and buddy locks. NMI

My earlier feedback must have been lost:
Pls add the buddy_free_bulk() spill in patch 5, next to the batched refill. Keep only the transfer array in this patch, on top of it. As posted buddy_cache_spill() arrives here with both branches. Until this patch a free into a full cache takes the buddy lock for every object.

Also your libarena compatibility fixups series changes the same lines of buddy.h, it seems. I suspect you want them to go first and rebase this one.

pw-bot: cr

author reviewer
#15 lore raw

Re: [PATCH bpf-next v3 6/7] selftests/bpf: libarena: Add a transfer cache

Quoted 6 lines · Alexei Starovoitov
Quoted 3 lines · Emil Tsalapatis

When the array is full or its lock is busy, spill the local batch to the buddy allocator with buddy_free_bulk(), so the next frees fit in the local cache instead of each taking the transfer and buddy locks. NMI

Quoted 6 lines

My earlier feedback must have been lost:
Pls add the buddy_free_bulk() spill in patch 5, next to the batched refill. Keep only the transfer array in this patch, on top of it. As posted buddy_cache_spill() arrives here with both branches. Until this patch a free into a full cache takes the buddy lock for every object.

Hi Alexei,

Yes I must have missed that, sorry about that. I will address and respin.

Quoted 3 lines

Also your libarena compatibility fixups series changes the same lines of buddy.h, it seems. I suspect you want them to go first and rebase this one.

Yes, the plan is to first merge the compatibility fixes then make the larger change. I'll respin on top once we handle the compatibility patch.

Quoted 1 line

pw-bot: cr

author reviewer
#16 lore raw

PATCH v3 7/7 selftests/bpf: libarena: Cover buddy cache fast paths

In reply to Emil Tsalapatis

Add coverage for the bulk allocation helpers. Check object stride, bounds and contents, then free the objects in a non-sequential order to exercise their independent metadata and coalescing.

Exercise local-cache initialization, batch refill, LIFO reuse and capacity overflow. Verify that overflow moves a batch into the transfer cache and that a later underflow consumes it without reaching the buddy allocator.

Document and test that explicit bulk allocations bypass the per-CPU cache.

---
 .../bpf/libarena/selftests/test_buddy.bpf.c   | 262 ++++++++++++++++++
 .../selftests/bpf/libarena/src/buddy.bpf.c    |   5 +
 2 files changed, 267 insertions(+)
tools/testing/selftests/bpf/libarena/selftests/test_buddy.bpf.c +262 -0
index e7c6ac76433c..a25326a3b9a0 100644
@@ -17,6 +17,24 @@ struct segarr_entry {
 #define SEGARRLEN (512)
 static struct segarr_entry __arena segarr[SEGARRLEN];
 static void __arena *ptrs[17];
+
+enum buddy_alloc_path_test_consts {
+	TEST_BULK_NORDER = 3,
+	TEST_BULK_ITEMS = 1 << TEST_BULK_NORDER,
+	TEST_BULK_SIZE = 17,
+	TEST_BULK_STEP = 32,
+	TEST_CACHE_ORDER = 2,
+	TEST_CACHE_SIZE = BUDDY_MIN_ALLOC_BYTES << TEST_CACHE_ORDER,
+};
+
+struct buddy_alloc_path_test_storage {
+	void __arena *bulk[BUDDY_CACHE_CAPACITY + 1];
+	void __arena *to_free[BUDDY_CACHE_CAPACITY + 1];
+	void __arena *drain[BUDDY_CACHE_BATCH_SIZE];
+};
+
+static struct buddy_alloc_path_test_storage __arena alloc_path_test;
+
 size_t __arena alloc_sizes[] = { 3, 17, 1025, 129, 16350, 333, 9, 517 };
 size_t __arena alloc_multiple_sizes[] = { 3, 17, 1025, 129, 16350, 333, 9, 517, 2099 };
 size_t __arena alloc_free_sizes[] = { 3, 17, 64, 129, 256, 333, 512, 517 };
@@ -221,4 +239,248 @@ __weak int test_buddy_alignment(void)
 	return 0;
 }
 
+SEC("syscall")
+__weak int test_buddy_bulk_alloc_free(void)
+{
+	const size_t uncached_size =
+		(BUDDY_MIN_ALLOC_BYTES << (BUDDY_CACHE_NUM_ORDERS - 1)) + 1;
+	void __arena *sentinel = (void __arena *)&alloc_path_test;
+	u8 __arena *mem;
+	unsigned int i, j, idx;
+	int ret, destroy_ret;
+
+	ret = buddy_init(&buddy);
+	if (ret)
+		return ret;
+
+	/*
+	 * A 17-byte request rounds up to a 32-byte buddy allocation. Verify
+	 * that the bulk allocator returns every child at the expected stride,
+	 * without writing beyond the requested output array.
+	 */
+	alloc_path_test.bulk[TEST_BULK_ITEMS] = sentinel;
+	ret = buddy_alloc_bulk(&buddy, TEST_BULK_SIZE, TEST_BULK_NORDER,
+			       alloc_path_test.bulk);
+	if (ret)
+		goto out;
+	if (alloc_path_test.bulk[TEST_BULK_ITEMS] != sentinel) {
+		ret = -EOVERFLOW;
+		goto out;
+	}
+
+	for (i = zero; i < TEST_BULK_ITEMS && can_loop; i++) {
+		mem = alloc_path_test.bulk[i];
+		if (!mem) {
+			ret = -ENOMEM;
+			goto out;
+		}
+		if (i && (u64)mem !=
+			 (u64)alloc_path_test.bulk[i - 1] + TEST_BULK_STEP) {
+			ret = -EINVAL;
+			goto out;
+		}
+
+		for (j = zero; j < TEST_BULK_SIZE && can_loop; j++)
+			mem[j] = (u8)(i + 1);
+	}
+
+	for (i = zero; i < TEST_BULK_ITEMS && can_loop; i++) {
+		mem = alloc_path_test.bulk[i];
+		for (j = zero; j < TEST_BULK_SIZE && can_loop; j++) {
+			if (mem[j] != (u8)(i + 1)) {
+				ret = -EINVAL;
+				goto out;
+			}
+		}
+
+		/* Five is coprime to eight, producing a permutation of all items. */
+		idx = (i * 5) & (TEST_BULK_ITEMS - 1);
+		alloc_path_test.to_free[i] = alloc_path_test.bulk[idx];
+	}
+	alloc_path_test.to_free[TEST_BULK_ITEMS] = NULL;
+
+	ret = buddy_free_bulk(&buddy, alloc_path_test.to_free,
+			      TEST_BULK_ITEMS + 1);
+	if (ret)
+		goto out;
+
+	/*
+	 * Order eight is immediately above the per-CPU cache range. Free a bulk
+	 * allocation one child at a time in a non-sequential order to exercise
+	 * independent metadata and coalescing without the cache intercepting it.
+	 */
+	ret = buddy_alloc_bulk(&buddy, uncached_size, TEST_BULK_NORDER,
+			       alloc_path_test.bulk);
+	if (ret)
+		goto out;
+
+	for (i = zero; i < TEST_BULK_ITEMS && can_loop; i++) {
+		idx = (i * 5) & (TEST_BULK_ITEMS - 1);
+		ret = buddy_free(&buddy, alloc_path_test.bulk[idx]);
+		if (ret)
+			goto out;
+	}
+
+out:
+	destroy_ret = buddy_destroy(&buddy);
+	return ret ?: destroy_ret;
+}
+
+SEC("syscall")
+__weak int test_buddy_cache_paths(void)
+{
+#ifdef BPF_ARENA_ASAN
+	return 0;
+#else
+	struct buddy_cache __arena *cache;
+	struct buddy_cache_order __arena *slots;
+	struct buddy_transfer_order __arena *transfer;
+	void __arena *direct, *cached, *reused, *refilled, *overflow_reused;
+	void __arena *transferred;
+	unsigned int cpu, i;
+	int ret, destroy_ret;
+
+	ret = buddy_init(&buddy);
+	if (ret)
+		return ret;
+
+	cpu = bpf_get_smp_processor_id();
+	cache = buddy.percpu[cpu];
+	if (!cache) {
+		ret = -EINVAL;
+		goto out;
+	}
+	slots = &cache->orders[TEST_CACHE_ORDER];
+	transfer = &buddy.transfer[TEST_CACHE_ORDER];
+	if (READ_ONCE(slots->elems) || READ_ONCE(transfer->elems)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* A miss bulk-refills the local order and returns one object directly. */
+	direct = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+	if (!direct || READ_ONCE(slots->elems) != BUDDY_CACHE_BATCH_SIZE - 1) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	/* Subsequent allocations and frees stay on the packed local stack. */
+	cached = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+	if (!cached || READ_ONCE(slots->elems) != BUDDY_CACHE_BATCH_SIZE - 2) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	ret = buddy_free(&buddy, cached);
+	if (ret)
+		goto out;
+	reused = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+	if (reused != cached) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Drain the local stack, then verify that the next miss refills it. */
+	for (i = zero; i < BUDDY_CACHE_BATCH_SIZE - 2 && can_loop; i++) {
+		alloc_path_test.drain[i] = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+		if (!alloc_path_test.drain[i]) {
+			ret = -ENOMEM;
+			goto out;
+		}
+	}
+	if (READ_ONCE(slots->elems)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	refilled = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+	if (!refilled ||
+	    READ_ONCE(slots->elems) != BUDDY_CACHE_BATCH_SIZE - 1) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Bulk allocation bypasses the cache. Fill the remaining local slots. */
+	ret = buddy_alloc_bulk(&buddy, TEST_CACHE_SIZE,
+			       arena_fls(BUDDY_CACHE_CAPACITY) - 1,
+			       alloc_path_test.bulk);
+	if (ret)
+		goto out;
+
+	ret = buddy_free(&buddy, alloc_path_test.bulk[0]);
+	if (ret)
+		goto out;
+
+	ret = buddy_free(&buddy, alloc_path_test.bulk[1]);
+	if (ret)
+		goto out;
+
+	for (i = 2;
+	     i <= BUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE && can_loop;
+	     i++) {
+		ret = buddy_free(&buddy, alloc_path_test.bulk[i]);
+		if (ret)
+			goto out;
+	}
+	if (READ_ONCE(slots->elems) != BUDDY_CACHE_CAPACITY) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* A free beyond local capacity transfers a batch to the shared cache. */
+	ret = buddy_free(&buddy,
+			 alloc_path_test.bulk[BUDDY_CACHE_CAPACITY -
+					      BUDDY_CACHE_BATCH_SIZE + 1]);
+	if (ret)
+		goto out;
+	if (READ_ONCE(slots->elems) !=
+			BUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE + 1 ||
+	    READ_ONCE(transfer->elems) != BUDDY_CACHE_BATCH_SIZE) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	overflow_reused = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+	if (overflow_reused != alloc_path_test.bulk[BUDDY_CACHE_CAPACITY -
+							 BUDDY_CACHE_BATCH_SIZE + 1] ||
+	    READ_ONCE(slots->elems) !=
+			BUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Drain locally, then consume the batch without touching buddy. */
+	for (i = zero;
+	     i < BUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE && can_loop;
+	     i++) {
+		alloc_path_test.drain[i] = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+		if (!alloc_path_test.drain[i]) {
+			ret = -ENOMEM;
+			goto out;
+		}
+	}
+	transferred = buddy_alloc(&buddy, TEST_CACHE_SIZE);
+	if (transferred != alloc_path_test.bulk[BUDDY_CACHE_CAPACITY -
+						     BUDDY_CACHE_BATCH_SIZE] ||
+	    READ_ONCE(slots->elems) != BUDDY_CACHE_BATCH_SIZE - 1 ||
+	    READ_ONCE(transfer->elems)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Interior pointers are rejected without consuming the allocation. */
+	i = BUDDY_CACHE_CAPACITY - BUDDY_CACHE_BATCH_SIZE + 2;
+	if (!buddy_free(&buddy, (u8 __arena *)alloc_path_test.bulk[i] +
+				 BUDDY_MIN_ALLOC_BYTES)) {
+		ret = -EINVAL;
+		goto out;
+	}
+	ret = buddy_free(&buddy, alloc_path_test.bulk[i]);
+
+out:
+	destroy_ret = buddy_destroy(&buddy);
+	return ret ?: destroy_ret;
+#endif
+}
+
 __weak char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +5 -0
index e6528cb5a63d..a10dd814f3d5 100644
@@ -1177,6 +1177,11 @@ int buddy_alloc_bulk(struct buddy __arena *buddy, size_t size, size_t norder,
 	if (!buddy || !out || norder >= BUDDY_CHUNK_NUM_ORDERS)
 		return -EINVAL;
 
+	/*
+	 * Bulk allocations bypass the per-cpu caches and reserve one buddy
+	 * block covering all returned objects. The buddy metadata records
+	 * each object separately so it can later be freed independently.
+	 */
 	order = size_to_order(size);
 	address = buddy_alloc_memory(buddy, size, order, norder);
 	if (!address)
Signature · 1 line
2.52.0