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 #ifdef CONFIG_CGROUP_BPF
12 #define LOCAL_STORAGE_CREATE_FLAG_MASK \
13 (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
15 struct bpf_cgroup_storage_map {
19 struct bpf_prog *prog;
21 struct list_head list;
24 static struct bpf_cgroup_storage_map *map_to_storage(struct bpf_map *map)
26 return container_of(map, struct bpf_cgroup_storage_map, map);
29 static int bpf_cgroup_storage_key_cmp(
30 const struct bpf_cgroup_storage_key *key1,
31 const struct bpf_cgroup_storage_key *key2)
33 if (key1->cgroup_inode_id < key2->cgroup_inode_id)
35 else if (key1->cgroup_inode_id > key2->cgroup_inode_id)
37 else if (key1->attach_type < key2->attach_type)
39 else if (key1->attach_type > key2->attach_type)
44 static struct bpf_cgroup_storage *cgroup_storage_lookup(
45 struct bpf_cgroup_storage_map *map, struct bpf_cgroup_storage_key *key,
48 struct rb_root *root = &map->root;
52 spin_lock_bh(&map->lock);
56 struct bpf_cgroup_storage *storage;
58 storage = container_of(node, struct bpf_cgroup_storage, node);
60 switch (bpf_cgroup_storage_key_cmp(key, &storage->key)) {
65 node = node->rb_right;
69 spin_unlock_bh(&map->lock);
75 spin_unlock_bh(&map->lock);
80 static int cgroup_storage_insert(struct bpf_cgroup_storage_map *map,
81 struct bpf_cgroup_storage *storage)
83 struct rb_root *root = &map->root;
84 struct rb_node **new = &(root->rb_node), *parent = NULL;
87 struct bpf_cgroup_storage *this;
89 this = container_of(*new, struct bpf_cgroup_storage, node);
92 switch (bpf_cgroup_storage_key_cmp(&storage->key, &this->key)) {
94 new = &((*new)->rb_left);
97 new = &((*new)->rb_right);
104 rb_link_node(&storage->node, parent, new);
105 rb_insert_color(&storage->node, root);
110 static void *cgroup_storage_lookup_elem(struct bpf_map *_map, void *_key)
112 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
113 struct bpf_cgroup_storage_key *key = _key;
114 struct bpf_cgroup_storage *storage;
116 storage = cgroup_storage_lookup(map, key, false);
120 return &READ_ONCE(storage->buf)->data[0];
123 static int cgroup_storage_update_elem(struct bpf_map *map, void *_key,
124 void *value, u64 flags)
126 struct bpf_cgroup_storage_key *key = _key;
127 struct bpf_cgroup_storage *storage;
128 struct bpf_storage_buffer *new;
130 if (flags & BPF_NOEXIST)
133 storage = cgroup_storage_lookup((struct bpf_cgroup_storage_map *)map,
138 new = kmalloc_node(sizeof(struct bpf_storage_buffer) +
139 map->value_size, __GFP_ZERO | GFP_USER,
144 memcpy(&new->data[0], value, map->value_size);
146 new = xchg(&storage->buf, new);
152 static int cgroup_storage_get_next_key(struct bpf_map *_map, void *_key,
155 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
156 struct bpf_cgroup_storage_key *key = _key;
157 struct bpf_cgroup_storage_key *next = _next_key;
158 struct bpf_cgroup_storage *storage;
160 spin_lock_bh(&map->lock);
162 if (list_empty(&map->list))
166 storage = cgroup_storage_lookup(map, key, true);
170 storage = list_next_entry(storage, list);
174 storage = list_first_entry(&map->list,
175 struct bpf_cgroup_storage, list);
178 spin_unlock_bh(&map->lock);
179 next->attach_type = storage->key.attach_type;
180 next->cgroup_inode_id = storage->key.cgroup_inode_id;
184 spin_unlock_bh(&map->lock);
188 static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
190 int numa_node = bpf_map_attr_numa_node(attr);
191 struct bpf_cgroup_storage_map *map;
193 if (attr->key_size != sizeof(struct bpf_cgroup_storage_key))
194 return ERR_PTR(-EINVAL);
196 if (attr->value_size > PAGE_SIZE)
197 return ERR_PTR(-E2BIG);
199 if (attr->map_flags & ~LOCAL_STORAGE_CREATE_FLAG_MASK)
200 /* reserved bits should not be used */
201 return ERR_PTR(-EINVAL);
203 if (attr->max_entries)
204 /* max_entries is not used and enforced to be 0 */
205 return ERR_PTR(-EINVAL);
207 map = kmalloc_node(sizeof(struct bpf_cgroup_storage_map),
208 __GFP_ZERO | GFP_USER, numa_node);
210 return ERR_PTR(-ENOMEM);
212 map->map.pages = round_up(sizeof(struct bpf_cgroup_storage_map),
213 PAGE_SIZE) >> PAGE_SHIFT;
215 /* copy mandatory map attributes */
216 bpf_map_init_from_attr(&map->map, attr);
218 spin_lock_init(&map->lock);
220 INIT_LIST_HEAD(&map->list);
225 static void cgroup_storage_map_free(struct bpf_map *_map)
227 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
229 WARN_ON(!RB_EMPTY_ROOT(&map->root));
230 WARN_ON(!list_empty(&map->list));
235 static int cgroup_storage_delete_elem(struct bpf_map *map, void *key)
240 const struct bpf_map_ops cgroup_storage_map_ops = {
241 .map_alloc = cgroup_storage_map_alloc,
242 .map_free = cgroup_storage_map_free,
243 .map_get_next_key = cgroup_storage_get_next_key,
244 .map_lookup_elem = cgroup_storage_lookup_elem,
245 .map_update_elem = cgroup_storage_update_elem,
246 .map_delete_elem = cgroup_storage_delete_elem,
249 int bpf_cgroup_storage_assign(struct bpf_prog *prog, struct bpf_map *_map)
251 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
254 spin_lock_bh(&map->lock);
256 if (map->prog && map->prog != prog)
258 if (prog->aux->cgroup_storage && prog->aux->cgroup_storage != _map)
262 prog->aux->cgroup_storage = _map;
265 spin_unlock_bh(&map->lock);
270 void bpf_cgroup_storage_release(struct bpf_prog *prog, struct bpf_map *_map)
272 struct bpf_cgroup_storage_map *map = map_to_storage(_map);
274 spin_lock_bh(&map->lock);
275 if (map->prog == prog) {
276 WARN_ON(prog->aux->cgroup_storage != _map);
278 prog->aux->cgroup_storage = NULL;
280 spin_unlock_bh(&map->lock);
283 struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog)
285 struct bpf_cgroup_storage *storage;
289 map = prog->aux->cgroup_storage;
293 pages = round_up(sizeof(struct bpf_cgroup_storage) +
294 sizeof(struct bpf_storage_buffer) +
295 map->value_size, PAGE_SIZE) >> PAGE_SHIFT;
296 if (bpf_map_charge_memlock(map, pages))
297 return ERR_PTR(-EPERM);
299 storage = kmalloc_node(sizeof(struct bpf_cgroup_storage),
300 __GFP_ZERO | GFP_USER, map->numa_node);
302 bpf_map_uncharge_memlock(map, pages);
303 return ERR_PTR(-ENOMEM);
306 storage->buf = kmalloc_node(sizeof(struct bpf_storage_buffer) +
307 map->value_size, __GFP_ZERO | GFP_USER,
310 bpf_map_uncharge_memlock(map, pages);
312 return ERR_PTR(-ENOMEM);
315 storage->map = (struct bpf_cgroup_storage_map *)map;
320 void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage)
328 map = &storage->map->map;
329 pages = round_up(sizeof(struct bpf_cgroup_storage) +
330 sizeof(struct bpf_storage_buffer) +
331 map->value_size, PAGE_SIZE) >> PAGE_SHIFT;
332 bpf_map_uncharge_memlock(map, pages);
334 kfree_rcu(storage->buf, rcu);
335 kfree_rcu(storage, rcu);
338 void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
339 struct cgroup *cgroup,
340 enum bpf_attach_type type)
342 struct bpf_cgroup_storage_map *map;
347 storage->key.attach_type = type;
348 storage->key.cgroup_inode_id = cgroup->kn->id.id;
352 spin_lock_bh(&map->lock);
353 WARN_ON(cgroup_storage_insert(map, storage));
354 list_add(&storage->list, &map->list);
355 spin_unlock_bh(&map->lock);
358 void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage)
360 struct bpf_cgroup_storage_map *map;
361 struct rb_root *root;
368 spin_lock_bh(&map->lock);
370 rb_erase(&storage->node, root);
372 list_del(&storage->list);
373 spin_unlock_bh(&map->lock);