xref: /linux/kernel/bpf/queue_stack_maps.c (revision f9bff0e31881d03badf191d3b0005839391f5f2b)
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 
13 #define QUEUE_STACK_CREATE_FLAG_MASK \
14 	(BPF_F_NUMA_NODE | BPF_F_ACCESS_MASK)
15 
16 struct bpf_queue_stack {
17 	struct bpf_map map;
18 	raw_spinlock_t lock;
19 	u32 head, tail;
20 	u32 size; /* max_entries + 1 */
21 
22 	char elements[] __aligned(8);
23 };
24 
25 static struct bpf_queue_stack *bpf_queue_stack(struct bpf_map *map)
26 {
27 	return container_of(map, struct bpf_queue_stack, map);
28 }
29 
30 static bool queue_stack_map_is_empty(struct bpf_queue_stack *qs)
31 {
32 	return qs->head == qs->tail;
33 }
34 
35 static bool queue_stack_map_is_full(struct bpf_queue_stack *qs)
36 {
37 	u32 head = qs->head + 1;
38 
39 	if (unlikely(head >= qs->size))
40 		head = 0;
41 
42 	return head == qs->tail;
43 }
44 
45 /* Called from syscall */
46 static int queue_stack_map_alloc_check(union bpf_attr *attr)
47 {
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))
53 		return -EINVAL;
54 
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.
58 		 */
59 		return -E2BIG;
60 
61 	return 0;
62 }
63 
64 static struct bpf_map *queue_stack_map_alloc(union bpf_attr *attr)
65 {
66 	int numa_node = bpf_map_attr_numa_node(attr);
67 	struct bpf_queue_stack *qs;
68 	u64 size, queue_size;
69 
70 	size = (u64) attr->max_entries + 1;
71 	queue_size = sizeof(*qs) + size * attr->value_size;
72 
73 	qs = bpf_map_area_alloc(queue_size, numa_node);
74 	if (!qs)
75 		return ERR_PTR(-ENOMEM);
76 
77 	bpf_map_init_from_attr(&qs->map, attr);
78 
79 	qs->size = size;
80 
81 	raw_spin_lock_init(&qs->lock);
82 
83 	return &qs->map;
84 }
85 
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)
88 {
89 	struct bpf_queue_stack *qs = bpf_queue_stack(map);
90 
91 	bpf_map_area_free(qs);
92 }
93 
94 static long __queue_map_get(struct bpf_map *map, void *value, bool delete)
95 {
96 	struct bpf_queue_stack *qs = bpf_queue_stack(map);
97 	unsigned long flags;
98 	int err = 0;
99 	void *ptr;
100 
101 	raw_spin_lock_irqsave(&qs->lock, flags);
102 
103 	if (queue_stack_map_is_empty(qs)) {
104 		memset(value, 0, qs->map.value_size);
105 		err = -ENOENT;
106 		goto out;
107 	}
108 
109 	ptr = &qs->elements[qs->tail * qs->map.value_size];
110 	memcpy(value, ptr, qs->map.value_size);
111 
112 	if (delete) {
113 		if (unlikely(++qs->tail >= qs->size))
114 			qs->tail = 0;
115 	}
116 
117 out:
118 	raw_spin_unlock_irqrestore(&qs->lock, flags);
119 	return err;
120 }
121 
122 
123 static long __stack_map_get(struct bpf_map *map, void *value, bool delete)
124 {
125 	struct bpf_queue_stack *qs = bpf_queue_stack(map);
126 	unsigned long flags;
127 	int err = 0;
128 	void *ptr;
129 	u32 index;
130 
131 	raw_spin_lock_irqsave(&qs->lock, flags);
132 
133 	if (queue_stack_map_is_empty(qs)) {
134 		memset(value, 0, qs->map.value_size);
135 		err = -ENOENT;
136 		goto out;
137 	}
138 
139 	index = qs->head - 1;
140 	if (unlikely(index >= qs->size))
141 		index = qs->size - 1;
142 
143 	ptr = &qs->elements[index * qs->map.value_size];
144 	memcpy(value, ptr, qs->map.value_size);
145 
146 	if (delete)
147 		qs->head = index;
148 
149 out:
150 	raw_spin_unlock_irqrestore(&qs->lock, flags);
151 	return err;
152 }
153 
154 /* Called from syscall or from eBPF program */
155 static long queue_map_peek_elem(struct bpf_map *map, void *value)
156 {
157 	return __queue_map_get(map, value, false);
158 }
159 
160 /* Called from syscall or from eBPF program */
161 static long stack_map_peek_elem(struct bpf_map *map, void *value)
162 {
163 	return __stack_map_get(map, value, false);
164 }
165 
166 /* Called from syscall or from eBPF program */
167 static long queue_map_pop_elem(struct bpf_map *map, void *value)
168 {
169 	return __queue_map_get(map, value, true);
170 }
171 
172 /* Called from syscall or from eBPF program */
173 static long stack_map_pop_elem(struct bpf_map *map, void *value)
174 {
175 	return __stack_map_get(map, value, true);
176 }
177 
178 /* Called from syscall or from eBPF program */
179 static long queue_stack_map_push_elem(struct bpf_map *map, void *value,
180 				      u64 flags)
181 {
182 	struct bpf_queue_stack *qs = bpf_queue_stack(map);
183 	unsigned long irq_flags;
184 	int err = 0;
185 	void *dst;
186 
187 	/* BPF_EXIST is used to force making room for a new element in case the
188 	 * map is full
189 	 */
190 	bool replace = (flags & BPF_EXIST);
191 
192 	/* Check supported flags for queue and stack maps */
193 	if (flags & BPF_NOEXIST || flags > BPF_EXIST)
194 		return -EINVAL;
195 
196 	raw_spin_lock_irqsave(&qs->lock, irq_flags);
197 
198 	if (queue_stack_map_is_full(qs)) {
199 		if (!replace) {
200 			err = -E2BIG;
201 			goto out;
202 		}
203 		/* advance tail pointer to overwrite oldest element */
204 		if (unlikely(++qs->tail >= qs->size))
205 			qs->tail = 0;
206 	}
207 
208 	dst = &qs->elements[qs->head * qs->map.value_size];
209 	memcpy(dst, value, qs->map.value_size);
210 
211 	if (unlikely(++qs->head >= qs->size))
212 		qs->head = 0;
213 
214 out:
215 	raw_spin_unlock_irqrestore(&qs->lock, irq_flags);
216 	return err;
217 }
218 
219 /* Called from syscall or from eBPF program */
220 static void *queue_stack_map_lookup_elem(struct bpf_map *map, void *key)
221 {
222 	return NULL;
223 }
224 
225 /* Called from syscall or from eBPF program */
226 static long queue_stack_map_update_elem(struct bpf_map *map, void *key,
227 					void *value, u64 flags)
228 {
229 	return -EINVAL;
230 }
231 
232 /* Called from syscall or from eBPF program */
233 static long queue_stack_map_delete_elem(struct bpf_map *map, void *key)
234 {
235 	return -EINVAL;
236 }
237 
238 /* Called from syscall */
239 static int queue_stack_map_get_next_key(struct bpf_map *map, void *key,
240 					void *next_key)
241 {
242 	return -EINVAL;
243 }
244 
245 static u64 queue_stack_map_mem_usage(const struct bpf_map *map)
246 {
247 	u64 usage = sizeof(struct bpf_queue_stack);
248 
249 	usage += ((u64)map->max_entries + 1) * map->value_size;
250 	return usage;
251 }
252 
253 BTF_ID_LIST_SINGLE(queue_map_btf_ids, struct, bpf_queue_stack)
254 const struct bpf_map_ops queue_map_ops = {
255 	.map_meta_equal = bpf_map_meta_equal,
256 	.map_alloc_check = queue_stack_map_alloc_check,
257 	.map_alloc = queue_stack_map_alloc,
258 	.map_free = queue_stack_map_free,
259 	.map_lookup_elem = queue_stack_map_lookup_elem,
260 	.map_update_elem = queue_stack_map_update_elem,
261 	.map_delete_elem = queue_stack_map_delete_elem,
262 	.map_push_elem = queue_stack_map_push_elem,
263 	.map_pop_elem = queue_map_pop_elem,
264 	.map_peek_elem = queue_map_peek_elem,
265 	.map_get_next_key = queue_stack_map_get_next_key,
266 	.map_mem_usage = queue_stack_map_mem_usage,
267 	.map_btf_id = &queue_map_btf_ids[0],
268 };
269 
270 const struct bpf_map_ops stack_map_ops = {
271 	.map_meta_equal = bpf_map_meta_equal,
272 	.map_alloc_check = queue_stack_map_alloc_check,
273 	.map_alloc = queue_stack_map_alloc,
274 	.map_free = queue_stack_map_free,
275 	.map_lookup_elem = queue_stack_map_lookup_elem,
276 	.map_update_elem = queue_stack_map_update_elem,
277 	.map_delete_elem = queue_stack_map_delete_elem,
278 	.map_push_elem = queue_stack_map_push_elem,
279 	.map_pop_elem = stack_map_pop_elem,
280 	.map_peek_elem = stack_map_peek_elem,
281 	.map_get_next_key = queue_stack_map_get_next_key,
282 	.map_mem_usage = queue_stack_map_mem_usage,
283 	.map_btf_id = &queue_map_btf_ids[0],
284 };
285