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 "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
)
51 /* check sanity of attributes */
52 if (attr
->max_entries
== 0 || attr
->key_size
!= 0 ||
53 attr
->value_size
== 0 ||
54 attr
->map_flags
& ~QUEUE_STACK_CREATE_FLAG_MASK
||
55 !bpf_map_flags_access_ok(attr
->map_flags
))
58 if (attr
->value_size
> KMALLOC_MAX_SIZE
)
59 /* if value_size is bigger, the user space won't be able to
60 * access the elements.
67 static struct bpf_map
*queue_stack_map_alloc(union bpf_attr
*attr
)
69 int ret
, numa_node
= bpf_map_attr_numa_node(attr
);
70 struct bpf_map_memory mem
= {0};
71 struct bpf_queue_stack
*qs
;
72 u64 size
, queue_size
, cost
;
74 size
= (u64
) attr
->max_entries
+ 1;
75 cost
= queue_size
= sizeof(*qs
) + size
* attr
->value_size
;
77 ret
= bpf_map_charge_init(&mem
, cost
);
81 qs
= bpf_map_area_alloc(queue_size
, numa_node
);
83 bpf_map_charge_finish(&mem
);
84 return ERR_PTR(-ENOMEM
);
87 memset(qs
, 0, sizeof(*qs
));
89 bpf_map_init_from_attr(&qs
->map
, attr
);
91 bpf_map_charge_move(&qs
->map
.memory
, &mem
);
94 raw_spin_lock_init(&qs
->lock
);
99 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
100 static void queue_stack_map_free(struct bpf_map
*map
)
102 struct bpf_queue_stack
*qs
= bpf_queue_stack(map
);
104 bpf_map_area_free(qs
);
107 static int __queue_map_get(struct bpf_map
*map
, void *value
, bool delete)
109 struct bpf_queue_stack
*qs
= bpf_queue_stack(map
);
114 raw_spin_lock_irqsave(&qs
->lock
, flags
);
116 if (queue_stack_map_is_empty(qs
)) {
117 memset(value
, 0, qs
->map
.value_size
);
122 ptr
= &qs
->elements
[qs
->tail
* qs
->map
.value_size
];
123 memcpy(value
, ptr
, qs
->map
.value_size
);
126 if (unlikely(++qs
->tail
>= qs
->size
))
131 raw_spin_unlock_irqrestore(&qs
->lock
, flags
);
136 static int __stack_map_get(struct bpf_map
*map
, void *value
, bool delete)
138 struct bpf_queue_stack
*qs
= bpf_queue_stack(map
);
144 raw_spin_lock_irqsave(&qs
->lock
, flags
);
146 if (queue_stack_map_is_empty(qs
)) {
147 memset(value
, 0, qs
->map
.value_size
);
152 index
= qs
->head
- 1;
153 if (unlikely(index
>= qs
->size
))
154 index
= qs
->size
- 1;
156 ptr
= &qs
->elements
[index
* qs
->map
.value_size
];
157 memcpy(value
, ptr
, qs
->map
.value_size
);
163 raw_spin_unlock_irqrestore(&qs
->lock
, flags
);
167 /* Called from syscall or from eBPF program */
168 static int queue_map_peek_elem(struct bpf_map
*map
, void *value
)
170 return __queue_map_get(map
, value
, false);
173 /* Called from syscall or from eBPF program */
174 static int stack_map_peek_elem(struct bpf_map
*map
, void *value
)
176 return __stack_map_get(map
, value
, false);
179 /* Called from syscall or from eBPF program */
180 static int queue_map_pop_elem(struct bpf_map
*map
, void *value
)
182 return __queue_map_get(map
, value
, true);
185 /* Called from syscall or from eBPF program */
186 static int stack_map_pop_elem(struct bpf_map
*map
, void *value
)
188 return __stack_map_get(map
, value
, true);
191 /* Called from syscall or from eBPF program */
192 static int queue_stack_map_push_elem(struct bpf_map
*map
, void *value
,
195 struct bpf_queue_stack
*qs
= bpf_queue_stack(map
);
196 unsigned long irq_flags
;
200 /* BPF_EXIST is used to force making room for a new element in case the
203 bool replace
= (flags
& BPF_EXIST
);
205 /* Check supported flags for queue and stack maps */
206 if (flags
& BPF_NOEXIST
|| flags
> BPF_EXIST
)
209 raw_spin_lock_irqsave(&qs
->lock
, irq_flags
);
211 if (queue_stack_map_is_full(qs
)) {
216 /* advance tail pointer to overwrite oldest element */
217 if (unlikely(++qs
->tail
>= qs
->size
))
221 dst
= &qs
->elements
[qs
->head
* qs
->map
.value_size
];
222 memcpy(dst
, value
, qs
->map
.value_size
);
224 if (unlikely(++qs
->head
>= qs
->size
))
228 raw_spin_unlock_irqrestore(&qs
->lock
, irq_flags
);
232 /* Called from syscall or from eBPF program */
233 static void *queue_stack_map_lookup_elem(struct bpf_map
*map
, void *key
)
238 /* Called from syscall or from eBPF program */
239 static int queue_stack_map_update_elem(struct bpf_map
*map
, void *key
,
240 void *value
, u64 flags
)
245 /* Called from syscall or from eBPF program */
246 static int queue_stack_map_delete_elem(struct bpf_map
*map
, void *key
)
251 /* Called from syscall */
252 static int queue_stack_map_get_next_key(struct bpf_map
*map
, void *key
,
258 static int queue_map_btf_id
;
259 const struct bpf_map_ops queue_map_ops
= {
260 .map_alloc_check
= queue_stack_map_alloc_check
,
261 .map_alloc
= queue_stack_map_alloc
,
262 .map_free
= queue_stack_map_free
,
263 .map_lookup_elem
= queue_stack_map_lookup_elem
,
264 .map_update_elem
= queue_stack_map_update_elem
,
265 .map_delete_elem
= queue_stack_map_delete_elem
,
266 .map_push_elem
= queue_stack_map_push_elem
,
267 .map_pop_elem
= queue_map_pop_elem
,
268 .map_peek_elem
= queue_map_peek_elem
,
269 .map_get_next_key
= queue_stack_map_get_next_key
,
270 .map_btf_name
= "bpf_queue_stack",
271 .map_btf_id
= &queue_map_btf_id
,
274 static int stack_map_btf_id
;
275 const struct bpf_map_ops stack_map_ops
= {
276 .map_alloc_check
= queue_stack_map_alloc_check
,
277 .map_alloc
= queue_stack_map_alloc
,
278 .map_free
= queue_stack_map_free
,
279 .map_lookup_elem
= queue_stack_map_lookup_elem
,
280 .map_update_elem
= queue_stack_map_update_elem
,
281 .map_delete_elem
= queue_stack_map_delete_elem
,
282 .map_push_elem
= queue_stack_map_push_elem
,
283 .map_pop_elem
= stack_map_pop_elem
,
284 .map_peek_elem
= stack_map_peek_elem
,
285 .map_get_next_key
= queue_stack_map_get_next_key
,
286 .map_btf_name
= "bpf_queue_stack",
287 .map_btf_id
= &stack_map_btf_id
,