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