1 //SPDX-License-Identifier: GPL-2.0
2 #include <linux/bpf-cgroup.h>
5 #include <linux/filter.h>
7 #include <linux/rbtree.h>
8 #include <linux/slab.h>
10 DEFINE_PER_CPU(void*, bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);
12 #ifdef CONFIG_CGROUP_BPF
14 #define LOCAL_STORAGE_CREATE_FLAG_MASK \
15 (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
17 struct bpf_cgroup_storage_map {
21 struct bpf_prog *prog;
23 struct list_head list;
26 static struct bpf_cgroup_storage_map *map_to_storage(struct bpf_map *map)
28 return container_of(map, struct bpf_cgroup_storage_map, map);
31 static int bpf_cgroup_storage_key_cmp(
32 const struct bpf_cgroup_storage_key *key1,
33 const struct bpf_cgroup_storage_key *key2)
35 if (key1->cgroup_inode_id < key2->cgroup_inode_id)
37 else if (key1->cgroup_inode_id > key2->cgroup_inode_id)
39 else if (key1->attach_type < key2->attach_type)
41 else if (key1->attach_type > key2->attach_type)
46 static struct bpf_cgroup_storage *cgroup_storage_lookup(
47 struct bpf_cgroup_storage_map *map, struct bpf_cgroup_storage_key *key,
50 struct rb_root *root = &map->root;
54 spin_lock_bh(&map->lock);
58 struct bpf_cgroup_storage *storage;
60 storage = container_of(node, struct bpf_cgroup_storage, node);
62 switch (bpf_cgroup_storage_key_cmp(key, &storage->key)) {
67 node = node->rb_right;
71 spin_unlock_bh(&map->lock);
77 spin_unlock_bh(&map->lock);
82 static int cgroup_storage_insert(struct bpf_cgroup_storage_map *map,
83 struct bpf_cgroup_storage *storage)
85 struct rb_root *root = &map->root;
86 struct rb_node **new = &(root->rb_node), *parent = NULL;
89 struct bpf_cgroup_storage *this;
91 this = container_of(*new, struct bpf_cgroup_storage, node);
94 switch (bpf_cgroup_storage_key_cmp(&storage->key, &this->key)) {
96 new = &((*new)->rb_left);
99 new = &((*new)->rb_right);
106 rb_link_node(&storage->node, parent, new);
107 rb_insert_color(&storage->node, root);
112 static void *cgroup_storage_lookup_elem(struct bpf_map *_map, void *_key)
114 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
115 struct bpf_cgroup_storage_key *key = _key;
116 struct bpf_cgroup_storage *storage;
118 storage = cgroup_storage_lookup(map, key, false);
122 return &READ_ONCE(storage->buf)->data[0];
125 static int cgroup_storage_update_elem(struct bpf_map *map, void *_key,
126 void *value, u64 flags)
128 struct bpf_cgroup_storage_key *key = _key;
129 struct bpf_cgroup_storage *storage;
130 struct bpf_storage_buffer *new;
132 if (flags & BPF_NOEXIST)
135 storage = cgroup_storage_lookup((struct bpf_cgroup_storage_map *)map,
140 new = kmalloc_node(sizeof(struct bpf_storage_buffer) +
141 map->value_size, __GFP_ZERO | GFP_USER,
146 memcpy(&new->data[0], value, map->value_size);
148 new = xchg(&storage->buf, new);
154 static int cgroup_storage_get_next_key(struct bpf_map *_map, void *_key,
157 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
158 struct bpf_cgroup_storage_key *key = _key;
159 struct bpf_cgroup_storage_key *next = _next_key;
160 struct bpf_cgroup_storage *storage;
162 spin_lock_bh(&map->lock);
164 if (list_empty(&map->list))
168 storage = cgroup_storage_lookup(map, key, true);
172 storage = list_next_entry(storage, list);
176 storage = list_first_entry(&map->list,
177 struct bpf_cgroup_storage, list);
180 spin_unlock_bh(&map->lock);
181 next->attach_type = storage->key.attach_type;
182 next->cgroup_inode_id = storage->key.cgroup_inode_id;
186 spin_unlock_bh(&map->lock);
190 static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
192 int numa_node = bpf_map_attr_numa_node(attr);
193 struct bpf_cgroup_storage_map *map;
195 if (attr->key_size != sizeof(struct bpf_cgroup_storage_key))
196 return ERR_PTR(-EINVAL);
198 if (attr->value_size > PAGE_SIZE)
199 return ERR_PTR(-E2BIG);
201 if (attr->map_flags & ~LOCAL_STORAGE_CREATE_FLAG_MASK)
202 /* reserved bits should not be used */
203 return ERR_PTR(-EINVAL);
205 if (attr->max_entries)
206 /* max_entries is not used and enforced to be 0 */
207 return ERR_PTR(-EINVAL);
209 map = kmalloc_node(sizeof(struct bpf_cgroup_storage_map),
210 __GFP_ZERO | GFP_USER, numa_node);
212 return ERR_PTR(-ENOMEM);
214 map->map.pages = round_up(sizeof(struct bpf_cgroup_storage_map),
215 PAGE_SIZE) >> PAGE_SHIFT;
217 /* copy mandatory map attributes */
218 bpf_map_init_from_attr(&map->map, attr);
220 spin_lock_init(&map->lock);
222 INIT_LIST_HEAD(&map->list);
227 static void cgroup_storage_map_free(struct bpf_map *_map)
229 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
231 WARN_ON(!RB_EMPTY_ROOT(&map->root));
232 WARN_ON(!list_empty(&map->list));
237 static int cgroup_storage_delete_elem(struct bpf_map *map, void *key)
242 const struct bpf_map_ops cgroup_storage_map_ops = {
243 .map_alloc = cgroup_storage_map_alloc,
244 .map_free = cgroup_storage_map_free,
245 .map_get_next_key = cgroup_storage_get_next_key,
246 .map_lookup_elem = cgroup_storage_lookup_elem,
247 .map_update_elem = cgroup_storage_update_elem,
248 .map_delete_elem = cgroup_storage_delete_elem,
249 .map_check_btf = map_check_no_btf,
252 int bpf_cgroup_storage_assign(struct bpf_prog *prog, struct bpf_map *_map)
254 enum bpf_cgroup_storage_type stype = cgroup_storage_type(_map);
255 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
258 spin_lock_bh(&map->lock);
260 if (map->prog && map->prog != prog)
262 if (prog->aux->cgroup_storage[stype] &&
263 prog->aux->cgroup_storage[stype] != _map)
267 prog->aux->cgroup_storage[stype] = _map;
270 spin_unlock_bh(&map->lock);
275 void bpf_cgroup_storage_release(struct bpf_prog *prog, struct bpf_map *_map)
277 enum bpf_cgroup_storage_type stype = cgroup_storage_type(_map);
278 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
280 spin_lock_bh(&map->lock);
281 if (map->prog == prog) {
282 WARN_ON(prog->aux->cgroup_storage[stype] != _map);
284 prog->aux->cgroup_storage[stype] = NULL;
286 spin_unlock_bh(&map->lock);
289 struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog,
290 enum bpf_cgroup_storage_type stype)
292 struct bpf_cgroup_storage *storage;
296 map = prog->aux->cgroup_storage[stype];
300 pages = round_up(sizeof(struct bpf_cgroup_storage) +
301 sizeof(struct bpf_storage_buffer) +
302 map->value_size, PAGE_SIZE) >> PAGE_SHIFT;
303 if (bpf_map_charge_memlock(map, pages))
304 return ERR_PTR(-EPERM);
306 storage = kmalloc_node(sizeof(struct bpf_cgroup_storage),
307 __GFP_ZERO | GFP_USER, map->numa_node);
309 bpf_map_uncharge_memlock(map, pages);
310 return ERR_PTR(-ENOMEM);
313 storage->buf = kmalloc_node(sizeof(struct bpf_storage_buffer) +
314 map->value_size, __GFP_ZERO | GFP_USER,
317 bpf_map_uncharge_memlock(map, pages);
319 return ERR_PTR(-ENOMEM);
322 storage->map = (struct bpf_cgroup_storage_map *)map;
327 void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage)
335 map = &storage->map->map;
336 pages = round_up(sizeof(struct bpf_cgroup_storage) +
337 sizeof(struct bpf_storage_buffer) +
338 map->value_size, PAGE_SIZE) >> PAGE_SHIFT;
339 bpf_map_uncharge_memlock(map, pages);
341 kfree_rcu(storage->buf, rcu);
342 kfree_rcu(storage, rcu);
345 void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
346 struct cgroup *cgroup,
347 enum bpf_attach_type type)
349 struct bpf_cgroup_storage_map *map;
354 storage->key.attach_type = type;
355 storage->key.cgroup_inode_id = cgroup->kn->id.id;
359 spin_lock_bh(&map->lock);
360 WARN_ON(cgroup_storage_insert(map, storage));
361 list_add(&storage->list, &map->list);
362 spin_unlock_bh(&map->lock);
365 void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage)
367 struct bpf_cgroup_storage_map *map;
368 struct rb_root *root;
375 spin_lock_bh(&map->lock);
377 rb_erase(&storage->node, root);
379 list_del(&storage->list);
380 spin_unlock_bh(&map->lock);