1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
7 #include <uapi/linux/bpf.h>
8 #include <uapi/linux/filter.h>
10 #include <linux/workqueue.h>
11 #include <linux/file.h>
12 #include <linux/percpu.h>
13 #include <linux/err.h>
14 #include <linux/rbtree_latch.h>
15 #include <linux/numa.h>
16 #include <linux/mm_types.h>
17 #include <linux/wait.h>
18 #include <linux/refcount.h>
19 #include <linux/mutex.h>
20 #include <linux/module.h>
21 #include <linux/kallsyms.h>
22 #include <linux/capability.h>
23 #include <linux/sched/mm.h>
24 #include <linux/slab.h>
25 #include <linux/percpu-refcount.h>
26 #include <linux/stddef.h>
27 #include <linux/bpfptr.h>
28 #include <linux/btf.h>
29 #include <linux/rcupdate_trace.h>
31 struct bpf_verifier_env;
32 struct bpf_verifier_log;
41 struct exception_table_entry;
42 struct seq_operations;
43 struct bpf_iter_aux_info;
44 struct bpf_local_storage;
45 struct bpf_local_storage_map;
49 struct bpf_func_state;
53 extern struct idr btf_idr;
54 extern spinlock_t btf_idr_lock;
55 extern struct kobject *btf_kobj;
57 typedef u64 (*bpf_callback_t)(u64, u64, u64, u64, u64);
58 typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,
59 struct bpf_iter_aux_info *aux);
60 typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
61 typedef unsigned int (*bpf_func_t)(const void *,
62 const struct bpf_insn *);
63 struct bpf_iter_seq_info {
64 const struct seq_operations *seq_ops;
65 bpf_iter_init_seq_priv_t init_seq_private;
66 bpf_iter_fini_seq_priv_t fini_seq_private;
70 /* map is generic key/value storage optionally accessible by eBPF programs */
72 /* funcs callable from userspace (via syscall) */
73 int (*map_alloc_check)(union bpf_attr *attr);
74 struct bpf_map *(*map_alloc)(union bpf_attr *attr);
75 void (*map_release)(struct bpf_map *map, struct file *map_file);
76 void (*map_free)(struct bpf_map *map);
77 int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
78 void (*map_release_uref)(struct bpf_map *map);
79 void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
80 int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr,
81 union bpf_attr __user *uattr);
82 int (*map_lookup_and_delete_elem)(struct bpf_map *map, void *key,
83 void *value, u64 flags);
84 int (*map_lookup_and_delete_batch)(struct bpf_map *map,
85 const union bpf_attr *attr,
86 union bpf_attr __user *uattr);
87 int (*map_update_batch)(struct bpf_map *map, const union bpf_attr *attr,
88 union bpf_attr __user *uattr);
89 int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr,
90 union bpf_attr __user *uattr);
92 /* funcs callable from userspace and from eBPF programs */
93 void *(*map_lookup_elem)(struct bpf_map *map, void *key);
94 int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
95 int (*map_delete_elem)(struct bpf_map *map, void *key);
96 int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
97 int (*map_pop_elem)(struct bpf_map *map, void *value);
98 int (*map_peek_elem)(struct bpf_map *map, void *value);
99 void *(*map_lookup_percpu_elem)(struct bpf_map *map, void *key, u32 cpu);
101 /* funcs called by prog_array and perf_event_array map */
102 void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
104 void (*map_fd_put_ptr)(void *ptr);
105 int (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
106 u32 (*map_fd_sys_lookup_elem)(void *ptr);
107 void (*map_seq_show_elem)(struct bpf_map *map, void *key,
109 int (*map_check_btf)(const struct bpf_map *map,
110 const struct btf *btf,
111 const struct btf_type *key_type,
112 const struct btf_type *value_type);
114 /* Prog poke tracking helpers. */
115 int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux);
116 void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux);
117 void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old,
118 struct bpf_prog *new);
120 /* Direct value access helpers. */
121 int (*map_direct_value_addr)(const struct bpf_map *map,
123 int (*map_direct_value_meta)(const struct bpf_map *map,
125 int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
126 __poll_t (*map_poll)(struct bpf_map *map, struct file *filp,
127 struct poll_table_struct *pts);
129 /* Functions called by bpf_local_storage maps */
130 int (*map_local_storage_charge)(struct bpf_local_storage_map *smap,
131 void *owner, u32 size);
132 void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap,
133 void *owner, u32 size);
134 struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(void *owner);
137 int (*map_redirect)(struct bpf_map *map, u32 ifindex, u64 flags);
139 /* map_meta_equal must be implemented for maps that can be
140 * used as an inner map. It is a runtime check to ensure
141 * an inner map can be inserted to an outer map.
143 * Some properties of the inner map has been used during the
144 * verification time. When inserting an inner map at the runtime,
145 * map_meta_equal has to ensure the inserting map has the same
146 * properties that the verifier has used earlier.
148 bool (*map_meta_equal)(const struct bpf_map *meta0,
149 const struct bpf_map *meta1);
152 int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,
153 struct bpf_func_state *caller,
154 struct bpf_func_state *callee);
155 int (*map_for_each_callback)(struct bpf_map *map,
156 bpf_callback_t callback_fn,
157 void *callback_ctx, u64 flags);
159 /* BTF id of struct allocated by map_alloc */
162 /* bpf_iter info used to open a seq_file */
163 const struct bpf_iter_seq_info *iter_seq_info;
167 /* Support at most 8 pointers in a BPF map value */
168 BPF_MAP_VALUE_OFF_MAX = 8,
169 BPF_MAP_OFF_ARR_MAX = BPF_MAP_VALUE_OFF_MAX +
170 1 + /* for bpf_spin_lock */
171 1, /* for bpf_timer */
179 struct bpf_map_value_off_desc {
181 enum bpf_kptr_type type;
184 struct module *module;
185 btf_dtor_kfunc_t dtor;
190 struct bpf_map_value_off {
192 struct bpf_map_value_off_desc off[];
195 struct bpf_map_off_arr {
197 u32 field_off[BPF_MAP_OFF_ARR_MAX];
198 u8 field_sz[BPF_MAP_OFF_ARR_MAX];
202 /* The first two cachelines with read-mostly members of which some
203 * are also accessed in fast-path (e.g. ops, max_entries).
205 const struct bpf_map_ops *ops ____cacheline_aligned;
206 struct bpf_map *inner_map_meta;
207 #ifdef CONFIG_SECURITY
210 enum bpf_map_type map_type;
214 u64 map_extra; /* any per-map-type extra fields */
216 int spin_lock_off; /* >=0 valid offset, <0 error */
217 struct bpf_map_value_off *kptr_off_tab;
218 int timer_off; /* >=0 valid offset, <0 error */
222 u32 btf_value_type_id;
223 u32 btf_vmlinux_value_type_id;
225 #ifdef CONFIG_MEMCG_KMEM
226 struct obj_cgroup *objcg;
228 char name[BPF_OBJ_NAME_LEN];
229 struct bpf_map_off_arr *off_arr;
230 /* The 3rd and 4th cacheline with misc members to avoid false sharing
231 * particularly with refcounting.
233 atomic64_t refcnt ____cacheline_aligned;
235 struct work_struct work;
236 struct mutex freeze_mutex;
238 /* 'Ownership' of program-containing map is claimed by the first program
239 * that is going to use this map or by the first program which FD is
240 * stored in the map to make sure that all callers and callees have the
241 * same prog type, JITed flag and xdp_has_frags flag.
245 enum bpf_prog_type type;
250 bool frozen; /* write-once; write-protected by freeze_mutex */
253 static inline bool map_value_has_spin_lock(const struct bpf_map *map)
255 return map->spin_lock_off >= 0;
258 static inline bool map_value_has_timer(const struct bpf_map *map)
260 return map->timer_off >= 0;
263 static inline bool map_value_has_kptrs(const struct bpf_map *map)
265 return !IS_ERR_OR_NULL(map->kptr_off_tab);
268 static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
270 if (unlikely(map_value_has_spin_lock(map)))
271 memset(dst + map->spin_lock_off, 0, sizeof(struct bpf_spin_lock));
272 if (unlikely(map_value_has_timer(map)))
273 memset(dst + map->timer_off, 0, sizeof(struct bpf_timer));
274 if (unlikely(map_value_has_kptrs(map))) {
275 struct bpf_map_value_off *tab = map->kptr_off_tab;
278 for (i = 0; i < tab->nr_off; i++)
279 *(u64 *)(dst + tab->off[i].offset) = 0;
283 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
284 * forced to use 'long' read/writes to try to atomically copy long counters.
285 * Best-effort only. No barriers here, since it _will_ race with concurrent
286 * updates from BPF programs. Called from bpf syscall and mostly used with
287 * size 8 or 16 bytes, so ask compiler to inline it.
289 static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
291 const long *lsrc = src;
294 size /= sizeof(long);
299 /* copy everything but bpf_spin_lock, bpf_timer, and kptrs. There could be one of each. */
300 static inline void __copy_map_value(struct bpf_map *map, void *dst, void *src, bool long_memcpy)
305 if (likely(!map->off_arr)) {
307 bpf_long_memcpy(dst, src, round_up(map->value_size, 8));
309 memcpy(dst, src, map->value_size);
313 for (i = 0; i < map->off_arr->cnt; i++) {
314 u32 next_off = map->off_arr->field_off[i];
316 memcpy(dst + curr_off, src + curr_off, next_off - curr_off);
317 curr_off += map->off_arr->field_sz[i];
319 memcpy(dst + curr_off, src + curr_off, map->value_size - curr_off);
322 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
324 __copy_map_value(map, dst, src, false);
327 static inline void copy_map_value_long(struct bpf_map *map, void *dst, void *src)
329 __copy_map_value(map, dst, src, true);
332 static inline void zero_map_value(struct bpf_map *map, void *dst)
337 if (likely(!map->off_arr)) {
338 memset(dst, 0, map->value_size);
342 for (i = 0; i < map->off_arr->cnt; i++) {
343 u32 next_off = map->off_arr->field_off[i];
345 memset(dst + curr_off, 0, next_off - curr_off);
346 curr_off += map->off_arr->field_sz[i];
348 memset(dst + curr_off, 0, map->value_size - curr_off);
351 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
353 void bpf_timer_cancel_and_free(void *timer);
354 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
356 struct bpf_offload_dev;
357 struct bpf_offloaded_map;
359 struct bpf_map_dev_ops {
360 int (*map_get_next_key)(struct bpf_offloaded_map *map,
361 void *key, void *next_key);
362 int (*map_lookup_elem)(struct bpf_offloaded_map *map,
363 void *key, void *value);
364 int (*map_update_elem)(struct bpf_offloaded_map *map,
365 void *key, void *value, u64 flags);
366 int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
369 struct bpf_offloaded_map {
371 struct net_device *netdev;
372 const struct bpf_map_dev_ops *dev_ops;
374 struct list_head offloads;
377 static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
379 return container_of(map, struct bpf_offloaded_map, map);
382 static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
384 return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
387 static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
389 return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) &&
390 map->ops->map_seq_show_elem;
393 int map_check_no_btf(const struct bpf_map *map,
394 const struct btf *btf,
395 const struct btf_type *key_type,
396 const struct btf_type *value_type);
398 bool bpf_map_meta_equal(const struct bpf_map *meta0,
399 const struct bpf_map *meta1);
401 extern const struct bpf_map_ops bpf_map_offload_ops;
403 /* bpf_type_flag contains a set of flags that are applicable to the values of
404 * arg_type, ret_type and reg_type. For example, a pointer value may be null,
405 * or a memory is read-only. We classify types into two categories: base types
406 * and extended types. Extended types are base types combined with a type flag.
408 * Currently there are no more than 32 base types in arg_type, ret_type and
411 #define BPF_BASE_TYPE_BITS 8
414 /* PTR may be NULL. */
415 PTR_MAYBE_NULL = BIT(0 + BPF_BASE_TYPE_BITS),
417 /* MEM is read-only. When applied on bpf_arg, it indicates the arg is
418 * compatible with both mutable and immutable memory.
420 MEM_RDONLY = BIT(1 + BPF_BASE_TYPE_BITS),
422 /* MEM was "allocated" from a different helper, and cannot be mixed
423 * with regular non-MEM_ALLOC'ed MEM types.
425 MEM_ALLOC = BIT(2 + BPF_BASE_TYPE_BITS),
427 /* MEM is in user address space. */
428 MEM_USER = BIT(3 + BPF_BASE_TYPE_BITS),
430 /* MEM is a percpu memory. MEM_PERCPU tags PTR_TO_BTF_ID. When tagged
431 * with MEM_PERCPU, PTR_TO_BTF_ID _cannot_ be directly accessed. In
432 * order to drop this tag, it must be passed into bpf_per_cpu_ptr()
433 * or bpf_this_cpu_ptr(), which will return the pointer corresponding
434 * to the specified cpu.
436 MEM_PERCPU = BIT(4 + BPF_BASE_TYPE_BITS),
438 /* Indicates that the argument will be released. */
439 OBJ_RELEASE = BIT(5 + BPF_BASE_TYPE_BITS),
441 /* PTR is not trusted. This is only used with PTR_TO_BTF_ID, to mark
442 * unreferenced and referenced kptr loaded from map value using a load
443 * instruction, so that they can only be dereferenced but not escape the
444 * BPF program into the kernel (i.e. cannot be passed as arguments to
445 * kfunc or bpf helpers).
447 PTR_UNTRUSTED = BIT(6 + BPF_BASE_TYPE_BITS),
449 MEM_UNINIT = BIT(7 + BPF_BASE_TYPE_BITS),
451 /* DYNPTR points to memory local to the bpf program. */
452 DYNPTR_TYPE_LOCAL = BIT(8 + BPF_BASE_TYPE_BITS),
454 /* DYNPTR points to a kernel-produced ringbuf record. */
455 DYNPTR_TYPE_RINGBUF = BIT(9 + BPF_BASE_TYPE_BITS),
457 /* Size is known at compile time. */
458 MEM_FIXED_SIZE = BIT(10 + BPF_BASE_TYPE_BITS),
461 __BPF_TYPE_LAST_FLAG = __BPF_TYPE_FLAG_MAX - 1,
464 #define DYNPTR_TYPE_FLAG_MASK (DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF)
466 /* Max number of base types. */
467 #define BPF_BASE_TYPE_LIMIT (1UL << BPF_BASE_TYPE_BITS)
469 /* Max number of all types. */
470 #define BPF_TYPE_LIMIT (__BPF_TYPE_LAST_FLAG | (__BPF_TYPE_LAST_FLAG - 1))
472 /* function argument constraints */
474 ARG_DONTCARE = 0, /* unused argument in helper function */
476 /* the following constraints used to prototype
477 * bpf_map_lookup/update/delete_elem() functions
479 ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */
480 ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
481 ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
483 /* Used to prototype bpf_memcmp() and other functions that access data
484 * on eBPF program stack
486 ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */
488 ARG_CONST_SIZE, /* number of bytes accessed from memory */
489 ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
491 ARG_PTR_TO_CTX, /* pointer to context */
492 ARG_ANYTHING, /* any (initialized) argument is ok */
493 ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */
494 ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
495 ARG_PTR_TO_INT, /* pointer to int */
496 ARG_PTR_TO_LONG, /* pointer to long */
497 ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */
498 ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */
499 ARG_PTR_TO_ALLOC_MEM, /* pointer to dynamically allocated memory */
500 ARG_CONST_ALLOC_SIZE_OR_ZERO, /* number of allocated bytes requested */
501 ARG_PTR_TO_BTF_ID_SOCK_COMMON, /* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */
502 ARG_PTR_TO_PERCPU_BTF_ID, /* pointer to in-kernel percpu type */
503 ARG_PTR_TO_FUNC, /* pointer to a bpf program function */
504 ARG_PTR_TO_STACK, /* pointer to stack */
505 ARG_PTR_TO_CONST_STR, /* pointer to a null terminated read-only string */
506 ARG_PTR_TO_TIMER, /* pointer to bpf_timer */
507 ARG_PTR_TO_KPTR, /* pointer to referenced kptr */
508 ARG_PTR_TO_DYNPTR, /* pointer to bpf_dynptr. See bpf_type_flag for dynptr type */
511 /* Extended arg_types. */
512 ARG_PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_MAP_VALUE,
513 ARG_PTR_TO_MEM_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_MEM,
514 ARG_PTR_TO_CTX_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_CTX,
515 ARG_PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_SOCKET,
516 ARG_PTR_TO_ALLOC_MEM_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_ALLOC_MEM,
517 ARG_PTR_TO_STACK_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_STACK,
518 ARG_PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_BTF_ID,
519 /* pointer to memory does not need to be initialized, helper function must fill
520 * all bytes or clear them in error case.
522 ARG_PTR_TO_UNINIT_MEM = MEM_UNINIT | ARG_PTR_TO_MEM,
523 /* Pointer to valid memory of size known at compile time. */
524 ARG_PTR_TO_FIXED_SIZE_MEM = MEM_FIXED_SIZE | ARG_PTR_TO_MEM,
526 /* This must be the last entry. Its purpose is to ensure the enum is
527 * wide enough to hold the higher bits reserved for bpf_type_flag.
529 __BPF_ARG_TYPE_LIMIT = BPF_TYPE_LIMIT,
531 static_assert(__BPF_ARG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
533 /* type of values returned from helper functions */
534 enum bpf_return_type {
535 RET_INTEGER, /* function returns integer */
536 RET_VOID, /* function doesn't return anything */
537 RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */
538 RET_PTR_TO_SOCKET, /* returns a pointer to a socket */
539 RET_PTR_TO_TCP_SOCK, /* returns a pointer to a tcp_sock */
540 RET_PTR_TO_SOCK_COMMON, /* returns a pointer to a sock_common */
541 RET_PTR_TO_ALLOC_MEM, /* returns a pointer to dynamically allocated memory */
542 RET_PTR_TO_MEM_OR_BTF_ID, /* returns a pointer to a valid memory or a btf_id */
543 RET_PTR_TO_BTF_ID, /* returns a pointer to a btf_id */
546 /* Extended ret_types. */
547 RET_PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_MAP_VALUE,
548 RET_PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCKET,
549 RET_PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_TCP_SOCK,
550 RET_PTR_TO_SOCK_COMMON_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCK_COMMON,
551 RET_PTR_TO_ALLOC_MEM_OR_NULL = PTR_MAYBE_NULL | MEM_ALLOC | RET_PTR_TO_ALLOC_MEM,
552 RET_PTR_TO_DYNPTR_MEM_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_ALLOC_MEM,
553 RET_PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_BTF_ID,
555 /* This must be the last entry. Its purpose is to ensure the enum is
556 * wide enough to hold the higher bits reserved for bpf_type_flag.
558 __BPF_RET_TYPE_LIMIT = BPF_TYPE_LIMIT,
560 static_assert(__BPF_RET_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
562 /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
563 * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
564 * instructions after verifying
566 struct bpf_func_proto {
567 u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
570 enum bpf_return_type ret_type;
573 enum bpf_arg_type arg1_type;
574 enum bpf_arg_type arg2_type;
575 enum bpf_arg_type arg3_type;
576 enum bpf_arg_type arg4_type;
577 enum bpf_arg_type arg5_type;
579 enum bpf_arg_type arg_type[5];
599 int *ret_btf_id; /* return value btf_id */
600 bool (*allowed)(const struct bpf_prog *prog);
603 /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
604 * the first argument to eBPF programs.
605 * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
609 enum bpf_access_type {
614 /* types of values stored in eBPF registers */
615 /* Pointer types represent:
618 * pointer + (u16) var
619 * pointer + (u16) var + imm
620 * if (range > 0) then [ptr, ptr + range - off) is safe to access
621 * if (id > 0) means that some 'var' was added
622 * if (off > 0) means that 'imm' was added
625 NOT_INIT = 0, /* nothing was written into register */
626 SCALAR_VALUE, /* reg doesn't contain a valid pointer */
627 PTR_TO_CTX, /* reg points to bpf_context */
628 CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
629 PTR_TO_MAP_VALUE, /* reg points to map element value */
630 PTR_TO_MAP_KEY, /* reg points to a map element key */
631 PTR_TO_STACK, /* reg == frame_pointer + offset */
632 PTR_TO_PACKET_META, /* skb->data - meta_len */
633 PTR_TO_PACKET, /* reg points to skb->data */
634 PTR_TO_PACKET_END, /* skb->data + headlen */
635 PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */
636 PTR_TO_SOCKET, /* reg points to struct bpf_sock */
637 PTR_TO_SOCK_COMMON, /* reg points to sock_common */
638 PTR_TO_TCP_SOCK, /* reg points to struct tcp_sock */
639 PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
640 PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
641 /* PTR_TO_BTF_ID points to a kernel struct that does not need
642 * to be null checked by the BPF program. This does not imply the
643 * pointer is _not_ null and in practice this can easily be a null
644 * pointer when reading pointer chains. The assumption is program
645 * context will handle null pointer dereference typically via fault
646 * handling. The verifier must keep this in mind and can make no
647 * assumptions about null or non-null when doing branch analysis.
648 * Further, when passed into helpers the helpers can not, without
649 * additional context, assume the value is non-null.
652 /* PTR_TO_BTF_ID_OR_NULL points to a kernel struct that has not
653 * been checked for null. Used primarily to inform the verifier
654 * an explicit null check is required for this struct.
656 PTR_TO_MEM, /* reg points to valid memory region */
657 PTR_TO_BUF, /* reg points to a read/write buffer */
658 PTR_TO_FUNC, /* reg points to a bpf program function */
659 PTR_TO_DYNPTR, /* reg points to a dynptr */
662 /* Extended reg_types. */
663 PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | PTR_TO_MAP_VALUE,
664 PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | PTR_TO_SOCKET,
665 PTR_TO_SOCK_COMMON_OR_NULL = PTR_MAYBE_NULL | PTR_TO_SOCK_COMMON,
666 PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | PTR_TO_TCP_SOCK,
667 PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | PTR_TO_BTF_ID,
669 /* This must be the last entry. Its purpose is to ensure the enum is
670 * wide enough to hold the higher bits reserved for bpf_type_flag.
672 __BPF_REG_TYPE_LIMIT = BPF_TYPE_LIMIT,
674 static_assert(__BPF_REG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
676 /* The information passed from prog-specific *_is_valid_access
677 * back to the verifier.
679 struct bpf_insn_access_aux {
680 enum bpf_reg_type reg_type;
688 struct bpf_verifier_log *log; /* for verbose logs */
692 bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
694 aux->ctx_field_size = size;
697 static inline bool bpf_pseudo_func(const struct bpf_insn *insn)
699 return insn->code == (BPF_LD | BPF_IMM | BPF_DW) &&
700 insn->src_reg == BPF_PSEUDO_FUNC;
703 struct bpf_prog_ops {
704 int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
705 union bpf_attr __user *uattr);
708 struct bpf_verifier_ops {
709 /* return eBPF function prototype for verification */
710 const struct bpf_func_proto *
711 (*get_func_proto)(enum bpf_func_id func_id,
712 const struct bpf_prog *prog);
714 /* return true if 'size' wide access at offset 'off' within bpf_context
715 * with 'type' (read or write) is allowed
717 bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
718 const struct bpf_prog *prog,
719 struct bpf_insn_access_aux *info);
720 int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
721 const struct bpf_prog *prog);
722 int (*gen_ld_abs)(const struct bpf_insn *orig,
723 struct bpf_insn *insn_buf);
724 u32 (*convert_ctx_access)(enum bpf_access_type type,
725 const struct bpf_insn *src,
726 struct bpf_insn *dst,
727 struct bpf_prog *prog, u32 *target_size);
728 int (*btf_struct_access)(struct bpf_verifier_log *log,
729 const struct btf *btf,
730 const struct btf_type *t, int off, int size,
731 enum bpf_access_type atype,
732 u32 *next_btf_id, enum bpf_type_flag *flag);
735 struct bpf_prog_offload_ops {
736 /* verifier basic callbacks */
737 int (*insn_hook)(struct bpf_verifier_env *env,
738 int insn_idx, int prev_insn_idx);
739 int (*finalize)(struct bpf_verifier_env *env);
740 /* verifier optimization callbacks (called after .finalize) */
741 int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
742 struct bpf_insn *insn);
743 int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
744 /* program management callbacks */
745 int (*prepare)(struct bpf_prog *prog);
746 int (*translate)(struct bpf_prog *prog);
747 void (*destroy)(struct bpf_prog *prog);
750 struct bpf_prog_offload {
751 struct bpf_prog *prog;
752 struct net_device *netdev;
753 struct bpf_offload_dev *offdev;
755 struct list_head offloads;
762 enum bpf_cgroup_storage_type {
763 BPF_CGROUP_STORAGE_SHARED,
764 BPF_CGROUP_STORAGE_PERCPU,
765 __BPF_CGROUP_STORAGE_MAX
768 #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
770 /* The longest tracepoint has 12 args.
771 * See include/trace/bpf_probe.h
773 #define MAX_BPF_FUNC_ARGS 12
775 /* The maximum number of arguments passed through registers
776 * a single function may have.
778 #define MAX_BPF_FUNC_REG_ARGS 5
780 /* The argument is a structure. */
781 #define BTF_FMODEL_STRUCT_ARG BIT(0)
783 struct btf_func_model {
786 u8 arg_size[MAX_BPF_FUNC_ARGS];
787 u8 arg_flags[MAX_BPF_FUNC_ARGS];
790 /* Restore arguments before returning from trampoline to let original function
791 * continue executing. This flag is used for fentry progs when there are no
794 #define BPF_TRAMP_F_RESTORE_REGS BIT(0)
795 /* Call original function after fentry progs, but before fexit progs.
796 * Makes sense for fentry/fexit, normal calls and indirect calls.
798 #define BPF_TRAMP_F_CALL_ORIG BIT(1)
799 /* Skip current frame and return to parent. Makes sense for fentry/fexit
800 * programs only. Should not be used with normal calls and indirect calls.
802 #define BPF_TRAMP_F_SKIP_FRAME BIT(2)
803 /* Store IP address of the caller on the trampoline stack,
804 * so it's available for trampoline's programs.
806 #define BPF_TRAMP_F_IP_ARG BIT(3)
807 /* Return the return value of fentry prog. Only used by bpf_struct_ops. */
808 #define BPF_TRAMP_F_RET_FENTRY_RET BIT(4)
810 /* Get original function from stack instead of from provided direct address.
811 * Makes sense for trampolines with fexit or fmod_ret programs.
813 #define BPF_TRAMP_F_ORIG_STACK BIT(5)
815 /* This trampoline is on a function with another ftrace_ops with IPMODIFY,
816 * e.g., a live patch. This flag is set and cleared by ftrace call backs,
818 #define BPF_TRAMP_F_SHARE_IPMODIFY BIT(6)
820 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
823 #define BPF_MAX_TRAMP_LINKS 38
825 struct bpf_tramp_links {
826 struct bpf_tramp_link *links[BPF_MAX_TRAMP_LINKS];
830 struct bpf_tramp_run_ctx;
832 /* Different use cases for BPF trampoline:
833 * 1. replace nop at the function entry (kprobe equivalent)
834 * flags = BPF_TRAMP_F_RESTORE_REGS
835 * fentry = a set of programs to run before returning from trampoline
837 * 2. replace nop at the function entry (kprobe + kretprobe equivalent)
838 * flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME
839 * orig_call = fentry_ip + MCOUNT_INSN_SIZE
840 * fentry = a set of program to run before calling original function
841 * fexit = a set of program to run after original function
843 * 3. replace direct call instruction anywhere in the function body
844 * or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid)
846 * fentry = a set of programs to run before returning from trampoline
847 * With flags = BPF_TRAMP_F_CALL_ORIG
848 * orig_call = original callback addr or direct function addr
849 * fentry = a set of program to run before calling original function
850 * fexit = a set of program to run after original function
852 struct bpf_tramp_image;
853 int arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end,
854 const struct btf_func_model *m, u32 flags,
855 struct bpf_tramp_links *tlinks,
857 /* these two functions are called from generated trampoline */
858 u64 notrace __bpf_prog_enter(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx);
859 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start, struct bpf_tramp_run_ctx *run_ctx);
860 u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx);
861 void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start,
862 struct bpf_tramp_run_ctx *run_ctx);
863 u64 notrace __bpf_prog_enter_lsm_cgroup(struct bpf_prog *prog,
864 struct bpf_tramp_run_ctx *run_ctx);
865 void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start,
866 struct bpf_tramp_run_ctx *run_ctx);
867 u64 notrace __bpf_prog_enter_struct_ops(struct bpf_prog *prog,
868 struct bpf_tramp_run_ctx *run_ctx);
869 void notrace __bpf_prog_exit_struct_ops(struct bpf_prog *prog, u64 start,
870 struct bpf_tramp_run_ctx *run_ctx);
871 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr);
872 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr);
877 char name[KSYM_NAME_LEN];
878 struct list_head lnode;
879 struct latch_tree_node tnode;
883 enum bpf_tramp_prog_type {
886 BPF_TRAMP_MODIFY_RETURN,
888 BPF_TRAMP_REPLACE, /* more than MAX */
891 struct bpf_tramp_image {
893 struct bpf_ksym ksym;
894 struct percpu_ref pcref;
899 struct work_struct work;
903 struct bpf_trampoline {
904 /* hlist for trampoline_table */
905 struct hlist_node hlist;
906 struct ftrace_ops *fops;
907 /* serializes access to fields of this trampoline */
913 struct btf_func_model model;
917 /* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF
918 * program by replacing one of its functions. func.addr is the address
919 * of the function it replaced.
921 struct bpf_prog *extension_prog;
922 /* list of BPF programs using this trampoline */
923 struct hlist_head progs_hlist[BPF_TRAMP_MAX];
924 /* Number of attached programs. A counter per kind. */
925 int progs_cnt[BPF_TRAMP_MAX];
926 /* Executable image of trampoline */
927 struct bpf_tramp_image *cur_image;
932 struct bpf_attach_target_info {
933 struct btf_func_model fmodel;
935 const char *tgt_name;
936 const struct btf_type *tgt_type;
939 #define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
941 struct bpf_dispatcher_prog {
942 struct bpf_prog *prog;
946 struct bpf_dispatcher {
947 /* dispatcher mutex */
950 struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX];
955 struct bpf_ksym ksym;
958 static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(
960 const struct bpf_insn *insnsi,
963 return bpf_func(ctx, insnsi);
966 #ifdef CONFIG_BPF_JIT
967 int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
968 int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
969 struct bpf_trampoline *bpf_trampoline_get(u64 key,
970 struct bpf_attach_target_info *tgt_info);
971 void bpf_trampoline_put(struct bpf_trampoline *tr);
972 int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_funcs);
973 #define BPF_DISPATCHER_INIT(_name) { \
974 .mutex = __MUTEX_INITIALIZER(_name.mutex), \
975 .func = &_name##_func, \
982 .lnode = LIST_HEAD_INIT(_name.ksym.lnode), \
987 #define BPF_DISPATCHER_ATTRIBUTES __attribute__((patchable_function_entry(5)))
989 #define BPF_DISPATCHER_ATTRIBUTES
992 #define DEFINE_BPF_DISPATCHER(name) \
993 notrace BPF_DISPATCHER_ATTRIBUTES \
994 noinline __nocfi unsigned int bpf_dispatcher_##name##_func( \
996 const struct bpf_insn *insnsi, \
997 bpf_func_t bpf_func) \
999 return bpf_func(ctx, insnsi); \
1001 EXPORT_SYMBOL(bpf_dispatcher_##name##_func); \
1002 struct bpf_dispatcher bpf_dispatcher_##name = \
1003 BPF_DISPATCHER_INIT(bpf_dispatcher_##name);
1004 #define DECLARE_BPF_DISPATCHER(name) \
1005 unsigned int bpf_dispatcher_##name##_func( \
1007 const struct bpf_insn *insnsi, \
1008 bpf_func_t bpf_func); \
1009 extern struct bpf_dispatcher bpf_dispatcher_##name;
1010 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func
1011 #define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
1012 void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
1013 struct bpf_prog *to);
1014 /* Called only from JIT-enabled code, so there's no need for stubs. */
1015 void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
1016 void bpf_image_ksym_del(struct bpf_ksym *ksym);
1017 void bpf_ksym_add(struct bpf_ksym *ksym);
1018 void bpf_ksym_del(struct bpf_ksym *ksym);
1019 int bpf_jit_charge_modmem(u32 size);
1020 void bpf_jit_uncharge_modmem(u32 size);
1021 bool bpf_prog_has_trampoline(const struct bpf_prog *prog);
1023 static inline int bpf_trampoline_link_prog(struct bpf_tramp_link *link,
1024 struct bpf_trampoline *tr)
1028 static inline int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link,
1029 struct bpf_trampoline *tr)
1033 static inline struct bpf_trampoline *bpf_trampoline_get(u64 key,
1034 struct bpf_attach_target_info *tgt_info)
1036 return ERR_PTR(-EOPNOTSUPP);
1038 static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
1039 #define DEFINE_BPF_DISPATCHER(name)
1040 #define DECLARE_BPF_DISPATCHER(name)
1041 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nop_func
1042 #define BPF_DISPATCHER_PTR(name) NULL
1043 static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
1044 struct bpf_prog *from,
1045 struct bpf_prog *to) {}
1046 static inline bool is_bpf_image_address(unsigned long address)
1050 static inline bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
1056 struct bpf_func_info_aux {
1061 enum bpf_jit_poke_reason {
1062 BPF_POKE_REASON_TAIL_CALL,
1065 /* Descriptor of pokes pointing /into/ the JITed image. */
1066 struct bpf_jit_poke_descriptor {
1067 void *tailcall_target;
1068 void *tailcall_bypass;
1073 struct bpf_map *map;
1077 bool tailcall_target_stable;
1083 /* reg_type info for ctx arguments */
1084 struct bpf_ctx_arg_aux {
1086 enum bpf_reg_type reg_type;
1090 struct btf_mod_pair {
1092 struct module *module;
1095 struct bpf_kfunc_desc_tab;
1097 struct bpf_prog_aux {
1106 u32 func_cnt; /* used by non-func prog as the number of func progs */
1107 u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
1108 u32 attach_btf_id; /* in-kernel BTF type id to attach to */
1109 u32 ctx_arg_info_size;
1110 u32 max_rdonly_access;
1111 u32 max_rdwr_access;
1112 struct btf *attach_btf;
1113 const struct bpf_ctx_arg_aux *ctx_arg_info;
1114 struct mutex dst_mutex; /* protects dst_* pointers below, *after* prog becomes visible */
1115 struct bpf_prog *dst_prog;
1116 struct bpf_trampoline *dst_trampoline;
1117 enum bpf_prog_type saved_dst_prog_type;
1118 enum bpf_attach_type saved_dst_attach_type;
1119 bool verifier_zext; /* Zero extensions has been inserted by verifier. */
1120 bool offload_requested;
1121 bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
1122 bool func_proto_unreliable;
1124 bool tail_call_reachable;
1126 /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
1127 const struct btf_type *attach_func_proto;
1128 /* function name for valid attach_btf_id */
1129 const char *attach_func_name;
1130 struct bpf_prog **func;
1131 void *jit_data; /* JIT specific data. arch dependent */
1132 struct bpf_jit_poke_descriptor *poke_tab;
1133 struct bpf_kfunc_desc_tab *kfunc_tab;
1134 struct bpf_kfunc_btf_tab *kfunc_btf_tab;
1136 struct bpf_ksym ksym;
1137 const struct bpf_prog_ops *ops;
1138 struct bpf_map **used_maps;
1139 struct mutex used_maps_mutex; /* mutex for used_maps and used_map_cnt */
1140 struct btf_mod_pair *used_btfs;
1141 struct bpf_prog *prog;
1142 struct user_struct *user;
1143 u64 load_time; /* ns since boottime */
1145 int cgroup_atype; /* enum cgroup_bpf_attach_type */
1146 struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1147 char name[BPF_OBJ_NAME_LEN];
1148 #ifdef CONFIG_SECURITY
1151 struct bpf_prog_offload *offload;
1153 struct bpf_func_info *func_info;
1154 struct bpf_func_info_aux *func_info_aux;
1155 /* bpf_line_info loaded from userspace. linfo->insn_off
1156 * has the xlated insn offset.
1157 * Both the main and sub prog share the same linfo.
1158 * The subprog can access its first linfo by
1159 * using the linfo_idx.
1161 struct bpf_line_info *linfo;
1162 /* jited_linfo is the jited addr of the linfo. It has a
1163 * one to one mapping to linfo:
1164 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
1165 * Both the main and sub prog share the same jited_linfo.
1166 * The subprog can access its first jited_linfo by
1167 * using the linfo_idx.
1172 /* subprog can use linfo_idx to access its first linfo and
1174 * main prog always has linfo_idx == 0
1178 struct exception_table_entry *extable;
1180 struct work_struct work;
1181 struct rcu_head rcu;
1186 u16 pages; /* Number of allocated pages */
1187 u16 jited:1, /* Is our filter JIT'ed? */
1188 jit_requested:1,/* archs need to JIT the prog */
1189 gpl_compatible:1, /* Is filter GPL compatible? */
1190 cb_access:1, /* Is control block accessed? */
1191 dst_needed:1, /* Do we need dst entry? */
1192 blinding_requested:1, /* needs constant blinding */
1193 blinded:1, /* Was blinded */
1194 is_func:1, /* program is a bpf function */
1195 kprobe_override:1, /* Do we override a kprobe? */
1196 has_callchain_buf:1, /* callchain buffer allocated? */
1197 enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */
1198 call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */
1199 call_get_func_ip:1, /* Do we call get_func_ip() */
1200 tstamp_type_access:1; /* Accessed __sk_buff->tstamp_type */
1201 enum bpf_prog_type type; /* Type of BPF program */
1202 enum bpf_attach_type expected_attach_type; /* For some prog types */
1203 u32 len; /* Number of filter blocks */
1204 u32 jited_len; /* Size of jited insns in bytes */
1205 u8 tag[BPF_TAG_SIZE];
1206 struct bpf_prog_stats __percpu *stats;
1207 int __percpu *active;
1208 unsigned int (*bpf_func)(const void *ctx,
1209 const struct bpf_insn *insn);
1210 struct bpf_prog_aux *aux; /* Auxiliary fields */
1211 struct sock_fprog_kern *orig_prog; /* Original BPF program */
1212 /* Instructions for interpreter */
1214 DECLARE_FLEX_ARRAY(struct sock_filter, insns);
1215 DECLARE_FLEX_ARRAY(struct bpf_insn, insnsi);
1219 struct bpf_array_aux {
1220 /* Programs with direct jumps into programs part of this array. */
1221 struct list_head poke_progs;
1222 struct bpf_map *map;
1223 struct mutex poke_mutex;
1224 struct work_struct work;
1230 enum bpf_link_type type;
1231 const struct bpf_link_ops *ops;
1232 struct bpf_prog *prog;
1233 struct work_struct work;
1236 struct bpf_link_ops {
1237 void (*release)(struct bpf_link *link);
1238 void (*dealloc)(struct bpf_link *link);
1239 int (*detach)(struct bpf_link *link);
1240 int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
1241 struct bpf_prog *old_prog);
1242 void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);
1243 int (*fill_link_info)(const struct bpf_link *link,
1244 struct bpf_link_info *info);
1247 struct bpf_tramp_link {
1248 struct bpf_link link;
1249 struct hlist_node tramp_hlist;
1253 struct bpf_shim_tramp_link {
1254 struct bpf_tramp_link link;
1255 struct bpf_trampoline *trampoline;
1258 struct bpf_tracing_link {
1259 struct bpf_tramp_link link;
1260 enum bpf_attach_type attach_type;
1261 struct bpf_trampoline *trampoline;
1262 struct bpf_prog *tgt_prog;
1265 struct bpf_link_primer {
1266 struct bpf_link *link;
1272 struct bpf_struct_ops_value;
1275 #define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
1276 struct bpf_struct_ops {
1277 const struct bpf_verifier_ops *verifier_ops;
1278 int (*init)(struct btf *btf);
1279 int (*check_member)(const struct btf_type *t,
1280 const struct btf_member *member);
1281 int (*init_member)(const struct btf_type *t,
1282 const struct btf_member *member,
1283 void *kdata, const void *udata);
1284 int (*reg)(void *kdata);
1285 void (*unreg)(void *kdata);
1286 const struct btf_type *type;
1287 const struct btf_type *value_type;
1289 struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
1294 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
1295 #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
1296 const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
1297 void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
1298 bool bpf_struct_ops_get(const void *kdata);
1299 void bpf_struct_ops_put(const void *kdata);
1300 int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
1302 int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks,
1303 struct bpf_tramp_link *link,
1304 const struct btf_func_model *model,
1305 void *image, void *image_end);
1306 static inline bool bpf_try_module_get(const void *data, struct module *owner)
1308 if (owner == BPF_MODULE_OWNER)
1309 return bpf_struct_ops_get(data);
1311 return try_module_get(owner);
1313 static inline void bpf_module_put(const void *data, struct module *owner)
1315 if (owner == BPF_MODULE_OWNER)
1316 bpf_struct_ops_put(data);
1322 /* Define it here to avoid the use of forward declaration */
1323 struct bpf_dummy_ops_state {
1327 struct bpf_dummy_ops {
1328 int (*test_1)(struct bpf_dummy_ops_state *cb);
1329 int (*test_2)(struct bpf_dummy_ops_state *cb, int a1, unsigned short a2,
1330 char a3, unsigned long a4);
1333 int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
1334 union bpf_attr __user *uattr);
1337 static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
1341 static inline void bpf_struct_ops_init(struct btf *btf,
1342 struct bpf_verifier_log *log)
1345 static inline bool bpf_try_module_get(const void *data, struct module *owner)
1347 return try_module_get(owner);
1349 static inline void bpf_module_put(const void *data, struct module *owner)
1353 static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
1361 #if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM)
1362 int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
1364 void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog);
1366 static inline int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
1371 static inline void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog)
1380 struct bpf_array_aux *aux;
1382 char value[0] __aligned(8);
1383 void *ptrs[0] __aligned(8);
1384 void __percpu *pptrs[0] __aligned(8);
1388 #define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */
1389 #define MAX_TAIL_CALL_CNT 33
1391 /* Maximum number of loops for bpf_loop */
1392 #define BPF_MAX_LOOPS BIT(23)
1394 #define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \
1395 BPF_F_RDONLY_PROG | \
1399 #define BPF_MAP_CAN_READ BIT(0)
1400 #define BPF_MAP_CAN_WRITE BIT(1)
1402 /* Maximum number of user-producer ring buffer samples that can be drained in
1403 * a call to bpf_user_ringbuf_drain().
1405 #define BPF_MAX_USER_RINGBUF_SAMPLES (128 * 1024)
1407 static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
1409 u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1411 /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
1414 if (access_flags & BPF_F_RDONLY_PROG)
1415 return BPF_MAP_CAN_READ;
1416 else if (access_flags & BPF_F_WRONLY_PROG)
1417 return BPF_MAP_CAN_WRITE;
1419 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
1422 static inline bool bpf_map_flags_access_ok(u32 access_flags)
1424 return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
1425 (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1428 struct bpf_event_entry {
1429 struct perf_event *event;
1430 struct file *perf_file;
1431 struct file *map_file;
1432 struct rcu_head rcu;
1435 static inline bool map_type_contains_progs(struct bpf_map *map)
1437 return map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
1438 map->map_type == BPF_MAP_TYPE_DEVMAP ||
1439 map->map_type == BPF_MAP_TYPE_CPUMAP;
1442 bool bpf_prog_map_compatible(struct bpf_map *map, const struct bpf_prog *fp);
1443 int bpf_prog_calc_tag(struct bpf_prog *fp);
1445 const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
1446 const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void);
1448 typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
1449 unsigned long off, unsigned long len);
1450 typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
1451 const struct bpf_insn *src,
1452 struct bpf_insn *dst,
1453 struct bpf_prog *prog,
1456 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1457 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
1459 /* an array of programs to be executed under rcu_lock.
1462 * ret = bpf_prog_run_array(rcu_dereference(&bpf_prog_array), ctx, bpf_prog_run);
1464 * the structure returned by bpf_prog_array_alloc() should be populated
1465 * with program pointers and the last pointer must be NULL.
1466 * The user has to keep refcnt on the program and make sure the program
1467 * is removed from the array before bpf_prog_put().
1468 * The 'struct bpf_prog_array *' should only be replaced with xchg()
1469 * since other cpus are walking the array of pointers in parallel.
1471 struct bpf_prog_array_item {
1472 struct bpf_prog *prog;
1474 struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1479 struct bpf_prog_array {
1480 struct rcu_head rcu;
1481 struct bpf_prog_array_item items[];
1484 struct bpf_empty_prog_array {
1485 struct bpf_prog_array hdr;
1486 struct bpf_prog *null_prog;
1489 /* to avoid allocating empty bpf_prog_array for cgroups that
1490 * don't have bpf program attached use one global 'bpf_empty_prog_array'
1491 * It will not be modified the caller of bpf_prog_array_alloc()
1492 * (since caller requested prog_cnt == 0)
1493 * that pointer should be 'freed' by bpf_prog_array_free()
1495 extern struct bpf_empty_prog_array bpf_empty_prog_array;
1497 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
1498 void bpf_prog_array_free(struct bpf_prog_array *progs);
1499 /* Use when traversal over the bpf_prog_array uses tasks_trace rcu */
1500 void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs);
1501 int bpf_prog_array_length(struct bpf_prog_array *progs);
1502 bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
1503 int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
1504 __u32 __user *prog_ids, u32 cnt);
1506 void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
1507 struct bpf_prog *old_prog);
1508 int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index);
1509 int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
1510 struct bpf_prog *prog);
1511 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
1512 u32 *prog_ids, u32 request_cnt,
1514 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
1515 struct bpf_prog *exclude_prog,
1516 struct bpf_prog *include_prog,
1518 struct bpf_prog_array **new_array);
1520 struct bpf_run_ctx {};
1522 struct bpf_cg_run_ctx {
1523 struct bpf_run_ctx run_ctx;
1524 const struct bpf_prog_array_item *prog_item;
1528 struct bpf_trace_run_ctx {
1529 struct bpf_run_ctx run_ctx;
1533 struct bpf_tramp_run_ctx {
1534 struct bpf_run_ctx run_ctx;
1536 struct bpf_run_ctx *saved_run_ctx;
1539 static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
1541 struct bpf_run_ctx *old_ctx = NULL;
1543 #ifdef CONFIG_BPF_SYSCALL
1544 old_ctx = current->bpf_ctx;
1545 current->bpf_ctx = new_ctx;
1550 static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)
1552 #ifdef CONFIG_BPF_SYSCALL
1553 current->bpf_ctx = old_ctx;
1557 /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
1558 #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE (1 << 0)
1559 /* BPF program asks to set CN on the packet. */
1560 #define BPF_RET_SET_CN (1 << 0)
1562 typedef u32 (*bpf_prog_run_fn)(const struct bpf_prog *prog, const void *ctx);
1564 static __always_inline u32
1565 bpf_prog_run_array(const struct bpf_prog_array *array,
1566 const void *ctx, bpf_prog_run_fn run_prog)
1568 const struct bpf_prog_array_item *item;
1569 const struct bpf_prog *prog;
1570 struct bpf_run_ctx *old_run_ctx;
1571 struct bpf_trace_run_ctx run_ctx;
1574 RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held");
1576 if (unlikely(!array))
1580 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
1581 item = &array->items[0];
1582 while ((prog = READ_ONCE(item->prog))) {
1583 run_ctx.bpf_cookie = item->bpf_cookie;
1584 ret &= run_prog(prog, ctx);
1587 bpf_reset_run_ctx(old_run_ctx);
1592 /* Notes on RCU design for bpf_prog_arrays containing sleepable programs:
1594 * We use the tasks_trace rcu flavor read section to protect the bpf_prog_array
1595 * overall. As a result, we must use the bpf_prog_array_free_sleepable
1596 * in order to use the tasks_trace rcu grace period.
1598 * When a non-sleepable program is inside the array, we take the rcu read
1599 * section and disable preemption for that program alone, so it can access
1600 * rcu-protected dynamically sized maps.
1602 static __always_inline u32
1603 bpf_prog_run_array_sleepable(const struct bpf_prog_array __rcu *array_rcu,
1604 const void *ctx, bpf_prog_run_fn run_prog)
1606 const struct bpf_prog_array_item *item;
1607 const struct bpf_prog *prog;
1608 const struct bpf_prog_array *array;
1609 struct bpf_run_ctx *old_run_ctx;
1610 struct bpf_trace_run_ctx run_ctx;
1615 rcu_read_lock_trace();
1618 array = rcu_dereference_check(array_rcu, rcu_read_lock_trace_held());
1619 if (unlikely(!array))
1621 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
1622 item = &array->items[0];
1623 while ((prog = READ_ONCE(item->prog))) {
1624 if (!prog->aux->sleepable)
1627 run_ctx.bpf_cookie = item->bpf_cookie;
1628 ret &= run_prog(prog, ctx);
1631 if (!prog->aux->sleepable)
1634 bpf_reset_run_ctx(old_run_ctx);
1637 rcu_read_unlock_trace();
1641 #ifdef CONFIG_BPF_SYSCALL
1642 DECLARE_PER_CPU(int, bpf_prog_active);
1643 extern struct mutex bpf_stats_enabled_mutex;
1646 * Block execution of BPF programs attached to instrumentation (perf,
1647 * kprobes, tracepoints) to prevent deadlocks on map operations as any of
1648 * these events can happen inside a region which holds a map bucket lock
1649 * and can deadlock on it.
1651 static inline void bpf_disable_instrumentation(void)
1654 this_cpu_inc(bpf_prog_active);
1657 static inline void bpf_enable_instrumentation(void)
1659 this_cpu_dec(bpf_prog_active);
1663 extern const struct file_operations bpf_map_fops;
1664 extern const struct file_operations bpf_prog_fops;
1665 extern const struct file_operations bpf_iter_fops;
1667 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1668 extern const struct bpf_prog_ops _name ## _prog_ops; \
1669 extern const struct bpf_verifier_ops _name ## _verifier_ops;
1670 #define BPF_MAP_TYPE(_id, _ops) \
1671 extern const struct bpf_map_ops _ops;
1672 #define BPF_LINK_TYPE(_id, _name)
1673 #include <linux/bpf_types.h>
1674 #undef BPF_PROG_TYPE
1676 #undef BPF_LINK_TYPE
1678 extern const struct bpf_prog_ops bpf_offload_prog_ops;
1679 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
1680 extern const struct bpf_verifier_ops xdp_analyzer_ops;
1682 struct bpf_prog *bpf_prog_get(u32 ufd);
1683 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1685 void bpf_prog_add(struct bpf_prog *prog, int i);
1686 void bpf_prog_sub(struct bpf_prog *prog, int i);
1687 void bpf_prog_inc(struct bpf_prog *prog);
1688 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
1689 void bpf_prog_put(struct bpf_prog *prog);
1691 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
1692 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
1694 struct bpf_map_value_off_desc *bpf_map_kptr_off_contains(struct bpf_map *map, u32 offset);
1695 void bpf_map_free_kptr_off_tab(struct bpf_map *map);
1696 struct bpf_map_value_off *bpf_map_copy_kptr_off_tab(const struct bpf_map *map);
1697 bool bpf_map_equal_kptr_off_tab(const struct bpf_map *map_a, const struct bpf_map *map_b);
1698 void bpf_map_free_kptrs(struct bpf_map *map, void *map_value);
1700 struct bpf_map *bpf_map_get(u32 ufd);
1701 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
1702 struct bpf_map *__bpf_map_get(struct fd f);
1703 void bpf_map_inc(struct bpf_map *map);
1704 void bpf_map_inc_with_uref(struct bpf_map *map);
1705 struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
1706 void bpf_map_put_with_uref(struct bpf_map *map);
1707 void bpf_map_put(struct bpf_map *map);
1708 void *bpf_map_area_alloc(u64 size, int numa_node);
1709 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
1710 void bpf_map_area_free(void *base);
1711 bool bpf_map_write_active(const struct bpf_map *map);
1712 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
1713 int generic_map_lookup_batch(struct bpf_map *map,
1714 const union bpf_attr *attr,
1715 union bpf_attr __user *uattr);
1716 int generic_map_update_batch(struct bpf_map *map,
1717 const union bpf_attr *attr,
1718 union bpf_attr __user *uattr);
1719 int generic_map_delete_batch(struct bpf_map *map,
1720 const union bpf_attr *attr,
1721 union bpf_attr __user *uattr);
1722 struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
1723 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id);
1725 #ifdef CONFIG_MEMCG_KMEM
1726 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1728 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags);
1729 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
1730 size_t align, gfp_t flags);
1732 static inline void *
1733 bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1736 return kmalloc_node(size, flags, node);
1739 static inline void *
1740 bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
1742 return kzalloc(size, flags);
1745 static inline void __percpu *
1746 bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align,
1749 return __alloc_percpu_gfp(size, align, flags);
1753 extern int sysctl_unprivileged_bpf_disabled;
1755 static inline bool bpf_allow_ptr_leaks(void)
1757 return perfmon_capable();
1760 static inline bool bpf_allow_uninit_stack(void)
1762 return perfmon_capable();
1765 static inline bool bpf_allow_ptr_to_map_access(void)
1767 return perfmon_capable();
1770 static inline bool bpf_bypass_spec_v1(void)
1772 return perfmon_capable();
1775 static inline bool bpf_bypass_spec_v4(void)
1777 return perfmon_capable();
1780 int bpf_map_new_fd(struct bpf_map *map, int flags);
1781 int bpf_prog_new_fd(struct bpf_prog *prog);
1783 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
1784 const struct bpf_link_ops *ops, struct bpf_prog *prog);
1785 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
1786 int bpf_link_settle(struct bpf_link_primer *primer);
1787 void bpf_link_cleanup(struct bpf_link_primer *primer);
1788 void bpf_link_inc(struct bpf_link *link);
1789 void bpf_link_put(struct bpf_link *link);
1790 int bpf_link_new_fd(struct bpf_link *link);
1791 struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
1792 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
1793 struct bpf_link *bpf_link_get_curr_or_next(u32 *id);
1795 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
1796 int bpf_obj_get_user(const char __user *pathname, int flags);
1798 #define BPF_ITER_FUNC_PREFIX "bpf_iter_"
1799 #define DEFINE_BPF_ITER_FUNC(target, args...) \
1800 extern int bpf_iter_ ## target(args); \
1801 int __init bpf_iter_ ## target(args) { return 0; }
1804 * The task type of iterators.
1806 * For BPF task iterators, they can be parameterized with various
1807 * parameters to visit only some of tasks.
1809 * BPF_TASK_ITER_ALL (default)
1810 * Iterate over resources of every task.
1813 * Iterate over resources of a task/tid.
1815 * BPF_TASK_ITER_TGID
1816 * Iterate over resources of every task of a process / task group.
1818 enum bpf_iter_task_type {
1819 BPF_TASK_ITER_ALL = 0,
1824 struct bpf_iter_aux_info {
1825 /* for map_elem iter */
1826 struct bpf_map *map;
1828 /* for cgroup iter */
1830 struct cgroup *start; /* starting cgroup */
1831 enum bpf_cgroup_iter_order order;
1834 enum bpf_iter_task_type type;
1839 typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
1840 union bpf_iter_link_info *linfo,
1841 struct bpf_iter_aux_info *aux);
1842 typedef void (*bpf_iter_detach_target_t)(struct bpf_iter_aux_info *aux);
1843 typedef void (*bpf_iter_show_fdinfo_t) (const struct bpf_iter_aux_info *aux,
1844 struct seq_file *seq);
1845 typedef int (*bpf_iter_fill_link_info_t)(const struct bpf_iter_aux_info *aux,
1846 struct bpf_link_info *info);
1847 typedef const struct bpf_func_proto *
1848 (*bpf_iter_get_func_proto_t)(enum bpf_func_id func_id,
1849 const struct bpf_prog *prog);
1851 enum bpf_iter_feature {
1852 BPF_ITER_RESCHED = BIT(0),
1855 #define BPF_ITER_CTX_ARG_MAX 2
1856 struct bpf_iter_reg {
1858 bpf_iter_attach_target_t attach_target;
1859 bpf_iter_detach_target_t detach_target;
1860 bpf_iter_show_fdinfo_t show_fdinfo;
1861 bpf_iter_fill_link_info_t fill_link_info;
1862 bpf_iter_get_func_proto_t get_func_proto;
1863 u32 ctx_arg_info_size;
1865 struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
1866 const struct bpf_iter_seq_info *seq_info;
1869 struct bpf_iter_meta {
1870 __bpf_md_ptr(struct seq_file *, seq);
1875 struct bpf_iter__bpf_map_elem {
1876 __bpf_md_ptr(struct bpf_iter_meta *, meta);
1877 __bpf_md_ptr(struct bpf_map *, map);
1878 __bpf_md_ptr(void *, key);
1879 __bpf_md_ptr(void *, value);
1882 int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
1883 void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
1884 bool bpf_iter_prog_supported(struct bpf_prog *prog);
1885 const struct bpf_func_proto *
1886 bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
1887 int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_prog *prog);
1888 int bpf_iter_new_fd(struct bpf_link *link);
1889 bool bpf_link_is_iter(struct bpf_link *link);
1890 struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
1891 int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx);
1892 void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
1893 struct seq_file *seq);
1894 int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
1895 struct bpf_link_info *info);
1897 int map_set_for_each_callback_args(struct bpf_verifier_env *env,
1898 struct bpf_func_state *caller,
1899 struct bpf_func_state *callee);
1901 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
1902 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
1903 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1905 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
1908 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
1910 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
1911 void *key, void *value, u64 map_flags);
1912 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1913 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1914 void *key, void *value, u64 map_flags);
1915 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1917 int bpf_get_file_flag(int flags);
1918 int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size,
1919 size_t actual_size);
1921 /* verify correctness of eBPF program */
1922 int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr);
1924 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1925 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
1928 struct btf *bpf_get_btf_vmlinux(void);
1933 struct bpf_dtab_netdev;
1934 struct bpf_cpu_map_entry;
1936 void __dev_flush(void);
1937 int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
1938 struct net_device *dev_rx);
1939 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
1940 struct net_device *dev_rx);
1941 int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
1942 struct bpf_map *map, bool exclude_ingress);
1943 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
1944 struct bpf_prog *xdp_prog);
1945 int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
1946 struct bpf_prog *xdp_prog, struct bpf_map *map,
1947 bool exclude_ingress);
1949 void __cpu_map_flush(void);
1950 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf,
1951 struct net_device *dev_rx);
1952 int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
1953 struct sk_buff *skb);
1955 /* Return map's numa specified by userspace */
1956 static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
1958 return (attr->map_flags & BPF_F_NUMA_NODE) ?
1959 attr->numa_node : NUMA_NO_NODE;
1962 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
1963 int array_map_alloc_check(union bpf_attr *attr);
1965 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1966 union bpf_attr __user *uattr);
1967 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1968 union bpf_attr __user *uattr);
1969 int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1970 const union bpf_attr *kattr,
1971 union bpf_attr __user *uattr);
1972 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1973 const union bpf_attr *kattr,
1974 union bpf_attr __user *uattr);
1975 int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
1976 const union bpf_attr *kattr,
1977 union bpf_attr __user *uattr);
1978 int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
1979 const union bpf_attr *kattr,
1980 union bpf_attr __user *uattr);
1981 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
1982 const struct bpf_prog *prog,
1983 struct bpf_insn_access_aux *info);
1985 static inline bool bpf_tracing_ctx_access(int off, int size,
1986 enum bpf_access_type type)
1988 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1990 if (type != BPF_READ)
1992 if (off % size != 0)
1997 static inline bool bpf_tracing_btf_ctx_access(int off, int size,
1998 enum bpf_access_type type,
1999 const struct bpf_prog *prog,
2000 struct bpf_insn_access_aux *info)
2002 if (!bpf_tracing_ctx_access(off, size, type))
2004 return btf_ctx_access(off, size, type, prog, info);
2007 int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf,
2008 const struct btf_type *t, int off, int size,
2009 enum bpf_access_type atype,
2010 u32 *next_btf_id, enum bpf_type_flag *flag);
2011 bool btf_struct_ids_match(struct bpf_verifier_log *log,
2012 const struct btf *btf, u32 id, int off,
2013 const struct btf *need_btf, u32 need_type_id,
2016 int btf_distill_func_proto(struct bpf_verifier_log *log,
2018 const struct btf_type *func_proto,
2019 const char *func_name,
2020 struct btf_func_model *m);
2022 struct bpf_kfunc_arg_meta {
2029 struct bpf_reg_state;
2030 int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
2031 struct bpf_reg_state *regs);
2032 int btf_check_subprog_call(struct bpf_verifier_env *env, int subprog,
2033 struct bpf_reg_state *regs);
2034 int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
2035 const struct btf *btf, u32 func_id,
2036 struct bpf_reg_state *regs,
2037 struct bpf_kfunc_arg_meta *meta);
2038 int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
2039 struct bpf_reg_state *reg);
2040 int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
2041 struct btf *btf, const struct btf_type *t);
2043 struct bpf_prog *bpf_prog_by_id(u32 id);
2044 struct bpf_link *bpf_link_by_id(u32 id);
2046 const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
2047 void bpf_task_storage_free(struct task_struct *task);
2048 bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);
2049 const struct btf_func_model *
2050 bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
2051 const struct bpf_insn *insn);
2052 struct bpf_core_ctx {
2053 struct bpf_verifier_log *log;
2054 const struct btf *btf;
2057 int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
2058 int relo_idx, void *insn);
2060 static inline bool unprivileged_ebpf_enabled(void)
2062 return !sysctl_unprivileged_bpf_disabled;
2065 /* Not all bpf prog type has the bpf_ctx.
2066 * For the bpf prog type that has initialized the bpf_ctx,
2067 * this function can be used to decide if a kernel function
2068 * is called by a bpf program.
2070 static inline bool has_current_bpf_ctx(void)
2072 return !!current->bpf_ctx;
2075 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
2076 #else /* !CONFIG_BPF_SYSCALL */
2077 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
2079 return ERR_PTR(-EOPNOTSUPP);
2082 static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
2083 enum bpf_prog_type type,
2086 return ERR_PTR(-EOPNOTSUPP);
2089 static inline void bpf_prog_add(struct bpf_prog *prog, int i)
2093 static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
2097 static inline void bpf_prog_put(struct bpf_prog *prog)
2101 static inline void bpf_prog_inc(struct bpf_prog *prog)
2105 static inline struct bpf_prog *__must_check
2106 bpf_prog_inc_not_zero(struct bpf_prog *prog)
2108 return ERR_PTR(-EOPNOTSUPP);
2111 static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2112 const struct bpf_link_ops *ops,
2113 struct bpf_prog *prog)
2117 static inline int bpf_link_prime(struct bpf_link *link,
2118 struct bpf_link_primer *primer)
2123 static inline int bpf_link_settle(struct bpf_link_primer *primer)
2128 static inline void bpf_link_cleanup(struct bpf_link_primer *primer)
2132 static inline void bpf_link_inc(struct bpf_link *link)
2136 static inline void bpf_link_put(struct bpf_link *link)
2140 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
2145 static inline void __dev_flush(void)
2150 struct bpf_dtab_netdev;
2151 struct bpf_cpu_map_entry;
2154 int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
2155 struct net_device *dev_rx)
2161 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
2162 struct net_device *dev_rx)
2168 int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
2169 struct bpf_map *map, bool exclude_ingress)
2176 static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
2177 struct sk_buff *skb,
2178 struct bpf_prog *xdp_prog)
2184 int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
2185 struct bpf_prog *xdp_prog, struct bpf_map *map,
2186 bool exclude_ingress)
2191 static inline void __cpu_map_flush(void)
2195 static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
2196 struct xdp_frame *xdpf,
2197 struct net_device *dev_rx)
2202 static inline int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
2203 struct sk_buff *skb)
2208 static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
2209 enum bpf_prog_type type)
2211 return ERR_PTR(-EOPNOTSUPP);
2214 static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
2215 const union bpf_attr *kattr,
2216 union bpf_attr __user *uattr)
2221 static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
2222 const union bpf_attr *kattr,
2223 union bpf_attr __user *uattr)
2228 static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
2229 const union bpf_attr *kattr,
2230 union bpf_attr __user *uattr)
2235 static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
2236 const union bpf_attr *kattr,
2237 union bpf_attr __user *uattr)
2242 static inline int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
2243 const union bpf_attr *kattr,
2244 union bpf_attr __user *uattr)
2249 static inline void bpf_map_put(struct bpf_map *map)
2253 static inline struct bpf_prog *bpf_prog_by_id(u32 id)
2255 return ERR_PTR(-ENOTSUPP);
2258 static inline int btf_struct_access(struct bpf_verifier_log *log,
2259 const struct btf *btf,
2260 const struct btf_type *t, int off, int size,
2261 enum bpf_access_type atype,
2262 u32 *next_btf_id, enum bpf_type_flag *flag)
2267 static inline const struct bpf_func_proto *
2268 bpf_base_func_proto(enum bpf_func_id func_id)
2273 static inline void bpf_task_storage_free(struct task_struct *task)
2277 static inline bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog)
2282 static inline const struct btf_func_model *
2283 bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
2284 const struct bpf_insn *insn)
2289 static inline bool unprivileged_ebpf_enabled(void)
2294 static inline bool has_current_bpf_ctx(void)
2299 static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
2302 #endif /* CONFIG_BPF_SYSCALL */
2304 void __bpf_free_used_btfs(struct bpf_prog_aux *aux,
2305 struct btf_mod_pair *used_btfs, u32 len);
2307 static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
2308 enum bpf_prog_type type)
2310 return bpf_prog_get_type_dev(ufd, type, false);
2313 void __bpf_free_used_maps(struct bpf_prog_aux *aux,
2314 struct bpf_map **used_maps, u32 len);
2316 bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
2318 int bpf_prog_offload_compile(struct bpf_prog *prog);
2319 void bpf_prog_offload_destroy(struct bpf_prog *prog);
2320 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
2321 struct bpf_prog *prog);
2323 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
2325 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
2326 int bpf_map_offload_update_elem(struct bpf_map *map,
2327 void *key, void *value, u64 flags);
2328 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
2329 int bpf_map_offload_get_next_key(struct bpf_map *map,
2330 void *key, void *next_key);
2332 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
2334 struct bpf_offload_dev *
2335 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
2336 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
2337 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
2338 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
2339 struct net_device *netdev);
2340 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
2341 struct net_device *netdev);
2342 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
2344 void unpriv_ebpf_notify(int new_state);
2346 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
2347 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
2349 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
2351 return aux->offload_requested;
2354 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
2356 return unlikely(map->ops == &bpf_map_offload_ops);
2359 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
2360 void bpf_map_offload_map_free(struct bpf_map *map);
2361 int bpf_prog_test_run_syscall(struct bpf_prog *prog,
2362 const union bpf_attr *kattr,
2363 union bpf_attr __user *uattr);
2365 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
2366 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype);
2367 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags);
2368 int sock_map_bpf_prog_query(const union bpf_attr *attr,
2369 union bpf_attr __user *uattr);
2371 void sock_map_unhash(struct sock *sk);
2372 void sock_map_destroy(struct sock *sk);
2373 void sock_map_close(struct sock *sk, long timeout);
2375 static inline int bpf_prog_offload_init(struct bpf_prog *prog,
2376 union bpf_attr *attr)
2381 static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
2386 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
2391 static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
2393 return ERR_PTR(-EOPNOTSUPP);
2396 static inline void bpf_map_offload_map_free(struct bpf_map *map)
2400 static inline int bpf_prog_test_run_syscall(struct bpf_prog *prog,
2401 const union bpf_attr *kattr,
2402 union bpf_attr __user *uattr)
2407 #ifdef CONFIG_BPF_SYSCALL
2408 static inline int sock_map_get_from_fd(const union bpf_attr *attr,
2409 struct bpf_prog *prog)
2414 static inline int sock_map_prog_detach(const union bpf_attr *attr,
2415 enum bpf_prog_type ptype)
2420 static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
2426 static inline int sock_map_bpf_prog_query(const union bpf_attr *attr,
2427 union bpf_attr __user *uattr)
2431 #endif /* CONFIG_BPF_SYSCALL */
2432 #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
2434 #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
2435 void bpf_sk_reuseport_detach(struct sock *sk);
2436 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
2438 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
2439 void *value, u64 map_flags);
2441 static inline void bpf_sk_reuseport_detach(struct sock *sk)
2445 #ifdef CONFIG_BPF_SYSCALL
2446 static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
2447 void *key, void *value)
2452 static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
2453 void *key, void *value,
2458 #endif /* CONFIG_BPF_SYSCALL */
2459 #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
2461 /* verifier prototypes for helper functions called from eBPF programs */
2462 extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
2463 extern const struct bpf_func_proto bpf_map_update_elem_proto;
2464 extern const struct bpf_func_proto bpf_map_delete_elem_proto;
2465 extern const struct bpf_func_proto bpf_map_push_elem_proto;
2466 extern const struct bpf_func_proto bpf_map_pop_elem_proto;
2467 extern const struct bpf_func_proto bpf_map_peek_elem_proto;
2468 extern const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto;
2470 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
2471 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
2472 extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
2473 extern const struct bpf_func_proto bpf_tail_call_proto;
2474 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
2475 extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
2476 extern const struct bpf_func_proto bpf_ktime_get_tai_ns_proto;
2477 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
2478 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
2479 extern const struct bpf_func_proto bpf_get_current_comm_proto;
2480 extern const struct bpf_func_proto bpf_get_stackid_proto;
2481 extern const struct bpf_func_proto bpf_get_stack_proto;
2482 extern const struct bpf_func_proto bpf_get_task_stack_proto;
2483 extern const struct bpf_func_proto bpf_get_stackid_proto_pe;
2484 extern const struct bpf_func_proto bpf_get_stack_proto_pe;
2485 extern const struct bpf_func_proto bpf_sock_map_update_proto;
2486 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
2487 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
2488 extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
2489 extern const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto;
2490 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
2491 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
2492 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
2493 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
2494 extern const struct bpf_func_proto bpf_spin_lock_proto;
2495 extern const struct bpf_func_proto bpf_spin_unlock_proto;
2496 extern const struct bpf_func_proto bpf_get_local_storage_proto;
2497 extern const struct bpf_func_proto bpf_strtol_proto;
2498 extern const struct bpf_func_proto bpf_strtoul_proto;
2499 extern const struct bpf_func_proto bpf_tcp_sock_proto;
2500 extern const struct bpf_func_proto bpf_jiffies64_proto;
2501 extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
2502 extern const struct bpf_func_proto bpf_event_output_data_proto;
2503 extern const struct bpf_func_proto bpf_ringbuf_output_proto;
2504 extern const struct bpf_func_proto bpf_ringbuf_reserve_proto;
2505 extern const struct bpf_func_proto bpf_ringbuf_submit_proto;
2506 extern const struct bpf_func_proto bpf_ringbuf_discard_proto;
2507 extern const struct bpf_func_proto bpf_ringbuf_query_proto;
2508 extern const struct bpf_func_proto bpf_ringbuf_reserve_dynptr_proto;
2509 extern const struct bpf_func_proto bpf_ringbuf_submit_dynptr_proto;
2510 extern const struct bpf_func_proto bpf_ringbuf_discard_dynptr_proto;
2511 extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
2512 extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
2513 extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
2514 extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
2515 extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
2516 extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;
2517 extern const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto;
2518 extern const struct bpf_func_proto bpf_copy_from_user_proto;
2519 extern const struct bpf_func_proto bpf_snprintf_btf_proto;
2520 extern const struct bpf_func_proto bpf_snprintf_proto;
2521 extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
2522 extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
2523 extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;
2524 extern const struct bpf_func_proto bpf_sock_from_file_proto;
2525 extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;
2526 extern const struct bpf_func_proto bpf_task_storage_get_proto;
2527 extern const struct bpf_func_proto bpf_task_storage_delete_proto;
2528 extern const struct bpf_func_proto bpf_for_each_map_elem_proto;
2529 extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto;
2530 extern const struct bpf_func_proto bpf_sk_setsockopt_proto;
2531 extern const struct bpf_func_proto bpf_sk_getsockopt_proto;
2532 extern const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto;
2533 extern const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto;
2534 extern const struct bpf_func_proto bpf_find_vma_proto;
2535 extern const struct bpf_func_proto bpf_loop_proto;
2536 extern const struct bpf_func_proto bpf_copy_from_user_task_proto;
2537 extern const struct bpf_func_proto bpf_set_retval_proto;
2538 extern const struct bpf_func_proto bpf_get_retval_proto;
2539 extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto;
2541 const struct bpf_func_proto *tracing_prog_func_proto(
2542 enum bpf_func_id func_id, const struct bpf_prog *prog);
2544 /* Shared helpers among cBPF and eBPF. */
2545 void bpf_user_rnd_init_once(void);
2546 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
2547 u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
2549 #if defined(CONFIG_NET)
2550 bool bpf_sock_common_is_valid_access(int off, int size,
2551 enum bpf_access_type type,
2552 struct bpf_insn_access_aux *info);
2553 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2554 struct bpf_insn_access_aux *info);
2555 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
2556 const struct bpf_insn *si,
2557 struct bpf_insn *insn_buf,
2558 struct bpf_prog *prog,
2561 static inline bool bpf_sock_common_is_valid_access(int off, int size,
2562 enum bpf_access_type type,
2563 struct bpf_insn_access_aux *info)
2567 static inline bool bpf_sock_is_valid_access(int off, int size,
2568 enum bpf_access_type type,
2569 struct bpf_insn_access_aux *info)
2573 static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
2574 const struct bpf_insn *si,
2575 struct bpf_insn *insn_buf,
2576 struct bpf_prog *prog,
2584 struct sk_reuseport_kern {
2585 struct sk_buff *skb;
2587 struct sock *selected_sk;
2588 struct sock *migrating_sk;
2594 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2595 struct bpf_insn_access_aux *info);
2597 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
2598 const struct bpf_insn *si,
2599 struct bpf_insn *insn_buf,
2600 struct bpf_prog *prog,
2603 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2604 struct bpf_insn_access_aux *info);
2606 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2607 const struct bpf_insn *si,
2608 struct bpf_insn *insn_buf,
2609 struct bpf_prog *prog,
2612 static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
2613 enum bpf_access_type type,
2614 struct bpf_insn_access_aux *info)
2619 static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
2620 const struct bpf_insn *si,
2621 struct bpf_insn *insn_buf,
2622 struct bpf_prog *prog,
2627 static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
2628 enum bpf_access_type type,
2629 struct bpf_insn_access_aux *info)
2634 static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2635 const struct bpf_insn *si,
2636 struct bpf_insn *insn_buf,
2637 struct bpf_prog *prog,
2642 #endif /* CONFIG_INET */
2644 enum bpf_text_poke_type {
2649 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
2650 void *addr1, void *addr2);
2652 void *bpf_arch_text_copy(void *dst, void *src, size_t len);
2653 int bpf_arch_text_invalidate(void *dst, size_t len);
2656 bool btf_id_set_contains(const struct btf_id_set *set, u32 id);
2658 #define MAX_BPRINTF_VARARGS 12
2660 int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
2661 u32 **bin_buf, u32 num_args);
2662 void bpf_bprintf_cleanup(void);
2664 /* the implementation of the opaque uapi struct bpf_dynptr */
2665 struct bpf_dynptr_kern {
2667 /* Size represents the number of usable bytes of dynptr data.
2668 * If for example the offset is at 4 for a local dynptr whose data is
2669 * of type u64, the number of usable bytes is 4.
2671 * The upper 8 bits are reserved. It is as follows:
2672 * Bits 0 - 23 = size
2673 * Bits 24 - 30 = dynptr type
2674 * Bit 31 = whether dynptr is read-only
2680 enum bpf_dynptr_type {
2681 BPF_DYNPTR_TYPE_INVALID,
2682 /* Points to memory that is local to the bpf program */
2683 BPF_DYNPTR_TYPE_LOCAL,
2684 /* Underlying data is a kernel-produced ringbuf record */
2685 BPF_DYNPTR_TYPE_RINGBUF,
2688 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
2689 enum bpf_dynptr_type type, u32 offset, u32 size);
2690 void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr);
2691 int bpf_dynptr_check_size(u32 size);
2692 u32 bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr);
2694 #ifdef CONFIG_BPF_LSM
2695 void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype);
2696 void bpf_cgroup_atype_put(int cgroup_atype);
2698 static inline void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype) {}
2699 static inline void bpf_cgroup_atype_put(int cgroup_atype) {}
2700 #endif /* CONFIG_BPF_LSM */
2709 #endif /* CONFIG_KEYS */
2710 #endif /* _LINUX_BPF_H */