summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/res_spin_lock_fail.c
blob: 330682a88c1611b87ee27f4e384b04e95e707536 (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
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2024-2025 Meta Platforms, Inc. and affiliates. */
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include "bpf_misc.h"
#include "bpf_experimental.h"

struct arr_elem {
	struct bpf_res_spin_lock lock;
};

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

long value;

struct bpf_spin_lock lock __hidden SEC(".data.A");
struct bpf_res_spin_lock res_lock __hidden SEC(".data.B");

SEC("?tc")
__failure __msg("point to map value or allocated object")
int res_spin_lock_arg(struct __sk_buff *ctx)
{
	struct arr_elem *elem;

	elem = bpf_map_lookup_elem(&arrmap, &(int){0});
	if (!elem)
		return 0;
	bpf_res_spin_lock((struct bpf_res_spin_lock *)bpf_core_cast(&elem->lock, struct __sk_buff));
	bpf_res_spin_lock(&elem->lock);
	return 0;
}

SEC("?tc")
__failure __msg("AA deadlock detected")
int res_spin_lock_AA(struct __sk_buff *ctx)
{
	struct arr_elem *elem;

	elem = bpf_map_lookup_elem(&arrmap, &(int){0});
	if (!elem)
		return 0;
	bpf_res_spin_lock(&elem->lock);
	bpf_res_spin_lock(&elem->lock);
	return 0;
}

SEC("?tc")
__failure __msg("AA deadlock detected")
int res_spin_lock_cond_AA(struct __sk_buff *ctx)
{
	struct arr_elem *elem;

	elem = bpf_map_lookup_elem(&arrmap, &(int){0});
	if (!elem)
		return 0;
	if (bpf_res_spin_lock(&elem->lock))
		return 0;
	bpf_res_spin_lock(&elem->lock);
	return 0;
}

SEC("?tc")
__failure __msg("unlock of different lock")
int res_spin_lock_mismatch_1(struct __sk_buff *ctx)
{
	struct arr_elem *elem;

	elem = bpf_map_lookup_elem(&arrmap, &(int){0});
	if (!elem)
		return 0;
	if (bpf_res_spin_lock(&elem->lock))
		return 0;
	bpf_res_spin_unlock(&res_lock);
	return 0;
}

SEC("?tc")
__failure __msg("unlock of different lock")
int res_spin_lock_mismatch_2(struct __sk_buff *ctx)
{
	struct arr_elem *elem;

	elem = bpf_map_lookup_elem(&arrmap, &(int){0});
	if (!elem)
		return 0;
	if (bpf_res_spin_lock(&res_lock))
		return 0;
	bpf_res_spin_unlock(&elem->lock);
	return 0;
}

SEC("?tc")
__failure __msg("unlock of different lock")
int res_spin_lock_irq_mismatch_1(struct __sk_buff *ctx)
{
	struct arr_elem *elem;
	unsigned long f1;

	elem = bpf_map_lookup_elem(&arrmap, &(int){0});
	if (!elem)
		return 0;
	bpf_local_irq_save(&f1);
	if (bpf_res_spin_lock(&res_lock))
		return 0;
	bpf_res_spin_unlock_irqrestore(&res_lock, &f1);
	return 0;
}

SEC("?tc")
__failure __msg("unlock of different lock")
int res_spin_lock_irq_mismatch_2(struct __sk_buff *ctx)
{
	struct arr_elem *elem;
	unsigned long f1;

	elem = bpf_map_lookup_elem(&arrmap, &(int){0});
	if (!elem)
		return 0;
	if (bpf_res_spin_lock_irqsave(&res_lock, &f1))
		return 0;
	bpf_res_spin_unlock(&res_lock);
	return 0;
}

SEC("?tc")
__success
int res_spin_lock_ooo(struct __sk_buff *ctx)
{
	struct arr_elem *elem;

	elem = bpf_map_lookup_elem(&arrmap, &(int){0});
	if (!elem)
		return 0;
	if (bpf_res_spin_lock(&res_lock))
		return 0;
	if (bpf_res_spin_lock(&elem->lock)) {
		bpf_res_spin_unlock(&res_lock);
		return 0;
	}
	bpf_res_spin_unlock(&elem->lock);
	bpf_res_spin_unlock(&res_lock);
	return 0;
}

SEC("?tc")
__success
int res_spin_lock_ooo_irq(struct __sk_buff *ctx)
{
	struct arr_elem *elem;
	unsigned long f1, f2;

	elem = bpf_map_lookup_elem(&arrmap, &(int){0});
	if (!elem)
		return 0;
	if (bpf_res_spin_lock_irqsave(&res_lock, &f1))
		return 0;
	if (bpf_res_spin_lock_irqsave(&elem->lock, &f2)) {
		bpf_res_spin_unlock_irqrestore(&res_lock, &f1);
		/* We won't have a unreleased IRQ flag error here. */
		return 0;
	}
	bpf_res_spin_unlock_irqrestore(&elem->lock, &f2);
	bpf_res_spin_unlock_irqrestore(&res_lock, &f1);
	return 0;
}

struct bpf_res_spin_lock lock1 __hidden SEC(".data.OO1");
struct bpf_res_spin_lock lock2 __hidden SEC(".data.OO2");

SEC("?tc")
__failure __msg("bpf_res_spin_unlock cannot be out of order")
int res_spin_lock_ooo_unlock(struct __sk_buff *ctx)
{
	if (bpf_res_spin_lock(&lock1))
		return 0;
	if (bpf_res_spin_lock(&lock2)) {
		bpf_res_spin_unlock(&lock1);
		return 0;
	}
	bpf_res_spin_unlock(&lock1);
	bpf_res_spin_unlock(&lock2);
	return 0;
}

SEC("?tc")
__failure __msg("off 1 doesn't point to 'struct bpf_res_spin_lock' that is at 0")
int res_spin_lock_bad_off(struct __sk_buff *ctx)
{
	struct arr_elem *elem;

	elem = bpf_map_lookup_elem(&arrmap, &(int){0});
	if (!elem)
		return 0;
	bpf_res_spin_lock((void *)&elem->lock + 1);
	return 0;
}

SEC("?tc")
__failure __msg("R1 doesn't have constant offset. bpf_res_spin_lock has to be at the constant offset")
int res_spin_lock_var_off(struct __sk_buff *ctx)
{
	struct arr_elem *elem;
	u64 val = value;

	elem = bpf_map_lookup_elem(&arrmap, &(int){0});
	if (!elem) {
		// FIXME: Only inline assembly use in assert macro doesn't emit
		//	  BTF definition.
		bpf_throw(0);
		return 0;
	}
	bpf_assert_range(val, 0, 40);
	bpf_res_spin_lock((void *)&value + val);
	return 0;
}

SEC("?tc")
__failure __msg("map 'res_spin.bss' has no valid bpf_res_spin_lock")
int res_spin_lock_no_lock_map(struct __sk_buff *ctx)
{
	bpf_res_spin_lock((void *)&value + 1);
	return 0;
}

SEC("?tc")
__failure __msg("local 'kptr' has no valid bpf_res_spin_lock")
int res_spin_lock_no_lock_kptr(struct __sk_buff *ctx)
{
	struct { int i; } *p = bpf_obj_new(typeof(*p));

	if (!p)
		return 0;
	bpf_res_spin_lock((void *)p);
	return 0;
}

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