summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/test_helper_restricted.c
blob: 5715c569ec0395ef1cbeee6b06c1ad45f239539a (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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
// SPDX-License-Identifier: GPL-2.0-only
#include <time.h>
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

struct timer {
	struct bpf_timer t;
};

struct lock {
	struct bpf_spin_lock l;
};

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__uint(max_entries, 1);
	__type(key, __u32);
	__type(value, struct timer);
} timers SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__uint(max_entries, 1);
	__type(key, __u32);
	__type(value, struct lock);
} locks SEC(".maps");

static int timer_cb(void *map, int *key, struct timer *timer)
{
	return 0;
}

static void timer_work(void)
{
	struct timer *timer;
	const int key = 0;

	timer  = bpf_map_lookup_elem(&timers, &key);
	if (timer) {
		bpf_timer_init(&timer->t, &timers, CLOCK_MONOTONIC);
		bpf_timer_set_callback(&timer->t, timer_cb);
		bpf_timer_start(&timer->t, 10E9, 0);
		bpf_timer_cancel(&timer->t);
	}
}

static void spin_lock_work(void)
{
	const int key = 0;
	struct lock *lock;

	lock = bpf_map_lookup_elem(&locks, &key);
	if (lock) {
		bpf_spin_lock(&lock->l);
		bpf_spin_unlock(&lock->l);
	}
}

SEC("?raw_tp/sys_enter")
int raw_tp_timer(void *ctx)
{
	timer_work();

	return 0;
}

SEC("?tp/syscalls/sys_enter_nanosleep")
int tp_timer(void *ctx)
{
	timer_work();

	return 0;
}

SEC("?kprobe")
int kprobe_timer(void *ctx)
{
	timer_work();

	return 0;
}

SEC("?perf_event")
int perf_event_timer(void *ctx)
{
	timer_work();

	return 0;
}

SEC("?raw_tp/sys_enter")
int raw_tp_spin_lock(void *ctx)
{
	spin_lock_work();

	return 0;
}

SEC("?tp/syscalls/sys_enter_nanosleep")
int tp_spin_lock(void *ctx)
{
	spin_lock_work();

	return 0;
}

SEC("?kprobe")
int kprobe_spin_lock(void *ctx)
{
	spin_lock_work();

	return 0;
}

SEC("?perf_event")
int perf_event_spin_lock(void *ctx)
{
	spin_lock_work();

	return 0;
}

const char LICENSE[] SEC("license") = "GPL";