selftests/bpf: libarena: compatibility fixups
Subsystems (3)
Emil Tsalapatis
reviewerv2 cover selftests/bpf: libarena: compatibility fixups
This patchset fixes several issues found while integrating with large BPF codebases.
Patch 1 fixes a mismatch between the in-code licenses for some files that do not actually correspond to the licenses at the top of the file. Remove the in-code license sections, since libarena is distributed only as source code anyway.
Patches 2-3 add an explicit allocator growth call. Some allocator users prefer to explicitly expand the arena instead of implicitly letting the allocator do so when required to satisfy an alloc. The change is non-functional for existing users.
Patch 4 changes the type of the volatile zero variable used for loop convergence to __u32. This is available from <linux/types.h>, while u32 needs to be typedef'ed in for userspace programs.
v1 -> v2 (https://lore.kernel.org/bpf/[email protected]/)
- Add a "Dual BSD/GPL" license section to the benchmark consumer so libarena-malloc and libarena-calloc remain loadable (Sashiko bot).
- Rename buddy_grow()'s size argument to nr_chunks and use a size_t loop index to make the chunk-count API explicit (Sashiko bot).
Emil Tsalapatis (4):
libarena: remove in-code licenses
selftests/bpf: libarena: make allocator growth on alloc failure
optional
selftests/bpf: libarena: add explicit buddy_grow() call selftests/bpf: libarena: Make zero variable type __u32 instead of u32
.../bpf/libarena/benchs/bench_malloc.bpf.c | 2 + .../bpf/libarena/include/libarena/buddy.h | 2 + .../bpf/libarena/include/libarena/common.h | 2 +- .../selftests/bpf/libarena/src/asan.bpf.c | 2 - .../selftests/bpf/libarena/src/buddy.bpf.c | 48 +++++++++++++++++-- .../selftests/bpf/libarena/src/common.bpf.c | 4 +- 6 files changed, 50 insertions(+), 10 deletions(-)
Signature · 1 line
2.52.0
Emil Tsalapatis
reviewerPATCH v2 1/4 libarena: remove in-code licenses
The libarena code currently specifies a license section in multiple locations. These sections are a) inaccurate, since the license is actually Dual BSD/GPL, not pure GPL, and b) superfluous, since the files already have the license on them and libarena is distributed as a source. Remove the license strings: The code that includes the the libarena sources should be the one with the license section.
--- tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c | 2 ++ tools/testing/selftests/bpf/libarena/src/asan.bpf.c | 2 -- tools/testing/selftests/bpf/libarena/src/buddy.bpf.c | 2 -- tools/testing/selftests/bpf/libarena/src/common.bpf.c | 2 -- 4 files changed, 2 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c +2 -0
@@ -49,3 +49,5 @@ int bench_calloc(void)__sync_add_and_fetch (&bench_hits, i);return 0 ; } + +char _license[] SEC("license") = "Dual BSD/GPL";
tools/testing/selftests/bpf/libarena/src/asan.bpf.c +0 -2
@@ -532,5 +532,3 @@ __weak __noasan int asan_init(struct asan_init_args *args)#pragma clang attribute pop#endif /* BPF_ARENA_ASAN */ - -__weak char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +0 -2
@@ -899,5 +899,3 @@ __weak int buddy_free(struct buddy __arena *buddy, void __arena *addr)return ret; } - -__weak char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/libarena/src/common.bpf.c +0 -2
@@ -85,5 +85,3 @@ __weak void arena_free(void __arena *ptr) {buddy_free (&buddy, ptr); } - -char _license[] SEC("license") = "GPL";
Signature · 1 line
2.52.0
Emil Tsalapatis
reviewerPATCH v2 2/4 selftests/bpf: libarena: make allocator growth on alloc failure optional
In reply to Emil TsalapatisThe buddy allocator currently always grows when an allocation fails due to a lack of allocated arena pages. This behavior assumes that the user sizes the arena to be as large as the maximum allocation size, but that is not always the case.
Add a nogrow allocation option for users that want to explicitly control the total mapped size of the arena. With this API, an allocation request that cannot be serviced from currently mapped arena pages fails instead of triggering bpf_arena_alloc_pages(). The user instead explicitly maps additional memory into the arena with a separate buddy_grow() call.
--- .../bpf/libarena/include/libarena/buddy.h | 1 + .../selftests/bpf/libarena/src/buddy.bpf.c | 20 +++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/libarena/include/libarena/buddy.h +1 -0
@@ -77,5 +77,6 @@ int buddy_init(struct buddy __arena *buddy); int buddy_destroy(struct buddy __arena *buddy); int buddy_free(struct buddy __arena *buddy, void __arena *free); void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size); +void __arena *buddy_alloc_nogrow(struct buddy __arena *buddy, size_t size);#endif /* __BPF__ */
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +18 -2
@@ -747,8 +747,9 @@ static u64 buddy_alloc_from_new_chunk(struct buddy __arena *buddy, struct buddy_return (u64 )address; } -__weak -void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size) + +static __always_inline +void __arena *buddy_alloc_maygrow(struct buddy __arena *buddy, size_t size, bool grow) { void __arena *address = NULL; struct buddy_chunk __arena *chunk;
@@ -772,6 +773,9 @@ void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size)if (address) goto done; +if (!grow) +return NULL ; +/* Get a new chunk. */ chunk =buddy_chunk_get (buddy);if (chunk)
@@ -796,6 +800,18 @@ void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size)return address; } +__weak +void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size) +{ +return buddy_alloc_maygrow (buddy, size, true); +} + +__weak +void __arena *buddy_alloc_nogrow(struct buddy __arena *buddy, size_t size) +{ +return buddy_alloc_maygrow (buddy, size, false); +} + static __always_inline int buddy_free_unlocked(struct buddy __arena *buddy, u64 addr) { struct buddy_header __arena *header, *buddy_header;
Signature · 1 line
2.52.0
Emil Tsalapatis
reviewerPATCH v2 3/4 selftests/bpf: libarena: add explicit buddy_grow() call
In reply to Emil TsalapatisAdd an API call for explicitly growing the arena through page allocation. Useful for programs that do not do so implicitly through buddy_alloc().
--- .../bpf/libarena/include/libarena/buddy.h | 1 + .../selftests/bpf/libarena/src/buddy.bpf.c | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+)
tools/testing/selftests/bpf/libarena/include/libarena/buddy.h +1 -0
@@ -78,5 +78,6 @@ int buddy_destroy(struct buddy __arena *buddy); int buddy_free(struct buddy __arena *buddy, void __arena *free); void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size); void __arena *buddy_alloc_nogrow(struct buddy __arena *buddy, size_t size); +int buddy_grow(struct buddy __arena *buddy, size_t nr_chunks);#endif /* __BPF__ */
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +26 -0
@@ -812,6 +812,32 @@ void __arena *buddy_alloc_nogrow(struct buddy __arena *buddy, size_t size)return buddy_alloc_maygrow (buddy, size, false); } +__weak +int buddy_grow(struct buddy __arena *buddy, size_t nr_chunks) +{ + struct buddy_chunk __arena *chunk; +unsigned long flags; +size_t chunk_index; + +for (chunk_index = zero; chunk_index < nr_chunks && can_loop; chunk_index++) { + chunk =buddy_chunk_get (buddy); +if (!chunk) +return -ENOMEM ; + +if (buddy_lock (buddy, flags)) { +bpf_arena_free_pages (&arena, chunk,BUDDY_CHUNK_PAGES ); +return -ENOMEM ; + } + + chunk->next = buddy->first_chunk; + buddy->first_chunk = chunk; + +buddy_unlock (buddy, flags); + } + +return 0 ; +} + static __always_inline int buddy_free_unlocked(struct buddy __arena *buddy, u64 addr) { struct buddy_header __arena *header, *buddy_header;
Signature · 1 line
2.52.0
Emil Tsalapatis
reviewerPATCH v2 4/4 selftests/bpf: libarena: Make zero variable type __u32 instead of u32
In reply to Emil TsalapatisChange the zero variable used for loop convergence to the user-visible __u32 type to avoid missing type issues when including the skeleton in userspace.
--- tools/testing/selftests/bpf/libarena/include/libarena/common.h | 2 +- tools/testing/selftests/bpf/libarena/src/common.bpf.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/libarena/include/libarena/common.h +1 -1
@@ -43,7 +43,7 @@ struct {* imprecise. To force the variable to be imprecise, initialize it with * the opaque volatile variable 0 instead of the constant 0. */ -volatile u32 zero__weak ; +volatile __u32 zero__weak ;extern volatile u64 asan_violated;int arena_fls (__u64 word);
tools/testing/selftests/bpf/libarena/src/common.bpf.c +1 -1
@@ -7,7 +7,7 @@#include <libarena/buddy.h> struct buddy __arena buddy; -volatile u32 zero =0 ; +volatile __u32 zero =0 ;/* * Storage for the queue nodes declared by bpf_arena_spin_lock.h. Each program
Signature · 1 line
2.52.0
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 9 lines · On Tue, 27 Oct 2026 19:24:09 +0000 you wrote:
This patchset fixes several issues found while integrating with large BPF codebases.
Patch 1 fixes a mismatch between the in-code licenses for some files that do not actually correspond to the licenses at the top of the file. Remove the in-code license sections, since libarena is distributed only as source code anyway.
[...]
Here is the summary with links:
- [bpf-next,v2,1/4] libarena: remove in-code licenses
https://git.kernel.org/bpf/bpf-next/c/6e7f37fc8623 - [bpf-next,v2,2/4] selftests/bpf: libarena: make allocator growth on alloc failure optional https://git.kernel.org/bpf/bpf-next/c/8e192d828784 - [bpf-next,v2,3/4] selftests/bpf: libarena: add explicit buddy_grow() call https://git.kernel.org/bpf/bpf-next/c/1d9c7ff6e163 - [bpf-next,v2,4/4] selftests/bpf: libarena: Make zero variable type __u32 instead of u32 https://git.kernel.org/bpf/bpf-next/c/c2bc2eebaa83
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html