1 // SPDX-License-Identifier: GPL-2.0
3 * queue_stack_maps.c: BPF queue and stack maps
5 * Copyright (c) 2018 Politecnico di Torino
8 #include <linux/list.h>
9 #include <linux/slab.h>
10 #include <linux/btf_ids.h>
11 #include "percpu_freelist.h"
13 #define QUEUE_STACK_CREATE_FLAG_MASK \
14 (BPF_F_NUMA_NODE | BPF_F_ACCESS_MASK)
16 struct bpf_queue_stack {
20 u32 size; /* max_entries + 1 */
22 char elements[] __aligned(8);
25 static struct bpf_queue_stack *bpf_queue_stack(struct bpf_map *map)
27 return container_of(map, struct bpf_queue_stack, map);
30 static bool queue_stack_map_is_empty(struct bpf_queue_stack *qs)
32 return qs->head == qs->tail;
35 static bool queue_stack_map_is_full(struct bpf_queue_stack *qs)
37 u32 head = qs->head + 1;
39 if (unlikely(head >= qs->size))
42 return head == qs->tail;
45 /* Called from syscall */
46 static int queue_stack_map_alloc_check(union bpf_attr *attr)
48 /* check sanity of attributes */
49 if (attr->max_entries == 0 || attr->key_size != 0 ||
50 attr->value_size == 0 ||
51 attr->map_flags & ~QUEUE_STACK_CREATE_FLAG_MASK ||
52 !bpf_map_flags_access_ok(attr->map_flags))
55 if (attr->value_size > KMALLOC_MAX_SIZE)
56 /* if value_size is bigger, the user space won't be able to
57 * access the elements.
64 static struct bpf_map *queue_stack_map_alloc(union bpf_attr *attr)
66 int numa_node = bpf_map_attr_numa_node(attr);
67 struct bpf_queue_stack *qs;
70 size = (u64) attr->max_entries + 1;
71 queue_size = sizeof(*qs) + size * attr->value_size;
73 qs = bpf_map_area_alloc(queue_size, numa_node);
75 return ERR_PTR(-ENOMEM);
77 bpf_map_init_from_attr(&qs->map, attr);
81 raw_spin_lock_init(&qs->lock);
86 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
87 static void queue_stack_map_free(struct bpf_map *map)
89 struct bpf_queue_stack *qs = bpf_queue_stack(map);
91 bpf_map_area_free(qs);
94 static long __queue_map_get(struct bpf_map *map, void *value, bool delete)
96 struct bpf_queue_stack *qs = bpf_queue_stack(map);
102 if (!raw_spin_trylock_irqsave(&qs->lock, flags))
105 raw_spin_lock_irqsave(&qs->lock, flags);
108 if (queue_stack_map_is_empty(qs)) {
109 memset(value, 0, qs->map.value_size);
114 ptr = &qs->elements[qs->tail * qs->map.value_size];
115 memcpy(value, ptr, qs->map.value_size);
118 if (unlikely(++qs->tail >= qs->size))
123 raw_spin_unlock_irqrestore(&qs->lock, flags);
128 static long __stack_map_get(struct bpf_map *map, void *value, bool delete)
130 struct bpf_queue_stack *qs = bpf_queue_stack(map);
137 if (!raw_spin_trylock_irqsave(&qs->lock, flags))
140 raw_spin_lock_irqsave(&qs->lock, flags);
143 if (queue_stack_map_is_empty(qs)) {
144 memset(value, 0, qs->map.value_size);
149 index = qs->head - 1;
150 if (unlikely(index >= qs->size))
151 index = qs->size - 1;
153 ptr = &qs->elements[index * qs->map.value_size];
154 memcpy(value, ptr, qs->map.value_size);
160 raw_spin_unlock_irqrestore(&qs->lock, flags);
164 /* Called from syscall or from eBPF program */
165 static long queue_map_peek_elem(struct bpf_map *map, void *value)
167 return __queue_map_get(map, value, false);
170 /* Called from syscall or from eBPF program */
171 static long stack_map_peek_elem(struct bpf_map *map, void *value)
173 return __stack_map_get(map, value, false);
176 /* Called from syscall or from eBPF program */
177 static long queue_map_pop_elem(struct bpf_map *map, void *value)
179 return __queue_map_get(map, value, true);
182 /* Called from syscall or from eBPF program */
183 static long stack_map_pop_elem(struct bpf_map *map, void *value)
185 return __stack_map_get(map, value, true);
188 /* Called from syscall or from eBPF program */
189 static long queue_stack_map_push_elem(struct bpf_map *map, void *value,
192 struct bpf_queue_stack *qs = bpf_queue_stack(map);
193 unsigned long irq_flags;
197 /* BPF_EXIST is used to force making room for a new element in case the
200 bool replace = (flags & BPF_EXIST);
202 /* Check supported flags for queue and stack maps */
203 if (flags & BPF_NOEXIST || flags > BPF_EXIST)
207 if (!raw_spin_trylock_irqsave(&qs->lock, irq_flags))
210 raw_spin_lock_irqsave(&qs->lock, irq_flags);
213 if (queue_stack_map_is_full(qs)) {
218 /* advance tail pointer to overwrite oldest element */
219 if (unlikely(++qs->tail >= qs->size))
223 dst = &qs->elements[qs->head * qs->map.value_size];
224 memcpy(dst, value, qs->map.value_size);
226 if (unlikely(++qs->head >= qs->size))
230 raw_spin_unlock_irqrestore(&qs->lock, irq_flags);
234 /* Called from syscall or from eBPF program */
235 static void *queue_stack_map_lookup_elem(struct bpf_map *map, void *key)
240 /* Called from syscall or from eBPF program */
241 static long queue_stack_map_update_elem(struct bpf_map *map, void *key,
242 void *value, u64 flags)
247 /* Called from syscall or from eBPF program */
248 static long queue_stack_map_delete_elem(struct bpf_map *map, void *key)
253 /* Called from syscall */
254 static int queue_stack_map_get_next_key(struct bpf_map *map, void *key,
260 static u64 queue_stack_map_mem_usage(const struct bpf_map *map)
262 u64 usage = sizeof(struct bpf_queue_stack);
264 usage += ((u64)map->max_entries + 1) * map->value_size;
268 BTF_ID_LIST_SINGLE(queue_map_btf_ids, struct, bpf_queue_stack)
269 const struct bpf_map_ops queue_map_ops = {
270 .map_meta_equal = bpf_map_meta_equal,
271 .map_alloc_check = queue_stack_map_alloc_check,
272 .map_alloc = queue_stack_map_alloc,
273 .map_free = queue_stack_map_free,
274 .map_lookup_elem = queue_stack_map_lookup_elem,
275 .map_update_elem = queue_stack_map_update_elem,
276 .map_delete_elem = queue_stack_map_delete_elem,
277 .map_push_elem = queue_stack_map_push_elem,
278 .map_pop_elem = queue_map_pop_elem,
279 .map_peek_elem = queue_map_peek_elem,
280 .map_get_next_key = queue_stack_map_get_next_key,
281 .map_mem_usage = queue_stack_map_mem_usage,
282 .map_btf_id = &queue_map_btf_ids[0],
285 const struct bpf_map_ops stack_map_ops = {
286 .map_meta_equal = bpf_map_meta_equal,
287 .map_alloc_check = queue_stack_map_alloc_check,
288 .map_alloc = queue_stack_map_alloc,
289 .map_free = queue_stack_map_free,
290 .map_lookup_elem = queue_stack_map_lookup_elem,
291 .map_update_elem = queue_stack_map_update_elem,
292 .map_delete_elem = queue_stack_map_delete_elem,
293 .map_push_elem = queue_stack_map_push_elem,
294 .map_pop_elem = stack_map_pop_elem,
295 .map_peek_elem = stack_map_peek_elem,
296 .map_get_next_key = queue_stack_map_get_next_key,
297 .map_mem_usage = queue_stack_map_mem_usage,
298 .map_btf_id = &queue_map_btf_ids[0],