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 DEFINE_PER_CPU(struct bpf_cgroup_storage*, bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);
14 #ifdef CONFIG_CGROUP_BPF
16 #define LOCAL_STORAGE_CREATE_FLAG_MASK \
17 (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
19 struct bpf_cgroup_storage_map {
23 struct bpf_prog *prog;
25 struct list_head list;
28 static struct bpf_cgroup_storage_map *map_to_storage(struct bpf_map *map)
30 return container_of(map, struct bpf_cgroup_storage_map, map);
33 static int bpf_cgroup_storage_key_cmp(
34 const struct bpf_cgroup_storage_key *key1,
35 const struct bpf_cgroup_storage_key *key2)
37 if (key1->cgroup_inode_id < key2->cgroup_inode_id)
39 else if (key1->cgroup_inode_id > key2->cgroup_inode_id)
41 else if (key1->attach_type < key2->attach_type)
43 else if (key1->attach_type > key2->attach_type)
48 static struct bpf_cgroup_storage *cgroup_storage_lookup(
49 struct bpf_cgroup_storage_map *map, struct bpf_cgroup_storage_key *key,
52 struct rb_root *root = &map->root;
56 spin_lock_bh(&map->lock);
60 struct bpf_cgroup_storage *storage;
62 storage = container_of(node, struct bpf_cgroup_storage, node);
64 switch (bpf_cgroup_storage_key_cmp(key, &storage->key)) {
69 node = node->rb_right;
73 spin_unlock_bh(&map->lock);
79 spin_unlock_bh(&map->lock);
84 static int cgroup_storage_insert(struct bpf_cgroup_storage_map *map,
85 struct bpf_cgroup_storage *storage)
87 struct rb_root *root = &map->root;
88 struct rb_node **new = &(root->rb_node), *parent = NULL;
91 struct bpf_cgroup_storage *this;
93 this = container_of(*new, struct bpf_cgroup_storage, node);
96 switch (bpf_cgroup_storage_key_cmp(&storage->key, &this->key)) {
98 new = &((*new)->rb_left);
101 new = &((*new)->rb_right);
108 rb_link_node(&storage->node, parent, new);
109 rb_insert_color(&storage->node, root);
114 static void *cgroup_storage_lookup_elem(struct bpf_map *_map, void *_key)
116 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
117 struct bpf_cgroup_storage_key *key = _key;
118 struct bpf_cgroup_storage *storage;
120 storage = cgroup_storage_lookup(map, key, false);
124 return &READ_ONCE(storage->buf)->data[0];
127 static int cgroup_storage_update_elem(struct bpf_map *map, void *_key,
128 void *value, u64 flags)
130 struct bpf_cgroup_storage_key *key = _key;
131 struct bpf_cgroup_storage *storage;
132 struct bpf_storage_buffer *new;
134 if (flags != BPF_ANY && flags != BPF_EXIST)
137 storage = cgroup_storage_lookup((struct bpf_cgroup_storage_map *)map,
142 new = kmalloc_node(sizeof(struct bpf_storage_buffer) +
144 __GFP_ZERO | GFP_ATOMIC | __GFP_NOWARN,
149 memcpy(&new->data[0], value, map->value_size);
151 new = xchg(&storage->buf, new);
157 int bpf_percpu_cgroup_storage_copy(struct bpf_map *_map, void *_key,
160 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
161 struct bpf_cgroup_storage_key *key = _key;
162 struct bpf_cgroup_storage *storage;
167 storage = cgroup_storage_lookup(map, key, false);
173 /* per_cpu areas are zero-filled and bpf programs can only
174 * access 'value_size' of them, so copying rounded areas
175 * will not leak any kernel data
177 size = round_up(_map->value_size, 8);
178 for_each_possible_cpu(cpu) {
179 bpf_long_memcpy(value + off,
180 per_cpu_ptr(storage->percpu_buf, cpu), size);
187 int bpf_percpu_cgroup_storage_update(struct bpf_map *_map, void *_key,
188 void *value, u64 map_flags)
190 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
191 struct bpf_cgroup_storage_key *key = _key;
192 struct bpf_cgroup_storage *storage;
196 if (map_flags != BPF_ANY && map_flags != BPF_EXIST)
200 storage = cgroup_storage_lookup(map, key, false);
206 /* the user space will provide round_up(value_size, 8) bytes that
207 * will be copied into per-cpu area. bpf programs can only access
208 * value_size of it. During lookup the same extra bytes will be
209 * returned or zeros which were zero-filled by percpu_alloc,
210 * so no kernel data leaks possible
212 size = round_up(_map->value_size, 8);
213 for_each_possible_cpu(cpu) {
214 bpf_long_memcpy(per_cpu_ptr(storage->percpu_buf, cpu),
222 static int cgroup_storage_get_next_key(struct bpf_map *_map, void *_key,
225 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
226 struct bpf_cgroup_storage_key *key = _key;
227 struct bpf_cgroup_storage_key *next = _next_key;
228 struct bpf_cgroup_storage *storage;
230 spin_lock_bh(&map->lock);
232 if (list_empty(&map->list))
236 storage = cgroup_storage_lookup(map, key, true);
240 storage = list_next_entry(storage, list);
244 storage = list_first_entry(&map->list,
245 struct bpf_cgroup_storage, list);
248 spin_unlock_bh(&map->lock);
249 next->attach_type = storage->key.attach_type;
250 next->cgroup_inode_id = storage->key.cgroup_inode_id;
254 spin_unlock_bh(&map->lock);
258 static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
260 int numa_node = bpf_map_attr_numa_node(attr);
261 struct bpf_cgroup_storage_map *map;
263 if (attr->key_size != sizeof(struct bpf_cgroup_storage_key))
264 return ERR_PTR(-EINVAL);
266 if (attr->value_size == 0)
267 return ERR_PTR(-EINVAL);
269 if (attr->value_size > PAGE_SIZE)
270 return ERR_PTR(-E2BIG);
272 if (attr->map_flags & ~LOCAL_STORAGE_CREATE_FLAG_MASK)
273 /* reserved bits should not be used */
274 return ERR_PTR(-EINVAL);
276 if (attr->max_entries)
277 /* max_entries is not used and enforced to be 0 */
278 return ERR_PTR(-EINVAL);
280 map = kmalloc_node(sizeof(struct bpf_cgroup_storage_map),
281 __GFP_ZERO | GFP_USER, numa_node);
283 return ERR_PTR(-ENOMEM);
285 map->map.pages = round_up(sizeof(struct bpf_cgroup_storage_map),
286 PAGE_SIZE) >> PAGE_SHIFT;
288 /* copy mandatory map attributes */
289 bpf_map_init_from_attr(&map->map, attr);
291 spin_lock_init(&map->lock);
293 INIT_LIST_HEAD(&map->list);
298 static void cgroup_storage_map_free(struct bpf_map *_map)
300 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
302 WARN_ON(!RB_EMPTY_ROOT(&map->root));
303 WARN_ON(!list_empty(&map->list));
308 static int cgroup_storage_delete_elem(struct bpf_map *map, void *key)
313 static int cgroup_storage_check_btf(const struct bpf_map *map,
314 const struct btf *btf,
315 const struct btf_type *key_type,
316 const struct btf_type *value_type)
318 struct btf_member *m;
321 /* Key is expected to be of struct bpf_cgroup_storage_key type,
323 * struct bpf_cgroup_storage_key {
324 * __u64 cgroup_inode_id;
330 * Key_type must be a structure with two fields.
332 if (BTF_INFO_KIND(key_type->info) != BTF_KIND_STRUCT ||
333 BTF_INFO_VLEN(key_type->info) != 2)
337 * The first field must be a 64 bit integer at 0 offset.
339 m = (struct btf_member *)(key_type + 1);
340 size = FIELD_SIZEOF(struct bpf_cgroup_storage_key, cgroup_inode_id);
341 if (!btf_member_is_reg_int(btf, key_type, m, 0, size))
345 * The second field must be a 32 bit integer at 64 bit offset.
348 offset = offsetof(struct bpf_cgroup_storage_key, attach_type);
349 size = FIELD_SIZEOF(struct bpf_cgroup_storage_key, attach_type);
350 if (!btf_member_is_reg_int(btf, key_type, m, offset, size))
356 static void cgroup_storage_seq_show_elem(struct bpf_map *map, void *_key,
359 enum bpf_cgroup_storage_type stype = cgroup_storage_type(map);
360 struct bpf_cgroup_storage_key *key = _key;
361 struct bpf_cgroup_storage *storage;
365 storage = cgroup_storage_lookup(map_to_storage(map), key, false);
371 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
372 stype = cgroup_storage_type(map);
373 if (stype == BPF_CGROUP_STORAGE_SHARED) {
375 btf_type_seq_show(map->btf, map->btf_value_type_id,
376 &READ_ONCE(storage->buf)->data[0], m);
379 seq_puts(m, ": {\n");
380 for_each_possible_cpu(cpu) {
381 seq_printf(m, "\tcpu%d: ", cpu);
382 btf_type_seq_show(map->btf, map->btf_value_type_id,
383 per_cpu_ptr(storage->percpu_buf, cpu),
392 const struct bpf_map_ops cgroup_storage_map_ops = {
393 .map_alloc = cgroup_storage_map_alloc,
394 .map_free = cgroup_storage_map_free,
395 .map_get_next_key = cgroup_storage_get_next_key,
396 .map_lookup_elem = cgroup_storage_lookup_elem,
397 .map_update_elem = cgroup_storage_update_elem,
398 .map_delete_elem = cgroup_storage_delete_elem,
399 .map_check_btf = cgroup_storage_check_btf,
400 .map_seq_show_elem = cgroup_storage_seq_show_elem,
403 int bpf_cgroup_storage_assign(struct bpf_prog *prog, struct bpf_map *_map)
405 enum bpf_cgroup_storage_type stype = cgroup_storage_type(_map);
406 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
409 spin_lock_bh(&map->lock);
411 if (map->prog && map->prog != prog)
413 if (prog->aux->cgroup_storage[stype] &&
414 prog->aux->cgroup_storage[stype] != _map)
418 prog->aux->cgroup_storage[stype] = _map;
421 spin_unlock_bh(&map->lock);
426 void bpf_cgroup_storage_release(struct bpf_prog *prog, struct bpf_map *_map)
428 enum bpf_cgroup_storage_type stype = cgroup_storage_type(_map);
429 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
431 spin_lock_bh(&map->lock);
432 if (map->prog == prog) {
433 WARN_ON(prog->aux->cgroup_storage[stype] != _map);
435 prog->aux->cgroup_storage[stype] = NULL;
437 spin_unlock_bh(&map->lock);
440 static size_t bpf_cgroup_storage_calculate_size(struct bpf_map *map, u32 *pages)
444 if (cgroup_storage_type(map) == BPF_CGROUP_STORAGE_SHARED) {
445 size = sizeof(struct bpf_storage_buffer) + map->value_size;
446 *pages = round_up(sizeof(struct bpf_cgroup_storage) + size,
447 PAGE_SIZE) >> PAGE_SHIFT;
449 size = map->value_size;
450 *pages = round_up(round_up(size, 8) * num_possible_cpus(),
451 PAGE_SIZE) >> PAGE_SHIFT;
457 struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog,
458 enum bpf_cgroup_storage_type stype)
460 struct bpf_cgroup_storage *storage;
466 map = prog->aux->cgroup_storage[stype];
470 size = bpf_cgroup_storage_calculate_size(map, &pages);
472 if (bpf_map_charge_memlock(map, pages))
473 return ERR_PTR(-EPERM);
475 storage = kmalloc_node(sizeof(struct bpf_cgroup_storage),
476 __GFP_ZERO | GFP_USER, map->numa_node);
480 flags = __GFP_ZERO | GFP_USER;
482 if (stype == BPF_CGROUP_STORAGE_SHARED) {
483 storage->buf = kmalloc_node(size, flags, map->numa_node);
487 storage->percpu_buf = __alloc_percpu_gfp(size, 8, flags);
488 if (!storage->percpu_buf)
492 storage->map = (struct bpf_cgroup_storage_map *)map;
497 bpf_map_uncharge_memlock(map, pages);
499 return ERR_PTR(-ENOMEM);
502 static void free_shared_cgroup_storage_rcu(struct rcu_head *rcu)
504 struct bpf_cgroup_storage *storage =
505 container_of(rcu, struct bpf_cgroup_storage, rcu);
511 static void free_percpu_cgroup_storage_rcu(struct rcu_head *rcu)
513 struct bpf_cgroup_storage *storage =
514 container_of(rcu, struct bpf_cgroup_storage, rcu);
516 free_percpu(storage->percpu_buf);
520 void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage)
522 enum bpf_cgroup_storage_type stype;
529 map = &storage->map->map;
531 bpf_cgroup_storage_calculate_size(map, &pages);
532 bpf_map_uncharge_memlock(map, pages);
534 stype = cgroup_storage_type(map);
535 if (stype == BPF_CGROUP_STORAGE_SHARED)
536 call_rcu(&storage->rcu, free_shared_cgroup_storage_rcu);
538 call_rcu(&storage->rcu, free_percpu_cgroup_storage_rcu);
541 void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
542 struct cgroup *cgroup,
543 enum bpf_attach_type type)
545 struct bpf_cgroup_storage_map *map;
550 storage->key.attach_type = type;
551 storage->key.cgroup_inode_id = cgroup->kn->id.id;
555 spin_lock_bh(&map->lock);
556 WARN_ON(cgroup_storage_insert(map, storage));
557 list_add(&storage->list, &map->list);
558 spin_unlock_bh(&map->lock);
561 void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage)
563 struct bpf_cgroup_storage_map *map;
564 struct rb_root *root;
571 spin_lock_bh(&map->lock);
573 rb_erase(&storage->node, root);
575 list_del(&storage->list);
576 spin_unlock_bh(&map->lock);