aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/libarena/src/common.bpf.c
blob: 41b1de3452fe83921eabb5379633adf107f1db13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <libarena/common.h>
#include <libarena/asan.h>
#include <libarena/buddy.h>

struct buddy __arena buddy;
volatile u32 zero = 0;

/*
 * Storage for the queue nodes declared by bpf_arena_spin_lock.h. Each program
 * linking the arena spinlock provides exactly one definition, so that the array
 * is emitted once rather than once per translation unit.
 */
struct arena_qnode __arena __hidden qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];

int arena_fls(__u64 word)
{
	if (!word)
		return 0;

	return 64 - __builtin_clzll(word);
}

SEC("syscall")
__weak int arena_get_info(struct arena_get_info_args *args)
{
	args->arena_base = arena_base(&arena);

	return 0;
}

SEC("syscall")
__weak int arena_alloc_reserve(struct arena_alloc_reserve_args *args)
{
	return bpf_arena_reserve_pages(&arena, NULL, args->nr_pages);
}

SEC("syscall")
__weak int arena_buddy_reset(void)
{
	buddy_destroy(&buddy);

	return buddy_init(&buddy);
}

SEC("syscall")
__weak int arena_buddy_destroy(void)
{
	return buddy_destroy(&buddy);
}

__weak void __arena *arena_malloc(size_t size)
{
	return buddy_alloc(&buddy, size);
}

__weak void arena_free(void __arena *ptr)
{
	buddy_free(&buddy, ptr);
}


char _license[] SEC("license") = "GPL";