Skip to contentSkip to conversation

selftests/bpf: libarena: compatibility fixups

PATCH Superseded Emil Tsalapatis 2 participants 7 messages Oct 27

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Emil Tsalapatis

author reviewer
#0 lore raw

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.

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/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 +-
 5 files changed, 48 insertions(+), 10 deletions(-)
Signature · 1 line
2.52.0
author reviewer
#1 lore raw

PATCH 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/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 --
 3 files changed, 6 deletions(-)
tools/testing/selftests/bpf/libarena/src/asan.bpf.c +0 -2
index de656b69d13a..8a0f06110606 100644
@@ -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
index 2490ab1396de..552ce87875e3 100644
@@ -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
index d1af60b81838..d859bd538c52 100644
@@ -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
#2 lore raw

Re: [PATCH bpf-next 1/4] libarena: remove in-code licenses

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests/bpf: missing license section in libarena_bench.bpf.o --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests/bpf: missing license section in libarena_bench.bpf.o --

commit 1bfbb56cf90063a6a767e7e64926692dd815ff9b
Author: Emil Tsalapatis <[email protected]>

libarena: remove in-code licenses

This commit removes the SEC("license") definitions from the libarena BPF source files (asan.bpf.c, buddy.bpf.c, and common.bpf.c) so that consumers of libarena provide their own license section.

Quoted 25 lines
tools/testing/selftests/bpf/libarena/src/asan.bpf.c +0 -2
index de656b69d13af..8a0f061106064 100644
@@ -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
index 2490ab1396de0..552ce87875e3b 100644
@@ -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 -0
index d1af60b818389..d859bd538c52d 100644

tools/testing/selftests/bpf/libarena/src/common.bpf.c __weak void arena_free(void __arena *ptr) · line 89

Quoted 5 lines
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";

[Severity: Medium]
Does removing _license from common.bpf.c, buddy.bpf.c, and asan.bpf.c leave libarena_bench.bpf.o without a license section and break the libarena benchmarks at load time?

In tools/testing/selftests/bpf/libarena/Makefile, libarena_bench.bpf.o is linked solely from LIBARENA_CORE_SOURCES and LIBARENA_BENCH_SOURCES:

    LIBARENA_BENCH_OBJECTS = $(notdir $(LIBARENA_CORE_SOURCES:.bpf.c=.bpf.o) \
                                      $(LIBARENA_BENCH_SOURCES:.bpf.c=.bpf.o))

Unlike the selftests in selftests/test_buddy.bpf.c and selftests/test_asan_buddy.bpf.c, the benchmark source in tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c does not define a SEC("license") section.

When running the libarena-malloc or libarena-calloc benchmarks in tools/testing/selftests/bpf/benchs/bench_libarena.c:

    malloc_setup() / calloc_setup()
      -> setup_common()
           -> libarena_bench__open_and_load()

libarena_bench.bpf.o now loads with an empty license string, so the BPF verifier rejects the programs with -EINVAL when checking GPL compatibility for their kfunc calls, kernel BTF accesses, and bpf_trace_printk usage.

Should tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c define a SEC("license") section now that the core library sources no longer provide one?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#3 lore raw

PATCH 2/4 selftests/bpf: libarena: make allocator growth on alloc failure optional

In reply to Emil Tsalapatis

The 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
index 528c69a1f38e..958450fc5f9d 100644
@@ -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
index 552ce87875e3..ed359d4af1f4 100644
@@ -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
author reviewer
#4 lore raw

PATCH 3/4 selftests/bpf: libarena: add explicit buddy_grow() call

In reply to Emil Tsalapatis

Add 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
index 958450fc5f9d..4cad0649dc17 100644
@@ -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 size);
 
 #endif /* __BPF__  */
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +26 -0
index ed359d4af1f4..f06b6bd5c088 100644
@@ -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 size)
+{
+	struct buddy_chunk __arena  *chunk;
+	unsigned long flags;
+	int i;
+
+	for (i = zero; i < size && can_loop; i++) {
+		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
#5 lore raw

Re: [PATCH bpf-next 3/4] selftests/bpf: libarena: add explicit buddy_grow() call

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests/bpf: buddy_grow() treats byte size parameter as 1 MiB chunk count --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests/bpf: buddy_grow() treats byte size parameter as 1 MiB chunk count --

commit 423ea247cbc2154b99ae3fbf8b3a6fc0e7d6e98f
Author: Emil Tsalapatis <[email protected]>

selftests/bpf: libarena: add explicit buddy_grow() call

This commit adds buddy_grow() to libarena to allow BPF programs to explicitly grow the buddy allocator arena instead of relying on implicit growth during buddy_alloc().

Quoted 20 lines
tools/testing/selftests/bpf/libarena/include/libarena/buddy.h +1 -0
index 958450fc5f9db..4cad0649dc17a 100644
@@ -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 size);
 
 #endif /* __BPF__  */
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +1 -0
index ed359d4af1f4b..f06b6bd5c088e 100644
@@ -812,6 +812,32 @@ void __arena *buddy_alloc_nogrow(struct buddy __arena *buddy, size_t size)
 	return buddy_alloc_maygrow(buddy, size, false);
 }
 
+__weak

tools/testing/selftests/bpf/libarena/src/buddy.bpf.c void __arena *buddy_alloc_nogrow(struct buddy __arena *buddy, size_t size) · line 825

Quoted 10 lines
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c +10 -0
+int buddy_grow(struct buddy __arena *buddy, size_t size)
+{
+	struct buddy_chunk __arena  *chunk;
+	unsigned long flags;
+	int i;
+
+	for (i = zero; i < size && can_loop; i++) {
+		chunk = buddy_chunk_get(buddy);
+		if (!chunk)
+			return -ENOMEM;

[Severity: Medium]
In tools/testing/selftests/bpf/libarena/include/libarena/buddy.h, buddy_grow() is declared right below buddy_alloc() and buddy_alloc_nogrow() with the same size_t size parameter, which takes a size in bytes in those functions.

In tools/testing/selftests/bpf/libarena/src/buddy.bpf.c, buddy_grow() uses size directly as the loop trip count for buddy_chunk_get(buddy). Each call to buddy_chunk_get() allocates a 1 MiB chunk (BUDDY_CHUNK_BYTES, or BUDDY_CHUNK_PAGES = 256 pages) out of the 1 GiB reserved virtual address range (BUDDY_VADDR_SIZE = 1024 chunks).

If a caller passes a byte count (consistent with buddy_alloc()) or a page count to buddy_grow(buddy, size), it will allocate size 1 MiB chunks, and any size greater than 1024 will exhaust BUDDY_VADDR_SIZE and fail with -ENOMEM while leaving the 1024 allocated chunks attached to buddy->first_chunk.

Should buddy_grow() convert a byte size into the number of chunks to allocate, or should the parameter be renamed (for example, to nr_chunks) and documented to make the unit explicit?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author reviewer
#6 lore raw

PATCH 4/4 selftests/bpf: libarena: Make zero variable type __u32 instead of u32

In reply to Emil Tsalapatis

Change 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
index a6220355a697..940e82cc2928 100644
@@ -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
index d859bd538c52..c3c5326d8ca2 100644
@@ -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