summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/test_spin_lock_fail.c
blob: 43f40c4fe241ac8bd8faa6a521cdb130b62a1852 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// SPDX-License-Identifier: GPL-2.0
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>
#include "bpf_experimental.h"

struct foo {
	struct bpf_spin_lock lock;
	int data;
};

struct array_map {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__type(key, int);
	__type(value, struct foo);
	__uint(max_entries, 1);
} array_map SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
	__uint(max_entries, 1);
	__type(key, int);
	__type(value, int);
	__array(values, struct array_map);
} map_of_maps SEC(".maps") = {
	.values = {
		[0] = &array_map,
	},
};

SEC(".data.A") struct bpf_spin_lock lockA;
SEC(".data.B") struct bpf_spin_lock lockB;

SEC("?tc")
int lock_id_kptr_preserve(void *ctx)
{
	struct foo *f;

	f = bpf_obj_new(typeof(*f));
	if (!f)
		return 0;
	bpf_this_cpu_ptr(f);
	return 0;
}

SEC("?tc")
int lock_id_global_zero(void *ctx)
{
	bpf_this_cpu_ptr(&lockA);
	return 0;
}

SEC("?tc")
int lock_id_mapval_preserve(void *ctx)
{
	struct foo *f;
	int key = 0;

	f = bpf_map_lookup_elem(&array_map, &key);
	if (!f)
		return 0;
	bpf_this_cpu_ptr(f);
	return 0;
}

SEC("?tc")
int lock_id_innermapval_preserve(void *ctx)
{
	struct foo *f;
	int key = 0;
	void *map;

	map = bpf_map_lookup_elem(&map_of_maps, &key);
	if (!map)
		return 0;
	f = bpf_map_lookup_elem(map, &key);
	if (!f)
		return 0;
	bpf_this_cpu_ptr(f);
	return 0;
}

#define CHECK(test, A, B)                                      \
	SEC("?tc")                                             \
	int lock_id_mismatch_##test(void *ctx)                 \
	{                                                      \
		struct foo *f1, *f2, *v, *iv;                  \
		int key = 0;                                   \
		void *map;                                     \
                                                               \
		map = bpf_map_lookup_elem(&map_of_maps, &key); \
		if (!map)                                      \
			return 0;                              \
		iv = bpf_map_lookup_elem(map, &key);           \
		if (!iv)                                       \
			return 0;                              \
		v = bpf_map_lookup_elem(&array_map, &key);     \
		if (!v)                                        \
			return 0;                              \
		f1 = bpf_obj_new(typeof(*f1));                 \
		if (!f1)                                       \
			return 0;                              \
		f2 = bpf_obj_new(typeof(*f2));                 \
		if (!f2) {                                     \
			bpf_obj_drop(f1);                      \
			return 0;                              \
		}                                              \
		bpf_spin_lock(A);                              \
		bpf_spin_unlock(B);                            \
		return 0;                                      \
	}

CHECK(kptr_kptr, &f1->lock, &f2->lock);
CHECK(kptr_global, &f1->lock, &lockA);
CHECK(kptr_mapval, &f1->lock, &v->lock);
CHECK(kptr_innermapval, &f1->lock, &iv->lock);

CHECK(global_global, &lockA, &lockB);
CHECK(global_kptr, &lockA, &f1->lock);
CHECK(global_mapval, &lockA, &v->lock);
CHECK(global_innermapval, &lockA, &iv->lock);

SEC("?tc")
int lock_id_mismatch_mapval_mapval(void *ctx)
{
	struct foo *f1, *f2;
	int key = 0;

	f1 = bpf_map_lookup_elem(&array_map, &key);
	if (!f1)
		return 0;
	f2 = bpf_map_lookup_elem(&array_map, &key);
	if (!f2)
		return 0;

	bpf_spin_lock(&f1->lock);
	f1->data = 42;
	bpf_spin_unlock(&f2->lock);

	return 0;
}

CHECK(mapval_kptr, &v->lock, &f1->lock);
CHECK(mapval_global, &v->lock, &lockB);
CHECK(mapval_innermapval, &v->lock, &iv->lock);

SEC("?tc")
int lock_id_mismatch_innermapval_innermapval1(void *ctx)
{
	struct foo *f1, *f2;
	int key = 0;
	void *map;

	map = bpf_map_lookup_elem(&map_of_maps, &key);
	if (!map)
		return 0;
	f1 = bpf_map_lookup_elem(map, &key);
	if (!f1)
		return 0;
	f2 = bpf_map_lookup_elem(map, &key);
	if (!f2)
		return 0;

	bpf_spin_lock(&f1->lock);
	f1->data = 42;
	bpf_spin_unlock(&f2->lock);

	return 0;
}

SEC("?tc")
int lock_id_mismatch_innermapval_innermapval2(void *ctx)
{
	struct foo *f1, *f2;
	int key = 0;
	void *map;

	map = bpf_map_lookup_elem(&map_of_maps, &key);
	if (!map)
		return 0;
	f1 = bpf_map_lookup_elem(map, &key);
	if (!f1)
		return 0;
	map = bpf_map_lookup_elem(&map_of_maps, &key);
	if (!map)
		return 0;
	f2 = bpf_map_lookup_elem(map, &key);
	if (!f2)
		return 0;

	bpf_spin_lock(&f1->lock);
	f1->data = 42;
	bpf_spin_unlock(&f2->lock);

	return 0;
}

CHECK(innermapval_kptr, &iv->lock, &f1->lock);
CHECK(innermapval_global, &iv->lock, &lockA);
CHECK(innermapval_mapval, &iv->lock, &v->lock);

#undef CHECK

__noinline
int global_subprog(struct __sk_buff *ctx)
{
	volatile int ret = 0;

	if (ctx->protocol)
		ret += ctx->protocol;
	return ret + ctx->mark;
}

__noinline
static int static_subprog_call_global(struct __sk_buff *ctx)
{
	volatile int ret = 0;

	if (ctx->protocol)
		return ret;
	return ret + ctx->len + global_subprog(ctx);
}

SEC("?tc")
int lock_global_subprog_call1(struct __sk_buff *ctx)
{
	int ret = 0;

	bpf_spin_lock(&lockA);
	if (ctx->mark == 42)
		ret = global_subprog(ctx);
	bpf_spin_unlock(&lockA);
	return ret;
}

SEC("?tc")
int lock_global_subprog_call2(struct __sk_buff *ctx)
{
	int ret = 0;

	bpf_spin_lock(&lockA);
	if (ctx->mark == 42)
		ret = static_subprog_call_global(ctx);
	bpf_spin_unlock(&lockA);
	return ret;
}

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