1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * queue_stack_maps.c: BPF queue and stack maps
4 *
5 * Copyright (c) 2018 Politecnico di Torino
6 */
7 #include <linux/bpf.h>
8 #include <linux/list.h>
9 #include <linux/slab.h>
10 #include <linux/btf_ids.h>
11 #include "percpu_freelist.h"
12 #include <asm/rqspinlock.h>
13
14 #define QUEUE_STACK_CREATE_FLAG_MASK \
15 (BPF_F_NUMA_NODE | BPF_F_ACCESS_MASK)
16
17 struct bpf_queue_stack {
18 struct bpf_map map;
19 rqspinlock_t lock;
20 u32 head, tail;
21 u32 size; /* max_entries + 1 */
22
23 char elements[] __aligned(8);
24 };
25
bpf_queue_stack(struct bpf_map * map)26 static struct bpf_queue_stack *bpf_queue_stack(struct bpf_map *map)
27 {
28 return container_of(map, struct bpf_queue_stack, map);
29 }
30
queue_stack_map_is_empty(struct bpf_queue_stack * qs)31 static bool queue_stack_map_is_empty(struct bpf_queue_stack *qs)
32 {
33 return qs->head == qs->tail;
34 }
35
queue_stack_map_is_full(struct bpf_queue_stack * qs)36 static bool queue_stack_map_is_full(struct bpf_queue_stack *qs)
37 {
38 u32 head = qs->head + 1;
39
40 if (unlikely(head >= qs->size))
41 head = 0;
42
43 return head == qs->tail;
44 }
45
46 /* Called from syscall */
queue_stack_map_alloc_check(union bpf_attr * attr)47 static int queue_stack_map_alloc_check(union bpf_attr *attr)
48 {
49 /* check sanity of attributes */
50 if (attr->max_entries == 0 || attr->key_size != 0 ||
51 attr->value_size == 0 ||
52 attr->map_flags & ~QUEUE_STACK_CREATE_FLAG_MASK ||
53 !bpf_map_flags_access_ok(attr->map_flags))
54 return -EINVAL;
55
56 if (attr->value_size > KMALLOC_MAX_SIZE)
57 /* if value_size is bigger, the user space won't be able to
58 * access the elements.
59 */
60 return -E2BIG;
61
62 return 0;
63 }
64
queue_stack_map_alloc(union bpf_attr * attr)65 static struct bpf_map *queue_stack_map_alloc(union bpf_attr *attr)
66 {
67 int numa_node = bpf_map_attr_numa_node(attr);
68 struct bpf_queue_stack *qs;
69 u64 size, queue_size;
70
71 size = (u64) attr->max_entries + 1;
72 queue_size = sizeof(*qs) + size * attr->value_size;
73
74 qs = bpf_map_area_alloc(queue_size, numa_node);
75 if (!qs)
76 return ERR_PTR(-ENOMEM);
77
78 bpf_map_init_from_attr(&qs->map, attr);
79
80 qs->size = size;
81
82 raw_res_spin_lock_init(&qs->lock);
83
84 return &qs->map;
85 }
86
87 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
queue_stack_map_free(struct bpf_map * map)88 static void queue_stack_map_free(struct bpf_map *map)
89 {
90 struct bpf_queue_stack *qs = bpf_queue_stack(map);
91
92 bpf_map_area_free(qs);
93 }
94
__queue_map_get(struct bpf_map * map,void * value,bool delete)95 static long __queue_map_get(struct bpf_map *map, void *value, bool delete)
96 {
97 struct bpf_queue_stack *qs = bpf_queue_stack(map);
98 unsigned long flags;
99 int err = 0;
100 void *ptr;
101
102 if (raw_res_spin_lock_irqsave(&qs->lock, flags)) {
103 memset(value, 0, qs->map.value_size);
104 return -EBUSY;
105 }
106
107 if (queue_stack_map_is_empty(qs)) {
108 memset(value, 0, qs->map.value_size);
109 err = -ENOENT;
110 goto out;
111 }
112
113 ptr = &qs->elements[qs->tail * qs->map.value_size];
114 memcpy(value, ptr, qs->map.value_size);
115
116 if (delete) {
117 if (unlikely(++qs->tail >= qs->size))
118 qs->tail = 0;
119 }
120
121 out:
122 raw_res_spin_unlock_irqrestore(&qs->lock, flags);
123 return err;
124 }
125
__stack_map_get(struct bpf_map * map,void * value,bool delete)126 static long __stack_map_get(struct bpf_map *map, void *value, bool delete)
127 {
128 struct bpf_queue_stack *qs = bpf_queue_stack(map);
129 unsigned long flags;
130 int err = 0;
131 void *ptr;
132 u32 index;
133
134 if (raw_res_spin_lock_irqsave(&qs->lock, flags)) {
135 memset(value, 0, qs->map.value_size);
136 return -EBUSY;
137 }
138
139 if (queue_stack_map_is_empty(qs)) {
140 memset(value, 0, qs->map.value_size);
141 err = -ENOENT;
142 goto out;
143 }
144
145 index = qs->head - 1;
146 if (unlikely(index >= qs->size))
147 index = qs->size - 1;
148
149 ptr = &qs->elements[index * qs->map.value_size];
150 memcpy(value, ptr, qs->map.value_size);
151
152 if (delete)
153 qs->head = index;
154
155 out:
156 raw_res_spin_unlock_irqrestore(&qs->lock, flags);
157 return err;
158 }
159
160 /* Called from syscall or from eBPF program */
queue_map_peek_elem(struct bpf_map * map,void * value)161 static long queue_map_peek_elem(struct bpf_map *map, void *value)
162 {
163 return __queue_map_get(map, value, false);
164 }
165
166 /* Called from syscall or from eBPF program */
stack_map_peek_elem(struct bpf_map * map,void * value)167 static long stack_map_peek_elem(struct bpf_map *map, void *value)
168 {
169 return __stack_map_get(map, value, false);
170 }
171
172 /* Called from syscall or from eBPF program */
queue_map_pop_elem(struct bpf_map * map,void * value)173 static long queue_map_pop_elem(struct bpf_map *map, void *value)
174 {
175 return __queue_map_get(map, value, true);
176 }
177
178 /* Called from syscall or from eBPF program */
stack_map_pop_elem(struct bpf_map * map,void * value)179 static long stack_map_pop_elem(struct bpf_map *map, void *value)
180 {
181 return __stack_map_get(map, value, true);
182 }
183
184 /* Called from syscall or from eBPF program */
queue_stack_map_push_elem(struct bpf_map * map,void * value,u64 flags)185 static long queue_stack_map_push_elem(struct bpf_map *map, void *value,
186 u64 flags)
187 {
188 struct bpf_queue_stack *qs = bpf_queue_stack(map);
189 unsigned long irq_flags;
190 int err = 0;
191 void *dst;
192
193 /* BPF_EXIST is used to force making room for a new element in case the
194 * map is full
195 */
196 bool replace = (flags & BPF_EXIST);
197
198 /* Check supported flags for queue and stack maps */
199 if (flags & BPF_NOEXIST || flags > BPF_EXIST)
200 return -EINVAL;
201
202 if (raw_res_spin_lock_irqsave(&qs->lock, irq_flags))
203 return -EBUSY;
204
205 if (queue_stack_map_is_full(qs)) {
206 if (!replace) {
207 err = -E2BIG;
208 goto out;
209 }
210 /* advance tail pointer to overwrite oldest element */
211 if (unlikely(++qs->tail >= qs->size))
212 qs->tail = 0;
213 }
214
215 dst = &qs->elements[qs->head * qs->map.value_size];
216 memcpy(dst, value, qs->map.value_size);
217
218 if (unlikely(++qs->head >= qs->size))
219 qs->head = 0;
220
221 out:
222 raw_res_spin_unlock_irqrestore(&qs->lock, irq_flags);
223 return err;
224 }
225
226 /* Called from syscall or from eBPF program */
queue_stack_map_lookup_elem(struct bpf_map * map,void * key)227 static void *queue_stack_map_lookup_elem(struct bpf_map *map, void *key)
228 {
229 return NULL;
230 }
231
232 /* Called from syscall or from eBPF program */
queue_stack_map_update_elem(struct bpf_map * map,void * key,void * value,u64 flags)233 static long queue_stack_map_update_elem(struct bpf_map *map, void *key,
234 void *value, u64 flags)
235 {
236 return -EINVAL;
237 }
238
239 /* Called from syscall or from eBPF program */
queue_stack_map_delete_elem(struct bpf_map * map,void * key)240 static long queue_stack_map_delete_elem(struct bpf_map *map, void *key)
241 {
242 return -EINVAL;
243 }
244
245 /* Called from syscall */
queue_stack_map_get_next_key(struct bpf_map * map,void * key,void * next_key)246 static int queue_stack_map_get_next_key(struct bpf_map *map, void *key,
247 void *next_key)
248 {
249 return -EINVAL;
250 }
251
queue_stack_map_mem_usage(const struct bpf_map * map)252 static u64 queue_stack_map_mem_usage(const struct bpf_map *map)
253 {
254 u64 usage = sizeof(struct bpf_queue_stack);
255
256 usage += ((u64)map->max_entries + 1) * map->value_size;
257 return usage;
258 }
259
260 BTF_ID_LIST_SINGLE(queue_map_btf_ids, struct, bpf_queue_stack)
261 const struct bpf_map_ops queue_map_ops = {
262 .map_meta_equal = bpf_map_meta_equal,
263 .map_alloc_check = queue_stack_map_alloc_check,
264 .map_alloc = queue_stack_map_alloc,
265 .map_free = queue_stack_map_free,
266 .map_lookup_elem = queue_stack_map_lookup_elem,
267 .map_update_elem = queue_stack_map_update_elem,
268 .map_delete_elem = queue_stack_map_delete_elem,
269 .map_push_elem = queue_stack_map_push_elem,
270 .map_pop_elem = queue_map_pop_elem,
271 .map_peek_elem = queue_map_peek_elem,
272 .map_get_next_key = queue_stack_map_get_next_key,
273 .map_mem_usage = queue_stack_map_mem_usage,
274 .map_btf_id = &queue_map_btf_ids[0],
275 };
276
277 const struct bpf_map_ops stack_map_ops = {
278 .map_meta_equal = bpf_map_meta_equal,
279 .map_alloc_check = queue_stack_map_alloc_check,
280 .map_alloc = queue_stack_map_alloc,
281 .map_free = queue_stack_map_free,
282 .map_lookup_elem = queue_stack_map_lookup_elem,
283 .map_update_elem = queue_stack_map_update_elem,
284 .map_delete_elem = queue_stack_map_delete_elem,
285 .map_push_elem = queue_stack_map_push_elem,
286 .map_pop_elem = stack_map_pop_elem,
287 .map_peek_elem = stack_map_peek_elem,
288 .map_get_next_key = queue_stack_map_get_next_key,
289 .map_mem_usage = queue_stack_map_mem_usage,
290 .map_btf_id = &queue_map_btf_ids[0],
291 };
292