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/kernel.h>
21 #include <linux/idr.h>
22 #include <linux/cred.h>
23 #include <linux/timekeeping.h>
24 #include <linux/ctype.h>
25 #include <linux/nospec.h>
26 #include <linux/audit.h>
27 #include <uapi/linux/btf.h>
28 #include <linux/pgtable.h>
29 #include <linux/bpf_lsm.h>
30 #include <linux/poll.h>
31 #include <linux/bpf-netns.h>
32 #include <linux/rcupdate_trace.h>
33 #include <linux/memcontrol.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(const 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 /* Please, do not use this function outside from the map creation path
271 * (e.g. in map update path) without taking care of setting the active
272 * memory cgroup (see at bpf_map_kmalloc_node() for example).
274 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
276 /* We really just want to fail instead of triggering OOM killer
277 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
278 * which is used for lower order allocation requests.
280 * It has been observed that higher order allocation requests done by
281 * vmalloc with __GFP_NORETRY being set might fail due to not trying
282 * to reclaim memory from the page cache, thus we set
283 * __GFP_RETRY_MAYFAIL to avoid such situations.
286 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT;
287 unsigned int flags = 0;
288 unsigned long align = 1;
291 if (size >= SIZE_MAX)
294 /* kmalloc()'ed memory can't be mmap()'ed */
296 BUG_ON(!PAGE_ALIGNED(size));
299 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
300 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
306 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
307 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
308 flags, numa_node, __builtin_return_address(0));
311 void *bpf_map_area_alloc(u64 size, int numa_node)
313 return __bpf_map_area_alloc(size, numa_node, false);
316 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
318 return __bpf_map_area_alloc(size, numa_node, true);
321 void bpf_map_area_free(void *area)
326 static u32 bpf_map_flags_retain_permanent(u32 flags)
328 /* Some map creation flags are not tied to the map object but
329 * rather to the map fd instead, so they have no meaning upon
330 * map object inspection since multiple file descriptors with
331 * different (access) properties can exist here. Thus, given
332 * this has zero meaning for the map itself, lets clear these
335 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
338 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
340 map->map_type = attr->map_type;
341 map->key_size = attr->key_size;
342 map->value_size = attr->value_size;
343 map->max_entries = attr->max_entries;
344 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
345 map->numa_node = bpf_map_attr_numa_node(attr);
348 static int bpf_map_alloc_id(struct bpf_map *map)
352 idr_preload(GFP_KERNEL);
353 spin_lock_bh(&map_idr_lock);
354 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
357 spin_unlock_bh(&map_idr_lock);
360 if (WARN_ON_ONCE(!id))
363 return id > 0 ? 0 : id;
366 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
370 /* Offloaded maps are removed from the IDR store when their device
371 * disappears - even if someone holds an fd to them they are unusable,
372 * the memory is gone, all ops will fail; they are simply waiting for
373 * refcnt to drop to be freed.
379 spin_lock_irqsave(&map_idr_lock, flags);
381 __acquire(&map_idr_lock);
383 idr_remove(&map_idr, map->id);
387 spin_unlock_irqrestore(&map_idr_lock, flags);
389 __release(&map_idr_lock);
392 #ifdef CONFIG_MEMCG_KMEM
393 static void bpf_map_save_memcg(struct bpf_map *map)
395 map->memcg = get_mem_cgroup_from_mm(current->mm);
398 static void bpf_map_release_memcg(struct bpf_map *map)
400 mem_cgroup_put(map->memcg);
403 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
406 struct mem_cgroup *old_memcg;
409 old_memcg = set_active_memcg(map->memcg);
410 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
411 set_active_memcg(old_memcg);
416 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
418 struct mem_cgroup *old_memcg;
421 old_memcg = set_active_memcg(map->memcg);
422 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
423 set_active_memcg(old_memcg);
428 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
429 size_t align, gfp_t flags)
431 struct mem_cgroup *old_memcg;
434 old_memcg = set_active_memcg(map->memcg);
435 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
436 set_active_memcg(old_memcg);
442 static void bpf_map_save_memcg(struct bpf_map *map)
446 static void bpf_map_release_memcg(struct bpf_map *map)
451 /* called from workqueue */
452 static void bpf_map_free_deferred(struct work_struct *work)
454 struct bpf_map *map = container_of(work, struct bpf_map, work);
456 security_bpf_map_free(map);
457 bpf_map_release_memcg(map);
458 /* implementation dependent freeing */
459 map->ops->map_free(map);
462 static void bpf_map_put_uref(struct bpf_map *map)
464 if (atomic64_dec_and_test(&map->usercnt)) {
465 if (map->ops->map_release_uref)
466 map->ops->map_release_uref(map);
470 /* decrement map refcnt and schedule it for freeing via workqueue
471 * (unrelying map implementation ops->map_free() might sleep)
473 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
475 if (atomic64_dec_and_test(&map->refcnt)) {
476 /* bpf_map_free_id() must be called first */
477 bpf_map_free_id(map, do_idr_lock);
479 INIT_WORK(&map->work, bpf_map_free_deferred);
480 schedule_work(&map->work);
484 void bpf_map_put(struct bpf_map *map)
486 __bpf_map_put(map, true);
488 EXPORT_SYMBOL_GPL(bpf_map_put);
490 void bpf_map_put_with_uref(struct bpf_map *map)
492 bpf_map_put_uref(map);
496 static int bpf_map_release(struct inode *inode, struct file *filp)
498 struct bpf_map *map = filp->private_data;
500 if (map->ops->map_release)
501 map->ops->map_release(map, filp);
503 bpf_map_put_with_uref(map);
507 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
509 fmode_t mode = f.file->f_mode;
511 /* Our file permissions may have been overridden by global
512 * map permissions facing syscall side.
514 if (READ_ONCE(map->frozen))
515 mode &= ~FMODE_CAN_WRITE;
519 #ifdef CONFIG_PROC_FS
520 /* Provides an approximation of the map's memory footprint.
521 * Used only to provide a backward compatibility and display
522 * a reasonable "memlock" info.
524 static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
528 size = round_up(map->key_size + bpf_map_value_size(map), 8);
530 return round_up(map->max_entries * size, PAGE_SIZE);
533 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
535 const struct bpf_map *map = filp->private_data;
536 const struct bpf_array *array;
537 u32 type = 0, jited = 0;
539 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
540 array = container_of(map, struct bpf_array, map);
541 type = array->aux->type;
542 jited = array->aux->jited;
559 bpf_map_memory_footprint(map),
561 READ_ONCE(map->frozen));
563 seq_printf(m, "owner_prog_type:\t%u\n", type);
564 seq_printf(m, "owner_jited:\t%u\n", jited);
569 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
572 /* We need this handler such that alloc_file() enables
573 * f_mode with FMODE_CAN_READ.
578 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
579 size_t siz, loff_t *ppos)
581 /* We need this handler such that alloc_file() enables
582 * f_mode with FMODE_CAN_WRITE.
587 /* called for any extra memory-mapped regions (except initial) */
588 static void bpf_map_mmap_open(struct vm_area_struct *vma)
590 struct bpf_map *map = vma->vm_file->private_data;
592 if (vma->vm_flags & VM_MAYWRITE) {
593 mutex_lock(&map->freeze_mutex);
595 mutex_unlock(&map->freeze_mutex);
599 /* called for all unmapped memory region (including initial) */
600 static void bpf_map_mmap_close(struct vm_area_struct *vma)
602 struct bpf_map *map = vma->vm_file->private_data;
604 if (vma->vm_flags & VM_MAYWRITE) {
605 mutex_lock(&map->freeze_mutex);
607 mutex_unlock(&map->freeze_mutex);
611 static const struct vm_operations_struct bpf_map_default_vmops = {
612 .open = bpf_map_mmap_open,
613 .close = bpf_map_mmap_close,
616 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
618 struct bpf_map *map = filp->private_data;
621 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
624 if (!(vma->vm_flags & VM_SHARED))
627 mutex_lock(&map->freeze_mutex);
629 if (vma->vm_flags & VM_WRITE) {
634 /* map is meant to be read-only, so do not allow mapping as
635 * writable, because it's possible to leak a writable page
636 * reference and allows user-space to still modify it after
637 * freezing, while verifier will assume contents do not change
639 if (map->map_flags & BPF_F_RDONLY_PROG) {
645 /* set default open/close callbacks */
646 vma->vm_ops = &bpf_map_default_vmops;
647 vma->vm_private_data = map;
648 vma->vm_flags &= ~VM_MAYEXEC;
649 if (!(vma->vm_flags & VM_WRITE))
650 /* disallow re-mapping with PROT_WRITE */
651 vma->vm_flags &= ~VM_MAYWRITE;
653 err = map->ops->map_mmap(map, vma);
657 if (vma->vm_flags & VM_MAYWRITE)
660 mutex_unlock(&map->freeze_mutex);
664 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
666 struct bpf_map *map = filp->private_data;
668 if (map->ops->map_poll)
669 return map->ops->map_poll(map, filp, pts);
674 const struct file_operations bpf_map_fops = {
675 #ifdef CONFIG_PROC_FS
676 .show_fdinfo = bpf_map_show_fdinfo,
678 .release = bpf_map_release,
679 .read = bpf_dummy_read,
680 .write = bpf_dummy_write,
681 .mmap = bpf_map_mmap,
682 .poll = bpf_map_poll,
685 int bpf_map_new_fd(struct bpf_map *map, int flags)
689 ret = security_bpf_map(map, OPEN_FMODE(flags));
693 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
697 int bpf_get_file_flag(int flags)
699 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
701 if (flags & BPF_F_RDONLY)
703 if (flags & BPF_F_WRONLY)
708 /* helper macro to check that unused fields 'union bpf_attr' are zero */
709 #define CHECK_ATTR(CMD) \
710 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
711 sizeof(attr->CMD##_LAST_FIELD), 0, \
713 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
714 sizeof(attr->CMD##_LAST_FIELD)) != NULL
716 /* dst and src must have at least "size" number of bytes.
717 * Return strlen on success and < 0 on error.
719 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
721 const char *end = src + size;
722 const char *orig_src = src;
724 memset(dst, 0, size);
725 /* Copy all isalnum(), '_' and '.' chars. */
726 while (src < end && *src) {
727 if (!isalnum(*src) &&
728 *src != '_' && *src != '.')
733 /* No '\0' found in "size" number of bytes */
737 return src - orig_src;
740 int map_check_no_btf(const struct bpf_map *map,
741 const struct btf *btf,
742 const struct btf_type *key_type,
743 const struct btf_type *value_type)
748 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
749 u32 btf_key_id, u32 btf_value_id)
751 const struct btf_type *key_type, *value_type;
752 u32 key_size, value_size;
755 /* Some maps allow key to be unspecified. */
757 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
758 if (!key_type || key_size != map->key_size)
761 key_type = btf_type_by_id(btf, 0);
762 if (!map->ops->map_check_btf)
766 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
767 if (!value_type || value_size != map->value_size)
770 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
772 if (map_value_has_spin_lock(map)) {
773 if (map->map_flags & BPF_F_RDONLY_PROG)
775 if (map->map_type != BPF_MAP_TYPE_HASH &&
776 map->map_type != BPF_MAP_TYPE_ARRAY &&
777 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
778 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
779 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
780 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
782 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
785 "verifier bug spin_lock_off %d value_size %d\n",
786 map->spin_lock_off, map->value_size);
791 if (map->ops->map_check_btf)
792 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
797 #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
798 /* called via syscall */
799 static int map_create(union bpf_attr *attr)
801 int numa_node = bpf_map_attr_numa_node(attr);
806 err = CHECK_ATTR(BPF_MAP_CREATE);
810 if (attr->btf_vmlinux_value_type_id) {
811 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
812 attr->btf_key_type_id || attr->btf_value_type_id)
814 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
818 f_flags = bpf_get_file_flag(attr->map_flags);
822 if (numa_node != NUMA_NO_NODE &&
823 ((unsigned int)numa_node >= nr_node_ids ||
824 !node_online(numa_node)))
827 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
828 map = find_and_alloc_map(attr);
832 err = bpf_obj_name_cpy(map->name, attr->map_name,
833 sizeof(attr->map_name));
837 atomic64_set(&map->refcnt, 1);
838 atomic64_set(&map->usercnt, 1);
839 mutex_init(&map->freeze_mutex);
841 map->spin_lock_off = -EINVAL;
842 if (attr->btf_key_type_id || attr->btf_value_type_id ||
843 /* Even the map's value is a kernel's struct,
844 * the bpf_prog.o must have BTF to begin with
845 * to figure out the corresponding kernel's
846 * counter part. Thus, attr->btf_fd has
849 attr->btf_vmlinux_value_type_id) {
852 btf = btf_get_by_fd(attr->btf_fd);
857 if (btf_is_kernel(btf)) {
864 if (attr->btf_value_type_id) {
865 err = map_check_btf(map, btf, attr->btf_key_type_id,
866 attr->btf_value_type_id);
871 map->btf_key_type_id = attr->btf_key_type_id;
872 map->btf_value_type_id = attr->btf_value_type_id;
873 map->btf_vmlinux_value_type_id =
874 attr->btf_vmlinux_value_type_id;
877 err = security_bpf_map_alloc(map);
881 err = bpf_map_alloc_id(map);
885 bpf_map_save_memcg(map);
887 err = bpf_map_new_fd(map, f_flags);
889 /* failed to allocate fd.
890 * bpf_map_put_with_uref() is needed because the above
891 * bpf_map_alloc_id() has published the map
892 * to the userspace and the userspace may
893 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
895 bpf_map_put_with_uref(map);
902 security_bpf_map_free(map);
905 map->ops->map_free(map);
909 /* if error is returned, fd is released.
910 * On success caller should complete fd access with matching fdput()
912 struct bpf_map *__bpf_map_get(struct fd f)
915 return ERR_PTR(-EBADF);
916 if (f.file->f_op != &bpf_map_fops) {
918 return ERR_PTR(-EINVAL);
921 return f.file->private_data;
924 void bpf_map_inc(struct bpf_map *map)
926 atomic64_inc(&map->refcnt);
928 EXPORT_SYMBOL_GPL(bpf_map_inc);
930 void bpf_map_inc_with_uref(struct bpf_map *map)
932 atomic64_inc(&map->refcnt);
933 atomic64_inc(&map->usercnt);
935 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
937 struct bpf_map *bpf_map_get(u32 ufd)
939 struct fd f = fdget(ufd);
942 map = __bpf_map_get(f);
952 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
954 struct fd f = fdget(ufd);
957 map = __bpf_map_get(f);
961 bpf_map_inc_with_uref(map);
967 /* map_idr_lock should have been held */
968 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
972 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
974 return ERR_PTR(-ENOENT);
976 atomic64_inc(&map->usercnt);
981 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
983 spin_lock_bh(&map_idr_lock);
984 map = __bpf_map_inc_not_zero(map, false);
985 spin_unlock_bh(&map_idr_lock);
989 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
991 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
996 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
999 return memdup_user(ukey, key_size);
1002 return ERR_PTR(-EINVAL);
1007 /* last field in 'union bpf_attr' used by this command */
1008 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1010 static int map_lookup_elem(union bpf_attr *attr)
1012 void __user *ukey = u64_to_user_ptr(attr->key);
1013 void __user *uvalue = u64_to_user_ptr(attr->value);
1014 int ufd = attr->map_fd;
1015 struct bpf_map *map;
1021 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1024 if (attr->flags & ~BPF_F_LOCK)
1028 map = __bpf_map_get(f);
1030 return PTR_ERR(map);
1031 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1036 if ((attr->flags & BPF_F_LOCK) &&
1037 !map_value_has_spin_lock(map)) {
1042 key = __bpf_copy_key(ukey, map->key_size);
1048 value_size = bpf_map_value_size(map);
1051 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1055 err = bpf_map_copy_value(map, key, value, attr->flags);
1060 if (copy_to_user(uvalue, value, value_size) != 0)
1075 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1077 static int map_update_elem(union bpf_attr *attr)
1079 void __user *ukey = u64_to_user_ptr(attr->key);
1080 void __user *uvalue = u64_to_user_ptr(attr->value);
1081 int ufd = attr->map_fd;
1082 struct bpf_map *map;
1088 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1092 map = __bpf_map_get(f);
1094 return PTR_ERR(map);
1095 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1100 if ((attr->flags & BPF_F_LOCK) &&
1101 !map_value_has_spin_lock(map)) {
1106 key = __bpf_copy_key(ukey, map->key_size);
1112 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1113 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
1114 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1115 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
1116 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1118 value_size = map->value_size;
1121 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1126 if (copy_from_user(value, uvalue, value_size) != 0)
1129 err = bpf_map_update_value(map, f, key, value, attr->flags);
1140 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1142 static int map_delete_elem(union bpf_attr *attr)
1144 void __user *ukey = u64_to_user_ptr(attr->key);
1145 int ufd = attr->map_fd;
1146 struct bpf_map *map;
1151 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1155 map = __bpf_map_get(f);
1157 return PTR_ERR(map);
1158 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1163 key = __bpf_copy_key(ukey, map->key_size);
1169 if (bpf_map_is_dev_bound(map)) {
1170 err = bpf_map_offload_delete_elem(map, key);
1172 } else if (IS_FD_PROG_ARRAY(map) ||
1173 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1174 /* These maps require sleepable context */
1175 err = map->ops->map_delete_elem(map, key);
1179 bpf_disable_instrumentation();
1181 err = map->ops->map_delete_elem(map, key);
1183 bpf_enable_instrumentation();
1184 maybe_wait_bpf_programs(map);
1192 /* last field in 'union bpf_attr' used by this command */
1193 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1195 static int map_get_next_key(union bpf_attr *attr)
1197 void __user *ukey = u64_to_user_ptr(attr->key);
1198 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1199 int ufd = attr->map_fd;
1200 struct bpf_map *map;
1201 void *key, *next_key;
1205 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1209 map = __bpf_map_get(f);
1211 return PTR_ERR(map);
1212 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1218 key = __bpf_copy_key(ukey, map->key_size);
1228 next_key = kmalloc(map->key_size, GFP_USER);
1232 if (bpf_map_is_dev_bound(map)) {
1233 err = bpf_map_offload_get_next_key(map, key, next_key);
1238 err = map->ops->map_get_next_key(map, key, next_key);
1245 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1259 int generic_map_delete_batch(struct bpf_map *map,
1260 const union bpf_attr *attr,
1261 union bpf_attr __user *uattr)
1263 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1268 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1271 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1272 !map_value_has_spin_lock(map)) {
1276 max_count = attr->batch.count;
1280 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1284 for (cp = 0; cp < max_count; cp++) {
1286 if (copy_from_user(key, keys + cp * map->key_size,
1290 if (bpf_map_is_dev_bound(map)) {
1291 err = bpf_map_offload_delete_elem(map, key);
1295 bpf_disable_instrumentation();
1297 err = map->ops->map_delete_elem(map, key);
1299 bpf_enable_instrumentation();
1300 maybe_wait_bpf_programs(map);
1304 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1311 int generic_map_update_batch(struct bpf_map *map,
1312 const union bpf_attr *attr,
1313 union bpf_attr __user *uattr)
1315 void __user *values = u64_to_user_ptr(attr->batch.values);
1316 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1317 u32 value_size, cp, max_count;
1318 int ufd = attr->map_fd;
1324 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1327 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1328 !map_value_has_spin_lock(map)) {
1332 value_size = bpf_map_value_size(map);
1334 max_count = attr->batch.count;
1338 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1342 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1348 for (cp = 0; cp < max_count; cp++) {
1350 if (copy_from_user(key, keys + cp * map->key_size,
1352 copy_from_user(value, values + cp * value_size, value_size))
1355 err = bpf_map_update_value(map, f, key, value,
1356 attr->batch.elem_flags);
1362 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1370 #define MAP_LOOKUP_RETRIES 3
1372 int generic_map_lookup_batch(struct bpf_map *map,
1373 const union bpf_attr *attr,
1374 union bpf_attr __user *uattr)
1376 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1377 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1378 void __user *values = u64_to_user_ptr(attr->batch.values);
1379 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1380 void *buf, *buf_prevkey, *prev_key, *key, *value;
1381 int err, retry = MAP_LOOKUP_RETRIES;
1382 u32 value_size, cp, max_count;
1384 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1387 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1388 !map_value_has_spin_lock(map))
1391 value_size = bpf_map_value_size(map);
1393 max_count = attr->batch.count;
1397 if (put_user(0, &uattr->batch.count))
1400 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1404 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1412 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1415 value = key + map->key_size;
1417 prev_key = buf_prevkey;
1419 for (cp = 0; cp < max_count;) {
1421 err = map->ops->map_get_next_key(map, prev_key, key);
1425 err = bpf_map_copy_value(map, key, value,
1426 attr->batch.elem_flags);
1428 if (err == -ENOENT) {
1440 if (copy_to_user(keys + cp * map->key_size, key,
1445 if (copy_to_user(values + cp * value_size, value, value_size)) {
1451 prev_key = buf_prevkey;
1453 swap(prev_key, key);
1454 retry = MAP_LOOKUP_RETRIES;
1461 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1462 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1471 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1473 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1475 void __user *ukey = u64_to_user_ptr(attr->key);
1476 void __user *uvalue = u64_to_user_ptr(attr->value);
1477 int ufd = attr->map_fd;
1478 struct bpf_map *map;
1484 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1488 map = __bpf_map_get(f);
1490 return PTR_ERR(map);
1491 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1492 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1497 key = __bpf_copy_key(ukey, map->key_size);
1503 value_size = map->value_size;
1506 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1510 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1511 map->map_type == BPF_MAP_TYPE_STACK) {
1512 err = map->ops->map_pop_elem(map, value);
1520 if (copy_to_user(uvalue, value, value_size) != 0) {
1536 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1538 static int map_freeze(const union bpf_attr *attr)
1540 int err = 0, ufd = attr->map_fd;
1541 struct bpf_map *map;
1544 if (CHECK_ATTR(BPF_MAP_FREEZE))
1548 map = __bpf_map_get(f);
1550 return PTR_ERR(map);
1552 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1557 mutex_lock(&map->freeze_mutex);
1559 if (map->writecnt) {
1563 if (READ_ONCE(map->frozen)) {
1567 if (!bpf_capable()) {
1572 WRITE_ONCE(map->frozen, true);
1574 mutex_unlock(&map->freeze_mutex);
1579 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1580 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1581 [_id] = & _name ## _prog_ops,
1582 #define BPF_MAP_TYPE(_id, _ops)
1583 #define BPF_LINK_TYPE(_id, _name)
1584 #include <linux/bpf_types.h>
1585 #undef BPF_PROG_TYPE
1587 #undef BPF_LINK_TYPE
1590 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1592 const struct bpf_prog_ops *ops;
1594 if (type >= ARRAY_SIZE(bpf_prog_types))
1596 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1597 ops = bpf_prog_types[type];
1601 if (!bpf_prog_is_dev_bound(prog->aux))
1602 prog->aux->ops = ops;
1604 prog->aux->ops = &bpf_offload_prog_ops;
1615 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1616 [BPF_AUDIT_LOAD] = "LOAD",
1617 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1620 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1622 struct audit_context *ctx = NULL;
1623 struct audit_buffer *ab;
1625 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1627 if (audit_enabled == AUDIT_OFF)
1629 if (op == BPF_AUDIT_LOAD)
1630 ctx = audit_context();
1631 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1634 audit_log_format(ab, "prog-id=%u op=%s",
1635 prog->aux->id, bpf_audit_str[op]);
1639 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1643 idr_preload(GFP_KERNEL);
1644 spin_lock_bh(&prog_idr_lock);
1645 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1648 spin_unlock_bh(&prog_idr_lock);
1651 /* id is in [1, INT_MAX) */
1652 if (WARN_ON_ONCE(!id))
1655 return id > 0 ? 0 : id;
1658 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1660 /* cBPF to eBPF migrations are currently not in the idr store.
1661 * Offloaded programs are removed from the store when their device
1662 * disappears - even if someone grabs an fd to them they are unusable,
1663 * simply waiting for refcnt to drop to be freed.
1669 spin_lock_bh(&prog_idr_lock);
1671 __acquire(&prog_idr_lock);
1673 idr_remove(&prog_idr, prog->aux->id);
1677 spin_unlock_bh(&prog_idr_lock);
1679 __release(&prog_idr_lock);
1682 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1684 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1686 kvfree(aux->func_info);
1687 kfree(aux->func_info_aux);
1688 free_uid(aux->user);
1689 security_bpf_prog_free(aux);
1690 bpf_prog_free(aux->prog);
1693 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1695 bpf_prog_kallsyms_del_all(prog);
1696 btf_put(prog->aux->btf);
1697 kvfree(prog->aux->jited_linfo);
1698 kvfree(prog->aux->linfo);
1699 kfree(prog->aux->kfunc_tab);
1700 if (prog->aux->attach_btf)
1701 btf_put(prog->aux->attach_btf);
1704 if (prog->aux->sleepable)
1705 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1707 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1709 __bpf_prog_put_rcu(&prog->aux->rcu);
1713 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1715 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
1716 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1717 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1718 /* bpf_prog_free_id() must be called first */
1719 bpf_prog_free_id(prog, do_idr_lock);
1720 __bpf_prog_put_noref(prog, true);
1724 void bpf_prog_put(struct bpf_prog *prog)
1726 __bpf_prog_put(prog, true);
1728 EXPORT_SYMBOL_GPL(bpf_prog_put);
1730 static int bpf_prog_release(struct inode *inode, struct file *filp)
1732 struct bpf_prog *prog = filp->private_data;
1738 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1739 struct bpf_prog_stats *stats)
1741 u64 nsecs = 0, cnt = 0, misses = 0;
1744 for_each_possible_cpu(cpu) {
1745 const struct bpf_prog_stats *st;
1747 u64 tnsecs, tcnt, tmisses;
1749 st = per_cpu_ptr(prog->stats, cpu);
1751 start = u64_stats_fetch_begin_irq(&st->syncp);
1754 tmisses = st->misses;
1755 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1760 stats->nsecs = nsecs;
1762 stats->misses = misses;
1765 #ifdef CONFIG_PROC_FS
1766 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1768 const struct bpf_prog *prog = filp->private_data;
1769 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1770 struct bpf_prog_stats stats;
1772 bpf_prog_get_stats(prog, &stats);
1773 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1780 "run_time_ns:\t%llu\n"
1782 "recursion_misses:\t%llu\n",
1786 prog->pages * 1ULL << PAGE_SHIFT,
1794 const struct file_operations bpf_prog_fops = {
1795 #ifdef CONFIG_PROC_FS
1796 .show_fdinfo = bpf_prog_show_fdinfo,
1798 .release = bpf_prog_release,
1799 .read = bpf_dummy_read,
1800 .write = bpf_dummy_write,
1803 int bpf_prog_new_fd(struct bpf_prog *prog)
1807 ret = security_bpf_prog(prog);
1811 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1812 O_RDWR | O_CLOEXEC);
1815 static struct bpf_prog *____bpf_prog_get(struct fd f)
1818 return ERR_PTR(-EBADF);
1819 if (f.file->f_op != &bpf_prog_fops) {
1821 return ERR_PTR(-EINVAL);
1824 return f.file->private_data;
1827 void bpf_prog_add(struct bpf_prog *prog, int i)
1829 atomic64_add(i, &prog->aux->refcnt);
1831 EXPORT_SYMBOL_GPL(bpf_prog_add);
1833 void bpf_prog_sub(struct bpf_prog *prog, int i)
1835 /* Only to be used for undoing previous bpf_prog_add() in some
1836 * error path. We still know that another entity in our call
1837 * path holds a reference to the program, thus atomic_sub() can
1838 * be safely used in such cases!
1840 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1842 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1844 void bpf_prog_inc(struct bpf_prog *prog)
1846 atomic64_inc(&prog->aux->refcnt);
1848 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1850 /* prog_idr_lock should have been held */
1851 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1855 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1858 return ERR_PTR(-ENOENT);
1862 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1864 bool bpf_prog_get_ok(struct bpf_prog *prog,
1865 enum bpf_prog_type *attach_type, bool attach_drv)
1867 /* not an attachment, just a refcount inc, always allow */
1871 if (prog->type != *attach_type)
1873 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1879 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1882 struct fd f = fdget(ufd);
1883 struct bpf_prog *prog;
1885 prog = ____bpf_prog_get(f);
1888 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1889 prog = ERR_PTR(-EINVAL);
1899 struct bpf_prog *bpf_prog_get(u32 ufd)
1901 return __bpf_prog_get(ufd, NULL, false);
1904 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1907 return __bpf_prog_get(ufd, &type, attach_drv);
1909 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1911 /* Initially all BPF programs could be loaded w/o specifying
1912 * expected_attach_type. Later for some of them specifying expected_attach_type
1913 * at load time became required so that program could be validated properly.
1914 * Programs of types that are allowed to be loaded both w/ and w/o (for
1915 * backward compatibility) expected_attach_type, should have the default attach
1916 * type assigned to expected_attach_type for the latter case, so that it can be
1917 * validated later at attach time.
1919 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1920 * prog type requires it but has some attach types that have to be backward
1923 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1925 switch (attr->prog_type) {
1926 case BPF_PROG_TYPE_CGROUP_SOCK:
1927 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1928 * exist so checking for non-zero is the way to go here.
1930 if (!attr->expected_attach_type)
1931 attr->expected_attach_type =
1932 BPF_CGROUP_INET_SOCK_CREATE;
1938 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1939 enum bpf_attach_type expected_attach_type,
1940 struct btf *attach_btf, u32 btf_id,
1941 struct bpf_prog *dst_prog)
1944 if (btf_id > BTF_MAX_TYPE)
1947 if (!attach_btf && !dst_prog)
1950 switch (prog_type) {
1951 case BPF_PROG_TYPE_TRACING:
1952 case BPF_PROG_TYPE_LSM:
1953 case BPF_PROG_TYPE_STRUCT_OPS:
1954 case BPF_PROG_TYPE_EXT:
1961 if (attach_btf && (!btf_id || dst_prog))
1964 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
1965 prog_type != BPF_PROG_TYPE_EXT)
1968 switch (prog_type) {
1969 case BPF_PROG_TYPE_CGROUP_SOCK:
1970 switch (expected_attach_type) {
1971 case BPF_CGROUP_INET_SOCK_CREATE:
1972 case BPF_CGROUP_INET_SOCK_RELEASE:
1973 case BPF_CGROUP_INET4_POST_BIND:
1974 case BPF_CGROUP_INET6_POST_BIND:
1979 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1980 switch (expected_attach_type) {
1981 case BPF_CGROUP_INET4_BIND:
1982 case BPF_CGROUP_INET6_BIND:
1983 case BPF_CGROUP_INET4_CONNECT:
1984 case BPF_CGROUP_INET6_CONNECT:
1985 case BPF_CGROUP_INET4_GETPEERNAME:
1986 case BPF_CGROUP_INET6_GETPEERNAME:
1987 case BPF_CGROUP_INET4_GETSOCKNAME:
1988 case BPF_CGROUP_INET6_GETSOCKNAME:
1989 case BPF_CGROUP_UDP4_SENDMSG:
1990 case BPF_CGROUP_UDP6_SENDMSG:
1991 case BPF_CGROUP_UDP4_RECVMSG:
1992 case BPF_CGROUP_UDP6_RECVMSG:
1997 case BPF_PROG_TYPE_CGROUP_SKB:
1998 switch (expected_attach_type) {
1999 case BPF_CGROUP_INET_INGRESS:
2000 case BPF_CGROUP_INET_EGRESS:
2005 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2006 switch (expected_attach_type) {
2007 case BPF_CGROUP_SETSOCKOPT:
2008 case BPF_CGROUP_GETSOCKOPT:
2013 case BPF_PROG_TYPE_SK_LOOKUP:
2014 if (expected_attach_type == BPF_SK_LOOKUP)
2017 case BPF_PROG_TYPE_EXT:
2018 if (expected_attach_type)
2026 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2028 switch (prog_type) {
2029 case BPF_PROG_TYPE_SCHED_CLS:
2030 case BPF_PROG_TYPE_SCHED_ACT:
2031 case BPF_PROG_TYPE_XDP:
2032 case BPF_PROG_TYPE_LWT_IN:
2033 case BPF_PROG_TYPE_LWT_OUT:
2034 case BPF_PROG_TYPE_LWT_XMIT:
2035 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2036 case BPF_PROG_TYPE_SK_SKB:
2037 case BPF_PROG_TYPE_SK_MSG:
2038 case BPF_PROG_TYPE_LIRC_MODE2:
2039 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2040 case BPF_PROG_TYPE_CGROUP_DEVICE:
2041 case BPF_PROG_TYPE_CGROUP_SOCK:
2042 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2043 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2044 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2045 case BPF_PROG_TYPE_SOCK_OPS:
2046 case BPF_PROG_TYPE_EXT: /* extends any prog */
2048 case BPF_PROG_TYPE_CGROUP_SKB:
2050 case BPF_PROG_TYPE_SK_REUSEPORT:
2051 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2057 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2059 switch (prog_type) {
2060 case BPF_PROG_TYPE_KPROBE:
2061 case BPF_PROG_TYPE_TRACEPOINT:
2062 case BPF_PROG_TYPE_PERF_EVENT:
2063 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2064 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2065 case BPF_PROG_TYPE_TRACING:
2066 case BPF_PROG_TYPE_LSM:
2067 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2068 case BPF_PROG_TYPE_EXT: /* extends any prog */
2075 /* last field in 'union bpf_attr' used by this command */
2076 #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
2078 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
2080 enum bpf_prog_type type = attr->prog_type;
2081 struct bpf_prog *prog, *dst_prog = NULL;
2082 struct btf *attach_btf = NULL;
2087 if (CHECK_ATTR(BPF_PROG_LOAD))
2090 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2091 BPF_F_ANY_ALIGNMENT |
2092 BPF_F_TEST_STATE_FREQ |
2094 BPF_F_TEST_RND_HI32))
2097 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2098 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2102 /* copy eBPF program license from user space */
2103 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
2104 sizeof(license) - 1) < 0)
2106 license[sizeof(license) - 1] = 0;
2108 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2109 is_gpl = license_is_gpl_compatible(license);
2111 if (attr->insn_cnt == 0 ||
2112 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2114 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2115 type != BPF_PROG_TYPE_CGROUP_SKB &&
2119 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2121 if (is_perfmon_prog_type(type) && !perfmon_capable())
2124 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2125 * or btf, we need to check which one it is
2127 if (attr->attach_prog_fd) {
2128 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2129 if (IS_ERR(dst_prog)) {
2131 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2132 if (IS_ERR(attach_btf))
2134 if (!btf_is_kernel(attach_btf)) {
2135 /* attaching through specifying bpf_prog's BTF
2136 * objects directly might be supported eventually
2138 btf_put(attach_btf);
2142 } else if (attr->attach_btf_id) {
2143 /* fall back to vmlinux BTF, if BTF type ID is specified */
2144 attach_btf = bpf_get_btf_vmlinux();
2145 if (IS_ERR(attach_btf))
2146 return PTR_ERR(attach_btf);
2149 btf_get(attach_btf);
2152 bpf_prog_load_fixup_attach_type(attr);
2153 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2154 attach_btf, attr->attach_btf_id,
2157 bpf_prog_put(dst_prog);
2159 btf_put(attach_btf);
2163 /* plain bpf_prog allocation */
2164 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2167 bpf_prog_put(dst_prog);
2169 btf_put(attach_btf);
2173 prog->expected_attach_type = attr->expected_attach_type;
2174 prog->aux->attach_btf = attach_btf;
2175 prog->aux->attach_btf_id = attr->attach_btf_id;
2176 prog->aux->dst_prog = dst_prog;
2177 prog->aux->offload_requested = !!attr->prog_ifindex;
2178 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2180 err = security_bpf_prog_alloc(prog->aux);
2184 prog->aux->user = get_current_user();
2185 prog->len = attr->insn_cnt;
2188 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
2189 bpf_prog_insn_size(prog)) != 0)
2192 prog->orig_prog = NULL;
2195 atomic64_set(&prog->aux->refcnt, 1);
2196 prog->gpl_compatible = is_gpl ? 1 : 0;
2198 if (bpf_prog_is_dev_bound(prog->aux)) {
2199 err = bpf_prog_offload_init(prog, attr);
2204 /* find program type: socket_filter vs tracing_filter */
2205 err = find_prog_type(type, prog);
2209 prog->aux->load_time = ktime_get_boottime_ns();
2210 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2211 sizeof(attr->prog_name));
2215 /* run eBPF verifier */
2216 err = bpf_check(&prog, attr, uattr);
2218 goto free_used_maps;
2220 prog = bpf_prog_select_runtime(prog, &err);
2222 goto free_used_maps;
2224 err = bpf_prog_alloc_id(prog);
2226 goto free_used_maps;
2228 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2229 * effectively publicly exposed. However, retrieving via
2230 * bpf_prog_get_fd_by_id() will take another reference,
2231 * therefore it cannot be gone underneath us.
2233 * Only for the time /after/ successful bpf_prog_new_fd()
2234 * and before returning to userspace, we might just hold
2235 * one reference and any parallel close on that fd could
2236 * rip everything out. Hence, below notifications must
2237 * happen before bpf_prog_new_fd().
2239 * Also, any failure handling from this point onwards must
2240 * be using bpf_prog_put() given the program is exposed.
2242 bpf_prog_kallsyms_add(prog);
2243 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2244 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2246 err = bpf_prog_new_fd(prog);
2252 /* In case we have subprogs, we need to wait for a grace
2253 * period before we can tear down JIT memory since symbols
2254 * are already exposed under kallsyms.
2256 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2259 free_uid(prog->aux->user);
2260 security_bpf_prog_free(prog->aux);
2262 if (prog->aux->attach_btf)
2263 btf_put(prog->aux->attach_btf);
2264 bpf_prog_free(prog);
2268 #define BPF_OBJ_LAST_FIELD file_flags
2270 static int bpf_obj_pin(const union bpf_attr *attr)
2272 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2275 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2278 static int bpf_obj_get(const union bpf_attr *attr)
2280 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2281 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2284 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2288 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2289 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2291 atomic64_set(&link->refcnt, 1);
2298 static void bpf_link_free_id(int id)
2303 spin_lock_bh(&link_idr_lock);
2304 idr_remove(&link_idr, id);
2305 spin_unlock_bh(&link_idr_lock);
2308 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2309 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2310 * anon_inode's release() call. This helper marksbpf_link as
2311 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2312 * is not decremented, it's the responsibility of a calling code that failed
2313 * to complete bpf_link initialization.
2315 void bpf_link_cleanup(struct bpf_link_primer *primer)
2317 primer->link->prog = NULL;
2318 bpf_link_free_id(primer->id);
2320 put_unused_fd(primer->fd);
2323 void bpf_link_inc(struct bpf_link *link)
2325 atomic64_inc(&link->refcnt);
2328 /* bpf_link_free is guaranteed to be called from process context */
2329 static void bpf_link_free(struct bpf_link *link)
2331 bpf_link_free_id(link->id);
2333 /* detach BPF program, clean up used resources */
2334 link->ops->release(link);
2335 bpf_prog_put(link->prog);
2337 /* free bpf_link and its containing memory */
2338 link->ops->dealloc(link);
2341 static void bpf_link_put_deferred(struct work_struct *work)
2343 struct bpf_link *link = container_of(work, struct bpf_link, work);
2345 bpf_link_free(link);
2348 /* bpf_link_put can be called from atomic context, but ensures that resources
2349 * are freed from process context
2351 void bpf_link_put(struct bpf_link *link)
2353 if (!atomic64_dec_and_test(&link->refcnt))
2357 INIT_WORK(&link->work, bpf_link_put_deferred);
2358 schedule_work(&link->work);
2360 bpf_link_free(link);
2364 static int bpf_link_release(struct inode *inode, struct file *filp)
2366 struct bpf_link *link = filp->private_data;
2372 #ifdef CONFIG_PROC_FS
2373 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2374 #define BPF_MAP_TYPE(_id, _ops)
2375 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2376 static const char *bpf_link_type_strs[] = {
2377 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2378 #include <linux/bpf_types.h>
2380 #undef BPF_PROG_TYPE
2382 #undef BPF_LINK_TYPE
2384 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2386 const struct bpf_link *link = filp->private_data;
2387 const struct bpf_prog *prog = link->prog;
2388 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2390 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2396 bpf_link_type_strs[link->type],
2400 if (link->ops->show_fdinfo)
2401 link->ops->show_fdinfo(link, m);
2405 static const struct file_operations bpf_link_fops = {
2406 #ifdef CONFIG_PROC_FS
2407 .show_fdinfo = bpf_link_show_fdinfo,
2409 .release = bpf_link_release,
2410 .read = bpf_dummy_read,
2411 .write = bpf_dummy_write,
2414 static int bpf_link_alloc_id(struct bpf_link *link)
2418 idr_preload(GFP_KERNEL);
2419 spin_lock_bh(&link_idr_lock);
2420 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2421 spin_unlock_bh(&link_idr_lock);
2427 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2428 * reserving unused FD and allocating ID from link_idr. This is to be paired
2429 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2430 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2431 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2432 * transient state is passed around in struct bpf_link_primer.
2433 * This is preferred way to create and initialize bpf_link, especially when
2434 * there are complicated and expensive operations inbetween creating bpf_link
2435 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2436 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2437 * expensive (and potentially failing) roll back operations in a rare case
2438 * that file, FD, or ID can't be allocated.
2440 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2445 fd = get_unused_fd_flags(O_CLOEXEC);
2450 id = bpf_link_alloc_id(link);
2456 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2458 bpf_link_free_id(id);
2460 return PTR_ERR(file);
2463 primer->link = link;
2464 primer->file = file;
2470 int bpf_link_settle(struct bpf_link_primer *primer)
2472 /* make bpf_link fetchable by ID */
2473 spin_lock_bh(&link_idr_lock);
2474 primer->link->id = primer->id;
2475 spin_unlock_bh(&link_idr_lock);
2476 /* make bpf_link fetchable by FD */
2477 fd_install(primer->fd, primer->file);
2478 /* pass through installed FD */
2482 int bpf_link_new_fd(struct bpf_link *link)
2484 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2487 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2489 struct fd f = fdget(ufd);
2490 struct bpf_link *link;
2493 return ERR_PTR(-EBADF);
2494 if (f.file->f_op != &bpf_link_fops) {
2496 return ERR_PTR(-EINVAL);
2499 link = f.file->private_data;
2506 struct bpf_tracing_link {
2507 struct bpf_link link;
2508 enum bpf_attach_type attach_type;
2509 struct bpf_trampoline *trampoline;
2510 struct bpf_prog *tgt_prog;
2513 static void bpf_tracing_link_release(struct bpf_link *link)
2515 struct bpf_tracing_link *tr_link =
2516 container_of(link, struct bpf_tracing_link, link);
2518 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2519 tr_link->trampoline));
2521 bpf_trampoline_put(tr_link->trampoline);
2523 /* tgt_prog is NULL if target is a kernel function */
2524 if (tr_link->tgt_prog)
2525 bpf_prog_put(tr_link->tgt_prog);
2528 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2530 struct bpf_tracing_link *tr_link =
2531 container_of(link, struct bpf_tracing_link, link);
2536 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2537 struct seq_file *seq)
2539 struct bpf_tracing_link *tr_link =
2540 container_of(link, struct bpf_tracing_link, link);
2543 "attach_type:\t%d\n",
2544 tr_link->attach_type);
2547 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2548 struct bpf_link_info *info)
2550 struct bpf_tracing_link *tr_link =
2551 container_of(link, struct bpf_tracing_link, link);
2553 info->tracing.attach_type = tr_link->attach_type;
2558 static const struct bpf_link_ops bpf_tracing_link_lops = {
2559 .release = bpf_tracing_link_release,
2560 .dealloc = bpf_tracing_link_dealloc,
2561 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2562 .fill_link_info = bpf_tracing_link_fill_link_info,
2565 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2569 struct bpf_link_primer link_primer;
2570 struct bpf_prog *tgt_prog = NULL;
2571 struct bpf_trampoline *tr = NULL;
2572 struct bpf_tracing_link *link;
2576 switch (prog->type) {
2577 case BPF_PROG_TYPE_TRACING:
2578 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2579 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2580 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2585 case BPF_PROG_TYPE_EXT:
2586 if (prog->expected_attach_type != 0) {
2591 case BPF_PROG_TYPE_LSM:
2592 if (prog->expected_attach_type != BPF_LSM_MAC) {
2602 if (!!tgt_prog_fd != !!btf_id) {
2608 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2609 if (prog->type != BPF_PROG_TYPE_EXT) {
2614 tgt_prog = bpf_prog_get(tgt_prog_fd);
2615 if (IS_ERR(tgt_prog)) {
2616 err = PTR_ERR(tgt_prog);
2621 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
2624 link = kzalloc(sizeof(*link), GFP_USER);
2629 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2630 &bpf_tracing_link_lops, prog);
2631 link->attach_type = prog->expected_attach_type;
2633 mutex_lock(&prog->aux->dst_mutex);
2635 /* There are a few possible cases here:
2637 * - if prog->aux->dst_trampoline is set, the program was just loaded
2638 * and not yet attached to anything, so we can use the values stored
2641 * - if prog->aux->dst_trampoline is NULL, the program has already been
2642 * attached to a target and its initial target was cleared (below)
2644 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2645 * target_btf_id using the link_create API.
2647 * - if tgt_prog == NULL when this function was called using the old
2648 * raw_tracepoint_open API, and we need a target from prog->aux
2650 * The combination of no saved target in prog->aux, and no target
2651 * specified on load is illegal, and we reject that here.
2653 if (!prog->aux->dst_trampoline && !tgt_prog) {
2658 if (!prog->aux->dst_trampoline ||
2659 (key && key != prog->aux->dst_trampoline->key)) {
2660 /* If there is no saved target, or the specified target is
2661 * different from the destination specified at load time, we
2662 * need a new trampoline and a check for compatibility
2664 struct bpf_attach_target_info tgt_info = {};
2666 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2671 tr = bpf_trampoline_get(key, &tgt_info);
2677 /* The caller didn't specify a target, or the target was the
2678 * same as the destination supplied during program load. This
2679 * means we can reuse the trampoline and reference from program
2680 * load time, and there is no need to allocate a new one. This
2681 * can only happen once for any program, as the saved values in
2682 * prog->aux are cleared below.
2684 tr = prog->aux->dst_trampoline;
2685 tgt_prog = prog->aux->dst_prog;
2688 err = bpf_link_prime(&link->link, &link_primer);
2692 err = bpf_trampoline_link_prog(prog, tr);
2694 bpf_link_cleanup(&link_primer);
2699 link->tgt_prog = tgt_prog;
2700 link->trampoline = tr;
2702 /* Always clear the trampoline and target prog from prog->aux to make
2703 * sure the original attach destination is not kept alive after a
2704 * program is (re-)attached to another target.
2706 if (prog->aux->dst_prog &&
2707 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2708 /* got extra prog ref from syscall, or attaching to different prog */
2709 bpf_prog_put(prog->aux->dst_prog);
2710 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2711 /* we allocated a new trampoline, so free the old one */
2712 bpf_trampoline_put(prog->aux->dst_trampoline);
2714 prog->aux->dst_prog = NULL;
2715 prog->aux->dst_trampoline = NULL;
2716 mutex_unlock(&prog->aux->dst_mutex);
2718 return bpf_link_settle(&link_primer);
2720 if (tr && tr != prog->aux->dst_trampoline)
2721 bpf_trampoline_put(tr);
2722 mutex_unlock(&prog->aux->dst_mutex);
2725 if (tgt_prog_fd && tgt_prog)
2726 bpf_prog_put(tgt_prog);
2730 struct bpf_raw_tp_link {
2731 struct bpf_link link;
2732 struct bpf_raw_event_map *btp;
2735 static void bpf_raw_tp_link_release(struct bpf_link *link)
2737 struct bpf_raw_tp_link *raw_tp =
2738 container_of(link, struct bpf_raw_tp_link, link);
2740 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
2741 bpf_put_raw_tracepoint(raw_tp->btp);
2744 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2746 struct bpf_raw_tp_link *raw_tp =
2747 container_of(link, struct bpf_raw_tp_link, link);
2752 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2753 struct seq_file *seq)
2755 struct bpf_raw_tp_link *raw_tp_link =
2756 container_of(link, struct bpf_raw_tp_link, link);
2760 raw_tp_link->btp->tp->name);
2763 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2764 struct bpf_link_info *info)
2766 struct bpf_raw_tp_link *raw_tp_link =
2767 container_of(link, struct bpf_raw_tp_link, link);
2768 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2769 const char *tp_name = raw_tp_link->btp->tp->name;
2770 u32 ulen = info->raw_tracepoint.tp_name_len;
2771 size_t tp_len = strlen(tp_name);
2776 info->raw_tracepoint.tp_name_len = tp_len + 1;
2781 if (ulen >= tp_len + 1) {
2782 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2787 if (copy_to_user(ubuf, tp_name, ulen - 1))
2789 if (put_user(zero, ubuf + ulen - 1))
2797 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
2798 .release = bpf_raw_tp_link_release,
2799 .dealloc = bpf_raw_tp_link_dealloc,
2800 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2801 .fill_link_info = bpf_raw_tp_link_fill_link_info,
2804 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2806 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2808 struct bpf_link_primer link_primer;
2809 struct bpf_raw_tp_link *link;
2810 struct bpf_raw_event_map *btp;
2811 struct bpf_prog *prog;
2812 const char *tp_name;
2816 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2819 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2821 return PTR_ERR(prog);
2823 switch (prog->type) {
2824 case BPF_PROG_TYPE_TRACING:
2825 case BPF_PROG_TYPE_EXT:
2826 case BPF_PROG_TYPE_LSM:
2827 if (attr->raw_tracepoint.name) {
2828 /* The attach point for this category of programs
2829 * should be specified via btf_id during program load.
2834 if (prog->type == BPF_PROG_TYPE_TRACING &&
2835 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
2836 tp_name = prog->aux->attach_func_name;
2839 err = bpf_tracing_prog_attach(prog, 0, 0);
2843 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2844 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2845 if (strncpy_from_user(buf,
2846 u64_to_user_ptr(attr->raw_tracepoint.name),
2847 sizeof(buf) - 1) < 0) {
2851 buf[sizeof(buf) - 1] = 0;
2859 btp = bpf_get_raw_tracepoint(tp_name);
2865 link = kzalloc(sizeof(*link), GFP_USER);
2870 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2871 &bpf_raw_tp_link_lops, prog);
2874 err = bpf_link_prime(&link->link, &link_primer);
2880 err = bpf_probe_register(link->btp, prog);
2882 bpf_link_cleanup(&link_primer);
2886 return bpf_link_settle(&link_primer);
2889 bpf_put_raw_tracepoint(btp);
2895 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2896 enum bpf_attach_type attach_type)
2898 switch (prog->type) {
2899 case BPF_PROG_TYPE_CGROUP_SOCK:
2900 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2901 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2902 case BPF_PROG_TYPE_SK_LOOKUP:
2903 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
2904 case BPF_PROG_TYPE_CGROUP_SKB:
2905 if (!capable(CAP_NET_ADMIN))
2906 /* cg-skb progs can be loaded by unpriv user.
2907 * check permissions at attach time.
2910 return prog->enforce_expected_attach_type &&
2911 prog->expected_attach_type != attach_type ?
2918 static enum bpf_prog_type
2919 attach_type_to_prog_type(enum bpf_attach_type attach_type)
2921 switch (attach_type) {
2922 case BPF_CGROUP_INET_INGRESS:
2923 case BPF_CGROUP_INET_EGRESS:
2924 return BPF_PROG_TYPE_CGROUP_SKB;
2925 case BPF_CGROUP_INET_SOCK_CREATE:
2926 case BPF_CGROUP_INET_SOCK_RELEASE:
2927 case BPF_CGROUP_INET4_POST_BIND:
2928 case BPF_CGROUP_INET6_POST_BIND:
2929 return BPF_PROG_TYPE_CGROUP_SOCK;
2930 case BPF_CGROUP_INET4_BIND:
2931 case BPF_CGROUP_INET6_BIND:
2932 case BPF_CGROUP_INET4_CONNECT:
2933 case BPF_CGROUP_INET6_CONNECT:
2934 case BPF_CGROUP_INET4_GETPEERNAME:
2935 case BPF_CGROUP_INET6_GETPEERNAME:
2936 case BPF_CGROUP_INET4_GETSOCKNAME:
2937 case BPF_CGROUP_INET6_GETSOCKNAME:
2938 case BPF_CGROUP_UDP4_SENDMSG:
2939 case BPF_CGROUP_UDP6_SENDMSG:
2940 case BPF_CGROUP_UDP4_RECVMSG:
2941 case BPF_CGROUP_UDP6_RECVMSG:
2942 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2943 case BPF_CGROUP_SOCK_OPS:
2944 return BPF_PROG_TYPE_SOCK_OPS;
2945 case BPF_CGROUP_DEVICE:
2946 return BPF_PROG_TYPE_CGROUP_DEVICE;
2947 case BPF_SK_MSG_VERDICT:
2948 return BPF_PROG_TYPE_SK_MSG;
2949 case BPF_SK_SKB_STREAM_PARSER:
2950 case BPF_SK_SKB_STREAM_VERDICT:
2951 case BPF_SK_SKB_VERDICT:
2952 return BPF_PROG_TYPE_SK_SKB;
2953 case BPF_LIRC_MODE2:
2954 return BPF_PROG_TYPE_LIRC_MODE2;
2955 case BPF_FLOW_DISSECTOR:
2956 return BPF_PROG_TYPE_FLOW_DISSECTOR;
2957 case BPF_CGROUP_SYSCTL:
2958 return BPF_PROG_TYPE_CGROUP_SYSCTL;
2959 case BPF_CGROUP_GETSOCKOPT:
2960 case BPF_CGROUP_SETSOCKOPT:
2961 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
2962 case BPF_TRACE_ITER:
2963 return BPF_PROG_TYPE_TRACING;
2965 return BPF_PROG_TYPE_SK_LOOKUP;
2967 return BPF_PROG_TYPE_XDP;
2969 return BPF_PROG_TYPE_UNSPEC;
2973 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
2975 #define BPF_F_ATTACH_MASK \
2976 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
2978 static int bpf_prog_attach(const union bpf_attr *attr)
2980 enum bpf_prog_type ptype;
2981 struct bpf_prog *prog;
2984 if (CHECK_ATTR(BPF_PROG_ATTACH))
2987 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
2990 ptype = attach_type_to_prog_type(attr->attach_type);
2991 if (ptype == BPF_PROG_TYPE_UNSPEC)
2994 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2996 return PTR_ERR(prog);
2998 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3004 case BPF_PROG_TYPE_SK_SKB:
3005 case BPF_PROG_TYPE_SK_MSG:
3006 ret = sock_map_get_from_fd(attr, prog);
3008 case BPF_PROG_TYPE_LIRC_MODE2:
3009 ret = lirc_prog_attach(attr, prog);
3011 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3012 ret = netns_bpf_prog_attach(attr, prog);
3014 case BPF_PROG_TYPE_CGROUP_DEVICE:
3015 case BPF_PROG_TYPE_CGROUP_SKB:
3016 case BPF_PROG_TYPE_CGROUP_SOCK:
3017 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3018 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3019 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3020 case BPF_PROG_TYPE_SOCK_OPS:
3021 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3032 #define BPF_PROG_DETACH_LAST_FIELD attach_type
3034 static int bpf_prog_detach(const union bpf_attr *attr)
3036 enum bpf_prog_type ptype;
3038 if (CHECK_ATTR(BPF_PROG_DETACH))
3041 ptype = attach_type_to_prog_type(attr->attach_type);
3044 case BPF_PROG_TYPE_SK_MSG:
3045 case BPF_PROG_TYPE_SK_SKB:
3046 return sock_map_prog_detach(attr, ptype);
3047 case BPF_PROG_TYPE_LIRC_MODE2:
3048 return lirc_prog_detach(attr);
3049 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3050 return netns_bpf_prog_detach(attr, ptype);
3051 case BPF_PROG_TYPE_CGROUP_DEVICE:
3052 case BPF_PROG_TYPE_CGROUP_SKB:
3053 case BPF_PROG_TYPE_CGROUP_SOCK:
3054 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3055 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3056 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3057 case BPF_PROG_TYPE_SOCK_OPS:
3058 return cgroup_bpf_prog_detach(attr, ptype);
3064 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3066 static int bpf_prog_query(const union bpf_attr *attr,
3067 union bpf_attr __user *uattr)
3069 if (!capable(CAP_NET_ADMIN))
3071 if (CHECK_ATTR(BPF_PROG_QUERY))
3073 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3076 switch (attr->query.attach_type) {
3077 case BPF_CGROUP_INET_INGRESS:
3078 case BPF_CGROUP_INET_EGRESS:
3079 case BPF_CGROUP_INET_SOCK_CREATE:
3080 case BPF_CGROUP_INET_SOCK_RELEASE:
3081 case BPF_CGROUP_INET4_BIND:
3082 case BPF_CGROUP_INET6_BIND:
3083 case BPF_CGROUP_INET4_POST_BIND:
3084 case BPF_CGROUP_INET6_POST_BIND:
3085 case BPF_CGROUP_INET4_CONNECT:
3086 case BPF_CGROUP_INET6_CONNECT:
3087 case BPF_CGROUP_INET4_GETPEERNAME:
3088 case BPF_CGROUP_INET6_GETPEERNAME:
3089 case BPF_CGROUP_INET4_GETSOCKNAME:
3090 case BPF_CGROUP_INET6_GETSOCKNAME:
3091 case BPF_CGROUP_UDP4_SENDMSG:
3092 case BPF_CGROUP_UDP6_SENDMSG:
3093 case BPF_CGROUP_UDP4_RECVMSG:
3094 case BPF_CGROUP_UDP6_RECVMSG:
3095 case BPF_CGROUP_SOCK_OPS:
3096 case BPF_CGROUP_DEVICE:
3097 case BPF_CGROUP_SYSCTL:
3098 case BPF_CGROUP_GETSOCKOPT:
3099 case BPF_CGROUP_SETSOCKOPT:
3100 return cgroup_bpf_prog_query(attr, uattr);
3101 case BPF_LIRC_MODE2:
3102 return lirc_prog_query(attr, uattr);
3103 case BPF_FLOW_DISSECTOR:
3105 return netns_bpf_prog_query(attr, uattr);
3111 #define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
3113 static int bpf_prog_test_run(const union bpf_attr *attr,
3114 union bpf_attr __user *uattr)
3116 struct bpf_prog *prog;
3117 int ret = -ENOTSUPP;
3119 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3122 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3123 (!attr->test.ctx_size_in && attr->test.ctx_in))
3126 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3127 (!attr->test.ctx_size_out && attr->test.ctx_out))
3130 prog = bpf_prog_get(attr->test.prog_fd);
3132 return PTR_ERR(prog);
3134 if (prog->aux->ops->test_run)
3135 ret = prog->aux->ops->test_run(prog, attr, uattr);
3141 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3143 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3144 union bpf_attr __user *uattr,
3148 u32 next_id = attr->start_id;
3151 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3154 if (!capable(CAP_SYS_ADMIN))
3159 if (!idr_get_next(idr, &next_id))
3161 spin_unlock_bh(lock);
3164 err = put_user(next_id, &uattr->next_id);
3169 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3171 struct bpf_map *map;
3173 spin_lock_bh(&map_idr_lock);
3175 map = idr_get_next(&map_idr, id);
3177 map = __bpf_map_inc_not_zero(map, false);
3183 spin_unlock_bh(&map_idr_lock);
3188 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3190 struct bpf_prog *prog;
3192 spin_lock_bh(&prog_idr_lock);
3194 prog = idr_get_next(&prog_idr, id);
3196 prog = bpf_prog_inc_not_zero(prog);
3202 spin_unlock_bh(&prog_idr_lock);
3207 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3209 struct bpf_prog *bpf_prog_by_id(u32 id)
3211 struct bpf_prog *prog;
3214 return ERR_PTR(-ENOENT);
3216 spin_lock_bh(&prog_idr_lock);
3217 prog = idr_find(&prog_idr, id);
3219 prog = bpf_prog_inc_not_zero(prog);
3221 prog = ERR_PTR(-ENOENT);
3222 spin_unlock_bh(&prog_idr_lock);
3226 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3228 struct bpf_prog *prog;
3229 u32 id = attr->prog_id;
3232 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3235 if (!capable(CAP_SYS_ADMIN))
3238 prog = bpf_prog_by_id(id);
3240 return PTR_ERR(prog);
3242 fd = bpf_prog_new_fd(prog);
3249 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3251 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3253 struct bpf_map *map;
3254 u32 id = attr->map_id;
3258 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3259 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3262 if (!capable(CAP_SYS_ADMIN))
3265 f_flags = bpf_get_file_flag(attr->open_flags);
3269 spin_lock_bh(&map_idr_lock);
3270 map = idr_find(&map_idr, id);
3272 map = __bpf_map_inc_not_zero(map, true);
3274 map = ERR_PTR(-ENOENT);
3275 spin_unlock_bh(&map_idr_lock);
3278 return PTR_ERR(map);
3280 fd = bpf_map_new_fd(map, f_flags);
3282 bpf_map_put_with_uref(map);
3287 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3288 unsigned long addr, u32 *off,
3291 const struct bpf_map *map;
3294 mutex_lock(&prog->aux->used_maps_mutex);
3295 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3296 map = prog->aux->used_maps[i];
3297 if (map == (void *)addr) {
3298 *type = BPF_PSEUDO_MAP_FD;
3301 if (!map->ops->map_direct_value_meta)
3303 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3304 *type = BPF_PSEUDO_MAP_VALUE;
3311 mutex_unlock(&prog->aux->used_maps_mutex);
3315 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3316 const struct cred *f_cred)
3318 const struct bpf_map *map;
3319 struct bpf_insn *insns;
3325 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3330 for (i = 0; i < prog->len; i++) {
3331 code = insns[i].code;
3333 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3334 insns[i].code = BPF_JMP | BPF_CALL;
3335 insns[i].imm = BPF_FUNC_tail_call;
3338 if (code == (BPF_JMP | BPF_CALL) ||
3339 code == (BPF_JMP | BPF_CALL_ARGS)) {
3340 if (code == (BPF_JMP | BPF_CALL_ARGS))
3341 insns[i].code = BPF_JMP | BPF_CALL;
3342 if (!bpf_dump_raw_ok(f_cred))
3346 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3347 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3351 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3354 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3355 map = bpf_map_from_imm(prog, imm, &off, &type);
3357 insns[i].src_reg = type;
3358 insns[i].imm = map->id;
3359 insns[i + 1].imm = off;
3367 static int set_info_rec_size(struct bpf_prog_info *info)
3370 * Ensure info.*_rec_size is the same as kernel expected size
3374 * Only allow zero *_rec_size if both _rec_size and _cnt are
3375 * zero. In this case, the kernel will set the expected
3376 * _rec_size back to the info.
3379 if ((info->nr_func_info || info->func_info_rec_size) &&
3380 info->func_info_rec_size != sizeof(struct bpf_func_info))
3383 if ((info->nr_line_info || info->line_info_rec_size) &&
3384 info->line_info_rec_size != sizeof(struct bpf_line_info))
3387 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3388 info->jited_line_info_rec_size != sizeof(__u64))
3391 info->func_info_rec_size = sizeof(struct bpf_func_info);
3392 info->line_info_rec_size = sizeof(struct bpf_line_info);
3393 info->jited_line_info_rec_size = sizeof(__u64);
3398 static int bpf_prog_get_info_by_fd(struct file *file,
3399 struct bpf_prog *prog,
3400 const union bpf_attr *attr,
3401 union bpf_attr __user *uattr)
3403 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3404 struct bpf_prog_info info;
3405 u32 info_len = attr->info.info_len;
3406 struct bpf_prog_stats stats;
3407 char __user *uinsns;
3411 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3414 info_len = min_t(u32, sizeof(info), info_len);
3416 memset(&info, 0, sizeof(info));
3417 if (copy_from_user(&info, uinfo, info_len))
3420 info.type = prog->type;
3421 info.id = prog->aux->id;
3422 info.load_time = prog->aux->load_time;
3423 info.created_by_uid = from_kuid_munged(current_user_ns(),
3424 prog->aux->user->uid);
3425 info.gpl_compatible = prog->gpl_compatible;
3427 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3428 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3430 mutex_lock(&prog->aux->used_maps_mutex);
3431 ulen = info.nr_map_ids;
3432 info.nr_map_ids = prog->aux->used_map_cnt;
3433 ulen = min_t(u32, info.nr_map_ids, ulen);
3435 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3438 for (i = 0; i < ulen; i++)
3439 if (put_user(prog->aux->used_maps[i]->id,
3440 &user_map_ids[i])) {
3441 mutex_unlock(&prog->aux->used_maps_mutex);
3445 mutex_unlock(&prog->aux->used_maps_mutex);
3447 err = set_info_rec_size(&info);
3451 bpf_prog_get_stats(prog, &stats);
3452 info.run_time_ns = stats.nsecs;
3453 info.run_cnt = stats.cnt;
3454 info.recursion_misses = stats.misses;
3456 if (!bpf_capable()) {
3457 info.jited_prog_len = 0;
3458 info.xlated_prog_len = 0;
3459 info.nr_jited_ksyms = 0;
3460 info.nr_jited_func_lens = 0;
3461 info.nr_func_info = 0;
3462 info.nr_line_info = 0;
3463 info.nr_jited_line_info = 0;
3467 ulen = info.xlated_prog_len;
3468 info.xlated_prog_len = bpf_prog_insn_size(prog);
3469 if (info.xlated_prog_len && ulen) {
3470 struct bpf_insn *insns_sanitized;
3473 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3474 info.xlated_prog_insns = 0;
3477 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3478 if (!insns_sanitized)
3480 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3481 ulen = min_t(u32, info.xlated_prog_len, ulen);
3482 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3483 kfree(insns_sanitized);
3488 if (bpf_prog_is_dev_bound(prog->aux)) {
3489 err = bpf_prog_offload_info_fill(&info, prog);
3495 /* NOTE: the following code is supposed to be skipped for offload.
3496 * bpf_prog_offload_info_fill() is the place to fill similar fields
3499 ulen = info.jited_prog_len;
3500 if (prog->aux->func_cnt) {
3503 info.jited_prog_len = 0;
3504 for (i = 0; i < prog->aux->func_cnt; i++)
3505 info.jited_prog_len += prog->aux->func[i]->jited_len;
3507 info.jited_prog_len = prog->jited_len;
3510 if (info.jited_prog_len && ulen) {
3511 if (bpf_dump_raw_ok(file->f_cred)) {
3512 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3513 ulen = min_t(u32, info.jited_prog_len, ulen);
3515 /* for multi-function programs, copy the JITed
3516 * instructions for all the functions
3518 if (prog->aux->func_cnt) {
3523 for (i = 0; i < prog->aux->func_cnt; i++) {
3524 len = prog->aux->func[i]->jited_len;
3525 len = min_t(u32, len, free);
3526 img = (u8 *) prog->aux->func[i]->bpf_func;
3527 if (copy_to_user(uinsns, img, len))
3535 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3539 info.jited_prog_insns = 0;
3543 ulen = info.nr_jited_ksyms;
3544 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
3546 if (bpf_dump_raw_ok(file->f_cred)) {
3547 unsigned long ksym_addr;
3548 u64 __user *user_ksyms;
3551 /* copy the address of the kernel symbol
3552 * corresponding to each function
3554 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3555 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
3556 if (prog->aux->func_cnt) {
3557 for (i = 0; i < ulen; i++) {
3558 ksym_addr = (unsigned long)
3559 prog->aux->func[i]->bpf_func;
3560 if (put_user((u64) ksym_addr,
3565 ksym_addr = (unsigned long) prog->bpf_func;
3566 if (put_user((u64) ksym_addr, &user_ksyms[0]))
3570 info.jited_ksyms = 0;
3574 ulen = info.nr_jited_func_lens;
3575 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
3577 if (bpf_dump_raw_ok(file->f_cred)) {
3578 u32 __user *user_lens;
3581 /* copy the JITed image lengths for each function */
3582 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3583 user_lens = u64_to_user_ptr(info.jited_func_lens);
3584 if (prog->aux->func_cnt) {
3585 for (i = 0; i < ulen; i++) {
3587 prog->aux->func[i]->jited_len;
3588 if (put_user(func_len, &user_lens[i]))
3592 func_len = prog->jited_len;
3593 if (put_user(func_len, &user_lens[0]))
3597 info.jited_func_lens = 0;
3602 info.btf_id = btf_obj_id(prog->aux->btf);
3604 ulen = info.nr_func_info;
3605 info.nr_func_info = prog->aux->func_info_cnt;
3606 if (info.nr_func_info && ulen) {
3607 char __user *user_finfo;
3609 user_finfo = u64_to_user_ptr(info.func_info);
3610 ulen = min_t(u32, info.nr_func_info, ulen);
3611 if (copy_to_user(user_finfo, prog->aux->func_info,
3612 info.func_info_rec_size * ulen))
3616 ulen = info.nr_line_info;
3617 info.nr_line_info = prog->aux->nr_linfo;
3618 if (info.nr_line_info && ulen) {
3619 __u8 __user *user_linfo;
3621 user_linfo = u64_to_user_ptr(info.line_info);
3622 ulen = min_t(u32, info.nr_line_info, ulen);
3623 if (copy_to_user(user_linfo, prog->aux->linfo,
3624 info.line_info_rec_size * ulen))
3628 ulen = info.nr_jited_line_info;
3629 if (prog->aux->jited_linfo)
3630 info.nr_jited_line_info = prog->aux->nr_linfo;
3632 info.nr_jited_line_info = 0;
3633 if (info.nr_jited_line_info && ulen) {
3634 if (bpf_dump_raw_ok(file->f_cred)) {
3635 __u64 __user *user_linfo;
3638 user_linfo = u64_to_user_ptr(info.jited_line_info);
3639 ulen = min_t(u32, info.nr_jited_line_info, ulen);
3640 for (i = 0; i < ulen; i++) {
3641 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3646 info.jited_line_info = 0;
3650 ulen = info.nr_prog_tags;
3651 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3653 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3656 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3657 ulen = min_t(u32, info.nr_prog_tags, ulen);
3658 if (prog->aux->func_cnt) {
3659 for (i = 0; i < ulen; i++) {
3660 if (copy_to_user(user_prog_tags[i],
3661 prog->aux->func[i]->tag,
3666 if (copy_to_user(user_prog_tags[0],
3667 prog->tag, BPF_TAG_SIZE))
3673 if (copy_to_user(uinfo, &info, info_len) ||
3674 put_user(info_len, &uattr->info.info_len))
3680 static int bpf_map_get_info_by_fd(struct file *file,
3681 struct bpf_map *map,
3682 const union bpf_attr *attr,
3683 union bpf_attr __user *uattr)
3685 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3686 struct bpf_map_info info;
3687 u32 info_len = attr->info.info_len;
3690 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3693 info_len = min_t(u32, sizeof(info), info_len);
3695 memset(&info, 0, sizeof(info));
3696 info.type = map->map_type;
3698 info.key_size = map->key_size;
3699 info.value_size = map->value_size;
3700 info.max_entries = map->max_entries;
3701 info.map_flags = map->map_flags;
3702 memcpy(info.name, map->name, sizeof(map->name));
3705 info.btf_id = btf_obj_id(map->btf);
3706 info.btf_key_type_id = map->btf_key_type_id;
3707 info.btf_value_type_id = map->btf_value_type_id;
3709 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
3711 if (bpf_map_is_dev_bound(map)) {
3712 err = bpf_map_offload_info_fill(&info, map);
3717 if (copy_to_user(uinfo, &info, info_len) ||
3718 put_user(info_len, &uattr->info.info_len))
3724 static int bpf_btf_get_info_by_fd(struct file *file,
3726 const union bpf_attr *attr,
3727 union bpf_attr __user *uattr)
3729 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3730 u32 info_len = attr->info.info_len;
3733 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
3737 return btf_get_info_by_fd(btf, attr, uattr);
3740 static int bpf_link_get_info_by_fd(struct file *file,
3741 struct bpf_link *link,
3742 const union bpf_attr *attr,
3743 union bpf_attr __user *uattr)
3745 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3746 struct bpf_link_info info;
3747 u32 info_len = attr->info.info_len;
3750 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3753 info_len = min_t(u32, sizeof(info), info_len);
3755 memset(&info, 0, sizeof(info));
3756 if (copy_from_user(&info, uinfo, info_len))
3759 info.type = link->type;
3761 info.prog_id = link->prog->aux->id;
3763 if (link->ops->fill_link_info) {
3764 err = link->ops->fill_link_info(link, &info);
3769 if (copy_to_user(uinfo, &info, info_len) ||
3770 put_user(info_len, &uattr->info.info_len))
3777 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3779 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3780 union bpf_attr __user *uattr)
3782 int ufd = attr->info.bpf_fd;
3786 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3793 if (f.file->f_op == &bpf_prog_fops)
3794 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
3796 else if (f.file->f_op == &bpf_map_fops)
3797 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
3799 else if (f.file->f_op == &btf_fops)
3800 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
3801 else if (f.file->f_op == &bpf_link_fops)
3802 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
3811 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3813 static int bpf_btf_load(const union bpf_attr *attr)
3815 if (CHECK_ATTR(BPF_BTF_LOAD))
3821 return btf_new_fd(attr);
3824 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3826 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3828 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3831 if (!capable(CAP_SYS_ADMIN))
3834 return btf_get_fd_by_id(attr->btf_id);
3837 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3838 union bpf_attr __user *uattr,
3839 u32 prog_id, u32 fd_type,
3840 const char *buf, u64 probe_offset,
3843 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3844 u32 len = buf ? strlen(buf) : 0, input_len;
3847 if (put_user(len, &uattr->task_fd_query.buf_len))
3849 input_len = attr->task_fd_query.buf_len;
3850 if (input_len && ubuf) {
3852 /* nothing to copy, just make ubuf NULL terminated */
3855 if (put_user(zero, ubuf))
3857 } else if (input_len >= len + 1) {
3858 /* ubuf can hold the string with NULL terminator */
3859 if (copy_to_user(ubuf, buf, len + 1))
3862 /* ubuf cannot hold the string with NULL terminator,
3863 * do a partial copy with NULL terminator.
3868 if (copy_to_user(ubuf, buf, input_len - 1))
3870 if (put_user(zero, ubuf + input_len - 1))
3875 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3876 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3877 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3878 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3884 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3886 static int bpf_task_fd_query(const union bpf_attr *attr,
3887 union bpf_attr __user *uattr)
3889 pid_t pid = attr->task_fd_query.pid;
3890 u32 fd = attr->task_fd_query.fd;
3891 const struct perf_event *event;
3892 struct task_struct *task;
3896 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3899 if (!capable(CAP_SYS_ADMIN))
3902 if (attr->task_fd_query.flags != 0)
3905 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3910 file = fget_task(task, fd);
3911 put_task_struct(task);
3915 if (file->f_op == &bpf_link_fops) {
3916 struct bpf_link *link = file->private_data;
3918 if (link->ops == &bpf_raw_tp_link_lops) {
3919 struct bpf_raw_tp_link *raw_tp =
3920 container_of(link, struct bpf_raw_tp_link, link);
3921 struct bpf_raw_event_map *btp = raw_tp->btp;
3923 err = bpf_task_fd_query_copy(attr, uattr,
3924 raw_tp->link.prog->aux->id,
3925 BPF_FD_TYPE_RAW_TRACEPOINT,
3926 btp->tp->name, 0, 0);
3932 event = perf_get_event(file);
3933 if (!IS_ERR(event)) {
3934 u64 probe_offset, probe_addr;
3935 u32 prog_id, fd_type;
3938 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3939 &buf, &probe_offset,
3942 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3956 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
3958 #define BPF_DO_BATCH(fn) \
3964 err = fn(map, attr, uattr); \
3967 static int bpf_map_do_batch(const union bpf_attr *attr,
3968 union bpf_attr __user *uattr,
3971 struct bpf_map *map;
3975 if (CHECK_ATTR(BPF_MAP_BATCH))
3978 ufd = attr->batch.map_fd;
3980 map = __bpf_map_get(f);
3982 return PTR_ERR(map);
3984 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
3985 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
3986 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
3991 if (cmd != BPF_MAP_LOOKUP_BATCH &&
3992 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
3997 if (cmd == BPF_MAP_LOOKUP_BATCH)
3998 BPF_DO_BATCH(map->ops->map_lookup_batch);
3999 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4000 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
4001 else if (cmd == BPF_MAP_UPDATE_BATCH)
4002 BPF_DO_BATCH(map->ops->map_update_batch);
4004 BPF_DO_BATCH(map->ops->map_delete_batch);
4011 static int tracing_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
4013 if (attr->link_create.attach_type != prog->expected_attach_type)
4016 if (prog->expected_attach_type == BPF_TRACE_ITER)
4017 return bpf_iter_link_attach(attr, prog);
4018 else if (prog->type == BPF_PROG_TYPE_EXT)
4019 return bpf_tracing_prog_attach(prog,
4020 attr->link_create.target_fd,
4021 attr->link_create.target_btf_id);
4025 #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
4026 static int link_create(union bpf_attr *attr)
4028 enum bpf_prog_type ptype;
4029 struct bpf_prog *prog;
4032 if (CHECK_ATTR(BPF_LINK_CREATE))
4035 prog = bpf_prog_get(attr->link_create.prog_fd);
4037 return PTR_ERR(prog);
4039 ret = bpf_prog_attach_check_attach_type(prog,
4040 attr->link_create.attach_type);
4044 if (prog->type == BPF_PROG_TYPE_EXT) {
4045 ret = tracing_bpf_link_attach(attr, prog);
4049 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4050 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4056 case BPF_PROG_TYPE_CGROUP_SKB:
4057 case BPF_PROG_TYPE_CGROUP_SOCK:
4058 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4059 case BPF_PROG_TYPE_SOCK_OPS:
4060 case BPF_PROG_TYPE_CGROUP_DEVICE:
4061 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4062 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4063 ret = cgroup_bpf_link_attach(attr, prog);
4065 case BPF_PROG_TYPE_TRACING:
4066 ret = tracing_bpf_link_attach(attr, prog);
4068 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4069 case BPF_PROG_TYPE_SK_LOOKUP:
4070 ret = netns_bpf_link_create(attr, prog);
4073 case BPF_PROG_TYPE_XDP:
4074 ret = bpf_xdp_link_attach(attr, prog);
4087 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4089 static int link_update(union bpf_attr *attr)
4091 struct bpf_prog *old_prog = NULL, *new_prog;
4092 struct bpf_link *link;
4096 if (CHECK_ATTR(BPF_LINK_UPDATE))
4099 flags = attr->link_update.flags;
4100 if (flags & ~BPF_F_REPLACE)
4103 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4105 return PTR_ERR(link);
4107 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4108 if (IS_ERR(new_prog)) {
4109 ret = PTR_ERR(new_prog);
4113 if (flags & BPF_F_REPLACE) {
4114 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4115 if (IS_ERR(old_prog)) {
4116 ret = PTR_ERR(old_prog);
4120 } else if (attr->link_update.old_prog_fd) {
4125 if (link->ops->update_prog)
4126 ret = link->ops->update_prog(link, new_prog, old_prog);
4132 bpf_prog_put(old_prog);
4134 bpf_prog_put(new_prog);
4140 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4142 static int link_detach(union bpf_attr *attr)
4144 struct bpf_link *link;
4147 if (CHECK_ATTR(BPF_LINK_DETACH))
4150 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4152 return PTR_ERR(link);
4154 if (link->ops->detach)
4155 ret = link->ops->detach(link);
4163 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4165 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4168 struct bpf_link *bpf_link_by_id(u32 id)
4170 struct bpf_link *link;
4173 return ERR_PTR(-ENOENT);
4175 spin_lock_bh(&link_idr_lock);
4176 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4177 link = idr_find(&link_idr, id);
4180 link = bpf_link_inc_not_zero(link);
4182 link = ERR_PTR(-EAGAIN);
4184 link = ERR_PTR(-ENOENT);
4186 spin_unlock_bh(&link_idr_lock);
4190 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4192 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4194 struct bpf_link *link;
4195 u32 id = attr->link_id;
4198 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4201 if (!capable(CAP_SYS_ADMIN))
4204 link = bpf_link_by_id(id);
4206 return PTR_ERR(link);
4208 fd = bpf_link_new_fd(link);
4215 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4217 static int bpf_stats_release(struct inode *inode, struct file *file)
4219 mutex_lock(&bpf_stats_enabled_mutex);
4220 static_key_slow_dec(&bpf_stats_enabled_key.key);
4221 mutex_unlock(&bpf_stats_enabled_mutex);
4225 static const struct file_operations bpf_stats_fops = {
4226 .release = bpf_stats_release,
4229 static int bpf_enable_runtime_stats(void)
4233 mutex_lock(&bpf_stats_enabled_mutex);
4235 /* Set a very high limit to avoid overflow */
4236 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4237 mutex_unlock(&bpf_stats_enabled_mutex);
4241 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4243 static_key_slow_inc(&bpf_stats_enabled_key.key);
4245 mutex_unlock(&bpf_stats_enabled_mutex);
4249 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4251 static int bpf_enable_stats(union bpf_attr *attr)
4254 if (CHECK_ATTR(BPF_ENABLE_STATS))
4257 if (!capable(CAP_SYS_ADMIN))
4260 switch (attr->enable_stats.type) {
4261 case BPF_STATS_RUN_TIME:
4262 return bpf_enable_runtime_stats();
4269 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4271 static int bpf_iter_create(union bpf_attr *attr)
4273 struct bpf_link *link;
4276 if (CHECK_ATTR(BPF_ITER_CREATE))
4279 if (attr->iter_create.flags)
4282 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4284 return PTR_ERR(link);
4286 err = bpf_iter_new_fd(link);
4292 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4294 static int bpf_prog_bind_map(union bpf_attr *attr)
4296 struct bpf_prog *prog;
4297 struct bpf_map *map;
4298 struct bpf_map **used_maps_old, **used_maps_new;
4301 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4304 if (attr->prog_bind_map.flags)
4307 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4309 return PTR_ERR(prog);
4311 map = bpf_map_get(attr->prog_bind_map.map_fd);
4317 mutex_lock(&prog->aux->used_maps_mutex);
4319 used_maps_old = prog->aux->used_maps;
4321 for (i = 0; i < prog->aux->used_map_cnt; i++)
4322 if (used_maps_old[i] == map) {
4327 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4328 sizeof(used_maps_new[0]),
4330 if (!used_maps_new) {
4335 memcpy(used_maps_new, used_maps_old,
4336 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4337 used_maps_new[prog->aux->used_map_cnt] = map;
4339 prog->aux->used_map_cnt++;
4340 prog->aux->used_maps = used_maps_new;
4342 kfree(used_maps_old);
4345 mutex_unlock(&prog->aux->used_maps_mutex);
4354 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4356 union bpf_attr attr;
4359 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
4362 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4365 size = min_t(u32, size, sizeof(attr));
4367 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4368 memset(&attr, 0, sizeof(attr));
4369 if (copy_from_user(&attr, uattr, size) != 0)
4372 err = security_bpf(cmd, &attr, size);
4377 case BPF_MAP_CREATE:
4378 err = map_create(&attr);
4380 case BPF_MAP_LOOKUP_ELEM:
4381 err = map_lookup_elem(&attr);
4383 case BPF_MAP_UPDATE_ELEM:
4384 err = map_update_elem(&attr);
4386 case BPF_MAP_DELETE_ELEM:
4387 err = map_delete_elem(&attr);
4389 case BPF_MAP_GET_NEXT_KEY:
4390 err = map_get_next_key(&attr);
4392 case BPF_MAP_FREEZE:
4393 err = map_freeze(&attr);
4396 err = bpf_prog_load(&attr, uattr);
4399 err = bpf_obj_pin(&attr);
4402 err = bpf_obj_get(&attr);
4404 case BPF_PROG_ATTACH:
4405 err = bpf_prog_attach(&attr);
4407 case BPF_PROG_DETACH:
4408 err = bpf_prog_detach(&attr);
4410 case BPF_PROG_QUERY:
4411 err = bpf_prog_query(&attr, uattr);
4413 case BPF_PROG_TEST_RUN:
4414 err = bpf_prog_test_run(&attr, uattr);
4416 case BPF_PROG_GET_NEXT_ID:
4417 err = bpf_obj_get_next_id(&attr, uattr,
4418 &prog_idr, &prog_idr_lock);
4420 case BPF_MAP_GET_NEXT_ID:
4421 err = bpf_obj_get_next_id(&attr, uattr,
4422 &map_idr, &map_idr_lock);
4424 case BPF_BTF_GET_NEXT_ID:
4425 err = bpf_obj_get_next_id(&attr, uattr,
4426 &btf_idr, &btf_idr_lock);
4428 case BPF_PROG_GET_FD_BY_ID:
4429 err = bpf_prog_get_fd_by_id(&attr);
4431 case BPF_MAP_GET_FD_BY_ID:
4432 err = bpf_map_get_fd_by_id(&attr);
4434 case BPF_OBJ_GET_INFO_BY_FD:
4435 err = bpf_obj_get_info_by_fd(&attr, uattr);
4437 case BPF_RAW_TRACEPOINT_OPEN:
4438 err = bpf_raw_tracepoint_open(&attr);
4441 err = bpf_btf_load(&attr);
4443 case BPF_BTF_GET_FD_BY_ID:
4444 err = bpf_btf_get_fd_by_id(&attr);
4446 case BPF_TASK_FD_QUERY:
4447 err = bpf_task_fd_query(&attr, uattr);
4449 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4450 err = map_lookup_and_delete_elem(&attr);
4452 case BPF_MAP_LOOKUP_BATCH:
4453 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
4455 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4456 err = bpf_map_do_batch(&attr, uattr,
4457 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4459 case BPF_MAP_UPDATE_BATCH:
4460 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
4462 case BPF_MAP_DELETE_BATCH:
4463 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);
4465 case BPF_LINK_CREATE:
4466 err = link_create(&attr);
4468 case BPF_LINK_UPDATE:
4469 err = link_update(&attr);
4471 case BPF_LINK_GET_FD_BY_ID:
4472 err = bpf_link_get_fd_by_id(&attr);
4474 case BPF_LINK_GET_NEXT_ID:
4475 err = bpf_obj_get_next_id(&attr, uattr,
4476 &link_idr, &link_idr_lock);
4478 case BPF_ENABLE_STATS:
4479 err = bpf_enable_stats(&attr);
4481 case BPF_ITER_CREATE:
4482 err = bpf_iter_create(&attr);
4484 case BPF_LINK_DETACH:
4485 err = link_detach(&attr);
4487 case BPF_PROG_BIND_MAP:
4488 err = bpf_prog_bind_map(&attr);