xref: /linux/kernel/bpf/local_storage.c (revision 0be3ff0c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bpf-cgroup.h>
3 #include <linux/bpf.h>
4 #include <linux/bpf_local_storage.h>
5 #include <linux/btf.h>
6 #include <linux/bug.h>
7 #include <linux/filter.h>
8 #include <linux/mm.h>
9 #include <linux/rbtree.h>
10 #include <linux/slab.h>
11 #include <uapi/linux/btf.h>
12 
13 #ifdef CONFIG_CGROUP_BPF
14 
15 #include "../cgroup/cgroup-internal.h"
16 
17 #define LOCAL_STORAGE_CREATE_FLAG_MASK					\
18 	(BPF_F_NUMA_NODE | BPF_F_ACCESS_MASK)
19 
20 struct bpf_cgroup_storage_map {
21 	struct bpf_map map;
22 
23 	spinlock_t lock;
24 	struct rb_root root;
25 	struct list_head list;
26 };
27 
28 static struct bpf_cgroup_storage_map *map_to_storage(struct bpf_map *map)
29 {
30 	return container_of(map, struct bpf_cgroup_storage_map, map);
31 }
32 
33 static bool attach_type_isolated(const struct bpf_map *map)
34 {
35 	return map->key_size == sizeof(struct bpf_cgroup_storage_key);
36 }
37 
38 static int bpf_cgroup_storage_key_cmp(const struct bpf_cgroup_storage_map *map,
39 				      const void *_key1, const void *_key2)
40 {
41 	if (attach_type_isolated(&map->map)) {
42 		const struct bpf_cgroup_storage_key *key1 = _key1;
43 		const struct bpf_cgroup_storage_key *key2 = _key2;
44 
45 		if (key1->cgroup_inode_id < key2->cgroup_inode_id)
46 			return -1;
47 		else if (key1->cgroup_inode_id > key2->cgroup_inode_id)
48 			return 1;
49 		else if (key1->attach_type < key2->attach_type)
50 			return -1;
51 		else if (key1->attach_type > key2->attach_type)
52 			return 1;
53 	} else {
54 		const __u64 *cgroup_inode_id1 = _key1;
55 		const __u64 *cgroup_inode_id2 = _key2;
56 
57 		if (*cgroup_inode_id1 < *cgroup_inode_id2)
58 			return -1;
59 		else if (*cgroup_inode_id1 > *cgroup_inode_id2)
60 			return 1;
61 	}
62 	return 0;
63 }
64 
65 struct bpf_cgroup_storage *
66 cgroup_storage_lookup(struct bpf_cgroup_storage_map *map,
67 		      void *key, bool locked)
68 {
69 	struct rb_root *root = &map->root;
70 	struct rb_node *node;
71 
72 	if (!locked)
73 		spin_lock_bh(&map->lock);
74 
75 	node = root->rb_node;
76 	while (node) {
77 		struct bpf_cgroup_storage *storage;
78 
79 		storage = container_of(node, struct bpf_cgroup_storage, node);
80 
81 		switch (bpf_cgroup_storage_key_cmp(map, key, &storage->key)) {
82 		case -1:
83 			node = node->rb_left;
84 			break;
85 		case 1:
86 			node = node->rb_right;
87 			break;
88 		default:
89 			if (!locked)
90 				spin_unlock_bh(&map->lock);
91 			return storage;
92 		}
93 	}
94 
95 	if (!locked)
96 		spin_unlock_bh(&map->lock);
97 
98 	return NULL;
99 }
100 
101 static int cgroup_storage_insert(struct bpf_cgroup_storage_map *map,
102 				 struct bpf_cgroup_storage *storage)
103 {
104 	struct rb_root *root = &map->root;
105 	struct rb_node **new = &(root->rb_node), *parent = NULL;
106 
107 	while (*new) {
108 		struct bpf_cgroup_storage *this;
109 
110 		this = container_of(*new, struct bpf_cgroup_storage, node);
111 
112 		parent = *new;
113 		switch (bpf_cgroup_storage_key_cmp(map, &storage->key, &this->key)) {
114 		case -1:
115 			new = &((*new)->rb_left);
116 			break;
117 		case 1:
118 			new = &((*new)->rb_right);
119 			break;
120 		default:
121 			return -EEXIST;
122 		}
123 	}
124 
125 	rb_link_node(&storage->node, parent, new);
126 	rb_insert_color(&storage->node, root);
127 
128 	return 0;
129 }
130 
131 static void *cgroup_storage_lookup_elem(struct bpf_map *_map, void *key)
132 {
133 	struct bpf_cgroup_storage_map *map = map_to_storage(_map);
134 	struct bpf_cgroup_storage *storage;
135 
136 	storage = cgroup_storage_lookup(map, key, false);
137 	if (!storage)
138 		return NULL;
139 
140 	return &READ_ONCE(storage->buf)->data[0];
141 }
142 
143 static int cgroup_storage_update_elem(struct bpf_map *map, void *key,
144 				      void *value, u64 flags)
145 {
146 	struct bpf_cgroup_storage *storage;
147 	struct bpf_storage_buffer *new;
148 
149 	if (unlikely(flags & ~(BPF_F_LOCK | BPF_EXIST)))
150 		return -EINVAL;
151 
152 	if (unlikely((flags & BPF_F_LOCK) &&
153 		     !map_value_has_spin_lock(map)))
154 		return -EINVAL;
155 
156 	storage = cgroup_storage_lookup((struct bpf_cgroup_storage_map *)map,
157 					key, false);
158 	if (!storage)
159 		return -ENOENT;
160 
161 	if (flags & BPF_F_LOCK) {
162 		copy_map_value_locked(map, storage->buf->data, value, false);
163 		return 0;
164 	}
165 
166 	new = bpf_map_kmalloc_node(map, struct_size(new, data, map->value_size),
167 				   __GFP_ZERO | GFP_ATOMIC | __GFP_NOWARN,
168 				   map->numa_node);
169 	if (!new)
170 		return -ENOMEM;
171 
172 	memcpy(&new->data[0], value, map->value_size);
173 	check_and_init_map_value(map, new->data);
174 
175 	new = xchg(&storage->buf, new);
176 	kfree_rcu(new, rcu);
177 
178 	return 0;
179 }
180 
181 int bpf_percpu_cgroup_storage_copy(struct bpf_map *_map, void *key,
182 				   void *value)
183 {
184 	struct bpf_cgroup_storage_map *map = map_to_storage(_map);
185 	struct bpf_cgroup_storage *storage;
186 	int cpu, off = 0;
187 	u32 size;
188 
189 	rcu_read_lock();
190 	storage = cgroup_storage_lookup(map, key, false);
191 	if (!storage) {
192 		rcu_read_unlock();
193 		return -ENOENT;
194 	}
195 
196 	/* per_cpu areas are zero-filled and bpf programs can only
197 	 * access 'value_size' of them, so copying rounded areas
198 	 * will not leak any kernel data
199 	 */
200 	size = round_up(_map->value_size, 8);
201 	for_each_possible_cpu(cpu) {
202 		bpf_long_memcpy(value + off,
203 				per_cpu_ptr(storage->percpu_buf, cpu), size);
204 		off += size;
205 	}
206 	rcu_read_unlock();
207 	return 0;
208 }
209 
210 int bpf_percpu_cgroup_storage_update(struct bpf_map *_map, void *key,
211 				     void *value, u64 map_flags)
212 {
213 	struct bpf_cgroup_storage_map *map = map_to_storage(_map);
214 	struct bpf_cgroup_storage *storage;
215 	int cpu, off = 0;
216 	u32 size;
217 
218 	if (map_flags != BPF_ANY && map_flags != BPF_EXIST)
219 		return -EINVAL;
220 
221 	rcu_read_lock();
222 	storage = cgroup_storage_lookup(map, key, false);
223 	if (!storage) {
224 		rcu_read_unlock();
225 		return -ENOENT;
226 	}
227 
228 	/* the user space will provide round_up(value_size, 8) bytes that
229 	 * will be copied into per-cpu area. bpf programs can only access
230 	 * value_size of it. During lookup the same extra bytes will be
231 	 * returned or zeros which were zero-filled by percpu_alloc,
232 	 * so no kernel data leaks possible
233 	 */
234 	size = round_up(_map->value_size, 8);
235 	for_each_possible_cpu(cpu) {
236 		bpf_long_memcpy(per_cpu_ptr(storage->percpu_buf, cpu),
237 				value + off, size);
238 		off += size;
239 	}
240 	rcu_read_unlock();
241 	return 0;
242 }
243 
244 static int cgroup_storage_get_next_key(struct bpf_map *_map, void *key,
245 				       void *_next_key)
246 {
247 	struct bpf_cgroup_storage_map *map = map_to_storage(_map);
248 	struct bpf_cgroup_storage *storage;
249 
250 	spin_lock_bh(&map->lock);
251 
252 	if (list_empty(&map->list))
253 		goto enoent;
254 
255 	if (key) {
256 		storage = cgroup_storage_lookup(map, key, true);
257 		if (!storage)
258 			goto enoent;
259 
260 		storage = list_next_entry(storage, list_map);
261 		if (!storage)
262 			goto enoent;
263 	} else {
264 		storage = list_first_entry(&map->list,
265 					 struct bpf_cgroup_storage, list_map);
266 	}
267 
268 	spin_unlock_bh(&map->lock);
269 
270 	if (attach_type_isolated(&map->map)) {
271 		struct bpf_cgroup_storage_key *next = _next_key;
272 		*next = storage->key;
273 	} else {
274 		__u64 *next = _next_key;
275 		*next = storage->key.cgroup_inode_id;
276 	}
277 	return 0;
278 
279 enoent:
280 	spin_unlock_bh(&map->lock);
281 	return -ENOENT;
282 }
283 
284 static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
285 {
286 	__u32 max_value_size = BPF_LOCAL_STORAGE_MAX_VALUE_SIZE;
287 	int numa_node = bpf_map_attr_numa_node(attr);
288 	struct bpf_cgroup_storage_map *map;
289 
290 	/* percpu is bound by PCPU_MIN_UNIT_SIZE, non-percu
291 	 * is the same as other local storages.
292 	 */
293 	if (attr->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
294 		max_value_size = min_t(__u32, max_value_size,
295 				       PCPU_MIN_UNIT_SIZE);
296 
297 	if (attr->key_size != sizeof(struct bpf_cgroup_storage_key) &&
298 	    attr->key_size != sizeof(__u64))
299 		return ERR_PTR(-EINVAL);
300 
301 	if (attr->value_size == 0)
302 		return ERR_PTR(-EINVAL);
303 
304 	if (attr->value_size > max_value_size)
305 		return ERR_PTR(-E2BIG);
306 
307 	if (attr->map_flags & ~LOCAL_STORAGE_CREATE_FLAG_MASK ||
308 	    !bpf_map_flags_access_ok(attr->map_flags))
309 		return ERR_PTR(-EINVAL);
310 
311 	if (attr->max_entries)
312 		/* max_entries is not used and enforced to be 0 */
313 		return ERR_PTR(-EINVAL);
314 
315 	map = kmalloc_node(sizeof(struct bpf_cgroup_storage_map),
316 			   __GFP_ZERO | GFP_USER | __GFP_ACCOUNT, numa_node);
317 	if (!map)
318 		return ERR_PTR(-ENOMEM);
319 
320 	/* copy mandatory map attributes */
321 	bpf_map_init_from_attr(&map->map, attr);
322 
323 	spin_lock_init(&map->lock);
324 	map->root = RB_ROOT;
325 	INIT_LIST_HEAD(&map->list);
326 
327 	return &map->map;
328 }
329 
330 static void cgroup_storage_map_free(struct bpf_map *_map)
331 {
332 	struct bpf_cgroup_storage_map *map = map_to_storage(_map);
333 	struct list_head *storages = &map->list;
334 	struct bpf_cgroup_storage *storage, *stmp;
335 
336 	mutex_lock(&cgroup_mutex);
337 
338 	list_for_each_entry_safe(storage, stmp, storages, list_map) {
339 		bpf_cgroup_storage_unlink(storage);
340 		bpf_cgroup_storage_free(storage);
341 	}
342 
343 	mutex_unlock(&cgroup_mutex);
344 
345 	WARN_ON(!RB_EMPTY_ROOT(&map->root));
346 	WARN_ON(!list_empty(&map->list));
347 
348 	kfree(map);
349 }
350 
351 static int cgroup_storage_delete_elem(struct bpf_map *map, void *key)
352 {
353 	return -EINVAL;
354 }
355 
356 static int cgroup_storage_check_btf(const struct bpf_map *map,
357 				    const struct btf *btf,
358 				    const struct btf_type *key_type,
359 				    const struct btf_type *value_type)
360 {
361 	if (attach_type_isolated(map)) {
362 		struct btf_member *m;
363 		u32 offset, size;
364 
365 		/* Key is expected to be of struct bpf_cgroup_storage_key type,
366 		 * which is:
367 		 * struct bpf_cgroup_storage_key {
368 		 *	__u64	cgroup_inode_id;
369 		 *	__u32	attach_type;
370 		 * };
371 		 */
372 
373 		/*
374 		 * Key_type must be a structure with two fields.
375 		 */
376 		if (BTF_INFO_KIND(key_type->info) != BTF_KIND_STRUCT ||
377 		    BTF_INFO_VLEN(key_type->info) != 2)
378 			return -EINVAL;
379 
380 		/*
381 		 * The first field must be a 64 bit integer at 0 offset.
382 		 */
383 		m = (struct btf_member *)(key_type + 1);
384 		size = sizeof_field(struct bpf_cgroup_storage_key, cgroup_inode_id);
385 		if (!btf_member_is_reg_int(btf, key_type, m, 0, size))
386 			return -EINVAL;
387 
388 		/*
389 		 * The second field must be a 32 bit integer at 64 bit offset.
390 		 */
391 		m++;
392 		offset = offsetof(struct bpf_cgroup_storage_key, attach_type);
393 		size = sizeof_field(struct bpf_cgroup_storage_key, attach_type);
394 		if (!btf_member_is_reg_int(btf, key_type, m, offset, size))
395 			return -EINVAL;
396 	} else {
397 		u32 int_data;
398 
399 		/*
400 		 * Key is expected to be u64, which stores the cgroup_inode_id
401 		 */
402 
403 		if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
404 			return -EINVAL;
405 
406 		int_data = *(u32 *)(key_type + 1);
407 		if (BTF_INT_BITS(int_data) != 64 || BTF_INT_OFFSET(int_data))
408 			return -EINVAL;
409 	}
410 
411 	return 0;
412 }
413 
414 static void cgroup_storage_seq_show_elem(struct bpf_map *map, void *key,
415 					 struct seq_file *m)
416 {
417 	enum bpf_cgroup_storage_type stype;
418 	struct bpf_cgroup_storage *storage;
419 	int cpu;
420 
421 	rcu_read_lock();
422 	storage = cgroup_storage_lookup(map_to_storage(map), key, false);
423 	if (!storage) {
424 		rcu_read_unlock();
425 		return;
426 	}
427 
428 	btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
429 	stype = cgroup_storage_type(map);
430 	if (stype == BPF_CGROUP_STORAGE_SHARED) {
431 		seq_puts(m, ": ");
432 		btf_type_seq_show(map->btf, map->btf_value_type_id,
433 				  &READ_ONCE(storage->buf)->data[0], m);
434 		seq_puts(m, "\n");
435 	} else {
436 		seq_puts(m, ": {\n");
437 		for_each_possible_cpu(cpu) {
438 			seq_printf(m, "\tcpu%d: ", cpu);
439 			btf_type_seq_show(map->btf, map->btf_value_type_id,
440 					  per_cpu_ptr(storage->percpu_buf, cpu),
441 					  m);
442 			seq_puts(m, "\n");
443 		}
444 		seq_puts(m, "}\n");
445 	}
446 	rcu_read_unlock();
447 }
448 
449 static int cgroup_storage_map_btf_id;
450 const struct bpf_map_ops cgroup_storage_map_ops = {
451 	.map_alloc = cgroup_storage_map_alloc,
452 	.map_free = cgroup_storage_map_free,
453 	.map_get_next_key = cgroup_storage_get_next_key,
454 	.map_lookup_elem = cgroup_storage_lookup_elem,
455 	.map_update_elem = cgroup_storage_update_elem,
456 	.map_delete_elem = cgroup_storage_delete_elem,
457 	.map_check_btf = cgroup_storage_check_btf,
458 	.map_seq_show_elem = cgroup_storage_seq_show_elem,
459 	.map_btf_name = "bpf_cgroup_storage_map",
460 	.map_btf_id = &cgroup_storage_map_btf_id,
461 };
462 
463 int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux, struct bpf_map *_map)
464 {
465 	enum bpf_cgroup_storage_type stype = cgroup_storage_type(_map);
466 
467 	if (aux->cgroup_storage[stype] &&
468 	    aux->cgroup_storage[stype] != _map)
469 		return -EBUSY;
470 
471 	aux->cgroup_storage[stype] = _map;
472 	return 0;
473 }
474 
475 static size_t bpf_cgroup_storage_calculate_size(struct bpf_map *map, u32 *pages)
476 {
477 	size_t size;
478 
479 	if (cgroup_storage_type(map) == BPF_CGROUP_STORAGE_SHARED) {
480 		size = sizeof(struct bpf_storage_buffer) + map->value_size;
481 		*pages = round_up(sizeof(struct bpf_cgroup_storage) + size,
482 				  PAGE_SIZE) >> PAGE_SHIFT;
483 	} else {
484 		size = map->value_size;
485 		*pages = round_up(round_up(size, 8) * num_possible_cpus(),
486 				  PAGE_SIZE) >> PAGE_SHIFT;
487 	}
488 
489 	return size;
490 }
491 
492 struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog,
493 					enum bpf_cgroup_storage_type stype)
494 {
495 	const gfp_t gfp = __GFP_ZERO | GFP_USER;
496 	struct bpf_cgroup_storage *storage;
497 	struct bpf_map *map;
498 	size_t size;
499 	u32 pages;
500 
501 	map = prog->aux->cgroup_storage[stype];
502 	if (!map)
503 		return NULL;
504 
505 	size = bpf_cgroup_storage_calculate_size(map, &pages);
506 
507 	storage = bpf_map_kmalloc_node(map, sizeof(struct bpf_cgroup_storage),
508 				       gfp, map->numa_node);
509 	if (!storage)
510 		goto enomem;
511 
512 	if (stype == BPF_CGROUP_STORAGE_SHARED) {
513 		storage->buf = bpf_map_kmalloc_node(map, size, gfp,
514 						    map->numa_node);
515 		if (!storage->buf)
516 			goto enomem;
517 		check_and_init_map_value(map, storage->buf->data);
518 	} else {
519 		storage->percpu_buf = bpf_map_alloc_percpu(map, size, 8, gfp);
520 		if (!storage->percpu_buf)
521 			goto enomem;
522 	}
523 
524 	storage->map = (struct bpf_cgroup_storage_map *)map;
525 
526 	return storage;
527 
528 enomem:
529 	kfree(storage);
530 	return ERR_PTR(-ENOMEM);
531 }
532 
533 static void free_shared_cgroup_storage_rcu(struct rcu_head *rcu)
534 {
535 	struct bpf_cgroup_storage *storage =
536 		container_of(rcu, struct bpf_cgroup_storage, rcu);
537 
538 	kfree(storage->buf);
539 	kfree(storage);
540 }
541 
542 static void free_percpu_cgroup_storage_rcu(struct rcu_head *rcu)
543 {
544 	struct bpf_cgroup_storage *storage =
545 		container_of(rcu, struct bpf_cgroup_storage, rcu);
546 
547 	free_percpu(storage->percpu_buf);
548 	kfree(storage);
549 }
550 
551 void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage)
552 {
553 	enum bpf_cgroup_storage_type stype;
554 	struct bpf_map *map;
555 
556 	if (!storage)
557 		return;
558 
559 	map = &storage->map->map;
560 	stype = cgroup_storage_type(map);
561 	if (stype == BPF_CGROUP_STORAGE_SHARED)
562 		call_rcu(&storage->rcu, free_shared_cgroup_storage_rcu);
563 	else
564 		call_rcu(&storage->rcu, free_percpu_cgroup_storage_rcu);
565 }
566 
567 void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
568 			     struct cgroup *cgroup,
569 			     enum bpf_attach_type type)
570 {
571 	struct bpf_cgroup_storage_map *map;
572 
573 	if (!storage)
574 		return;
575 
576 	storage->key.attach_type = type;
577 	storage->key.cgroup_inode_id = cgroup_id(cgroup);
578 
579 	map = storage->map;
580 
581 	spin_lock_bh(&map->lock);
582 	WARN_ON(cgroup_storage_insert(map, storage));
583 	list_add(&storage->list_map, &map->list);
584 	list_add(&storage->list_cg, &cgroup->bpf.storages);
585 	spin_unlock_bh(&map->lock);
586 }
587 
588 void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage)
589 {
590 	struct bpf_cgroup_storage_map *map;
591 	struct rb_root *root;
592 
593 	if (!storage)
594 		return;
595 
596 	map = storage->map;
597 
598 	spin_lock_bh(&map->lock);
599 	root = &map->root;
600 	rb_erase(&storage->node, root);
601 
602 	list_del(&storage->list_map);
603 	list_del(&storage->list_cg);
604 	spin_unlock_bh(&map->lock);
605 }
606 
607 #endif
608