xref: /linux/include/linux/bpf.h (revision 17637e1a581a22466ac3620a91e099683ff9cc6f)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3  */
4 #ifndef _LINUX_BPF_H
5 #define _LINUX_BPF_H 1
6 
7 #include <uapi/linux/bpf.h>
8 #include <uapi/linux/filter.h>
9 #include <linux/bpf_defs.h>
10 
11 #include <crypto/sha2.h>
12 #include <linux/workqueue.h>
13 #include <linux/file.h>
14 #include <linux/percpu.h>
15 #include <linux/err.h>
16 #include <linux/rbtree_latch.h>
17 #include <linux/numa.h>
18 #include <linux/mm_types.h>
19 #include <linux/wait.h>
20 #include <linux/refcount.h>
21 #include <linux/mutex.h>
22 #include <linux/module.h>
23 #include <linux/kallsyms.h>
24 #include <linux/capability.h>
25 #include <linux/sched/mm.h>
26 #include <linux/slab.h>
27 #include <linux/percpu-refcount.h>
28 #include <linux/stddef.h>
29 #include <linux/bpfptr.h>
30 #include <linux/btf.h>
31 #include <linux/rcupdate_trace.h>
32 #include <linux/static_call.h>
33 #include <linux/memcontrol.h>
34 #include <linux/cfi.h>
35 #include <linux/xattr.h>
36 #include <linux/key.h>
37 #include <linux/ftrace.h>
38 #include <asm/rqspinlock.h>
39 
40 struct bpf_verifier_env;
41 struct bpf_verifier_log;
42 struct perf_event;
43 struct bpf_prog;
44 struct bpf_prog_aux;
45 struct bpf_map;
46 struct bpf_arena;
47 struct sock;
48 struct seq_file;
49 struct btf;
50 struct btf_type;
51 struct exception_table_entry;
52 struct seq_operations;
53 struct bpf_iter_aux_info;
54 struct bpf_local_storage;
55 struct bpf_local_storage_map;
56 struct kobject;
57 struct mem_cgroup;
58 struct module;
59 struct bpf_func_state;
60 struct ftrace_ops;
61 struct cgroup;
62 struct bpf_token;
63 struct user_namespace;
64 struct super_block;
65 struct inode;
66 
67 extern struct idr btf_idr;
68 extern spinlock_t btf_idr_lock;
69 extern struct kobject *btf_kobj;
70 extern struct bpf_mem_alloc bpf_global_ma, bpf_global_percpu_ma;
71 extern bool bpf_global_ma_set;
72 
73 typedef u64 (*bpf_callback_t)(u64, u64, u64, u64, u64);
74 typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,
75 					struct bpf_iter_aux_info *aux);
76 typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
77 typedef unsigned int (*bpf_func_t)(const void *,
78 				   const struct bpf_insn *);
79 struct bpf_iter_seq_info {
80 	const struct seq_operations *seq_ops;
81 	bpf_iter_init_seq_priv_t init_seq_private;
82 	bpf_iter_fini_seq_priv_t fini_seq_private;
83 	u32 seq_priv_size;
84 };
85 
86 /* map is generic key/value storage optionally accessible by eBPF programs */
87 struct bpf_map_ops {
88 	/* funcs callable from userspace (via syscall) */
89 	int (*map_alloc_check)(union bpf_attr *attr);
90 	struct bpf_map *(*map_alloc)(union bpf_attr *attr);
91 	void (*map_release)(struct bpf_map *map, struct file *map_file);
92 	void (*map_free)(struct bpf_map *map);
93 	int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
94 	void (*map_release_uref)(struct bpf_map *map);
95 	void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
96 	int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr,
97 				union bpf_attr __user *uattr);
98 	int (*map_lookup_and_delete_elem)(struct bpf_map *map, void *key,
99 					  void *value, u64 flags);
100 	int (*map_lookup_and_delete_batch)(struct bpf_map *map,
101 					   const union bpf_attr *attr,
102 					   union bpf_attr __user *uattr);
103 	int (*map_update_batch)(struct bpf_map *map, struct file *map_file,
104 				const union bpf_attr *attr,
105 				union bpf_attr __user *uattr);
106 	int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr,
107 				union bpf_attr __user *uattr);
108 
109 	/* funcs callable from userspace and from eBPF programs */
110 	void *(*map_lookup_elem)(struct bpf_map *map, void *key);
111 	long (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
112 	long (*map_delete_elem)(struct bpf_map *map, void *key);
113 	long (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
114 	long (*map_pop_elem)(struct bpf_map *map, void *value);
115 	long (*map_peek_elem)(struct bpf_map *map, void *value);
116 	void *(*map_lookup_percpu_elem)(struct bpf_map *map, void *key, u32 cpu);
117 	int (*map_get_hash)(struct bpf_map *map);
118 
119 	/* funcs called by prog_array and perf_event_array map */
120 	void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
121 				int fd);
122 	/* If need_defer is true, the implementation should guarantee that
123 	 * the to-be-put element is still alive before the bpf program, which
124 	 * may manipulate it, exists.
125 	 */
126 	void (*map_fd_put_ptr)(struct bpf_map *map, void *ptr, bool need_defer);
127 	int (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
128 	u32 (*map_fd_sys_lookup_elem)(void *ptr);
129 	void (*map_seq_show_elem)(struct bpf_map *map, void *key,
130 				  struct seq_file *m);
131 	int (*map_check_btf)(struct bpf_map *map,
132 			     const struct btf *btf,
133 			     const struct btf_type *key_type,
134 			     const struct btf_type *value_type);
135 
136 	/* Prog poke tracking helpers. */
137 	int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux);
138 	void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux);
139 	void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old,
140 			     struct bpf_prog *new);
141 
142 	/* Direct value access helpers. */
143 	int (*map_direct_value_addr)(const struct bpf_map *map,
144 				     u64 *imm, u32 off);
145 	int (*map_direct_value_meta)(const struct bpf_map *map,
146 				     u64 imm, u32 *off);
147 	int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
148 	vm_fault_t (*map_mmap_fault)(struct bpf_map *map, struct vm_fault *vmf);
149 	__poll_t (*map_poll)(struct bpf_map *map, struct file *filp,
150 			     struct poll_table_struct *pts);
151 	unsigned long (*map_get_unmapped_area)(struct file *filep, unsigned long addr,
152 					       unsigned long len, unsigned long pgoff,
153 					       unsigned long flags);
154 
155 	/* Functions called by bpf_local_storage maps */
156 	int (*map_local_storage_charge)(struct bpf_local_storage_map *smap,
157 					void *owner, u32 size);
158 	void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap,
159 					   void *owner, u32 size);
160 	struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(void *owner);
161 
162 	/* Misc helpers.*/
163 	long (*map_redirect)(struct bpf_map *map, u64 key, u64 flags);
164 
165 	/* map_meta_equal must be implemented for maps that can be
166 	 * used as an inner map.  It is a runtime check to ensure
167 	 * an inner map can be inserted to an outer map.
168 	 *
169 	 * Some properties of the inner map has been used during the
170 	 * verification time.  When inserting an inner map at the runtime,
171 	 * map_meta_equal has to ensure the inserting map has the same
172 	 * properties that the verifier has used earlier.
173 	 */
174 	bool (*map_meta_equal)(const struct bpf_map *meta0,
175 			       const struct bpf_map *meta1);
176 
177 
178 	int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,
179 					      struct bpf_func_state *caller,
180 					      struct bpf_func_state *callee);
181 	long (*map_for_each_callback)(struct bpf_map *map,
182 				     bpf_callback_t callback_fn,
183 				     void *callback_ctx, u64 flags);
184 
185 	u64 (*map_mem_usage)(const struct bpf_map *map);
186 
187 	/* BTF id of struct allocated by map_alloc */
188 	int *map_btf_id;
189 
190 	/* bpf_iter info used to open a seq_file */
191 	const struct bpf_iter_seq_info *iter_seq_info;
192 };
193 
194 enum {
195 	/* Support at most 11 fields in a BTF type */
196 	BTF_FIELDS_MAX	   = 11,
197 };
198 
199 enum btf_field_type {
200 	BPF_SPIN_LOCK  = (1 << 0),
201 	BPF_TIMER      = (1 << 1),
202 	BPF_KPTR_UNREF = (1 << 2),
203 	BPF_KPTR_REF   = (1 << 3),
204 	BPF_KPTR_PERCPU = (1 << 4),
205 	BPF_KPTR       = BPF_KPTR_UNREF | BPF_KPTR_REF | BPF_KPTR_PERCPU,
206 	BPF_LIST_HEAD  = (1 << 5),
207 	BPF_LIST_NODE  = (1 << 6),
208 	BPF_RB_ROOT    = (1 << 7),
209 	BPF_RB_NODE    = (1 << 8),
210 	BPF_GRAPH_NODE = BPF_RB_NODE | BPF_LIST_NODE,
211 	BPF_GRAPH_ROOT = BPF_RB_ROOT | BPF_LIST_HEAD,
212 	BPF_REFCOUNT   = (1 << 9),
213 	BPF_WORKQUEUE  = (1 << 10),
214 	BPF_UPTR       = (1 << 11),
215 	BPF_RES_SPIN_LOCK = (1 << 12),
216 	BPF_TASK_WORK  = (1 << 13),
217 };
218 
219 enum bpf_cgroup_storage_type {
220 	BPF_CGROUP_STORAGE_SHARED,
221 	BPF_CGROUP_STORAGE_PERCPU,
222 	__BPF_CGROUP_STORAGE_MAX
223 #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
224 };
225 
226 #ifdef CONFIG_CGROUP_BPF
227 # define for_each_cgroup_storage_type(stype) \
228 	for (stype = 0; stype < MAX_BPF_CGROUP_STORAGE_TYPE; stype++)
229 #else
230 # define for_each_cgroup_storage_type(stype) for (; false; )
231 #endif /* CONFIG_CGROUP_BPF */
232 
233 typedef void (*btf_dtor_kfunc_t)(void *);
234 
235 struct btf_field_kptr {
236 	struct btf *btf;
237 	struct module *module;
238 	/* dtor used if btf_is_kernel(btf), otherwise the type is
239 	 * program-allocated, dtor is NULL,  and __bpf_obj_drop_impl is used
240 	 */
241 	btf_dtor_kfunc_t dtor;
242 	u32 btf_id;
243 };
244 
245 struct btf_field_graph_root {
246 	struct btf *btf;
247 	u32 value_btf_id;
248 	u32 node_offset;
249 	struct btf_record *value_rec;
250 };
251 
252 struct btf_field {
253 	u32 offset;
254 	u32 size;
255 	enum btf_field_type type;
256 	union {
257 		struct btf_field_kptr kptr;
258 		struct btf_field_graph_root graph_root;
259 	};
260 };
261 
262 struct btf_record {
263 	u32 cnt;
264 	u32 field_mask;
265 	int spin_lock_off;
266 	int res_spin_lock_off;
267 	int timer_off;
268 	int wq_off;
269 	int refcount_off;
270 	int task_work_off;
271 	struct btf_field fields[];
272 };
273 
274 /* Non-opaque version of bpf_rb_node in uapi/linux/bpf.h */
275 struct bpf_rb_node_kern {
276 	struct rb_node rb_node;
277 	void *owner;
278 } __attribute__((aligned(8)));
279 
280 /* Non-opaque version of bpf_list_node in uapi/linux/bpf.h */
281 struct bpf_list_node_kern {
282 	struct list_head list_head;
283 	void *owner;
284 } __attribute__((aligned(8)));
285 
286 /* 'Ownership' of program-containing map is claimed by the first program
287  * that is going to use this map or by the first program which FD is
288  * stored in the map to make sure that all callers and callees have the
289  * same prog type, JITed flag and xdp_has_frags flag.
290  */
291 struct bpf_map_owner {
292 	enum bpf_prog_type type;
293 	bool jited;
294 	bool xdp_has_frags;
295 	bool sleepable;
296 	u64 storage_cookie[MAX_BPF_CGROUP_STORAGE_TYPE];
297 	const struct btf_type *attach_func_proto;
298 	enum bpf_attach_type expected_attach_type;
299 };
300 
301 struct bpf_map {
302 	u8 sha[SHA256_DIGEST_SIZE];
303 	const struct bpf_map_ops *ops;
304 	struct bpf_map *inner_map_meta;
305 #ifdef CONFIG_SECURITY
306 	void *security;
307 #endif
308 	enum bpf_map_type map_type;
309 	u32 key_size;
310 	u32 value_size;
311 	u32 max_entries;
312 	u64 map_extra; /* any per-map-type extra fields */
313 	u32 map_flags;
314 	u32 id;
315 	struct btf_record *record;
316 	int numa_node;
317 	u32 btf_key_type_id;
318 	u32 btf_value_type_id;
319 	u32 btf_vmlinux_value_type_id;
320 	struct btf *btf;
321 #ifdef CONFIG_MEMCG
322 	struct obj_cgroup *objcg;
323 #endif
324 	char name[BPF_OBJ_NAME_LEN];
325 	struct mutex freeze_mutex;
326 	atomic64_t refcnt;
327 	atomic64_t usercnt;
328 	/* rcu is used before freeing and work is only used during freeing */
329 	union {
330 		struct work_struct work;
331 		struct rcu_head rcu;
332 	};
333 	atomic64_t writecnt;
334 	spinlock_t owner_lock;
335 	struct bpf_map_owner *owner;
336 	bool bypass_spec_v1;
337 	bool frozen; /* write-once; write-protected by freeze_mutex */
338 	bool free_after_mult_rcu_gp;
339 	bool free_after_rcu_gp;
340 	atomic64_t sleepable_refcnt;
341 	s64 __percpu *elem_count;
342 	u64 cookie; /* write-once */
343 	char *excl_prog_sha;
344 };
345 
346 static inline const char *btf_field_type_name(enum btf_field_type type)
347 {
348 	switch (type) {
349 	case BPF_SPIN_LOCK:
350 		return "bpf_spin_lock";
351 	case BPF_RES_SPIN_LOCK:
352 		return "bpf_res_spin_lock";
353 	case BPF_TIMER:
354 		return "bpf_timer";
355 	case BPF_WORKQUEUE:
356 		return "bpf_wq";
357 	case BPF_KPTR_UNREF:
358 	case BPF_KPTR_REF:
359 		return "kptr";
360 	case BPF_KPTR_PERCPU:
361 		return "percpu_kptr";
362 	case BPF_UPTR:
363 		return "uptr";
364 	case BPF_LIST_HEAD:
365 		return "bpf_list_head";
366 	case BPF_LIST_NODE:
367 		return "bpf_list_node";
368 	case BPF_RB_ROOT:
369 		return "bpf_rb_root";
370 	case BPF_RB_NODE:
371 		return "bpf_rb_node";
372 	case BPF_REFCOUNT:
373 		return "bpf_refcount";
374 	case BPF_TASK_WORK:
375 		return "bpf_task_work";
376 	default:
377 		WARN_ON_ONCE(1);
378 		return "unknown";
379 	}
380 }
381 
382 #if IS_ENABLED(CONFIG_DEBUG_KERNEL)
383 #define BPF_WARN_ONCE(cond, format...) WARN_ONCE(cond, format)
384 #else
385 #define BPF_WARN_ONCE(cond, format...) BUILD_BUG_ON_INVALID(cond)
386 #endif
387 
388 static inline u32 btf_field_type_size(enum btf_field_type type)
389 {
390 	switch (type) {
391 	case BPF_SPIN_LOCK:
392 		return sizeof(struct bpf_spin_lock);
393 	case BPF_RES_SPIN_LOCK:
394 		return sizeof(struct bpf_res_spin_lock);
395 	case BPF_TIMER:
396 		return sizeof(struct bpf_timer);
397 	case BPF_WORKQUEUE:
398 		return sizeof(struct bpf_wq);
399 	case BPF_KPTR_UNREF:
400 	case BPF_KPTR_REF:
401 	case BPF_KPTR_PERCPU:
402 	case BPF_UPTR:
403 		return sizeof(u64);
404 	case BPF_LIST_HEAD:
405 		return sizeof(struct bpf_list_head);
406 	case BPF_LIST_NODE:
407 		return sizeof(struct bpf_list_node);
408 	case BPF_RB_ROOT:
409 		return sizeof(struct bpf_rb_root);
410 	case BPF_RB_NODE:
411 		return sizeof(struct bpf_rb_node);
412 	case BPF_REFCOUNT:
413 		return sizeof(struct bpf_refcount);
414 	case BPF_TASK_WORK:
415 		return sizeof(struct bpf_task_work);
416 	default:
417 		WARN_ON_ONCE(1);
418 		return 0;
419 	}
420 }
421 
422 static inline u32 btf_field_type_align(enum btf_field_type type)
423 {
424 	switch (type) {
425 	case BPF_SPIN_LOCK:
426 		return __alignof__(struct bpf_spin_lock);
427 	case BPF_RES_SPIN_LOCK:
428 		return __alignof__(struct bpf_res_spin_lock);
429 	case BPF_TIMER:
430 		return __alignof__(struct bpf_timer);
431 	case BPF_WORKQUEUE:
432 		return __alignof__(struct bpf_wq);
433 	case BPF_KPTR_UNREF:
434 	case BPF_KPTR_REF:
435 	case BPF_KPTR_PERCPU:
436 	case BPF_UPTR:
437 		return __alignof__(u64);
438 	case BPF_LIST_HEAD:
439 		return __alignof__(struct bpf_list_head);
440 	case BPF_LIST_NODE:
441 		return __alignof__(struct bpf_list_node);
442 	case BPF_RB_ROOT:
443 		return __alignof__(struct bpf_rb_root);
444 	case BPF_RB_NODE:
445 		return __alignof__(struct bpf_rb_node);
446 	case BPF_REFCOUNT:
447 		return __alignof__(struct bpf_refcount);
448 	case BPF_TASK_WORK:
449 		return __alignof__(struct bpf_task_work);
450 	default:
451 		WARN_ON_ONCE(1);
452 		return 0;
453 	}
454 }
455 
456 static inline void bpf_obj_init_field(const struct btf_field *field, void *addr)
457 {
458 	memset(addr, 0, field->size);
459 
460 	switch (field->type) {
461 	case BPF_REFCOUNT:
462 		refcount_set((refcount_t *)addr, 1);
463 		break;
464 	case BPF_RB_NODE:
465 		RB_CLEAR_NODE((struct rb_node *)addr);
466 		break;
467 	case BPF_LIST_HEAD:
468 	case BPF_LIST_NODE:
469 		INIT_LIST_HEAD((struct list_head *)addr);
470 		break;
471 	case BPF_RB_ROOT:
472 		/* RB_ROOT_CACHED 0-inits, no need to do anything after memset */
473 	case BPF_SPIN_LOCK:
474 	case BPF_RES_SPIN_LOCK:
475 	case BPF_TIMER:
476 	case BPF_WORKQUEUE:
477 	case BPF_KPTR_UNREF:
478 	case BPF_KPTR_REF:
479 	case BPF_KPTR_PERCPU:
480 	case BPF_UPTR:
481 	case BPF_TASK_WORK:
482 		break;
483 	default:
484 		WARN_ON_ONCE(1);
485 		return;
486 	}
487 }
488 
489 static inline bool btf_record_has_field(const struct btf_record *rec, enum btf_field_type type)
490 {
491 	if (IS_ERR_OR_NULL(rec))
492 		return false;
493 	return rec->field_mask & type;
494 }
495 
496 static inline bool btf_field_is_nmi_safe(enum btf_field_type type)
497 {
498 	switch (type) {
499 	case BPF_SPIN_LOCK:
500 	case BPF_RES_SPIN_LOCK:
501 	case BPF_TIMER:
502 	case BPF_WORKQUEUE:
503 	case BPF_TASK_WORK:
504 	case BPF_KPTR_UNREF:
505 	case BPF_REFCOUNT:
506 		return true;
507 	default:
508 		return false;
509 	}
510 }
511 
512 static inline bool btf_record_has_nmi_unsafe_fields(const struct btf_record *rec)
513 {
514 	int i;
515 
516 	if (IS_ERR_OR_NULL(rec))
517 		return false;
518 	for (i = 0; i < rec->cnt; i++) {
519 		if (!btf_field_is_nmi_safe(rec->fields[i].type))
520 			return true;
521 	}
522 	return false;
523 }
524 
525 static inline void bpf_obj_init(const struct btf_record *rec, void *obj)
526 {
527 	int i;
528 
529 	if (IS_ERR_OR_NULL(rec))
530 		return;
531 	for (i = 0; i < rec->cnt; i++)
532 		bpf_obj_init_field(&rec->fields[i], obj + rec->fields[i].offset);
533 }
534 
535 /* 'dst' must be a temporary buffer and should not point to memory that is being
536  * used in parallel by a bpf program or bpf syscall, otherwise the access from
537  * the bpf program or bpf syscall may be corrupted by the reinitialization,
538  * leading to weird problems. Even 'dst' is newly-allocated from bpf memory
539  * allocator, it is still possible for 'dst' to be used in parallel by a bpf
540  * program or bpf syscall.
541  */
542 static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
543 {
544 	bpf_obj_init(map->record, dst);
545 }
546 
547 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
548  * forced to use 'long' read/writes to try to atomically copy long counters.
549  * Best-effort only.  No barriers here, since it _will_ race with concurrent
550  * updates from BPF programs. Called from bpf syscall and mostly used with
551  * size 8 or 16 bytes, so ask compiler to inline it.
552  */
553 static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
554 {
555 	const long *lsrc = src;
556 	long *ldst = dst;
557 
558 	size /= sizeof(long);
559 	while (size--)
560 		data_race(*ldst++ = *lsrc++);
561 }
562 
563 /* copy everything but bpf_spin_lock, bpf_timer, and kptrs. There could be one of each. */
564 static inline void bpf_obj_memcpy(struct btf_record *rec,
565 				  void *dst, void *src, u32 size,
566 				  bool long_memcpy)
567 {
568 	u32 curr_off = 0;
569 	int i;
570 
571 	if (IS_ERR_OR_NULL(rec)) {
572 		if (long_memcpy)
573 			bpf_long_memcpy(dst, src, size);
574 		else
575 			memcpy(dst, src, size);
576 		return;
577 	}
578 
579 	for (i = 0; i < rec->cnt; i++) {
580 		u32 next_off = rec->fields[i].offset;
581 		u32 sz = next_off - curr_off;
582 
583 		memcpy(dst + curr_off, src + curr_off, sz);
584 		curr_off += rec->fields[i].size + sz;
585 	}
586 	memcpy(dst + curr_off, src + curr_off, size - curr_off);
587 }
588 
589 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
590 {
591 	bpf_obj_memcpy(map->record, dst, src, map->value_size, false);
592 }
593 
594 static inline void copy_map_value_long(struct bpf_map *map, void *dst, void *src)
595 {
596 	bpf_obj_memcpy(map->record, dst, src, round_up(map->value_size, 8), true);
597 }
598 
599 static inline void bpf_obj_swap_uptrs(const struct btf_record *rec, void *dst, void *src)
600 {
601 	unsigned long *src_uptr, *dst_uptr;
602 	const struct btf_field *field;
603 	int i;
604 
605 	if (!btf_record_has_field(rec, BPF_UPTR))
606 		return;
607 
608 	for (i = 0, field = rec->fields; i < rec->cnt; i++, field++) {
609 		if (field->type != BPF_UPTR)
610 			continue;
611 
612 		src_uptr = src + field->offset;
613 		dst_uptr = dst + field->offset;
614 		swap(*src_uptr, *dst_uptr);
615 	}
616 }
617 
618 static inline void bpf_obj_memzero(struct btf_record *rec, void *dst, u32 size)
619 {
620 	u32 curr_off = 0;
621 	int i;
622 
623 	if (IS_ERR_OR_NULL(rec)) {
624 		memset(dst, 0, size);
625 		return;
626 	}
627 
628 	for (i = 0; i < rec->cnt; i++) {
629 		u32 next_off = rec->fields[i].offset;
630 		u32 sz = next_off - curr_off;
631 
632 		memset(dst + curr_off, 0, sz);
633 		curr_off += rec->fields[i].size + sz;
634 	}
635 	memset(dst + curr_off, 0, size - curr_off);
636 }
637 
638 static inline void zero_map_value(struct bpf_map *map, void *dst)
639 {
640 	bpf_obj_memzero(map->record, dst, map->value_size);
641 }
642 
643 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
644 			   bool lock_src);
645 void bpf_timer_cancel_and_free(void *timer);
646 void bpf_wq_cancel_and_free(void *timer);
647 void bpf_task_work_cancel_and_free(void *timer);
648 void bpf_list_head_free(const struct btf_field *field, void *list_head,
649 			struct bpf_spin_lock *spin_lock);
650 void bpf_rb_root_free(const struct btf_field *field, void *rb_root,
651 		      struct bpf_spin_lock *spin_lock);
652 u64 bpf_arena_get_kern_vm_start(struct bpf_arena *arena);
653 u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena);
654 u64 bpf_arena_map_kern_vm_start(struct bpf_map *map);
655 struct bpf_map *bpf_prog_arena(struct bpf_prog *prog);
656 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
657 
658 struct bpf_offload_dev;
659 struct bpf_offloaded_map;
660 
661 struct bpf_map_dev_ops {
662 	int (*map_get_next_key)(struct bpf_offloaded_map *map,
663 				void *key, void *next_key);
664 	int (*map_lookup_elem)(struct bpf_offloaded_map *map,
665 			       void *key, void *value);
666 	int (*map_update_elem)(struct bpf_offloaded_map *map,
667 			       void *key, void *value, u64 flags);
668 	int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
669 };
670 
671 struct bpf_offloaded_map {
672 	struct bpf_map map;
673 	struct net_device *netdev;
674 	const struct bpf_map_dev_ops *dev_ops;
675 	void *dev_priv;
676 	struct list_head offloads;
677 };
678 
679 static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
680 {
681 	return container_of(map, struct bpf_offloaded_map, map);
682 }
683 
684 static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
685 {
686 	return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
687 }
688 
689 static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
690 {
691 	return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) &&
692 		map->ops->map_seq_show_elem;
693 }
694 
695 int map_check_no_btf(struct bpf_map *map,
696 		     const struct btf *btf,
697 		     const struct btf_type *key_type,
698 		     const struct btf_type *value_type);
699 
700 bool bpf_map_meta_equal(const struct bpf_map *meta0,
701 			const struct bpf_map *meta1);
702 
703 static inline bool bpf_map_has_internal_structs(struct bpf_map *map)
704 {
705 	return btf_record_has_field(map->record, BPF_TIMER | BPF_WORKQUEUE | BPF_TASK_WORK);
706 }
707 
708 void bpf_map_free_internal_structs(struct bpf_map *map, void *obj);
709 
710 int bpf_dynptr_from_file_sleepable(struct file *file, u32 flags,
711 				   struct bpf_dynptr *ptr__uninit);
712 
713 #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
714 void *bpf_arena_alloc_pages_non_sleepable(void *p__map, void *addr__ign, u32 page_cnt, int node_id,
715 					  u64 flags);
716 void bpf_arena_free_pages_non_sleepable(void *p__map, void *ptr__ign, u32 page_cnt);
717 void *bpf_arena_alloc_pages_sleepable(void *p__map, void *addr__ign, u32 page_cnt, int node_id,
718 				      u64 flags);
719 #else
720 static inline void *bpf_arena_alloc_pages_non_sleepable(void *p__map, void *addr__ign, u32 page_cnt,
721 							int node_id, u64 flags)
722 {
723 	return NULL;
724 }
725 
726 static inline void bpf_arena_free_pages_non_sleepable(void *p__map, void *ptr__ign, u32 page_cnt)
727 {
728 }
729 
730 static inline void *bpf_arena_alloc_pages_sleepable(void *p__map, void *addr__ign, u32 page_cnt,
731 						    int node_id, u64 flags)
732 {
733 	return NULL;
734 }
735 #endif
736 
737 extern const struct bpf_map_ops bpf_map_offload_ops;
738 
739 /* bpf_type_flag contains a set of flags that are applicable to the values of
740  * arg_type, ret_type and reg_type. For example, a pointer value may be null,
741  * or a memory is read-only. We classify types into two categories: base types
742  * and extended types. Extended types are base types combined with a type flag.
743  *
744  * Currently there are no more than 32 base types in arg_type, ret_type and
745  * reg_types.
746  */
747 #define BPF_BASE_TYPE_BITS	8
748 
749 enum bpf_type_flag {
750 	/* PTR may be NULL. */
751 	PTR_MAYBE_NULL		= BIT(0 + BPF_BASE_TYPE_BITS),
752 
753 	/* MEM is read-only. When applied on bpf_arg, it indicates the arg is
754 	 * compatible with both mutable and immutable memory.
755 	 */
756 	MEM_RDONLY		= BIT(1 + BPF_BASE_TYPE_BITS),
757 
758 	/* MEM points to BPF ring buffer reservation. */
759 	MEM_RINGBUF		= BIT(2 + BPF_BASE_TYPE_BITS),
760 
761 	/* MEM is in user address space. */
762 	MEM_USER		= BIT(3 + BPF_BASE_TYPE_BITS),
763 
764 	/* MEM is a percpu memory. MEM_PERCPU tags PTR_TO_BTF_ID. When tagged
765 	 * with MEM_PERCPU, PTR_TO_BTF_ID _cannot_ be directly accessed. In
766 	 * order to drop this tag, it must be passed into bpf_per_cpu_ptr()
767 	 * or bpf_this_cpu_ptr(), which will return the pointer corresponding
768 	 * to the specified cpu.
769 	 */
770 	MEM_PERCPU		= BIT(4 + BPF_BASE_TYPE_BITS),
771 
772 	/* Indicates that the argument will be released. */
773 	OBJ_RELEASE		= BIT(5 + BPF_BASE_TYPE_BITS),
774 
775 	/* PTR is not trusted. This is only used with PTR_TO_BTF_ID, to mark
776 	 * unreferenced and referenced kptr loaded from map value using a load
777 	 * instruction, so that they can only be dereferenced but not escape the
778 	 * BPF program into the kernel (i.e. cannot be passed as arguments to
779 	 * kfunc or bpf helpers).
780 	 */
781 	PTR_UNTRUSTED		= BIT(6 + BPF_BASE_TYPE_BITS),
782 
783 	/* MEM can be uninitialized. */
784 	MEM_UNINIT		= BIT(7 + BPF_BASE_TYPE_BITS),
785 
786 	/* DYNPTR points to memory local to the bpf program. */
787 	DYNPTR_TYPE_LOCAL	= BIT(8 + BPF_BASE_TYPE_BITS),
788 
789 	/* DYNPTR points to a kernel-produced ringbuf record. */
790 	DYNPTR_TYPE_RINGBUF	= BIT(9 + BPF_BASE_TYPE_BITS),
791 
792 	/* Size is known at compile time. */
793 	MEM_FIXED_SIZE		= BIT(10 + BPF_BASE_TYPE_BITS),
794 
795 	/* MEM is of an allocated object of type in program BTF. This is used to
796 	 * tag PTR_TO_BTF_ID allocated using bpf_obj_new.
797 	 */
798 	MEM_ALLOC		= BIT(11 + BPF_BASE_TYPE_BITS),
799 
800 	/* PTR was passed from the kernel in a trusted context, and may be
801 	 * passed to kfuncs or BPF helper functions.
802 	 * Confusingly, this is _not_ the opposite of PTR_UNTRUSTED above.
803 	 * PTR_UNTRUSTED refers to a kptr that was read directly from a map
804 	 * without invoking bpf_kptr_xchg(). What we really need to know is
805 	 * whether a pointer is safe to pass to a kfunc or BPF helper function.
806 	 * While PTR_UNTRUSTED pointers are unsafe to pass to kfuncs and BPF
807 	 * helpers, they do not cover all possible instances of unsafe
808 	 * pointers. For example, a pointer that was obtained from walking a
809 	 * struct will _not_ get the PTR_UNTRUSTED type modifier, despite the
810 	 * fact that it may be NULL, invalid, etc. This is due to backwards
811 	 * compatibility requirements, as this was the behavior that was first
812 	 * introduced when kptrs were added. The behavior is now considered
813 	 * deprecated, and PTR_UNTRUSTED will eventually be removed.
814 	 *
815 	 * PTR_TRUSTED, on the other hand, is a pointer that the kernel
816 	 * guarantees to be valid and safe to pass to kfuncs and BPF helpers.
817 	 * For example, pointers passed to tracepoint arguments are considered
818 	 * PTR_TRUSTED, as are pointers that are passed to struct_ops
819 	 * callbacks. As alluded to above, pointers that are obtained from
820 	 * walking PTR_TRUSTED pointers are _not_ trusted. For example, if a
821 	 * struct task_struct *task is PTR_TRUSTED, then accessing
822 	 * task->last_wakee will lose the PTR_TRUSTED modifier when it's stored
823 	 * in a BPF register. Similarly, pointers passed to certain programs
824 	 * types such as kretprobes are not guaranteed to be valid, as they may
825 	 * for example contain an object that was recently freed.
826 	 */
827 	PTR_TRUSTED		= BIT(12 + BPF_BASE_TYPE_BITS),
828 
829 	/* MEM is tagged with rcu and memory access needs rcu_read_lock protection. */
830 	MEM_RCU			= BIT(13 + BPF_BASE_TYPE_BITS),
831 
832 	/* Used to tag PTR_TO_BTF_ID | MEM_ALLOC references which are non-owning.
833 	 * Currently only valid for linked-list and rbtree nodes. If the nodes
834 	 * have a bpf_refcount_field, they must be tagged MEM_RCU as well.
835 	 */
836 	NON_OWN_REF		= BIT(14 + BPF_BASE_TYPE_BITS),
837 
838 	/* DYNPTR points to sk_buff */
839 	DYNPTR_TYPE_SKB		= BIT(15 + BPF_BASE_TYPE_BITS),
840 
841 	/* DYNPTR points to xdp_buff */
842 	DYNPTR_TYPE_XDP		= BIT(16 + BPF_BASE_TYPE_BITS),
843 
844 	/* Memory must be aligned on some architectures, used in combination with
845 	 * MEM_FIXED_SIZE.
846 	 */
847 	MEM_ALIGNED		= BIT(17 + BPF_BASE_TYPE_BITS),
848 
849 	/* MEM is being written to, often combined with MEM_UNINIT. Non-presence
850 	 * of MEM_WRITE means that MEM is only being read. MEM_WRITE without the
851 	 * MEM_UNINIT means that memory needs to be initialized since it is also
852 	 * read.
853 	 */
854 	MEM_WRITE		= BIT(18 + BPF_BASE_TYPE_BITS),
855 
856 	/* DYNPTR points to skb_metadata_end()-skb_metadata_len() */
857 	DYNPTR_TYPE_SKB_META	= BIT(19 + BPF_BASE_TYPE_BITS),
858 
859 	/* DYNPTR points to file */
860 	DYNPTR_TYPE_FILE	= BIT(20 + BPF_BASE_TYPE_BITS),
861 
862 	__BPF_TYPE_FLAG_MAX,
863 	__BPF_TYPE_LAST_FLAG	= __BPF_TYPE_FLAG_MAX - 1,
864 };
865 
866 #define DYNPTR_TYPE_FLAG_MASK	(DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF | DYNPTR_TYPE_SKB \
867 				 | DYNPTR_TYPE_XDP | DYNPTR_TYPE_SKB_META | DYNPTR_TYPE_FILE)
868 
869 /* Max number of base types. */
870 #define BPF_BASE_TYPE_LIMIT	(1UL << BPF_BASE_TYPE_BITS)
871 
872 /* Max number of all types. */
873 #define BPF_TYPE_LIMIT		(__BPF_TYPE_LAST_FLAG | (__BPF_TYPE_LAST_FLAG - 1))
874 
875 /* function argument constraints */
876 enum bpf_arg_type {
877 	ARG_DONTCARE = 0,	/* unused argument in helper function */
878 
879 	/* the following constraints used to prototype
880 	 * bpf_map_lookup/update/delete_elem() functions
881 	 */
882 	ARG_CONST_MAP_PTR,	/* const argument used as pointer to bpf_map */
883 	ARG_PTR_TO_MAP_KEY,	/* pointer to stack used as map key */
884 	ARG_PTR_TO_MAP_VALUE,	/* pointer to stack used as map value */
885 
886 	/* Used to prototype bpf_memcmp() and other functions that access data
887 	 * on eBPF program stack
888 	 */
889 	ARG_PTR_TO_MEM,		/* pointer to valid memory (stack, packet, map value) */
890 	ARG_PTR_TO_ARENA,
891 
892 	ARG_MEM_SIZE,		/* number of bytes accessed from memory */
893 	ARG_MEM_SIZE_OR_ZERO,	/* number of bytes accessed from memory or 0 */
894 
895 	ARG_PTR_TO_CTX,		/* pointer to context */
896 	ARG_ANYTHING,		/* any (initialized) argument is ok */
897 	ARG_SCALAR,		/* scalar argument */
898 	ARG_PTR_TO_SPIN_LOCK,	/* pointer to bpf_spin_lock */
899 	ARG_PTR_TO_SOCK_COMMON,	/* pointer to sock_common */
900 	ARG_PTR_TO_SOCKET,	/* pointer to bpf_sock (fullsock) */
901 	ARG_PTR_TO_BTF_ID,	/* pointer to in-kernel struct */
902 	ARG_PTR_TO_RINGBUF_MEM,	/* pointer to dynamically reserved ringbuf memory */
903 	ARG_CONST_ALLOC_SIZE_OR_ZERO,	/* number of allocated bytes requested */
904 	ARG_PTR_TO_BTF_ID_SOCK_COMMON,	/* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */
905 	ARG_PTR_TO_PERCPU_BTF_ID,	/* pointer to in-kernel percpu type */
906 	ARG_PTR_TO_FUNC,	/* pointer to a bpf program function */
907 	ARG_PTR_TO_STACK,	/* pointer to stack */
908 	ARG_PTR_TO_CONST_STR,	/* pointer to a null terminated read-only string */
909 	ARG_PTR_TO_TIMER,	/* pointer to bpf_timer */
910 	ARG_KPTR_XCHG_DEST,	/* pointer to destination that kptrs are bpf_kptr_xchg'd into */
911 	ARG_PTR_TO_DYNPTR,      /* pointer to bpf_dynptr. See bpf_type_flag for dynptr type */
912 	__BPF_ARG_TYPE_MAX,
913 
914 	/* Extended arg_types. */
915 	ARG_PTR_TO_MAP_VALUE_OR_NULL	= PTR_MAYBE_NULL | ARG_PTR_TO_MAP_VALUE,
916 	ARG_PTR_TO_MEM_OR_NULL		= PTR_MAYBE_NULL | ARG_PTR_TO_MEM,
917 	ARG_PTR_TO_CTX_OR_NULL		= PTR_MAYBE_NULL | ARG_PTR_TO_CTX,
918 	ARG_PTR_TO_SOCKET_OR_NULL	= PTR_MAYBE_NULL | ARG_PTR_TO_SOCKET,
919 	ARG_PTR_TO_STACK_OR_NULL	= PTR_MAYBE_NULL | ARG_PTR_TO_STACK,
920 	ARG_PTR_TO_BTF_ID_OR_NULL	= PTR_MAYBE_NULL | ARG_PTR_TO_BTF_ID,
921 	/* Pointer to memory does not need to be initialized, since helper function
922 	 * fills all bytes or clears them in error case.
923 	 */
924 	ARG_PTR_TO_UNINIT_MEM		= MEM_UNINIT | MEM_WRITE | ARG_PTR_TO_MEM,
925 	/* Pointer to valid memory of size known at compile time. */
926 	ARG_PTR_TO_FIXED_SIZE_MEM	= MEM_FIXED_SIZE | ARG_PTR_TO_MEM,
927 
928 	/* This must be the last entry. Its purpose is to ensure the enum is
929 	 * wide enough to hold the higher bits reserved for bpf_type_flag.
930 	 */
931 	__BPF_ARG_TYPE_LIMIT	= BPF_TYPE_LIMIT,
932 };
933 static_assert(__BPF_ARG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
934 
935 /* type of values returned from helper functions */
936 enum bpf_return_type {
937 	RET_INTEGER,			/* function returns integer */
938 	RET_VOID,			/* function doesn't return anything */
939 	RET_PTR_TO_MAP_VALUE,		/* returns a pointer to map elem value */
940 	RET_PTR_TO_SOCKET,		/* returns a pointer to a socket */
941 	RET_PTR_TO_TCP_SOCK,		/* returns a pointer to a tcp_sock */
942 	RET_PTR_TO_SOCK_COMMON,		/* returns a pointer to a sock_common */
943 	RET_PTR_TO_MEM,			/* returns a pointer to memory */
944 	RET_PTR_TO_MEM_OR_BTF_ID,	/* returns a pointer to a valid memory or a btf_id */
945 	RET_PTR_TO_BTF_ID,		/* returns a pointer to a btf_id */
946 	__BPF_RET_TYPE_MAX,
947 
948 	/* Extended ret_types. */
949 	RET_PTR_TO_MAP_VALUE_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_MAP_VALUE,
950 	RET_PTR_TO_SOCKET_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_SOCKET,
951 	RET_PTR_TO_TCP_SOCK_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_TCP_SOCK,
952 	RET_PTR_TO_SOCK_COMMON_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_SOCK_COMMON,
953 	RET_PTR_TO_RINGBUF_MEM_OR_NULL	= PTR_MAYBE_NULL | MEM_RINGBUF | RET_PTR_TO_MEM,
954 	RET_PTR_TO_DYNPTR_MEM_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_MEM,
955 	RET_PTR_TO_BTF_ID_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_BTF_ID,
956 	RET_PTR_TO_BTF_ID_TRUSTED	= PTR_TRUSTED	 | RET_PTR_TO_BTF_ID,
957 
958 	/* This must be the last entry. Its purpose is to ensure the enum is
959 	 * wide enough to hold the higher bits reserved for bpf_type_flag.
960 	 */
961 	__BPF_RET_TYPE_LIMIT	= BPF_TYPE_LIMIT,
962 };
963 static_assert(__BPF_RET_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
964 
965 /* The longest tracepoint has 12 args.
966  * See include/trace/bpf_probe.h
967  *
968  * Also reuse this macro for maximum number of arguments a BPF function
969  * or a kfunc can have. Args 1-5 are passed in registers, args 6-12 via
970  * stack arg slots. The JIT may map some stack arg slots to registers based
971  * on the native calling convention (e.g., arg 6 to R9 on x86-64).
972  */
973 #define MAX_BPF_FUNC_ARGS 12
974 
975 /* The maximum number of arguments passed through registers
976  * a single function may have.
977  */
978 #define MAX_BPF_FUNC_REG_ARGS 5
979 
980 /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
981  * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
982  * instructions after verifying
983  */
984 struct bpf_func_proto {
985 	u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
986 	bool gpl_only;
987 	bool pkt_access;
988 	bool might_sleep;
989 	/* set to true if helper follows contract for llvm
990 	 * attribute bpf_fastcall:
991 	 * - void functions do not scratch r0
992 	 * - functions taking N arguments scratch only registers r1-rN
993 	 */
994 	bool allow_fastcall;
995 	enum bpf_return_type ret_type;
996 	union {
997 		struct {
998 			enum bpf_arg_type arg1_type;
999 			enum bpf_arg_type arg2_type;
1000 			enum bpf_arg_type arg3_type;
1001 			enum bpf_arg_type arg4_type;
1002 			enum bpf_arg_type arg5_type;
1003 		};
1004 		enum bpf_arg_type arg_type[MAX_BPF_FUNC_ARGS];
1005 	};
1006 	union {
1007 		struct {
1008 			u32 *arg1_btf_id;
1009 			u32 *arg2_btf_id;
1010 			u32 *arg3_btf_id;
1011 			u32 *arg4_btf_id;
1012 			u32 *arg5_btf_id;
1013 		};
1014 		u32 *arg_btf_id[MAX_BPF_FUNC_ARGS];
1015 		struct {
1016 			size_t arg1_size;
1017 			size_t arg2_size;
1018 			size_t arg3_size;
1019 			size_t arg4_size;
1020 			size_t arg5_size;
1021 		};
1022 		size_t arg_size[MAX_BPF_FUNC_ARGS];
1023 	};
1024 	int *ret_btf_id; /* return value btf_id */
1025 	bool (*allowed)(const struct bpf_prog *prog);
1026 };
1027 
1028 /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
1029  * the first argument to eBPF programs.
1030  * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
1031  */
1032 struct bpf_context;
1033 
1034 enum bpf_access_type {
1035 	BPF_READ = 1,
1036 	BPF_WRITE = 2
1037 };
1038 
1039 /* types of values stored in eBPF registers */
1040 /* Pointer types represent:
1041  * pointer
1042  * pointer + imm
1043  * pointer + (u16) var
1044  * pointer + (u16) var + imm
1045  * if (range > 0) then [ptr, ptr + range - off) is safe to access
1046  * if (id > 0) means that some 'var' was added
1047  * if (off > 0) means that 'imm' was added
1048  */
1049 enum bpf_reg_type {
1050 	NOT_INIT = 0,		 /* nothing was written into register */
1051 	SCALAR_VALUE,		 /* reg doesn't contain a valid pointer */
1052 	PTR_TO_CTX,		 /* reg points to bpf_context */
1053 	CONST_PTR_TO_MAP,	 /* reg points to struct bpf_map */
1054 	PTR_TO_MAP_VALUE,	 /* reg points to map element value */
1055 	PTR_TO_MAP_KEY,		 /* reg points to a map element key */
1056 	PTR_TO_STACK,		 /* reg == frame_pointer + offset */
1057 	PTR_TO_PACKET_META,	 /* skb->data - meta_len */
1058 	PTR_TO_PACKET,		 /* reg points to skb->data */
1059 	PTR_TO_PACKET_END,	 /* skb->data + headlen */
1060 	PTR_TO_FLOW_KEYS,	 /* reg points to bpf_flow_keys */
1061 	PTR_TO_SOCKET,		 /* reg points to struct bpf_sock */
1062 	PTR_TO_SOCK_COMMON,	 /* reg points to sock_common */
1063 	PTR_TO_TCP_SOCK,	 /* reg points to struct tcp_sock */
1064 	PTR_TO_TP_BUFFER,	 /* reg points to a writable raw tp's buffer */
1065 	PTR_TO_XDP_SOCK,	 /* reg points to struct xdp_sock */
1066 	/* PTR_TO_BTF_ID points to a kernel struct that does not need
1067 	 * to be null checked by the BPF program. This does not imply the
1068 	 * pointer is _not_ null and in practice this can easily be a null
1069 	 * pointer when reading pointer chains. The assumption is program
1070 	 * context will handle null pointer dereference typically via fault
1071 	 * handling. The verifier must keep this in mind and can make no
1072 	 * assumptions about null or non-null when doing branch analysis.
1073 	 * Further, when passed into helpers the helpers can not, without
1074 	 * additional context, assume the value is non-null.
1075 	 */
1076 	PTR_TO_BTF_ID,
1077 	PTR_TO_MEM,		 /* reg points to valid memory region */
1078 	PTR_TO_ARENA,
1079 	PTR_TO_BUF,		 /* reg points to a read/write buffer */
1080 	PTR_TO_FUNC,		 /* reg points to a bpf program function */
1081 	PTR_TO_INSN,		 /* reg points to a bpf program instruction */
1082 	CONST_PTR_TO_DYNPTR,	 /* reg points to a const struct bpf_dynptr */
1083 	__BPF_REG_TYPE_MAX,
1084 
1085 	/* Extended reg_types. */
1086 	PTR_TO_MAP_VALUE_OR_NULL	= PTR_MAYBE_NULL | PTR_TO_MAP_VALUE,
1087 	PTR_TO_SOCKET_OR_NULL		= PTR_MAYBE_NULL | PTR_TO_SOCKET,
1088 	PTR_TO_SOCK_COMMON_OR_NULL	= PTR_MAYBE_NULL | PTR_TO_SOCK_COMMON,
1089 	PTR_TO_TCP_SOCK_OR_NULL		= PTR_MAYBE_NULL | PTR_TO_TCP_SOCK,
1090 	/* PTR_TO_BTF_ID_OR_NULL points to a kernel struct that has not
1091 	 * been checked for null. Used primarily to inform the verifier
1092 	 * an explicit null check is required for this struct.
1093 	 */
1094 	PTR_TO_BTF_ID_OR_NULL		= PTR_MAYBE_NULL | PTR_TO_BTF_ID,
1095 
1096 	/* This must be the last entry. Its purpose is to ensure the enum is
1097 	 * wide enough to hold the higher bits reserved for bpf_type_flag.
1098 	 */
1099 	__BPF_REG_TYPE_LIMIT	= BPF_TYPE_LIMIT,
1100 };
1101 static_assert(__BPF_REG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
1102 
1103 /* The information passed from prog-specific *_is_valid_access
1104  * back to the verifier.
1105  */
1106 struct bpf_insn_access_aux {
1107 	enum bpf_reg_type reg_type;
1108 	bool is_ldsx;
1109 	union {
1110 		int ctx_field_size;
1111 		struct {
1112 			struct btf *btf;
1113 			u32 btf_id;
1114 			u32 ref_id;
1115 		};
1116 	};
1117 	struct bpf_verifier_log *log; /* for verbose logs */
1118 	bool is_retval; /* is accessing function return value ? */
1119 };
1120 
1121 static inline void
1122 bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
1123 {
1124 	aux->ctx_field_size = size;
1125 }
1126 
1127 static bool bpf_is_ldimm64(const struct bpf_insn *insn)
1128 {
1129 	return insn->code == (BPF_LD | BPF_IMM | BPF_DW);
1130 }
1131 
1132 static inline bool bpf_pseudo_func(const struct bpf_insn *insn)
1133 {
1134 	return bpf_is_ldimm64(insn) && insn->src_reg == BPF_PSEUDO_FUNC;
1135 }
1136 
1137 struct bpf_prog_ops {
1138 	int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
1139 			union bpf_attr __user *uattr);
1140 };
1141 
1142 struct bpf_reg_state;
1143 struct bpf_verifier_ops {
1144 	/* return eBPF function prototype for verification */
1145 	const struct bpf_func_proto *
1146 	(*get_func_proto)(enum bpf_func_id func_id,
1147 			  const struct bpf_prog *prog);
1148 
1149 	/* return true if 'size' wide access at offset 'off' within bpf_context
1150 	 * with 'type' (read or write) is allowed
1151 	 */
1152 	bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
1153 				const struct bpf_prog *prog,
1154 				struct bpf_insn_access_aux *info);
1155 	int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
1156 			    const struct bpf_prog *prog);
1157 	int (*gen_epilogue)(struct bpf_insn *insn, const struct bpf_prog *prog,
1158 			    s16 ctx_stack_off);
1159 	int (*gen_ld_abs)(const struct bpf_insn *orig,
1160 			  struct bpf_insn *insn_buf);
1161 	u32 (*convert_ctx_access)(enum bpf_access_type type,
1162 				  const struct bpf_insn *src,
1163 				  struct bpf_insn *dst,
1164 				  struct bpf_prog *prog, u32 *target_size);
1165 	int (*btf_struct_access)(struct bpf_verifier_log *log,
1166 				 const struct bpf_reg_state *reg,
1167 				 int off, int size);
1168 };
1169 
1170 struct bpf_prog_offload_ops {
1171 	/* verifier basic callbacks */
1172 	int (*insn_hook)(struct bpf_verifier_env *env,
1173 			 int insn_idx, int prev_insn_idx);
1174 	int (*finalize)(struct bpf_verifier_env *env);
1175 	/* verifier optimization callbacks (called after .finalize) */
1176 	int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
1177 			    struct bpf_insn *insn);
1178 	int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
1179 	/* program management callbacks */
1180 	int (*prepare)(struct bpf_prog *prog);
1181 	int (*translate)(struct bpf_prog *prog);
1182 	void (*destroy)(struct bpf_prog *prog);
1183 };
1184 
1185 struct bpf_prog_offload {
1186 	struct bpf_prog		*prog;
1187 	struct net_device	*netdev;
1188 	struct bpf_offload_dev	*offdev;
1189 	void			*dev_priv;
1190 	struct list_head	offloads;
1191 	bool			dev_state;
1192 	bool			opt_failed;
1193 	void			*jited_image;
1194 	u32			jited_len;
1195 };
1196 
1197 /* The argument is signed. */
1198 #define BTF_FMODEL_SIGNED_ARG		BIT(1)
1199 
1200 /* The argument is an arena pointer. */
1201 #define BTF_FMODEL_ARENA_ARG		BIT(2)
1202 
1203 /* The argument is nullable. */
1204 #define BTF_FMODEL_NULLABLE_ARG		BIT(3)
1205 
1206 struct btf_func_model {
1207 	u8 ret_size;
1208 	u8 ret_flags;
1209 	u8 nr_args;
1210 	u8 arg_size[MAX_BPF_FUNC_ARGS];
1211 	u8 arg_flags[MAX_BPF_FUNC_ARGS];
1212 };
1213 
1214 /* Restore arguments before returning from trampoline to let original function
1215  * continue executing. This flag is used for fentry progs when there are no
1216  * fexit progs.
1217  */
1218 #define BPF_TRAMP_F_RESTORE_REGS	BIT(0)
1219 /* Call original function after fentry progs, but before fexit progs.
1220  * Makes sense for fentry/fexit, normal calls and indirect calls.
1221  */
1222 #define BPF_TRAMP_F_CALL_ORIG		BIT(1)
1223 /* Skip current frame and return to parent.  Makes sense for fentry/fexit
1224  * programs only. Should not be used with normal calls and indirect calls.
1225  */
1226 #define BPF_TRAMP_F_SKIP_FRAME		BIT(2)
1227 /* Store IP address of the caller on the trampoline stack,
1228  * so it's available for trampoline's programs.
1229  */
1230 #define BPF_TRAMP_F_IP_ARG		BIT(3)
1231 /* Return the return value of fentry prog. Only used by bpf_struct_ops. */
1232 #define BPF_TRAMP_F_RET_FENTRY_RET	BIT(4)
1233 
1234 /* Get original function from stack instead of from provided direct address.
1235  * Makes sense for trampolines with fexit or fmod_ret programs.
1236  */
1237 #define BPF_TRAMP_F_ORIG_STACK		BIT(5)
1238 
1239 /* This trampoline is on a function with another ftrace_ops with IPMODIFY,
1240  * e.g., a live patch. This flag is set and cleared by ftrace call backs,
1241  */
1242 #define BPF_TRAMP_F_SHARE_IPMODIFY	BIT(6)
1243 
1244 /* Indicate that current trampoline is in a tail call context. Then, it has to
1245  * cache and restore tail_call_cnt to avoid infinite tail call loop.
1246  */
1247 #define BPF_TRAMP_F_TAIL_CALL_CTX	BIT(7)
1248 
1249 /*
1250  * Indicate the trampoline should be suitable to receive indirect calls;
1251  * without this indirectly calling the generated code can result in #UD/#CP,
1252  * depending on the CFI options.
1253  *
1254  * Used by bpf_struct_ops.
1255  *
1256  * Incompatible with FENTRY usage, overloads @func_addr argument.
1257  */
1258 #define BPF_TRAMP_F_INDIRECT		BIT(8)
1259 
1260 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
1261  * bytes on x86. The trampoline image has to fit in PAGE_SIZE.
1262  */
1263 enum {
1264 #if defined(__s390x__) || defined(__powerpc64__)
1265 	BPF_MAX_TRAMP_LINKS = 27,
1266 #elif defined(__x86_64__)
1267 	BPF_MAX_TRAMP_LINKS = 36,
1268 #elif defined(__aarch64__)
1269 	BPF_MAX_TRAMP_LINKS = 37,
1270 #else
1271 	BPF_MAX_TRAMP_LINKS = 38,
1272 #endif
1273 };
1274 
1275 #define BPF_TRAMP_COOKIE_INDEX_SHIFT	8
1276 #define BPF_TRAMP_IS_RETURN_SHIFT	63
1277 
1278 struct bpf_tramp_nodes {
1279 	struct bpf_tramp_node *nodes[BPF_MAX_TRAMP_LINKS];
1280 	int nr_nodes;
1281 };
1282 
1283 /*
1284  * The arena base against which a struct_ops trampoline converts the
1285  * arguments marked with BTF_FMODEL_ARENA_ARG while saving them into the BPF
1286  * ctx, ctx[arg] = (u32)(kaddr - kern_vm_start). Zero when the trampoline
1287  * converts nothing.
1288  */
1289 u64 bpf_tramp_arena_base(const struct btf_func_model *m,
1290 			 struct bpf_tramp_nodes *tnodes, u32 flags);
1291 
1292 struct bpf_tramp_run_ctx;
1293 
1294 /* Different use cases for BPF trampoline:
1295  * 1. replace nop at the function entry (kprobe equivalent)
1296  *    flags = BPF_TRAMP_F_RESTORE_REGS
1297  *    fentry = a set of programs to run before returning from trampoline
1298  *
1299  * 2. replace nop at the function entry (kprobe + kretprobe equivalent)
1300  *    flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME
1301  *    orig_call = fentry_ip + MCOUNT_INSN_SIZE
1302  *    fentry = a set of program to run before calling original function
1303  *    fexit = a set of program to run after original function
1304  *
1305  * 3. replace direct call instruction anywhere in the function body
1306  *    or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid)
1307  *    With flags = 0
1308  *      fentry = a set of programs to run before returning from trampoline
1309  *    With flags = BPF_TRAMP_F_CALL_ORIG
1310  *      orig_call = original callback addr or direct function addr
1311  *      fentry = a set of program to run before calling original function
1312  *      fexit = a set of program to run after original function
1313  */
1314 struct bpf_tramp_image;
1315 int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,
1316 				const struct btf_func_model *m, u32 flags,
1317 				struct bpf_tramp_nodes *tnodes,
1318 				void *func_addr);
1319 void *arch_alloc_bpf_trampoline(unsigned int size);
1320 void arch_free_bpf_trampoline(void *image, unsigned int size);
1321 int __must_check arch_protect_bpf_trampoline(void *image, unsigned int size);
1322 int arch_bpf_trampoline_skip(void *nop, void *target);
1323 int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
1324 			     struct bpf_tramp_nodes *tnodes, void *func_addr);
1325 
1326 u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog,
1327 					     struct bpf_tramp_run_ctx *run_ctx);
1328 void notrace __bpf_prog_exit_sleepable_recur(struct bpf_prog *prog, u64 start,
1329 					     struct bpf_tramp_run_ctx *run_ctx);
1330 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr);
1331 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr);
1332 typedef u64 (*bpf_trampoline_enter_t)(struct bpf_prog *prog,
1333 				      struct bpf_tramp_run_ctx *run_ctx);
1334 typedef void (*bpf_trampoline_exit_t)(struct bpf_prog *prog, u64 start,
1335 				      struct bpf_tramp_run_ctx *run_ctx);
1336 bpf_trampoline_enter_t bpf_trampoline_enter(const struct bpf_prog *prog);
1337 bpf_trampoline_exit_t bpf_trampoline_exit(const struct bpf_prog *prog);
1338 
1339 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_JMP
1340 static inline bool bpf_trampoline_use_jmp(u64 flags)
1341 {
1342 	return flags & BPF_TRAMP_F_CALL_ORIG && !(flags & BPF_TRAMP_F_SKIP_FRAME);
1343 }
1344 #else
1345 static inline bool bpf_trampoline_use_jmp(u64 flags)
1346 {
1347 	return false;
1348 }
1349 #endif
1350 
1351 struct bpf_ksym {
1352 	unsigned long		 start;
1353 	unsigned long		 end;
1354 	char			 name[KSYM_NAME_LEN];
1355 	struct list_head	 lnode;
1356 	struct latch_tree_node	 tnode;
1357 	bool			 prog;
1358 	u32			 fp_start;
1359 	u32			 fp_end;
1360 };
1361 
1362 enum bpf_tramp_prog_type {
1363 	BPF_TRAMP_FENTRY,
1364 	BPF_TRAMP_FEXIT,
1365 	BPF_TRAMP_MODIFY_RETURN,
1366 	BPF_TRAMP_MAX,
1367 	BPF_TRAMP_REPLACE, /* more than MAX */
1368 	BPF_TRAMP_FSESSION,
1369 };
1370 
1371 /*
1372  * Each prog call in a trampoline image is preceded by a nop. When the prog is
1373  * detached, the nop is patched to a jump to target, right after the call, so
1374  * that tasks still running in the image skip the prog.
1375  */
1376 struct bpf_tramp_skip {
1377 	struct bpf_prog *prog;
1378 	void *nop;
1379 	void *target;
1380 };
1381 
1382 struct bpf_tramp_image {
1383 	void *image;
1384 	int size;
1385 	struct bpf_ksym ksym;
1386 	struct percpu_ref pcref;
1387 	bool call_orig;
1388 	/* entry in tr->images, the image holds a reference on tr */
1389 	struct bpf_trampoline *tr;
1390 	struct list_head list;
1391 	int nr_skips;
1392 	struct bpf_tramp_skip *skips;
1393 	union {
1394 		struct rcu_head rcu;
1395 		struct work_struct work;
1396 	};
1397 };
1398 
1399 static inline void bpf_tramp_image_add_skip(struct bpf_tramp_image *im, struct bpf_prog *prog,
1400 					    void *nop, void *target)
1401 {
1402 	struct bpf_tramp_skip *skip;
1403 
1404 	/* struct_ops trampolines and arch_bpf_trampoline_size() have no image */
1405 	if (!im || !im->skips)
1406 		return;
1407 	skip = &im->skips[im->nr_skips++];
1408 	skip->prog = prog;
1409 	skip->nop = nop;
1410 	skip->target = target;
1411 }
1412 
1413 struct bpf_trampoline {
1414 	/* hlist for trampoline_key_table */
1415 	struct hlist_node hlist_key;
1416 	/* hlist for trampoline_ip_table */
1417 	struct hlist_node hlist_ip;
1418 	struct ftrace_ops *fops;
1419 	refcount_t refcnt;
1420 	u32 flags;
1421 	u64 key;
1422 	unsigned long ip;
1423 	struct {
1424 		struct btf_func_model model;
1425 		void *addr;
1426 		bool ftrace_managed;
1427 	} func;
1428 	/* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF
1429 	 * program by replacing one of its functions. func.addr is the address
1430 	 * of the function it replaced.
1431 	 */
1432 	struct bpf_prog *extension_prog;
1433 	/* list of BPF programs using this trampoline */
1434 	struct hlist_head progs_hlist[BPF_TRAMP_MAX];
1435 	/* Number of attached programs. A counter per kind. */
1436 	int progs_cnt[BPF_TRAMP_MAX];
1437 	/* Executable image of trampoline */
1438 	struct bpf_tramp_image *cur_image;
1439 	/* Images not freed yet, cur_image and older ones still in use */
1440 	struct list_head images;
1441 	/* Used as temporary old image storage for multi_attach */
1442 	struct {
1443 		struct bpf_tramp_image *old_image;
1444 		u32 old_flags;
1445 	} multi_attach;
1446 };
1447 
1448 struct bpf_attach_target_info {
1449 	struct btf_func_model fmodel;
1450 	long tgt_addr;
1451 	struct module *tgt_mod;
1452 	const char *tgt_name;
1453 	const struct btf_type *tgt_type;
1454 };
1455 
1456 #define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
1457 
1458 struct bpf_dispatcher_prog {
1459 	struct bpf_prog *prog;
1460 	refcount_t users;
1461 };
1462 
1463 struct bpf_dispatcher {
1464 	/* dispatcher mutex */
1465 	struct mutex mutex;
1466 	void *func;
1467 	struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX];
1468 	int num_progs;
1469 	void *image;
1470 	void *rw_image;
1471 	u32 image_off;
1472 	struct bpf_ksym ksym;
1473 #ifdef CONFIG_HAVE_STATIC_CALL
1474 	struct static_call_key *sc_key;
1475 	void *sc_tramp;
1476 #endif
1477 };
1478 
1479 #ifndef __bpfcall
1480 #define __bpfcall __nocfi
1481 #endif
1482 
1483 static __always_inline __bpfcall unsigned int bpf_dispatcher_nop_func(
1484 	const void *ctx,
1485 	const struct bpf_insn *insnsi,
1486 	bpf_func_t bpf_func)
1487 {
1488 	return bpf_func(ctx, insnsi);
1489 }
1490 
1491 /* the implementation of the opaque uapi struct bpf_dynptr */
1492 struct bpf_dynptr_kern {
1493 	void *data;
1494 	/* Size represents the number of usable bytes of dynptr data.
1495 	 * If for example the offset is at 4 for a local dynptr whose data is
1496 	 * of type u64, the number of usable bytes is 4.
1497 	 *
1498 	 * The upper 8 bits are reserved. It is as follows:
1499 	 * Bits 0 - 23 = size
1500 	 * Bits 24 - 30 = dynptr type
1501 	 * Bit 31 = whether dynptr is read-only
1502 	 */
1503 	u32 size;
1504 	u32 offset;
1505 } __aligned(8);
1506 
1507 enum bpf_dynptr_type {
1508 	BPF_DYNPTR_TYPE_INVALID,
1509 	/* Points to memory that is local to the bpf program */
1510 	BPF_DYNPTR_TYPE_LOCAL,
1511 	/* Underlying data is a ringbuf record */
1512 	BPF_DYNPTR_TYPE_RINGBUF,
1513 	/* Underlying data is a sk_buff */
1514 	BPF_DYNPTR_TYPE_SKB,
1515 	/* Underlying data is a xdp_buff */
1516 	BPF_DYNPTR_TYPE_XDP,
1517 	/* Points to skb_metadata_end()-skb_metadata_len() */
1518 	BPF_DYNPTR_TYPE_SKB_META,
1519 	/* Underlying data is a file */
1520 	BPF_DYNPTR_TYPE_FILE,
1521 };
1522 
1523 int bpf_dynptr_check_size(u64 size);
1524 u64 __bpf_dynptr_size(const struct bpf_dynptr_kern *ptr);
1525 const void *__bpf_dynptr_data(const struct bpf_dynptr_kern *ptr, u64 len);
1526 void *__bpf_dynptr_data_rw(const struct bpf_dynptr_kern *ptr, u64 len);
1527 bool __bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr);
1528 int __bpf_dynptr_write(const struct bpf_dynptr_kern *dst, u64 offset,
1529 		       void *src, u64 len, u64 flags);
1530 void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr *p, u64 offset,
1531 			    void *buffer__nullable, u64 buffer__szk);
1532 
1533 static inline int bpf_dynptr_check_off_len(const struct bpf_dynptr_kern *ptr, u64 offset, u64 len)
1534 {
1535 	u64 size = __bpf_dynptr_size(ptr);
1536 
1537 	if (len > size || offset > size - len)
1538 		return -E2BIG;
1539 
1540 	return 0;
1541 }
1542 
1543 struct bpf_tracing_multi_link;
1544 
1545 #ifdef CONFIG_BPF_JIT
1546 int bpf_trampoline_link_prog(struct bpf_tramp_node *node,
1547 			     struct bpf_trampoline *tr,
1548 			     struct bpf_prog *tgt_prog);
1549 int bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,
1550 			       struct bpf_trampoline *tr,
1551 			       struct bpf_prog *tgt_prog);
1552 struct bpf_trampoline *bpf_trampoline_get(u64 key,
1553 					  struct bpf_attach_target_info *tgt_info);
1554 void bpf_trampoline_put(struct bpf_trampoline *tr);
1555 int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_funcs);
1556 
1557 int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids,
1558 				struct bpf_tracing_multi_link *link);
1559 void bpf_trampoline_multi_detach(struct bpf_prog *prog,
1560 				 struct bpf_tracing_multi_link *link);
1561 void bpf_trampoline_set_flags(struct bpf_trampoline *tr, u32 flags);
1562 
1563 /*
1564  * When the architecture supports STATIC_CALL replace the bpf_dispatcher_fn
1565  * indirection with a direct call to the bpf program. If the architecture does
1566  * not have STATIC_CALL, avoid a double-indirection.
1567  */
1568 #ifdef CONFIG_HAVE_STATIC_CALL
1569 
1570 #define __BPF_DISPATCHER_SC_INIT(_name)				\
1571 	.sc_key = &STATIC_CALL_KEY(_name),			\
1572 	.sc_tramp = STATIC_CALL_TRAMP_ADDR(_name),
1573 
1574 #define __BPF_DISPATCHER_SC(name)				\
1575 	DEFINE_STATIC_CALL(bpf_dispatcher_##name##_call, bpf_dispatcher_nop_func)
1576 
1577 #define __BPF_DISPATCHER_CALL(name)				\
1578 	static_call(bpf_dispatcher_##name##_call)(ctx, insnsi, bpf_func)
1579 
1580 #define __BPF_DISPATCHER_UPDATE(_d, _new)			\
1581 	__static_call_update((_d)->sc_key, (_d)->sc_tramp, (_new))
1582 
1583 #else
1584 #define __BPF_DISPATCHER_SC_INIT(name)
1585 #define __BPF_DISPATCHER_SC(name)
1586 #define __BPF_DISPATCHER_CALL(name)		bpf_func(ctx, insnsi)
1587 #define __BPF_DISPATCHER_UPDATE(_d, _new)
1588 #endif
1589 
1590 #define BPF_DISPATCHER_INIT(_name) {				\
1591 	.mutex = __MUTEX_INITIALIZER(_name.mutex),		\
1592 	.func = &_name##_func,					\
1593 	.progs = {},						\
1594 	.num_progs = 0,						\
1595 	.image = NULL,						\
1596 	.image_off = 0,						\
1597 	.ksym = {						\
1598 		.name  = #_name,				\
1599 		.lnode = LIST_HEAD_INIT(_name.ksym.lnode),	\
1600 	},							\
1601 	__BPF_DISPATCHER_SC_INIT(_name##_call)			\
1602 }
1603 
1604 #define DEFINE_BPF_DISPATCHER(name)					\
1605 	__BPF_DISPATCHER_SC(name);					\
1606 	noinline __bpfcall unsigned int bpf_dispatcher_##name##_func(	\
1607 		const void *ctx,					\
1608 		const struct bpf_insn *insnsi,				\
1609 		bpf_func_t bpf_func)					\
1610 	{								\
1611 		return __BPF_DISPATCHER_CALL(name);			\
1612 	}								\
1613 	EXPORT_SYMBOL(bpf_dispatcher_##name##_func);			\
1614 	struct bpf_dispatcher bpf_dispatcher_##name =			\
1615 		BPF_DISPATCHER_INIT(bpf_dispatcher_##name);
1616 
1617 #define DECLARE_BPF_DISPATCHER(name)					\
1618 	unsigned int bpf_dispatcher_##name##_func(			\
1619 		const void *ctx,					\
1620 		const struct bpf_insn *insnsi,				\
1621 		bpf_func_t bpf_func);					\
1622 	extern struct bpf_dispatcher bpf_dispatcher_##name;
1623 
1624 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func
1625 #define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
1626 void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
1627 				struct bpf_prog *to);
1628 /* Called only from JIT-enabled code, so there's no need for stubs. */
1629 void bpf_image_ksym_init(void *data, unsigned int size, struct bpf_ksym *ksym);
1630 void bpf_image_ksym_add(struct bpf_ksym *ksym);
1631 void bpf_image_ksym_del(struct bpf_ksym *ksym);
1632 void bpf_ksym_add(struct bpf_ksym *ksym);
1633 void bpf_ksym_del(struct bpf_ksym *ksym);
1634 bool bpf_has_frame_pointer(unsigned long ip);
1635 int bpf_jit_charge_modmem(u32 size);
1636 void bpf_jit_uncharge_modmem(u32 size);
1637 bool bpf_prog_has_trampoline(const struct bpf_prog *prog);
1638 bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const struct bpf_prog *prog,
1639 				 int insn_idx);
1640 u16 bpf_out_stack_arg_cnt(const struct bpf_verifier_env *env, const struct bpf_prog *prog);
1641 #else
1642 static inline int bpf_trampoline_link_prog(struct bpf_tramp_node *node,
1643 					   struct bpf_trampoline *tr,
1644 					   struct bpf_prog *tgt_prog)
1645 {
1646 	return -ENOTSUPP;
1647 }
1648 static inline int bpf_trampoline_unlink_prog(struct bpf_tramp_node *node,
1649 					     struct bpf_trampoline *tr,
1650 					     struct bpf_prog *tgt_prog)
1651 {
1652 	return -ENOTSUPP;
1653 }
1654 static inline struct bpf_trampoline *bpf_trampoline_get(u64 key,
1655 							struct bpf_attach_target_info *tgt_info)
1656 {
1657 	return NULL;
1658 }
1659 static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
1660 #define DEFINE_BPF_DISPATCHER(name)
1661 #define DECLARE_BPF_DISPATCHER(name)
1662 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nop_func
1663 #define BPF_DISPATCHER_PTR(name) NULL
1664 static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
1665 					      struct bpf_prog *from,
1666 					      struct bpf_prog *to) {}
1667 static inline bool is_bpf_image_address(unsigned long address)
1668 {
1669 	return false;
1670 }
1671 static inline bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
1672 {
1673 	return false;
1674 }
1675 static inline int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids,
1676 					      struct bpf_tracing_multi_link *link)
1677 {
1678 	return -ENOTSUPP;
1679 }
1680 static inline void bpf_trampoline_multi_detach(struct bpf_prog *prog,
1681 					       struct bpf_tracing_multi_link *link)
1682 {
1683 }
1684 static inline void bpf_trampoline_set_flags(struct bpf_trampoline *tr, u32 flags) {}
1685 #endif
1686 
1687 struct bpf_func_info_aux {
1688 	u16 linkage;
1689 	bool unreliable;
1690 	/* Indexed by in_sleepable. */
1691 	bool called[2];
1692 	bool verified[2];
1693 };
1694 
1695 enum bpf_jit_poke_reason {
1696 	BPF_POKE_REASON_TAIL_CALL,
1697 };
1698 
1699 /* Descriptor of pokes pointing /into/ the JITed image. */
1700 struct bpf_jit_poke_descriptor {
1701 	void *tailcall_target;
1702 	void *tailcall_bypass;
1703 	void *bypass_addr;
1704 	void *aux;
1705 	union {
1706 		struct {
1707 			struct bpf_map *map;
1708 			u32 key;
1709 		} tail_call;
1710 	};
1711 	bool tailcall_target_stable;
1712 	u8 adj_off;
1713 	u16 reason;
1714 	u32 insn_idx;
1715 };
1716 
1717 /* reg_type info for ctx arguments */
1718 struct bpf_ctx_arg_aux {
1719 	u32 offset;
1720 	enum bpf_reg_type reg_type;
1721 	struct btf *btf;
1722 	u32 btf_id;
1723 	u32 ref_id;
1724 	bool refcounted;
1725 };
1726 
1727 struct btf_mod_pair {
1728 	struct btf *btf;
1729 	struct module *module;
1730 };
1731 
1732 struct bpf_kfunc_desc_tab;
1733 
1734 enum bpf_stream_id {
1735 	BPF_STDOUT = 1,
1736 	BPF_STDERR = 2,
1737 };
1738 
1739 struct bpf_stream_elem {
1740 	struct llist_node node;
1741 	int total_len;
1742 	int consumed_len;
1743 	char str[];
1744 };
1745 
1746 enum {
1747 	/* 100k bytes */
1748 	BPF_STREAM_MAX_CAPACITY = 100000ULL,
1749 };
1750 
1751 struct bpf_stream {
1752 	atomic_t capacity;
1753 	struct llist_head log;	/* list of in-flight stream elements in LIFO order */
1754 
1755 	struct mutex lock;  /* lock protecting backlog_{head,tail} */
1756 	struct llist_node *backlog_head; /* list of in-flight stream elements in FIFO order */
1757 	struct llist_node *backlog_tail; /* tail of the list above */
1758 };
1759 
1760 struct bpf_stream_stage {
1761 	struct llist_head log;
1762 	int len;
1763 };
1764 
1765 enum bpf_sig_verdict {
1766 	BPF_SIG_UNSIGNED = 0,
1767 	BPF_SIG_VERIFIED,
1768 };
1769 
1770 enum bpf_sig_keyring {
1771 	BPF_SIG_KEYRING_NONE = 0,
1772 	BPF_SIG_KEYRING_BUILTIN,
1773 	BPF_SIG_KEYRING_SECONDARY,
1774 	BPF_SIG_KEYRING_PLATFORM,
1775 	BPF_SIG_KEYRING_USER,
1776 };
1777 
1778 struct bpf_prog_aux {
1779 	atomic64_t refcnt;
1780 	u32 used_map_cnt;
1781 	u32 used_btf_cnt;
1782 	u32 max_ctx_offset;
1783 	u32 max_pkt_offset;
1784 	u32 max_tp_access;
1785 	u32 stack_depth;
1786 	u32 id;
1787 	u32 func_cnt; /* used by non-func prog as the number of func progs */
1788 	u32 real_func_cnt; /* includes hidden progs, only used for JIT and freeing progs */
1789 	u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
1790 	u32 attach_btf_id; /* in-kernel BTF type id to attach to */
1791 	u32 attach_st_ops_member_off;
1792 	u32 ctx_arg_info_size;
1793 	u32 max_rdonly_access;
1794 	u32 max_rdwr_access;
1795 	u32 subprog_start;
1796 	struct btf *attach_btf;
1797 	struct bpf_ctx_arg_aux *ctx_arg_info;
1798 	void __percpu *priv_stack_ptr;
1799 	struct mutex dst_mutex; /* protects dst_* pointers below, *after* prog becomes visible */
1800 	struct bpf_prog *dst_prog;
1801 	struct bpf_trampoline *dst_trampoline;
1802 	enum bpf_prog_type saved_dst_prog_type;
1803 	enum bpf_attach_type saved_dst_attach_type;
1804 	bool verifier_zext; /* Zero extensions has been inserted by verifier. */
1805 	bool dev_bound; /* Program is bound to the netdev. */
1806 	bool offload_requested; /* Program is bound and offloaded to the netdev. */
1807 	bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
1808 	bool attach_tracing_prog; /* true if tracing another tracing program */
1809 	bool tramp_linked; /* true if it was ever linked to a trampoline */
1810 	bool func_proto_unreliable;
1811 	bool tail_call_reachable;
1812 	bool xdp_has_frags;
1813 	bool exception_cb;
1814 	bool exception_boundary;
1815 	bool is_extended; /* true if extended by freplace program */
1816 	bool jits_use_priv_stack;
1817 	bool priv_stack_requested;
1818 	bool changes_pkt_data;
1819 	bool might_sleep;
1820 	bool kprobe_write_ctx;
1821 	struct {
1822 		s32 keyring_serial;
1823 		u8 keyring_type;
1824 		u8 verdict;
1825 	} sig;
1826 	u64 prog_array_member_cnt; /* counts how many times as member of prog_array */
1827 	struct mutex ext_mutex; /* mutex for is_extended and prog_array_member_cnt */
1828 	struct bpf_arena *arena;
1829 	void (*recursion_detected)(struct bpf_prog *prog); /* callback if recursion is detected */
1830 	/* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
1831 	const struct btf_type *attach_func_proto;
1832 	/* function name for valid attach_btf_id */
1833 	const char *attach_func_name;
1834 	struct bpf_prog **func;
1835 	struct bpf_prog_aux *main_prog_aux;
1836 	void *jit_data; /* JIT specific data. arch dependent */
1837 	struct bpf_jit_poke_descriptor *poke_tab;
1838 	struct bpf_kfunc_desc_tab *kfunc_tab;
1839 	struct bpf_kfunc_btf_tab *kfunc_btf_tab;
1840 	u32 size_poke_tab;
1841 #ifdef CONFIG_FINEIBT
1842 	struct bpf_ksym ksym_prefix;
1843 #endif
1844 	struct bpf_ksym ksym;
1845 	const struct bpf_prog_ops *ops;
1846 	const struct bpf_struct_ops *st_ops;
1847 	struct bpf_map **used_maps;
1848 	struct mutex used_maps_mutex; /* mutex for used_maps and used_map_cnt */
1849 	struct btf_mod_pair *used_btfs;
1850 	struct bpf_prog *prog;
1851 	struct user_struct *user;
1852 	u64 load_time; /* ns since boottime */
1853 	u32 verified_insns;
1854 	int cgroup_atype; /* enum cgroup_bpf_attach_type */
1855 	struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1856 	char name[BPF_OBJ_NAME_LEN];
1857 	u64 (*bpf_exception_cb)(u64 cookie, u64 sp, u64 bp, u64, u64);
1858 	u16 stack_arg_sp_adjust;
1859 #ifdef CONFIG_SECURITY
1860 	void *security;
1861 #endif
1862 	struct bpf_token *token;
1863 	struct bpf_prog_offload *offload;
1864 	struct btf *btf;
1865 	struct bpf_func_info *func_info;
1866 	struct bpf_func_info_aux *func_info_aux;
1867 	/* bpf_line_info loaded from userspace.  linfo->insn_off
1868 	 * has the xlated insn offset.
1869 	 * Both the main and sub prog share the same linfo.
1870 	 * The subprog can access its first linfo by
1871 	 * using the linfo_idx.
1872 	 */
1873 	struct bpf_line_info *linfo;
1874 	/* jited_linfo is the jited addr of the linfo.  It has a
1875 	 * one to one mapping to linfo:
1876 	 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
1877 	 * Both the main and sub prog share the same jited_linfo.
1878 	 * The subprog can access its first jited_linfo by
1879 	 * using the linfo_idx.
1880 	 */
1881 	void **jited_linfo;
1882 	u32 func_info_cnt;
1883 	u32 nr_linfo;
1884 	/* subprog can use linfo_idx to access its first linfo and
1885 	 * jited_linfo.
1886 	 * main prog always has linfo_idx == 0
1887 	 */
1888 	u32 linfo_idx;
1889 	struct module *mod;
1890 	u32 num_exentries;
1891 	struct exception_table_entry *extable;
1892 	union {
1893 		struct work_struct work;
1894 		struct rcu_head	rcu;
1895 	};
1896 	struct bpf_stream stream[2];
1897 	struct mutex st_ops_assoc_mutex;
1898 	struct bpf_map __rcu *st_ops_assoc;
1899 };
1900 
1901 #define BPF_NR_CONTEXTS        4       /* normal, softirq, hardirq, NMI */
1902 
1903 struct bpf_prog {
1904 	u16			pages;		/* Number of allocated pages */
1905 	u32			jited:1,	/* Is our filter JIT'ed? */
1906 				jit_requested:1,/* archs need to JIT the prog */
1907 				jit_required:1,	/* program strictly requires JIT compiler */
1908 				gpl_compatible:1, /* Is filter GPL compatible? */
1909 				cb_access:1,	/* Is control block accessed? */
1910 				dst_needed:1,	/* Do we need dst entry? */
1911 				blinding_requested:1, /* needs constant blinding */
1912 				blinded:1,	/* Was blinded */
1913 				is_func:1,	/* program is a bpf function */
1914 				kprobe_override:1, /* Do we override a kprobe? */
1915 				has_callchain_buf:1, /* callchain buffer allocated? */
1916 				enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */
1917 				call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */
1918 				call_get_func_ip:1, /* Do we call get_func_ip() */
1919 				call_session_cookie:1, /* Do we call bpf_session_cookie() */
1920 				tstamp_type_access:1, /* Accessed __sk_buff->tstamp_type */
1921 				sleepable:1;	/* BPF program is sleepable */
1922 	enum bpf_prog_type	type;		/* Type of BPF program */
1923 	enum bpf_attach_type	expected_attach_type; /* For some prog types */
1924 	u32			len;		/* Number of filter blocks */
1925 	u32			jited_len;	/* Size of jited insns in bytes */
1926 	union {
1927 		u8 digest[SHA256_DIGEST_SIZE];
1928 		u8 tag[BPF_TAG_SIZE];
1929 	};
1930 	struct bpf_prog_stats __percpu *stats;
1931 	u8 __percpu		*active;	/* u8[BPF_NR_CONTEXTS] for recursion protection */
1932 	unsigned int		(*bpf_func)(const void *ctx,
1933 					    const struct bpf_insn *insn);
1934 	struct bpf_prog_aux	*aux;		/* Auxiliary fields */
1935 	struct sock_fprog_kern	*orig_prog;	/* Original BPF program */
1936 	/* Instructions for interpreter */
1937 	union {
1938 		DECLARE_FLEX_ARRAY(struct sock_filter, insns);
1939 		DECLARE_FLEX_ARRAY(struct bpf_insn, insnsi);
1940 	};
1941 };
1942 
1943 struct bpf_array_aux {
1944 	/* Programs with direct jumps into programs part of this array. */
1945 	struct list_head poke_progs;
1946 	struct bpf_map *map;
1947 	struct mutex poke_mutex;
1948 	struct work_struct work;
1949 };
1950 
1951 struct bpf_link {
1952 	atomic64_t refcnt;
1953 	u32 id;
1954 	enum bpf_link_type type;
1955 	const struct bpf_link_ops *ops;
1956 	struct bpf_prog *prog;
1957 
1958 	u32 flags;
1959 	enum bpf_attach_type attach_type;
1960 
1961 	/* rcu is used before freeing, work can be used to schedule that
1962 	 * RCU-based freeing before that, so they never overlap
1963 	 */
1964 	union {
1965 		struct rcu_head rcu;
1966 		struct work_struct work;
1967 	};
1968 	/* whether BPF link itself has "sleepable" semantics, which can differ
1969 	 * from underlying BPF program having a "sleepable" semantics, as BPF
1970 	 * link's semantics is determined by target attach hook
1971 	 */
1972 	bool sleepable;
1973 };
1974 
1975 struct bpf_link_ops {
1976 	void (*release)(struct bpf_link *link);
1977 	/* deallocate link resources callback, called without RCU grace period
1978 	 * waiting
1979 	 */
1980 	void (*dealloc)(struct bpf_link *link);
1981 	/* deallocate link resources callback, called after RCU grace period;
1982 	 * if either the underlying BPF program is sleepable or BPF link's
1983 	 * target hook is sleepable, we'll go through tasks trace RCU GP and
1984 	 * then "classic" RCU GP; this need for chaining tasks trace and
1985 	 * classic RCU GPs is designated by setting bpf_link->sleepable flag
1986 	 *
1987 	 * For non-sleepable tracepoint links we go through SRCU gp instead,
1988 	 * since RCU is not used in that case. Sleepable tracepoints still
1989 	 * follow the scheme above.
1990 	 */
1991 	void (*dealloc_deferred)(struct bpf_link *link);
1992 	int (*detach)(struct bpf_link *link);
1993 	int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
1994 			   struct bpf_prog *old_prog);
1995 	void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);
1996 	int (*fill_link_info)(const struct bpf_link *link,
1997 			      struct bpf_link_info *info);
1998 	int (*update_map)(struct bpf_link *link, struct bpf_map *new_map,
1999 			  struct bpf_map *old_map);
2000 	__poll_t (*poll)(struct file *file, struct poll_table_struct *pts);
2001 };
2002 
2003 struct bpf_tramp_node {
2004 	struct bpf_link *link;
2005 	struct hlist_node tramp_hlist;
2006 	u64 cookie;
2007 };
2008 
2009 struct bpf_tramp_link {
2010 	struct bpf_link link;
2011 	struct bpf_tramp_node node;
2012 };
2013 
2014 struct bpf_shim_tramp_link {
2015 	struct bpf_tramp_link link;
2016 	struct bpf_trampoline *trampoline;
2017 };
2018 
2019 struct bpf_tracing_link {
2020 	struct bpf_tramp_link link;
2021 	struct bpf_tramp_node fexit;
2022 	struct bpf_trampoline *trampoline;
2023 	struct bpf_prog *tgt_prog;
2024 };
2025 
2026 struct bpf_tracing_multi_node {
2027 	struct bpf_tramp_node node;
2028 	struct bpf_trampoline *trampoline;
2029 	struct ftrace_func_entry entry;
2030 };
2031 
2032 struct bpf_tracing_multi_data {
2033 	struct ftrace_hash *unreg;
2034 	struct ftrace_hash *modify;
2035 	struct ftrace_hash *reg;
2036 	struct ftrace_func_entry *entry;
2037 };
2038 
2039 struct bpf_tracing_multi_link {
2040 	struct bpf_link link;
2041 	struct bpf_tracing_multi_data data;
2042 	u64 *cookies;
2043 	struct bpf_tramp_node *fexits;
2044 	int nodes_cnt;
2045 	struct bpf_tracing_multi_node nodes[] __counted_by(nodes_cnt);
2046 };
2047 
2048 struct bpf_raw_tp_link {
2049 	struct bpf_link link;
2050 	struct bpf_raw_event_map *btp;
2051 	u64 cookie;
2052 };
2053 
2054 struct bpf_link_primer {
2055 	struct bpf_link *link;
2056 	struct file *file;
2057 	int fd;
2058 	u32 id;
2059 };
2060 
2061 struct bpf_mount_opts {
2062 	kuid_t uid;
2063 	kgid_t gid;
2064 	umode_t mode;
2065 
2066 	/* BPF token-related delegation options */
2067 	u64 delegate_cmds;
2068 	u64 delegate_maps;
2069 	u64 delegate_progs;
2070 	u64 delegate_attachs;
2071 
2072 	struct simple_xattr_cache xa_cache;
2073 };
2074 
2075 struct bpf_token {
2076 	struct work_struct work;
2077 	atomic64_t refcnt;
2078 	struct user_namespace *userns;
2079 	u64 allowed_cmds;
2080 	u64 allowed_maps;
2081 	u64 allowed_progs;
2082 	u64 allowed_attachs;
2083 #ifdef CONFIG_SECURITY
2084 	void *security;
2085 #endif
2086 };
2087 
2088 struct bpf_struct_ops_value;
2089 struct btf_member;
2090 
2091 #define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
2092 /**
2093  * struct bpf_struct_ops - A structure of callbacks allowing a subsystem to
2094  *			   define a BPF_MAP_TYPE_STRUCT_OPS map type composed
2095  *			   of BPF_PROG_TYPE_STRUCT_OPS progs.
2096  * @verifier_ops: A structure of callbacks that are invoked by the verifier
2097  *		  when determining whether the struct_ops progs in the
2098  *		  struct_ops map are valid.
2099  * @init: A callback that is invoked a single time, and before any other
2100  *	  callback, to initialize the structure. A nonzero return value means
2101  *	  the subsystem could not be initialized.
2102  * @check_member: When defined, a callback invoked by the verifier to allow
2103  *		  the subsystem to determine if an entry in the struct_ops map
2104  *		  is valid. A nonzero return value means that the map is
2105  *		  invalid and should be rejected by the verifier.
2106  * @init_member: A callback that is invoked for each member of the struct_ops
2107  *		 map to allow the subsystem to initialize the member. A nonzero
2108  *		 value means the member could not be initialized. This callback
2109  *		 is exclusive with the @type, @type_id, @value_type, and
2110  *		 @value_id fields.
2111  * @reg: A callback that is invoked when the struct_ops map has been
2112  *	 initialized and is being attached to. Zero means the struct_ops map
2113  *	 has been successfully registered and is live. A nonzero return value
2114  *	 means the struct_ops map could not be registered.
2115  * @unreg: A callback that is invoked when the struct_ops map should be
2116  *	   unregistered.
2117  * @update: A callback that is invoked when the live struct_ops map is being
2118  *	    updated to contain new values. This callback is only invoked when
2119  *	    the struct_ops map is loaded with BPF_F_LINK. If not defined, the
2120  *	    it is assumed that the struct_ops map cannot be updated.
2121  * @validate: A callback that is invoked after all of the members have been
2122  *	      initialized. This callback should perform static checks on the
2123  *	      map, meaning that it should either fail or succeed
2124  *	      deterministically. A struct_ops map that has been validated may
2125  *	      not necessarily succeed in being registered if the call to @reg
2126  *	      fails. For example, a valid struct_ops map may be loaded, but
2127  *	      then fail to be registered due to there being another active
2128  *	      struct_ops map on the system in the subsystem already. For this
2129  *	      reason, if this callback is not defined, the check is skipped as
2130  *	      the struct_ops map will have final verification performed in
2131  *	      @reg.
2132  * @cfi_stubs: Pointer to a structure of stub functions for CFI. These stubs
2133  *	       provide the correct Control Flow Integrity hashes for the
2134  *	       trampolines generated by BPF struct_ops.
2135  * @owner: The module that owns this struct_ops. Used for module reference
2136  *	   counting to ensure the module providing the struct_ops cannot be
2137  *	   unloaded while in use.
2138  * @name: The name of the struct bpf_struct_ops object.
2139  * @func_models: Func models
2140  */
2141 struct bpf_struct_ops {
2142 	const struct bpf_verifier_ops *verifier_ops;
2143 	int (*init)(struct btf *btf);
2144 	int (*check_member)(const struct btf_type *t,
2145 			    const struct btf_member *member,
2146 			    const struct bpf_prog *prog);
2147 	int (*init_member)(const struct btf_type *t,
2148 			   const struct btf_member *member,
2149 			   void *kdata, const void *udata);
2150 	int (*reg)(void *kdata, struct bpf_link *link);
2151 	void (*unreg)(void *kdata, struct bpf_link *link);
2152 	int (*update)(void *kdata, void *old_kdata, struct bpf_link *link);
2153 	int (*validate)(void *kdata);
2154 	void *cfi_stubs;
2155 	struct module *owner;
2156 	const char *name;
2157 	struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
2158 };
2159 
2160 /* Every member of a struct_ops type has an instance even a member is not
2161  * an operator (function pointer). The "info" field will be assigned to
2162  * prog->aux->ctx_arg_info of BPF struct_ops programs to provide the
2163  * argument information required by the verifier to verify the program.
2164  *
2165  * btf_ctx_access() will lookup prog->aux->ctx_arg_info to find the
2166  * corresponding entry for an given argument.
2167  */
2168 struct bpf_struct_ops_arg_info {
2169 	struct bpf_ctx_arg_aux *info;
2170 	u32 cnt;
2171 };
2172 
2173 struct bpf_struct_ops_desc {
2174 	struct bpf_struct_ops *st_ops;
2175 
2176 	const struct btf_type *type;
2177 	const struct btf_type *value_type;
2178 	u32 type_id;
2179 	u32 value_id;
2180 
2181 	/* Collection of argument information for each member */
2182 	struct bpf_struct_ops_arg_info *arg_info;
2183 };
2184 
2185 enum bpf_struct_ops_state {
2186 	BPF_STRUCT_OPS_STATE_INIT,
2187 	BPF_STRUCT_OPS_STATE_INUSE,
2188 	BPF_STRUCT_OPS_STATE_TOBEFREE,
2189 	BPF_STRUCT_OPS_STATE_READY,
2190 };
2191 
2192 struct bpf_struct_ops_common_value {
2193 	refcount_t refcnt;
2194 	enum bpf_struct_ops_state state;
2195 };
2196 
2197 static inline bool bpf_prog_get_recursion_context(struct bpf_prog *prog)
2198 {
2199 #ifdef CONFIG_ARM64
2200 	u8 rctx = interrupt_context_level();
2201 	u8 *active = this_cpu_ptr(prog->active);
2202 	u32 val;
2203 
2204 	preempt_disable();
2205 	active[rctx]++;
2206 	val = le32_to_cpu(*(__le32 *)active);
2207 	preempt_enable();
2208 	if (val != BIT(rctx * 8))
2209 		return false;
2210 
2211 	return true;
2212 #else
2213 	return this_cpu_inc_return(*(int __percpu *)(prog->active)) == 1;
2214 #endif
2215 }
2216 
2217 static inline void bpf_prog_put_recursion_context(struct bpf_prog *prog)
2218 {
2219 #ifdef CONFIG_ARM64
2220 	u8 rctx = interrupt_context_level();
2221 	u8 *active = this_cpu_ptr(prog->active);
2222 
2223 	preempt_disable();
2224 	active[rctx]--;
2225 	preempt_enable();
2226 #else
2227 	this_cpu_dec(*(int __percpu *)(prog->active));
2228 #endif
2229 }
2230 
2231 static inline bool is_tracing_multi(enum bpf_attach_type type)
2232 {
2233 	return type == BPF_TRACE_FENTRY_MULTI || type == BPF_TRACE_FEXIT_MULTI ||
2234 	       type == BPF_TRACE_FSESSION_MULTI;
2235 }
2236 
2237 static inline bool is_struct_ops_tramp(const struct bpf_tramp_nodes *fentry_nodes)
2238 {
2239 	return fentry_nodes->nr_nodes == 1 &&
2240 	       fentry_nodes->nodes[0]->link->type == BPF_LINK_TYPE_STRUCT_OPS;
2241 }
2242 
2243 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
2244 /* This macro helps developer to register a struct_ops type and generate
2245  * type information correctly. Developers should use this macro to register
2246  * a struct_ops type instead of calling __register_bpf_struct_ops() directly.
2247  */
2248 #define register_bpf_struct_ops(st_ops, type)				\
2249 	({								\
2250 		struct bpf_struct_ops_##type {				\
2251 			struct bpf_struct_ops_common_value common;	\
2252 			struct type data ____cacheline_aligned_in_smp;	\
2253 		};							\
2254 		BTF_TYPE_EMIT(struct bpf_struct_ops_##type);		\
2255 		__register_bpf_struct_ops(st_ops);			\
2256 	})
2257 #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
2258 bool bpf_struct_ops_get(const void *kdata);
2259 void bpf_struct_ops_put(const void *kdata);
2260 int bpf_struct_ops_supported(const struct bpf_struct_ops *st_ops, u32 moff);
2261 int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
2262 				       void *value);
2263 int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_nodes *tnodes,
2264 				      struct bpf_tramp_node *node,
2265 				      const struct btf_func_model *model,
2266 				      void *stub_func,
2267 				      void **image, u32 *image_off,
2268 				      bool allow_alloc);
2269 void bpf_struct_ops_image_free(void *image);
2270 static inline bool bpf_try_module_get(const void *data, struct module *owner)
2271 {
2272 	if (owner == BPF_MODULE_OWNER)
2273 		return bpf_struct_ops_get(data);
2274 	else
2275 		return try_module_get(owner);
2276 }
2277 static inline void bpf_module_put(const void *data, struct module *owner)
2278 {
2279 	if (owner == BPF_MODULE_OWNER)
2280 		bpf_struct_ops_put(data);
2281 	else
2282 		module_put(owner);
2283 }
2284 int bpf_struct_ops_link_create(union bpf_attr *attr);
2285 int bpf_prog_assoc_struct_ops(struct bpf_prog *prog, struct bpf_map *map);
2286 void bpf_prog_disassoc_struct_ops(struct bpf_prog *prog);
2287 void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux);
2288 u32 bpf_struct_ops_id(const void *kdata);
2289 int bpf_struct_ops_for_each_prog(const void *kdata,
2290 				 int (*cb)(struct bpf_prog *prog, void *data),
2291 				 void *data);
2292 
2293 #ifdef CONFIG_NET
2294 /* Define it here to avoid the use of forward declaration */
2295 struct bpf_dummy_ops_state {
2296 	int val;
2297 };
2298 
2299 struct bpf_dummy_ops {
2300 	int (*test_1)(struct bpf_dummy_ops_state *cb);
2301 	int (*test_2)(struct bpf_dummy_ops_state *cb, int a1, unsigned short a2,
2302 		      char a3, unsigned long a4);
2303 	int (*test_sleepable)(struct bpf_dummy_ops_state *cb);
2304 };
2305 
2306 int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
2307 			    union bpf_attr __user *uattr);
2308 #endif
2309 int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
2310 			     struct btf *btf,
2311 			     struct bpf_verifier_log *log);
2312 void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map);
2313 void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_ops_desc);
2314 #else
2315 #define register_bpf_struct_ops(st_ops, type) ({ (void *)(st_ops); 0; })
2316 static inline bool bpf_try_module_get(const void *data, struct module *owner)
2317 {
2318 	return try_module_get(owner);
2319 }
2320 static inline void bpf_module_put(const void *data, struct module *owner)
2321 {
2322 	module_put(owner);
2323 }
2324 static inline int bpf_struct_ops_supported(const struct bpf_struct_ops *st_ops, u32 moff)
2325 {
2326 	return -ENOTSUPP;
2327 }
2328 static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
2329 						     void *key,
2330 						     void *value)
2331 {
2332 	return -EINVAL;
2333 }
2334 static inline int bpf_struct_ops_link_create(union bpf_attr *attr)
2335 {
2336 	return -EOPNOTSUPP;
2337 }
2338 static inline int bpf_prog_assoc_struct_ops(struct bpf_prog *prog, struct bpf_map *map)
2339 {
2340 	return -EOPNOTSUPP;
2341 }
2342 static inline void bpf_prog_disassoc_struct_ops(struct bpf_prog *prog)
2343 {
2344 }
2345 static inline void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux)
2346 {
2347 	return NULL;
2348 }
2349 static inline void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
2350 {
2351 }
2352 
2353 static inline void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_ops_desc)
2354 {
2355 }
2356 
2357 #endif
2358 
2359 static inline int bpf_fsession_cnt(struct bpf_tramp_nodes *nodes)
2360 {
2361 	struct bpf_tramp_nodes fentries = nodes[BPF_TRAMP_FENTRY];
2362 	int cnt = 0;
2363 
2364 	for (int i = 0; i < nodes[BPF_TRAMP_FENTRY].nr_nodes; i++) {
2365 		if (fentries.nodes[i]->link->prog->expected_attach_type == BPF_TRACE_FSESSION)
2366 			cnt++;
2367 		if (fentries.nodes[i]->link->prog->expected_attach_type == BPF_TRACE_FSESSION_MULTI)
2368 			cnt++;
2369 	}
2370 
2371 	return cnt;
2372 }
2373 
2374 static inline bool bpf_prog_calls_session_cookie(struct bpf_tramp_node *node)
2375 {
2376 	return node->link->prog->call_session_cookie;
2377 }
2378 
2379 static inline int bpf_fsession_cookie_cnt(struct bpf_tramp_nodes *nodes)
2380 {
2381 	struct bpf_tramp_nodes fentries = nodes[BPF_TRAMP_FENTRY];
2382 	int cnt = 0;
2383 
2384 	for (int i = 0; i < nodes[BPF_TRAMP_FENTRY].nr_nodes; i++) {
2385 		if (bpf_prog_calls_session_cookie(fentries.nodes[i]))
2386 			cnt++;
2387 	}
2388 
2389 	return cnt;
2390 }
2391 
2392 int bpf_prog_ctx_arg_info_init(struct bpf_prog *prog,
2393 			       const struct bpf_ctx_arg_aux *info, u32 cnt);
2394 
2395 #if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM)
2396 int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
2397 				    int cgroup_atype,
2398 				    enum bpf_attach_type attach_type);
2399 void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog);
2400 #else
2401 static inline int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
2402 						  int cgroup_atype,
2403 						  enum bpf_attach_type attach_type)
2404 {
2405 	return -EOPNOTSUPP;
2406 }
2407 static inline void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog)
2408 {
2409 }
2410 #endif
2411 
2412 struct bpf_array {
2413 	struct bpf_map map;
2414 	u32 elem_size;
2415 	u32 index_mask;
2416 	struct bpf_array_aux *aux;
2417 	union {
2418 		DECLARE_FLEX_ARRAY(char, value) __aligned(8);
2419 		DECLARE_FLEX_ARRAY(void *, ptrs) __aligned(8);
2420 		DECLARE_FLEX_ARRAY(void __percpu *, pptrs) __aligned(8);
2421 	};
2422 };
2423 
2424 /*
2425  * The bpf_array_get_next_key() function may be used for all array-like
2426  * maps, i.e., maps with u32 keys with range [0 ,..., max_entries)
2427  */
2428 int bpf_array_get_next_key(struct bpf_map *map, void *key, void *next_key);
2429 
2430 #define BPF_COMPLEXITY_LIMIT_INSNS      1000000 /* yes. 1M insns */
2431 #define MAX_TAIL_CALL_CNT 33
2432 
2433 /* Maximum number of loops for bpf_loop and bpf_iter_num.
2434  * It's enum to expose it (and thus make it discoverable) through BTF.
2435  */
2436 enum {
2437 	BPF_MAX_LOOPS = 8 * 1024 * 1024,
2438 	BPF_MAX_TIMED_LOOPS = 0xffff,
2439 };
2440 
2441 #define BPF_F_ACCESS_MASK	(BPF_F_RDONLY |		\
2442 				 BPF_F_RDONLY_PROG |	\
2443 				 BPF_F_WRONLY |		\
2444 				 BPF_F_WRONLY_PROG)
2445 
2446 #define BPF_MAP_CAN_READ	BIT(0)
2447 #define BPF_MAP_CAN_WRITE	BIT(1)
2448 
2449 /* Maximum number of user-producer ring buffer samples that can be drained in
2450  * a call to bpf_user_ringbuf_drain().
2451  */
2452 #define BPF_MAX_USER_RINGBUF_SAMPLES (128 * 1024)
2453 
2454 static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
2455 {
2456 	u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
2457 
2458 	/* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
2459 	 * not possible.
2460 	 */
2461 	if (access_flags & BPF_F_RDONLY_PROG)
2462 		return BPF_MAP_CAN_READ;
2463 	else if (access_flags & BPF_F_WRONLY_PROG)
2464 		return BPF_MAP_CAN_WRITE;
2465 	else
2466 		return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
2467 }
2468 
2469 static inline bool bpf_map_flags_access_ok(u32 access_flags)
2470 {
2471 	return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
2472 	       (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
2473 }
2474 
2475 static inline struct bpf_map_owner *bpf_map_owner_alloc(struct bpf_map *map)
2476 {
2477 	return kzalloc_obj(*map->owner, GFP_ATOMIC);
2478 }
2479 
2480 static inline void bpf_map_owner_free(struct bpf_map *map)
2481 {
2482 	kfree(map->owner);
2483 }
2484 
2485 struct bpf_event_entry {
2486 	struct perf_event *event;
2487 	struct file *perf_file;
2488 	struct file *map_file;
2489 	struct rcu_head rcu;
2490 };
2491 
2492 static inline bool map_type_contains_progs(struct bpf_map *map)
2493 {
2494 	return map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
2495 	       map->map_type == BPF_MAP_TYPE_DEVMAP ||
2496 	       map->map_type == BPF_MAP_TYPE_CPUMAP;
2497 }
2498 
2499 bool bpf_prog_map_compatible(struct bpf_map *map, const struct bpf_prog *fp);
2500 int bpf_prog_calc_tag(struct bpf_prog *fp);
2501 
2502 const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
2503 const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void);
2504 
2505 const struct bpf_func_proto *bpf_get_perf_event_read_value_proto(void);
2506 
2507 typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
2508 					unsigned long off, unsigned long len);
2509 typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
2510 					const struct bpf_insn *src,
2511 					struct bpf_insn *dst,
2512 					struct bpf_prog *prog,
2513 					u32 *target_size);
2514 
2515 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
2516 		     void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
2517 
2518 /* an array of programs to be executed under rcu_lock.
2519  *
2520  * Typical usage:
2521  * ret = bpf_prog_run_array(rcu_dereference(&bpf_prog_array), ctx, bpf_prog_run);
2522  *
2523  * the structure returned by bpf_prog_array_alloc() should be populated
2524  * with program pointers and the last pointer must be NULL.
2525  * The user has to keep refcnt on the program and make sure the program
2526  * is removed from the array before bpf_prog_put().
2527  * The 'struct bpf_prog_array *' should only be replaced with xchg()
2528  * since other cpus are walking the array of pointers in parallel.
2529  */
2530 struct bpf_prog_array_item {
2531 	struct bpf_prog *prog;
2532 	union {
2533 		struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
2534 		u64 bpf_cookie;
2535 	};
2536 };
2537 
2538 struct bpf_prog_array {
2539 	struct rcu_head rcu;
2540 	struct bpf_prog_array_item items[];
2541 };
2542 
2543 /* to avoid allocating empty bpf_prog_array for cgroups that
2544  * don't have bpf program attached use one global 'bpf_empty_prog_array'
2545  * It will not be modified the caller of bpf_prog_array_alloc()
2546  * (since caller requested prog_cnt == 0)
2547  * that pointer should be 'freed' by bpf_prog_array_free()
2548  */
2549 extern struct bpf_prog_array bpf_empty_prog_array;
2550 
2551 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
2552 void bpf_prog_array_free(struct bpf_prog_array *progs);
2553 /* Use when traversal over the bpf_prog_array uses tasks_trace rcu */
2554 void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs);
2555 int bpf_prog_array_length(struct bpf_prog_array *progs);
2556 bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
2557 int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
2558 				__u32 __user *prog_ids, u32 cnt);
2559 
2560 void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
2561 				struct bpf_prog *old_prog);
2562 int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index);
2563 int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
2564 			     struct bpf_prog *prog);
2565 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
2566 			     u32 *prog_ids, u32 request_cnt,
2567 			     u32 *prog_cnt);
2568 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
2569 			struct bpf_prog *exclude_prog,
2570 			struct bpf_prog *include_prog,
2571 			u64 bpf_cookie,
2572 			struct bpf_prog_array **new_array);
2573 
2574 struct bpf_run_ctx {};
2575 
2576 struct bpf_cg_run_ctx {
2577 	struct bpf_run_ctx run_ctx;
2578 	const struct bpf_prog_array_item *prog_item;
2579 	int retval;
2580 };
2581 
2582 struct bpf_trace_run_ctx {
2583 	struct bpf_run_ctx run_ctx;
2584 	u64 bpf_cookie;
2585 	bool is_uprobe;
2586 };
2587 
2588 struct bpf_tramp_run_ctx {
2589 	struct bpf_run_ctx run_ctx;
2590 	u64 bpf_cookie;
2591 	struct bpf_run_ctx *saved_run_ctx;
2592 };
2593 
2594 static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
2595 {
2596 	struct bpf_run_ctx *old_ctx = NULL;
2597 
2598 #ifdef CONFIG_BPF_SYSCALL
2599 	old_ctx = current->bpf_ctx;
2600 	current->bpf_ctx = new_ctx;
2601 #endif
2602 	return old_ctx;
2603 }
2604 
2605 static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)
2606 {
2607 #ifdef CONFIG_BPF_SYSCALL
2608 	current->bpf_ctx = old_ctx;
2609 #endif
2610 }
2611 
2612 /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
2613 #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE			(1 << 0)
2614 /* BPF program asks to set CN on the packet. */
2615 #define BPF_RET_SET_CN						(1 << 0)
2616 
2617 typedef u32 (*bpf_prog_run_fn)(const struct bpf_prog *prog, const void *ctx);
2618 
2619 static __always_inline u32
2620 bpf_prog_run_array(const struct bpf_prog_array *array,
2621 		   const void *ctx, bpf_prog_run_fn run_prog)
2622 {
2623 	const struct bpf_prog_array_item *item;
2624 	const struct bpf_prog *prog;
2625 	struct bpf_run_ctx *old_run_ctx;
2626 	struct bpf_trace_run_ctx run_ctx;
2627 	u32 ret = 1;
2628 
2629 	RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held");
2630 
2631 	if (unlikely(!array))
2632 		return ret;
2633 
2634 	run_ctx.is_uprobe = false;
2635 
2636 	migrate_disable();
2637 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
2638 	item = &array->items[0];
2639 	while ((prog = READ_ONCE(item->prog))) {
2640 		run_ctx.bpf_cookie = item->bpf_cookie;
2641 		ret &= run_prog(prog, ctx);
2642 		item++;
2643 	}
2644 	bpf_reset_run_ctx(old_run_ctx);
2645 	migrate_enable();
2646 	return ret;
2647 }
2648 
2649 /* Notes on RCU design for bpf_prog_arrays containing sleepable programs:
2650  *
2651  * We use the tasks_trace rcu flavor read section to protect the bpf_prog_array
2652  * overall. As a result, we must use the bpf_prog_array_free_sleepable
2653  * in order to use the tasks_trace rcu grace period.
2654  *
2655  * When a non-sleepable program is inside the array, we take the rcu read
2656  * section and disable preemption for that program alone, so it can access
2657  * rcu-protected dynamically sized maps.
2658  */
2659 static __always_inline u32
2660 bpf_prog_run_array_uprobe(const struct bpf_prog_array *array,
2661 			  const void *ctx, bpf_prog_run_fn run_prog)
2662 {
2663 	const struct bpf_prog_array_item *item;
2664 	const struct bpf_prog *prog;
2665 	struct bpf_run_ctx *old_run_ctx;
2666 	struct bpf_trace_run_ctx run_ctx;
2667 	u32 ret = 1;
2668 
2669 	might_fault();
2670 	RCU_LOCKDEP_WARN(!rcu_read_lock_trace_held(), "no rcu lock held");
2671 
2672 	if (unlikely(!array))
2673 		return ret;
2674 
2675 	migrate_disable();
2676 
2677 	run_ctx.is_uprobe = true;
2678 
2679 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
2680 	item = &array->items[0];
2681 	while ((prog = READ_ONCE(item->prog))) {
2682 		if (!prog->sleepable)
2683 			rcu_read_lock();
2684 
2685 		run_ctx.bpf_cookie = item->bpf_cookie;
2686 		ret &= run_prog(prog, ctx);
2687 		item++;
2688 
2689 		if (!prog->sleepable)
2690 			rcu_read_unlock();
2691 	}
2692 	bpf_reset_run_ctx(old_run_ctx);
2693 	migrate_enable();
2694 	return ret;
2695 }
2696 
2697 bool bpf_jit_bypass_spec_v1(void);
2698 bool bpf_jit_bypass_spec_v4(void);
2699 
2700 #define bpf_rcu_lock_held() \
2701 	(rcu_read_lock_held() || rcu_read_lock_trace_held() || rcu_read_lock_bh_held())
2702 
2703 #ifdef CONFIG_BPF_SYSCALL
2704 DECLARE_PER_CPU(int, bpf_prog_active);
2705 extern struct mutex bpf_stats_enabled_mutex;
2706 
2707 /*
2708  * Block execution of BPF programs attached to instrumentation (perf,
2709  * kprobes, tracepoints) to prevent deadlocks on map operations as any of
2710  * these events can happen inside a region which holds a map bucket lock
2711  * and can deadlock on it.
2712  */
2713 static inline void bpf_disable_instrumentation(void)
2714 {
2715 	migrate_disable();
2716 	this_cpu_inc(bpf_prog_active);
2717 }
2718 
2719 static inline void bpf_enable_instrumentation(void)
2720 {
2721 	this_cpu_dec(bpf_prog_active);
2722 	migrate_enable();
2723 }
2724 
2725 extern const struct super_operations bpf_super_ops;
2726 extern const struct file_operations bpf_map_fops;
2727 extern const struct file_operations bpf_prog_fops;
2728 extern const struct file_operations bpf_iter_fops;
2729 extern const struct file_operations bpf_token_fops;
2730 
2731 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
2732 	extern const struct bpf_prog_ops _name ## _prog_ops; \
2733 	extern const struct bpf_verifier_ops _name ## _verifier_ops;
2734 #define BPF_MAP_TYPE(_id, _ops) \
2735 	extern const struct bpf_map_ops _ops;
2736 #define BPF_LINK_TYPE(_id, _name)
2737 #include <linux/bpf_types.h>
2738 #undef BPF_PROG_TYPE
2739 #undef BPF_MAP_TYPE
2740 #undef BPF_LINK_TYPE
2741 
2742 extern const struct bpf_prog_ops bpf_offload_prog_ops;
2743 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
2744 extern const struct bpf_verifier_ops xdp_analyzer_ops;
2745 
2746 struct bpf_prog *bpf_prog_get(u32 ufd);
2747 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
2748 				       bool attach_drv);
2749 void bpf_prog_add(struct bpf_prog *prog, int i);
2750 void bpf_prog_sub(struct bpf_prog *prog, int i);
2751 void bpf_prog_inc(struct bpf_prog *prog);
2752 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
2753 void bpf_prog_put(struct bpf_prog *prog);
2754 
2755 void bpf_prog_free_id(struct bpf_prog *prog);
2756 void bpf_map_free_id(struct bpf_map *map);
2757 
2758 struct btf_field *btf_record_find(const struct btf_record *rec,
2759 				  u32 offset, u32 field_mask);
2760 void btf_record_free(struct btf_record *rec);
2761 void bpf_map_free_record(struct bpf_map *map);
2762 struct btf_record *btf_record_dup(const struct btf_record *rec);
2763 bool btf_record_equal(const struct btf_record *rec_a, const struct btf_record *rec_b);
2764 void bpf_obj_free_timer(const struct btf_record *rec, void *obj);
2765 void bpf_obj_free_workqueue(const struct btf_record *rec, void *obj);
2766 void bpf_obj_free_task_work(const struct btf_record *rec, void *obj);
2767 void bpf_obj_cancel_fields(struct bpf_map *map, void *obj);
2768 void bpf_obj_free_fields(const struct btf_record *rec, void *obj);
2769 void __bpf_obj_drop_impl(void *p, const struct btf_record *rec, bool percpu);
2770 
2771 struct bpf_map *bpf_map_get(u32 ufd);
2772 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
2773 
2774 /*
2775  * The __bpf_map_get() and __btf_get_by_fd() functions parse a file
2776  * descriptor and return a corresponding map or btf object.
2777  * Their names are double underscored to emphasize the fact that they
2778  * do not increase refcnt. To also increase refcnt use corresponding
2779  * bpf_map_get() and btf_get_by_fd() functions.
2780  */
2781 
2782 static inline struct bpf_map *__bpf_map_get(struct fd f)
2783 {
2784 	if (fd_empty(f))
2785 		return ERR_PTR(-EBADF);
2786 	if (unlikely(fd_file(f)->f_op != &bpf_map_fops))
2787 		return ERR_PTR(-EINVAL);
2788 	return fd_file(f)->private_data;
2789 }
2790 
2791 static inline struct btf *__btf_get_by_fd(struct fd f)
2792 {
2793 	if (fd_empty(f))
2794 		return ERR_PTR(-EBADF);
2795 	if (unlikely(fd_file(f)->f_op != &btf_fops))
2796 		return ERR_PTR(-EINVAL);
2797 	return fd_file(f)->private_data;
2798 }
2799 
2800 void bpf_map_inc(struct bpf_map *map);
2801 void bpf_map_inc_with_uref(struct bpf_map *map);
2802 struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref);
2803 struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
2804 void bpf_map_put_with_uref(struct bpf_map *map);
2805 void bpf_map_put(struct bpf_map *map);
2806 void *bpf_map_area_alloc(u64 size, int numa_node);
2807 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
2808 void bpf_map_area_free(void *base);
2809 bool bpf_map_write_active(const struct bpf_map *map);
2810 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
2811 int  generic_map_lookup_batch(struct bpf_map *map,
2812 			      const union bpf_attr *attr,
2813 			      union bpf_attr __user *uattr);
2814 int  generic_map_update_batch(struct bpf_map *map, struct file *map_file,
2815 			      const union bpf_attr *attr,
2816 			      union bpf_attr __user *uattr);
2817 int  generic_map_delete_batch(struct bpf_map *map,
2818 			      const union bpf_attr *attr,
2819 			      union bpf_attr __user *uattr);
2820 struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
2821 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id);
2822 
2823 
2824 int bpf_map_alloc_pages(const struct bpf_map *map, int nid,
2825 			unsigned long nr_pages, struct page **page_array);
2826 #ifdef CONFIG_MEMCG
2827 void bpf_map_memcg_enter(const struct bpf_map *map, struct mem_cgroup **old_memcg,
2828 			 struct mem_cgroup **new_memcg);
2829 void bpf_map_memcg_exit(struct mem_cgroup *old_memcg,
2830 			struct mem_cgroup *memcg);
2831 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
2832 			   int node);
2833 void *bpf_map_kmalloc_nolock(const struct bpf_map *map, size_t size, gfp_t flags,
2834 			     int node);
2835 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags);
2836 void *bpf_map_kvcalloc(struct bpf_map *map, size_t n, size_t size,
2837 		       gfp_t flags);
2838 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
2839 				    size_t align, gfp_t flags);
2840 #else
2841 /*
2842  * These specialized allocators have to be macros for their allocations to be
2843  * accounted separately (to have separate alloc_tag).
2844  */
2845 #define bpf_map_kmalloc_node(_map, _size, _flags, _node)	\
2846 		kmalloc_node(_size, _flags, _node)
2847 #define bpf_map_kmalloc_nolock(_map, _size, _flags, _node)	\
2848 		kmalloc_nolock(_size, _flags, _node)
2849 #define bpf_map_kzalloc(_map, _size, _flags)			\
2850 		kzalloc(_size, _flags)
2851 #define bpf_map_kvcalloc(_map, _n, _size, _flags)		\
2852 		kvcalloc(_n, _size, _flags)
2853 #define bpf_map_alloc_percpu(_map, _size, _align, _flags)	\
2854 		__alloc_percpu_gfp(_size, _align, _flags)
2855 static inline void bpf_map_memcg_enter(const struct bpf_map *map, struct mem_cgroup **old_memcg,
2856 				       struct mem_cgroup **new_memcg)
2857 {
2858 	*new_memcg = NULL;
2859 	*old_memcg = NULL;
2860 }
2861 
2862 static inline void bpf_map_memcg_exit(struct mem_cgroup *old_memcg,
2863 				      struct mem_cgroup *memcg)
2864 {
2865 }
2866 #endif
2867 
2868 static inline int
2869 bpf_map_init_elem_count(struct bpf_map *map)
2870 {
2871 	size_t size = sizeof(*map->elem_count), align = size;
2872 	gfp_t flags = GFP_USER | __GFP_NOWARN;
2873 
2874 	map->elem_count = bpf_map_alloc_percpu(map, size, align, flags);
2875 	if (!map->elem_count)
2876 		return -ENOMEM;
2877 
2878 	return 0;
2879 }
2880 
2881 static inline void
2882 bpf_map_free_elem_count(struct bpf_map *map)
2883 {
2884 	free_percpu(map->elem_count);
2885 }
2886 
2887 static inline void bpf_map_inc_elem_count(struct bpf_map *map)
2888 {
2889 	this_cpu_inc(*map->elem_count);
2890 }
2891 
2892 static inline void bpf_map_dec_elem_count(struct bpf_map *map)
2893 {
2894 	this_cpu_dec(*map->elem_count);
2895 }
2896 
2897 extern int sysctl_unprivileged_bpf_disabled;
2898 
2899 bool bpf_token_capable(const struct bpf_token *token, int cap);
2900 
2901 static inline bool bpf_allow_ptr_leaks(const struct bpf_token *token)
2902 {
2903 	return bpf_token_capable(token, CAP_PERFMON);
2904 }
2905 
2906 static inline bool bpf_allow_uninit_stack(const struct bpf_token *token)
2907 {
2908 	return bpf_token_capable(token, CAP_PERFMON);
2909 }
2910 
2911 static inline bool bpf_bypass_spec_v1(const struct bpf_token *token)
2912 {
2913 	return bpf_jit_bypass_spec_v1() ||
2914 		cpu_mitigations_off() ||
2915 		bpf_token_capable(token, CAP_PERFMON);
2916 }
2917 
2918 static inline bool bpf_bypass_spec_v4(const struct bpf_token *token)
2919 {
2920 	return bpf_jit_bypass_spec_v4() ||
2921 		cpu_mitigations_off() ||
2922 		bpf_token_capable(token, CAP_PERFMON);
2923 }
2924 
2925 int bpf_map_new_fd(struct bpf_map *map, int flags);
2926 int bpf_prog_new_fd(struct bpf_prog *prog);
2927 
2928 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2929 		   const struct bpf_link_ops *ops, struct bpf_prog *prog,
2930 		   enum bpf_attach_type attach_type);
2931 void bpf_link_init_sleepable(struct bpf_link *link, enum bpf_link_type type,
2932 			     const struct bpf_link_ops *ops, struct bpf_prog *prog,
2933 			     enum bpf_attach_type attach_type, bool sleepable);
2934 void bpf_tramp_link_init(struct bpf_tramp_link *link, enum bpf_link_type type,
2935 			 const struct bpf_link_ops *ops, struct bpf_prog *prog,
2936 			 enum bpf_attach_type attach_type, u64 cookie);
2937 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
2938 int bpf_link_settle(struct bpf_link_primer *primer);
2939 void bpf_link_cleanup(struct bpf_link_primer *primer);
2940 void bpf_link_inc(struct bpf_link *link);
2941 struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link);
2942 void bpf_link_put(struct bpf_link *link);
2943 int bpf_link_new_fd(struct bpf_link *link);
2944 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
2945 struct bpf_link *bpf_link_get_curr_or_next(u32 *id);
2946 
2947 void bpf_token_inc(struct bpf_token *token);
2948 void bpf_token_put(struct bpf_token *token);
2949 int bpf_token_create(union bpf_attr *attr);
2950 struct bpf_token *bpf_token_get_from_fd(u32 ufd);
2951 int bpf_token_get_info_by_fd(struct bpf_token *token,
2952 			     const union bpf_attr *attr,
2953 			     union bpf_attr __user *uattr);
2954 
2955 bool bpf_token_allow_cmd(const struct bpf_token *token, enum bpf_cmd cmd);
2956 bool bpf_token_allow_map_type(const struct bpf_token *token, enum bpf_map_type type);
2957 bool bpf_token_allow_prog_type(const struct bpf_token *token,
2958 			       enum bpf_prog_type prog_type,
2959 			       enum bpf_attach_type attach_type);
2960 
2961 int bpf_obj_pin_user(u32 ufd, int path_fd, const char __user *pathname);
2962 int bpf_obj_get_user(int path_fd, const char __user *pathname, int flags);
2963 struct inode *bpf_get_inode(struct super_block *sb, const struct inode *dir,
2964 			    umode_t mode);
2965 
2966 #define BPF_ITER_FUNC_PREFIX "bpf_iter_"
2967 #define DEFINE_BPF_ITER_FUNC(target, args...)			\
2968 	extern int bpf_iter_ ## target(args);			\
2969 	int __init bpf_iter_ ## target(args) { return 0; }
2970 
2971 /*
2972  * The task type of iterators.
2973  *
2974  * For BPF task iterators, they can be parameterized with various
2975  * parameters to visit only some of tasks.
2976  *
2977  * BPF_TASK_ITER_ALL (default)
2978  *	Iterate over resources of every task.
2979  *
2980  * BPF_TASK_ITER_TID
2981  *	Iterate over resources of a task/tid.
2982  *
2983  * BPF_TASK_ITER_TGID
2984  *	Iterate over resources of every task of a process / task group.
2985  */
2986 enum bpf_iter_task_type {
2987 	BPF_TASK_ITER_ALL = 0,
2988 	BPF_TASK_ITER_TID,
2989 	BPF_TASK_ITER_TGID,
2990 };
2991 
2992 struct bpf_iter_aux_info {
2993 	/* for map_elem iter */
2994 	struct bpf_map *map;
2995 
2996 	/* for cgroup iter */
2997 	struct {
2998 		struct cgroup *start; /* starting cgroup */
2999 		enum bpf_cgroup_iter_order order;
3000 	} cgroup;
3001 	struct {
3002 		enum bpf_iter_task_type	type;
3003 		u32 pid;
3004 	} task;
3005 };
3006 
3007 typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
3008 					union bpf_iter_link_info *linfo,
3009 					struct bpf_iter_aux_info *aux);
3010 typedef void (*bpf_iter_detach_target_t)(struct bpf_iter_aux_info *aux);
3011 typedef void (*bpf_iter_show_fdinfo_t) (const struct bpf_iter_aux_info *aux,
3012 					struct seq_file *seq);
3013 typedef int (*bpf_iter_fill_link_info_t)(const struct bpf_iter_aux_info *aux,
3014 					 struct bpf_link_info *info);
3015 typedef const struct bpf_func_proto *
3016 (*bpf_iter_get_func_proto_t)(enum bpf_func_id func_id,
3017 			     const struct bpf_prog *prog);
3018 
3019 enum bpf_iter_feature {
3020 	BPF_ITER_RESCHED	= BIT(0),
3021 };
3022 
3023 #define BPF_ITER_CTX_ARG_MAX 2
3024 struct bpf_iter_reg {
3025 	const char *target;
3026 	bpf_iter_attach_target_t attach_target;
3027 	bpf_iter_detach_target_t detach_target;
3028 	bpf_iter_show_fdinfo_t show_fdinfo;
3029 	bpf_iter_fill_link_info_t fill_link_info;
3030 	bpf_iter_get_func_proto_t get_func_proto;
3031 	u32 ctx_arg_info_size;
3032 	u32 feature;
3033 	struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
3034 	const struct bpf_iter_seq_info *seq_info;
3035 };
3036 
3037 struct bpf_iter_meta {
3038 	__bpf_md_ptr(struct seq_file *, seq);
3039 	u64 session_id;
3040 	u64 seq_num;
3041 };
3042 
3043 struct bpf_iter__bpf_map_elem {
3044 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
3045 	__bpf_md_ptr(struct bpf_map *, map);
3046 	__bpf_md_ptr(void *, key);
3047 	__bpf_md_ptr(void *, value);
3048 };
3049 
3050 int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
3051 void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
3052 int bpf_iter_prog_supported(struct bpf_prog *prog);
3053 const struct bpf_func_proto *
3054 bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
3055 int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_prog *prog);
3056 int bpf_iter_new_fd(struct bpf_link *link);
3057 bool bpf_link_is_iter(struct bpf_link *link);
3058 struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
3059 int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx);
3060 void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
3061 			      struct seq_file *seq);
3062 int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
3063 				struct bpf_link_info *info);
3064 
3065 int map_set_for_each_callback_args(struct bpf_verifier_env *env,
3066 				   struct bpf_func_state *caller,
3067 				   struct bpf_func_state *callee);
3068 
3069 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value, u64 flags);
3070 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value, u64 flags);
3071 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
3072 			   u64 flags);
3073 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
3074 			    u64 flags);
3075 
3076 int bpf_stackmap_extract(struct bpf_map *map, void *key, void *value, bool delete);
3077 
3078 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
3079 				 void *key, void *value, u64 map_flags);
3080 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
3081 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
3082 				void *key, void *value, u64 map_flags);
3083 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
3084 
3085 int bpf_get_file_flag(int flags);
3086 int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size,
3087 			     size_t actual_size);
3088 
3089 /* verify correctness of eBPF program */
3090 struct bpf_log_attr;
3091 int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr,
3092 	      struct bpf_log_attr *attr_log);
3093 
3094 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
3095 int bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
3096 s32 bpf_call_args_imm(s16 idx);
3097 #else
3098 static inline s32 bpf_call_args_imm(s16 idx)
3099 {
3100 	return 0;
3101 }
3102 #endif
3103 
3104 struct btf *bpf_get_btf_vmlinux(void);
3105 
3106 /* Map specifics */
3107 struct xdp_frame;
3108 struct sk_buff;
3109 struct bpf_dtab_netdev;
3110 struct bpf_cpu_map_entry;
3111 
3112 void __dev_flush(struct list_head *flush_list);
3113 int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
3114 		    struct net_device *dev_rx);
3115 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
3116 		    struct net_device *dev_rx);
3117 int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
3118 			  struct bpf_map *map, bool exclude_ingress);
3119 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
3120 			     const struct bpf_prog *xdp_prog);
3121 int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
3122 			   const struct bpf_prog *xdp_prog,
3123 			   struct bpf_map *map, bool exclude_ingress);
3124 
3125 void __cpu_map_flush(struct list_head *flush_list);
3126 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf,
3127 		    struct net_device *dev_rx);
3128 int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
3129 			     struct sk_buff *skb);
3130 
3131 /* Return map's numa specified by userspace */
3132 static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
3133 {
3134 	return (attr->map_flags & BPF_F_NUMA_NODE) ?
3135 		attr->numa_node : NUMA_NO_NODE;
3136 }
3137 
3138 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
3139 int array_map_alloc_check(union bpf_attr *attr);
3140 
3141 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
3142 			  union bpf_attr __user *uattr);
3143 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
3144 			  union bpf_attr __user *uattr);
3145 int bpf_prog_test_run_tracing(struct bpf_prog *prog,
3146 			      const union bpf_attr *kattr,
3147 			      union bpf_attr __user *uattr);
3148 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
3149 				     const union bpf_attr *kattr,
3150 				     union bpf_attr __user *uattr);
3151 int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
3152 			     const union bpf_attr *kattr,
3153 			     union bpf_attr __user *uattr);
3154 int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
3155 				const union bpf_attr *kattr,
3156 				union bpf_attr __user *uattr);
3157 int bpf_prog_test_run_nf(struct bpf_prog *prog,
3158 			 const union bpf_attr *kattr,
3159 			 union bpf_attr __user *uattr);
3160 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
3161 		    const struct bpf_prog *prog,
3162 		    struct bpf_insn_access_aux *info);
3163 
3164 static inline bool bpf_tracing_ctx_access(int off, int size,
3165 					  enum bpf_access_type type)
3166 {
3167 	if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
3168 		return false;
3169 	if (type != BPF_READ)
3170 		return false;
3171 	if (off % size != 0)
3172 		return false;
3173 	return true;
3174 }
3175 
3176 static inline bool bpf_tracing_btf_ctx_access(int off, int size,
3177 					      enum bpf_access_type type,
3178 					      const struct bpf_prog *prog,
3179 					      struct bpf_insn_access_aux *info)
3180 {
3181 	if (!bpf_tracing_ctx_access(off, size, type))
3182 		return false;
3183 	return btf_ctx_access(off, size, type, prog, info);
3184 }
3185 
3186 int btf_struct_access(struct bpf_verifier_log *log,
3187 		      const struct bpf_reg_state *reg,
3188 		      int off, int size, enum bpf_access_type atype,
3189 		      u32 *next_btf_id, enum bpf_type_flag *flag, const char **field_name);
3190 bool btf_struct_ids_match(struct bpf_verifier_log *log,
3191 			  const struct btf *btf, u32 id, int off,
3192 			  const struct btf *need_btf, u32 need_type_id,
3193 			  bool strict, bool walk_flex_arrays);
3194 
3195 int btf_distill_func_proto(struct bpf_verifier_log *log,
3196 			   struct btf *btf,
3197 			   const struct btf_type *func_proto,
3198 			   const char *func_name,
3199 			   struct btf_func_model *m);
3200 
3201 struct bpf_reg_state;
3202 int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog);
3203 int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
3204 			 struct btf *btf, const struct btf_type *t);
3205 const char *btf_find_decl_tag_value(const struct btf *btf, const struct btf_type *pt,
3206 				    int comp_idx, const char *tag_key);
3207 int btf_find_next_decl_tag(const struct btf *btf, const struct btf_type *pt,
3208 			   int comp_idx, const char *tag_key, int last_id);
3209 
3210 struct bpf_prog *bpf_prog_by_id(u32 id);
3211 struct bpf_link *bpf_link_by_id(u32 id);
3212 
3213 const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id,
3214 						 const struct bpf_prog *prog);
3215 void bpf_task_storage_free(struct task_struct *task);
3216 void bpf_cgrp_storage_free(struct cgroup *cgroup);
3217 const struct btf_func_model *
3218 bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
3219 			 const struct bpf_insn *insn);
3220 int bpf_get_kfunc_addr(const struct bpf_prog *prog, u32 func_id,
3221 		       u16 btf_fd_idx, u8 **func_addr);
3222 
3223 struct bpf_core_ctx {
3224 	struct bpf_verifier_log *log;
3225 	const struct btf *btf;
3226 };
3227 
3228 bool btf_nested_type_is_trusted(struct bpf_verifier_log *log,
3229 				const struct bpf_reg_state *reg,
3230 				const char *field_name, u32 btf_id, const char *suffix);
3231 
3232 bool btf_type_ids_nocast_alias(struct bpf_verifier_log *log,
3233 			       const struct btf *reg_btf, u32 reg_id,
3234 			       const struct btf *arg_btf, u32 arg_id);
3235 
3236 int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
3237 		   int relo_idx, void *insn);
3238 
3239 static inline bool unprivileged_ebpf_enabled(void)
3240 {
3241 	return !sysctl_unprivileged_bpf_disabled;
3242 }
3243 
3244 /* Not all bpf prog type has the bpf_ctx.
3245  * For the bpf prog type that has initialized the bpf_ctx,
3246  * this function can be used to decide if a kernel function
3247  * is called by a bpf program.
3248  */
3249 static inline bool has_current_bpf_ctx(void)
3250 {
3251 	return !!current->bpf_ctx;
3252 }
3253 
3254 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
3255 
3256 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
3257 		     enum bpf_dynptr_type type, u32 offset, u32 size);
3258 void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr);
3259 void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr);
3260 void bpf_prog_report_arena_violation(bool write, unsigned long addr, unsigned long fault_ip);
3261 
3262 static __always_inline u32
3263 bpf_prog_run_array_sleepable(const struct bpf_prog_array *array,
3264 			     const void *ctx, bpf_prog_run_fn run_prog)
3265 {
3266 	const struct bpf_prog_array_item *item;
3267 	struct bpf_prog *prog;
3268 	struct bpf_run_ctx *old_run_ctx;
3269 	struct bpf_trace_run_ctx run_ctx;
3270 	u32 ret = 1;
3271 
3272 	if (unlikely(!array))
3273 		return ret;
3274 
3275 	migrate_disable();
3276 
3277 	run_ctx.is_uprobe = false;
3278 
3279 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
3280 	item = &array->items[0];
3281 	while ((prog = READ_ONCE(item->prog))) {
3282 		/* Skip dummy_bpf_prog placeholder (len == 0) */
3283 		if (unlikely(!prog->len)) {
3284 			item++;
3285 			continue;
3286 		}
3287 
3288 		if (unlikely(!bpf_prog_get_recursion_context(prog))) {
3289 			bpf_prog_inc_misses_counter(prog);
3290 			bpf_prog_put_recursion_context(prog);
3291 			item++;
3292 			continue;
3293 		}
3294 
3295 		run_ctx.bpf_cookie = item->bpf_cookie;
3296 
3297 		if (!prog->sleepable) {
3298 			guard(rcu)();
3299 			ret &= run_prog(prog, ctx);
3300 		} else {
3301 			ret &= run_prog(prog, ctx);
3302 		}
3303 
3304 		bpf_prog_put_recursion_context(prog);
3305 		item++;
3306 	}
3307 	bpf_reset_run_ctx(old_run_ctx);
3308 	migrate_enable();
3309 	return ret;
3310 }
3311 
3312 #else /* !CONFIG_BPF_SYSCALL */
3313 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
3314 {
3315 	return ERR_PTR(-EOPNOTSUPP);
3316 }
3317 
3318 static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
3319 						     enum bpf_prog_type type,
3320 						     bool attach_drv)
3321 {
3322 	return ERR_PTR(-EOPNOTSUPP);
3323 }
3324 
3325 static inline void bpf_prog_add(struct bpf_prog *prog, int i)
3326 {
3327 }
3328 
3329 static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
3330 {
3331 }
3332 
3333 static inline void bpf_prog_put(struct bpf_prog *prog)
3334 {
3335 }
3336 
3337 static inline void bpf_prog_inc(struct bpf_prog *prog)
3338 {
3339 }
3340 
3341 static inline struct bpf_prog *__must_check
3342 bpf_prog_inc_not_zero(struct bpf_prog *prog)
3343 {
3344 	return ERR_PTR(-EOPNOTSUPP);
3345 }
3346 
3347 static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
3348 				 const struct bpf_link_ops *ops,
3349 				 struct bpf_prog *prog, enum bpf_attach_type attach_type)
3350 {
3351 }
3352 
3353 static inline void bpf_link_init_sleepable(struct bpf_link *link, enum bpf_link_type type,
3354 					   const struct bpf_link_ops *ops, struct bpf_prog *prog,
3355 					   enum bpf_attach_type attach_type, bool sleepable)
3356 {
3357 }
3358 
3359 static inline void bpf_tramp_link_init(struct bpf_tramp_link *link, enum bpf_link_type type,
3360 				       const struct bpf_link_ops *ops, struct bpf_prog *prog,
3361 				       enum bpf_attach_type attach_type, u64 cookie)
3362 {
3363 }
3364 
3365 static inline int bpf_link_prime(struct bpf_link *link,
3366 				 struct bpf_link_primer *primer)
3367 {
3368 	return -EOPNOTSUPP;
3369 }
3370 
3371 static inline int bpf_link_settle(struct bpf_link_primer *primer)
3372 {
3373 	return -EOPNOTSUPP;
3374 }
3375 
3376 static inline void bpf_link_cleanup(struct bpf_link_primer *primer)
3377 {
3378 }
3379 
3380 static inline void bpf_link_inc(struct bpf_link *link)
3381 {
3382 }
3383 
3384 static inline struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
3385 {
3386 	return NULL;
3387 }
3388 
3389 static inline void bpf_link_put(struct bpf_link *link)
3390 {
3391 }
3392 
3393 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
3394 {
3395 	return -EOPNOTSUPP;
3396 }
3397 
3398 static inline bool bpf_token_capable(const struct bpf_token *token, int cap)
3399 {
3400 	return capable(cap) || (cap != CAP_SYS_ADMIN && capable(CAP_SYS_ADMIN));
3401 }
3402 
3403 static inline void bpf_token_inc(struct bpf_token *token)
3404 {
3405 }
3406 
3407 static inline void bpf_token_put(struct bpf_token *token)
3408 {
3409 }
3410 
3411 static inline struct bpf_token *bpf_token_get_from_fd(u32 ufd)
3412 {
3413 	return ERR_PTR(-EOPNOTSUPP);
3414 }
3415 
3416 static inline int bpf_token_get_info_by_fd(struct bpf_token *token,
3417 					   const union bpf_attr *attr,
3418 					   union bpf_attr __user *uattr)
3419 {
3420 	return -EOPNOTSUPP;
3421 }
3422 
3423 static inline void __dev_flush(struct list_head *flush_list)
3424 {
3425 }
3426 
3427 struct xdp_frame;
3428 struct bpf_dtab_netdev;
3429 struct bpf_cpu_map_entry;
3430 
3431 static inline
3432 int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
3433 		    struct net_device *dev_rx)
3434 {
3435 	return 0;
3436 }
3437 
3438 static inline
3439 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
3440 		    struct net_device *dev_rx)
3441 {
3442 	return 0;
3443 }
3444 
3445 static inline
3446 int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
3447 			  struct bpf_map *map, bool exclude_ingress)
3448 {
3449 	return 0;
3450 }
3451 
3452 struct sk_buff;
3453 
3454 static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
3455 					   struct sk_buff *skb,
3456 					   const struct bpf_prog *xdp_prog)
3457 {
3458 	return 0;
3459 }
3460 
3461 static inline
3462 int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
3463 			   const struct bpf_prog *xdp_prog,
3464 			   struct bpf_map *map, bool exclude_ingress)
3465 {
3466 	return 0;
3467 }
3468 
3469 static inline void __cpu_map_flush(struct list_head *flush_list)
3470 {
3471 }
3472 
3473 static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
3474 				  struct xdp_frame *xdpf,
3475 				  struct net_device *dev_rx)
3476 {
3477 	return 0;
3478 }
3479 
3480 static inline int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
3481 					   struct sk_buff *skb)
3482 {
3483 	return -EOPNOTSUPP;
3484 }
3485 
3486 static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
3487 				enum bpf_prog_type type)
3488 {
3489 	return ERR_PTR(-EOPNOTSUPP);
3490 }
3491 
3492 static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
3493 					const union bpf_attr *kattr,
3494 					union bpf_attr __user *uattr)
3495 {
3496 	return -ENOTSUPP;
3497 }
3498 
3499 static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
3500 					const union bpf_attr *kattr,
3501 					union bpf_attr __user *uattr)
3502 {
3503 	return -ENOTSUPP;
3504 }
3505 
3506 static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
3507 					    const union bpf_attr *kattr,
3508 					    union bpf_attr __user *uattr)
3509 {
3510 	return -ENOTSUPP;
3511 }
3512 
3513 static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
3514 						   const union bpf_attr *kattr,
3515 						   union bpf_attr __user *uattr)
3516 {
3517 	return -ENOTSUPP;
3518 }
3519 
3520 static inline int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
3521 					      const union bpf_attr *kattr,
3522 					      union bpf_attr __user *uattr)
3523 {
3524 	return -ENOTSUPP;
3525 }
3526 
3527 static inline void bpf_map_put(struct bpf_map *map)
3528 {
3529 }
3530 
3531 static inline struct bpf_prog *bpf_prog_by_id(u32 id)
3532 {
3533 	return ERR_PTR(-ENOTSUPP);
3534 }
3535 
3536 static inline int btf_struct_access(struct bpf_verifier_log *log,
3537 				    const struct bpf_reg_state *reg,
3538 				    int off, int size, enum bpf_access_type atype,
3539 				    u32 *next_btf_id, enum bpf_type_flag *flag,
3540 				    const char **field_name)
3541 {
3542 	return -EACCES;
3543 }
3544 
3545 static inline const struct bpf_func_proto *
3546 bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
3547 {
3548 	return NULL;
3549 }
3550 
3551 static inline void bpf_task_storage_free(struct task_struct *task)
3552 {
3553 }
3554 
3555 static inline const struct btf_func_model *
3556 bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
3557 			 const struct bpf_insn *insn)
3558 {
3559 	return NULL;
3560 }
3561 
3562 static inline int
3563 bpf_get_kfunc_addr(const struct bpf_prog *prog, u32 func_id,
3564 		   u16 btf_fd_idx, u8 **func_addr)
3565 {
3566 	return -ENOTSUPP;
3567 }
3568 
3569 static inline bool unprivileged_ebpf_enabled(void)
3570 {
3571 	return false;
3572 }
3573 
3574 static inline bool has_current_bpf_ctx(void)
3575 {
3576 	return false;
3577 }
3578 
3579 static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
3580 {
3581 }
3582 
3583 static inline void bpf_cgrp_storage_free(struct cgroup *cgroup)
3584 {
3585 }
3586 
3587 static inline void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
3588 				   enum bpf_dynptr_type type, u32 offset, u32 size)
3589 {
3590 }
3591 
3592 static inline void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr)
3593 {
3594 }
3595 
3596 static inline void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr)
3597 {
3598 }
3599 
3600 static inline void bpf_prog_report_arena_violation(bool write, unsigned long addr,
3601 						   unsigned long fault_ip)
3602 {
3603 }
3604 #endif /* CONFIG_BPF_SYSCALL */
3605 
3606 static inline bool bpf_net_capable(void)
3607 {
3608 	return capable(CAP_NET_ADMIN) || capable(CAP_SYS_ADMIN);
3609 }
3610 
3611 static __always_inline int
3612 bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr)
3613 {
3614 	int ret = -EFAULT;
3615 
3616 	if (IS_ENABLED(CONFIG_BPF_EVENTS))
3617 		ret = copy_from_kernel_nofault(dst, unsafe_ptr, size);
3618 	if (unlikely(ret < 0))
3619 		memset(dst, 0, size);
3620 	return ret;
3621 }
3622 
3623 void __bpf_free_used_btfs(struct btf_mod_pair *used_btfs, u32 len);
3624 
3625 static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
3626 						 enum bpf_prog_type type)
3627 {
3628 	return bpf_prog_get_type_dev(ufd, type, false);
3629 }
3630 
3631 void __bpf_free_used_maps(struct bpf_prog_aux *aux,
3632 			  struct bpf_map **used_maps, u32 len);
3633 
3634 bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
3635 
3636 int bpf_prog_offload_compile(struct bpf_prog *prog);
3637 void bpf_prog_dev_bound_destroy(struct bpf_prog *prog);
3638 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
3639 			       struct bpf_prog *prog);
3640 
3641 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
3642 
3643 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
3644 int bpf_map_offload_update_elem(struct bpf_map *map,
3645 				void *key, void *value, u64 flags);
3646 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
3647 int bpf_map_offload_get_next_key(struct bpf_map *map,
3648 				 void *key, void *next_key);
3649 
3650 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
3651 
3652 struct bpf_offload_dev *
3653 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
3654 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
3655 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
3656 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
3657 				    struct net_device *netdev);
3658 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
3659 				       struct net_device *netdev);
3660 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
3661 
3662 void unpriv_ebpf_notify(int new_state);
3663 
3664 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
3665 int bpf_dev_bound_kfunc_check(struct bpf_verifier_log *log,
3666 			      struct bpf_prog_aux *prog_aux);
3667 void *bpf_dev_bound_resolve_kfunc(struct bpf_prog *prog, u32 func_id);
3668 int bpf_prog_dev_bound_init(struct bpf_prog *prog, union bpf_attr *attr);
3669 int bpf_prog_dev_bound_inherit(struct bpf_prog *new_prog, struct bpf_prog *old_prog);
3670 void bpf_dev_bound_netdev_unregister(struct net_device *dev);
3671 
3672 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
3673 {
3674 	return aux->dev_bound;
3675 }
3676 
3677 static inline bool bpf_prog_is_offloaded(const struct bpf_prog_aux *aux)
3678 {
3679 	return aux->offload_requested;
3680 }
3681 
3682 bool bpf_prog_dev_bound_match(const struct bpf_prog *lhs, const struct bpf_prog *rhs);
3683 
3684 static inline bool bpf_map_is_offloaded(struct bpf_map *map)
3685 {
3686 	return unlikely(map->ops == &bpf_map_offload_ops);
3687 }
3688 
3689 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
3690 void bpf_map_offload_map_free(struct bpf_map *map);
3691 u64 bpf_map_offload_map_mem_usage(const struct bpf_map *map);
3692 int bpf_prog_test_run_syscall(struct bpf_prog *prog,
3693 			      const union bpf_attr *kattr,
3694 			      union bpf_attr __user *uattr);
3695 
3696 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
3697 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype);
3698 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags);
3699 int sock_map_bpf_prog_query(const union bpf_attr *attr,
3700 			    union bpf_attr __user *uattr);
3701 int sock_map_link_create(const union bpf_attr *attr, struct bpf_prog *prog);
3702 
3703 void sock_map_unhash(struct sock *sk);
3704 void sock_map_destroy(struct sock *sk);
3705 void sock_map_close(struct sock *sk, long timeout);
3706 #else
3707 static inline int bpf_dev_bound_kfunc_check(struct bpf_verifier_log *log,
3708 					    struct bpf_prog_aux *prog_aux)
3709 {
3710 	return -EOPNOTSUPP;
3711 }
3712 
3713 static inline void *bpf_dev_bound_resolve_kfunc(struct bpf_prog *prog,
3714 						u32 func_id)
3715 {
3716 	return NULL;
3717 }
3718 
3719 static inline int bpf_prog_dev_bound_init(struct bpf_prog *prog,
3720 					  union bpf_attr *attr)
3721 {
3722 	return -EOPNOTSUPP;
3723 }
3724 
3725 static inline int bpf_prog_dev_bound_inherit(struct bpf_prog *new_prog,
3726 					     struct bpf_prog *old_prog)
3727 {
3728 	return -EOPNOTSUPP;
3729 }
3730 
3731 static inline void bpf_dev_bound_netdev_unregister(struct net_device *dev)
3732 {
3733 }
3734 
3735 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
3736 {
3737 	return false;
3738 }
3739 
3740 static inline bool bpf_prog_is_offloaded(struct bpf_prog_aux *aux)
3741 {
3742 	return false;
3743 }
3744 
3745 static inline bool bpf_prog_dev_bound_match(const struct bpf_prog *lhs, const struct bpf_prog *rhs)
3746 {
3747 	return false;
3748 }
3749 
3750 static inline bool bpf_map_is_offloaded(struct bpf_map *map)
3751 {
3752 	return false;
3753 }
3754 
3755 static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
3756 {
3757 	return ERR_PTR(-EOPNOTSUPP);
3758 }
3759 
3760 static inline void bpf_map_offload_map_free(struct bpf_map *map)
3761 {
3762 }
3763 
3764 static inline u64 bpf_map_offload_map_mem_usage(const struct bpf_map *map)
3765 {
3766 	return 0;
3767 }
3768 
3769 static inline int bpf_prog_test_run_syscall(struct bpf_prog *prog,
3770 					    const union bpf_attr *kattr,
3771 					    union bpf_attr __user *uattr)
3772 {
3773 	return -ENOTSUPP;
3774 }
3775 
3776 #ifdef CONFIG_BPF_SYSCALL
3777 static inline int sock_map_get_from_fd(const union bpf_attr *attr,
3778 				       struct bpf_prog *prog)
3779 {
3780 	return -EINVAL;
3781 }
3782 
3783 static inline int sock_map_prog_detach(const union bpf_attr *attr,
3784 				       enum bpf_prog_type ptype)
3785 {
3786 	return -EOPNOTSUPP;
3787 }
3788 
3789 static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
3790 					   u64 flags)
3791 {
3792 	return -EOPNOTSUPP;
3793 }
3794 
3795 static inline int sock_map_bpf_prog_query(const union bpf_attr *attr,
3796 					  union bpf_attr __user *uattr)
3797 {
3798 	return -EINVAL;
3799 }
3800 
3801 static inline int sock_map_link_create(const union bpf_attr *attr, struct bpf_prog *prog)
3802 {
3803 	return -EOPNOTSUPP;
3804 }
3805 #endif /* CONFIG_BPF_SYSCALL */
3806 #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
3807 
3808 static __always_inline void
3809 bpf_prog_inc_misses_counters(const struct bpf_prog_array *array)
3810 {
3811 	const struct bpf_prog_array_item *item;
3812 	struct bpf_prog *prog;
3813 
3814 	if (unlikely(!array))
3815 		return;
3816 
3817 	item = &array->items[0];
3818 	while ((prog = READ_ONCE(item->prog))) {
3819 		bpf_prog_inc_misses_counter(prog);
3820 		item++;
3821 	}
3822 }
3823 
3824 #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
3825 void bpf_sk_reuseport_detach(struct sock *sk);
3826 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
3827 				       void *value);
3828 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
3829 				       void *value, u64 map_flags);
3830 #else
3831 static inline void bpf_sk_reuseport_detach(struct sock *sk)
3832 {
3833 }
3834 
3835 #ifdef CONFIG_BPF_SYSCALL
3836 static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
3837 						     void *key, void *value)
3838 {
3839 	return -EOPNOTSUPP;
3840 }
3841 
3842 static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
3843 						     void *key, void *value,
3844 						     u64 map_flags)
3845 {
3846 	return -EOPNOTSUPP;
3847 }
3848 #endif /* CONFIG_BPF_SYSCALL */
3849 #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
3850 
3851 #ifdef CONFIG_KEYS
3852 struct bpf_key {
3853 	struct key *key;
3854 	bool has_ref;
3855 };
3856 #endif /* CONFIG_KEYS */
3857 
3858 #if defined(CONFIG_KEYS) && defined(CONFIG_BPF_SYSCALL)
3859 struct bpf_key *bpf_lookup_user_key(s32 serial, u64 flags);
3860 struct bpf_key *bpf_lookup_system_key(u64 id);
3861 void bpf_key_put(struct bpf_key *bkey);
3862 int bpf_verify_pkcs7_signature(const struct bpf_dynptr *data_p,
3863 			       const struct bpf_dynptr *sig_p,
3864 			       struct bpf_key *trusted_keyring);
3865 
3866 static inline s32 bpf_key_serial(const struct bpf_key *key)
3867 {
3868 	return key->has_ref ? key->key->serial : 0;
3869 }
3870 #else
3871 static inline struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags)
3872 {
3873 	return NULL;
3874 }
3875 
3876 static inline struct bpf_key *bpf_lookup_system_key(u64 id)
3877 {
3878 	return NULL;
3879 }
3880 
3881 static inline void bpf_key_put(struct bpf_key *bkey)
3882 {
3883 }
3884 
3885 static inline int bpf_verify_pkcs7_signature(const struct bpf_dynptr *data_p,
3886 					     const struct bpf_dynptr *sig_p,
3887 					     struct bpf_key *trusted_keyring)
3888 {
3889 	return -EOPNOTSUPP;
3890 }
3891 
3892 static inline s32 bpf_key_serial(const struct bpf_key *key)
3893 {
3894 	return 0;
3895 }
3896 #endif /* defined(CONFIG_KEYS) && defined(CONFIG_BPF_SYSCALL) */
3897 
3898 /* verifier prototypes for helper functions called from eBPF programs */
3899 extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
3900 extern const struct bpf_func_proto bpf_map_update_elem_proto;
3901 extern const struct bpf_func_proto bpf_map_delete_elem_proto;
3902 extern const struct bpf_func_proto bpf_map_push_elem_proto;
3903 extern const struct bpf_func_proto bpf_map_pop_elem_proto;
3904 extern const struct bpf_func_proto bpf_map_peek_elem_proto;
3905 extern const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto;
3906 
3907 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
3908 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
3909 extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
3910 extern const struct bpf_func_proto bpf_tail_call_proto;
3911 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
3912 extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
3913 extern const struct bpf_func_proto bpf_ktime_get_tai_ns_proto;
3914 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
3915 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
3916 extern const struct bpf_func_proto bpf_get_current_comm_proto;
3917 extern const struct bpf_func_proto bpf_get_stackid_proto;
3918 extern const struct bpf_func_proto bpf_get_stack_proto;
3919 extern const struct bpf_func_proto bpf_get_stack_sleepable_proto;
3920 extern const struct bpf_func_proto bpf_get_task_stack_proto;
3921 extern const struct bpf_func_proto bpf_get_task_stack_sleepable_proto;
3922 extern const struct bpf_func_proto bpf_get_stackid_proto_pe;
3923 extern const struct bpf_func_proto bpf_get_stack_proto_pe;
3924 extern const struct bpf_func_proto bpf_sock_map_update_proto;
3925 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
3926 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
3927 extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
3928 extern const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto;
3929 extern const struct bpf_func_proto bpf_current_task_under_cgroup_proto;
3930 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
3931 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
3932 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
3933 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
3934 extern const struct bpf_func_proto bpf_spin_lock_proto;
3935 extern const struct bpf_func_proto bpf_spin_unlock_proto;
3936 extern const struct bpf_func_proto bpf_get_local_storage_proto;
3937 extern const struct bpf_func_proto bpf_strtol_proto;
3938 extern const struct bpf_func_proto bpf_strtoul_proto;
3939 extern const struct bpf_func_proto bpf_tcp_sock_proto;
3940 extern const struct bpf_func_proto bpf_jiffies64_proto;
3941 extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
3942 extern const struct bpf_func_proto bpf_event_output_data_proto;
3943 extern const struct bpf_func_proto bpf_ringbuf_output_proto;
3944 extern const struct bpf_func_proto bpf_ringbuf_reserve_proto;
3945 extern const struct bpf_func_proto bpf_ringbuf_submit_proto;
3946 extern const struct bpf_func_proto bpf_ringbuf_discard_proto;
3947 extern const struct bpf_func_proto bpf_ringbuf_query_proto;
3948 extern const struct bpf_func_proto bpf_ringbuf_reserve_dynptr_proto;
3949 extern const struct bpf_func_proto bpf_ringbuf_submit_dynptr_proto;
3950 extern const struct bpf_func_proto bpf_ringbuf_discard_dynptr_proto;
3951 extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
3952 extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
3953 extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
3954 extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
3955 extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
3956 extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;
3957 extern const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto;
3958 extern const struct bpf_func_proto bpf_copy_from_user_proto;
3959 extern const struct bpf_func_proto bpf_snprintf_btf_proto;
3960 extern const struct bpf_func_proto bpf_snprintf_proto;
3961 extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
3962 extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
3963 extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;
3964 extern const struct bpf_func_proto bpf_sock_from_file_proto;
3965 extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;
3966 extern const struct bpf_func_proto bpf_task_storage_get_recur_proto;
3967 extern const struct bpf_func_proto bpf_task_storage_get_proto;
3968 extern const struct bpf_func_proto bpf_task_storage_delete_recur_proto;
3969 extern const struct bpf_func_proto bpf_task_storage_delete_proto;
3970 extern const struct bpf_func_proto bpf_for_each_map_elem_proto;
3971 extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto;
3972 extern const struct bpf_func_proto bpf_sk_setsockopt_proto;
3973 extern const struct bpf_func_proto bpf_sk_getsockopt_proto;
3974 extern const struct bpf_func_proto bpf_sk_setsockopt_nodelay_proto;
3975 extern const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto;
3976 extern const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto;
3977 extern const struct bpf_func_proto bpf_find_vma_proto;
3978 extern const struct bpf_func_proto bpf_loop_proto;
3979 extern const struct bpf_func_proto bpf_copy_from_user_task_proto;
3980 extern const struct bpf_func_proto bpf_set_retval_proto;
3981 extern const struct bpf_func_proto bpf_get_retval_proto;
3982 extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto;
3983 extern const struct bpf_func_proto bpf_cgrp_storage_get_proto;
3984 extern const struct bpf_func_proto bpf_cgrp_storage_delete_proto;
3985 
3986 const struct bpf_func_proto *tracing_prog_func_proto(
3987   enum bpf_func_id func_id, const struct bpf_prog *prog);
3988 
3989 /* Shared helpers among cBPF and eBPF. */
3990 void bpf_user_rnd_init_once(void);
3991 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
3992 u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
3993 
3994 #if defined(CONFIG_NET)
3995 bool bpf_sock_common_is_valid_access(int off, int size,
3996 				     enum bpf_access_type type,
3997 				     struct bpf_insn_access_aux *info);
3998 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
3999 			      struct bpf_insn_access_aux *info);
4000 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
4001 				const struct bpf_insn *si,
4002 				struct bpf_insn *insn_buf,
4003 				struct bpf_prog *prog,
4004 				u32 *target_size);
4005 int bpf_dynptr_from_skb_rdonly(struct __sk_buff *skb, u64 flags,
4006 			       struct bpf_dynptr *ptr);
4007 #else
4008 static inline bool bpf_sock_common_is_valid_access(int off, int size,
4009 						   enum bpf_access_type type,
4010 						   struct bpf_insn_access_aux *info)
4011 {
4012 	return false;
4013 }
4014 static inline bool bpf_sock_is_valid_access(int off, int size,
4015 					    enum bpf_access_type type,
4016 					    struct bpf_insn_access_aux *info)
4017 {
4018 	return false;
4019 }
4020 static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
4021 					      const struct bpf_insn *si,
4022 					      struct bpf_insn *insn_buf,
4023 					      struct bpf_prog *prog,
4024 					      u32 *target_size)
4025 {
4026 	return 0;
4027 }
4028 static inline int bpf_dynptr_from_skb_rdonly(struct __sk_buff *skb, u64 flags,
4029 					     struct bpf_dynptr *ptr)
4030 {
4031 	return -EOPNOTSUPP;
4032 }
4033 #endif
4034 
4035 #ifdef CONFIG_INET
4036 struct sk_reuseport_kern {
4037 	struct sk_buff *skb;
4038 	struct sock *sk;
4039 	struct sock *selected_sk;
4040 	struct sock *migrating_sk;
4041 	void *data_end;
4042 	u32 hash;
4043 	u32 reuseport_id;
4044 	bool bind_inany;
4045 };
4046 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
4047 				  struct bpf_insn_access_aux *info);
4048 
4049 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
4050 				    const struct bpf_insn *si,
4051 				    struct bpf_insn *insn_buf,
4052 				    struct bpf_prog *prog,
4053 				    u32 *target_size);
4054 
4055 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
4056 				  struct bpf_insn_access_aux *info);
4057 
4058 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
4059 				    const struct bpf_insn *si,
4060 				    struct bpf_insn *insn_buf,
4061 				    struct bpf_prog *prog,
4062 				    u32 *target_size);
4063 #else
4064 static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
4065 						enum bpf_access_type type,
4066 						struct bpf_insn_access_aux *info)
4067 {
4068 	return false;
4069 }
4070 
4071 static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
4072 						  const struct bpf_insn *si,
4073 						  struct bpf_insn *insn_buf,
4074 						  struct bpf_prog *prog,
4075 						  u32 *target_size)
4076 {
4077 	return 0;
4078 }
4079 static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
4080 						enum bpf_access_type type,
4081 						struct bpf_insn_access_aux *info)
4082 {
4083 	return false;
4084 }
4085 
4086 static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
4087 						  const struct bpf_insn *si,
4088 						  struct bpf_insn *insn_buf,
4089 						  struct bpf_prog *prog,
4090 						  u32 *target_size)
4091 {
4092 	return 0;
4093 }
4094 #endif /* CONFIG_INET */
4095 
4096 enum bpf_text_poke_type {
4097 	BPF_MOD_NOP,
4098 	BPF_MOD_CALL,
4099 	BPF_MOD_JUMP,
4100 };
4101 
4102 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
4103 		       enum bpf_text_poke_type new_t, void *old_addr,
4104 		       void *new_addr);
4105 
4106 void bpf_arch_poke_desc_update(struct bpf_jit_poke_descriptor *poke,
4107 			       struct bpf_prog *new, struct bpf_prog *old);
4108 
4109 void *bpf_arch_text_copy(void *dst, void *src, size_t len);
4110 int bpf_arch_text_invalidate(void *dst, size_t len);
4111 
4112 struct btf_id_set;
4113 bool btf_id_set_contains(const struct btf_id_set *set, u32 id);
4114 
4115 #define MAX_BPRINTF_VARARGS		12
4116 #define MAX_BPRINTF_BUF			1024
4117 
4118 /* Per-cpu temp buffers used by printf-like helpers to store the bprintf binary
4119  * arguments representation.
4120  */
4121 #define MAX_BPRINTF_BIN_ARGS	512
4122 
4123 struct bpf_bprintf_buffers {
4124 	char bin_args[MAX_BPRINTF_BIN_ARGS];
4125 	char buf[MAX_BPRINTF_BUF];
4126 };
4127 
4128 struct bpf_bprintf_data {
4129 	u32 *bin_args;
4130 	char *buf;
4131 	bool get_bin_args;
4132 	bool get_buf;
4133 };
4134 
4135 int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
4136 			u32 num_args, struct bpf_bprintf_data *data);
4137 void bpf_bprintf_cleanup(struct bpf_bprintf_data *data);
4138 int bpf_try_get_buffers(struct bpf_bprintf_buffers **bufs);
4139 void bpf_put_buffers(void);
4140 
4141 void bpf_prog_stream_init(struct bpf_prog *prog);
4142 void bpf_prog_stream_free(struct bpf_prog *prog);
4143 int bpf_prog_stream_read(struct bpf_prog *prog, enum bpf_stream_id stream_id, void __user *buf, int len);
4144 void bpf_stream_stage_init(struct bpf_stream_stage *ss);
4145 void bpf_stream_stage_free(struct bpf_stream_stage *ss);
4146 __printf(2, 3)
4147 int bpf_stream_stage_printk(struct bpf_stream_stage *ss, const char *fmt, ...);
4148 int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog,
4149 			    enum bpf_stream_id stream_id);
4150 int bpf_stream_stage_dump_stack(struct bpf_stream_stage *ss);
4151 
4152 #define bpf_stream_printk(ss, ...) bpf_stream_stage_printk(&ss, __VA_ARGS__)
4153 #define bpf_stream_dump_stack(ss) bpf_stream_stage_dump_stack(&ss)
4154 
4155 #define bpf_stream_stage(ss, prog, stream_id, expr)            \
4156 	({                                                     \
4157 		bpf_stream_stage_init(&ss);                    \
4158 		(expr);                                        \
4159 		bpf_stream_stage_commit(&ss, prog, stream_id); \
4160 		bpf_stream_stage_free(&ss);                    \
4161 	})
4162 
4163 #ifdef CONFIG_BPF_LSM
4164 void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype);
4165 void bpf_cgroup_atype_put(int cgroup_atype);
4166 #else
4167 static inline void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype) {}
4168 static inline void bpf_cgroup_atype_put(int cgroup_atype) {}
4169 #endif /* CONFIG_BPF_LSM */
4170 
4171 static inline bool type_is_alloc(u32 type)
4172 {
4173 	return type & MEM_ALLOC;
4174 }
4175 
4176 static inline gfp_t bpf_memcg_flags(gfp_t flags)
4177 {
4178 	if (memcg_bpf_enabled())
4179 		return flags | __GFP_ACCOUNT;
4180 	return flags;
4181 }
4182 
4183 static inline bool bpf_is_subprog(const struct bpf_prog *prog)
4184 {
4185 	return prog->aux->func_idx != 0;
4186 }
4187 
4188 const struct bpf_line_info *bpf_find_linfo(const struct bpf_prog *prog, u32 insn_off);
4189 struct bpf_linfo_source {
4190 	const char *file;
4191 	const char *line;
4192 	u32 file_name_off;
4193 	int line_num;
4194 	int line_col;
4195 };
4196 
4197 void bpf_get_linfo_source(struct btf *btf, const struct bpf_line_info *linfo,
4198 			  struct bpf_linfo_source *src);
4199 int bpf_prog_get_file_line(struct bpf_prog *prog, unsigned long ip, const char **filep,
4200 			   const char **linep, int *nump);
4201 struct bpf_prog *bpf_prog_find_from_stack(void);
4202 
4203 int bpf_insn_array_init(struct bpf_map *map, const struct bpf_prog *prog);
4204 int bpf_insn_array_ready(struct bpf_map *map);
4205 void bpf_insn_array_release(struct bpf_map *map);
4206 void bpf_insn_array_adjust(struct bpf_map *map, u32 off, u32 len);
4207 void bpf_insn_array_adjust_after_remove(struct bpf_map *map, u32 off, u32 len);
4208 
4209 #ifdef CONFIG_BPF_SYSCALL
4210 void bpf_prog_update_insn_ptrs(struct bpf_prog *prog, u32 *offsets, void *image);
4211 #else
4212 static inline void
4213 bpf_prog_update_insn_ptrs(struct bpf_prog *prog, u32 *offsets, void *image)
4214 {
4215 }
4216 #endif
4217 
4218 static inline bool bpf_map_is_percpu_map(enum bpf_map_type map_type)
4219 {
4220 	switch (map_type) {
4221 	case BPF_MAP_TYPE_PERCPU_ARRAY:
4222 	case BPF_MAP_TYPE_PERCPU_HASH:
4223 	case BPF_MAP_TYPE_LRU_PERCPU_HASH:
4224 	case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
4225 		return true;
4226 	default:
4227 		return false;
4228 	}
4229 }
4230 
4231 static inline int bpf_map_check_op_flags(struct bpf_map *map, u64 flags, u64 allowed_flags)
4232 {
4233 	u32 cpu;
4234 
4235 	if ((u32)flags & ~allowed_flags)
4236 		return -EINVAL;
4237 
4238 	if ((flags & BPF_F_LOCK) && !btf_record_has_field(map->record, BPF_SPIN_LOCK))
4239 		return -EINVAL;
4240 
4241 	if (!(flags & BPF_F_CPU) && flags >> 32)
4242 		return -EINVAL;
4243 
4244 	if (flags & (BPF_F_CPU | BPF_F_ALL_CPUS)) {
4245 		if (!bpf_map_is_percpu_map(map->map_type))
4246 			return -EINVAL;
4247 		if ((flags & BPF_F_CPU) && (flags & BPF_F_ALL_CPUS))
4248 			return -EINVAL;
4249 
4250 		cpu = flags >> 32;
4251 		if ((flags & BPF_F_CPU) && (cpu >= nr_cpu_ids || !cpu_possible(cpu)))
4252 			return -ERANGE;
4253 	}
4254 
4255 	return 0;
4256 }
4257 
4258 #endif /* _LINUX_BPF_H */
4259