1 //SPDX-License-Identifier: GPL-2.0
2 #include <linux/bpf-cgroup.h>
6 #include <linux/filter.h>
8 #include <linux/rbtree.h>
9 #include <linux/slab.h>
10 #include <uapi/linux/btf.h>
12 #ifdef CONFIG_CGROUP_BPF
14 DEFINE_PER_CPU(struct bpf_cgroup_storage_info,
15 bpf_cgroup_storage_info[BPF_CGROUP_STORAGE_NEST_MAX]);
17 #include "../cgroup/cgroup-internal.h"
19 #define LOCAL_STORAGE_CREATE_FLAG_MASK \
20 (BPF_F_NUMA_NODE | BPF_F_ACCESS_MASK)
22 struct bpf_cgroup_storage_map {
27 struct list_head list;
30 static struct bpf_cgroup_storage_map *map_to_storage(struct bpf_map *map)
32 return container_of(map, struct bpf_cgroup_storage_map, map);
35 static bool attach_type_isolated(const struct bpf_map *map)
37 return map->key_size == sizeof(struct bpf_cgroup_storage_key);
40 static int bpf_cgroup_storage_key_cmp(const struct bpf_cgroup_storage_map *map,
41 const void *_key1, const void *_key2)
43 if (attach_type_isolated(&map->map)) {
44 const struct bpf_cgroup_storage_key *key1 = _key1;
45 const struct bpf_cgroup_storage_key *key2 = _key2;
47 if (key1->cgroup_inode_id < key2->cgroup_inode_id)
49 else if (key1->cgroup_inode_id > key2->cgroup_inode_id)
51 else if (key1->attach_type < key2->attach_type)
53 else if (key1->attach_type > key2->attach_type)
56 const __u64 *cgroup_inode_id1 = _key1;
57 const __u64 *cgroup_inode_id2 = _key2;
59 if (*cgroup_inode_id1 < *cgroup_inode_id2)
61 else if (*cgroup_inode_id1 > *cgroup_inode_id2)
67 struct bpf_cgroup_storage *
68 cgroup_storage_lookup(struct bpf_cgroup_storage_map *map,
69 void *key, bool locked)
71 struct rb_root *root = &map->root;
75 spin_lock_bh(&map->lock);
79 struct bpf_cgroup_storage *storage;
81 storage = container_of(node, struct bpf_cgroup_storage, node);
83 switch (bpf_cgroup_storage_key_cmp(map, key, &storage->key)) {
88 node = node->rb_right;
92 spin_unlock_bh(&map->lock);
98 spin_unlock_bh(&map->lock);
103 static int cgroup_storage_insert(struct bpf_cgroup_storage_map *map,
104 struct bpf_cgroup_storage *storage)
106 struct rb_root *root = &map->root;
107 struct rb_node **new = &(root->rb_node), *parent = NULL;
110 struct bpf_cgroup_storage *this;
112 this = container_of(*new, struct bpf_cgroup_storage, node);
115 switch (bpf_cgroup_storage_key_cmp(map, &storage->key, &this->key)) {
117 new = &((*new)->rb_left);
120 new = &((*new)->rb_right);
127 rb_link_node(&storage->node, parent, new);
128 rb_insert_color(&storage->node, root);
133 static void *cgroup_storage_lookup_elem(struct bpf_map *_map, void *key)
135 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
136 struct bpf_cgroup_storage *storage;
138 storage = cgroup_storage_lookup(map, key, false);
142 return &READ_ONCE(storage->buf)->data[0];
145 static int cgroup_storage_update_elem(struct bpf_map *map, void *key,
146 void *value, u64 flags)
148 struct bpf_cgroup_storage *storage;
149 struct bpf_storage_buffer *new;
151 if (unlikely(flags & ~(BPF_F_LOCK | BPF_EXIST)))
154 if (unlikely((flags & BPF_F_LOCK) &&
155 !map_value_has_spin_lock(map)))
158 storage = cgroup_storage_lookup((struct bpf_cgroup_storage_map *)map,
163 if (flags & BPF_F_LOCK) {
164 copy_map_value_locked(map, storage->buf->data, value, false);
168 new = kmalloc_node(sizeof(struct bpf_storage_buffer) +
170 __GFP_ZERO | GFP_ATOMIC | __GFP_NOWARN,
175 memcpy(&new->data[0], value, map->value_size);
176 check_and_init_map_lock(map, new->data);
178 new = xchg(&storage->buf, new);
184 int bpf_percpu_cgroup_storage_copy(struct bpf_map *_map, void *key,
187 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
188 struct bpf_cgroup_storage *storage;
193 storage = cgroup_storage_lookup(map, key, false);
199 /* per_cpu areas are zero-filled and bpf programs can only
200 * access 'value_size' of them, so copying rounded areas
201 * will not leak any kernel data
203 size = round_up(_map->value_size, 8);
204 for_each_possible_cpu(cpu) {
205 bpf_long_memcpy(value + off,
206 per_cpu_ptr(storage->percpu_buf, cpu), size);
213 int bpf_percpu_cgroup_storage_update(struct bpf_map *_map, void *key,
214 void *value, u64 map_flags)
216 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
217 struct bpf_cgroup_storage *storage;
221 if (map_flags != BPF_ANY && map_flags != BPF_EXIST)
225 storage = cgroup_storage_lookup(map, key, false);
231 /* the user space will provide round_up(value_size, 8) bytes that
232 * will be copied into per-cpu area. bpf programs can only access
233 * value_size of it. During lookup the same extra bytes will be
234 * returned or zeros which were zero-filled by percpu_alloc,
235 * so no kernel data leaks possible
237 size = round_up(_map->value_size, 8);
238 for_each_possible_cpu(cpu) {
239 bpf_long_memcpy(per_cpu_ptr(storage->percpu_buf, cpu),
247 static int cgroup_storage_get_next_key(struct bpf_map *_map, void *key,
250 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
251 struct bpf_cgroup_storage *storage;
253 spin_lock_bh(&map->lock);
255 if (list_empty(&map->list))
259 storage = cgroup_storage_lookup(map, key, true);
263 storage = list_next_entry(storage, list_map);
267 storage = list_first_entry(&map->list,
268 struct bpf_cgroup_storage, list_map);
271 spin_unlock_bh(&map->lock);
273 if (attach_type_isolated(&map->map)) {
274 struct bpf_cgroup_storage_key *next = _next_key;
275 *next = storage->key;
277 __u64 *next = _next_key;
278 *next = storage->key.cgroup_inode_id;
283 spin_unlock_bh(&map->lock);
287 static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
289 int numa_node = bpf_map_attr_numa_node(attr);
290 struct bpf_cgroup_storage_map *map;
291 struct bpf_map_memory mem;
294 if (attr->key_size != sizeof(struct bpf_cgroup_storage_key) &&
295 attr->key_size != sizeof(__u64))
296 return ERR_PTR(-EINVAL);
298 if (attr->value_size == 0)
299 return ERR_PTR(-EINVAL);
301 if (attr->value_size > PAGE_SIZE)
302 return ERR_PTR(-E2BIG);
304 if (attr->map_flags & ~LOCAL_STORAGE_CREATE_FLAG_MASK ||
305 !bpf_map_flags_access_ok(attr->map_flags))
306 return ERR_PTR(-EINVAL);
308 if (attr->max_entries)
309 /* max_entries is not used and enforced to be 0 */
310 return ERR_PTR(-EINVAL);
312 ret = bpf_map_charge_init(&mem, sizeof(struct bpf_cgroup_storage_map));
316 map = kmalloc_node(sizeof(struct bpf_cgroup_storage_map),
317 __GFP_ZERO | GFP_USER, numa_node);
319 bpf_map_charge_finish(&mem);
320 return ERR_PTR(-ENOMEM);
323 bpf_map_charge_move(&map->map.memory, &mem);
325 /* copy mandatory map attributes */
326 bpf_map_init_from_attr(&map->map, attr);
328 spin_lock_init(&map->lock);
330 INIT_LIST_HEAD(&map->list);
335 static void cgroup_storage_map_free(struct bpf_map *_map)
337 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
338 struct list_head *storages = &map->list;
339 struct bpf_cgroup_storage *storage, *stmp;
341 mutex_lock(&cgroup_mutex);
343 list_for_each_entry_safe(storage, stmp, storages, list_map) {
344 bpf_cgroup_storage_unlink(storage);
345 bpf_cgroup_storage_free(storage);
348 mutex_unlock(&cgroup_mutex);
350 WARN_ON(!RB_EMPTY_ROOT(&map->root));
351 WARN_ON(!list_empty(&map->list));
356 static int cgroup_storage_delete_elem(struct bpf_map *map, void *key)
361 static int cgroup_storage_check_btf(const struct bpf_map *map,
362 const struct btf *btf,
363 const struct btf_type *key_type,
364 const struct btf_type *value_type)
366 if (attach_type_isolated(map)) {
367 struct btf_member *m;
370 /* Key is expected to be of struct bpf_cgroup_storage_key type,
372 * struct bpf_cgroup_storage_key {
373 * __u64 cgroup_inode_id;
379 * Key_type must be a structure with two fields.
381 if (BTF_INFO_KIND(key_type->info) != BTF_KIND_STRUCT ||
382 BTF_INFO_VLEN(key_type->info) != 2)
386 * The first field must be a 64 bit integer at 0 offset.
388 m = (struct btf_member *)(key_type + 1);
389 size = sizeof_field(struct bpf_cgroup_storage_key, cgroup_inode_id);
390 if (!btf_member_is_reg_int(btf, key_type, m, 0, size))
394 * The second field must be a 32 bit integer at 64 bit offset.
397 offset = offsetof(struct bpf_cgroup_storage_key, attach_type);
398 size = sizeof_field(struct bpf_cgroup_storage_key, attach_type);
399 if (!btf_member_is_reg_int(btf, key_type, m, offset, size))
405 * Key is expected to be u64, which stores the cgroup_inode_id
408 if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
411 int_data = *(u32 *)(key_type + 1);
412 if (BTF_INT_BITS(int_data) != 64 || BTF_INT_OFFSET(int_data))
419 static void cgroup_storage_seq_show_elem(struct bpf_map *map, void *key,
422 enum bpf_cgroup_storage_type stype = cgroup_storage_type(map);
423 struct bpf_cgroup_storage *storage;
427 storage = cgroup_storage_lookup(map_to_storage(map), key, false);
433 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
434 stype = cgroup_storage_type(map);
435 if (stype == BPF_CGROUP_STORAGE_SHARED) {
437 btf_type_seq_show(map->btf, map->btf_value_type_id,
438 &READ_ONCE(storage->buf)->data[0], m);
441 seq_puts(m, ": {\n");
442 for_each_possible_cpu(cpu) {
443 seq_printf(m, "\tcpu%d: ", cpu);
444 btf_type_seq_show(map->btf, map->btf_value_type_id,
445 per_cpu_ptr(storage->percpu_buf, cpu),
454 static int cgroup_storage_map_btf_id;
455 const struct bpf_map_ops cgroup_storage_map_ops = {
456 .map_alloc = cgroup_storage_map_alloc,
457 .map_free = cgroup_storage_map_free,
458 .map_get_next_key = cgroup_storage_get_next_key,
459 .map_lookup_elem = cgroup_storage_lookup_elem,
460 .map_update_elem = cgroup_storage_update_elem,
461 .map_delete_elem = cgroup_storage_delete_elem,
462 .map_check_btf = cgroup_storage_check_btf,
463 .map_seq_show_elem = cgroup_storage_seq_show_elem,
464 .map_btf_name = "bpf_cgroup_storage_map",
465 .map_btf_id = &cgroup_storage_map_btf_id,
468 int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux, struct bpf_map *_map)
470 enum bpf_cgroup_storage_type stype = cgroup_storage_type(_map);
472 if (aux->cgroup_storage[stype] &&
473 aux->cgroup_storage[stype] != _map)
476 aux->cgroup_storage[stype] = _map;
480 static size_t bpf_cgroup_storage_calculate_size(struct bpf_map *map, u32 *pages)
484 if (cgroup_storage_type(map) == BPF_CGROUP_STORAGE_SHARED) {
485 size = sizeof(struct bpf_storage_buffer) + map->value_size;
486 *pages = round_up(sizeof(struct bpf_cgroup_storage) + size,
487 PAGE_SIZE) >> PAGE_SHIFT;
489 size = map->value_size;
490 *pages = round_up(round_up(size, 8) * num_possible_cpus(),
491 PAGE_SIZE) >> PAGE_SHIFT;
497 struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog,
498 enum bpf_cgroup_storage_type stype)
500 struct bpf_cgroup_storage *storage;
506 map = prog->aux->cgroup_storage[stype];
510 size = bpf_cgroup_storage_calculate_size(map, &pages);
512 if (bpf_map_charge_memlock(map, pages))
513 return ERR_PTR(-EPERM);
515 storage = kmalloc_node(sizeof(struct bpf_cgroup_storage),
516 __GFP_ZERO | GFP_USER, map->numa_node);
520 flags = __GFP_ZERO | GFP_USER;
522 if (stype == BPF_CGROUP_STORAGE_SHARED) {
523 storage->buf = kmalloc_node(size, flags, map->numa_node);
526 check_and_init_map_lock(map, storage->buf->data);
528 storage->percpu_buf = __alloc_percpu_gfp(size, 8, flags);
529 if (!storage->percpu_buf)
533 storage->map = (struct bpf_cgroup_storage_map *)map;
538 bpf_map_uncharge_memlock(map, pages);
540 return ERR_PTR(-ENOMEM);
543 static void free_shared_cgroup_storage_rcu(struct rcu_head *rcu)
545 struct bpf_cgroup_storage *storage =
546 container_of(rcu, struct bpf_cgroup_storage, rcu);
552 static void free_percpu_cgroup_storage_rcu(struct rcu_head *rcu)
554 struct bpf_cgroup_storage *storage =
555 container_of(rcu, struct bpf_cgroup_storage, rcu);
557 free_percpu(storage->percpu_buf);
561 void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage)
563 enum bpf_cgroup_storage_type stype;
570 map = &storage->map->map;
572 bpf_cgroup_storage_calculate_size(map, &pages);
573 bpf_map_uncharge_memlock(map, pages);
575 stype = cgroup_storage_type(map);
576 if (stype == BPF_CGROUP_STORAGE_SHARED)
577 call_rcu(&storage->rcu, free_shared_cgroup_storage_rcu);
579 call_rcu(&storage->rcu, free_percpu_cgroup_storage_rcu);
582 void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
583 struct cgroup *cgroup,
584 enum bpf_attach_type type)
586 struct bpf_cgroup_storage_map *map;
591 storage->key.attach_type = type;
592 storage->key.cgroup_inode_id = cgroup_id(cgroup);
596 spin_lock_bh(&map->lock);
597 WARN_ON(cgroup_storage_insert(map, storage));
598 list_add(&storage->list_map, &map->list);
599 list_add(&storage->list_cg, &cgroup->bpf.storages);
600 spin_unlock_bh(&map->lock);
603 void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage)
605 struct bpf_cgroup_storage_map *map;
606 struct rb_root *root;
613 spin_lock_bh(&map->lock);
615 rb_erase(&storage->node, root);
617 list_del(&storage->list_map);
618 list_del(&storage->list_cg);
619 spin_unlock_bh(&map->lock);