summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/libarena/src/common.bpf.c
blob: d1af60b81838948f566c9714074011c85253a4a9 (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <limits.h>

#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 *arena_calloc(size_t ncount, size_t size)
{
	void __arena *mem;
	size_t total;

	/*
	 * Ideally we'd be using __builtin_mul_overflow here,
	 * but the BPF compiler backend doesn't implement __multi3.
	 * There are ways to optimize the division away from the
	 * overflow check, but any costs are dwarfed by the
	 * buddy_alloc() call. Keep it simple for now.
	 */
	if (unlikely(ncount && size >= ULLONG_MAX / ncount))
		return NULL;

	total = ncount * size;

	mem = buddy_alloc(&buddy, total);
	if (likely(mem))
		arena_memset(mem, 0, total);

	return mem;
}

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

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