1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
7 #include <uapi/linux/bpf.h>
9 #include <linux/workqueue.h>
10 #include <linux/file.h>
11 #include <linux/percpu.h>
12 #include <linux/err.h>
13 #include <linux/rbtree_latch.h>
14 #include <linux/numa.h>
15 #include <linux/mm_types.h>
16 #include <linux/wait.h>
17 #include <linux/u64_stats_sync.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>
24 struct bpf_verifier_env;
25 struct bpf_verifier_log;
34 struct exception_table_entry;
35 struct seq_operations;
36 struct bpf_iter_aux_info;
37 struct bpf_local_storage;
38 struct bpf_local_storage_map;
40 extern struct idr btf_idr;
41 extern spinlock_t btf_idr_lock;
43 typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,
44 struct bpf_iter_aux_info *aux);
45 typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
46 struct bpf_iter_seq_info {
47 const struct seq_operations *seq_ops;
48 bpf_iter_init_seq_priv_t init_seq_private;
49 bpf_iter_fini_seq_priv_t fini_seq_private;
53 /* map is generic key/value storage optionally accesible by eBPF programs */
55 /* funcs callable from userspace (via syscall) */
56 int (*map_alloc_check)(union bpf_attr *attr);
57 struct bpf_map *(*map_alloc)(union bpf_attr *attr);
58 void (*map_release)(struct bpf_map *map, struct file *map_file);
59 void (*map_free)(struct bpf_map *map);
60 int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
61 void (*map_release_uref)(struct bpf_map *map);
62 void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
63 int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr,
64 union bpf_attr __user *uattr);
65 int (*map_lookup_and_delete_batch)(struct bpf_map *map,
66 const union bpf_attr *attr,
67 union bpf_attr __user *uattr);
68 int (*map_update_batch)(struct bpf_map *map, const union bpf_attr *attr,
69 union bpf_attr __user *uattr);
70 int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr,
71 union bpf_attr __user *uattr);
73 /* funcs callable from userspace and from eBPF programs */
74 void *(*map_lookup_elem)(struct bpf_map *map, void *key);
75 int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
76 int (*map_delete_elem)(struct bpf_map *map, void *key);
77 int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
78 int (*map_pop_elem)(struct bpf_map *map, void *value);
79 int (*map_peek_elem)(struct bpf_map *map, void *value);
81 /* funcs called by prog_array and perf_event_array map */
82 void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
84 void (*map_fd_put_ptr)(void *ptr);
85 u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
86 u32 (*map_fd_sys_lookup_elem)(void *ptr);
87 void (*map_seq_show_elem)(struct bpf_map *map, void *key,
89 int (*map_check_btf)(const struct bpf_map *map,
90 const struct btf *btf,
91 const struct btf_type *key_type,
92 const struct btf_type *value_type);
94 /* Prog poke tracking helpers. */
95 int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux);
96 void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux);
97 void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old,
98 struct bpf_prog *new);
100 /* Direct value access helpers. */
101 int (*map_direct_value_addr)(const struct bpf_map *map,
103 int (*map_direct_value_meta)(const struct bpf_map *map,
105 int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
106 __poll_t (*map_poll)(struct bpf_map *map, struct file *filp,
107 struct poll_table_struct *pts);
109 /* Functions called by bpf_local_storage maps */
110 int (*map_local_storage_charge)(struct bpf_local_storage_map *smap,
111 void *owner, u32 size);
112 void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap,
113 void *owner, u32 size);
114 struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(void *owner);
116 /* map_meta_equal must be implemented for maps that can be
117 * used as an inner map. It is a runtime check to ensure
118 * an inner map can be inserted to an outer map.
120 * Some properties of the inner map has been used during the
121 * verification time. When inserting an inner map at the runtime,
122 * map_meta_equal has to ensure the inserting map has the same
123 * properties that the verifier has used earlier.
125 bool (*map_meta_equal)(const struct bpf_map *meta0,
126 const struct bpf_map *meta1);
128 /* BTF name and id of struct allocated by map_alloc */
129 const char * const map_btf_name;
132 /* bpf_iter info used to open a seq_file */
133 const struct bpf_iter_seq_info *iter_seq_info;
136 struct bpf_map_memory {
138 struct user_struct *user;
142 /* The first two cachelines with read-mostly members of which some
143 * are also accessed in fast-path (e.g. ops, max_entries).
145 const struct bpf_map_ops *ops ____cacheline_aligned;
146 struct bpf_map *inner_map_meta;
147 #ifdef CONFIG_SECURITY
150 enum bpf_map_type map_type;
155 int spin_lock_off; /* >=0 valid offset, <0 error */
159 u32 btf_value_type_id;
161 struct bpf_map_memory memory;
162 char name[BPF_OBJ_NAME_LEN];
163 u32 btf_vmlinux_value_type_id;
165 bool frozen; /* write-once; write-protected by freeze_mutex */
168 /* The 3rd and 4th cacheline with misc members to avoid false sharing
169 * particularly with refcounting.
171 atomic64_t refcnt ____cacheline_aligned;
173 struct work_struct work;
174 struct mutex freeze_mutex;
175 u64 writecnt; /* writable mmap cnt; protected by freeze_mutex */
178 static inline bool map_value_has_spin_lock(const struct bpf_map *map)
180 return map->spin_lock_off >= 0;
183 static inline void check_and_init_map_lock(struct bpf_map *map, void *dst)
185 if (likely(!map_value_has_spin_lock(map)))
187 *(struct bpf_spin_lock *)(dst + map->spin_lock_off) =
188 (struct bpf_spin_lock){};
191 /* copy everything but bpf_spin_lock */
192 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
194 if (unlikely(map_value_has_spin_lock(map))) {
195 u32 off = map->spin_lock_off;
197 memcpy(dst, src, off);
198 memcpy(dst + off + sizeof(struct bpf_spin_lock),
199 src + off + sizeof(struct bpf_spin_lock),
200 map->value_size - off - sizeof(struct bpf_spin_lock));
202 memcpy(dst, src, map->value_size);
205 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
207 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
209 struct bpf_offload_dev;
210 struct bpf_offloaded_map;
212 struct bpf_map_dev_ops {
213 int (*map_get_next_key)(struct bpf_offloaded_map *map,
214 void *key, void *next_key);
215 int (*map_lookup_elem)(struct bpf_offloaded_map *map,
216 void *key, void *value);
217 int (*map_update_elem)(struct bpf_offloaded_map *map,
218 void *key, void *value, u64 flags);
219 int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
222 struct bpf_offloaded_map {
224 struct net_device *netdev;
225 const struct bpf_map_dev_ops *dev_ops;
227 struct list_head offloads;
230 static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
232 return container_of(map, struct bpf_offloaded_map, map);
235 static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
237 return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
240 static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
242 return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) &&
243 map->ops->map_seq_show_elem;
246 int map_check_no_btf(const struct bpf_map *map,
247 const struct btf *btf,
248 const struct btf_type *key_type,
249 const struct btf_type *value_type);
251 bool bpf_map_meta_equal(const struct bpf_map *meta0,
252 const struct bpf_map *meta1);
254 extern const struct bpf_map_ops bpf_map_offload_ops;
256 /* function argument constraints */
258 ARG_DONTCARE = 0, /* unused argument in helper function */
260 /* the following constraints used to prototype
261 * bpf_map_lookup/update/delete_elem() functions
263 ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */
264 ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
265 ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
266 ARG_PTR_TO_UNINIT_MAP_VALUE, /* pointer to valid memory used to store a map value */
267 ARG_PTR_TO_MAP_VALUE_OR_NULL, /* pointer to stack used as map value or NULL */
269 /* the following constraints used to prototype bpf_memcmp() and other
270 * functions that access data on eBPF program stack
272 ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */
273 ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */
274 ARG_PTR_TO_UNINIT_MEM, /* pointer to memory does not need to be initialized,
275 * helper function must fill all bytes or clear
276 * them in error case.
279 ARG_CONST_SIZE, /* number of bytes accessed from memory */
280 ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
282 ARG_PTR_TO_CTX, /* pointer to context */
283 ARG_PTR_TO_CTX_OR_NULL, /* pointer to context or NULL */
284 ARG_ANYTHING, /* any (initialized) argument is ok */
285 ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */
286 ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
287 ARG_PTR_TO_INT, /* pointer to int */
288 ARG_PTR_TO_LONG, /* pointer to long */
289 ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */
290 ARG_PTR_TO_SOCKET_OR_NULL, /* pointer to bpf_sock (fullsock) or NULL */
291 ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */
292 ARG_PTR_TO_ALLOC_MEM, /* pointer to dynamically allocated memory */
293 ARG_PTR_TO_ALLOC_MEM_OR_NULL, /* pointer to dynamically allocated memory or NULL */
294 ARG_CONST_ALLOC_SIZE_OR_ZERO, /* number of allocated bytes requested */
298 /* type of values returned from helper functions */
299 enum bpf_return_type {
300 RET_INTEGER, /* function returns integer */
301 RET_VOID, /* function doesn't return anything */
302 RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */
303 RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
304 RET_PTR_TO_SOCKET_OR_NULL, /* returns a pointer to a socket or NULL */
305 RET_PTR_TO_TCP_SOCK_OR_NULL, /* returns a pointer to a tcp_sock or NULL */
306 RET_PTR_TO_SOCK_COMMON_OR_NULL, /* returns a pointer to a sock_common or NULL */
307 RET_PTR_TO_ALLOC_MEM_OR_NULL, /* returns a pointer to dynamically allocated memory or NULL */
308 RET_PTR_TO_BTF_ID_OR_NULL, /* returns a pointer to a btf_id or NULL */
311 /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
312 * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
313 * instructions after verifying
315 struct bpf_func_proto {
316 u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
319 enum bpf_return_type ret_type;
322 enum bpf_arg_type arg1_type;
323 enum bpf_arg_type arg2_type;
324 enum bpf_arg_type arg3_type;
325 enum bpf_arg_type arg4_type;
326 enum bpf_arg_type arg5_type;
328 enum bpf_arg_type arg_type[5];
340 int *ret_btf_id; /* return value btf_id */
341 bool (*allowed)(const struct bpf_prog *prog);
344 /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
345 * the first argument to eBPF programs.
346 * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
350 enum bpf_access_type {
355 /* types of values stored in eBPF registers */
356 /* Pointer types represent:
359 * pointer + (u16) var
360 * pointer + (u16) var + imm
361 * if (range > 0) then [ptr, ptr + range - off) is safe to access
362 * if (id > 0) means that some 'var' was added
363 * if (off > 0) means that 'imm' was added
366 NOT_INIT = 0, /* nothing was written into register */
367 SCALAR_VALUE, /* reg doesn't contain a valid pointer */
368 PTR_TO_CTX, /* reg points to bpf_context */
369 CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
370 PTR_TO_MAP_VALUE, /* reg points to map element value */
371 PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
372 PTR_TO_STACK, /* reg == frame_pointer + offset */
373 PTR_TO_PACKET_META, /* skb->data - meta_len */
374 PTR_TO_PACKET, /* reg points to skb->data */
375 PTR_TO_PACKET_END, /* skb->data + headlen */
376 PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */
377 PTR_TO_SOCKET, /* reg points to struct bpf_sock */
378 PTR_TO_SOCKET_OR_NULL, /* reg points to struct bpf_sock or NULL */
379 PTR_TO_SOCK_COMMON, /* reg points to sock_common */
380 PTR_TO_SOCK_COMMON_OR_NULL, /* reg points to sock_common or NULL */
381 PTR_TO_TCP_SOCK, /* reg points to struct tcp_sock */
382 PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */
383 PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
384 PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
385 PTR_TO_BTF_ID, /* reg points to kernel struct */
386 PTR_TO_BTF_ID_OR_NULL, /* reg points to kernel struct or NULL */
387 PTR_TO_MEM, /* reg points to valid memory region */
388 PTR_TO_MEM_OR_NULL, /* reg points to valid memory region or NULL */
389 PTR_TO_RDONLY_BUF, /* reg points to a readonly buffer */
390 PTR_TO_RDONLY_BUF_OR_NULL, /* reg points to a readonly buffer or NULL */
391 PTR_TO_RDWR_BUF, /* reg points to a read/write buffer */
392 PTR_TO_RDWR_BUF_OR_NULL, /* reg points to a read/write buffer or NULL */
395 /* The information passed from prog-specific *_is_valid_access
396 * back to the verifier.
398 struct bpf_insn_access_aux {
399 enum bpf_reg_type reg_type;
404 struct bpf_verifier_log *log; /* for verbose logs */
408 bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
410 aux->ctx_field_size = size;
413 struct bpf_prog_ops {
414 int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
415 union bpf_attr __user *uattr);
418 struct bpf_verifier_ops {
419 /* return eBPF function prototype for verification */
420 const struct bpf_func_proto *
421 (*get_func_proto)(enum bpf_func_id func_id,
422 const struct bpf_prog *prog);
424 /* return true if 'size' wide access at offset 'off' within bpf_context
425 * with 'type' (read or write) is allowed
427 bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
428 const struct bpf_prog *prog,
429 struct bpf_insn_access_aux *info);
430 int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
431 const struct bpf_prog *prog);
432 int (*gen_ld_abs)(const struct bpf_insn *orig,
433 struct bpf_insn *insn_buf);
434 u32 (*convert_ctx_access)(enum bpf_access_type type,
435 const struct bpf_insn *src,
436 struct bpf_insn *dst,
437 struct bpf_prog *prog, u32 *target_size);
438 int (*btf_struct_access)(struct bpf_verifier_log *log,
439 const struct btf_type *t, int off, int size,
440 enum bpf_access_type atype,
444 struct bpf_prog_offload_ops {
445 /* verifier basic callbacks */
446 int (*insn_hook)(struct bpf_verifier_env *env,
447 int insn_idx, int prev_insn_idx);
448 int (*finalize)(struct bpf_verifier_env *env);
449 /* verifier optimization callbacks (called after .finalize) */
450 int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
451 struct bpf_insn *insn);
452 int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
453 /* program management callbacks */
454 int (*prepare)(struct bpf_prog *prog);
455 int (*translate)(struct bpf_prog *prog);
456 void (*destroy)(struct bpf_prog *prog);
459 struct bpf_prog_offload {
460 struct bpf_prog *prog;
461 struct net_device *netdev;
462 struct bpf_offload_dev *offdev;
464 struct list_head offloads;
471 enum bpf_cgroup_storage_type {
472 BPF_CGROUP_STORAGE_SHARED,
473 BPF_CGROUP_STORAGE_PERCPU,
474 __BPF_CGROUP_STORAGE_MAX
477 #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
479 /* The longest tracepoint has 12 args.
480 * See include/trace/bpf_probe.h
482 #define MAX_BPF_FUNC_ARGS 12
484 struct bpf_prog_stats {
487 struct u64_stats_sync syncp;
488 } __aligned(2 * sizeof(u64));
490 struct btf_func_model {
493 u8 arg_size[MAX_BPF_FUNC_ARGS];
496 /* Restore arguments before returning from trampoline to let original function
497 * continue executing. This flag is used for fentry progs when there are no
500 #define BPF_TRAMP_F_RESTORE_REGS BIT(0)
501 /* Call original function after fentry progs, but before fexit progs.
502 * Makes sense for fentry/fexit, normal calls and indirect calls.
504 #define BPF_TRAMP_F_CALL_ORIG BIT(1)
505 /* Skip current frame and return to parent. Makes sense for fentry/fexit
506 * programs only. Should not be used with normal calls and indirect calls.
508 #define BPF_TRAMP_F_SKIP_FRAME BIT(2)
510 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
511 * bytes on x86. Pick a number to fit into BPF_IMAGE_SIZE / 2
513 #define BPF_MAX_TRAMP_PROGS 40
515 struct bpf_tramp_progs {
516 struct bpf_prog *progs[BPF_MAX_TRAMP_PROGS];
520 /* Different use cases for BPF trampoline:
521 * 1. replace nop at the function entry (kprobe equivalent)
522 * flags = BPF_TRAMP_F_RESTORE_REGS
523 * fentry = a set of programs to run before returning from trampoline
525 * 2. replace nop at the function entry (kprobe + kretprobe equivalent)
526 * flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME
527 * orig_call = fentry_ip + MCOUNT_INSN_SIZE
528 * fentry = a set of program to run before calling original function
529 * fexit = a set of program to run after original function
531 * 3. replace direct call instruction anywhere in the function body
532 * or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid)
534 * fentry = a set of programs to run before returning from trampoline
535 * With flags = BPF_TRAMP_F_CALL_ORIG
536 * orig_call = original callback addr or direct function addr
537 * fentry = a set of program to run before calling original function
538 * fexit = a set of program to run after original function
540 int arch_prepare_bpf_trampoline(void *image, void *image_end,
541 const struct btf_func_model *m, u32 flags,
542 struct bpf_tramp_progs *tprogs,
544 /* these two functions are called from generated trampoline */
545 u64 notrace __bpf_prog_enter(void);
546 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start);
547 void notrace __bpf_prog_enter_sleepable(void);
548 void notrace __bpf_prog_exit_sleepable(void);
553 char name[KSYM_NAME_LEN];
554 struct list_head lnode;
555 struct latch_tree_node tnode;
559 enum bpf_tramp_prog_type {
562 BPF_TRAMP_MODIFY_RETURN,
564 BPF_TRAMP_REPLACE, /* more than MAX */
567 struct bpf_trampoline {
568 /* hlist for trampoline_table */
569 struct hlist_node hlist;
570 /* serializes access to fields of this trampoline */
575 struct btf_func_model model;
579 /* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF
580 * program by replacing one of its functions. func.addr is the address
581 * of the function it replaced.
583 struct bpf_prog *extension_prog;
584 /* list of BPF programs using this trampoline */
585 struct hlist_head progs_hlist[BPF_TRAMP_MAX];
586 /* Number of attached programs. A counter per kind. */
587 int progs_cnt[BPF_TRAMP_MAX];
588 /* Executable image of trampoline */
591 struct bpf_ksym ksym;
594 #define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
596 struct bpf_dispatcher_prog {
597 struct bpf_prog *prog;
601 struct bpf_dispatcher {
602 /* dispatcher mutex */
605 struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX];
609 struct bpf_ksym ksym;
612 static __always_inline unsigned int bpf_dispatcher_nop_func(
614 const struct bpf_insn *insnsi,
615 unsigned int (*bpf_func)(const void *,
616 const struct bpf_insn *))
618 return bpf_func(ctx, insnsi);
620 #ifdef CONFIG_BPF_JIT
621 struct bpf_trampoline *bpf_trampoline_lookup(u64 key);
622 int bpf_trampoline_link_prog(struct bpf_prog *prog);
623 int bpf_trampoline_unlink_prog(struct bpf_prog *prog);
624 void bpf_trampoline_put(struct bpf_trampoline *tr);
625 #define BPF_DISPATCHER_INIT(_name) { \
626 .mutex = __MUTEX_INITIALIZER(_name.mutex), \
627 .func = &_name##_func, \
634 .lnode = LIST_HEAD_INIT(_name.ksym.lnode), \
638 #define DEFINE_BPF_DISPATCHER(name) \
639 noinline unsigned int bpf_dispatcher_##name##_func( \
641 const struct bpf_insn *insnsi, \
642 unsigned int (*bpf_func)(const void *, \
643 const struct bpf_insn *)) \
645 return bpf_func(ctx, insnsi); \
647 EXPORT_SYMBOL(bpf_dispatcher_##name##_func); \
648 struct bpf_dispatcher bpf_dispatcher_##name = \
649 BPF_DISPATCHER_INIT(bpf_dispatcher_##name);
650 #define DECLARE_BPF_DISPATCHER(name) \
651 unsigned int bpf_dispatcher_##name##_func( \
653 const struct bpf_insn *insnsi, \
654 unsigned int (*bpf_func)(const void *, \
655 const struct bpf_insn *)); \
656 extern struct bpf_dispatcher bpf_dispatcher_##name;
657 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func
658 #define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
659 void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
660 struct bpf_prog *to);
661 /* Called only from JIT-enabled code, so there's no need for stubs. */
662 void *bpf_jit_alloc_exec_page(void);
663 void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
664 void bpf_image_ksym_del(struct bpf_ksym *ksym);
665 void bpf_ksym_add(struct bpf_ksym *ksym);
666 void bpf_ksym_del(struct bpf_ksym *ksym);
668 static inline struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
672 static inline int bpf_trampoline_link_prog(struct bpf_prog *prog)
676 static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog)
680 static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
681 #define DEFINE_BPF_DISPATCHER(name)
682 #define DECLARE_BPF_DISPATCHER(name)
683 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nop_func
684 #define BPF_DISPATCHER_PTR(name) NULL
685 static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
686 struct bpf_prog *from,
687 struct bpf_prog *to) {}
688 static inline bool is_bpf_image_address(unsigned long address)
694 struct bpf_func_info_aux {
699 enum bpf_jit_poke_reason {
700 BPF_POKE_REASON_TAIL_CALL,
703 /* Descriptor of pokes pointing /into/ the JITed image. */
704 struct bpf_jit_poke_descriptor {
705 void *tailcall_target;
706 void *tailcall_bypass;
714 bool tailcall_target_stable;
720 /* reg_type info for ctx arguments */
721 struct bpf_ctx_arg_aux {
723 enum bpf_reg_type reg_type;
727 struct bpf_prog_aux {
735 u32 func_cnt; /* used by non-func prog as the number of func progs */
736 u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
737 u32 attach_btf_id; /* in-kernel BTF type id to attach to */
738 u32 ctx_arg_info_size;
739 u32 max_rdonly_access;
741 const struct bpf_ctx_arg_aux *ctx_arg_info;
742 struct bpf_prog *linked_prog;
743 bool verifier_zext; /* Zero extensions has been inserted by verifier. */
744 bool offload_requested;
745 bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
746 bool func_proto_unreliable;
748 bool tail_call_reachable;
749 enum bpf_tramp_prog_type trampoline_prog_type;
750 struct bpf_trampoline *trampoline;
751 struct hlist_node tramp_hlist;
752 /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
753 const struct btf_type *attach_func_proto;
754 /* function name for valid attach_btf_id */
755 const char *attach_func_name;
756 struct bpf_prog **func;
757 void *jit_data; /* JIT specific data. arch dependent */
758 struct bpf_jit_poke_descriptor *poke_tab;
760 struct bpf_ksym ksym;
761 const struct bpf_prog_ops *ops;
762 struct bpf_map **used_maps;
763 struct mutex used_maps_mutex; /* mutex for used_maps and used_map_cnt */
764 struct bpf_prog *prog;
765 struct user_struct *user;
766 u64 load_time; /* ns since boottime */
767 struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
768 char name[BPF_OBJ_NAME_LEN];
769 #ifdef CONFIG_SECURITY
772 struct bpf_prog_offload *offload;
774 struct bpf_func_info *func_info;
775 struct bpf_func_info_aux *func_info_aux;
776 /* bpf_line_info loaded from userspace. linfo->insn_off
777 * has the xlated insn offset.
778 * Both the main and sub prog share the same linfo.
779 * The subprog can access its first linfo by
780 * using the linfo_idx.
782 struct bpf_line_info *linfo;
783 /* jited_linfo is the jited addr of the linfo. It has a
784 * one to one mapping to linfo:
785 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
786 * Both the main and sub prog share the same jited_linfo.
787 * The subprog can access its first jited_linfo by
788 * using the linfo_idx.
793 /* subprog can use linfo_idx to access its first linfo and
795 * main prog always has linfo_idx == 0
799 struct exception_table_entry *extable;
800 struct bpf_prog_stats __percpu *stats;
802 struct work_struct work;
807 struct bpf_array_aux {
808 /* 'Ownership' of prog array is claimed by the first program that
809 * is going to use this map or by the first program which FD is
810 * stored in the map to make sure that all callers and callees have
811 * the same prog type and JITed flag.
813 enum bpf_prog_type type;
815 /* Programs with direct jumps into programs part of this array. */
816 struct list_head poke_progs;
818 struct mutex poke_mutex;
819 struct work_struct work;
825 enum bpf_link_type type;
826 const struct bpf_link_ops *ops;
827 struct bpf_prog *prog;
828 struct work_struct work;
831 struct bpf_link_ops {
832 void (*release)(struct bpf_link *link);
833 void (*dealloc)(struct bpf_link *link);
834 int (*detach)(struct bpf_link *link);
835 int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
836 struct bpf_prog *old_prog);
837 void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);
838 int (*fill_link_info)(const struct bpf_link *link,
839 struct bpf_link_info *info);
842 struct bpf_link_primer {
843 struct bpf_link *link;
849 struct bpf_struct_ops_value;
853 #define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
854 struct bpf_struct_ops {
855 const struct bpf_verifier_ops *verifier_ops;
856 int (*init)(struct btf *btf);
857 int (*check_member)(const struct btf_type *t,
858 const struct btf_member *member);
859 int (*init_member)(const struct btf_type *t,
860 const struct btf_member *member,
861 void *kdata, const void *udata);
862 int (*reg)(void *kdata);
863 void (*unreg)(void *kdata);
864 const struct btf_type *type;
865 const struct btf_type *value_type;
867 struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
872 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
873 #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
874 const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
875 void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
876 bool bpf_struct_ops_get(const void *kdata);
877 void bpf_struct_ops_put(const void *kdata);
878 int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
880 static inline bool bpf_try_module_get(const void *data, struct module *owner)
882 if (owner == BPF_MODULE_OWNER)
883 return bpf_struct_ops_get(data);
885 return try_module_get(owner);
887 static inline void bpf_module_put(const void *data, struct module *owner)
889 if (owner == BPF_MODULE_OWNER)
890 bpf_struct_ops_put(data);
895 static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
899 static inline void bpf_struct_ops_init(struct btf *btf,
900 struct bpf_verifier_log *log)
903 static inline bool bpf_try_module_get(const void *data, struct module *owner)
905 return try_module_get(owner);
907 static inline void bpf_module_put(const void *data, struct module *owner)
911 static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
923 struct bpf_array_aux *aux;
925 char value[0] __aligned(8);
926 void *ptrs[0] __aligned(8);
927 void __percpu *pptrs[0] __aligned(8);
931 #define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */
932 #define MAX_TAIL_CALL_CNT 32
934 #define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \
935 BPF_F_RDONLY_PROG | \
939 #define BPF_MAP_CAN_READ BIT(0)
940 #define BPF_MAP_CAN_WRITE BIT(1)
942 static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
944 u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
946 /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
949 if (access_flags & BPF_F_RDONLY_PROG)
950 return BPF_MAP_CAN_READ;
951 else if (access_flags & BPF_F_WRONLY_PROG)
952 return BPF_MAP_CAN_WRITE;
954 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
957 static inline bool bpf_map_flags_access_ok(u32 access_flags)
959 return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
960 (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
963 struct bpf_event_entry {
964 struct perf_event *event;
965 struct file *perf_file;
966 struct file *map_file;
970 bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
971 int bpf_prog_calc_tag(struct bpf_prog *fp);
972 const char *kernel_type_name(u32 btf_type_id);
974 const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
976 typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
977 unsigned long off, unsigned long len);
978 typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
979 const struct bpf_insn *src,
980 struct bpf_insn *dst,
981 struct bpf_prog *prog,
984 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
985 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
987 /* an array of programs to be executed under rcu_lock.
990 * ret = BPF_PROG_RUN_ARRAY(&bpf_prog_array, ctx, BPF_PROG_RUN);
992 * the structure returned by bpf_prog_array_alloc() should be populated
993 * with program pointers and the last pointer must be NULL.
994 * The user has to keep refcnt on the program and make sure the program
995 * is removed from the array before bpf_prog_put().
996 * The 'struct bpf_prog_array *' should only be replaced with xchg()
997 * since other cpus are walking the array of pointers in parallel.
999 struct bpf_prog_array_item {
1000 struct bpf_prog *prog;
1001 struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1004 struct bpf_prog_array {
1005 struct rcu_head rcu;
1006 struct bpf_prog_array_item items[];
1009 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
1010 void bpf_prog_array_free(struct bpf_prog_array *progs);
1011 int bpf_prog_array_length(struct bpf_prog_array *progs);
1012 bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
1013 int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
1014 __u32 __user *prog_ids, u32 cnt);
1016 void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
1017 struct bpf_prog *old_prog);
1018 int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index);
1019 int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
1020 struct bpf_prog *prog);
1021 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
1022 u32 *prog_ids, u32 request_cnt,
1024 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
1025 struct bpf_prog *exclude_prog,
1026 struct bpf_prog *include_prog,
1027 struct bpf_prog_array **new_array);
1029 #define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null) \
1031 struct bpf_prog_array_item *_item; \
1032 struct bpf_prog *_prog; \
1033 struct bpf_prog_array *_array; \
1035 migrate_disable(); \
1037 _array = rcu_dereference(array); \
1038 if (unlikely(check_non_null && !_array))\
1040 _item = &_array->items[0]; \
1041 while ((_prog = READ_ONCE(_item->prog))) { \
1042 bpf_cgroup_storage_set(_item->cgroup_storage); \
1043 _ret &= func(_prog, ctx); \
1047 rcu_read_unlock(); \
1052 /* To be used by __cgroup_bpf_run_filter_skb for EGRESS BPF progs
1053 * so BPF programs can request cwr for TCP packets.
1055 * Current cgroup skb programs can only return 0 or 1 (0 to drop the
1056 * packet. This macro changes the behavior so the low order bit
1057 * indicates whether the packet should be dropped (0) or not (1)
1058 * and the next bit is a congestion notification bit. This could be
1059 * used by TCP to call tcp_enter_cwr()
1061 * Hence, new allowed return values of CGROUP EGRESS BPF programs are:
1064 * 2: drop packet and cn
1065 * 3: keep packet and cn
1067 * This macro then converts it to one of the NET_XMIT or an error
1068 * code that is then interpreted as drop packet (and no cn):
1069 * 0: NET_XMIT_SUCCESS skb should be transmitted
1070 * 1: NET_XMIT_DROP skb should be dropped and cn
1071 * 2: NET_XMIT_CN skb should be transmitted and cn
1072 * 3: -EPERM skb should be dropped
1074 #define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(array, ctx, func) \
1076 struct bpf_prog_array_item *_item; \
1077 struct bpf_prog *_prog; \
1078 struct bpf_prog_array *_array; \
1082 migrate_disable(); \
1084 _array = rcu_dereference(array); \
1085 _item = &_array->items[0]; \
1086 while ((_prog = READ_ONCE(_item->prog))) { \
1087 bpf_cgroup_storage_set(_item->cgroup_storage); \
1088 ret = func(_prog, ctx); \
1089 _ret &= (ret & 1); \
1093 rcu_read_unlock(); \
1096 _ret = (_cn ? NET_XMIT_CN : NET_XMIT_SUCCESS); \
1098 _ret = (_cn ? NET_XMIT_DROP : -EPERM); \
1102 #define BPF_PROG_RUN_ARRAY(array, ctx, func) \
1103 __BPF_PROG_RUN_ARRAY(array, ctx, func, false)
1105 #define BPF_PROG_RUN_ARRAY_CHECK(array, ctx, func) \
1106 __BPF_PROG_RUN_ARRAY(array, ctx, func, true)
1108 #ifdef CONFIG_BPF_SYSCALL
1109 DECLARE_PER_CPU(int, bpf_prog_active);
1110 extern struct mutex bpf_stats_enabled_mutex;
1113 * Block execution of BPF programs attached to instrumentation (perf,
1114 * kprobes, tracepoints) to prevent deadlocks on map operations as any of
1115 * these events can happen inside a region which holds a map bucket lock
1116 * and can deadlock on it.
1118 * Use the preemption safe inc/dec variants on RT because migrate disable
1119 * is preemptible on RT and preemption in the middle of the RMW operation
1120 * might lead to inconsistent state. Use the raw variants for non RT
1121 * kernels as migrate_disable() maps to preempt_disable() so the slightly
1122 * more expensive save operation can be avoided.
1124 static inline void bpf_disable_instrumentation(void)
1127 if (IS_ENABLED(CONFIG_PREEMPT_RT))
1128 this_cpu_inc(bpf_prog_active);
1130 __this_cpu_inc(bpf_prog_active);
1133 static inline void bpf_enable_instrumentation(void)
1135 if (IS_ENABLED(CONFIG_PREEMPT_RT))
1136 this_cpu_dec(bpf_prog_active);
1138 __this_cpu_dec(bpf_prog_active);
1142 extern const struct file_operations bpf_map_fops;
1143 extern const struct file_operations bpf_prog_fops;
1144 extern const struct file_operations bpf_iter_fops;
1146 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1147 extern const struct bpf_prog_ops _name ## _prog_ops; \
1148 extern const struct bpf_verifier_ops _name ## _verifier_ops;
1149 #define BPF_MAP_TYPE(_id, _ops) \
1150 extern const struct bpf_map_ops _ops;
1151 #define BPF_LINK_TYPE(_id, _name)
1152 #include <linux/bpf_types.h>
1153 #undef BPF_PROG_TYPE
1155 #undef BPF_LINK_TYPE
1157 extern const struct bpf_prog_ops bpf_offload_prog_ops;
1158 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
1159 extern const struct bpf_verifier_ops xdp_analyzer_ops;
1161 struct bpf_prog *bpf_prog_get(u32 ufd);
1162 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1164 void bpf_prog_add(struct bpf_prog *prog, int i);
1165 void bpf_prog_sub(struct bpf_prog *prog, int i);
1166 void bpf_prog_inc(struct bpf_prog *prog);
1167 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
1168 void bpf_prog_put(struct bpf_prog *prog);
1169 int __bpf_prog_charge(struct user_struct *user, u32 pages);
1170 void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
1171 void __bpf_free_used_maps(struct bpf_prog_aux *aux,
1172 struct bpf_map **used_maps, u32 len);
1174 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
1175 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
1177 struct bpf_map *bpf_map_get(u32 ufd);
1178 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
1179 struct bpf_map *__bpf_map_get(struct fd f);
1180 void bpf_map_inc(struct bpf_map *map);
1181 void bpf_map_inc_with_uref(struct bpf_map *map);
1182 struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
1183 void bpf_map_put_with_uref(struct bpf_map *map);
1184 void bpf_map_put(struct bpf_map *map);
1185 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
1186 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
1187 int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size);
1188 void bpf_map_charge_finish(struct bpf_map_memory *mem);
1189 void bpf_map_charge_move(struct bpf_map_memory *dst,
1190 struct bpf_map_memory *src);
1191 void *bpf_map_area_alloc(u64 size, int numa_node);
1192 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
1193 void bpf_map_area_free(void *base);
1194 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
1195 int generic_map_lookup_batch(struct bpf_map *map,
1196 const union bpf_attr *attr,
1197 union bpf_attr __user *uattr);
1198 int generic_map_update_batch(struct bpf_map *map,
1199 const union bpf_attr *attr,
1200 union bpf_attr __user *uattr);
1201 int generic_map_delete_batch(struct bpf_map *map,
1202 const union bpf_attr *attr,
1203 union bpf_attr __user *uattr);
1204 struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
1205 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id);
1207 extern int sysctl_unprivileged_bpf_disabled;
1209 static inline bool bpf_allow_ptr_leaks(void)
1211 return perfmon_capable();
1214 static inline bool bpf_allow_ptr_to_map_access(void)
1216 return perfmon_capable();
1219 static inline bool bpf_bypass_spec_v1(void)
1221 return perfmon_capable();
1224 static inline bool bpf_bypass_spec_v4(void)
1226 return perfmon_capable();
1229 int bpf_map_new_fd(struct bpf_map *map, int flags);
1230 int bpf_prog_new_fd(struct bpf_prog *prog);
1232 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
1233 const struct bpf_link_ops *ops, struct bpf_prog *prog);
1234 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
1235 int bpf_link_settle(struct bpf_link_primer *primer);
1236 void bpf_link_cleanup(struct bpf_link_primer *primer);
1237 void bpf_link_inc(struct bpf_link *link);
1238 void bpf_link_put(struct bpf_link *link);
1239 int bpf_link_new_fd(struct bpf_link *link);
1240 struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
1241 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
1243 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
1244 int bpf_obj_get_user(const char __user *pathname, int flags);
1246 #define BPF_ITER_FUNC_PREFIX "bpf_iter_"
1247 #define DEFINE_BPF_ITER_FUNC(target, args...) \
1248 extern int bpf_iter_ ## target(args); \
1249 int __init bpf_iter_ ## target(args) { return 0; }
1251 struct bpf_iter_aux_info {
1252 struct bpf_map *map;
1255 typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
1256 union bpf_iter_link_info *linfo,
1257 struct bpf_iter_aux_info *aux);
1258 typedef void (*bpf_iter_detach_target_t)(struct bpf_iter_aux_info *aux);
1259 typedef void (*bpf_iter_show_fdinfo_t) (const struct bpf_iter_aux_info *aux,
1260 struct seq_file *seq);
1261 typedef int (*bpf_iter_fill_link_info_t)(const struct bpf_iter_aux_info *aux,
1262 struct bpf_link_info *info);
1264 #define BPF_ITER_CTX_ARG_MAX 2
1265 struct bpf_iter_reg {
1267 bpf_iter_attach_target_t attach_target;
1268 bpf_iter_detach_target_t detach_target;
1269 bpf_iter_show_fdinfo_t show_fdinfo;
1270 bpf_iter_fill_link_info_t fill_link_info;
1271 u32 ctx_arg_info_size;
1272 struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
1273 const struct bpf_iter_seq_info *seq_info;
1276 struct bpf_iter_meta {
1277 __bpf_md_ptr(struct seq_file *, seq);
1282 struct bpf_iter__bpf_map_elem {
1283 __bpf_md_ptr(struct bpf_iter_meta *, meta);
1284 __bpf_md_ptr(struct bpf_map *, map);
1285 __bpf_md_ptr(void *, key);
1286 __bpf_md_ptr(void *, value);
1289 int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
1290 void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
1291 bool bpf_iter_prog_supported(struct bpf_prog *prog);
1292 int bpf_iter_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
1293 int bpf_iter_new_fd(struct bpf_link *link);
1294 bool bpf_link_is_iter(struct bpf_link *link);
1295 struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
1296 int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx);
1297 void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
1298 struct seq_file *seq);
1299 int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
1300 struct bpf_link_info *info);
1302 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
1303 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
1304 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1306 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
1309 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
1311 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
1312 void *key, void *value, u64 map_flags);
1313 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1314 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1315 void *key, void *value, u64 map_flags);
1316 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1318 int bpf_get_file_flag(int flags);
1319 int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size,
1320 size_t actual_size);
1322 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
1323 * forced to use 'long' read/writes to try to atomically copy long counters.
1324 * Best-effort only. No barriers here, since it _will_ race with concurrent
1325 * updates from BPF programs. Called from bpf syscall and mostly used with
1326 * size 8 or 16 bytes, so ask compiler to inline it.
1328 static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
1330 const long *lsrc = src;
1333 size /= sizeof(long);
1338 /* verify correctness of eBPF program */
1339 int bpf_check(struct bpf_prog **fp, union bpf_attr *attr,
1340 union bpf_attr __user *uattr);
1341 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
1347 struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
1348 struct bpf_dtab_netdev *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key);
1349 void __dev_flush(void);
1350 int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
1351 struct net_device *dev_rx);
1352 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
1353 struct net_device *dev_rx);
1354 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
1355 struct bpf_prog *xdp_prog);
1356 bool dev_map_can_have_prog(struct bpf_map *map);
1358 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
1359 void __cpu_map_flush(void);
1360 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
1361 struct net_device *dev_rx);
1362 bool cpu_map_prog_allowed(struct bpf_map *map);
1364 /* Return map's numa specified by userspace */
1365 static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
1367 return (attr->map_flags & BPF_F_NUMA_NODE) ?
1368 attr->numa_node : NUMA_NO_NODE;
1371 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
1372 int array_map_alloc_check(union bpf_attr *attr);
1374 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1375 union bpf_attr __user *uattr);
1376 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1377 union bpf_attr __user *uattr);
1378 int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1379 const union bpf_attr *kattr,
1380 union bpf_attr __user *uattr);
1381 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1382 const union bpf_attr *kattr,
1383 union bpf_attr __user *uattr);
1384 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
1385 const struct bpf_prog *prog,
1386 struct bpf_insn_access_aux *info);
1387 int btf_struct_access(struct bpf_verifier_log *log,
1388 const struct btf_type *t, int off, int size,
1389 enum bpf_access_type atype,
1391 bool btf_struct_ids_match(struct bpf_verifier_log *log,
1392 int off, u32 id, u32 need_type_id);
1394 int btf_distill_func_proto(struct bpf_verifier_log *log,
1396 const struct btf_type *func_proto,
1397 const char *func_name,
1398 struct btf_func_model *m);
1400 struct bpf_reg_state;
1401 int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
1402 struct bpf_reg_state *regs);
1403 int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
1404 struct bpf_reg_state *reg);
1405 int btf_check_type_match(struct bpf_verifier_env *env, struct bpf_prog *prog,
1406 struct btf *btf, const struct btf_type *t);
1408 struct bpf_prog *bpf_prog_by_id(u32 id);
1409 struct bpf_link *bpf_link_by_id(u32 id);
1411 const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
1412 #else /* !CONFIG_BPF_SYSCALL */
1413 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
1415 return ERR_PTR(-EOPNOTSUPP);
1418 static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
1419 enum bpf_prog_type type,
1422 return ERR_PTR(-EOPNOTSUPP);
1425 static inline void bpf_prog_add(struct bpf_prog *prog, int i)
1429 static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
1433 static inline void bpf_prog_put(struct bpf_prog *prog)
1437 static inline void bpf_prog_inc(struct bpf_prog *prog)
1441 static inline struct bpf_prog *__must_check
1442 bpf_prog_inc_not_zero(struct bpf_prog *prog)
1444 return ERR_PTR(-EOPNOTSUPP);
1447 static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
1452 static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1456 static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
1457 const struct bpf_link_ops *ops,
1458 struct bpf_prog *prog)
1462 static inline int bpf_link_prime(struct bpf_link *link,
1463 struct bpf_link_primer *primer)
1468 static inline int bpf_link_settle(struct bpf_link_primer *primer)
1473 static inline void bpf_link_cleanup(struct bpf_link_primer *primer)
1477 static inline void bpf_link_inc(struct bpf_link *link)
1481 static inline void bpf_link_put(struct bpf_link *link)
1485 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
1490 static inline struct net_device *__dev_map_lookup_elem(struct bpf_map *map,
1496 static inline struct net_device *__dev_map_hash_lookup_elem(struct bpf_map *map,
1501 static inline bool dev_map_can_have_prog(struct bpf_map *map)
1506 static inline void __dev_flush(void)
1511 struct bpf_dtab_netdev;
1514 int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
1515 struct net_device *dev_rx)
1521 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
1522 struct net_device *dev_rx)
1529 static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
1530 struct sk_buff *skb,
1531 struct bpf_prog *xdp_prog)
1537 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
1542 static inline void __cpu_map_flush(void)
1546 static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
1547 struct xdp_buff *xdp,
1548 struct net_device *dev_rx)
1553 static inline bool cpu_map_prog_allowed(struct bpf_map *map)
1558 static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
1559 enum bpf_prog_type type)
1561 return ERR_PTR(-EOPNOTSUPP);
1564 static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
1565 const union bpf_attr *kattr,
1566 union bpf_attr __user *uattr)
1571 static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
1572 const union bpf_attr *kattr,
1573 union bpf_attr __user *uattr)
1578 static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1579 const union bpf_attr *kattr,
1580 union bpf_attr __user *uattr)
1585 static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1586 const union bpf_attr *kattr,
1587 union bpf_attr __user *uattr)
1592 static inline void bpf_map_put(struct bpf_map *map)
1596 static inline struct bpf_prog *bpf_prog_by_id(u32 id)
1598 return ERR_PTR(-ENOTSUPP);
1601 static inline const struct bpf_func_proto *
1602 bpf_base_func_proto(enum bpf_func_id func_id)
1606 #endif /* CONFIG_BPF_SYSCALL */
1608 static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
1609 enum bpf_prog_type type)
1611 return bpf_prog_get_type_dev(ufd, type, false);
1614 bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
1616 int bpf_prog_offload_compile(struct bpf_prog *prog);
1617 void bpf_prog_offload_destroy(struct bpf_prog *prog);
1618 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
1619 struct bpf_prog *prog);
1621 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
1623 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
1624 int bpf_map_offload_update_elem(struct bpf_map *map,
1625 void *key, void *value, u64 flags);
1626 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
1627 int bpf_map_offload_get_next_key(struct bpf_map *map,
1628 void *key, void *next_key);
1630 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
1632 struct bpf_offload_dev *
1633 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
1634 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
1635 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
1636 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
1637 struct net_device *netdev);
1638 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
1639 struct net_device *netdev);
1640 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
1642 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
1643 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
1645 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
1647 return aux->offload_requested;
1650 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1652 return unlikely(map->ops == &bpf_map_offload_ops);
1655 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
1656 void bpf_map_offload_map_free(struct bpf_map *map);
1658 static inline int bpf_prog_offload_init(struct bpf_prog *prog,
1659 union bpf_attr *attr)
1664 static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
1669 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
1674 static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
1676 return ERR_PTR(-EOPNOTSUPP);
1679 static inline void bpf_map_offload_map_free(struct bpf_map *map)
1682 #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
1684 #if defined(CONFIG_BPF_STREAM_PARSER)
1685 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
1686 struct bpf_prog *old, u32 which);
1687 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
1688 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype);
1689 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags);
1690 void sock_map_unhash(struct sock *sk);
1691 void sock_map_close(struct sock *sk, long timeout);
1693 static inline int sock_map_prog_update(struct bpf_map *map,
1694 struct bpf_prog *prog,
1695 struct bpf_prog *old, u32 which)
1700 static inline int sock_map_get_from_fd(const union bpf_attr *attr,
1701 struct bpf_prog *prog)
1706 static inline int sock_map_prog_detach(const union bpf_attr *attr,
1707 enum bpf_prog_type ptype)
1712 static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
1717 #endif /* CONFIG_BPF_STREAM_PARSER */
1719 #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
1720 void bpf_sk_reuseport_detach(struct sock *sk);
1721 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
1723 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
1724 void *value, u64 map_flags);
1726 static inline void bpf_sk_reuseport_detach(struct sock *sk)
1730 #ifdef CONFIG_BPF_SYSCALL
1731 static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
1732 void *key, void *value)
1737 static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
1738 void *key, void *value,
1743 #endif /* CONFIG_BPF_SYSCALL */
1744 #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
1746 /* verifier prototypes for helper functions called from eBPF programs */
1747 extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
1748 extern const struct bpf_func_proto bpf_map_update_elem_proto;
1749 extern const struct bpf_func_proto bpf_map_delete_elem_proto;
1750 extern const struct bpf_func_proto bpf_map_push_elem_proto;
1751 extern const struct bpf_func_proto bpf_map_pop_elem_proto;
1752 extern const struct bpf_func_proto bpf_map_peek_elem_proto;
1754 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
1755 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
1756 extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
1757 extern const struct bpf_func_proto bpf_tail_call_proto;
1758 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
1759 extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
1760 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
1761 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
1762 extern const struct bpf_func_proto bpf_get_current_comm_proto;
1763 extern const struct bpf_func_proto bpf_get_stackid_proto;
1764 extern const struct bpf_func_proto bpf_get_stack_proto;
1765 extern const struct bpf_func_proto bpf_get_task_stack_proto;
1766 extern const struct bpf_func_proto bpf_get_stackid_proto_pe;
1767 extern const struct bpf_func_proto bpf_get_stack_proto_pe;
1768 extern const struct bpf_func_proto bpf_sock_map_update_proto;
1769 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
1770 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
1771 extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
1772 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
1773 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
1774 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
1775 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
1776 extern const struct bpf_func_proto bpf_spin_lock_proto;
1777 extern const struct bpf_func_proto bpf_spin_unlock_proto;
1778 extern const struct bpf_func_proto bpf_get_local_storage_proto;
1779 extern const struct bpf_func_proto bpf_strtol_proto;
1780 extern const struct bpf_func_proto bpf_strtoul_proto;
1781 extern const struct bpf_func_proto bpf_tcp_sock_proto;
1782 extern const struct bpf_func_proto bpf_jiffies64_proto;
1783 extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
1784 extern const struct bpf_func_proto bpf_event_output_data_proto;
1785 extern const struct bpf_func_proto bpf_ringbuf_output_proto;
1786 extern const struct bpf_func_proto bpf_ringbuf_reserve_proto;
1787 extern const struct bpf_func_proto bpf_ringbuf_submit_proto;
1788 extern const struct bpf_func_proto bpf_ringbuf_discard_proto;
1789 extern const struct bpf_func_proto bpf_ringbuf_query_proto;
1790 extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
1791 extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
1792 extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
1793 extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
1794 extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
1795 extern const struct bpf_func_proto bpf_copy_from_user_proto;
1797 const struct bpf_func_proto *bpf_tracing_func_proto(
1798 enum bpf_func_id func_id, const struct bpf_prog *prog);
1800 const struct bpf_func_proto *tracing_prog_func_proto(
1801 enum bpf_func_id func_id, const struct bpf_prog *prog);
1803 /* Shared helpers among cBPF and eBPF. */
1804 void bpf_user_rnd_init_once(void);
1805 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1806 u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
1808 #if defined(CONFIG_NET)
1809 bool bpf_sock_common_is_valid_access(int off, int size,
1810 enum bpf_access_type type,
1811 struct bpf_insn_access_aux *info);
1812 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1813 struct bpf_insn_access_aux *info);
1814 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1815 const struct bpf_insn *si,
1816 struct bpf_insn *insn_buf,
1817 struct bpf_prog *prog,
1820 static inline bool bpf_sock_common_is_valid_access(int off, int size,
1821 enum bpf_access_type type,
1822 struct bpf_insn_access_aux *info)
1826 static inline bool bpf_sock_is_valid_access(int off, int size,
1827 enum bpf_access_type type,
1828 struct bpf_insn_access_aux *info)
1832 static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
1833 const struct bpf_insn *si,
1834 struct bpf_insn *insn_buf,
1835 struct bpf_prog *prog,
1843 struct sk_reuseport_kern {
1844 struct sk_buff *skb;
1846 struct sock *selected_sk;
1852 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1853 struct bpf_insn_access_aux *info);
1855 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1856 const struct bpf_insn *si,
1857 struct bpf_insn *insn_buf,
1858 struct bpf_prog *prog,
1861 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
1862 struct bpf_insn_access_aux *info);
1864 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1865 const struct bpf_insn *si,
1866 struct bpf_insn *insn_buf,
1867 struct bpf_prog *prog,
1870 static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
1871 enum bpf_access_type type,
1872 struct bpf_insn_access_aux *info)
1877 static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
1878 const struct bpf_insn *si,
1879 struct bpf_insn *insn_buf,
1880 struct bpf_prog *prog,
1885 static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
1886 enum bpf_access_type type,
1887 struct bpf_insn_access_aux *info)
1892 static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
1893 const struct bpf_insn *si,
1894 struct bpf_insn *insn_buf,
1895 struct bpf_prog *prog,
1900 #endif /* CONFIG_INET */
1902 enum bpf_text_poke_type {
1907 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
1908 void *addr1, void *addr2);
1911 bool btf_id_set_contains(const struct btf_id_set *set, u32 id);
1913 #endif /* _LINUX_BPF_H */