Skip to content

Commit 367e6e4

Browse files
etsalAlexei Starovoitov
authored andcommitted
selftests/bpf: libarena: Directly return arena pointers from functions
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>
1 parent b9b23fe commit 367e6e4

4 files changed

Lines changed: 14 additions & 16 deletions

File tree

tools/testing/selftests/bpf/libarena/include/libarena/buddy.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ struct buddy {
7676
int buddy_init(struct buddy __arena *buddy);
7777
int buddy_destroy(struct buddy __arena *buddy);
7878
int buddy_free(struct buddy __arena *buddy, void __arena *free);
79-
u64 buddy_alloc_internal(struct buddy __arena *buddy, size_t size);
80-
#define buddy_alloc(alloc, size) ((void __arena *)buddy_alloc_internal((alloc), (size)))
79+
void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size);
8180

8281
#endif /* __BPF__ */

tools/testing/selftests/bpf/libarena/include/libarena/common.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ extern volatile u64 asan_violated;
4848

4949
int arena_fls(__u64 word);
5050

51-
u64 arena_malloc_internal(size_t size);
52-
#define arena_malloc(size) ((void __arena *)arena_malloc_internal((size)))
51+
void __arena *arena_malloc(size_t size);
5352
void arena_free(void __arena *ptr);
5453

5554
/*

tools/testing/selftests/bpf/libarena/src/buddy.bpf.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -750,49 +750,49 @@ static u64 buddy_alloc_from_new_chunk(struct buddy __arena *buddy, struct buddy_
750750
return (u64)address;
751751
}
752752
__weak
753-
u64 buddy_alloc_internal(struct buddy __arena *buddy, size_t size)
753+
void __arena *buddy_alloc(struct buddy __arena *buddy, size_t size)
754754
{
755-
u64 address = (u64)NULL;
755+
void __arena *address = NULL;
756756
struct buddy_chunk __arena *chunk;
757757
int order;
758758

759759
if (!buddy)
760-
return (u64)NULL;
760+
return NULL;
761761

762762
order = size_to_order(size);
763763
if (order >= BUDDY_CHUNK_NUM_ORDERS || order < 0) {
764764
arena_stderr("invalid order %d (sz %lu)\n", order, size);
765-
return (u64)NULL;
765+
return NULL;
766766
}
767767

768768
if (buddy_lock(buddy))
769-
return (u64)NULL;
769+
return NULL;
770770

771-
address = buddy_alloc_from_existing_chunks(buddy, order);
771+
address = (u8 __arena *)buddy_alloc_from_existing_chunks(buddy, order);
772772
buddy_unlock(buddy);
773773
if (address)
774774
goto done;
775775

776776
/* Get a new chunk. */
777777
chunk = buddy_chunk_get(buddy);
778778
if (chunk)
779-
address = buddy_alloc_from_new_chunk(buddy, chunk, order);
779+
address = (u8 __arena *)buddy_alloc_from_new_chunk(buddy, chunk, order);
780780

781781
done:
782782
/* If we failed to allocate memory, return NULL. */
783783
if (!address)
784-
return (u64)NULL;
784+
return NULL;
785785

786786
/*
787787
* Unpoison exactly the amount of bytes requested. If the
788788
* data is smaller than the header, we must poison any
789789
* unused bytes that were part of the header.
790790
*/
791791
if (size < BUDDY_HEADER_OFF + sizeof(struct buddy_header __arena))
792-
asan_poison((u8 __arena *)address + BUDDY_HEADER_OFF, BUDDY_POISONED,
792+
asan_poison(address + BUDDY_HEADER_OFF, BUDDY_POISONED,
793793
sizeof(struct buddy_header __arena));
794794

795-
asan_unpoison((u8 __arena *)address, size);
795+
asan_unpoison(address, size);
796796

797797
return address;
798798
}

tools/testing/selftests/bpf/libarena/src/common.bpf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ __weak int arena_buddy_reset(void)
3838
return buddy_init(&buddy);
3939
}
4040

41-
__weak u64 arena_malloc_internal(size_t size)
41+
__weak void __arena *arena_malloc(size_t size)
4242
{
43-
return buddy_alloc_internal(&buddy, size);
43+
return buddy_alloc(&buddy, size);
4444
}
4545

4646
__weak void arena_free(void __arena *ptr)

0 commit comments

Comments
 (0)