1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
5 #include <linux/bpf_trace.h>
6 #include <linux/bpf_lirc.h>
7 #include <linux/bpf_verifier.h>
9 #include <linux/syscalls.h>
10 #include <linux/slab.h>
11 #include <linux/sched/signal.h>
12 #include <linux/vmalloc.h>
13 #include <linux/mmzone.h>
14 #include <linux/anon_inodes.h>
15 #include <linux/fdtable.h>
16 #include <linux/file.h>
18 #include <linux/license.h>
19 #include <linux/filter.h>
20 #include <linux/version.h>
21 #include <linux/kernel.h>
22 #include <linux/idr.h>
23 #include <linux/cred.h>
24 #include <linux/timekeeping.h>
25 #include <linux/ctype.h>
26 #include <linux/nospec.h>
27 #include <linux/audit.h>
28 #include <uapi/linux/btf.h>
29 #include <linux/pgtable.h>
30 #include <linux/bpf_lsm.h>
31 #include <linux/poll.h>
32 #include <linux/bpf-netns.h>
33 #include <linux/rcupdate_trace.h>
35 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
36 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
38 #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
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_PROG_ARRAY(map) || \
43 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
45 DEFINE_PER_CPU(int, bpf_prog_active);
46 static DEFINE_IDR(prog_idr);
47 static DEFINE_SPINLOCK(prog_idr_lock);
48 static DEFINE_IDR(map_idr);
49 static DEFINE_SPINLOCK(map_idr_lock);
50 static DEFINE_IDR(link_idr);
51 static DEFINE_SPINLOCK(link_idr_lock);
53 int sysctl_unprivileged_bpf_disabled __read_mostly;
55 static const struct bpf_map_ops * const bpf_map_types[] = {
56 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
57 #define BPF_MAP_TYPE(_id, _ops) \
59 #define BPF_LINK_TYPE(_id, _name)
60 #include <linux/bpf_types.h>
67 * If we're handed a bigger struct than we know of, ensure all the unknown bits
68 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
69 * we don't know about yet.
71 * There is a ToCToU between this function call and the following
72 * copy_from_user() call. However, this is not a concern since this function is
73 * meant to be a future-proofing of bits.
75 int bpf_check_uarg_tail_zero(void __user *uaddr,
79 unsigned char __user *addr = uaddr + expected_size;
82 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
85 if (actual_size <= expected_size)
88 res = check_zeroed_user(addr, actual_size - expected_size);
91 return res ? 0 : -E2BIG;
94 const struct bpf_map_ops bpf_map_offload_ops = {
95 .map_meta_equal = bpf_map_meta_equal,
96 .map_alloc = bpf_map_offload_map_alloc,
97 .map_free = bpf_map_offload_map_free,
98 .map_check_btf = map_check_no_btf,
101 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
103 const struct bpf_map_ops *ops;
104 u32 type = attr->map_type;
108 if (type >= ARRAY_SIZE(bpf_map_types))
109 return ERR_PTR(-EINVAL);
110 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
111 ops = bpf_map_types[type];
113 return ERR_PTR(-EINVAL);
115 if (ops->map_alloc_check) {
116 err = ops->map_alloc_check(attr);
120 if (attr->map_ifindex)
121 ops = &bpf_map_offload_ops;
122 map = ops->map_alloc(attr);
126 map->map_type = type;
130 static u32 bpf_map_value_size(struct bpf_map *map)
132 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
133 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
134 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
135 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
136 return round_up(map->value_size, 8) * num_possible_cpus();
137 else if (IS_FD_MAP(map))
140 return map->value_size;
143 static void maybe_wait_bpf_programs(struct bpf_map *map)
145 /* Wait for any running BPF programs to complete so that
146 * userspace, when we return to it, knows that all programs
147 * that could be running use the new map value.
149 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
150 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
154 static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
155 void *value, __u64 flags)
159 /* Need to create a kthread, thus must support schedule */
160 if (bpf_map_is_dev_bound(map)) {
161 return bpf_map_offload_update_elem(map, key, value, flags);
162 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
163 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
164 return map->ops->map_update_elem(map, key, value, flags);
165 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
166 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
167 return sock_map_update_elem_sys(map, key, value, flags);
168 } else if (IS_FD_PROG_ARRAY(map)) {
169 return bpf_fd_array_map_update_elem(map, f.file, key, value,
173 bpf_disable_instrumentation();
174 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
175 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
176 err = bpf_percpu_hash_update(map, key, value, flags);
177 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
178 err = bpf_percpu_array_update(map, key, value, flags);
179 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
180 err = bpf_percpu_cgroup_storage_update(map, key, value,
182 } else if (IS_FD_ARRAY(map)) {
184 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
187 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
189 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
192 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
193 /* rcu_read_lock() is not needed */
194 err = bpf_fd_reuseport_array_update_elem(map, key, value,
196 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
197 map->map_type == BPF_MAP_TYPE_STACK) {
198 err = map->ops->map_push_elem(map, value, flags);
201 err = map->ops->map_update_elem(map, key, value, flags);
204 bpf_enable_instrumentation();
205 maybe_wait_bpf_programs(map);
210 static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
216 if (bpf_map_is_dev_bound(map))
217 return bpf_map_offload_lookup_elem(map, key, value);
219 bpf_disable_instrumentation();
220 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
221 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
222 err = bpf_percpu_hash_copy(map, key, value);
223 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
224 err = bpf_percpu_array_copy(map, key, value);
225 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
226 err = bpf_percpu_cgroup_storage_copy(map, key, value);
227 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
228 err = bpf_stackmap_copy(map, key, value);
229 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
230 err = bpf_fd_array_map_lookup_elem(map, key, value);
231 } else if (IS_FD_HASH(map)) {
232 err = bpf_fd_htab_map_lookup_elem(map, key, value);
233 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
234 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
235 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
236 map->map_type == BPF_MAP_TYPE_STACK) {
237 err = map->ops->map_peek_elem(map, value);
238 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
239 /* struct_ops map requires directly updating "value" */
240 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
243 if (map->ops->map_lookup_elem_sys_only)
244 ptr = map->ops->map_lookup_elem_sys_only(map, key);
246 ptr = map->ops->map_lookup_elem(map, key);
253 if (flags & BPF_F_LOCK)
254 /* lock 'ptr' and copy everything but lock */
255 copy_map_value_locked(map, value, ptr, true);
257 copy_map_value(map, value, ptr);
258 /* mask lock, since value wasn't zero inited */
259 check_and_init_map_lock(map, value);
264 bpf_enable_instrumentation();
265 maybe_wait_bpf_programs(map);
270 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
272 /* We really just want to fail instead of triggering OOM killer
273 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
274 * which is used for lower order allocation requests.
276 * It has been observed that higher order allocation requests done by
277 * vmalloc with __GFP_NORETRY being set might fail due to not trying
278 * to reclaim memory from the page cache, thus we set
279 * __GFP_RETRY_MAYFAIL to avoid such situations.
282 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO;
283 unsigned int flags = 0;
284 unsigned long align = 1;
287 if (size >= SIZE_MAX)
290 /* kmalloc()'ed memory can't be mmap()'ed */
292 BUG_ON(!PAGE_ALIGNED(size));
295 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
296 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
302 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
303 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
304 flags, numa_node, __builtin_return_address(0));
307 void *bpf_map_area_alloc(u64 size, int numa_node)
309 return __bpf_map_area_alloc(size, numa_node, false);
312 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
314 return __bpf_map_area_alloc(size, numa_node, true);
317 void bpf_map_area_free(void *area)
322 static u32 bpf_map_flags_retain_permanent(u32 flags)
324 /* Some map creation flags are not tied to the map object but
325 * rather to the map fd instead, so they have no meaning upon
326 * map object inspection since multiple file descriptors with
327 * different (access) properties can exist here. Thus, given
328 * this has zero meaning for the map itself, lets clear these
331 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
334 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
336 map->map_type = attr->map_type;
337 map->key_size = attr->key_size;
338 map->value_size = attr->value_size;
339 map->max_entries = attr->max_entries;
340 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
341 map->numa_node = bpf_map_attr_numa_node(attr);
344 static int bpf_charge_memlock(struct user_struct *user, u32 pages)
346 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
348 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
349 atomic_long_sub(pages, &user->locked_vm);
355 static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
358 atomic_long_sub(pages, &user->locked_vm);
361 int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
363 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
364 struct user_struct *user;
367 if (size >= U32_MAX - PAGE_SIZE)
370 user = get_current_user();
371 ret = bpf_charge_memlock(user, pages);
383 void bpf_map_charge_finish(struct bpf_map_memory *mem)
385 bpf_uncharge_memlock(mem->user, mem->pages);
389 void bpf_map_charge_move(struct bpf_map_memory *dst,
390 struct bpf_map_memory *src)
394 /* Make sure src will not be used for the redundant uncharging. */
395 memset(src, 0, sizeof(struct bpf_map_memory));
398 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
402 ret = bpf_charge_memlock(map->memory.user, pages);
405 map->memory.pages += pages;
409 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
411 bpf_uncharge_memlock(map->memory.user, pages);
412 map->memory.pages -= pages;
415 static int bpf_map_alloc_id(struct bpf_map *map)
419 idr_preload(GFP_KERNEL);
420 spin_lock_bh(&map_idr_lock);
421 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
424 spin_unlock_bh(&map_idr_lock);
427 if (WARN_ON_ONCE(!id))
430 return id > 0 ? 0 : id;
433 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
437 /* Offloaded maps are removed from the IDR store when their device
438 * disappears - even if someone holds an fd to them they are unusable,
439 * the memory is gone, all ops will fail; they are simply waiting for
440 * refcnt to drop to be freed.
446 spin_lock_irqsave(&map_idr_lock, flags);
448 __acquire(&map_idr_lock);
450 idr_remove(&map_idr, map->id);
454 spin_unlock_irqrestore(&map_idr_lock, flags);
456 __release(&map_idr_lock);
459 /* called from workqueue */
460 static void bpf_map_free_deferred(struct work_struct *work)
462 struct bpf_map *map = container_of(work, struct bpf_map, work);
463 struct bpf_map_memory mem;
465 bpf_map_charge_move(&mem, &map->memory);
466 security_bpf_map_free(map);
467 /* implementation dependent freeing */
468 map->ops->map_free(map);
469 bpf_map_charge_finish(&mem);
472 static void bpf_map_put_uref(struct bpf_map *map)
474 if (atomic64_dec_and_test(&map->usercnt)) {
475 if (map->ops->map_release_uref)
476 map->ops->map_release_uref(map);
480 /* decrement map refcnt and schedule it for freeing via workqueue
481 * (unrelying map implementation ops->map_free() might sleep)
483 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
485 if (atomic64_dec_and_test(&map->refcnt)) {
486 /* bpf_map_free_id() must be called first */
487 bpf_map_free_id(map, do_idr_lock);
489 INIT_WORK(&map->work, bpf_map_free_deferred);
490 schedule_work(&map->work);
494 void bpf_map_put(struct bpf_map *map)
496 __bpf_map_put(map, true);
498 EXPORT_SYMBOL_GPL(bpf_map_put);
500 void bpf_map_put_with_uref(struct bpf_map *map)
502 bpf_map_put_uref(map);
506 static int bpf_map_release(struct inode *inode, struct file *filp)
508 struct bpf_map *map = filp->private_data;
510 if (map->ops->map_release)
511 map->ops->map_release(map, filp);
513 bpf_map_put_with_uref(map);
517 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
519 fmode_t mode = f.file->f_mode;
521 /* Our file permissions may have been overridden by global
522 * map permissions facing syscall side.
524 if (READ_ONCE(map->frozen))
525 mode &= ~FMODE_CAN_WRITE;
529 #ifdef CONFIG_PROC_FS
530 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
532 const struct bpf_map *map = filp->private_data;
533 const struct bpf_array *array;
534 u32 type = 0, jited = 0;
536 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
537 array = container_of(map, struct bpf_array, map);
538 type = array->aux->type;
539 jited = array->aux->jited;
556 map->memory.pages * 1ULL << PAGE_SHIFT,
558 READ_ONCE(map->frozen));
560 seq_printf(m, "owner_prog_type:\t%u\n", type);
561 seq_printf(m, "owner_jited:\t%u\n", jited);
566 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
569 /* We need this handler such that alloc_file() enables
570 * f_mode with FMODE_CAN_READ.
575 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
576 size_t siz, loff_t *ppos)
578 /* We need this handler such that alloc_file() enables
579 * f_mode with FMODE_CAN_WRITE.
584 /* called for any extra memory-mapped regions (except initial) */
585 static void bpf_map_mmap_open(struct vm_area_struct *vma)
587 struct bpf_map *map = vma->vm_file->private_data;
589 if (vma->vm_flags & VM_MAYWRITE) {
590 mutex_lock(&map->freeze_mutex);
592 mutex_unlock(&map->freeze_mutex);
596 /* called for all unmapped memory region (including initial) */
597 static void bpf_map_mmap_close(struct vm_area_struct *vma)
599 struct bpf_map *map = vma->vm_file->private_data;
601 if (vma->vm_flags & VM_MAYWRITE) {
602 mutex_lock(&map->freeze_mutex);
604 mutex_unlock(&map->freeze_mutex);
608 static const struct vm_operations_struct bpf_map_default_vmops = {
609 .open = bpf_map_mmap_open,
610 .close = bpf_map_mmap_close,
613 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
615 struct bpf_map *map = filp->private_data;
618 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
621 if (!(vma->vm_flags & VM_SHARED))
624 mutex_lock(&map->freeze_mutex);
626 if (vma->vm_flags & VM_WRITE) {
631 /* map is meant to be read-only, so do not allow mapping as
632 * writable, because it's possible to leak a writable page
633 * reference and allows user-space to still modify it after
634 * freezing, while verifier will assume contents do not change
636 if (map->map_flags & BPF_F_RDONLY_PROG) {
642 /* set default open/close callbacks */
643 vma->vm_ops = &bpf_map_default_vmops;
644 vma->vm_private_data = map;
645 vma->vm_flags &= ~VM_MAYEXEC;
646 if (!(vma->vm_flags & VM_WRITE))
647 /* disallow re-mapping with PROT_WRITE */
648 vma->vm_flags &= ~VM_MAYWRITE;
650 err = map->ops->map_mmap(map, vma);
654 if (vma->vm_flags & VM_MAYWRITE)
657 mutex_unlock(&map->freeze_mutex);
661 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
663 struct bpf_map *map = filp->private_data;
665 if (map->ops->map_poll)
666 return map->ops->map_poll(map, filp, pts);
671 const struct file_operations bpf_map_fops = {
672 #ifdef CONFIG_PROC_FS
673 .show_fdinfo = bpf_map_show_fdinfo,
675 .release = bpf_map_release,
676 .read = bpf_dummy_read,
677 .write = bpf_dummy_write,
678 .mmap = bpf_map_mmap,
679 .poll = bpf_map_poll,
682 int bpf_map_new_fd(struct bpf_map *map, int flags)
686 ret = security_bpf_map(map, OPEN_FMODE(flags));
690 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
694 int bpf_get_file_flag(int flags)
696 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
698 if (flags & BPF_F_RDONLY)
700 if (flags & BPF_F_WRONLY)
705 /* helper macro to check that unused fields 'union bpf_attr' are zero */
706 #define CHECK_ATTR(CMD) \
707 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
708 sizeof(attr->CMD##_LAST_FIELD), 0, \
710 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
711 sizeof(attr->CMD##_LAST_FIELD)) != NULL
713 /* dst and src must have at least "size" number of bytes.
714 * Return strlen on success and < 0 on error.
716 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
718 const char *end = src + size;
719 const char *orig_src = src;
721 memset(dst, 0, size);
722 /* Copy all isalnum(), '_' and '.' chars. */
723 while (src < end && *src) {
724 if (!isalnum(*src) &&
725 *src != '_' && *src != '.')
730 /* No '\0' found in "size" number of bytes */
734 return src - orig_src;
737 int map_check_no_btf(const struct bpf_map *map,
738 const struct btf *btf,
739 const struct btf_type *key_type,
740 const struct btf_type *value_type)
745 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
746 u32 btf_key_id, u32 btf_value_id)
748 const struct btf_type *key_type, *value_type;
749 u32 key_size, value_size;
752 /* Some maps allow key to be unspecified. */
754 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
755 if (!key_type || key_size != map->key_size)
758 key_type = btf_type_by_id(btf, 0);
759 if (!map->ops->map_check_btf)
763 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
764 if (!value_type || value_size != map->value_size)
767 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
769 if (map_value_has_spin_lock(map)) {
770 if (map->map_flags & BPF_F_RDONLY_PROG)
772 if (map->map_type != BPF_MAP_TYPE_HASH &&
773 map->map_type != BPF_MAP_TYPE_ARRAY &&
774 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
775 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
776 map->map_type != BPF_MAP_TYPE_INODE_STORAGE)
778 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
781 "verifier bug spin_lock_off %d value_size %d\n",
782 map->spin_lock_off, map->value_size);
787 if (map->ops->map_check_btf)
788 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
793 #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
794 /* called via syscall */
795 static int map_create(union bpf_attr *attr)
797 int numa_node = bpf_map_attr_numa_node(attr);
798 struct bpf_map_memory mem;
803 err = CHECK_ATTR(BPF_MAP_CREATE);
807 if (attr->btf_vmlinux_value_type_id) {
808 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
809 attr->btf_key_type_id || attr->btf_value_type_id)
811 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
815 f_flags = bpf_get_file_flag(attr->map_flags);
819 if (numa_node != NUMA_NO_NODE &&
820 ((unsigned int)numa_node >= nr_node_ids ||
821 !node_online(numa_node)))
824 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
825 map = find_and_alloc_map(attr);
829 err = bpf_obj_name_cpy(map->name, attr->map_name,
830 sizeof(attr->map_name));
834 atomic64_set(&map->refcnt, 1);
835 atomic64_set(&map->usercnt, 1);
836 mutex_init(&map->freeze_mutex);
838 map->spin_lock_off = -EINVAL;
839 if (attr->btf_key_type_id || attr->btf_value_type_id ||
840 /* Even the map's value is a kernel's struct,
841 * the bpf_prog.o must have BTF to begin with
842 * to figure out the corresponding kernel's
843 * counter part. Thus, attr->btf_fd has
846 attr->btf_vmlinux_value_type_id) {
849 btf = btf_get_by_fd(attr->btf_fd);
856 if (attr->btf_value_type_id) {
857 err = map_check_btf(map, btf, attr->btf_key_type_id,
858 attr->btf_value_type_id);
863 map->btf_key_type_id = attr->btf_key_type_id;
864 map->btf_value_type_id = attr->btf_value_type_id;
865 map->btf_vmlinux_value_type_id =
866 attr->btf_vmlinux_value_type_id;
869 err = security_bpf_map_alloc(map);
873 err = bpf_map_alloc_id(map);
877 err = bpf_map_new_fd(map, f_flags);
879 /* failed to allocate fd.
880 * bpf_map_put_with_uref() is needed because the above
881 * bpf_map_alloc_id() has published the map
882 * to the userspace and the userspace may
883 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
885 bpf_map_put_with_uref(map);
892 security_bpf_map_free(map);
895 bpf_map_charge_move(&mem, &map->memory);
896 map->ops->map_free(map);
897 bpf_map_charge_finish(&mem);
901 /* if error is returned, fd is released.
902 * On success caller should complete fd access with matching fdput()
904 struct bpf_map *__bpf_map_get(struct fd f)
907 return ERR_PTR(-EBADF);
908 if (f.file->f_op != &bpf_map_fops) {
910 return ERR_PTR(-EINVAL);
913 return f.file->private_data;
916 void bpf_map_inc(struct bpf_map *map)
918 atomic64_inc(&map->refcnt);
920 EXPORT_SYMBOL_GPL(bpf_map_inc);
922 void bpf_map_inc_with_uref(struct bpf_map *map)
924 atomic64_inc(&map->refcnt);
925 atomic64_inc(&map->usercnt);
927 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
929 struct bpf_map *bpf_map_get(u32 ufd)
931 struct fd f = fdget(ufd);
934 map = __bpf_map_get(f);
944 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
946 struct fd f = fdget(ufd);
949 map = __bpf_map_get(f);
953 bpf_map_inc_with_uref(map);
959 /* map_idr_lock should have been held */
960 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
964 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
966 return ERR_PTR(-ENOENT);
968 atomic64_inc(&map->usercnt);
973 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
975 spin_lock_bh(&map_idr_lock);
976 map = __bpf_map_inc_not_zero(map, false);
977 spin_unlock_bh(&map_idr_lock);
981 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
983 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
988 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
991 return memdup_user(ukey, key_size);
994 return ERR_PTR(-EINVAL);
999 /* last field in 'union bpf_attr' used by this command */
1000 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1002 static int map_lookup_elem(union bpf_attr *attr)
1004 void __user *ukey = u64_to_user_ptr(attr->key);
1005 void __user *uvalue = u64_to_user_ptr(attr->value);
1006 int ufd = attr->map_fd;
1007 struct bpf_map *map;
1013 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1016 if (attr->flags & ~BPF_F_LOCK)
1020 map = __bpf_map_get(f);
1022 return PTR_ERR(map);
1023 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1028 if ((attr->flags & BPF_F_LOCK) &&
1029 !map_value_has_spin_lock(map)) {
1034 key = __bpf_copy_key(ukey, map->key_size);
1040 value_size = bpf_map_value_size(map);
1043 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1047 err = bpf_map_copy_value(map, key, value, attr->flags);
1052 if (copy_to_user(uvalue, value, value_size) != 0)
1067 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1069 static int map_update_elem(union bpf_attr *attr)
1071 void __user *ukey = u64_to_user_ptr(attr->key);
1072 void __user *uvalue = u64_to_user_ptr(attr->value);
1073 int ufd = attr->map_fd;
1074 struct bpf_map *map;
1080 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1084 map = __bpf_map_get(f);
1086 return PTR_ERR(map);
1087 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1092 if ((attr->flags & BPF_F_LOCK) &&
1093 !map_value_has_spin_lock(map)) {
1098 key = __bpf_copy_key(ukey, map->key_size);
1104 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1105 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
1106 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1107 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
1108 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1110 value_size = map->value_size;
1113 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1118 if (copy_from_user(value, uvalue, value_size) != 0)
1121 err = bpf_map_update_value(map, f, key, value, attr->flags);
1132 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1134 static int map_delete_elem(union bpf_attr *attr)
1136 void __user *ukey = u64_to_user_ptr(attr->key);
1137 int ufd = attr->map_fd;
1138 struct bpf_map *map;
1143 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1147 map = __bpf_map_get(f);
1149 return PTR_ERR(map);
1150 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1155 key = __bpf_copy_key(ukey, map->key_size);
1161 if (bpf_map_is_dev_bound(map)) {
1162 err = bpf_map_offload_delete_elem(map, key);
1164 } else if (IS_FD_PROG_ARRAY(map) ||
1165 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1166 /* These maps require sleepable context */
1167 err = map->ops->map_delete_elem(map, key);
1171 bpf_disable_instrumentation();
1173 err = map->ops->map_delete_elem(map, key);
1175 bpf_enable_instrumentation();
1176 maybe_wait_bpf_programs(map);
1184 /* last field in 'union bpf_attr' used by this command */
1185 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1187 static int map_get_next_key(union bpf_attr *attr)
1189 void __user *ukey = u64_to_user_ptr(attr->key);
1190 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1191 int ufd = attr->map_fd;
1192 struct bpf_map *map;
1193 void *key, *next_key;
1197 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1201 map = __bpf_map_get(f);
1203 return PTR_ERR(map);
1204 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1210 key = __bpf_copy_key(ukey, map->key_size);
1220 next_key = kmalloc(map->key_size, GFP_USER);
1224 if (bpf_map_is_dev_bound(map)) {
1225 err = bpf_map_offload_get_next_key(map, key, next_key);
1230 err = map->ops->map_get_next_key(map, key, next_key);
1237 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1251 int generic_map_delete_batch(struct bpf_map *map,
1252 const union bpf_attr *attr,
1253 union bpf_attr __user *uattr)
1255 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1260 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1263 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1264 !map_value_has_spin_lock(map)) {
1268 max_count = attr->batch.count;
1272 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1276 for (cp = 0; cp < max_count; cp++) {
1278 if (copy_from_user(key, keys + cp * map->key_size,
1282 if (bpf_map_is_dev_bound(map)) {
1283 err = bpf_map_offload_delete_elem(map, key);
1287 bpf_disable_instrumentation();
1289 err = map->ops->map_delete_elem(map, key);
1291 bpf_enable_instrumentation();
1292 maybe_wait_bpf_programs(map);
1296 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1303 int generic_map_update_batch(struct bpf_map *map,
1304 const union bpf_attr *attr,
1305 union bpf_attr __user *uattr)
1307 void __user *values = u64_to_user_ptr(attr->batch.values);
1308 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1309 u32 value_size, cp, max_count;
1310 int ufd = attr->map_fd;
1316 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1319 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1320 !map_value_has_spin_lock(map)) {
1324 value_size = bpf_map_value_size(map);
1326 max_count = attr->batch.count;
1330 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1334 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1340 for (cp = 0; cp < max_count; cp++) {
1342 if (copy_from_user(key, keys + cp * map->key_size,
1344 copy_from_user(value, values + cp * value_size, value_size))
1347 err = bpf_map_update_value(map, f, key, value,
1348 attr->batch.elem_flags);
1354 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1362 #define MAP_LOOKUP_RETRIES 3
1364 int generic_map_lookup_batch(struct bpf_map *map,
1365 const union bpf_attr *attr,
1366 union bpf_attr __user *uattr)
1368 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1369 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1370 void __user *values = u64_to_user_ptr(attr->batch.values);
1371 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1372 void *buf, *buf_prevkey, *prev_key, *key, *value;
1373 int err, retry = MAP_LOOKUP_RETRIES;
1374 u32 value_size, cp, max_count;
1376 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1379 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1380 !map_value_has_spin_lock(map))
1383 value_size = bpf_map_value_size(map);
1385 max_count = attr->batch.count;
1389 if (put_user(0, &uattr->batch.count))
1392 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1396 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1404 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1407 value = key + map->key_size;
1409 prev_key = buf_prevkey;
1411 for (cp = 0; cp < max_count;) {
1413 err = map->ops->map_get_next_key(map, prev_key, key);
1417 err = bpf_map_copy_value(map, key, value,
1418 attr->batch.elem_flags);
1420 if (err == -ENOENT) {
1432 if (copy_to_user(keys + cp * map->key_size, key,
1437 if (copy_to_user(values + cp * value_size, value, value_size)) {
1443 prev_key = buf_prevkey;
1445 swap(prev_key, key);
1446 retry = MAP_LOOKUP_RETRIES;
1453 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1454 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1463 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1465 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1467 void __user *ukey = u64_to_user_ptr(attr->key);
1468 void __user *uvalue = u64_to_user_ptr(attr->value);
1469 int ufd = attr->map_fd;
1470 struct bpf_map *map;
1476 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1480 map = __bpf_map_get(f);
1482 return PTR_ERR(map);
1483 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1484 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1489 key = __bpf_copy_key(ukey, map->key_size);
1495 value_size = map->value_size;
1498 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1502 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1503 map->map_type == BPF_MAP_TYPE_STACK) {
1504 err = map->ops->map_pop_elem(map, value);
1512 if (copy_to_user(uvalue, value, value_size) != 0) {
1528 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1530 static int map_freeze(const union bpf_attr *attr)
1532 int err = 0, ufd = attr->map_fd;
1533 struct bpf_map *map;
1536 if (CHECK_ATTR(BPF_MAP_FREEZE))
1540 map = __bpf_map_get(f);
1542 return PTR_ERR(map);
1544 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1549 mutex_lock(&map->freeze_mutex);
1551 if (map->writecnt) {
1555 if (READ_ONCE(map->frozen)) {
1559 if (!bpf_capable()) {
1564 WRITE_ONCE(map->frozen, true);
1566 mutex_unlock(&map->freeze_mutex);
1571 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1572 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1573 [_id] = & _name ## _prog_ops,
1574 #define BPF_MAP_TYPE(_id, _ops)
1575 #define BPF_LINK_TYPE(_id, _name)
1576 #include <linux/bpf_types.h>
1577 #undef BPF_PROG_TYPE
1579 #undef BPF_LINK_TYPE
1582 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1584 const struct bpf_prog_ops *ops;
1586 if (type >= ARRAY_SIZE(bpf_prog_types))
1588 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1589 ops = bpf_prog_types[type];
1593 if (!bpf_prog_is_dev_bound(prog->aux))
1594 prog->aux->ops = ops;
1596 prog->aux->ops = &bpf_offload_prog_ops;
1607 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1608 [BPF_AUDIT_LOAD] = "LOAD",
1609 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1612 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1614 struct audit_context *ctx = NULL;
1615 struct audit_buffer *ab;
1617 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1619 if (audit_enabled == AUDIT_OFF)
1621 if (op == BPF_AUDIT_LOAD)
1622 ctx = audit_context();
1623 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1626 audit_log_format(ab, "prog-id=%u op=%s",
1627 prog->aux->id, bpf_audit_str[op]);
1631 int __bpf_prog_charge(struct user_struct *user, u32 pages)
1633 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1634 unsigned long user_bufs;
1637 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1638 if (user_bufs > memlock_limit) {
1639 atomic_long_sub(pages, &user->locked_vm);
1647 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1650 atomic_long_sub(pages, &user->locked_vm);
1653 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1655 struct user_struct *user = get_current_user();
1658 ret = __bpf_prog_charge(user, prog->pages);
1664 prog->aux->user = user;
1668 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1670 struct user_struct *user = prog->aux->user;
1672 __bpf_prog_uncharge(user, prog->pages);
1676 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1680 idr_preload(GFP_KERNEL);
1681 spin_lock_bh(&prog_idr_lock);
1682 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1685 spin_unlock_bh(&prog_idr_lock);
1688 /* id is in [1, INT_MAX) */
1689 if (WARN_ON_ONCE(!id))
1692 return id > 0 ? 0 : id;
1695 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1697 /* cBPF to eBPF migrations are currently not in the idr store.
1698 * Offloaded programs are removed from the store when their device
1699 * disappears - even if someone grabs an fd to them they are unusable,
1700 * simply waiting for refcnt to drop to be freed.
1706 spin_lock_bh(&prog_idr_lock);
1708 __acquire(&prog_idr_lock);
1710 idr_remove(&prog_idr, prog->aux->id);
1714 spin_unlock_bh(&prog_idr_lock);
1716 __release(&prog_idr_lock);
1719 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1721 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1723 kvfree(aux->func_info);
1724 kfree(aux->func_info_aux);
1725 bpf_prog_uncharge_memlock(aux->prog);
1726 security_bpf_prog_free(aux);
1727 bpf_prog_free(aux->prog);
1730 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1732 bpf_prog_kallsyms_del_all(prog);
1733 btf_put(prog->aux->btf);
1734 bpf_prog_free_linfo(prog);
1737 if (prog->aux->sleepable)
1738 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1740 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1742 __bpf_prog_put_rcu(&prog->aux->rcu);
1746 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1748 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
1749 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1750 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1751 /* bpf_prog_free_id() must be called first */
1752 bpf_prog_free_id(prog, do_idr_lock);
1753 __bpf_prog_put_noref(prog, true);
1757 void bpf_prog_put(struct bpf_prog *prog)
1759 __bpf_prog_put(prog, true);
1761 EXPORT_SYMBOL_GPL(bpf_prog_put);
1763 static int bpf_prog_release(struct inode *inode, struct file *filp)
1765 struct bpf_prog *prog = filp->private_data;
1771 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1772 struct bpf_prog_stats *stats)
1774 u64 nsecs = 0, cnt = 0;
1777 for_each_possible_cpu(cpu) {
1778 const struct bpf_prog_stats *st;
1782 st = per_cpu_ptr(prog->aux->stats, cpu);
1784 start = u64_stats_fetch_begin_irq(&st->syncp);
1787 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1791 stats->nsecs = nsecs;
1795 #ifdef CONFIG_PROC_FS
1796 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1798 const struct bpf_prog *prog = filp->private_data;
1799 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1800 struct bpf_prog_stats stats;
1802 bpf_prog_get_stats(prog, &stats);
1803 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1810 "run_time_ns:\t%llu\n"
1815 prog->pages * 1ULL << PAGE_SHIFT,
1822 const struct file_operations bpf_prog_fops = {
1823 #ifdef CONFIG_PROC_FS
1824 .show_fdinfo = bpf_prog_show_fdinfo,
1826 .release = bpf_prog_release,
1827 .read = bpf_dummy_read,
1828 .write = bpf_dummy_write,
1831 int bpf_prog_new_fd(struct bpf_prog *prog)
1835 ret = security_bpf_prog(prog);
1839 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1840 O_RDWR | O_CLOEXEC);
1843 static struct bpf_prog *____bpf_prog_get(struct fd f)
1846 return ERR_PTR(-EBADF);
1847 if (f.file->f_op != &bpf_prog_fops) {
1849 return ERR_PTR(-EINVAL);
1852 return f.file->private_data;
1855 void bpf_prog_add(struct bpf_prog *prog, int i)
1857 atomic64_add(i, &prog->aux->refcnt);
1859 EXPORT_SYMBOL_GPL(bpf_prog_add);
1861 void bpf_prog_sub(struct bpf_prog *prog, int i)
1863 /* Only to be used for undoing previous bpf_prog_add() in some
1864 * error path. We still know that another entity in our call
1865 * path holds a reference to the program, thus atomic_sub() can
1866 * be safely used in such cases!
1868 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1870 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1872 void bpf_prog_inc(struct bpf_prog *prog)
1874 atomic64_inc(&prog->aux->refcnt);
1876 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1878 /* prog_idr_lock should have been held */
1879 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1883 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1886 return ERR_PTR(-ENOENT);
1890 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1892 bool bpf_prog_get_ok(struct bpf_prog *prog,
1893 enum bpf_prog_type *attach_type, bool attach_drv)
1895 /* not an attachment, just a refcount inc, always allow */
1899 if (prog->type != *attach_type)
1901 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1907 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1910 struct fd f = fdget(ufd);
1911 struct bpf_prog *prog;
1913 prog = ____bpf_prog_get(f);
1916 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1917 prog = ERR_PTR(-EINVAL);
1927 struct bpf_prog *bpf_prog_get(u32 ufd)
1929 return __bpf_prog_get(ufd, NULL, false);
1932 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1935 return __bpf_prog_get(ufd, &type, attach_drv);
1937 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1939 /* Initially all BPF programs could be loaded w/o specifying
1940 * expected_attach_type. Later for some of them specifying expected_attach_type
1941 * at load time became required so that program could be validated properly.
1942 * Programs of types that are allowed to be loaded both w/ and w/o (for
1943 * backward compatibility) expected_attach_type, should have the default attach
1944 * type assigned to expected_attach_type for the latter case, so that it can be
1945 * validated later at attach time.
1947 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1948 * prog type requires it but has some attach types that have to be backward
1951 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1953 switch (attr->prog_type) {
1954 case BPF_PROG_TYPE_CGROUP_SOCK:
1955 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1956 * exist so checking for non-zero is the way to go here.
1958 if (!attr->expected_attach_type)
1959 attr->expected_attach_type =
1960 BPF_CGROUP_INET_SOCK_CREATE;
1966 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1967 enum bpf_attach_type expected_attach_type,
1968 u32 btf_id, u32 prog_fd)
1971 if (btf_id > BTF_MAX_TYPE)
1974 switch (prog_type) {
1975 case BPF_PROG_TYPE_TRACING:
1976 case BPF_PROG_TYPE_LSM:
1977 case BPF_PROG_TYPE_STRUCT_OPS:
1978 case BPF_PROG_TYPE_EXT:
1985 if (prog_fd && prog_type != BPF_PROG_TYPE_TRACING &&
1986 prog_type != BPF_PROG_TYPE_EXT)
1989 switch (prog_type) {
1990 case BPF_PROG_TYPE_CGROUP_SOCK:
1991 switch (expected_attach_type) {
1992 case BPF_CGROUP_INET_SOCK_CREATE:
1993 case BPF_CGROUP_INET_SOCK_RELEASE:
1994 case BPF_CGROUP_INET4_POST_BIND:
1995 case BPF_CGROUP_INET6_POST_BIND:
2000 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2001 switch (expected_attach_type) {
2002 case BPF_CGROUP_INET4_BIND:
2003 case BPF_CGROUP_INET6_BIND:
2004 case BPF_CGROUP_INET4_CONNECT:
2005 case BPF_CGROUP_INET6_CONNECT:
2006 case BPF_CGROUP_INET4_GETPEERNAME:
2007 case BPF_CGROUP_INET6_GETPEERNAME:
2008 case BPF_CGROUP_INET4_GETSOCKNAME:
2009 case BPF_CGROUP_INET6_GETSOCKNAME:
2010 case BPF_CGROUP_UDP4_SENDMSG:
2011 case BPF_CGROUP_UDP6_SENDMSG:
2012 case BPF_CGROUP_UDP4_RECVMSG:
2013 case BPF_CGROUP_UDP6_RECVMSG:
2018 case BPF_PROG_TYPE_CGROUP_SKB:
2019 switch (expected_attach_type) {
2020 case BPF_CGROUP_INET_INGRESS:
2021 case BPF_CGROUP_INET_EGRESS:
2026 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2027 switch (expected_attach_type) {
2028 case BPF_CGROUP_SETSOCKOPT:
2029 case BPF_CGROUP_GETSOCKOPT:
2034 case BPF_PROG_TYPE_SK_LOOKUP:
2035 if (expected_attach_type == BPF_SK_LOOKUP)
2038 case BPF_PROG_TYPE_EXT:
2039 if (expected_attach_type)
2047 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2049 switch (prog_type) {
2050 case BPF_PROG_TYPE_SCHED_CLS:
2051 case BPF_PROG_TYPE_SCHED_ACT:
2052 case BPF_PROG_TYPE_XDP:
2053 case BPF_PROG_TYPE_LWT_IN:
2054 case BPF_PROG_TYPE_LWT_OUT:
2055 case BPF_PROG_TYPE_LWT_XMIT:
2056 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2057 case BPF_PROG_TYPE_SK_SKB:
2058 case BPF_PROG_TYPE_SK_MSG:
2059 case BPF_PROG_TYPE_LIRC_MODE2:
2060 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2061 case BPF_PROG_TYPE_CGROUP_DEVICE:
2062 case BPF_PROG_TYPE_CGROUP_SOCK:
2063 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2064 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2065 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2066 case BPF_PROG_TYPE_SOCK_OPS:
2067 case BPF_PROG_TYPE_EXT: /* extends any prog */
2069 case BPF_PROG_TYPE_CGROUP_SKB:
2071 case BPF_PROG_TYPE_SK_REUSEPORT:
2072 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2078 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2080 switch (prog_type) {
2081 case BPF_PROG_TYPE_KPROBE:
2082 case BPF_PROG_TYPE_TRACEPOINT:
2083 case BPF_PROG_TYPE_PERF_EVENT:
2084 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2085 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2086 case BPF_PROG_TYPE_TRACING:
2087 case BPF_PROG_TYPE_LSM:
2088 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2089 case BPF_PROG_TYPE_EXT: /* extends any prog */
2096 /* last field in 'union bpf_attr' used by this command */
2097 #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
2099 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
2101 enum bpf_prog_type type = attr->prog_type;
2102 struct bpf_prog *prog;
2107 if (CHECK_ATTR(BPF_PROG_LOAD))
2110 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2111 BPF_F_ANY_ALIGNMENT |
2112 BPF_F_TEST_STATE_FREQ |
2114 BPF_F_TEST_RND_HI32))
2117 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2118 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2122 /* copy eBPF program license from user space */
2123 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
2124 sizeof(license) - 1) < 0)
2126 license[sizeof(license) - 1] = 0;
2128 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2129 is_gpl = license_is_gpl_compatible(license);
2131 if (attr->insn_cnt == 0 ||
2132 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2134 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2135 type != BPF_PROG_TYPE_CGROUP_SKB &&
2139 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2141 if (is_perfmon_prog_type(type) && !perfmon_capable())
2144 bpf_prog_load_fixup_attach_type(attr);
2145 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2146 attr->attach_btf_id,
2147 attr->attach_prog_fd))
2150 /* plain bpf_prog allocation */
2151 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2155 prog->expected_attach_type = attr->expected_attach_type;
2156 prog->aux->attach_btf_id = attr->attach_btf_id;
2157 if (attr->attach_prog_fd) {
2158 struct bpf_prog *dst_prog;
2160 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2161 if (IS_ERR(dst_prog)) {
2162 err = PTR_ERR(dst_prog);
2163 goto free_prog_nouncharge;
2165 prog->aux->dst_prog = dst_prog;
2168 prog->aux->offload_requested = !!attr->prog_ifindex;
2169 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2171 err = security_bpf_prog_alloc(prog->aux);
2173 goto free_prog_nouncharge;
2175 err = bpf_prog_charge_memlock(prog);
2179 prog->len = attr->insn_cnt;
2182 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
2183 bpf_prog_insn_size(prog)) != 0)
2186 prog->orig_prog = NULL;
2189 atomic64_set(&prog->aux->refcnt, 1);
2190 prog->gpl_compatible = is_gpl ? 1 : 0;
2192 if (bpf_prog_is_dev_bound(prog->aux)) {
2193 err = bpf_prog_offload_init(prog, attr);
2198 /* find program type: socket_filter vs tracing_filter */
2199 err = find_prog_type(type, prog);
2203 prog->aux->load_time = ktime_get_boottime_ns();
2204 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2205 sizeof(attr->prog_name));
2209 /* run eBPF verifier */
2210 err = bpf_check(&prog, attr, uattr);
2212 goto free_used_maps;
2214 prog = bpf_prog_select_runtime(prog, &err);
2216 goto free_used_maps;
2218 err = bpf_prog_alloc_id(prog);
2220 goto free_used_maps;
2222 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2223 * effectively publicly exposed. However, retrieving via
2224 * bpf_prog_get_fd_by_id() will take another reference,
2225 * therefore it cannot be gone underneath us.
2227 * Only for the time /after/ successful bpf_prog_new_fd()
2228 * and before returning to userspace, we might just hold
2229 * one reference and any parallel close on that fd could
2230 * rip everything out. Hence, below notifications must
2231 * happen before bpf_prog_new_fd().
2233 * Also, any failure handling from this point onwards must
2234 * be using bpf_prog_put() given the program is exposed.
2236 bpf_prog_kallsyms_add(prog);
2237 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2238 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2240 err = bpf_prog_new_fd(prog);
2246 /* In case we have subprogs, we need to wait for a grace
2247 * period before we can tear down JIT memory since symbols
2248 * are already exposed under kallsyms.
2250 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2253 bpf_prog_uncharge_memlock(prog);
2255 security_bpf_prog_free(prog->aux);
2256 free_prog_nouncharge:
2257 bpf_prog_free(prog);
2261 #define BPF_OBJ_LAST_FIELD file_flags
2263 static int bpf_obj_pin(const union bpf_attr *attr)
2265 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2268 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2271 static int bpf_obj_get(const union bpf_attr *attr)
2273 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2274 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2277 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2281 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2282 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2284 atomic64_set(&link->refcnt, 1);
2291 static void bpf_link_free_id(int id)
2296 spin_lock_bh(&link_idr_lock);
2297 idr_remove(&link_idr, id);
2298 spin_unlock_bh(&link_idr_lock);
2301 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2302 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2303 * anon_inode's release() call. This helper marksbpf_link as
2304 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2305 * is not decremented, it's the responsibility of a calling code that failed
2306 * to complete bpf_link initialization.
2308 void bpf_link_cleanup(struct bpf_link_primer *primer)
2310 primer->link->prog = NULL;
2311 bpf_link_free_id(primer->id);
2313 put_unused_fd(primer->fd);
2316 void bpf_link_inc(struct bpf_link *link)
2318 atomic64_inc(&link->refcnt);
2321 /* bpf_link_free is guaranteed to be called from process context */
2322 static void bpf_link_free(struct bpf_link *link)
2324 bpf_link_free_id(link->id);
2326 /* detach BPF program, clean up used resources */
2327 link->ops->release(link);
2328 bpf_prog_put(link->prog);
2330 /* free bpf_link and its containing memory */
2331 link->ops->dealloc(link);
2334 static void bpf_link_put_deferred(struct work_struct *work)
2336 struct bpf_link *link = container_of(work, struct bpf_link, work);
2338 bpf_link_free(link);
2341 /* bpf_link_put can be called from atomic context, but ensures that resources
2342 * are freed from process context
2344 void bpf_link_put(struct bpf_link *link)
2346 if (!atomic64_dec_and_test(&link->refcnt))
2350 INIT_WORK(&link->work, bpf_link_put_deferred);
2351 schedule_work(&link->work);
2353 bpf_link_free(link);
2357 static int bpf_link_release(struct inode *inode, struct file *filp)
2359 struct bpf_link *link = filp->private_data;
2365 #ifdef CONFIG_PROC_FS
2366 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2367 #define BPF_MAP_TYPE(_id, _ops)
2368 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2369 static const char *bpf_link_type_strs[] = {
2370 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2371 #include <linux/bpf_types.h>
2373 #undef BPF_PROG_TYPE
2375 #undef BPF_LINK_TYPE
2377 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2379 const struct bpf_link *link = filp->private_data;
2380 const struct bpf_prog *prog = link->prog;
2381 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2383 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2389 bpf_link_type_strs[link->type],
2393 if (link->ops->show_fdinfo)
2394 link->ops->show_fdinfo(link, m);
2398 static const struct file_operations bpf_link_fops = {
2399 #ifdef CONFIG_PROC_FS
2400 .show_fdinfo = bpf_link_show_fdinfo,
2402 .release = bpf_link_release,
2403 .read = bpf_dummy_read,
2404 .write = bpf_dummy_write,
2407 static int bpf_link_alloc_id(struct bpf_link *link)
2411 idr_preload(GFP_KERNEL);
2412 spin_lock_bh(&link_idr_lock);
2413 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2414 spin_unlock_bh(&link_idr_lock);
2420 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2421 * reserving unused FD and allocating ID from link_idr. This is to be paired
2422 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2423 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2424 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2425 * transient state is passed around in struct bpf_link_primer.
2426 * This is preferred way to create and initialize bpf_link, especially when
2427 * there are complicated and expensive operations inbetween creating bpf_link
2428 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2429 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2430 * expensive (and potentially failing) roll back operations in a rare case
2431 * that file, FD, or ID can't be allocated.
2433 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2438 fd = get_unused_fd_flags(O_CLOEXEC);
2443 id = bpf_link_alloc_id(link);
2449 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2451 bpf_link_free_id(id);
2453 return PTR_ERR(file);
2456 primer->link = link;
2457 primer->file = file;
2463 int bpf_link_settle(struct bpf_link_primer *primer)
2465 /* make bpf_link fetchable by ID */
2466 spin_lock_bh(&link_idr_lock);
2467 primer->link->id = primer->id;
2468 spin_unlock_bh(&link_idr_lock);
2469 /* make bpf_link fetchable by FD */
2470 fd_install(primer->fd, primer->file);
2471 /* pass through installed FD */
2475 int bpf_link_new_fd(struct bpf_link *link)
2477 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2480 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2482 struct fd f = fdget(ufd);
2483 struct bpf_link *link;
2486 return ERR_PTR(-EBADF);
2487 if (f.file->f_op != &bpf_link_fops) {
2489 return ERR_PTR(-EINVAL);
2492 link = f.file->private_data;
2499 struct bpf_tracing_link {
2500 struct bpf_link link;
2501 enum bpf_attach_type attach_type;
2502 struct bpf_trampoline *trampoline;
2503 struct bpf_prog *tgt_prog;
2506 static void bpf_tracing_link_release(struct bpf_link *link)
2508 struct bpf_tracing_link *tr_link =
2509 container_of(link, struct bpf_tracing_link, link);
2511 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2512 tr_link->trampoline));
2514 bpf_trampoline_put(tr_link->trampoline);
2516 /* tgt_prog is NULL if target is a kernel function */
2517 if (tr_link->tgt_prog)
2518 bpf_prog_put(tr_link->tgt_prog);
2521 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2523 struct bpf_tracing_link *tr_link =
2524 container_of(link, struct bpf_tracing_link, link);
2529 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2530 struct seq_file *seq)
2532 struct bpf_tracing_link *tr_link =
2533 container_of(link, struct bpf_tracing_link, link);
2536 "attach_type:\t%d\n",
2537 tr_link->attach_type);
2540 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2541 struct bpf_link_info *info)
2543 struct bpf_tracing_link *tr_link =
2544 container_of(link, struct bpf_tracing_link, link);
2546 info->tracing.attach_type = tr_link->attach_type;
2551 static const struct bpf_link_ops bpf_tracing_link_lops = {
2552 .release = bpf_tracing_link_release,
2553 .dealloc = bpf_tracing_link_dealloc,
2554 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2555 .fill_link_info = bpf_tracing_link_fill_link_info,
2558 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2562 struct bpf_link_primer link_primer;
2563 struct bpf_prog *tgt_prog = NULL;
2564 struct bpf_trampoline *tr = NULL;
2565 struct bpf_tracing_link *link;
2569 switch (prog->type) {
2570 case BPF_PROG_TYPE_TRACING:
2571 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2572 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2573 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2578 case BPF_PROG_TYPE_EXT:
2579 if (prog->expected_attach_type != 0) {
2584 case BPF_PROG_TYPE_LSM:
2585 if (prog->expected_attach_type != BPF_LSM_MAC) {
2595 if (!!tgt_prog_fd != !!btf_id) {
2601 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2602 if (prog->type != BPF_PROG_TYPE_EXT) {
2607 tgt_prog = bpf_prog_get(tgt_prog_fd);
2608 if (IS_ERR(tgt_prog)) {
2609 err = PTR_ERR(tgt_prog);
2614 key = bpf_trampoline_compute_key(tgt_prog, btf_id);
2617 link = kzalloc(sizeof(*link), GFP_USER);
2622 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2623 &bpf_tracing_link_lops, prog);
2624 link->attach_type = prog->expected_attach_type;
2626 mutex_lock(&prog->aux->dst_mutex);
2628 /* There are a few possible cases here:
2630 * - if prog->aux->dst_trampoline is set, the program was just loaded
2631 * and not yet attached to anything, so we can use the values stored
2634 * - if prog->aux->dst_trampoline is NULL, the program has already been
2635 * attached to a target and its initial target was cleared (below)
2637 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2638 * target_btf_id using the link_create API.
2640 * - if tgt_prog == NULL when this function was called using the old
2641 * raw_tracepoint_open API, and we need a target from prog->aux
2643 * The combination of no saved target in prog->aux, and no target
2644 * specified on load is illegal, and we reject that here.
2646 if (!prog->aux->dst_trampoline && !tgt_prog) {
2651 if (!prog->aux->dst_trampoline ||
2652 (key && key != prog->aux->dst_trampoline->key)) {
2653 /* If there is no saved target, or the specified target is
2654 * different from the destination specified at load time, we
2655 * need a new trampoline and a check for compatibility
2657 struct bpf_attach_target_info tgt_info = {};
2659 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2664 tr = bpf_trampoline_get(key, &tgt_info);
2670 /* The caller didn't specify a target, or the target was the
2671 * same as the destination supplied during program load. This
2672 * means we can reuse the trampoline and reference from program
2673 * load time, and there is no need to allocate a new one. This
2674 * can only happen once for any program, as the saved values in
2675 * prog->aux are cleared below.
2677 tr = prog->aux->dst_trampoline;
2678 tgt_prog = prog->aux->dst_prog;
2681 err = bpf_link_prime(&link->link, &link_primer);
2685 err = bpf_trampoline_link_prog(prog, tr);
2687 bpf_link_cleanup(&link_primer);
2692 link->tgt_prog = tgt_prog;
2693 link->trampoline = tr;
2695 /* Always clear the trampoline and target prog from prog->aux to make
2696 * sure the original attach destination is not kept alive after a
2697 * program is (re-)attached to another target.
2699 if (prog->aux->dst_prog &&
2700 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2701 /* got extra prog ref from syscall, or attaching to different prog */
2702 bpf_prog_put(prog->aux->dst_prog);
2703 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2704 /* we allocated a new trampoline, so free the old one */
2705 bpf_trampoline_put(prog->aux->dst_trampoline);
2707 prog->aux->dst_prog = NULL;
2708 prog->aux->dst_trampoline = NULL;
2709 mutex_unlock(&prog->aux->dst_mutex);
2711 return bpf_link_settle(&link_primer);
2713 if (tr && tr != prog->aux->dst_trampoline)
2714 bpf_trampoline_put(tr);
2715 mutex_unlock(&prog->aux->dst_mutex);
2718 if (tgt_prog_fd && tgt_prog)
2719 bpf_prog_put(tgt_prog);
2724 struct bpf_raw_tp_link {
2725 struct bpf_link link;
2726 struct bpf_raw_event_map *btp;
2729 static void bpf_raw_tp_link_release(struct bpf_link *link)
2731 struct bpf_raw_tp_link *raw_tp =
2732 container_of(link, struct bpf_raw_tp_link, link);
2734 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
2735 bpf_put_raw_tracepoint(raw_tp->btp);
2738 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2740 struct bpf_raw_tp_link *raw_tp =
2741 container_of(link, struct bpf_raw_tp_link, link);
2746 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2747 struct seq_file *seq)
2749 struct bpf_raw_tp_link *raw_tp_link =
2750 container_of(link, struct bpf_raw_tp_link, link);
2754 raw_tp_link->btp->tp->name);
2757 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2758 struct bpf_link_info *info)
2760 struct bpf_raw_tp_link *raw_tp_link =
2761 container_of(link, struct bpf_raw_tp_link, link);
2762 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2763 const char *tp_name = raw_tp_link->btp->tp->name;
2764 u32 ulen = info->raw_tracepoint.tp_name_len;
2765 size_t tp_len = strlen(tp_name);
2770 info->raw_tracepoint.tp_name_len = tp_len + 1;
2775 if (ulen >= tp_len + 1) {
2776 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2781 if (copy_to_user(ubuf, tp_name, ulen - 1))
2783 if (put_user(zero, ubuf + ulen - 1))
2791 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
2792 .release = bpf_raw_tp_link_release,
2793 .dealloc = bpf_raw_tp_link_dealloc,
2794 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2795 .fill_link_info = bpf_raw_tp_link_fill_link_info,
2798 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2800 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2802 struct bpf_link_primer link_primer;
2803 struct bpf_raw_tp_link *link;
2804 struct bpf_raw_event_map *btp;
2805 struct bpf_prog *prog;
2806 const char *tp_name;
2810 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2813 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2815 return PTR_ERR(prog);
2817 switch (prog->type) {
2818 case BPF_PROG_TYPE_TRACING:
2819 case BPF_PROG_TYPE_EXT:
2820 case BPF_PROG_TYPE_LSM:
2821 if (attr->raw_tracepoint.name) {
2822 /* The attach point for this category of programs
2823 * should be specified via btf_id during program load.
2828 if (prog->type == BPF_PROG_TYPE_TRACING &&
2829 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
2830 tp_name = prog->aux->attach_func_name;
2833 return bpf_tracing_prog_attach(prog, 0, 0);
2834 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2835 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2836 if (strncpy_from_user(buf,
2837 u64_to_user_ptr(attr->raw_tracepoint.name),
2838 sizeof(buf) - 1) < 0) {
2842 buf[sizeof(buf) - 1] = 0;
2850 btp = bpf_get_raw_tracepoint(tp_name);
2856 link = kzalloc(sizeof(*link), GFP_USER);
2861 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2862 &bpf_raw_tp_link_lops, prog);
2865 err = bpf_link_prime(&link->link, &link_primer);
2871 err = bpf_probe_register(link->btp, prog);
2873 bpf_link_cleanup(&link_primer);
2877 return bpf_link_settle(&link_primer);
2880 bpf_put_raw_tracepoint(btp);
2886 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2887 enum bpf_attach_type attach_type)
2889 switch (prog->type) {
2890 case BPF_PROG_TYPE_CGROUP_SOCK:
2891 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2892 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2893 case BPF_PROG_TYPE_SK_LOOKUP:
2894 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
2895 case BPF_PROG_TYPE_CGROUP_SKB:
2896 if (!capable(CAP_NET_ADMIN))
2897 /* cg-skb progs can be loaded by unpriv user.
2898 * check permissions at attach time.
2901 return prog->enforce_expected_attach_type &&
2902 prog->expected_attach_type != attach_type ?
2909 static enum bpf_prog_type
2910 attach_type_to_prog_type(enum bpf_attach_type attach_type)
2912 switch (attach_type) {
2913 case BPF_CGROUP_INET_INGRESS:
2914 case BPF_CGROUP_INET_EGRESS:
2915 return BPF_PROG_TYPE_CGROUP_SKB;
2916 case BPF_CGROUP_INET_SOCK_CREATE:
2917 case BPF_CGROUP_INET_SOCK_RELEASE:
2918 case BPF_CGROUP_INET4_POST_BIND:
2919 case BPF_CGROUP_INET6_POST_BIND:
2920 return BPF_PROG_TYPE_CGROUP_SOCK;
2921 case BPF_CGROUP_INET4_BIND:
2922 case BPF_CGROUP_INET6_BIND:
2923 case BPF_CGROUP_INET4_CONNECT:
2924 case BPF_CGROUP_INET6_CONNECT:
2925 case BPF_CGROUP_INET4_GETPEERNAME:
2926 case BPF_CGROUP_INET6_GETPEERNAME:
2927 case BPF_CGROUP_INET4_GETSOCKNAME:
2928 case BPF_CGROUP_INET6_GETSOCKNAME:
2929 case BPF_CGROUP_UDP4_SENDMSG:
2930 case BPF_CGROUP_UDP6_SENDMSG:
2931 case BPF_CGROUP_UDP4_RECVMSG:
2932 case BPF_CGROUP_UDP6_RECVMSG:
2933 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2934 case BPF_CGROUP_SOCK_OPS:
2935 return BPF_PROG_TYPE_SOCK_OPS;
2936 case BPF_CGROUP_DEVICE:
2937 return BPF_PROG_TYPE_CGROUP_DEVICE;
2938 case BPF_SK_MSG_VERDICT:
2939 return BPF_PROG_TYPE_SK_MSG;
2940 case BPF_SK_SKB_STREAM_PARSER:
2941 case BPF_SK_SKB_STREAM_VERDICT:
2942 return BPF_PROG_TYPE_SK_SKB;
2943 case BPF_LIRC_MODE2:
2944 return BPF_PROG_TYPE_LIRC_MODE2;
2945 case BPF_FLOW_DISSECTOR:
2946 return BPF_PROG_TYPE_FLOW_DISSECTOR;
2947 case BPF_CGROUP_SYSCTL:
2948 return BPF_PROG_TYPE_CGROUP_SYSCTL;
2949 case BPF_CGROUP_GETSOCKOPT:
2950 case BPF_CGROUP_SETSOCKOPT:
2951 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
2952 case BPF_TRACE_ITER:
2953 return BPF_PROG_TYPE_TRACING;
2955 return BPF_PROG_TYPE_SK_LOOKUP;
2957 return BPF_PROG_TYPE_XDP;
2959 return BPF_PROG_TYPE_UNSPEC;
2963 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
2965 #define BPF_F_ATTACH_MASK \
2966 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
2968 static int bpf_prog_attach(const union bpf_attr *attr)
2970 enum bpf_prog_type ptype;
2971 struct bpf_prog *prog;
2974 if (CHECK_ATTR(BPF_PROG_ATTACH))
2977 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
2980 ptype = attach_type_to_prog_type(attr->attach_type);
2981 if (ptype == BPF_PROG_TYPE_UNSPEC)
2984 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2986 return PTR_ERR(prog);
2988 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
2994 case BPF_PROG_TYPE_SK_SKB:
2995 case BPF_PROG_TYPE_SK_MSG:
2996 ret = sock_map_get_from_fd(attr, prog);
2998 case BPF_PROG_TYPE_LIRC_MODE2:
2999 ret = lirc_prog_attach(attr, prog);
3001 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3002 ret = netns_bpf_prog_attach(attr, prog);
3004 case BPF_PROG_TYPE_CGROUP_DEVICE:
3005 case BPF_PROG_TYPE_CGROUP_SKB:
3006 case BPF_PROG_TYPE_CGROUP_SOCK:
3007 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3008 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3009 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3010 case BPF_PROG_TYPE_SOCK_OPS:
3011 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3022 #define BPF_PROG_DETACH_LAST_FIELD attach_type
3024 static int bpf_prog_detach(const union bpf_attr *attr)
3026 enum bpf_prog_type ptype;
3028 if (CHECK_ATTR(BPF_PROG_DETACH))
3031 ptype = attach_type_to_prog_type(attr->attach_type);
3034 case BPF_PROG_TYPE_SK_MSG:
3035 case BPF_PROG_TYPE_SK_SKB:
3036 return sock_map_prog_detach(attr, ptype);
3037 case BPF_PROG_TYPE_LIRC_MODE2:
3038 return lirc_prog_detach(attr);
3039 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3040 return netns_bpf_prog_detach(attr, ptype);
3041 case BPF_PROG_TYPE_CGROUP_DEVICE:
3042 case BPF_PROG_TYPE_CGROUP_SKB:
3043 case BPF_PROG_TYPE_CGROUP_SOCK:
3044 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3045 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3046 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3047 case BPF_PROG_TYPE_SOCK_OPS:
3048 return cgroup_bpf_prog_detach(attr, ptype);
3054 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3056 static int bpf_prog_query(const union bpf_attr *attr,
3057 union bpf_attr __user *uattr)
3059 if (!capable(CAP_NET_ADMIN))
3061 if (CHECK_ATTR(BPF_PROG_QUERY))
3063 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3066 switch (attr->query.attach_type) {
3067 case BPF_CGROUP_INET_INGRESS:
3068 case BPF_CGROUP_INET_EGRESS:
3069 case BPF_CGROUP_INET_SOCK_CREATE:
3070 case BPF_CGROUP_INET_SOCK_RELEASE:
3071 case BPF_CGROUP_INET4_BIND:
3072 case BPF_CGROUP_INET6_BIND:
3073 case BPF_CGROUP_INET4_POST_BIND:
3074 case BPF_CGROUP_INET6_POST_BIND:
3075 case BPF_CGROUP_INET4_CONNECT:
3076 case BPF_CGROUP_INET6_CONNECT:
3077 case BPF_CGROUP_INET4_GETPEERNAME:
3078 case BPF_CGROUP_INET6_GETPEERNAME:
3079 case BPF_CGROUP_INET4_GETSOCKNAME:
3080 case BPF_CGROUP_INET6_GETSOCKNAME:
3081 case BPF_CGROUP_UDP4_SENDMSG:
3082 case BPF_CGROUP_UDP6_SENDMSG:
3083 case BPF_CGROUP_UDP4_RECVMSG:
3084 case BPF_CGROUP_UDP6_RECVMSG:
3085 case BPF_CGROUP_SOCK_OPS:
3086 case BPF_CGROUP_DEVICE:
3087 case BPF_CGROUP_SYSCTL:
3088 case BPF_CGROUP_GETSOCKOPT:
3089 case BPF_CGROUP_SETSOCKOPT:
3090 return cgroup_bpf_prog_query(attr, uattr);
3091 case BPF_LIRC_MODE2:
3092 return lirc_prog_query(attr, uattr);
3093 case BPF_FLOW_DISSECTOR:
3095 return netns_bpf_prog_query(attr, uattr);
3101 #define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
3103 static int bpf_prog_test_run(const union bpf_attr *attr,
3104 union bpf_attr __user *uattr)
3106 struct bpf_prog *prog;
3107 int ret = -ENOTSUPP;
3109 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3112 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3113 (!attr->test.ctx_size_in && attr->test.ctx_in))
3116 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3117 (!attr->test.ctx_size_out && attr->test.ctx_out))
3120 prog = bpf_prog_get(attr->test.prog_fd);
3122 return PTR_ERR(prog);
3124 if (prog->aux->ops->test_run)
3125 ret = prog->aux->ops->test_run(prog, attr, uattr);
3131 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3133 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3134 union bpf_attr __user *uattr,
3138 u32 next_id = attr->start_id;
3141 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3144 if (!capable(CAP_SYS_ADMIN))
3149 if (!idr_get_next(idr, &next_id))
3151 spin_unlock_bh(lock);
3154 err = put_user(next_id, &uattr->next_id);
3159 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3161 struct bpf_map *map;
3163 spin_lock_bh(&map_idr_lock);
3165 map = idr_get_next(&map_idr, id);
3167 map = __bpf_map_inc_not_zero(map, false);
3173 spin_unlock_bh(&map_idr_lock);
3178 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3180 struct bpf_prog *prog;
3182 spin_lock_bh(&prog_idr_lock);
3184 prog = idr_get_next(&prog_idr, id);
3186 prog = bpf_prog_inc_not_zero(prog);
3192 spin_unlock_bh(&prog_idr_lock);
3197 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3199 struct bpf_prog *bpf_prog_by_id(u32 id)
3201 struct bpf_prog *prog;
3204 return ERR_PTR(-ENOENT);
3206 spin_lock_bh(&prog_idr_lock);
3207 prog = idr_find(&prog_idr, id);
3209 prog = bpf_prog_inc_not_zero(prog);
3211 prog = ERR_PTR(-ENOENT);
3212 spin_unlock_bh(&prog_idr_lock);
3216 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3218 struct bpf_prog *prog;
3219 u32 id = attr->prog_id;
3222 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3225 if (!capable(CAP_SYS_ADMIN))
3228 prog = bpf_prog_by_id(id);
3230 return PTR_ERR(prog);
3232 fd = bpf_prog_new_fd(prog);
3239 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3241 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3243 struct bpf_map *map;
3244 u32 id = attr->map_id;
3248 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3249 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3252 if (!capable(CAP_SYS_ADMIN))
3255 f_flags = bpf_get_file_flag(attr->open_flags);
3259 spin_lock_bh(&map_idr_lock);
3260 map = idr_find(&map_idr, id);
3262 map = __bpf_map_inc_not_zero(map, true);
3264 map = ERR_PTR(-ENOENT);
3265 spin_unlock_bh(&map_idr_lock);
3268 return PTR_ERR(map);
3270 fd = bpf_map_new_fd(map, f_flags);
3272 bpf_map_put_with_uref(map);
3277 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3278 unsigned long addr, u32 *off,
3281 const struct bpf_map *map;
3284 mutex_lock(&prog->aux->used_maps_mutex);
3285 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3286 map = prog->aux->used_maps[i];
3287 if (map == (void *)addr) {
3288 *type = BPF_PSEUDO_MAP_FD;
3291 if (!map->ops->map_direct_value_meta)
3293 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3294 *type = BPF_PSEUDO_MAP_VALUE;
3301 mutex_unlock(&prog->aux->used_maps_mutex);
3305 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3306 const struct cred *f_cred)
3308 const struct bpf_map *map;
3309 struct bpf_insn *insns;
3315 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3320 for (i = 0; i < prog->len; i++) {
3321 code = insns[i].code;
3323 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3324 insns[i].code = BPF_JMP | BPF_CALL;
3325 insns[i].imm = BPF_FUNC_tail_call;
3328 if (code == (BPF_JMP | BPF_CALL) ||
3329 code == (BPF_JMP | BPF_CALL_ARGS)) {
3330 if (code == (BPF_JMP | BPF_CALL_ARGS))
3331 insns[i].code = BPF_JMP | BPF_CALL;
3332 if (!bpf_dump_raw_ok(f_cred))
3336 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3337 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3341 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3344 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3345 map = bpf_map_from_imm(prog, imm, &off, &type);
3347 insns[i].src_reg = type;
3348 insns[i].imm = map->id;
3349 insns[i + 1].imm = off;
3357 static int set_info_rec_size(struct bpf_prog_info *info)
3360 * Ensure info.*_rec_size is the same as kernel expected size
3364 * Only allow zero *_rec_size if both _rec_size and _cnt are
3365 * zero. In this case, the kernel will set the expected
3366 * _rec_size back to the info.
3369 if ((info->nr_func_info || info->func_info_rec_size) &&
3370 info->func_info_rec_size != sizeof(struct bpf_func_info))
3373 if ((info->nr_line_info || info->line_info_rec_size) &&
3374 info->line_info_rec_size != sizeof(struct bpf_line_info))
3377 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3378 info->jited_line_info_rec_size != sizeof(__u64))
3381 info->func_info_rec_size = sizeof(struct bpf_func_info);
3382 info->line_info_rec_size = sizeof(struct bpf_line_info);
3383 info->jited_line_info_rec_size = sizeof(__u64);
3388 static int bpf_prog_get_info_by_fd(struct file *file,
3389 struct bpf_prog *prog,
3390 const union bpf_attr *attr,
3391 union bpf_attr __user *uattr)
3393 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3394 struct bpf_prog_info info;
3395 u32 info_len = attr->info.info_len;
3396 struct bpf_prog_stats stats;
3397 char __user *uinsns;
3401 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3404 info_len = min_t(u32, sizeof(info), info_len);
3406 memset(&info, 0, sizeof(info));
3407 if (copy_from_user(&info, uinfo, info_len))
3410 info.type = prog->type;
3411 info.id = prog->aux->id;
3412 info.load_time = prog->aux->load_time;
3413 info.created_by_uid = from_kuid_munged(current_user_ns(),
3414 prog->aux->user->uid);
3415 info.gpl_compatible = prog->gpl_compatible;
3417 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3418 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3420 mutex_lock(&prog->aux->used_maps_mutex);
3421 ulen = info.nr_map_ids;
3422 info.nr_map_ids = prog->aux->used_map_cnt;
3423 ulen = min_t(u32, info.nr_map_ids, ulen);
3425 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3428 for (i = 0; i < ulen; i++)
3429 if (put_user(prog->aux->used_maps[i]->id,
3430 &user_map_ids[i])) {
3431 mutex_unlock(&prog->aux->used_maps_mutex);
3435 mutex_unlock(&prog->aux->used_maps_mutex);
3437 err = set_info_rec_size(&info);
3441 bpf_prog_get_stats(prog, &stats);
3442 info.run_time_ns = stats.nsecs;
3443 info.run_cnt = stats.cnt;
3445 if (!bpf_capable()) {
3446 info.jited_prog_len = 0;
3447 info.xlated_prog_len = 0;
3448 info.nr_jited_ksyms = 0;
3449 info.nr_jited_func_lens = 0;
3450 info.nr_func_info = 0;
3451 info.nr_line_info = 0;
3452 info.nr_jited_line_info = 0;
3456 ulen = info.xlated_prog_len;
3457 info.xlated_prog_len = bpf_prog_insn_size(prog);
3458 if (info.xlated_prog_len && ulen) {
3459 struct bpf_insn *insns_sanitized;
3462 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3463 info.xlated_prog_insns = 0;
3466 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3467 if (!insns_sanitized)
3469 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3470 ulen = min_t(u32, info.xlated_prog_len, ulen);
3471 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3472 kfree(insns_sanitized);
3477 if (bpf_prog_is_dev_bound(prog->aux)) {
3478 err = bpf_prog_offload_info_fill(&info, prog);
3484 /* NOTE: the following code is supposed to be skipped for offload.
3485 * bpf_prog_offload_info_fill() is the place to fill similar fields
3488 ulen = info.jited_prog_len;
3489 if (prog->aux->func_cnt) {
3492 info.jited_prog_len = 0;
3493 for (i = 0; i < prog->aux->func_cnt; i++)
3494 info.jited_prog_len += prog->aux->func[i]->jited_len;
3496 info.jited_prog_len = prog->jited_len;
3499 if (info.jited_prog_len && ulen) {
3500 if (bpf_dump_raw_ok(file->f_cred)) {
3501 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3502 ulen = min_t(u32, info.jited_prog_len, ulen);
3504 /* for multi-function programs, copy the JITed
3505 * instructions for all the functions
3507 if (prog->aux->func_cnt) {
3512 for (i = 0; i < prog->aux->func_cnt; i++) {
3513 len = prog->aux->func[i]->jited_len;
3514 len = min_t(u32, len, free);
3515 img = (u8 *) prog->aux->func[i]->bpf_func;
3516 if (copy_to_user(uinsns, img, len))
3524 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3528 info.jited_prog_insns = 0;
3532 ulen = info.nr_jited_ksyms;
3533 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
3535 if (bpf_dump_raw_ok(file->f_cred)) {
3536 unsigned long ksym_addr;
3537 u64 __user *user_ksyms;
3540 /* copy the address of the kernel symbol
3541 * corresponding to each function
3543 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3544 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
3545 if (prog->aux->func_cnt) {
3546 for (i = 0; i < ulen; i++) {
3547 ksym_addr = (unsigned long)
3548 prog->aux->func[i]->bpf_func;
3549 if (put_user((u64) ksym_addr,
3554 ksym_addr = (unsigned long) prog->bpf_func;
3555 if (put_user((u64) ksym_addr, &user_ksyms[0]))
3559 info.jited_ksyms = 0;
3563 ulen = info.nr_jited_func_lens;
3564 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
3566 if (bpf_dump_raw_ok(file->f_cred)) {
3567 u32 __user *user_lens;
3570 /* copy the JITed image lengths for each function */
3571 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3572 user_lens = u64_to_user_ptr(info.jited_func_lens);
3573 if (prog->aux->func_cnt) {
3574 for (i = 0; i < ulen; i++) {
3576 prog->aux->func[i]->jited_len;
3577 if (put_user(func_len, &user_lens[i]))
3581 func_len = prog->jited_len;
3582 if (put_user(func_len, &user_lens[0]))
3586 info.jited_func_lens = 0;
3591 info.btf_id = btf_id(prog->aux->btf);
3593 ulen = info.nr_func_info;
3594 info.nr_func_info = prog->aux->func_info_cnt;
3595 if (info.nr_func_info && ulen) {
3596 char __user *user_finfo;
3598 user_finfo = u64_to_user_ptr(info.func_info);
3599 ulen = min_t(u32, info.nr_func_info, ulen);
3600 if (copy_to_user(user_finfo, prog->aux->func_info,
3601 info.func_info_rec_size * ulen))
3605 ulen = info.nr_line_info;
3606 info.nr_line_info = prog->aux->nr_linfo;
3607 if (info.nr_line_info && ulen) {
3608 __u8 __user *user_linfo;
3610 user_linfo = u64_to_user_ptr(info.line_info);
3611 ulen = min_t(u32, info.nr_line_info, ulen);
3612 if (copy_to_user(user_linfo, prog->aux->linfo,
3613 info.line_info_rec_size * ulen))
3617 ulen = info.nr_jited_line_info;
3618 if (prog->aux->jited_linfo)
3619 info.nr_jited_line_info = prog->aux->nr_linfo;
3621 info.nr_jited_line_info = 0;
3622 if (info.nr_jited_line_info && ulen) {
3623 if (bpf_dump_raw_ok(file->f_cred)) {
3624 __u64 __user *user_linfo;
3627 user_linfo = u64_to_user_ptr(info.jited_line_info);
3628 ulen = min_t(u32, info.nr_jited_line_info, ulen);
3629 for (i = 0; i < ulen; i++) {
3630 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3635 info.jited_line_info = 0;
3639 ulen = info.nr_prog_tags;
3640 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3642 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3645 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3646 ulen = min_t(u32, info.nr_prog_tags, ulen);
3647 if (prog->aux->func_cnt) {
3648 for (i = 0; i < ulen; i++) {
3649 if (copy_to_user(user_prog_tags[i],
3650 prog->aux->func[i]->tag,
3655 if (copy_to_user(user_prog_tags[0],
3656 prog->tag, BPF_TAG_SIZE))
3662 if (copy_to_user(uinfo, &info, info_len) ||
3663 put_user(info_len, &uattr->info.info_len))
3669 static int bpf_map_get_info_by_fd(struct file *file,
3670 struct bpf_map *map,
3671 const union bpf_attr *attr,
3672 union bpf_attr __user *uattr)
3674 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3675 struct bpf_map_info info;
3676 u32 info_len = attr->info.info_len;
3679 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3682 info_len = min_t(u32, sizeof(info), info_len);
3684 memset(&info, 0, sizeof(info));
3685 info.type = map->map_type;
3687 info.key_size = map->key_size;
3688 info.value_size = map->value_size;
3689 info.max_entries = map->max_entries;
3690 info.map_flags = map->map_flags;
3691 memcpy(info.name, map->name, sizeof(map->name));
3694 info.btf_id = btf_id(map->btf);
3695 info.btf_key_type_id = map->btf_key_type_id;
3696 info.btf_value_type_id = map->btf_value_type_id;
3698 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
3700 if (bpf_map_is_dev_bound(map)) {
3701 err = bpf_map_offload_info_fill(&info, map);
3706 if (copy_to_user(uinfo, &info, info_len) ||
3707 put_user(info_len, &uattr->info.info_len))
3713 static int bpf_btf_get_info_by_fd(struct file *file,
3715 const union bpf_attr *attr,
3716 union bpf_attr __user *uattr)
3718 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3719 u32 info_len = attr->info.info_len;
3722 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
3726 return btf_get_info_by_fd(btf, attr, uattr);
3729 static int bpf_link_get_info_by_fd(struct file *file,
3730 struct bpf_link *link,
3731 const union bpf_attr *attr,
3732 union bpf_attr __user *uattr)
3734 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3735 struct bpf_link_info info;
3736 u32 info_len = attr->info.info_len;
3739 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3742 info_len = min_t(u32, sizeof(info), info_len);
3744 memset(&info, 0, sizeof(info));
3745 if (copy_from_user(&info, uinfo, info_len))
3748 info.type = link->type;
3750 info.prog_id = link->prog->aux->id;
3752 if (link->ops->fill_link_info) {
3753 err = link->ops->fill_link_info(link, &info);
3758 if (copy_to_user(uinfo, &info, info_len) ||
3759 put_user(info_len, &uattr->info.info_len))
3766 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3768 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3769 union bpf_attr __user *uattr)
3771 int ufd = attr->info.bpf_fd;
3775 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3782 if (f.file->f_op == &bpf_prog_fops)
3783 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
3785 else if (f.file->f_op == &bpf_map_fops)
3786 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
3788 else if (f.file->f_op == &btf_fops)
3789 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
3790 else if (f.file->f_op == &bpf_link_fops)
3791 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
3800 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3802 static int bpf_btf_load(const union bpf_attr *attr)
3804 if (CHECK_ATTR(BPF_BTF_LOAD))
3810 return btf_new_fd(attr);
3813 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3815 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3817 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3820 if (!capable(CAP_SYS_ADMIN))
3823 return btf_get_fd_by_id(attr->btf_id);
3826 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3827 union bpf_attr __user *uattr,
3828 u32 prog_id, u32 fd_type,
3829 const char *buf, u64 probe_offset,
3832 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3833 u32 len = buf ? strlen(buf) : 0, input_len;
3836 if (put_user(len, &uattr->task_fd_query.buf_len))
3838 input_len = attr->task_fd_query.buf_len;
3839 if (input_len && ubuf) {
3841 /* nothing to copy, just make ubuf NULL terminated */
3844 if (put_user(zero, ubuf))
3846 } else if (input_len >= len + 1) {
3847 /* ubuf can hold the string with NULL terminator */
3848 if (copy_to_user(ubuf, buf, len + 1))
3851 /* ubuf cannot hold the string with NULL terminator,
3852 * do a partial copy with NULL terminator.
3857 if (copy_to_user(ubuf, buf, input_len - 1))
3859 if (put_user(zero, ubuf + input_len - 1))
3864 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3865 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3866 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3867 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3873 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3875 static int bpf_task_fd_query(const union bpf_attr *attr,
3876 union bpf_attr __user *uattr)
3878 pid_t pid = attr->task_fd_query.pid;
3879 u32 fd = attr->task_fd_query.fd;
3880 const struct perf_event *event;
3881 struct files_struct *files;
3882 struct task_struct *task;
3886 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3889 if (!capable(CAP_SYS_ADMIN))
3892 if (attr->task_fd_query.flags != 0)
3895 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3899 files = get_files_struct(task);
3900 put_task_struct(task);
3905 spin_lock(&files->file_lock);
3906 file = fcheck_files(files, fd);
3911 spin_unlock(&files->file_lock);
3912 put_files_struct(files);
3917 if (file->f_op == &bpf_link_fops) {
3918 struct bpf_link *link = file->private_data;
3920 if (link->ops == &bpf_raw_tp_link_lops) {
3921 struct bpf_raw_tp_link *raw_tp =
3922 container_of(link, struct bpf_raw_tp_link, link);
3923 struct bpf_raw_event_map *btp = raw_tp->btp;
3925 err = bpf_task_fd_query_copy(attr, uattr,
3926 raw_tp->link.prog->aux->id,
3927 BPF_FD_TYPE_RAW_TRACEPOINT,
3928 btp->tp->name, 0, 0);
3934 event = perf_get_event(file);
3935 if (!IS_ERR(event)) {
3936 u64 probe_offset, probe_addr;
3937 u32 prog_id, fd_type;
3940 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3941 &buf, &probe_offset,
3944 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3959 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
3961 #define BPF_DO_BATCH(fn) \
3967 err = fn(map, attr, uattr); \
3970 static int bpf_map_do_batch(const union bpf_attr *attr,
3971 union bpf_attr __user *uattr,
3974 struct bpf_map *map;
3978 if (CHECK_ATTR(BPF_MAP_BATCH))
3981 ufd = attr->batch.map_fd;
3983 map = __bpf_map_get(f);
3985 return PTR_ERR(map);
3987 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
3988 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
3989 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
3994 if (cmd != BPF_MAP_LOOKUP_BATCH &&
3995 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4000 if (cmd == BPF_MAP_LOOKUP_BATCH)
4001 BPF_DO_BATCH(map->ops->map_lookup_batch);
4002 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4003 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
4004 else if (cmd == BPF_MAP_UPDATE_BATCH)
4005 BPF_DO_BATCH(map->ops->map_update_batch);
4007 BPF_DO_BATCH(map->ops->map_delete_batch);
4014 static int tracing_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
4016 if (attr->link_create.attach_type != prog->expected_attach_type)
4019 if (prog->expected_attach_type == BPF_TRACE_ITER)
4020 return bpf_iter_link_attach(attr, prog);
4021 else if (prog->type == BPF_PROG_TYPE_EXT)
4022 return bpf_tracing_prog_attach(prog,
4023 attr->link_create.target_fd,
4024 attr->link_create.target_btf_id);
4028 #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
4029 static int link_create(union bpf_attr *attr)
4031 enum bpf_prog_type ptype;
4032 struct bpf_prog *prog;
4035 if (CHECK_ATTR(BPF_LINK_CREATE))
4038 prog = bpf_prog_get(attr->link_create.prog_fd);
4040 return PTR_ERR(prog);
4042 ret = bpf_prog_attach_check_attach_type(prog,
4043 attr->link_create.attach_type);
4047 if (prog->type == BPF_PROG_TYPE_EXT) {
4048 ret = tracing_bpf_link_attach(attr, prog);
4052 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4053 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4059 case BPF_PROG_TYPE_CGROUP_SKB:
4060 case BPF_PROG_TYPE_CGROUP_SOCK:
4061 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4062 case BPF_PROG_TYPE_SOCK_OPS:
4063 case BPF_PROG_TYPE_CGROUP_DEVICE:
4064 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4065 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4066 ret = cgroup_bpf_link_attach(attr, prog);
4068 case BPF_PROG_TYPE_TRACING:
4069 ret = tracing_bpf_link_attach(attr, prog);
4071 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4072 case BPF_PROG_TYPE_SK_LOOKUP:
4073 ret = netns_bpf_link_create(attr, prog);
4076 case BPF_PROG_TYPE_XDP:
4077 ret = bpf_xdp_link_attach(attr, prog);
4090 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4092 static int link_update(union bpf_attr *attr)
4094 struct bpf_prog *old_prog = NULL, *new_prog;
4095 struct bpf_link *link;
4099 if (CHECK_ATTR(BPF_LINK_UPDATE))
4102 flags = attr->link_update.flags;
4103 if (flags & ~BPF_F_REPLACE)
4106 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4108 return PTR_ERR(link);
4110 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4111 if (IS_ERR(new_prog)) {
4112 ret = PTR_ERR(new_prog);
4116 if (flags & BPF_F_REPLACE) {
4117 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4118 if (IS_ERR(old_prog)) {
4119 ret = PTR_ERR(old_prog);
4123 } else if (attr->link_update.old_prog_fd) {
4128 if (link->ops->update_prog)
4129 ret = link->ops->update_prog(link, new_prog, old_prog);
4135 bpf_prog_put(old_prog);
4137 bpf_prog_put(new_prog);
4143 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4145 static int link_detach(union bpf_attr *attr)
4147 struct bpf_link *link;
4150 if (CHECK_ATTR(BPF_LINK_DETACH))
4153 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4155 return PTR_ERR(link);
4157 if (link->ops->detach)
4158 ret = link->ops->detach(link);
4166 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4168 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4171 struct bpf_link *bpf_link_by_id(u32 id)
4173 struct bpf_link *link;
4176 return ERR_PTR(-ENOENT);
4178 spin_lock_bh(&link_idr_lock);
4179 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4180 link = idr_find(&link_idr, id);
4183 link = bpf_link_inc_not_zero(link);
4185 link = ERR_PTR(-EAGAIN);
4187 link = ERR_PTR(-ENOENT);
4189 spin_unlock_bh(&link_idr_lock);
4193 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4195 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4197 struct bpf_link *link;
4198 u32 id = attr->link_id;
4201 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4204 if (!capable(CAP_SYS_ADMIN))
4207 link = bpf_link_by_id(id);
4209 return PTR_ERR(link);
4211 fd = bpf_link_new_fd(link);
4218 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4220 static int bpf_stats_release(struct inode *inode, struct file *file)
4222 mutex_lock(&bpf_stats_enabled_mutex);
4223 static_key_slow_dec(&bpf_stats_enabled_key.key);
4224 mutex_unlock(&bpf_stats_enabled_mutex);
4228 static const struct file_operations bpf_stats_fops = {
4229 .release = bpf_stats_release,
4232 static int bpf_enable_runtime_stats(void)
4236 mutex_lock(&bpf_stats_enabled_mutex);
4238 /* Set a very high limit to avoid overflow */
4239 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4240 mutex_unlock(&bpf_stats_enabled_mutex);
4244 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4246 static_key_slow_inc(&bpf_stats_enabled_key.key);
4248 mutex_unlock(&bpf_stats_enabled_mutex);
4252 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4254 static int bpf_enable_stats(union bpf_attr *attr)
4257 if (CHECK_ATTR(BPF_ENABLE_STATS))
4260 if (!capable(CAP_SYS_ADMIN))
4263 switch (attr->enable_stats.type) {
4264 case BPF_STATS_RUN_TIME:
4265 return bpf_enable_runtime_stats();
4272 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4274 static int bpf_iter_create(union bpf_attr *attr)
4276 struct bpf_link *link;
4279 if (CHECK_ATTR(BPF_ITER_CREATE))
4282 if (attr->iter_create.flags)
4285 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4287 return PTR_ERR(link);
4289 err = bpf_iter_new_fd(link);
4295 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4297 static int bpf_prog_bind_map(union bpf_attr *attr)
4299 struct bpf_prog *prog;
4300 struct bpf_map *map;
4301 struct bpf_map **used_maps_old, **used_maps_new;
4304 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4307 if (attr->prog_bind_map.flags)
4310 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4312 return PTR_ERR(prog);
4314 map = bpf_map_get(attr->prog_bind_map.map_fd);
4320 mutex_lock(&prog->aux->used_maps_mutex);
4322 used_maps_old = prog->aux->used_maps;
4324 for (i = 0; i < prog->aux->used_map_cnt; i++)
4325 if (used_maps_old[i] == map) {
4330 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4331 sizeof(used_maps_new[0]),
4333 if (!used_maps_new) {
4338 memcpy(used_maps_new, used_maps_old,
4339 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4340 used_maps_new[prog->aux->used_map_cnt] = map;
4342 prog->aux->used_map_cnt++;
4343 prog->aux->used_maps = used_maps_new;
4345 kfree(used_maps_old);
4348 mutex_unlock(&prog->aux->used_maps_mutex);
4357 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4359 union bpf_attr attr;
4362 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
4365 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4368 size = min_t(u32, size, sizeof(attr));
4370 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4371 memset(&attr, 0, sizeof(attr));
4372 if (copy_from_user(&attr, uattr, size) != 0)
4375 err = security_bpf(cmd, &attr, size);
4380 case BPF_MAP_CREATE:
4381 err = map_create(&attr);
4383 case BPF_MAP_LOOKUP_ELEM:
4384 err = map_lookup_elem(&attr);
4386 case BPF_MAP_UPDATE_ELEM:
4387 err = map_update_elem(&attr);
4389 case BPF_MAP_DELETE_ELEM:
4390 err = map_delete_elem(&attr);
4392 case BPF_MAP_GET_NEXT_KEY:
4393 err = map_get_next_key(&attr);
4395 case BPF_MAP_FREEZE:
4396 err = map_freeze(&attr);
4399 err = bpf_prog_load(&attr, uattr);
4402 err = bpf_obj_pin(&attr);
4405 err = bpf_obj_get(&attr);
4407 case BPF_PROG_ATTACH:
4408 err = bpf_prog_attach(&attr);
4410 case BPF_PROG_DETACH:
4411 err = bpf_prog_detach(&attr);
4413 case BPF_PROG_QUERY:
4414 err = bpf_prog_query(&attr, uattr);
4416 case BPF_PROG_TEST_RUN:
4417 err = bpf_prog_test_run(&attr, uattr);
4419 case BPF_PROG_GET_NEXT_ID:
4420 err = bpf_obj_get_next_id(&attr, uattr,
4421 &prog_idr, &prog_idr_lock);
4423 case BPF_MAP_GET_NEXT_ID:
4424 err = bpf_obj_get_next_id(&attr, uattr,
4425 &map_idr, &map_idr_lock);
4427 case BPF_BTF_GET_NEXT_ID:
4428 err = bpf_obj_get_next_id(&attr, uattr,
4429 &btf_idr, &btf_idr_lock);
4431 case BPF_PROG_GET_FD_BY_ID:
4432 err = bpf_prog_get_fd_by_id(&attr);
4434 case BPF_MAP_GET_FD_BY_ID:
4435 err = bpf_map_get_fd_by_id(&attr);
4437 case BPF_OBJ_GET_INFO_BY_FD:
4438 err = bpf_obj_get_info_by_fd(&attr, uattr);
4440 case BPF_RAW_TRACEPOINT_OPEN:
4441 err = bpf_raw_tracepoint_open(&attr);
4444 err = bpf_btf_load(&attr);
4446 case BPF_BTF_GET_FD_BY_ID:
4447 err = bpf_btf_get_fd_by_id(&attr);
4449 case BPF_TASK_FD_QUERY:
4450 err = bpf_task_fd_query(&attr, uattr);
4452 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4453 err = map_lookup_and_delete_elem(&attr);
4455 case BPF_MAP_LOOKUP_BATCH:
4456 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
4458 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4459 err = bpf_map_do_batch(&attr, uattr,
4460 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4462 case BPF_MAP_UPDATE_BATCH:
4463 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
4465 case BPF_MAP_DELETE_BATCH:
4466 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);
4468 case BPF_LINK_CREATE:
4469 err = link_create(&attr);
4471 case BPF_LINK_UPDATE:
4472 err = link_update(&attr);
4474 case BPF_LINK_GET_FD_BY_ID:
4475 err = bpf_link_get_fd_by_id(&attr);
4477 case BPF_LINK_GET_NEXT_ID:
4478 err = bpf_obj_get_next_id(&attr, uattr,
4479 &link_idr, &link_idr_lock);
4481 case BPF_ENABLE_STATS:
4482 err = bpf_enable_stats(&attr);
4484 case BPF_ITER_CREATE:
4485 err = bpf_iter_create(&attr);
4487 case BPF_LINK_DETACH:
4488 err = link_detach(&attr);
4490 case BPF_PROG_BIND_MAP:
4491 err = bpf_prog_bind_map(&attr);