1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 #include <linux/bpf.h>
13 #include <linux/bpf_trace.h>
14 #include <linux/bpf_lirc.h>
15 #include <linux/btf.h>
16 #include <linux/syscalls.h>
17 #include <linux/slab.h>
18 #include <linux/sched/signal.h>
19 #include <linux/vmalloc.h>
20 #include <linux/mmzone.h>
21 #include <linux/anon_inodes.h>
22 #include <linux/fdtable.h>
23 #include <linux/file.h>
25 #include <linux/license.h>
26 #include <linux/filter.h>
27 #include <linux/version.h>
28 #include <linux/kernel.h>
29 #include <linux/idr.h>
30 #include <linux/cred.h>
31 #include <linux/timekeeping.h>
32 #include <linux/ctype.h>
33 #include <linux/nospec.h>
35 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
36 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
38 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
39 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
40 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
42 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
44 DEFINE_PER_CPU(int, bpf_prog_active);
45 static DEFINE_IDR(prog_idr);
46 static DEFINE_SPINLOCK(prog_idr_lock);
47 static DEFINE_IDR(map_idr);
48 static DEFINE_SPINLOCK(map_idr_lock);
50 int sysctl_unprivileged_bpf_disabled __read_mostly;
52 static const struct bpf_map_ops * const bpf_map_types[] = {
53 #define BPF_PROG_TYPE(_id, _ops)
54 #define BPF_MAP_TYPE(_id, _ops) \
56 #include <linux/bpf_types.h>
62 * If we're handed a bigger struct than we know of, ensure all the unknown bits
63 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
64 * we don't know about yet.
66 * There is a ToCToU between this function call and the following
67 * copy_from_user() call. However, this is not a concern since this function is
68 * meant to be a future-proofing of bits.
70 int bpf_check_uarg_tail_zero(void __user *uaddr,
74 unsigned char __user *addr;
75 unsigned char __user *end;
79 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
82 if (unlikely(!access_ok(uaddr, actual_size)))
85 if (actual_size <= expected_size)
88 addr = uaddr + expected_size;
89 end = uaddr + actual_size;
91 for (; addr < end; addr++) {
92 err = get_user(val, addr);
102 const struct bpf_map_ops bpf_map_offload_ops = {
103 .map_alloc = bpf_map_offload_map_alloc,
104 .map_free = bpf_map_offload_map_free,
105 .map_check_btf = map_check_no_btf,
108 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
110 const struct bpf_map_ops *ops;
111 u32 type = attr->map_type;
115 if (type >= ARRAY_SIZE(bpf_map_types))
116 return ERR_PTR(-EINVAL);
117 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
118 ops = bpf_map_types[type];
120 return ERR_PTR(-EINVAL);
122 if (ops->map_alloc_check) {
123 err = ops->map_alloc_check(attr);
127 if (attr->map_ifindex)
128 ops = &bpf_map_offload_ops;
129 map = ops->map_alloc(attr);
133 map->map_type = type;
137 void *bpf_map_area_alloc(size_t size, int numa_node)
139 /* We really just want to fail instead of triggering OOM killer
140 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
141 * which is used for lower order allocation requests.
143 * It has been observed that higher order allocation requests done by
144 * vmalloc with __GFP_NORETRY being set might fail due to not trying
145 * to reclaim memory from the page cache, thus we set
146 * __GFP_RETRY_MAYFAIL to avoid such situations.
149 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
152 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
153 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
159 return __vmalloc_node_flags_caller(size, numa_node,
160 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
161 flags, __builtin_return_address(0));
164 void bpf_map_area_free(void *area)
169 static u32 bpf_map_flags_retain_permanent(u32 flags)
171 /* Some map creation flags are not tied to the map object but
172 * rather to the map fd instead, so they have no meaning upon
173 * map object inspection since multiple file descriptors with
174 * different (access) properties can exist here. Thus, given
175 * this has zero meaning for the map itself, lets clear these
178 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
181 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
183 map->map_type = attr->map_type;
184 map->key_size = attr->key_size;
185 map->value_size = attr->value_size;
186 map->max_entries = attr->max_entries;
187 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
188 map->numa_node = bpf_map_attr_numa_node(attr);
191 int bpf_map_precharge_memlock(u32 pages)
193 struct user_struct *user = get_current_user();
194 unsigned long memlock_limit, cur;
196 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
197 cur = atomic_long_read(&user->locked_vm);
199 if (cur + pages > memlock_limit)
204 static int bpf_charge_memlock(struct user_struct *user, u32 pages)
206 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
208 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
209 atomic_long_sub(pages, &user->locked_vm);
215 static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
217 atomic_long_sub(pages, &user->locked_vm);
220 static int bpf_map_init_memlock(struct bpf_map *map)
222 struct user_struct *user = get_current_user();
225 ret = bpf_charge_memlock(user, map->pages);
234 static void bpf_map_release_memlock(struct bpf_map *map)
236 struct user_struct *user = map->user;
237 bpf_uncharge_memlock(user, map->pages);
241 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
245 ret = bpf_charge_memlock(map->user, pages);
252 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
254 bpf_uncharge_memlock(map->user, pages);
258 static int bpf_map_alloc_id(struct bpf_map *map)
262 idr_preload(GFP_KERNEL);
263 spin_lock_bh(&map_idr_lock);
264 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
267 spin_unlock_bh(&map_idr_lock);
270 if (WARN_ON_ONCE(!id))
273 return id > 0 ? 0 : id;
276 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
280 /* Offloaded maps are removed from the IDR store when their device
281 * disappears - even if someone holds an fd to them they are unusable,
282 * the memory is gone, all ops will fail; they are simply waiting for
283 * refcnt to drop to be freed.
289 spin_lock_irqsave(&map_idr_lock, flags);
291 __acquire(&map_idr_lock);
293 idr_remove(&map_idr, map->id);
297 spin_unlock_irqrestore(&map_idr_lock, flags);
299 __release(&map_idr_lock);
302 /* called from workqueue */
303 static void bpf_map_free_deferred(struct work_struct *work)
305 struct bpf_map *map = container_of(work, struct bpf_map, work);
307 bpf_map_release_memlock(map);
308 security_bpf_map_free(map);
309 /* implementation dependent freeing */
310 map->ops->map_free(map);
313 static void bpf_map_put_uref(struct bpf_map *map)
315 if (atomic_dec_and_test(&map->usercnt)) {
316 if (map->ops->map_release_uref)
317 map->ops->map_release_uref(map);
321 /* decrement map refcnt and schedule it for freeing via workqueue
322 * (unrelying map implementation ops->map_free() might sleep)
324 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
326 if (atomic_dec_and_test(&map->refcnt)) {
327 /* bpf_map_free_id() must be called first */
328 bpf_map_free_id(map, do_idr_lock);
330 INIT_WORK(&map->work, bpf_map_free_deferred);
331 schedule_work(&map->work);
335 void bpf_map_put(struct bpf_map *map)
337 __bpf_map_put(map, true);
339 EXPORT_SYMBOL_GPL(bpf_map_put);
341 void bpf_map_put_with_uref(struct bpf_map *map)
343 bpf_map_put_uref(map);
347 static int bpf_map_release(struct inode *inode, struct file *filp)
349 struct bpf_map *map = filp->private_data;
351 if (map->ops->map_release)
352 map->ops->map_release(map, filp);
354 bpf_map_put_with_uref(map);
358 #ifdef CONFIG_PROC_FS
359 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
361 const struct bpf_map *map = filp->private_data;
362 const struct bpf_array *array;
363 u32 owner_prog_type = 0;
366 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
367 array = container_of(map, struct bpf_array, map);
368 owner_prog_type = array->owner_prog_type;
369 owner_jited = array->owner_jited;
385 map->pages * 1ULL << PAGE_SHIFT,
388 if (owner_prog_type) {
389 seq_printf(m, "owner_prog_type:\t%u\n",
391 seq_printf(m, "owner_jited:\t%u\n",
397 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
400 /* We need this handler such that alloc_file() enables
401 * f_mode with FMODE_CAN_READ.
406 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
407 size_t siz, loff_t *ppos)
409 /* We need this handler such that alloc_file() enables
410 * f_mode with FMODE_CAN_WRITE.
415 const struct file_operations bpf_map_fops = {
416 #ifdef CONFIG_PROC_FS
417 .show_fdinfo = bpf_map_show_fdinfo,
419 .release = bpf_map_release,
420 .read = bpf_dummy_read,
421 .write = bpf_dummy_write,
424 int bpf_map_new_fd(struct bpf_map *map, int flags)
428 ret = security_bpf_map(map, OPEN_FMODE(flags));
432 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
436 int bpf_get_file_flag(int flags)
438 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
440 if (flags & BPF_F_RDONLY)
442 if (flags & BPF_F_WRONLY)
447 /* helper macro to check that unused fields 'union bpf_attr' are zero */
448 #define CHECK_ATTR(CMD) \
449 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
450 sizeof(attr->CMD##_LAST_FIELD), 0, \
452 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
453 sizeof(attr->CMD##_LAST_FIELD)) != NULL
455 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
456 * Return 0 on success and < 0 on error.
458 static int bpf_obj_name_cpy(char *dst, const char *src)
460 const char *end = src + BPF_OBJ_NAME_LEN;
462 memset(dst, 0, BPF_OBJ_NAME_LEN);
464 /* Copy all isalnum() and '_' char */
465 while (src < end && *src) {
466 if (!isalnum(*src) && *src != '_')
471 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
478 int map_check_no_btf(const struct bpf_map *map,
479 const struct btf *btf,
480 const struct btf_type *key_type,
481 const struct btf_type *value_type)
486 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
487 u32 btf_key_id, u32 btf_value_id)
489 const struct btf_type *key_type, *value_type;
490 u32 key_size, value_size;
493 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
494 if (!key_type || key_size != map->key_size)
497 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
498 if (!value_type || value_size != map->value_size)
501 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
503 if (map_value_has_spin_lock(map)) {
504 if (map->map_type != BPF_MAP_TYPE_HASH &&
505 map->map_type != BPF_MAP_TYPE_ARRAY &&
506 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE)
508 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
511 "verifier bug spin_lock_off %d value_size %d\n",
512 map->spin_lock_off, map->value_size);
517 if (map->ops->map_check_btf)
518 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
523 #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
524 /* called via syscall */
525 static int map_create(union bpf_attr *attr)
527 int numa_node = bpf_map_attr_numa_node(attr);
532 err = CHECK_ATTR(BPF_MAP_CREATE);
536 f_flags = bpf_get_file_flag(attr->map_flags);
540 if (numa_node != NUMA_NO_NODE &&
541 ((unsigned int)numa_node >= nr_node_ids ||
542 !node_online(numa_node)))
545 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
546 map = find_and_alloc_map(attr);
550 err = bpf_obj_name_cpy(map->name, attr->map_name);
552 goto free_map_nouncharge;
554 atomic_set(&map->refcnt, 1);
555 atomic_set(&map->usercnt, 1);
557 if (attr->btf_key_type_id || attr->btf_value_type_id) {
560 if (!attr->btf_key_type_id || !attr->btf_value_type_id) {
562 goto free_map_nouncharge;
565 btf = btf_get_by_fd(attr->btf_fd);
568 goto free_map_nouncharge;
571 err = map_check_btf(map, btf, attr->btf_key_type_id,
572 attr->btf_value_type_id);
575 goto free_map_nouncharge;
579 map->btf_key_type_id = attr->btf_key_type_id;
580 map->btf_value_type_id = attr->btf_value_type_id;
582 map->spin_lock_off = -EINVAL;
585 err = security_bpf_map_alloc(map);
587 goto free_map_nouncharge;
589 err = bpf_map_init_memlock(map);
593 err = bpf_map_alloc_id(map);
597 err = bpf_map_new_fd(map, f_flags);
599 /* failed to allocate fd.
600 * bpf_map_put_with_uref() is needed because the above
601 * bpf_map_alloc_id() has published the map
602 * to the userspace and the userspace may
603 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
605 bpf_map_put_with_uref(map);
612 bpf_map_release_memlock(map);
614 security_bpf_map_free(map);
617 map->ops->map_free(map);
621 /* if error is returned, fd is released.
622 * On success caller should complete fd access with matching fdput()
624 struct bpf_map *__bpf_map_get(struct fd f)
627 return ERR_PTR(-EBADF);
628 if (f.file->f_op != &bpf_map_fops) {
630 return ERR_PTR(-EINVAL);
633 return f.file->private_data;
636 /* prog's and map's refcnt limit */
637 #define BPF_MAX_REFCNT 32768
639 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
641 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
642 atomic_dec(&map->refcnt);
643 return ERR_PTR(-EBUSY);
646 atomic_inc(&map->usercnt);
649 EXPORT_SYMBOL_GPL(bpf_map_inc);
651 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
653 struct fd f = fdget(ufd);
656 map = __bpf_map_get(f);
660 map = bpf_map_inc(map, true);
666 /* map_idr_lock should have been held */
667 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
672 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
674 if (refold >= BPF_MAX_REFCNT) {
675 __bpf_map_put(map, false);
676 return ERR_PTR(-EBUSY);
680 return ERR_PTR(-ENOENT);
683 atomic_inc(&map->usercnt);
688 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
693 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
696 return memdup_user(ukey, key_size);
699 return ERR_PTR(-EINVAL);
704 /* last field in 'union bpf_attr' used by this command */
705 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
707 static int map_lookup_elem(union bpf_attr *attr)
709 void __user *ukey = u64_to_user_ptr(attr->key);
710 void __user *uvalue = u64_to_user_ptr(attr->value);
711 int ufd = attr->map_fd;
713 void *key, *value, *ptr;
718 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
721 if (attr->flags & ~BPF_F_LOCK)
725 map = __bpf_map_get(f);
729 if (!(f.file->f_mode & FMODE_CAN_READ)) {
734 if ((attr->flags & BPF_F_LOCK) &&
735 !map_value_has_spin_lock(map)) {
740 key = __bpf_copy_key(ukey, map->key_size);
746 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
747 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
748 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
749 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
750 value_size = round_up(map->value_size, 8) * num_possible_cpus();
751 else if (IS_FD_MAP(map))
752 value_size = sizeof(u32);
754 value_size = map->value_size;
757 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
761 if (bpf_map_is_dev_bound(map)) {
762 err = bpf_map_offload_lookup_elem(map, key, value);
767 this_cpu_inc(bpf_prog_active);
768 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
769 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
770 err = bpf_percpu_hash_copy(map, key, value);
771 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
772 err = bpf_percpu_array_copy(map, key, value);
773 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
774 err = bpf_percpu_cgroup_storage_copy(map, key, value);
775 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
776 err = bpf_stackmap_copy(map, key, value);
777 } else if (IS_FD_ARRAY(map)) {
778 err = bpf_fd_array_map_lookup_elem(map, key, value);
779 } else if (IS_FD_HASH(map)) {
780 err = bpf_fd_htab_map_lookup_elem(map, key, value);
781 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
782 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
783 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
784 map->map_type == BPF_MAP_TYPE_STACK) {
785 err = map->ops->map_peek_elem(map, value);
788 ptr = map->ops->map_lookup_elem(map, key);
795 if (attr->flags & BPF_F_LOCK)
796 /* lock 'ptr' and copy everything but lock */
797 copy_map_value_locked(map, value, ptr, true);
799 copy_map_value(map, value, ptr);
800 /* mask lock, since value wasn't zero inited */
801 check_and_init_map_lock(map, value);
805 this_cpu_dec(bpf_prog_active);
813 if (copy_to_user(uvalue, value, value_size) != 0)
827 static void maybe_wait_bpf_programs(struct bpf_map *map)
829 /* Wait for any running BPF programs to complete so that
830 * userspace, when we return to it, knows that all programs
831 * that could be running use the new map value.
833 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
834 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
838 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
840 static int map_update_elem(union bpf_attr *attr)
842 void __user *ukey = u64_to_user_ptr(attr->key);
843 void __user *uvalue = u64_to_user_ptr(attr->value);
844 int ufd = attr->map_fd;
851 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
855 map = __bpf_map_get(f);
859 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
864 if ((attr->flags & BPF_F_LOCK) &&
865 !map_value_has_spin_lock(map)) {
870 key = __bpf_copy_key(ukey, map->key_size);
876 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
877 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
878 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
879 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
880 value_size = round_up(map->value_size, 8) * num_possible_cpus();
882 value_size = map->value_size;
885 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
890 if (copy_from_user(value, uvalue, value_size) != 0)
893 /* Need to create a kthread, thus must support schedule */
894 if (bpf_map_is_dev_bound(map)) {
895 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
897 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
898 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
899 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
900 err = map->ops->map_update_elem(map, key, value, attr->flags);
904 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
905 * inside bpf map update or delete otherwise deadlocks are possible
908 __this_cpu_inc(bpf_prog_active);
909 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
910 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
911 err = bpf_percpu_hash_update(map, key, value, attr->flags);
912 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
913 err = bpf_percpu_array_update(map, key, value, attr->flags);
914 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
915 err = bpf_percpu_cgroup_storage_update(map, key, value,
917 } else if (IS_FD_ARRAY(map)) {
919 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
922 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
924 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
927 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
928 /* rcu_read_lock() is not needed */
929 err = bpf_fd_reuseport_array_update_elem(map, key, value,
931 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
932 map->map_type == BPF_MAP_TYPE_STACK) {
933 err = map->ops->map_push_elem(map, value, attr->flags);
936 err = map->ops->map_update_elem(map, key, value, attr->flags);
939 __this_cpu_dec(bpf_prog_active);
941 maybe_wait_bpf_programs(map);
952 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
954 static int map_delete_elem(union bpf_attr *attr)
956 void __user *ukey = u64_to_user_ptr(attr->key);
957 int ufd = attr->map_fd;
963 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
967 map = __bpf_map_get(f);
971 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
976 key = __bpf_copy_key(ukey, map->key_size);
982 if (bpf_map_is_dev_bound(map)) {
983 err = bpf_map_offload_delete_elem(map, key);
988 __this_cpu_inc(bpf_prog_active);
990 err = map->ops->map_delete_elem(map, key);
992 __this_cpu_dec(bpf_prog_active);
994 maybe_wait_bpf_programs(map);
1002 /* last field in 'union bpf_attr' used by this command */
1003 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1005 static int map_get_next_key(union bpf_attr *attr)
1007 void __user *ukey = u64_to_user_ptr(attr->key);
1008 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1009 int ufd = attr->map_fd;
1010 struct bpf_map *map;
1011 void *key, *next_key;
1015 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1019 map = __bpf_map_get(f);
1021 return PTR_ERR(map);
1023 if (!(f.file->f_mode & FMODE_CAN_READ)) {
1029 key = __bpf_copy_key(ukey, map->key_size);
1039 next_key = kmalloc(map->key_size, GFP_USER);
1043 if (bpf_map_is_dev_bound(map)) {
1044 err = bpf_map_offload_get_next_key(map, key, next_key);
1049 err = map->ops->map_get_next_key(map, key, next_key);
1056 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1070 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1072 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1074 void __user *ukey = u64_to_user_ptr(attr->key);
1075 void __user *uvalue = u64_to_user_ptr(attr->value);
1076 int ufd = attr->map_fd;
1077 struct bpf_map *map;
1083 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1087 map = __bpf_map_get(f);
1089 return PTR_ERR(map);
1091 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
1096 key = __bpf_copy_key(ukey, map->key_size);
1102 value_size = map->value_size;
1105 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1109 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1110 map->map_type == BPF_MAP_TYPE_STACK) {
1111 err = map->ops->map_pop_elem(map, value);
1119 if (copy_to_user(uvalue, value, value_size) != 0)
1133 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1134 #define BPF_PROG_TYPE(_id, _name) \
1135 [_id] = & _name ## _prog_ops,
1136 #define BPF_MAP_TYPE(_id, _ops)
1137 #include <linux/bpf_types.h>
1138 #undef BPF_PROG_TYPE
1142 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1144 const struct bpf_prog_ops *ops;
1146 if (type >= ARRAY_SIZE(bpf_prog_types))
1148 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1149 ops = bpf_prog_types[type];
1153 if (!bpf_prog_is_dev_bound(prog->aux))
1154 prog->aux->ops = ops;
1156 prog->aux->ops = &bpf_offload_prog_ops;
1161 /* drop refcnt on maps used by eBPF program and free auxilary data */
1162 static void free_used_maps(struct bpf_prog_aux *aux)
1164 enum bpf_cgroup_storage_type stype;
1167 for_each_cgroup_storage_type(stype) {
1168 if (!aux->cgroup_storage[stype])
1170 bpf_cgroup_storage_release(aux->prog,
1171 aux->cgroup_storage[stype]);
1174 for (i = 0; i < aux->used_map_cnt; i++)
1175 bpf_map_put(aux->used_maps[i]);
1177 kfree(aux->used_maps);
1180 int __bpf_prog_charge(struct user_struct *user, u32 pages)
1182 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1183 unsigned long user_bufs;
1186 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1187 if (user_bufs > memlock_limit) {
1188 atomic_long_sub(pages, &user->locked_vm);
1196 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1199 atomic_long_sub(pages, &user->locked_vm);
1202 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1204 struct user_struct *user = get_current_user();
1207 ret = __bpf_prog_charge(user, prog->pages);
1213 prog->aux->user = user;
1217 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1219 struct user_struct *user = prog->aux->user;
1221 __bpf_prog_uncharge(user, prog->pages);
1225 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1229 idr_preload(GFP_KERNEL);
1230 spin_lock_bh(&prog_idr_lock);
1231 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1234 spin_unlock_bh(&prog_idr_lock);
1237 /* id is in [1, INT_MAX) */
1238 if (WARN_ON_ONCE(!id))
1241 return id > 0 ? 0 : id;
1244 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1246 /* cBPF to eBPF migrations are currently not in the idr store.
1247 * Offloaded programs are removed from the store when their device
1248 * disappears - even if someone grabs an fd to them they are unusable,
1249 * simply waiting for refcnt to drop to be freed.
1255 spin_lock_bh(&prog_idr_lock);
1257 __acquire(&prog_idr_lock);
1259 idr_remove(&prog_idr, prog->aux->id);
1263 spin_unlock_bh(&prog_idr_lock);
1265 __release(&prog_idr_lock);
1268 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1270 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1272 free_used_maps(aux);
1273 bpf_prog_uncharge_memlock(aux->prog);
1274 security_bpf_prog_free(aux);
1275 bpf_prog_free(aux->prog);
1278 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1280 if (atomic_dec_and_test(&prog->aux->refcnt)) {
1281 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1282 /* bpf_prog_free_id() must be called first */
1283 bpf_prog_free_id(prog, do_idr_lock);
1284 bpf_prog_kallsyms_del_all(prog);
1285 btf_put(prog->aux->btf);
1286 kvfree(prog->aux->func_info);
1287 bpf_prog_free_linfo(prog);
1289 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1293 void bpf_prog_put(struct bpf_prog *prog)
1295 __bpf_prog_put(prog, true);
1297 EXPORT_SYMBOL_GPL(bpf_prog_put);
1299 static int bpf_prog_release(struct inode *inode, struct file *filp)
1301 struct bpf_prog *prog = filp->private_data;
1307 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1308 struct bpf_prog_stats *stats)
1310 u64 nsecs = 0, cnt = 0;
1313 for_each_possible_cpu(cpu) {
1314 const struct bpf_prog_stats *st;
1318 st = per_cpu_ptr(prog->aux->stats, cpu);
1320 start = u64_stats_fetch_begin_irq(&st->syncp);
1323 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1327 stats->nsecs = nsecs;
1331 #ifdef CONFIG_PROC_FS
1332 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1334 const struct bpf_prog *prog = filp->private_data;
1335 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1336 struct bpf_prog_stats stats;
1338 bpf_prog_get_stats(prog, &stats);
1339 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1346 "run_time_ns:\t%llu\n"
1351 prog->pages * 1ULL << PAGE_SHIFT,
1358 const struct file_operations bpf_prog_fops = {
1359 #ifdef CONFIG_PROC_FS
1360 .show_fdinfo = bpf_prog_show_fdinfo,
1362 .release = bpf_prog_release,
1363 .read = bpf_dummy_read,
1364 .write = bpf_dummy_write,
1367 int bpf_prog_new_fd(struct bpf_prog *prog)
1371 ret = security_bpf_prog(prog);
1375 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1376 O_RDWR | O_CLOEXEC);
1379 static struct bpf_prog *____bpf_prog_get(struct fd f)
1382 return ERR_PTR(-EBADF);
1383 if (f.file->f_op != &bpf_prog_fops) {
1385 return ERR_PTR(-EINVAL);
1388 return f.file->private_data;
1391 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
1393 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1394 atomic_sub(i, &prog->aux->refcnt);
1395 return ERR_PTR(-EBUSY);
1399 EXPORT_SYMBOL_GPL(bpf_prog_add);
1401 void bpf_prog_sub(struct bpf_prog *prog, int i)
1403 /* Only to be used for undoing previous bpf_prog_add() in some
1404 * error path. We still know that another entity in our call
1405 * path holds a reference to the program, thus atomic_sub() can
1406 * be safely used in such cases!
1408 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1410 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1412 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1414 return bpf_prog_add(prog, 1);
1416 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1418 /* prog_idr_lock should have been held */
1419 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1423 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1425 if (refold >= BPF_MAX_REFCNT) {
1426 __bpf_prog_put(prog, false);
1427 return ERR_PTR(-EBUSY);
1431 return ERR_PTR(-ENOENT);
1435 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1437 bool bpf_prog_get_ok(struct bpf_prog *prog,
1438 enum bpf_prog_type *attach_type, bool attach_drv)
1440 /* not an attachment, just a refcount inc, always allow */
1444 if (prog->type != *attach_type)
1446 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1452 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1455 struct fd f = fdget(ufd);
1456 struct bpf_prog *prog;
1458 prog = ____bpf_prog_get(f);
1461 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1462 prog = ERR_PTR(-EINVAL);
1466 prog = bpf_prog_inc(prog);
1472 struct bpf_prog *bpf_prog_get(u32 ufd)
1474 return __bpf_prog_get(ufd, NULL, false);
1477 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1480 return __bpf_prog_get(ufd, &type, attach_drv);
1482 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1484 /* Initially all BPF programs could be loaded w/o specifying
1485 * expected_attach_type. Later for some of them specifying expected_attach_type
1486 * at load time became required so that program could be validated properly.
1487 * Programs of types that are allowed to be loaded both w/ and w/o (for
1488 * backward compatibility) expected_attach_type, should have the default attach
1489 * type assigned to expected_attach_type for the latter case, so that it can be
1490 * validated later at attach time.
1492 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1493 * prog type requires it but has some attach types that have to be backward
1496 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1498 switch (attr->prog_type) {
1499 case BPF_PROG_TYPE_CGROUP_SOCK:
1500 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1501 * exist so checking for non-zero is the way to go here.
1503 if (!attr->expected_attach_type)
1504 attr->expected_attach_type =
1505 BPF_CGROUP_INET_SOCK_CREATE;
1511 bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1512 enum bpf_attach_type expected_attach_type)
1514 switch (prog_type) {
1515 case BPF_PROG_TYPE_CGROUP_SOCK:
1516 switch (expected_attach_type) {
1517 case BPF_CGROUP_INET_SOCK_CREATE:
1518 case BPF_CGROUP_INET4_POST_BIND:
1519 case BPF_CGROUP_INET6_POST_BIND:
1524 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1525 switch (expected_attach_type) {
1526 case BPF_CGROUP_INET4_BIND:
1527 case BPF_CGROUP_INET6_BIND:
1528 case BPF_CGROUP_INET4_CONNECT:
1529 case BPF_CGROUP_INET6_CONNECT:
1530 case BPF_CGROUP_UDP4_SENDMSG:
1531 case BPF_CGROUP_UDP6_SENDMSG:
1541 /* last field in 'union bpf_attr' used by this command */
1542 #define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
1544 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
1546 enum bpf_prog_type type = attr->prog_type;
1547 struct bpf_prog *prog;
1552 if (CHECK_ATTR(BPF_PROG_LOAD))
1555 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | BPF_F_ANY_ALIGNMENT))
1558 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1559 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1560 !capable(CAP_SYS_ADMIN))
1563 /* copy eBPF program license from user space */
1564 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1565 sizeof(license) - 1) < 0)
1567 license[sizeof(license) - 1] = 0;
1569 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1570 is_gpl = license_is_gpl_compatible(license);
1572 if (attr->insn_cnt == 0 ||
1573 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
1575 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1576 type != BPF_PROG_TYPE_CGROUP_SKB &&
1577 !capable(CAP_SYS_ADMIN))
1580 bpf_prog_load_fixup_attach_type(attr);
1581 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1584 /* plain bpf_prog allocation */
1585 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1589 prog->expected_attach_type = attr->expected_attach_type;
1591 prog->aux->offload_requested = !!attr->prog_ifindex;
1593 err = security_bpf_prog_alloc(prog->aux);
1595 goto free_prog_nouncharge;
1597 err = bpf_prog_charge_memlock(prog);
1601 prog->len = attr->insn_cnt;
1604 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1605 bpf_prog_insn_size(prog)) != 0)
1608 prog->orig_prog = NULL;
1611 atomic_set(&prog->aux->refcnt, 1);
1612 prog->gpl_compatible = is_gpl ? 1 : 0;
1614 if (bpf_prog_is_dev_bound(prog->aux)) {
1615 err = bpf_prog_offload_init(prog, attr);
1620 /* find program type: socket_filter vs tracing_filter */
1621 err = find_prog_type(type, prog);
1625 prog->aux->load_time = ktime_get_boot_ns();
1626 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1630 /* run eBPF verifier */
1631 err = bpf_check(&prog, attr, uattr);
1633 goto free_used_maps;
1635 prog = bpf_prog_select_runtime(prog, &err);
1637 goto free_used_maps;
1639 err = bpf_prog_alloc_id(prog);
1641 goto free_used_maps;
1643 err = bpf_prog_new_fd(prog);
1645 /* failed to allocate fd.
1646 * bpf_prog_put() is needed because the above
1647 * bpf_prog_alloc_id() has published the prog
1648 * to the userspace and the userspace may
1649 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1655 bpf_prog_kallsyms_add(prog);
1656 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
1660 bpf_prog_free_linfo(prog);
1661 kvfree(prog->aux->func_info);
1662 btf_put(prog->aux->btf);
1663 bpf_prog_kallsyms_del_subprogs(prog);
1664 free_used_maps(prog->aux);
1666 bpf_prog_uncharge_memlock(prog);
1668 security_bpf_prog_free(prog->aux);
1669 free_prog_nouncharge:
1670 bpf_prog_free(prog);
1674 #define BPF_OBJ_LAST_FIELD file_flags
1676 static int bpf_obj_pin(const union bpf_attr *attr)
1678 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1681 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1684 static int bpf_obj_get(const union bpf_attr *attr)
1686 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1687 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1690 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1694 struct bpf_raw_tracepoint {
1695 struct bpf_raw_event_map *btp;
1696 struct bpf_prog *prog;
1699 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1701 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1704 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1705 bpf_prog_put(raw_tp->prog);
1707 bpf_put_raw_tracepoint(raw_tp->btp);
1712 static const struct file_operations bpf_raw_tp_fops = {
1713 .release = bpf_raw_tracepoint_release,
1714 .read = bpf_dummy_read,
1715 .write = bpf_dummy_write,
1718 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1720 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1722 struct bpf_raw_tracepoint *raw_tp;
1723 struct bpf_raw_event_map *btp;
1724 struct bpf_prog *prog;
1728 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1729 sizeof(tp_name) - 1) < 0)
1731 tp_name[sizeof(tp_name) - 1] = 0;
1733 btp = bpf_get_raw_tracepoint(tp_name);
1737 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1744 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1745 BPF_PROG_TYPE_RAW_TRACEPOINT);
1747 err = PTR_ERR(prog);
1751 err = bpf_probe_register(raw_tp->btp, prog);
1755 raw_tp->prog = prog;
1756 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1759 bpf_probe_unregister(raw_tp->btp, prog);
1770 bpf_put_raw_tracepoint(btp);
1774 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1775 enum bpf_attach_type attach_type)
1777 switch (prog->type) {
1778 case BPF_PROG_TYPE_CGROUP_SOCK:
1779 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1780 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1786 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1788 #define BPF_F_ATTACH_MASK \
1789 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1791 static int bpf_prog_attach(const union bpf_attr *attr)
1793 enum bpf_prog_type ptype;
1794 struct bpf_prog *prog;
1797 if (!capable(CAP_NET_ADMIN))
1800 if (CHECK_ATTR(BPF_PROG_ATTACH))
1803 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
1806 switch (attr->attach_type) {
1807 case BPF_CGROUP_INET_INGRESS:
1808 case BPF_CGROUP_INET_EGRESS:
1809 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1811 case BPF_CGROUP_INET_SOCK_CREATE:
1812 case BPF_CGROUP_INET4_POST_BIND:
1813 case BPF_CGROUP_INET6_POST_BIND:
1814 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1816 case BPF_CGROUP_INET4_BIND:
1817 case BPF_CGROUP_INET6_BIND:
1818 case BPF_CGROUP_INET4_CONNECT:
1819 case BPF_CGROUP_INET6_CONNECT:
1820 case BPF_CGROUP_UDP4_SENDMSG:
1821 case BPF_CGROUP_UDP6_SENDMSG:
1822 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1824 case BPF_CGROUP_SOCK_OPS:
1825 ptype = BPF_PROG_TYPE_SOCK_OPS;
1827 case BPF_CGROUP_DEVICE:
1828 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1830 case BPF_SK_MSG_VERDICT:
1831 ptype = BPF_PROG_TYPE_SK_MSG;
1833 case BPF_SK_SKB_STREAM_PARSER:
1834 case BPF_SK_SKB_STREAM_VERDICT:
1835 ptype = BPF_PROG_TYPE_SK_SKB;
1837 case BPF_LIRC_MODE2:
1838 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1840 case BPF_FLOW_DISSECTOR:
1841 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
1847 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1849 return PTR_ERR(prog);
1851 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1857 case BPF_PROG_TYPE_SK_SKB:
1858 case BPF_PROG_TYPE_SK_MSG:
1859 ret = sock_map_get_from_fd(attr, prog);
1861 case BPF_PROG_TYPE_LIRC_MODE2:
1862 ret = lirc_prog_attach(attr, prog);
1864 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1865 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1868 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
1876 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1878 static int bpf_prog_detach(const union bpf_attr *attr)
1880 enum bpf_prog_type ptype;
1882 if (!capable(CAP_NET_ADMIN))
1885 if (CHECK_ATTR(BPF_PROG_DETACH))
1888 switch (attr->attach_type) {
1889 case BPF_CGROUP_INET_INGRESS:
1890 case BPF_CGROUP_INET_EGRESS:
1891 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1893 case BPF_CGROUP_INET_SOCK_CREATE:
1894 case BPF_CGROUP_INET4_POST_BIND:
1895 case BPF_CGROUP_INET6_POST_BIND:
1896 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1898 case BPF_CGROUP_INET4_BIND:
1899 case BPF_CGROUP_INET6_BIND:
1900 case BPF_CGROUP_INET4_CONNECT:
1901 case BPF_CGROUP_INET6_CONNECT:
1902 case BPF_CGROUP_UDP4_SENDMSG:
1903 case BPF_CGROUP_UDP6_SENDMSG:
1904 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1906 case BPF_CGROUP_SOCK_OPS:
1907 ptype = BPF_PROG_TYPE_SOCK_OPS;
1909 case BPF_CGROUP_DEVICE:
1910 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1912 case BPF_SK_MSG_VERDICT:
1913 return sock_map_get_from_fd(attr, NULL);
1914 case BPF_SK_SKB_STREAM_PARSER:
1915 case BPF_SK_SKB_STREAM_VERDICT:
1916 return sock_map_get_from_fd(attr, NULL);
1917 case BPF_LIRC_MODE2:
1918 return lirc_prog_detach(attr);
1919 case BPF_FLOW_DISSECTOR:
1920 return skb_flow_dissector_bpf_prog_detach(attr);
1925 return cgroup_bpf_prog_detach(attr, ptype);
1928 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1930 static int bpf_prog_query(const union bpf_attr *attr,
1931 union bpf_attr __user *uattr)
1933 if (!capable(CAP_NET_ADMIN))
1935 if (CHECK_ATTR(BPF_PROG_QUERY))
1937 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1940 switch (attr->query.attach_type) {
1941 case BPF_CGROUP_INET_INGRESS:
1942 case BPF_CGROUP_INET_EGRESS:
1943 case BPF_CGROUP_INET_SOCK_CREATE:
1944 case BPF_CGROUP_INET4_BIND:
1945 case BPF_CGROUP_INET6_BIND:
1946 case BPF_CGROUP_INET4_POST_BIND:
1947 case BPF_CGROUP_INET6_POST_BIND:
1948 case BPF_CGROUP_INET4_CONNECT:
1949 case BPF_CGROUP_INET6_CONNECT:
1950 case BPF_CGROUP_UDP4_SENDMSG:
1951 case BPF_CGROUP_UDP6_SENDMSG:
1952 case BPF_CGROUP_SOCK_OPS:
1953 case BPF_CGROUP_DEVICE:
1955 case BPF_LIRC_MODE2:
1956 return lirc_prog_query(attr, uattr);
1961 return cgroup_bpf_prog_query(attr, uattr);
1964 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1966 static int bpf_prog_test_run(const union bpf_attr *attr,
1967 union bpf_attr __user *uattr)
1969 struct bpf_prog *prog;
1970 int ret = -ENOTSUPP;
1972 if (!capable(CAP_SYS_ADMIN))
1974 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1977 prog = bpf_prog_get(attr->test.prog_fd);
1979 return PTR_ERR(prog);
1981 if (prog->aux->ops->test_run)
1982 ret = prog->aux->ops->test_run(prog, attr, uattr);
1988 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1990 static int bpf_obj_get_next_id(const union bpf_attr *attr,
1991 union bpf_attr __user *uattr,
1995 u32 next_id = attr->start_id;
1998 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2001 if (!capable(CAP_SYS_ADMIN))
2006 if (!idr_get_next(idr, &next_id))
2008 spin_unlock_bh(lock);
2011 err = put_user(next_id, &uattr->next_id);
2016 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2018 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2020 struct bpf_prog *prog;
2021 u32 id = attr->prog_id;
2024 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2027 if (!capable(CAP_SYS_ADMIN))
2030 spin_lock_bh(&prog_idr_lock);
2031 prog = idr_find(&prog_idr, id);
2033 prog = bpf_prog_inc_not_zero(prog);
2035 prog = ERR_PTR(-ENOENT);
2036 spin_unlock_bh(&prog_idr_lock);
2039 return PTR_ERR(prog);
2041 fd = bpf_prog_new_fd(prog);
2048 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2050 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2052 struct bpf_map *map;
2053 u32 id = attr->map_id;
2057 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2058 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
2061 if (!capable(CAP_SYS_ADMIN))
2064 f_flags = bpf_get_file_flag(attr->open_flags);
2068 spin_lock_bh(&map_idr_lock);
2069 map = idr_find(&map_idr, id);
2071 map = bpf_map_inc_not_zero(map, true);
2073 map = ERR_PTR(-ENOENT);
2074 spin_unlock_bh(&map_idr_lock);
2077 return PTR_ERR(map);
2079 fd = bpf_map_new_fd(map, f_flags);
2081 bpf_map_put_with_uref(map);
2086 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
2087 unsigned long addr, u32 *off,
2090 const struct bpf_map *map;
2093 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2094 map = prog->aux->used_maps[i];
2095 if (map == (void *)addr) {
2096 *type = BPF_PSEUDO_MAP_FD;
2099 if (!map->ops->map_direct_value_meta)
2101 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2102 *type = BPF_PSEUDO_MAP_VALUE;
2110 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2112 const struct bpf_map *map;
2113 struct bpf_insn *insns;
2118 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2123 for (i = 0; i < prog->len; i++) {
2124 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2125 insns[i].code = BPF_JMP | BPF_CALL;
2126 insns[i].imm = BPF_FUNC_tail_call;
2129 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2130 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2131 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2132 insns[i].code = BPF_JMP | BPF_CALL;
2133 if (!bpf_dump_raw_ok())
2138 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2141 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
2142 map = bpf_map_from_imm(prog, imm, &off, &type);
2144 insns[i].src_reg = type;
2145 insns[i].imm = map->id;
2146 insns[i + 1].imm = off;
2154 static int set_info_rec_size(struct bpf_prog_info *info)
2157 * Ensure info.*_rec_size is the same as kernel expected size
2161 * Only allow zero *_rec_size if both _rec_size and _cnt are
2162 * zero. In this case, the kernel will set the expected
2163 * _rec_size back to the info.
2166 if ((info->nr_func_info || info->func_info_rec_size) &&
2167 info->func_info_rec_size != sizeof(struct bpf_func_info))
2170 if ((info->nr_line_info || info->line_info_rec_size) &&
2171 info->line_info_rec_size != sizeof(struct bpf_line_info))
2174 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
2175 info->jited_line_info_rec_size != sizeof(__u64))
2178 info->func_info_rec_size = sizeof(struct bpf_func_info);
2179 info->line_info_rec_size = sizeof(struct bpf_line_info);
2180 info->jited_line_info_rec_size = sizeof(__u64);
2185 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2186 const union bpf_attr *attr,
2187 union bpf_attr __user *uattr)
2189 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2190 struct bpf_prog_info info = {};
2191 u32 info_len = attr->info.info_len;
2192 struct bpf_prog_stats stats;
2193 char __user *uinsns;
2197 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2200 info_len = min_t(u32, sizeof(info), info_len);
2202 if (copy_from_user(&info, uinfo, info_len))
2205 info.type = prog->type;
2206 info.id = prog->aux->id;
2207 info.load_time = prog->aux->load_time;
2208 info.created_by_uid = from_kuid_munged(current_user_ns(),
2209 prog->aux->user->uid);
2210 info.gpl_compatible = prog->gpl_compatible;
2212 memcpy(info.tag, prog->tag, sizeof(prog->tag));
2213 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2215 ulen = info.nr_map_ids;
2216 info.nr_map_ids = prog->aux->used_map_cnt;
2217 ulen = min_t(u32, info.nr_map_ids, ulen);
2219 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
2222 for (i = 0; i < ulen; i++)
2223 if (put_user(prog->aux->used_maps[i]->id,
2228 err = set_info_rec_size(&info);
2232 bpf_prog_get_stats(prog, &stats);
2233 info.run_time_ns = stats.nsecs;
2234 info.run_cnt = stats.cnt;
2236 if (!capable(CAP_SYS_ADMIN)) {
2237 info.jited_prog_len = 0;
2238 info.xlated_prog_len = 0;
2239 info.nr_jited_ksyms = 0;
2240 info.nr_jited_func_lens = 0;
2241 info.nr_func_info = 0;
2242 info.nr_line_info = 0;
2243 info.nr_jited_line_info = 0;
2247 ulen = info.xlated_prog_len;
2248 info.xlated_prog_len = bpf_prog_insn_size(prog);
2249 if (info.xlated_prog_len && ulen) {
2250 struct bpf_insn *insns_sanitized;
2253 if (prog->blinded && !bpf_dump_raw_ok()) {
2254 info.xlated_prog_insns = 0;
2257 insns_sanitized = bpf_insn_prepare_dump(prog);
2258 if (!insns_sanitized)
2260 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2261 ulen = min_t(u32, info.xlated_prog_len, ulen);
2262 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2263 kfree(insns_sanitized);
2268 if (bpf_prog_is_dev_bound(prog->aux)) {
2269 err = bpf_prog_offload_info_fill(&info, prog);
2275 /* NOTE: the following code is supposed to be skipped for offload.
2276 * bpf_prog_offload_info_fill() is the place to fill similar fields
2279 ulen = info.jited_prog_len;
2280 if (prog->aux->func_cnt) {
2283 info.jited_prog_len = 0;
2284 for (i = 0; i < prog->aux->func_cnt; i++)
2285 info.jited_prog_len += prog->aux->func[i]->jited_len;
2287 info.jited_prog_len = prog->jited_len;
2290 if (info.jited_prog_len && ulen) {
2291 if (bpf_dump_raw_ok()) {
2292 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2293 ulen = min_t(u32, info.jited_prog_len, ulen);
2295 /* for multi-function programs, copy the JITed
2296 * instructions for all the functions
2298 if (prog->aux->func_cnt) {
2303 for (i = 0; i < prog->aux->func_cnt; i++) {
2304 len = prog->aux->func[i]->jited_len;
2305 len = min_t(u32, len, free);
2306 img = (u8 *) prog->aux->func[i]->bpf_func;
2307 if (copy_to_user(uinsns, img, len))
2315 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2319 info.jited_prog_insns = 0;
2323 ulen = info.nr_jited_ksyms;
2324 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
2326 if (bpf_dump_raw_ok()) {
2327 unsigned long ksym_addr;
2328 u64 __user *user_ksyms;
2331 /* copy the address of the kernel symbol
2332 * corresponding to each function
2334 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2335 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
2336 if (prog->aux->func_cnt) {
2337 for (i = 0; i < ulen; i++) {
2338 ksym_addr = (unsigned long)
2339 prog->aux->func[i]->bpf_func;
2340 if (put_user((u64) ksym_addr,
2345 ksym_addr = (unsigned long) prog->bpf_func;
2346 if (put_user((u64) ksym_addr, &user_ksyms[0]))
2350 info.jited_ksyms = 0;
2354 ulen = info.nr_jited_func_lens;
2355 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
2357 if (bpf_dump_raw_ok()) {
2358 u32 __user *user_lens;
2361 /* copy the JITed image lengths for each function */
2362 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2363 user_lens = u64_to_user_ptr(info.jited_func_lens);
2364 if (prog->aux->func_cnt) {
2365 for (i = 0; i < ulen; i++) {
2367 prog->aux->func[i]->jited_len;
2368 if (put_user(func_len, &user_lens[i]))
2372 func_len = prog->jited_len;
2373 if (put_user(func_len, &user_lens[0]))
2377 info.jited_func_lens = 0;
2382 info.btf_id = btf_id(prog->aux->btf);
2384 ulen = info.nr_func_info;
2385 info.nr_func_info = prog->aux->func_info_cnt;
2386 if (info.nr_func_info && ulen) {
2387 char __user *user_finfo;
2389 user_finfo = u64_to_user_ptr(info.func_info);
2390 ulen = min_t(u32, info.nr_func_info, ulen);
2391 if (copy_to_user(user_finfo, prog->aux->func_info,
2392 info.func_info_rec_size * ulen))
2396 ulen = info.nr_line_info;
2397 info.nr_line_info = prog->aux->nr_linfo;
2398 if (info.nr_line_info && ulen) {
2399 __u8 __user *user_linfo;
2401 user_linfo = u64_to_user_ptr(info.line_info);
2402 ulen = min_t(u32, info.nr_line_info, ulen);
2403 if (copy_to_user(user_linfo, prog->aux->linfo,
2404 info.line_info_rec_size * ulen))
2408 ulen = info.nr_jited_line_info;
2409 if (prog->aux->jited_linfo)
2410 info.nr_jited_line_info = prog->aux->nr_linfo;
2412 info.nr_jited_line_info = 0;
2413 if (info.nr_jited_line_info && ulen) {
2414 if (bpf_dump_raw_ok()) {
2415 __u64 __user *user_linfo;
2418 user_linfo = u64_to_user_ptr(info.jited_line_info);
2419 ulen = min_t(u32, info.nr_jited_line_info, ulen);
2420 for (i = 0; i < ulen; i++) {
2421 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2426 info.jited_line_info = 0;
2430 ulen = info.nr_prog_tags;
2431 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2433 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2436 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2437 ulen = min_t(u32, info.nr_prog_tags, ulen);
2438 if (prog->aux->func_cnt) {
2439 for (i = 0; i < ulen; i++) {
2440 if (copy_to_user(user_prog_tags[i],
2441 prog->aux->func[i]->tag,
2446 if (copy_to_user(user_prog_tags[0],
2447 prog->tag, BPF_TAG_SIZE))
2453 if (copy_to_user(uinfo, &info, info_len) ||
2454 put_user(info_len, &uattr->info.info_len))
2460 static int bpf_map_get_info_by_fd(struct bpf_map *map,
2461 const union bpf_attr *attr,
2462 union bpf_attr __user *uattr)
2464 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2465 struct bpf_map_info info = {};
2466 u32 info_len = attr->info.info_len;
2469 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2472 info_len = min_t(u32, sizeof(info), info_len);
2474 info.type = map->map_type;
2476 info.key_size = map->key_size;
2477 info.value_size = map->value_size;
2478 info.max_entries = map->max_entries;
2479 info.map_flags = map->map_flags;
2480 memcpy(info.name, map->name, sizeof(map->name));
2483 info.btf_id = btf_id(map->btf);
2484 info.btf_key_type_id = map->btf_key_type_id;
2485 info.btf_value_type_id = map->btf_value_type_id;
2488 if (bpf_map_is_dev_bound(map)) {
2489 err = bpf_map_offload_info_fill(&info, map);
2494 if (copy_to_user(uinfo, &info, info_len) ||
2495 put_user(info_len, &uattr->info.info_len))
2501 static int bpf_btf_get_info_by_fd(struct btf *btf,
2502 const union bpf_attr *attr,
2503 union bpf_attr __user *uattr)
2505 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2506 u32 info_len = attr->info.info_len;
2509 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
2513 return btf_get_info_by_fd(btf, attr, uattr);
2516 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2518 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2519 union bpf_attr __user *uattr)
2521 int ufd = attr->info.bpf_fd;
2525 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2532 if (f.file->f_op == &bpf_prog_fops)
2533 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2535 else if (f.file->f_op == &bpf_map_fops)
2536 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2538 else if (f.file->f_op == &btf_fops)
2539 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
2547 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2549 static int bpf_btf_load(const union bpf_attr *attr)
2551 if (CHECK_ATTR(BPF_BTF_LOAD))
2554 if (!capable(CAP_SYS_ADMIN))
2557 return btf_new_fd(attr);
2560 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2562 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2564 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2567 if (!capable(CAP_SYS_ADMIN))
2570 return btf_get_fd_by_id(attr->btf_id);
2573 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2574 union bpf_attr __user *uattr,
2575 u32 prog_id, u32 fd_type,
2576 const char *buf, u64 probe_offset,
2579 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2580 u32 len = buf ? strlen(buf) : 0, input_len;
2583 if (put_user(len, &uattr->task_fd_query.buf_len))
2585 input_len = attr->task_fd_query.buf_len;
2586 if (input_len && ubuf) {
2588 /* nothing to copy, just make ubuf NULL terminated */
2591 if (put_user(zero, ubuf))
2593 } else if (input_len >= len + 1) {
2594 /* ubuf can hold the string with NULL terminator */
2595 if (copy_to_user(ubuf, buf, len + 1))
2598 /* ubuf cannot hold the string with NULL terminator,
2599 * do a partial copy with NULL terminator.
2604 if (copy_to_user(ubuf, buf, input_len - 1))
2606 if (put_user(zero, ubuf + input_len - 1))
2611 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2612 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2613 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2614 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2620 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2622 static int bpf_task_fd_query(const union bpf_attr *attr,
2623 union bpf_attr __user *uattr)
2625 pid_t pid = attr->task_fd_query.pid;
2626 u32 fd = attr->task_fd_query.fd;
2627 const struct perf_event *event;
2628 struct files_struct *files;
2629 struct task_struct *task;
2633 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2636 if (!capable(CAP_SYS_ADMIN))
2639 if (attr->task_fd_query.flags != 0)
2642 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2646 files = get_files_struct(task);
2647 put_task_struct(task);
2652 spin_lock(&files->file_lock);
2653 file = fcheck_files(files, fd);
2658 spin_unlock(&files->file_lock);
2659 put_files_struct(files);
2664 if (file->f_op == &bpf_raw_tp_fops) {
2665 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2666 struct bpf_raw_event_map *btp = raw_tp->btp;
2668 err = bpf_task_fd_query_copy(attr, uattr,
2669 raw_tp->prog->aux->id,
2670 BPF_FD_TYPE_RAW_TRACEPOINT,
2671 btp->tp->name, 0, 0);
2675 event = perf_get_event(file);
2676 if (!IS_ERR(event)) {
2677 u64 probe_offset, probe_addr;
2678 u32 prog_id, fd_type;
2681 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2682 &buf, &probe_offset,
2685 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2699 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2701 union bpf_attr attr = {};
2704 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
2707 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
2710 size = min_t(u32, size, sizeof(attr));
2712 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2713 if (copy_from_user(&attr, uattr, size) != 0)
2716 err = security_bpf(cmd, &attr, size);
2721 case BPF_MAP_CREATE:
2722 err = map_create(&attr);
2724 case BPF_MAP_LOOKUP_ELEM:
2725 err = map_lookup_elem(&attr);
2727 case BPF_MAP_UPDATE_ELEM:
2728 err = map_update_elem(&attr);
2730 case BPF_MAP_DELETE_ELEM:
2731 err = map_delete_elem(&attr);
2733 case BPF_MAP_GET_NEXT_KEY:
2734 err = map_get_next_key(&attr);
2737 err = bpf_prog_load(&attr, uattr);
2740 err = bpf_obj_pin(&attr);
2743 err = bpf_obj_get(&attr);
2745 case BPF_PROG_ATTACH:
2746 err = bpf_prog_attach(&attr);
2748 case BPF_PROG_DETACH:
2749 err = bpf_prog_detach(&attr);
2751 case BPF_PROG_QUERY:
2752 err = bpf_prog_query(&attr, uattr);
2754 case BPF_PROG_TEST_RUN:
2755 err = bpf_prog_test_run(&attr, uattr);
2757 case BPF_PROG_GET_NEXT_ID:
2758 err = bpf_obj_get_next_id(&attr, uattr,
2759 &prog_idr, &prog_idr_lock);
2761 case BPF_MAP_GET_NEXT_ID:
2762 err = bpf_obj_get_next_id(&attr, uattr,
2763 &map_idr, &map_idr_lock);
2765 case BPF_PROG_GET_FD_BY_ID:
2766 err = bpf_prog_get_fd_by_id(&attr);
2768 case BPF_MAP_GET_FD_BY_ID:
2769 err = bpf_map_get_fd_by_id(&attr);
2771 case BPF_OBJ_GET_INFO_BY_FD:
2772 err = bpf_obj_get_info_by_fd(&attr, uattr);
2774 case BPF_RAW_TRACEPOINT_OPEN:
2775 err = bpf_raw_tracepoint_open(&attr);
2778 err = bpf_btf_load(&attr);
2780 case BPF_BTF_GET_FD_BY_ID:
2781 err = bpf_btf_get_fd_by_id(&attr);
2783 case BPF_TASK_FD_QUERY:
2784 err = bpf_task_fd_query(&attr, uattr);
2786 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2787 err = map_lookup_and_delete_elem(&attr);