summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/call_rcu.c
blob: 505008647fd6f7a1089aa494eb27fba10000e63f (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */

#include <vmlinux.h>
#include <bpf/bpf_helpers.h>

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

/* rh is not at offset 0, so the callback's value recovery is exercised. */
struct elem {
	__u64 pad;
	struct bpf_rcu_head rh;
	__u64 val;
};

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__uint(max_entries, 2);
	__type(key, __u32);
	__type(value, struct elem);
} arr SEC(".maps");

__u32 cb_key;
__u64 cb_keys;
__u64 cb_val;
__u32 cb_max_entries;
int callbacks;
int arm_err;
int busy_err;
int chain;		/* set by userspace: number of times to re-arm from the callback */
int chain_err;

static int reclaim(struct bpf_map *map, void *key, void *value)
{
	struct elem *e = value;

	cb_key = *(__u32 *)key;
	__sync_fetch_and_or(&cb_keys, 1ULL << cb_key);
	cb_val = e->val;
	cb_max_entries = map->max_entries;
	e->val = 0;

	if (chain > 0) {
		chain--;
		chain_err = bpf_call_rcu(&e->rh, &arr, reclaim);
	}

	__sync_fetch_and_add(&callbacks, 1);
	return 0;
}

SEC("syscall")
int arm(void *ctx)
{
	__u32 key = 1;
	struct elem *e;

	e = bpf_map_lookup_elem(&arr, &key);
	if (!e)
		return 1;

	e->val = 0xdeadbeef;
	/* Keep a grace period from elapsing between the two arms. */
	bpf_rcu_read_lock();
	arm_err = bpf_call_rcu(&e->rh, &arr, reclaim);
	busy_err = bpf_call_rcu(&e->rh, &arr, reclaim);
	bpf_rcu_read_unlock();
	return 0;
}

SEC("syscall")
int arm_both(void *ctx)
{
	__u32 key0 = 0, key1 = 1;
	struct elem *e0, *e1;

	e0 = bpf_map_lookup_elem(&arr, &key0);
	e1 = bpf_map_lookup_elem(&arr, &key1);
	if (!e0 || !e1)
		return 1;

	e0->val = 0xdeadbeef;
	e1->val = 0xdeadbeef;
	arm_err = bpf_call_rcu(&e0->rh, &arr, reclaim);
	arm_err |= bpf_call_rcu(&e1->rh, &arr, reclaim);
	return 0;
}

SEC("syscall")
int arm_trace(void *ctx)
{
	__u32 key = 1;
	struct elem *e;

	e = bpf_map_lookup_elem(&arr, &key);
	if (!e)
		return 1;

	e->val = 0xdeadbeef;
	/* No lock needed: the enclosing rcu_read_lock_trace() already blocks the grace period. */
	arm_err = bpf_call_rcu_tasks_trace(&e->rh, &arr, reclaim);
	busy_err = bpf_call_rcu_tasks_trace(&e->rh, &arr, reclaim);
	return 0;
}

SEC("iter/bpf_map_elem")
int dump(struct bpf_iter__bpf_map_elem *ctx)
{
	return 0;
}