xref: /linux/kernel/bpf/queue_stack_maps.c (revision 87267b89459813cb50ab5377e076b639c07b4491)
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 
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 
31 static bool queue_stack_map_is_empty(struct bpf_queue_stack *qs)
32 {
33 	return qs->head == qs->tail;
34 }
35 
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 */
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 
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 */
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 
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 
126 
127 static long __stack_map_get(struct bpf_map *map, void *value, bool delete)
128 {
129 	struct bpf_queue_stack *qs = bpf_queue_stack(map);
130 	unsigned long flags;
131 	int err = 0;
132 	void *ptr;
133 	u32 index;
134 
135 	if (raw_res_spin_lock_irqsave(&qs->lock, flags)) {
136 		memset(value, 0, qs->map.value_size);
137 		return -EBUSY;
138 	}
139 
140 	if (queue_stack_map_is_empty(qs)) {
141 		memset(value, 0, qs->map.value_size);
142 		err = -ENOENT;
143 		goto out;
144 	}
145 
146 	index = qs->head - 1;
147 	if (unlikely(index >= qs->size))
148 		index = qs->size - 1;
149 
150 	ptr = &qs->elements[index * qs->map.value_size];
151 	memcpy(value, ptr, qs->map.value_size);
152 
153 	if (delete)
154 		qs->head = index;
155 
156 out:
157 	raw_res_spin_unlock_irqrestore(&qs->lock, flags);
158 	return err;
159 }
160 
161 /* Called from syscall or from eBPF program */
162 static long queue_map_peek_elem(struct bpf_map *map, void *value)
163 {
164 	return __queue_map_get(map, value, false);
165 }
166 
167 /* Called from syscall or from eBPF program */
168 static long stack_map_peek_elem(struct bpf_map *map, void *value)
169 {
170 	return __stack_map_get(map, value, false);
171 }
172 
173 /* Called from syscall or from eBPF program */
174 static long queue_map_pop_elem(struct bpf_map *map, void *value)
175 {
176 	return __queue_map_get(map, value, true);
177 }
178 
179 /* Called from syscall or from eBPF program */
180 static long stack_map_pop_elem(struct bpf_map *map, void *value)
181 {
182 	return __stack_map_get(map, value, true);
183 }
184 
185 /* Called from syscall or from eBPF program */
186 static long queue_stack_map_push_elem(struct bpf_map *map, void *value,
187 				      u64 flags)
188 {
189 	struct bpf_queue_stack *qs = bpf_queue_stack(map);
190 	unsigned long irq_flags;
191 	int err = 0;
192 	void *dst;
193 
194 	/* BPF_EXIST is used to force making room for a new element in case the
195 	 * map is full
196 	 */
197 	bool replace = (flags & BPF_EXIST);
198 
199 	/* Check supported flags for queue and stack maps */
200 	if (flags & BPF_NOEXIST || flags > BPF_EXIST)
201 		return -EINVAL;
202 
203 	if (raw_res_spin_lock_irqsave(&qs->lock, irq_flags))
204 		return -EBUSY;
205 
206 	if (queue_stack_map_is_full(qs)) {
207 		if (!replace) {
208 			err = -E2BIG;
209 			goto out;
210 		}
211 		/* advance tail pointer to overwrite oldest element */
212 		if (unlikely(++qs->tail >= qs->size))
213 			qs->tail = 0;
214 	}
215 
216 	dst = &qs->elements[qs->head * qs->map.value_size];
217 	memcpy(dst, value, qs->map.value_size);
218 
219 	if (unlikely(++qs->head >= qs->size))
220 		qs->head = 0;
221 
222 out:
223 	raw_res_spin_unlock_irqrestore(&qs->lock, irq_flags);
224 	return err;
225 }
226 
227 /* Called from syscall or from eBPF program */
228 static void *queue_stack_map_lookup_elem(struct bpf_map *map, void *key)
229 {
230 	return NULL;
231 }
232 
233 /* Called from syscall or from eBPF program */
234 static long queue_stack_map_update_elem(struct bpf_map *map, void *key,
235 					void *value, u64 flags)
236 {
237 	return -EINVAL;
238 }
239 
240 /* Called from syscall or from eBPF program */
241 static long queue_stack_map_delete_elem(struct bpf_map *map, void *key)
242 {
243 	return -EINVAL;
244 }
245 
246 /* Called from syscall */
247 static int queue_stack_map_get_next_key(struct bpf_map *map, void *key,
248 					void *next_key)
249 {
250 	return -EINVAL;
251 }
252 
253 static u64 queue_stack_map_mem_usage(const struct bpf_map *map)
254 {
255 	u64 usage = sizeof(struct bpf_queue_stack);
256 
257 	usage += ((u64)map->max_entries + 1) * map->value_size;
258 	return usage;
259 }
260 
261 BTF_ID_LIST_SINGLE(queue_map_btf_ids, struct, bpf_queue_stack)
262 const struct bpf_map_ops queue_map_ops = {
263 	.map_meta_equal = bpf_map_meta_equal,
264 	.map_alloc_check = queue_stack_map_alloc_check,
265 	.map_alloc = queue_stack_map_alloc,
266 	.map_free = queue_stack_map_free,
267 	.map_lookup_elem = queue_stack_map_lookup_elem,
268 	.map_update_elem = queue_stack_map_update_elem,
269 	.map_delete_elem = queue_stack_map_delete_elem,
270 	.map_push_elem = queue_stack_map_push_elem,
271 	.map_pop_elem = queue_map_pop_elem,
272 	.map_peek_elem = queue_map_peek_elem,
273 	.map_get_next_key = queue_stack_map_get_next_key,
274 	.map_mem_usage = queue_stack_map_mem_usage,
275 	.map_btf_id = &queue_map_btf_ids[0],
276 };
277 
278 const struct bpf_map_ops stack_map_ops = {
279 	.map_meta_equal = bpf_map_meta_equal,
280 	.map_alloc_check = queue_stack_map_alloc_check,
281 	.map_alloc = queue_stack_map_alloc,
282 	.map_free = queue_stack_map_free,
283 	.map_lookup_elem = queue_stack_map_lookup_elem,
284 	.map_update_elem = queue_stack_map_update_elem,
285 	.map_delete_elem = queue_stack_map_delete_elem,
286 	.map_push_elem = queue_stack_map_push_elem,
287 	.map_pop_elem = stack_map_pop_elem,
288 	.map_peek_elem = stack_map_peek_elem,
289 	.map_get_next_key = queue_stack_map_get_next_key,
290 	.map_mem_usage = queue_stack_map_mem_usage,
291 	.map_btf_id = &queue_map_btf_ids[0],
292 };
293