]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: libarena: Directly return arena pointers from functions
authorEmil Tsalapatis <emil@etsalapatis.com>
Tue, 2 Jun 2026 00:41:19 +0000 (20:41 -0400)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 2 Jun 2026 01:42:33 +0000 (18:42 -0700)
Now that the __arena annotation includes a BTF type tag, and the
verifier can identify arena pointers at BTF loading time, return
arena pointers as their true type instead of casting to u64. Remove the
preprocessor typecast wrappers used to hide this from the caller.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Emil Tsalapatis <emil@etsalapatis.com>
Link: https://lore.kernel.org/r/20260602004120.17087-6-emil@etsalapatis.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/libarena/include/libarena/buddy.h
tools/testing/selftests/bpf/libarena/include/libarena/common.h
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
tools/testing/selftests/bpf/libarena/src/common.bpf.c

index 4d57fc1b5c26f367e74e3135f6dd434f0d4aa6d2..528c69a1f38e0c175b3d8002dffd3159c9c4b776 100644 (file)
@@ -76,7 +76,6 @@ 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);
-u64 buddy_alloc_internal(struct buddy __arena *buddy, size_t size);
-#define buddy_alloc(alloc, size) ((void __arena *)buddy_alloc_internal((alloc), (size)))
+void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size);
 
 #endif /* __BPF__  */
index ca1a6c1d6477343e687f39ac125dd110d39ccb42..a3eb1641ac36dfce0e3669aedc597b3a02529eef 100644 (file)
@@ -48,8 +48,7 @@ extern volatile u64 asan_violated;
 
 int arena_fls(__u64 word);
 
-u64 arena_malloc_internal(size_t size);
-#define arena_malloc(size) ((void __arena *)arena_malloc_internal((size)))
+void __arena *arena_malloc(size_t size);
 void arena_free(void __arena *ptr);
 
 /*
index f4ed4c3abb4b026356a86fd50e041d816bfbc8c6..c674ee5cfcc1d4ef9a5d2c1d6db4fc7350d6fb71 100644 (file)
@@ -750,25 +750,25 @@ static u64 buddy_alloc_from_new_chunk(struct buddy __arena *buddy, struct buddy_
        return (u64)address;
 }
 __weak
-u64 buddy_alloc_internal(struct buddy __arena *buddy, size_t size)
+void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size)
 {
-       u64 address = (u64)NULL;
+       void __arena *address = NULL;
        struct buddy_chunk __arena *chunk;
        int order;
 
        if (!buddy)
-               return (u64)NULL;
+               return NULL;
 
        order = size_to_order(size);
        if (order >= BUDDY_CHUNK_NUM_ORDERS || order < 0) {
                arena_stderr("invalid order %d (sz %lu)\n", order, size);
-               return (u64)NULL;
+               return NULL;
        }
 
        if (buddy_lock(buddy))
-               return (u64)NULL;
+               return NULL;
 
-       address = buddy_alloc_from_existing_chunks(buddy, order);
+       address = (u8 __arena *)buddy_alloc_from_existing_chunks(buddy, order);
        buddy_unlock(buddy);
        if (address)
                goto done;
@@ -776,12 +776,12 @@ u64 buddy_alloc_internal(struct buddy __arena *buddy, size_t size)
        /* Get a new chunk. */
        chunk = buddy_chunk_get(buddy);
        if (chunk)
-               address = buddy_alloc_from_new_chunk(buddy, chunk, order);
+               address = (u8 __arena *)buddy_alloc_from_new_chunk(buddy, chunk, order);
 
 done:
        /* If we failed to allocate memory, return NULL. */
        if (!address)
-               return (u64)NULL;
+               return NULL;
 
        /*
         * Unpoison exactly the amount of bytes requested. If the
@@ -789,10 +789,10 @@ done:
         * unused bytes that were part of the header.
         */
        if (size < BUDDY_HEADER_OFF + sizeof(struct buddy_header __arena))
-               asan_poison((u8 __arena *)address + BUDDY_HEADER_OFF, BUDDY_POISONED,
+               asan_poison(address + BUDDY_HEADER_OFF, BUDDY_POISONED,
                            sizeof(struct buddy_header __arena));
 
-       asan_unpoison((u8 __arena *)address, size);
+       asan_unpoison(address, size);
 
        return address;
 }
index ec9de29e6f3ee9a3890f55babeb07b913cbe5aa2..50be57213dfb8db2a5a59aa9a0529d743d2e1b16 100644 (file)
@@ -38,9 +38,9 @@ __weak int arena_buddy_reset(void)
        return buddy_init(&buddy);
 }
 
-__weak u64 arena_malloc_internal(size_t size)
+__weak void __arena *arena_malloc(size_t size)
 {
-       return buddy_alloc_internal(&buddy, size);
+       return buddy_alloc(&buddy, size);
 }
 
 __weak void arena_free(void __arena *ptr)