summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/percpu_alloc_fail.c
blob: f2b8eb2ff76f7a98a088ae20b4f8afc3b98ab941 (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
#include "bpf_experimental.h"
#include "bpf_misc.h"

struct val_t {
	long b, c, d;
};

struct val2_t {
	long b;
};

struct val_with_ptr_t {
	char *p;
};

struct val_with_rb_root_t {
	struct bpf_spin_lock lock;
};

struct val_600b_t {
	char b[600];
};

struct elem {
	long sum;
	struct val_t __percpu_kptr *pc;
};

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

long ret;

SEC("?fentry/bpf_fentry_test1")
__failure __msg("store to referenced kptr disallowed")
int BPF_PROG(test_array_map_1)
{
	struct val_t __percpu_kptr *p;
	struct elem *e;
	int index = 0;

	e = bpf_map_lookup_elem(&array, &index);
	if (!e)
		return 0;

	p = bpf_percpu_obj_new(struct val_t);
	if (!p)
		return 0;

	p = bpf_kptr_xchg(&e->pc, p);
	if (p)
		bpf_percpu_obj_drop(p);

	e->pc = (struct val_t __percpu_kptr *)ret;
	return 0;
}

SEC("?fentry/bpf_fentry_test1")
__failure __msg("invalid kptr access, R2 type=percpu_ptr_val2_t expected=ptr_val_t")
int BPF_PROG(test_array_map_2)
{
	struct val2_t __percpu_kptr *p2;
	struct val_t __percpu_kptr *p;
	struct elem *e;
	int index = 0;

	e = bpf_map_lookup_elem(&array, &index);
	if (!e)
		return 0;

	p2 = bpf_percpu_obj_new(struct val2_t);
	if (!p2)
		return 0;

	p = bpf_kptr_xchg(&e->pc, p2);
	if (p)
		bpf_percpu_obj_drop(p);

	return 0;
}

SEC("?fentry.s/bpf_fentry_test1")
__failure __msg("R1 type=scalar expected=percpu_ptr_, percpu_rcu_ptr_, percpu_trusted_ptr_")
int BPF_PROG(test_array_map_3)
{
	struct val_t __percpu_kptr *p, *p1;
	struct val_t *v;
	struct elem *e;
	int index = 0;

	e = bpf_map_lookup_elem(&array, &index);
	if (!e)
		return 0;

	p = bpf_percpu_obj_new(struct val_t);
	if (!p)
		return 0;

	p1 = bpf_kptr_xchg(&e->pc, p);
	if (p1)
		bpf_percpu_obj_drop(p1);

	v = bpf_this_cpu_ptr(p);
	ret = v->b;
	return 0;
}

SEC("?fentry.s/bpf_fentry_test1")
__failure __msg("arg#0 expected for bpf_percpu_obj_drop_impl()")
int BPF_PROG(test_array_map_4)
{
	struct val_t __percpu_kptr *p;

	p = bpf_percpu_obj_new(struct val_t);
	if (!p)
		return 0;

	bpf_obj_drop(p);
	return 0;
}

SEC("?fentry.s/bpf_fentry_test1")
__failure __msg("arg#0 expected for bpf_obj_drop_impl()")
int BPF_PROG(test_array_map_5)
{
	struct val_t *p;

	p = bpf_obj_new(struct val_t);
	if (!p)
		return 0;

	bpf_percpu_obj_drop(p);
	return 0;
}

SEC("?fentry.s/bpf_fentry_test1")
__failure __msg("bpf_percpu_obj_new type ID argument must be of a struct of scalars")
int BPF_PROG(test_array_map_6)
{
	struct val_with_ptr_t __percpu_kptr *p;

	p = bpf_percpu_obj_new(struct val_with_ptr_t);
	if (!p)
		return 0;

	bpf_percpu_obj_drop(p);
	return 0;
}

SEC("?fentry.s/bpf_fentry_test1")
__failure __msg("bpf_percpu_obj_new type ID argument must not contain special fields")
int BPF_PROG(test_array_map_7)
{
	struct val_with_rb_root_t __percpu_kptr *p;

	p = bpf_percpu_obj_new(struct val_with_rb_root_t);
	if (!p)
		return 0;

	bpf_percpu_obj_drop(p);
	return 0;
}

SEC("?fentry.s/bpf_fentry_test1")
__failure __msg("bpf_percpu_obj_new type size (600) is greater than 512")
int BPF_PROG(test_array_map_8)
{
	struct val_600b_t __percpu_kptr *p;

	p = bpf_percpu_obj_new(struct val_600b_t);
	if (!p)
		return 0;

	bpf_percpu_obj_drop(p);
	return 0;
}

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