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/capability.h>
11 #include <linux/btf_ids.h>
12 #include "percpu_freelist.h"
14 #define QUEUE_STACK_CREATE_FLAG_MASK \
15 (BPF_F_NUMA_NODE | BPF_F_ACCESS_MASK)
17 struct bpf_queue_stack {
21 u32 size; /* max_entries + 1 */
23 char elements[] __aligned(8);
26 static struct bpf_queue_stack *bpf_queue_stack(struct bpf_map *map)
28 return container_of(map, struct bpf_queue_stack, map);
31 static bool queue_stack_map_is_empty(struct bpf_queue_stack *qs)
33 return qs->head == qs->tail;
36 static bool queue_stack_map_is_full(struct bpf_queue_stack *qs)
38 u32 head = qs->head + 1;
40 if (unlikely(head >= qs->size))
43 return head == qs->tail;
46 /* Called from syscall */
47 static int queue_stack_map_alloc_check(union bpf_attr *attr)
52 /* check sanity of attributes */
53 if (attr->max_entries == 0 || attr->key_size != 0 ||
54 attr->value_size == 0 ||
55 attr->map_flags & ~QUEUE_STACK_CREATE_FLAG_MASK ||
56 !bpf_map_flags_access_ok(attr->map_flags))
59 if (attr->value_size > KMALLOC_MAX_SIZE)
60 /* if value_size is bigger, the user space won't be able to
61 * access the elements.
68 static struct bpf_map *queue_stack_map_alloc(union bpf_attr *attr)
70 int numa_node = bpf_map_attr_numa_node(attr);
71 struct bpf_queue_stack *qs;
74 size = (u64) attr->max_entries + 1;
75 queue_size = sizeof(*qs) + size * attr->value_size;
77 qs = bpf_map_area_alloc(queue_size, numa_node);
79 return ERR_PTR(-ENOMEM);
81 bpf_map_init_from_attr(&qs->map, attr);
85 raw_spin_lock_init(&qs->lock);
90 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
91 static void queue_stack_map_free(struct bpf_map *map)
93 struct bpf_queue_stack *qs = bpf_queue_stack(map);
95 bpf_map_area_free(qs);
98 static int __queue_map_get(struct bpf_map *map, void *value, bool delete)
100 struct bpf_queue_stack *qs = bpf_queue_stack(map);
105 raw_spin_lock_irqsave(&qs->lock, flags);
107 if (queue_stack_map_is_empty(qs)) {
108 memset(value, 0, qs->map.value_size);
113 ptr = &qs->elements[qs->tail * qs->map.value_size];
114 memcpy(value, ptr, qs->map.value_size);
117 if (unlikely(++qs->tail >= qs->size))
122 raw_spin_unlock_irqrestore(&qs->lock, flags);
127 static int __stack_map_get(struct bpf_map *map, void *value, bool delete)
129 struct bpf_queue_stack *qs = bpf_queue_stack(map);
135 raw_spin_lock_irqsave(&qs->lock, flags);
137 if (queue_stack_map_is_empty(qs)) {
138 memset(value, 0, qs->map.value_size);
143 index = qs->head - 1;
144 if (unlikely(index >= qs->size))
145 index = qs->size - 1;
147 ptr = &qs->elements[index * qs->map.value_size];
148 memcpy(value, ptr, qs->map.value_size);
154 raw_spin_unlock_irqrestore(&qs->lock, flags);
158 /* Called from syscall or from eBPF program */
159 static int queue_map_peek_elem(struct bpf_map *map, void *value)
161 return __queue_map_get(map, value, false);
164 /* Called from syscall or from eBPF program */
165 static int stack_map_peek_elem(struct bpf_map *map, void *value)
167 return __stack_map_get(map, value, false);
170 /* Called from syscall or from eBPF program */
171 static int queue_map_pop_elem(struct bpf_map *map, void *value)
173 return __queue_map_get(map, value, true);
176 /* Called from syscall or from eBPF program */
177 static int stack_map_pop_elem(struct bpf_map *map, void *value)
179 return __stack_map_get(map, value, true);
182 /* Called from syscall or from eBPF program */
183 static int queue_stack_map_push_elem(struct bpf_map *map, void *value,
186 struct bpf_queue_stack *qs = bpf_queue_stack(map);
187 unsigned long irq_flags;
191 /* BPF_EXIST is used to force making room for a new element in case the
194 bool replace = (flags & BPF_EXIST);
196 /* Check supported flags for queue and stack maps */
197 if (flags & BPF_NOEXIST || flags > BPF_EXIST)
200 raw_spin_lock_irqsave(&qs->lock, irq_flags);
202 if (queue_stack_map_is_full(qs)) {
207 /* advance tail pointer to overwrite oldest element */
208 if (unlikely(++qs->tail >= qs->size))
212 dst = &qs->elements[qs->head * qs->map.value_size];
213 memcpy(dst, value, qs->map.value_size);
215 if (unlikely(++qs->head >= qs->size))
219 raw_spin_unlock_irqrestore(&qs->lock, irq_flags);
223 /* Called from syscall or from eBPF program */
224 static void *queue_stack_map_lookup_elem(struct bpf_map *map, void *key)
229 /* Called from syscall or from eBPF program */
230 static int queue_stack_map_update_elem(struct bpf_map *map, void *key,
231 void *value, u64 flags)
236 /* Called from syscall or from eBPF program */
237 static int queue_stack_map_delete_elem(struct bpf_map *map, void *key)
242 /* Called from syscall */
243 static int queue_stack_map_get_next_key(struct bpf_map *map, void *key,
249 BTF_ID_LIST_SINGLE(queue_map_btf_ids, struct, bpf_queue_stack)
250 const struct bpf_map_ops queue_map_ops = {
251 .map_meta_equal = bpf_map_meta_equal,
252 .map_alloc_check = queue_stack_map_alloc_check,
253 .map_alloc = queue_stack_map_alloc,
254 .map_free = queue_stack_map_free,
255 .map_lookup_elem = queue_stack_map_lookup_elem,
256 .map_update_elem = queue_stack_map_update_elem,
257 .map_delete_elem = queue_stack_map_delete_elem,
258 .map_push_elem = queue_stack_map_push_elem,
259 .map_pop_elem = queue_map_pop_elem,
260 .map_peek_elem = queue_map_peek_elem,
261 .map_get_next_key = queue_stack_map_get_next_key,
262 .map_btf_id = &queue_map_btf_ids[0],
265 const struct bpf_map_ops stack_map_ops = {
266 .map_meta_equal = bpf_map_meta_equal,
267 .map_alloc_check = queue_stack_map_alloc_check,
268 .map_alloc = queue_stack_map_alloc,
269 .map_free = queue_stack_map_free,
270 .map_lookup_elem = queue_stack_map_lookup_elem,
271 .map_update_elem = queue_stack_map_update_elem,
272 .map_delete_elem = queue_stack_map_delete_elem,
273 .map_push_elem = queue_stack_map_push_elem,
274 .map_pop_elem = stack_map_pop_elem,
275 .map_peek_elem = stack_map_peek_elem,
276 .map_get_next_key = queue_stack_map_get_next_key,
277 .map_btf_id = &queue_map_btf_ids[0],