xref: /linux-6.15/include/linux/bpf.h (revision 6dcd6d01)
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 
10 #include <linux/workqueue.h>
11 #include <linux/file.h>
12 #include <linux/percpu.h>
13 #include <linux/err.h>
14 #include <linux/rbtree_latch.h>
15 #include <linux/numa.h>
16 #include <linux/mm_types.h>
17 #include <linux/wait.h>
18 #include <linux/refcount.h>
19 #include <linux/mutex.h>
20 #include <linux/module.h>
21 #include <linux/kallsyms.h>
22 #include <linux/capability.h>
23 #include <linux/sched/mm.h>
24 #include <linux/slab.h>
25 #include <linux/percpu-refcount.h>
26 #include <linux/stddef.h>
27 #include <linux/bpfptr.h>
28 #include <linux/btf.h>
29 #include <linux/rcupdate_trace.h>
30 #include <linux/static_call.h>
31 
32 struct bpf_verifier_env;
33 struct bpf_verifier_log;
34 struct perf_event;
35 struct bpf_prog;
36 struct bpf_prog_aux;
37 struct bpf_map;
38 struct sock;
39 struct seq_file;
40 struct btf;
41 struct btf_type;
42 struct exception_table_entry;
43 struct seq_operations;
44 struct bpf_iter_aux_info;
45 struct bpf_local_storage;
46 struct bpf_local_storage_map;
47 struct kobject;
48 struct mem_cgroup;
49 struct module;
50 struct bpf_func_state;
51 struct ftrace_ops;
52 struct cgroup;
53 
54 extern struct idr btf_idr;
55 extern spinlock_t btf_idr_lock;
56 extern struct kobject *btf_kobj;
57 
58 typedef u64 (*bpf_callback_t)(u64, u64, u64, u64, u64);
59 typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,
60 					struct bpf_iter_aux_info *aux);
61 typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
62 typedef unsigned int (*bpf_func_t)(const void *,
63 				   const struct bpf_insn *);
64 struct bpf_iter_seq_info {
65 	const struct seq_operations *seq_ops;
66 	bpf_iter_init_seq_priv_t init_seq_private;
67 	bpf_iter_fini_seq_priv_t fini_seq_private;
68 	u32 seq_priv_size;
69 };
70 
71 /* map is generic key/value storage optionally accessible by eBPF programs */
72 struct bpf_map_ops {
73 	/* funcs callable from userspace (via syscall) */
74 	int (*map_alloc_check)(union bpf_attr *attr);
75 	struct bpf_map *(*map_alloc)(union bpf_attr *attr);
76 	void (*map_release)(struct bpf_map *map, struct file *map_file);
77 	void (*map_free)(struct bpf_map *map);
78 	int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
79 	void (*map_release_uref)(struct bpf_map *map);
80 	void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
81 	int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr,
82 				union bpf_attr __user *uattr);
83 	int (*map_lookup_and_delete_elem)(struct bpf_map *map, void *key,
84 					  void *value, u64 flags);
85 	int (*map_lookup_and_delete_batch)(struct bpf_map *map,
86 					   const union bpf_attr *attr,
87 					   union bpf_attr __user *uattr);
88 	int (*map_update_batch)(struct bpf_map *map, const union bpf_attr *attr,
89 				union bpf_attr __user *uattr);
90 	int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr,
91 				union bpf_attr __user *uattr);
92 
93 	/* funcs callable from userspace and from eBPF programs */
94 	void *(*map_lookup_elem)(struct bpf_map *map, void *key);
95 	int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
96 	int (*map_delete_elem)(struct bpf_map *map, void *key);
97 	int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
98 	int (*map_pop_elem)(struct bpf_map *map, void *value);
99 	int (*map_peek_elem)(struct bpf_map *map, void *value);
100 	void *(*map_lookup_percpu_elem)(struct bpf_map *map, void *key, u32 cpu);
101 
102 	/* funcs called by prog_array and perf_event_array map */
103 	void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
104 				int fd);
105 	void (*map_fd_put_ptr)(void *ptr);
106 	int (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
107 	u32 (*map_fd_sys_lookup_elem)(void *ptr);
108 	void (*map_seq_show_elem)(struct bpf_map *map, void *key,
109 				  struct seq_file *m);
110 	int (*map_check_btf)(const struct bpf_map *map,
111 			     const struct btf *btf,
112 			     const struct btf_type *key_type,
113 			     const struct btf_type *value_type);
114 
115 	/* Prog poke tracking helpers. */
116 	int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux);
117 	void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux);
118 	void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old,
119 			     struct bpf_prog *new);
120 
121 	/* Direct value access helpers. */
122 	int (*map_direct_value_addr)(const struct bpf_map *map,
123 				     u64 *imm, u32 off);
124 	int (*map_direct_value_meta)(const struct bpf_map *map,
125 				     u64 imm, u32 *off);
126 	int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
127 	__poll_t (*map_poll)(struct bpf_map *map, struct file *filp,
128 			     struct poll_table_struct *pts);
129 
130 	/* Functions called by bpf_local_storage maps */
131 	int (*map_local_storage_charge)(struct bpf_local_storage_map *smap,
132 					void *owner, u32 size);
133 	void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap,
134 					   void *owner, u32 size);
135 	struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(void *owner);
136 
137 	/* Misc helpers.*/
138 	int (*map_redirect)(struct bpf_map *map, u32 ifindex, u64 flags);
139 
140 	/* map_meta_equal must be implemented for maps that can be
141 	 * used as an inner map.  It is a runtime check to ensure
142 	 * an inner map can be inserted to an outer map.
143 	 *
144 	 * Some properties of the inner map has been used during the
145 	 * verification time.  When inserting an inner map at the runtime,
146 	 * map_meta_equal has to ensure the inserting map has the same
147 	 * properties that the verifier has used earlier.
148 	 */
149 	bool (*map_meta_equal)(const struct bpf_map *meta0,
150 			       const struct bpf_map *meta1);
151 
152 
153 	int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,
154 					      struct bpf_func_state *caller,
155 					      struct bpf_func_state *callee);
156 	int (*map_for_each_callback)(struct bpf_map *map,
157 				     bpf_callback_t callback_fn,
158 				     void *callback_ctx, u64 flags);
159 
160 	/* BTF id of struct allocated by map_alloc */
161 	int *map_btf_id;
162 
163 	/* bpf_iter info used to open a seq_file */
164 	const struct bpf_iter_seq_info *iter_seq_info;
165 };
166 
167 enum {
168 	/* Support at most 8 pointers in a BTF type */
169 	BTF_FIELDS_MAX	      = 10,
170 	BPF_MAP_OFF_ARR_MAX   = BTF_FIELDS_MAX,
171 };
172 
173 enum btf_field_type {
174 	BPF_SPIN_LOCK  = (1 << 0),
175 	BPF_TIMER      = (1 << 1),
176 	BPF_KPTR_UNREF = (1 << 2),
177 	BPF_KPTR_REF   = (1 << 3),
178 	BPF_KPTR       = BPF_KPTR_UNREF | BPF_KPTR_REF,
179 };
180 
181 struct btf_field_kptr {
182 	struct btf *btf;
183 	struct module *module;
184 	btf_dtor_kfunc_t dtor;
185 	u32 btf_id;
186 };
187 
188 struct btf_field {
189 	u32 offset;
190 	enum btf_field_type type;
191 	union {
192 		struct btf_field_kptr kptr;
193 	};
194 };
195 
196 struct btf_record {
197 	u32 cnt;
198 	u32 field_mask;
199 	int spin_lock_off;
200 	int timer_off;
201 	struct btf_field fields[];
202 };
203 
204 struct btf_field_offs {
205 	u32 cnt;
206 	u32 field_off[BPF_MAP_OFF_ARR_MAX];
207 	u8 field_sz[BPF_MAP_OFF_ARR_MAX];
208 };
209 
210 struct bpf_map {
211 	/* The first two cachelines with read-mostly members of which some
212 	 * are also accessed in fast-path (e.g. ops, max_entries).
213 	 */
214 	const struct bpf_map_ops *ops ____cacheline_aligned;
215 	struct bpf_map *inner_map_meta;
216 #ifdef CONFIG_SECURITY
217 	void *security;
218 #endif
219 	enum bpf_map_type map_type;
220 	u32 key_size;
221 	u32 value_size;
222 	u32 max_entries;
223 	u64 map_extra; /* any per-map-type extra fields */
224 	u32 map_flags;
225 	u32 id;
226 	struct btf_record *record;
227 	int numa_node;
228 	u32 btf_key_type_id;
229 	u32 btf_value_type_id;
230 	u32 btf_vmlinux_value_type_id;
231 	struct btf *btf;
232 #ifdef CONFIG_MEMCG_KMEM
233 	struct obj_cgroup *objcg;
234 #endif
235 	char name[BPF_OBJ_NAME_LEN];
236 	struct btf_field_offs *field_offs;
237 	/* The 3rd and 4th cacheline with misc members to avoid false sharing
238 	 * particularly with refcounting.
239 	 */
240 	atomic64_t refcnt ____cacheline_aligned;
241 	atomic64_t usercnt;
242 	struct work_struct work;
243 	struct mutex freeze_mutex;
244 	atomic64_t writecnt;
245 	/* 'Ownership' of program-containing map is claimed by the first program
246 	 * that is going to use this map or by the first program which FD is
247 	 * stored in the map to make sure that all callers and callees have the
248 	 * same prog type, JITed flag and xdp_has_frags flag.
249 	 */
250 	struct {
251 		spinlock_t lock;
252 		enum bpf_prog_type type;
253 		bool jited;
254 		bool xdp_has_frags;
255 	} owner;
256 	bool bypass_spec_v1;
257 	bool frozen; /* write-once; write-protected by freeze_mutex */
258 };
259 
260 static inline const char *btf_field_type_name(enum btf_field_type type)
261 {
262 	switch (type) {
263 	case BPF_SPIN_LOCK:
264 		return "bpf_spin_lock";
265 	case BPF_TIMER:
266 		return "bpf_timer";
267 	case BPF_KPTR_UNREF:
268 	case BPF_KPTR_REF:
269 		return "kptr";
270 	default:
271 		WARN_ON_ONCE(1);
272 		return "unknown";
273 	}
274 }
275 
276 static inline u32 btf_field_type_size(enum btf_field_type type)
277 {
278 	switch (type) {
279 	case BPF_SPIN_LOCK:
280 		return sizeof(struct bpf_spin_lock);
281 	case BPF_TIMER:
282 		return sizeof(struct bpf_timer);
283 	case BPF_KPTR_UNREF:
284 	case BPF_KPTR_REF:
285 		return sizeof(u64);
286 	default:
287 		WARN_ON_ONCE(1);
288 		return 0;
289 	}
290 }
291 
292 static inline u32 btf_field_type_align(enum btf_field_type type)
293 {
294 	switch (type) {
295 	case BPF_SPIN_LOCK:
296 		return __alignof__(struct bpf_spin_lock);
297 	case BPF_TIMER:
298 		return __alignof__(struct bpf_timer);
299 	case BPF_KPTR_UNREF:
300 	case BPF_KPTR_REF:
301 		return __alignof__(u64);
302 	default:
303 		WARN_ON_ONCE(1);
304 		return 0;
305 	}
306 }
307 
308 static inline bool btf_record_has_field(const struct btf_record *rec, enum btf_field_type type)
309 {
310 	if (IS_ERR_OR_NULL(rec))
311 		return false;
312 	return rec->field_mask & type;
313 }
314 
315 static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
316 {
317 	if (!IS_ERR_OR_NULL(map->record)) {
318 		struct btf_field *fields = map->record->fields;
319 		u32 cnt = map->record->cnt;
320 		int i;
321 
322 		for (i = 0; i < cnt; i++)
323 			memset(dst + fields[i].offset, 0, btf_field_type_size(fields[i].type));
324 	}
325 }
326 
327 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
328  * forced to use 'long' read/writes to try to atomically copy long counters.
329  * Best-effort only.  No barriers here, since it _will_ race with concurrent
330  * updates from BPF programs. Called from bpf syscall and mostly used with
331  * size 8 or 16 bytes, so ask compiler to inline it.
332  */
333 static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
334 {
335 	const long *lsrc = src;
336 	long *ldst = dst;
337 
338 	size /= sizeof(long);
339 	while (size--)
340 		*ldst++ = *lsrc++;
341 }
342 
343 /* copy everything but bpf_spin_lock, bpf_timer, and kptrs. There could be one of each. */
344 static inline void bpf_obj_memcpy(struct btf_field_offs *foffs,
345 				  void *dst, void *src, u32 size,
346 				  bool long_memcpy)
347 {
348 	u32 curr_off = 0;
349 	int i;
350 
351 	if (likely(!foffs)) {
352 		if (long_memcpy)
353 			bpf_long_memcpy(dst, src, round_up(size, 8));
354 		else
355 			memcpy(dst, src, size);
356 		return;
357 	}
358 
359 	for (i = 0; i < foffs->cnt; i++) {
360 		u32 next_off = foffs->field_off[i];
361 		u32 sz = next_off - curr_off;
362 
363 		memcpy(dst + curr_off, src + curr_off, sz);
364 		curr_off = next_off + foffs->field_sz[i];
365 	}
366 	memcpy(dst + curr_off, src + curr_off, size - curr_off);
367 }
368 
369 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
370 {
371 	bpf_obj_memcpy(map->field_offs, dst, src, map->value_size, false);
372 }
373 
374 static inline void copy_map_value_long(struct bpf_map *map, void *dst, void *src)
375 {
376 	bpf_obj_memcpy(map->field_offs, dst, src, map->value_size, true);
377 }
378 
379 static inline void bpf_obj_memzero(struct btf_field_offs *foffs, void *dst, u32 size)
380 {
381 	u32 curr_off = 0;
382 	int i;
383 
384 	if (likely(!foffs)) {
385 		memset(dst, 0, size);
386 		return;
387 	}
388 
389 	for (i = 0; i < foffs->cnt; i++) {
390 		u32 next_off = foffs->field_off[i];
391 		u32 sz = next_off - curr_off;
392 
393 		memset(dst + curr_off, 0, sz);
394 		curr_off = next_off + foffs->field_sz[i];
395 	}
396 	memset(dst + curr_off, 0, size - curr_off);
397 }
398 
399 static inline void zero_map_value(struct bpf_map *map, void *dst)
400 {
401 	bpf_obj_memzero(map->field_offs, dst, map->value_size);
402 }
403 
404 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
405 			   bool lock_src);
406 void bpf_timer_cancel_and_free(void *timer);
407 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
408 
409 struct bpf_offload_dev;
410 struct bpf_offloaded_map;
411 
412 struct bpf_map_dev_ops {
413 	int (*map_get_next_key)(struct bpf_offloaded_map *map,
414 				void *key, void *next_key);
415 	int (*map_lookup_elem)(struct bpf_offloaded_map *map,
416 			       void *key, void *value);
417 	int (*map_update_elem)(struct bpf_offloaded_map *map,
418 			       void *key, void *value, u64 flags);
419 	int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
420 };
421 
422 struct bpf_offloaded_map {
423 	struct bpf_map map;
424 	struct net_device *netdev;
425 	const struct bpf_map_dev_ops *dev_ops;
426 	void *dev_priv;
427 	struct list_head offloads;
428 };
429 
430 static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
431 {
432 	return container_of(map, struct bpf_offloaded_map, map);
433 }
434 
435 static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
436 {
437 	return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
438 }
439 
440 static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
441 {
442 	return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) &&
443 		map->ops->map_seq_show_elem;
444 }
445 
446 int map_check_no_btf(const struct bpf_map *map,
447 		     const struct btf *btf,
448 		     const struct btf_type *key_type,
449 		     const struct btf_type *value_type);
450 
451 bool bpf_map_meta_equal(const struct bpf_map *meta0,
452 			const struct bpf_map *meta1);
453 
454 extern const struct bpf_map_ops bpf_map_offload_ops;
455 
456 /* bpf_type_flag contains a set of flags that are applicable to the values of
457  * arg_type, ret_type and reg_type. For example, a pointer value may be null,
458  * or a memory is read-only. We classify types into two categories: base types
459  * and extended types. Extended types are base types combined with a type flag.
460  *
461  * Currently there are no more than 32 base types in arg_type, ret_type and
462  * reg_types.
463  */
464 #define BPF_BASE_TYPE_BITS	8
465 
466 enum bpf_type_flag {
467 	/* PTR may be NULL. */
468 	PTR_MAYBE_NULL		= BIT(0 + BPF_BASE_TYPE_BITS),
469 
470 	/* MEM is read-only. When applied on bpf_arg, it indicates the arg is
471 	 * compatible with both mutable and immutable memory.
472 	 */
473 	MEM_RDONLY		= BIT(1 + BPF_BASE_TYPE_BITS),
474 
475 	/* MEM was "allocated" from a different helper, and cannot be mixed
476 	 * with regular non-MEM_ALLOC'ed MEM types.
477 	 */
478 	MEM_ALLOC		= BIT(2 + BPF_BASE_TYPE_BITS),
479 
480 	/* MEM is in user address space. */
481 	MEM_USER		= BIT(3 + BPF_BASE_TYPE_BITS),
482 
483 	/* MEM is a percpu memory. MEM_PERCPU tags PTR_TO_BTF_ID. When tagged
484 	 * with MEM_PERCPU, PTR_TO_BTF_ID _cannot_ be directly accessed. In
485 	 * order to drop this tag, it must be passed into bpf_per_cpu_ptr()
486 	 * or bpf_this_cpu_ptr(), which will return the pointer corresponding
487 	 * to the specified cpu.
488 	 */
489 	MEM_PERCPU		= BIT(4 + BPF_BASE_TYPE_BITS),
490 
491 	/* Indicates that the argument will be released. */
492 	OBJ_RELEASE		= BIT(5 + BPF_BASE_TYPE_BITS),
493 
494 	/* PTR is not trusted. This is only used with PTR_TO_BTF_ID, to mark
495 	 * unreferenced and referenced kptr loaded from map value using a load
496 	 * instruction, so that they can only be dereferenced but not escape the
497 	 * BPF program into the kernel (i.e. cannot be passed as arguments to
498 	 * kfunc or bpf helpers).
499 	 */
500 	PTR_UNTRUSTED		= BIT(6 + BPF_BASE_TYPE_BITS),
501 
502 	MEM_UNINIT		= BIT(7 + BPF_BASE_TYPE_BITS),
503 
504 	/* DYNPTR points to memory local to the bpf program. */
505 	DYNPTR_TYPE_LOCAL	= BIT(8 + BPF_BASE_TYPE_BITS),
506 
507 	/* DYNPTR points to a kernel-produced ringbuf record. */
508 	DYNPTR_TYPE_RINGBUF	= BIT(9 + BPF_BASE_TYPE_BITS),
509 
510 	/* Size is known at compile time. */
511 	MEM_FIXED_SIZE		= BIT(10 + BPF_BASE_TYPE_BITS),
512 
513 	__BPF_TYPE_FLAG_MAX,
514 	__BPF_TYPE_LAST_FLAG	= __BPF_TYPE_FLAG_MAX - 1,
515 };
516 
517 #define DYNPTR_TYPE_FLAG_MASK	(DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF)
518 
519 /* Max number of base types. */
520 #define BPF_BASE_TYPE_LIMIT	(1UL << BPF_BASE_TYPE_BITS)
521 
522 /* Max number of all types. */
523 #define BPF_TYPE_LIMIT		(__BPF_TYPE_LAST_FLAG | (__BPF_TYPE_LAST_FLAG - 1))
524 
525 /* function argument constraints */
526 enum bpf_arg_type {
527 	ARG_DONTCARE = 0,	/* unused argument in helper function */
528 
529 	/* the following constraints used to prototype
530 	 * bpf_map_lookup/update/delete_elem() functions
531 	 */
532 	ARG_CONST_MAP_PTR,	/* const argument used as pointer to bpf_map */
533 	ARG_PTR_TO_MAP_KEY,	/* pointer to stack used as map key */
534 	ARG_PTR_TO_MAP_VALUE,	/* pointer to stack used as map value */
535 
536 	/* Used to prototype bpf_memcmp() and other functions that access data
537 	 * on eBPF program stack
538 	 */
539 	ARG_PTR_TO_MEM,		/* pointer to valid memory (stack, packet, map value) */
540 
541 	ARG_CONST_SIZE,		/* number of bytes accessed from memory */
542 	ARG_CONST_SIZE_OR_ZERO,	/* number of bytes accessed from memory or 0 */
543 
544 	ARG_PTR_TO_CTX,		/* pointer to context */
545 	ARG_ANYTHING,		/* any (initialized) argument is ok */
546 	ARG_PTR_TO_SPIN_LOCK,	/* pointer to bpf_spin_lock */
547 	ARG_PTR_TO_SOCK_COMMON,	/* pointer to sock_common */
548 	ARG_PTR_TO_INT,		/* pointer to int */
549 	ARG_PTR_TO_LONG,	/* pointer to long */
550 	ARG_PTR_TO_SOCKET,	/* pointer to bpf_sock (fullsock) */
551 	ARG_PTR_TO_BTF_ID,	/* pointer to in-kernel struct */
552 	ARG_PTR_TO_ALLOC_MEM,	/* pointer to dynamically allocated memory */
553 	ARG_CONST_ALLOC_SIZE_OR_ZERO,	/* number of allocated bytes requested */
554 	ARG_PTR_TO_BTF_ID_SOCK_COMMON,	/* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */
555 	ARG_PTR_TO_PERCPU_BTF_ID,	/* pointer to in-kernel percpu type */
556 	ARG_PTR_TO_FUNC,	/* pointer to a bpf program function */
557 	ARG_PTR_TO_STACK,	/* pointer to stack */
558 	ARG_PTR_TO_CONST_STR,	/* pointer to a null terminated read-only string */
559 	ARG_PTR_TO_TIMER,	/* pointer to bpf_timer */
560 	ARG_PTR_TO_KPTR,	/* pointer to referenced kptr */
561 	ARG_PTR_TO_DYNPTR,      /* pointer to bpf_dynptr. See bpf_type_flag for dynptr type */
562 	__BPF_ARG_TYPE_MAX,
563 
564 	/* Extended arg_types. */
565 	ARG_PTR_TO_MAP_VALUE_OR_NULL	= PTR_MAYBE_NULL | ARG_PTR_TO_MAP_VALUE,
566 	ARG_PTR_TO_MEM_OR_NULL		= PTR_MAYBE_NULL | ARG_PTR_TO_MEM,
567 	ARG_PTR_TO_CTX_OR_NULL		= PTR_MAYBE_NULL | ARG_PTR_TO_CTX,
568 	ARG_PTR_TO_SOCKET_OR_NULL	= PTR_MAYBE_NULL | ARG_PTR_TO_SOCKET,
569 	ARG_PTR_TO_ALLOC_MEM_OR_NULL	= PTR_MAYBE_NULL | ARG_PTR_TO_ALLOC_MEM,
570 	ARG_PTR_TO_STACK_OR_NULL	= PTR_MAYBE_NULL | ARG_PTR_TO_STACK,
571 	ARG_PTR_TO_BTF_ID_OR_NULL	= PTR_MAYBE_NULL | ARG_PTR_TO_BTF_ID,
572 	/* pointer to memory does not need to be initialized, helper function must fill
573 	 * all bytes or clear them in error case.
574 	 */
575 	ARG_PTR_TO_UNINIT_MEM		= MEM_UNINIT | ARG_PTR_TO_MEM,
576 	/* Pointer to valid memory of size known at compile time. */
577 	ARG_PTR_TO_FIXED_SIZE_MEM	= MEM_FIXED_SIZE | ARG_PTR_TO_MEM,
578 
579 	/* This must be the last entry. Its purpose is to ensure the enum is
580 	 * wide enough to hold the higher bits reserved for bpf_type_flag.
581 	 */
582 	__BPF_ARG_TYPE_LIMIT	= BPF_TYPE_LIMIT,
583 };
584 static_assert(__BPF_ARG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
585 
586 /* type of values returned from helper functions */
587 enum bpf_return_type {
588 	RET_INTEGER,			/* function returns integer */
589 	RET_VOID,			/* function doesn't return anything */
590 	RET_PTR_TO_MAP_VALUE,		/* returns a pointer to map elem value */
591 	RET_PTR_TO_SOCKET,		/* returns a pointer to a socket */
592 	RET_PTR_TO_TCP_SOCK,		/* returns a pointer to a tcp_sock */
593 	RET_PTR_TO_SOCK_COMMON,		/* returns a pointer to a sock_common */
594 	RET_PTR_TO_ALLOC_MEM,		/* returns a pointer to dynamically allocated memory */
595 	RET_PTR_TO_MEM_OR_BTF_ID,	/* returns a pointer to a valid memory or a btf_id */
596 	RET_PTR_TO_BTF_ID,		/* returns a pointer to a btf_id */
597 	__BPF_RET_TYPE_MAX,
598 
599 	/* Extended ret_types. */
600 	RET_PTR_TO_MAP_VALUE_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_MAP_VALUE,
601 	RET_PTR_TO_SOCKET_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_SOCKET,
602 	RET_PTR_TO_TCP_SOCK_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_TCP_SOCK,
603 	RET_PTR_TO_SOCK_COMMON_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_SOCK_COMMON,
604 	RET_PTR_TO_ALLOC_MEM_OR_NULL	= PTR_MAYBE_NULL | MEM_ALLOC | RET_PTR_TO_ALLOC_MEM,
605 	RET_PTR_TO_DYNPTR_MEM_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_ALLOC_MEM,
606 	RET_PTR_TO_BTF_ID_OR_NULL	= PTR_MAYBE_NULL | RET_PTR_TO_BTF_ID,
607 
608 	/* This must be the last entry. Its purpose is to ensure the enum is
609 	 * wide enough to hold the higher bits reserved for bpf_type_flag.
610 	 */
611 	__BPF_RET_TYPE_LIMIT	= BPF_TYPE_LIMIT,
612 };
613 static_assert(__BPF_RET_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
614 
615 /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
616  * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
617  * instructions after verifying
618  */
619 struct bpf_func_proto {
620 	u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
621 	bool gpl_only;
622 	bool pkt_access;
623 	enum bpf_return_type ret_type;
624 	union {
625 		struct {
626 			enum bpf_arg_type arg1_type;
627 			enum bpf_arg_type arg2_type;
628 			enum bpf_arg_type arg3_type;
629 			enum bpf_arg_type arg4_type;
630 			enum bpf_arg_type arg5_type;
631 		};
632 		enum bpf_arg_type arg_type[5];
633 	};
634 	union {
635 		struct {
636 			u32 *arg1_btf_id;
637 			u32 *arg2_btf_id;
638 			u32 *arg3_btf_id;
639 			u32 *arg4_btf_id;
640 			u32 *arg5_btf_id;
641 		};
642 		u32 *arg_btf_id[5];
643 		struct {
644 			size_t arg1_size;
645 			size_t arg2_size;
646 			size_t arg3_size;
647 			size_t arg4_size;
648 			size_t arg5_size;
649 		};
650 		size_t arg_size[5];
651 	};
652 	int *ret_btf_id; /* return value btf_id */
653 	bool (*allowed)(const struct bpf_prog *prog);
654 };
655 
656 /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
657  * the first argument to eBPF programs.
658  * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
659  */
660 struct bpf_context;
661 
662 enum bpf_access_type {
663 	BPF_READ = 1,
664 	BPF_WRITE = 2
665 };
666 
667 /* types of values stored in eBPF registers */
668 /* Pointer types represent:
669  * pointer
670  * pointer + imm
671  * pointer + (u16) var
672  * pointer + (u16) var + imm
673  * if (range > 0) then [ptr, ptr + range - off) is safe to access
674  * if (id > 0) means that some 'var' was added
675  * if (off > 0) means that 'imm' was added
676  */
677 enum bpf_reg_type {
678 	NOT_INIT = 0,		 /* nothing was written into register */
679 	SCALAR_VALUE,		 /* reg doesn't contain a valid pointer */
680 	PTR_TO_CTX,		 /* reg points to bpf_context */
681 	CONST_PTR_TO_MAP,	 /* reg points to struct bpf_map */
682 	PTR_TO_MAP_VALUE,	 /* reg points to map element value */
683 	PTR_TO_MAP_KEY,		 /* reg points to a map element key */
684 	PTR_TO_STACK,		 /* reg == frame_pointer + offset */
685 	PTR_TO_PACKET_META,	 /* skb->data - meta_len */
686 	PTR_TO_PACKET,		 /* reg points to skb->data */
687 	PTR_TO_PACKET_END,	 /* skb->data + headlen */
688 	PTR_TO_FLOW_KEYS,	 /* reg points to bpf_flow_keys */
689 	PTR_TO_SOCKET,		 /* reg points to struct bpf_sock */
690 	PTR_TO_SOCK_COMMON,	 /* reg points to sock_common */
691 	PTR_TO_TCP_SOCK,	 /* reg points to struct tcp_sock */
692 	PTR_TO_TP_BUFFER,	 /* reg points to a writable raw tp's buffer */
693 	PTR_TO_XDP_SOCK,	 /* reg points to struct xdp_sock */
694 	/* PTR_TO_BTF_ID points to a kernel struct that does not need
695 	 * to be null checked by the BPF program. This does not imply the
696 	 * pointer is _not_ null and in practice this can easily be a null
697 	 * pointer when reading pointer chains. The assumption is program
698 	 * context will handle null pointer dereference typically via fault
699 	 * handling. The verifier must keep this in mind and can make no
700 	 * assumptions about null or non-null when doing branch analysis.
701 	 * Further, when passed into helpers the helpers can not, without
702 	 * additional context, assume the value is non-null.
703 	 */
704 	PTR_TO_BTF_ID,
705 	/* PTR_TO_BTF_ID_OR_NULL points to a kernel struct that has not
706 	 * been checked for null. Used primarily to inform the verifier
707 	 * an explicit null check is required for this struct.
708 	 */
709 	PTR_TO_MEM,		 /* reg points to valid memory region */
710 	PTR_TO_BUF,		 /* reg points to a read/write buffer */
711 	PTR_TO_FUNC,		 /* reg points to a bpf program function */
712 	PTR_TO_DYNPTR,		 /* reg points to a dynptr */
713 	__BPF_REG_TYPE_MAX,
714 
715 	/* Extended reg_types. */
716 	PTR_TO_MAP_VALUE_OR_NULL	= PTR_MAYBE_NULL | PTR_TO_MAP_VALUE,
717 	PTR_TO_SOCKET_OR_NULL		= PTR_MAYBE_NULL | PTR_TO_SOCKET,
718 	PTR_TO_SOCK_COMMON_OR_NULL	= PTR_MAYBE_NULL | PTR_TO_SOCK_COMMON,
719 	PTR_TO_TCP_SOCK_OR_NULL		= PTR_MAYBE_NULL | PTR_TO_TCP_SOCK,
720 	PTR_TO_BTF_ID_OR_NULL		= PTR_MAYBE_NULL | PTR_TO_BTF_ID,
721 
722 	/* This must be the last entry. Its purpose is to ensure the enum is
723 	 * wide enough to hold the higher bits reserved for bpf_type_flag.
724 	 */
725 	__BPF_REG_TYPE_LIMIT	= BPF_TYPE_LIMIT,
726 };
727 static_assert(__BPF_REG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
728 
729 /* The information passed from prog-specific *_is_valid_access
730  * back to the verifier.
731  */
732 struct bpf_insn_access_aux {
733 	enum bpf_reg_type reg_type;
734 	union {
735 		int ctx_field_size;
736 		struct {
737 			struct btf *btf;
738 			u32 btf_id;
739 		};
740 	};
741 	struct bpf_verifier_log *log; /* for verbose logs */
742 };
743 
744 static inline void
745 bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
746 {
747 	aux->ctx_field_size = size;
748 }
749 
750 static inline bool bpf_pseudo_func(const struct bpf_insn *insn)
751 {
752 	return insn->code == (BPF_LD | BPF_IMM | BPF_DW) &&
753 	       insn->src_reg == BPF_PSEUDO_FUNC;
754 }
755 
756 struct bpf_prog_ops {
757 	int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
758 			union bpf_attr __user *uattr);
759 };
760 
761 struct bpf_verifier_ops {
762 	/* return eBPF function prototype for verification */
763 	const struct bpf_func_proto *
764 	(*get_func_proto)(enum bpf_func_id func_id,
765 			  const struct bpf_prog *prog);
766 
767 	/* return true if 'size' wide access at offset 'off' within bpf_context
768 	 * with 'type' (read or write) is allowed
769 	 */
770 	bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
771 				const struct bpf_prog *prog,
772 				struct bpf_insn_access_aux *info);
773 	int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
774 			    const struct bpf_prog *prog);
775 	int (*gen_ld_abs)(const struct bpf_insn *orig,
776 			  struct bpf_insn *insn_buf);
777 	u32 (*convert_ctx_access)(enum bpf_access_type type,
778 				  const struct bpf_insn *src,
779 				  struct bpf_insn *dst,
780 				  struct bpf_prog *prog, u32 *target_size);
781 	int (*btf_struct_access)(struct bpf_verifier_log *log,
782 				 const struct btf *btf,
783 				 const struct btf_type *t, int off, int size,
784 				 enum bpf_access_type atype,
785 				 u32 *next_btf_id, enum bpf_type_flag *flag);
786 };
787 
788 struct bpf_prog_offload_ops {
789 	/* verifier basic callbacks */
790 	int (*insn_hook)(struct bpf_verifier_env *env,
791 			 int insn_idx, int prev_insn_idx);
792 	int (*finalize)(struct bpf_verifier_env *env);
793 	/* verifier optimization callbacks (called after .finalize) */
794 	int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
795 			    struct bpf_insn *insn);
796 	int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
797 	/* program management callbacks */
798 	int (*prepare)(struct bpf_prog *prog);
799 	int (*translate)(struct bpf_prog *prog);
800 	void (*destroy)(struct bpf_prog *prog);
801 };
802 
803 struct bpf_prog_offload {
804 	struct bpf_prog		*prog;
805 	struct net_device	*netdev;
806 	struct bpf_offload_dev	*offdev;
807 	void			*dev_priv;
808 	struct list_head	offloads;
809 	bool			dev_state;
810 	bool			opt_failed;
811 	void			*jited_image;
812 	u32			jited_len;
813 };
814 
815 enum bpf_cgroup_storage_type {
816 	BPF_CGROUP_STORAGE_SHARED,
817 	BPF_CGROUP_STORAGE_PERCPU,
818 	__BPF_CGROUP_STORAGE_MAX
819 };
820 
821 #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
822 
823 /* The longest tracepoint has 12 args.
824  * See include/trace/bpf_probe.h
825  */
826 #define MAX_BPF_FUNC_ARGS 12
827 
828 /* The maximum number of arguments passed through registers
829  * a single function may have.
830  */
831 #define MAX_BPF_FUNC_REG_ARGS 5
832 
833 /* The argument is a structure. */
834 #define BTF_FMODEL_STRUCT_ARG		BIT(0)
835 
836 struct btf_func_model {
837 	u8 ret_size;
838 	u8 nr_args;
839 	u8 arg_size[MAX_BPF_FUNC_ARGS];
840 	u8 arg_flags[MAX_BPF_FUNC_ARGS];
841 };
842 
843 /* Restore arguments before returning from trampoline to let original function
844  * continue executing. This flag is used for fentry progs when there are no
845  * fexit progs.
846  */
847 #define BPF_TRAMP_F_RESTORE_REGS	BIT(0)
848 /* Call original function after fentry progs, but before fexit progs.
849  * Makes sense for fentry/fexit, normal calls and indirect calls.
850  */
851 #define BPF_TRAMP_F_CALL_ORIG		BIT(1)
852 /* Skip current frame and return to parent.  Makes sense for fentry/fexit
853  * programs only. Should not be used with normal calls and indirect calls.
854  */
855 #define BPF_TRAMP_F_SKIP_FRAME		BIT(2)
856 /* Store IP address of the caller on the trampoline stack,
857  * so it's available for trampoline's programs.
858  */
859 #define BPF_TRAMP_F_IP_ARG		BIT(3)
860 /* Return the return value of fentry prog. Only used by bpf_struct_ops. */
861 #define BPF_TRAMP_F_RET_FENTRY_RET	BIT(4)
862 
863 /* Get original function from stack instead of from provided direct address.
864  * Makes sense for trampolines with fexit or fmod_ret programs.
865  */
866 #define BPF_TRAMP_F_ORIG_STACK		BIT(5)
867 
868 /* This trampoline is on a function with another ftrace_ops with IPMODIFY,
869  * e.g., a live patch. This flag is set and cleared by ftrace call backs,
870  */
871 #define BPF_TRAMP_F_SHARE_IPMODIFY	BIT(6)
872 
873 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
874  * bytes on x86.
875  */
876 #define BPF_MAX_TRAMP_LINKS 38
877 
878 struct bpf_tramp_links {
879 	struct bpf_tramp_link *links[BPF_MAX_TRAMP_LINKS];
880 	int nr_links;
881 };
882 
883 struct bpf_tramp_run_ctx;
884 
885 /* Different use cases for BPF trampoline:
886  * 1. replace nop at the function entry (kprobe equivalent)
887  *    flags = BPF_TRAMP_F_RESTORE_REGS
888  *    fentry = a set of programs to run before returning from trampoline
889  *
890  * 2. replace nop at the function entry (kprobe + kretprobe equivalent)
891  *    flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME
892  *    orig_call = fentry_ip + MCOUNT_INSN_SIZE
893  *    fentry = a set of program to run before calling original function
894  *    fexit = a set of program to run after original function
895  *
896  * 3. replace direct call instruction anywhere in the function body
897  *    or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid)
898  *    With flags = 0
899  *      fentry = a set of programs to run before returning from trampoline
900  *    With flags = BPF_TRAMP_F_CALL_ORIG
901  *      orig_call = original callback addr or direct function addr
902  *      fentry = a set of program to run before calling original function
903  *      fexit = a set of program to run after original function
904  */
905 struct bpf_tramp_image;
906 int arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end,
907 				const struct btf_func_model *m, u32 flags,
908 				struct bpf_tramp_links *tlinks,
909 				void *orig_call);
910 u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog,
911 					     struct bpf_tramp_run_ctx *run_ctx);
912 void notrace __bpf_prog_exit_sleepable_recur(struct bpf_prog *prog, u64 start,
913 					     struct bpf_tramp_run_ctx *run_ctx);
914 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr);
915 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr);
916 typedef u64 (*bpf_trampoline_enter_t)(struct bpf_prog *prog,
917 				      struct bpf_tramp_run_ctx *run_ctx);
918 typedef void (*bpf_trampoline_exit_t)(struct bpf_prog *prog, u64 start,
919 				      struct bpf_tramp_run_ctx *run_ctx);
920 bpf_trampoline_enter_t bpf_trampoline_enter(const struct bpf_prog *prog);
921 bpf_trampoline_exit_t bpf_trampoline_exit(const struct bpf_prog *prog);
922 
923 struct bpf_ksym {
924 	unsigned long		 start;
925 	unsigned long		 end;
926 	char			 name[KSYM_NAME_LEN];
927 	struct list_head	 lnode;
928 	struct latch_tree_node	 tnode;
929 	bool			 prog;
930 };
931 
932 enum bpf_tramp_prog_type {
933 	BPF_TRAMP_FENTRY,
934 	BPF_TRAMP_FEXIT,
935 	BPF_TRAMP_MODIFY_RETURN,
936 	BPF_TRAMP_MAX,
937 	BPF_TRAMP_REPLACE, /* more than MAX */
938 };
939 
940 struct bpf_tramp_image {
941 	void *image;
942 	struct bpf_ksym ksym;
943 	struct percpu_ref pcref;
944 	void *ip_after_call;
945 	void *ip_epilogue;
946 	union {
947 		struct rcu_head rcu;
948 		struct work_struct work;
949 	};
950 };
951 
952 struct bpf_trampoline {
953 	/* hlist for trampoline_table */
954 	struct hlist_node hlist;
955 	struct ftrace_ops *fops;
956 	/* serializes access to fields of this trampoline */
957 	struct mutex mutex;
958 	refcount_t refcnt;
959 	u32 flags;
960 	u64 key;
961 	struct {
962 		struct btf_func_model model;
963 		void *addr;
964 		bool ftrace_managed;
965 	} func;
966 	/* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF
967 	 * program by replacing one of its functions. func.addr is the address
968 	 * of the function it replaced.
969 	 */
970 	struct bpf_prog *extension_prog;
971 	/* list of BPF programs using this trampoline */
972 	struct hlist_head progs_hlist[BPF_TRAMP_MAX];
973 	/* Number of attached programs. A counter per kind. */
974 	int progs_cnt[BPF_TRAMP_MAX];
975 	/* Executable image of trampoline */
976 	struct bpf_tramp_image *cur_image;
977 	u64 selector;
978 	struct module *mod;
979 };
980 
981 struct bpf_attach_target_info {
982 	struct btf_func_model fmodel;
983 	long tgt_addr;
984 	const char *tgt_name;
985 	const struct btf_type *tgt_type;
986 };
987 
988 #define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
989 
990 struct bpf_dispatcher_prog {
991 	struct bpf_prog *prog;
992 	refcount_t users;
993 };
994 
995 struct bpf_dispatcher {
996 	/* dispatcher mutex */
997 	struct mutex mutex;
998 	void *func;
999 	struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX];
1000 	int num_progs;
1001 	void *image;
1002 	void *rw_image;
1003 	u32 image_off;
1004 	struct bpf_ksym ksym;
1005 #ifdef CONFIG_HAVE_STATIC_CALL
1006 	struct static_call_key *sc_key;
1007 	void *sc_tramp;
1008 #endif
1009 };
1010 
1011 static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(
1012 	const void *ctx,
1013 	const struct bpf_insn *insnsi,
1014 	bpf_func_t bpf_func)
1015 {
1016 	return bpf_func(ctx, insnsi);
1017 }
1018 
1019 #ifdef CONFIG_BPF_JIT
1020 int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
1021 int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
1022 struct bpf_trampoline *bpf_trampoline_get(u64 key,
1023 					  struct bpf_attach_target_info *tgt_info);
1024 void bpf_trampoline_put(struct bpf_trampoline *tr);
1025 int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_funcs);
1026 
1027 /*
1028  * When the architecture supports STATIC_CALL replace the bpf_dispatcher_fn
1029  * indirection with a direct call to the bpf program. If the architecture does
1030  * not have STATIC_CALL, avoid a double-indirection.
1031  */
1032 #ifdef CONFIG_HAVE_STATIC_CALL
1033 
1034 #define __BPF_DISPATCHER_SC_INIT(_name)				\
1035 	.sc_key = &STATIC_CALL_KEY(_name),			\
1036 	.sc_tramp = STATIC_CALL_TRAMP_ADDR(_name),
1037 
1038 #define __BPF_DISPATCHER_SC(name)				\
1039 	DEFINE_STATIC_CALL(bpf_dispatcher_##name##_call, bpf_dispatcher_nop_func)
1040 
1041 #define __BPF_DISPATCHER_CALL(name)				\
1042 	static_call(bpf_dispatcher_##name##_call)(ctx, insnsi, bpf_func)
1043 
1044 #define __BPF_DISPATCHER_UPDATE(_d, _new)			\
1045 	__static_call_update((_d)->sc_key, (_d)->sc_tramp, (_new))
1046 
1047 #else
1048 #define __BPF_DISPATCHER_SC_INIT(name)
1049 #define __BPF_DISPATCHER_SC(name)
1050 #define __BPF_DISPATCHER_CALL(name)		bpf_func(ctx, insnsi)
1051 #define __BPF_DISPATCHER_UPDATE(_d, _new)
1052 #endif
1053 
1054 #define BPF_DISPATCHER_INIT(_name) {				\
1055 	.mutex = __MUTEX_INITIALIZER(_name.mutex),		\
1056 	.func = &_name##_func,					\
1057 	.progs = {},						\
1058 	.num_progs = 0,						\
1059 	.image = NULL,						\
1060 	.image_off = 0,						\
1061 	.ksym = {						\
1062 		.name  = #_name,				\
1063 		.lnode = LIST_HEAD_INIT(_name.ksym.lnode),	\
1064 	},							\
1065 	__BPF_DISPATCHER_SC_INIT(_name##_call)			\
1066 }
1067 
1068 #define DEFINE_BPF_DISPATCHER(name)					\
1069 	__BPF_DISPATCHER_SC(name);					\
1070 	noinline __nocfi unsigned int bpf_dispatcher_##name##_func(	\
1071 		const void *ctx,					\
1072 		const struct bpf_insn *insnsi,				\
1073 		bpf_func_t bpf_func)					\
1074 	{								\
1075 		return __BPF_DISPATCHER_CALL(name);			\
1076 	}								\
1077 	EXPORT_SYMBOL(bpf_dispatcher_##name##_func);			\
1078 	struct bpf_dispatcher bpf_dispatcher_##name =			\
1079 		BPF_DISPATCHER_INIT(bpf_dispatcher_##name);
1080 
1081 #define DECLARE_BPF_DISPATCHER(name)					\
1082 	unsigned int bpf_dispatcher_##name##_func(			\
1083 		const void *ctx,					\
1084 		const struct bpf_insn *insnsi,				\
1085 		bpf_func_t bpf_func);					\
1086 	extern struct bpf_dispatcher bpf_dispatcher_##name;
1087 
1088 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func
1089 #define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
1090 void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
1091 				struct bpf_prog *to);
1092 /* Called only from JIT-enabled code, so there's no need for stubs. */
1093 void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
1094 void bpf_image_ksym_del(struct bpf_ksym *ksym);
1095 void bpf_ksym_add(struct bpf_ksym *ksym);
1096 void bpf_ksym_del(struct bpf_ksym *ksym);
1097 int bpf_jit_charge_modmem(u32 size);
1098 void bpf_jit_uncharge_modmem(u32 size);
1099 bool bpf_prog_has_trampoline(const struct bpf_prog *prog);
1100 #else
1101 static inline int bpf_trampoline_link_prog(struct bpf_tramp_link *link,
1102 					   struct bpf_trampoline *tr)
1103 {
1104 	return -ENOTSUPP;
1105 }
1106 static inline int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link,
1107 					     struct bpf_trampoline *tr)
1108 {
1109 	return -ENOTSUPP;
1110 }
1111 static inline struct bpf_trampoline *bpf_trampoline_get(u64 key,
1112 							struct bpf_attach_target_info *tgt_info)
1113 {
1114 	return ERR_PTR(-EOPNOTSUPP);
1115 }
1116 static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
1117 #define DEFINE_BPF_DISPATCHER(name)
1118 #define DECLARE_BPF_DISPATCHER(name)
1119 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nop_func
1120 #define BPF_DISPATCHER_PTR(name) NULL
1121 static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
1122 					      struct bpf_prog *from,
1123 					      struct bpf_prog *to) {}
1124 static inline bool is_bpf_image_address(unsigned long address)
1125 {
1126 	return false;
1127 }
1128 static inline bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
1129 {
1130 	return false;
1131 }
1132 #endif
1133 
1134 struct bpf_func_info_aux {
1135 	u16 linkage;
1136 	bool unreliable;
1137 };
1138 
1139 enum bpf_jit_poke_reason {
1140 	BPF_POKE_REASON_TAIL_CALL,
1141 };
1142 
1143 /* Descriptor of pokes pointing /into/ the JITed image. */
1144 struct bpf_jit_poke_descriptor {
1145 	void *tailcall_target;
1146 	void *tailcall_bypass;
1147 	void *bypass_addr;
1148 	void *aux;
1149 	union {
1150 		struct {
1151 			struct bpf_map *map;
1152 			u32 key;
1153 		} tail_call;
1154 	};
1155 	bool tailcall_target_stable;
1156 	u8 adj_off;
1157 	u16 reason;
1158 	u32 insn_idx;
1159 };
1160 
1161 /* reg_type info for ctx arguments */
1162 struct bpf_ctx_arg_aux {
1163 	u32 offset;
1164 	enum bpf_reg_type reg_type;
1165 	u32 btf_id;
1166 };
1167 
1168 struct btf_mod_pair {
1169 	struct btf *btf;
1170 	struct module *module;
1171 };
1172 
1173 struct bpf_kfunc_desc_tab;
1174 
1175 struct bpf_prog_aux {
1176 	atomic64_t refcnt;
1177 	u32 used_map_cnt;
1178 	u32 used_btf_cnt;
1179 	u32 max_ctx_offset;
1180 	u32 max_pkt_offset;
1181 	u32 max_tp_access;
1182 	u32 stack_depth;
1183 	u32 id;
1184 	u32 func_cnt; /* used by non-func prog as the number of func progs */
1185 	u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
1186 	u32 attach_btf_id; /* in-kernel BTF type id to attach to */
1187 	u32 ctx_arg_info_size;
1188 	u32 max_rdonly_access;
1189 	u32 max_rdwr_access;
1190 	struct btf *attach_btf;
1191 	const struct bpf_ctx_arg_aux *ctx_arg_info;
1192 	struct mutex dst_mutex; /* protects dst_* pointers below, *after* prog becomes visible */
1193 	struct bpf_prog *dst_prog;
1194 	struct bpf_trampoline *dst_trampoline;
1195 	enum bpf_prog_type saved_dst_prog_type;
1196 	enum bpf_attach_type saved_dst_attach_type;
1197 	bool verifier_zext; /* Zero extensions has been inserted by verifier. */
1198 	bool offload_requested;
1199 	bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
1200 	bool func_proto_unreliable;
1201 	bool sleepable;
1202 	bool tail_call_reachable;
1203 	bool xdp_has_frags;
1204 	/* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
1205 	const struct btf_type *attach_func_proto;
1206 	/* function name for valid attach_btf_id */
1207 	const char *attach_func_name;
1208 	struct bpf_prog **func;
1209 	void *jit_data; /* JIT specific data. arch dependent */
1210 	struct bpf_jit_poke_descriptor *poke_tab;
1211 	struct bpf_kfunc_desc_tab *kfunc_tab;
1212 	struct bpf_kfunc_btf_tab *kfunc_btf_tab;
1213 	u32 size_poke_tab;
1214 	struct bpf_ksym ksym;
1215 	const struct bpf_prog_ops *ops;
1216 	struct bpf_map **used_maps;
1217 	struct mutex used_maps_mutex; /* mutex for used_maps and used_map_cnt */
1218 	struct btf_mod_pair *used_btfs;
1219 	struct bpf_prog *prog;
1220 	struct user_struct *user;
1221 	u64 load_time; /* ns since boottime */
1222 	u32 verified_insns;
1223 	int cgroup_atype; /* enum cgroup_bpf_attach_type */
1224 	struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1225 	char name[BPF_OBJ_NAME_LEN];
1226 #ifdef CONFIG_SECURITY
1227 	void *security;
1228 #endif
1229 	struct bpf_prog_offload *offload;
1230 	struct btf *btf;
1231 	struct bpf_func_info *func_info;
1232 	struct bpf_func_info_aux *func_info_aux;
1233 	/* bpf_line_info loaded from userspace.  linfo->insn_off
1234 	 * has the xlated insn offset.
1235 	 * Both the main and sub prog share the same linfo.
1236 	 * The subprog can access its first linfo by
1237 	 * using the linfo_idx.
1238 	 */
1239 	struct bpf_line_info *linfo;
1240 	/* jited_linfo is the jited addr of the linfo.  It has a
1241 	 * one to one mapping to linfo:
1242 	 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
1243 	 * Both the main and sub prog share the same jited_linfo.
1244 	 * The subprog can access its first jited_linfo by
1245 	 * using the linfo_idx.
1246 	 */
1247 	void **jited_linfo;
1248 	u32 func_info_cnt;
1249 	u32 nr_linfo;
1250 	/* subprog can use linfo_idx to access its first linfo and
1251 	 * jited_linfo.
1252 	 * main prog always has linfo_idx == 0
1253 	 */
1254 	u32 linfo_idx;
1255 	u32 num_exentries;
1256 	struct exception_table_entry *extable;
1257 	union {
1258 		struct work_struct work;
1259 		struct rcu_head	rcu;
1260 	};
1261 };
1262 
1263 struct bpf_prog {
1264 	u16			pages;		/* Number of allocated pages */
1265 	u16			jited:1,	/* Is our filter JIT'ed? */
1266 				jit_requested:1,/* archs need to JIT the prog */
1267 				gpl_compatible:1, /* Is filter GPL compatible? */
1268 				cb_access:1,	/* Is control block accessed? */
1269 				dst_needed:1,	/* Do we need dst entry? */
1270 				blinding_requested:1, /* needs constant blinding */
1271 				blinded:1,	/* Was blinded */
1272 				is_func:1,	/* program is a bpf function */
1273 				kprobe_override:1, /* Do we override a kprobe? */
1274 				has_callchain_buf:1, /* callchain buffer allocated? */
1275 				enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */
1276 				call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */
1277 				call_get_func_ip:1, /* Do we call get_func_ip() */
1278 				tstamp_type_access:1; /* Accessed __sk_buff->tstamp_type */
1279 	enum bpf_prog_type	type;		/* Type of BPF program */
1280 	enum bpf_attach_type	expected_attach_type; /* For some prog types */
1281 	u32			len;		/* Number of filter blocks */
1282 	u32			jited_len;	/* Size of jited insns in bytes */
1283 	u8			tag[BPF_TAG_SIZE];
1284 	struct bpf_prog_stats __percpu *stats;
1285 	int __percpu		*active;
1286 	unsigned int		(*bpf_func)(const void *ctx,
1287 					    const struct bpf_insn *insn);
1288 	struct bpf_prog_aux	*aux;		/* Auxiliary fields */
1289 	struct sock_fprog_kern	*orig_prog;	/* Original BPF program */
1290 	/* Instructions for interpreter */
1291 	union {
1292 		DECLARE_FLEX_ARRAY(struct sock_filter, insns);
1293 		DECLARE_FLEX_ARRAY(struct bpf_insn, insnsi);
1294 	};
1295 };
1296 
1297 struct bpf_array_aux {
1298 	/* Programs with direct jumps into programs part of this array. */
1299 	struct list_head poke_progs;
1300 	struct bpf_map *map;
1301 	struct mutex poke_mutex;
1302 	struct work_struct work;
1303 };
1304 
1305 struct bpf_link {
1306 	atomic64_t refcnt;
1307 	u32 id;
1308 	enum bpf_link_type type;
1309 	const struct bpf_link_ops *ops;
1310 	struct bpf_prog *prog;
1311 	struct work_struct work;
1312 };
1313 
1314 struct bpf_link_ops {
1315 	void (*release)(struct bpf_link *link);
1316 	void (*dealloc)(struct bpf_link *link);
1317 	int (*detach)(struct bpf_link *link);
1318 	int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
1319 			   struct bpf_prog *old_prog);
1320 	void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);
1321 	int (*fill_link_info)(const struct bpf_link *link,
1322 			      struct bpf_link_info *info);
1323 };
1324 
1325 struct bpf_tramp_link {
1326 	struct bpf_link link;
1327 	struct hlist_node tramp_hlist;
1328 	u64 cookie;
1329 };
1330 
1331 struct bpf_shim_tramp_link {
1332 	struct bpf_tramp_link link;
1333 	struct bpf_trampoline *trampoline;
1334 };
1335 
1336 struct bpf_tracing_link {
1337 	struct bpf_tramp_link link;
1338 	enum bpf_attach_type attach_type;
1339 	struct bpf_trampoline *trampoline;
1340 	struct bpf_prog *tgt_prog;
1341 };
1342 
1343 struct bpf_link_primer {
1344 	struct bpf_link *link;
1345 	struct file *file;
1346 	int fd;
1347 	u32 id;
1348 };
1349 
1350 struct bpf_struct_ops_value;
1351 struct btf_member;
1352 
1353 #define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
1354 struct bpf_struct_ops {
1355 	const struct bpf_verifier_ops *verifier_ops;
1356 	int (*init)(struct btf *btf);
1357 	int (*check_member)(const struct btf_type *t,
1358 			    const struct btf_member *member);
1359 	int (*init_member)(const struct btf_type *t,
1360 			   const struct btf_member *member,
1361 			   void *kdata, const void *udata);
1362 	int (*reg)(void *kdata);
1363 	void (*unreg)(void *kdata);
1364 	const struct btf_type *type;
1365 	const struct btf_type *value_type;
1366 	const char *name;
1367 	struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
1368 	u32 type_id;
1369 	u32 value_id;
1370 };
1371 
1372 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
1373 #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
1374 const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
1375 void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
1376 bool bpf_struct_ops_get(const void *kdata);
1377 void bpf_struct_ops_put(const void *kdata);
1378 int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
1379 				       void *value);
1380 int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks,
1381 				      struct bpf_tramp_link *link,
1382 				      const struct btf_func_model *model,
1383 				      void *image, void *image_end);
1384 static inline bool bpf_try_module_get(const void *data, struct module *owner)
1385 {
1386 	if (owner == BPF_MODULE_OWNER)
1387 		return bpf_struct_ops_get(data);
1388 	else
1389 		return try_module_get(owner);
1390 }
1391 static inline void bpf_module_put(const void *data, struct module *owner)
1392 {
1393 	if (owner == BPF_MODULE_OWNER)
1394 		bpf_struct_ops_put(data);
1395 	else
1396 		module_put(owner);
1397 }
1398 
1399 #ifdef CONFIG_NET
1400 /* Define it here to avoid the use of forward declaration */
1401 struct bpf_dummy_ops_state {
1402 	int val;
1403 };
1404 
1405 struct bpf_dummy_ops {
1406 	int (*test_1)(struct bpf_dummy_ops_state *cb);
1407 	int (*test_2)(struct bpf_dummy_ops_state *cb, int a1, unsigned short a2,
1408 		      char a3, unsigned long a4);
1409 };
1410 
1411 int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
1412 			    union bpf_attr __user *uattr);
1413 #endif
1414 #else
1415 static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
1416 {
1417 	return NULL;
1418 }
1419 static inline void bpf_struct_ops_init(struct btf *btf,
1420 				       struct bpf_verifier_log *log)
1421 {
1422 }
1423 static inline bool bpf_try_module_get(const void *data, struct module *owner)
1424 {
1425 	return try_module_get(owner);
1426 }
1427 static inline void bpf_module_put(const void *data, struct module *owner)
1428 {
1429 	module_put(owner);
1430 }
1431 static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
1432 						     void *key,
1433 						     void *value)
1434 {
1435 	return -EINVAL;
1436 }
1437 #endif
1438 
1439 #if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM)
1440 int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
1441 				    int cgroup_atype);
1442 void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog);
1443 #else
1444 static inline int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
1445 						  int cgroup_atype)
1446 {
1447 	return -EOPNOTSUPP;
1448 }
1449 static inline void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog)
1450 {
1451 }
1452 #endif
1453 
1454 struct bpf_array {
1455 	struct bpf_map map;
1456 	u32 elem_size;
1457 	u32 index_mask;
1458 	struct bpf_array_aux *aux;
1459 	union {
1460 		char value[0] __aligned(8);
1461 		void *ptrs[0] __aligned(8);
1462 		void __percpu *pptrs[0] __aligned(8);
1463 	};
1464 };
1465 
1466 #define BPF_COMPLEXITY_LIMIT_INSNS      1000000 /* yes. 1M insns */
1467 #define MAX_TAIL_CALL_CNT 33
1468 
1469 /* Maximum number of loops for bpf_loop */
1470 #define BPF_MAX_LOOPS	BIT(23)
1471 
1472 #define BPF_F_ACCESS_MASK	(BPF_F_RDONLY |		\
1473 				 BPF_F_RDONLY_PROG |	\
1474 				 BPF_F_WRONLY |		\
1475 				 BPF_F_WRONLY_PROG)
1476 
1477 #define BPF_MAP_CAN_READ	BIT(0)
1478 #define BPF_MAP_CAN_WRITE	BIT(1)
1479 
1480 /* Maximum number of user-producer ring buffer samples that can be drained in
1481  * a call to bpf_user_ringbuf_drain().
1482  */
1483 #define BPF_MAX_USER_RINGBUF_SAMPLES (128 * 1024)
1484 
1485 static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
1486 {
1487 	u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1488 
1489 	/* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
1490 	 * not possible.
1491 	 */
1492 	if (access_flags & BPF_F_RDONLY_PROG)
1493 		return BPF_MAP_CAN_READ;
1494 	else if (access_flags & BPF_F_WRONLY_PROG)
1495 		return BPF_MAP_CAN_WRITE;
1496 	else
1497 		return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
1498 }
1499 
1500 static inline bool bpf_map_flags_access_ok(u32 access_flags)
1501 {
1502 	return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
1503 	       (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1504 }
1505 
1506 struct bpf_event_entry {
1507 	struct perf_event *event;
1508 	struct file *perf_file;
1509 	struct file *map_file;
1510 	struct rcu_head rcu;
1511 };
1512 
1513 static inline bool map_type_contains_progs(struct bpf_map *map)
1514 {
1515 	return map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
1516 	       map->map_type == BPF_MAP_TYPE_DEVMAP ||
1517 	       map->map_type == BPF_MAP_TYPE_CPUMAP;
1518 }
1519 
1520 bool bpf_prog_map_compatible(struct bpf_map *map, const struct bpf_prog *fp);
1521 int bpf_prog_calc_tag(struct bpf_prog *fp);
1522 
1523 const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
1524 const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void);
1525 
1526 typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
1527 					unsigned long off, unsigned long len);
1528 typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
1529 					const struct bpf_insn *src,
1530 					struct bpf_insn *dst,
1531 					struct bpf_prog *prog,
1532 					u32 *target_size);
1533 
1534 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1535 		     void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
1536 
1537 /* an array of programs to be executed under rcu_lock.
1538  *
1539  * Typical usage:
1540  * ret = bpf_prog_run_array(rcu_dereference(&bpf_prog_array), ctx, bpf_prog_run);
1541  *
1542  * the structure returned by bpf_prog_array_alloc() should be populated
1543  * with program pointers and the last pointer must be NULL.
1544  * The user has to keep refcnt on the program and make sure the program
1545  * is removed from the array before bpf_prog_put().
1546  * The 'struct bpf_prog_array *' should only be replaced with xchg()
1547  * since other cpus are walking the array of pointers in parallel.
1548  */
1549 struct bpf_prog_array_item {
1550 	struct bpf_prog *prog;
1551 	union {
1552 		struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1553 		u64 bpf_cookie;
1554 	};
1555 };
1556 
1557 struct bpf_prog_array {
1558 	struct rcu_head rcu;
1559 	struct bpf_prog_array_item items[];
1560 };
1561 
1562 struct bpf_empty_prog_array {
1563 	struct bpf_prog_array hdr;
1564 	struct bpf_prog *null_prog;
1565 };
1566 
1567 /* to avoid allocating empty bpf_prog_array for cgroups that
1568  * don't have bpf program attached use one global 'bpf_empty_prog_array'
1569  * It will not be modified the caller of bpf_prog_array_alloc()
1570  * (since caller requested prog_cnt == 0)
1571  * that pointer should be 'freed' by bpf_prog_array_free()
1572  */
1573 extern struct bpf_empty_prog_array bpf_empty_prog_array;
1574 
1575 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
1576 void bpf_prog_array_free(struct bpf_prog_array *progs);
1577 /* Use when traversal over the bpf_prog_array uses tasks_trace rcu */
1578 void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs);
1579 int bpf_prog_array_length(struct bpf_prog_array *progs);
1580 bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
1581 int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
1582 				__u32 __user *prog_ids, u32 cnt);
1583 
1584 void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
1585 				struct bpf_prog *old_prog);
1586 int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index);
1587 int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
1588 			     struct bpf_prog *prog);
1589 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
1590 			     u32 *prog_ids, u32 request_cnt,
1591 			     u32 *prog_cnt);
1592 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
1593 			struct bpf_prog *exclude_prog,
1594 			struct bpf_prog *include_prog,
1595 			u64 bpf_cookie,
1596 			struct bpf_prog_array **new_array);
1597 
1598 struct bpf_run_ctx {};
1599 
1600 struct bpf_cg_run_ctx {
1601 	struct bpf_run_ctx run_ctx;
1602 	const struct bpf_prog_array_item *prog_item;
1603 	int retval;
1604 };
1605 
1606 struct bpf_trace_run_ctx {
1607 	struct bpf_run_ctx run_ctx;
1608 	u64 bpf_cookie;
1609 };
1610 
1611 struct bpf_tramp_run_ctx {
1612 	struct bpf_run_ctx run_ctx;
1613 	u64 bpf_cookie;
1614 	struct bpf_run_ctx *saved_run_ctx;
1615 };
1616 
1617 static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
1618 {
1619 	struct bpf_run_ctx *old_ctx = NULL;
1620 
1621 #ifdef CONFIG_BPF_SYSCALL
1622 	old_ctx = current->bpf_ctx;
1623 	current->bpf_ctx = new_ctx;
1624 #endif
1625 	return old_ctx;
1626 }
1627 
1628 static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)
1629 {
1630 #ifdef CONFIG_BPF_SYSCALL
1631 	current->bpf_ctx = old_ctx;
1632 #endif
1633 }
1634 
1635 /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
1636 #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE			(1 << 0)
1637 /* BPF program asks to set CN on the packet. */
1638 #define BPF_RET_SET_CN						(1 << 0)
1639 
1640 typedef u32 (*bpf_prog_run_fn)(const struct bpf_prog *prog, const void *ctx);
1641 
1642 static __always_inline u32
1643 bpf_prog_run_array(const struct bpf_prog_array *array,
1644 		   const void *ctx, bpf_prog_run_fn run_prog)
1645 {
1646 	const struct bpf_prog_array_item *item;
1647 	const struct bpf_prog *prog;
1648 	struct bpf_run_ctx *old_run_ctx;
1649 	struct bpf_trace_run_ctx run_ctx;
1650 	u32 ret = 1;
1651 
1652 	RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held");
1653 
1654 	if (unlikely(!array))
1655 		return ret;
1656 
1657 	migrate_disable();
1658 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
1659 	item = &array->items[0];
1660 	while ((prog = READ_ONCE(item->prog))) {
1661 		run_ctx.bpf_cookie = item->bpf_cookie;
1662 		ret &= run_prog(prog, ctx);
1663 		item++;
1664 	}
1665 	bpf_reset_run_ctx(old_run_ctx);
1666 	migrate_enable();
1667 	return ret;
1668 }
1669 
1670 /* Notes on RCU design for bpf_prog_arrays containing sleepable programs:
1671  *
1672  * We use the tasks_trace rcu flavor read section to protect the bpf_prog_array
1673  * overall. As a result, we must use the bpf_prog_array_free_sleepable
1674  * in order to use the tasks_trace rcu grace period.
1675  *
1676  * When a non-sleepable program is inside the array, we take the rcu read
1677  * section and disable preemption for that program alone, so it can access
1678  * rcu-protected dynamically sized maps.
1679  */
1680 static __always_inline u32
1681 bpf_prog_run_array_sleepable(const struct bpf_prog_array __rcu *array_rcu,
1682 			     const void *ctx, bpf_prog_run_fn run_prog)
1683 {
1684 	const struct bpf_prog_array_item *item;
1685 	const struct bpf_prog *prog;
1686 	const struct bpf_prog_array *array;
1687 	struct bpf_run_ctx *old_run_ctx;
1688 	struct bpf_trace_run_ctx run_ctx;
1689 	u32 ret = 1;
1690 
1691 	might_fault();
1692 
1693 	rcu_read_lock_trace();
1694 	migrate_disable();
1695 
1696 	array = rcu_dereference_check(array_rcu, rcu_read_lock_trace_held());
1697 	if (unlikely(!array))
1698 		goto out;
1699 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
1700 	item = &array->items[0];
1701 	while ((prog = READ_ONCE(item->prog))) {
1702 		if (!prog->aux->sleepable)
1703 			rcu_read_lock();
1704 
1705 		run_ctx.bpf_cookie = item->bpf_cookie;
1706 		ret &= run_prog(prog, ctx);
1707 		item++;
1708 
1709 		if (!prog->aux->sleepable)
1710 			rcu_read_unlock();
1711 	}
1712 	bpf_reset_run_ctx(old_run_ctx);
1713 out:
1714 	migrate_enable();
1715 	rcu_read_unlock_trace();
1716 	return ret;
1717 }
1718 
1719 #ifdef CONFIG_BPF_SYSCALL
1720 DECLARE_PER_CPU(int, bpf_prog_active);
1721 extern struct mutex bpf_stats_enabled_mutex;
1722 
1723 /*
1724  * Block execution of BPF programs attached to instrumentation (perf,
1725  * kprobes, tracepoints) to prevent deadlocks on map operations as any of
1726  * these events can happen inside a region which holds a map bucket lock
1727  * and can deadlock on it.
1728  */
1729 static inline void bpf_disable_instrumentation(void)
1730 {
1731 	migrate_disable();
1732 	this_cpu_inc(bpf_prog_active);
1733 }
1734 
1735 static inline void bpf_enable_instrumentation(void)
1736 {
1737 	this_cpu_dec(bpf_prog_active);
1738 	migrate_enable();
1739 }
1740 
1741 extern const struct file_operations bpf_map_fops;
1742 extern const struct file_operations bpf_prog_fops;
1743 extern const struct file_operations bpf_iter_fops;
1744 
1745 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1746 	extern const struct bpf_prog_ops _name ## _prog_ops; \
1747 	extern const struct bpf_verifier_ops _name ## _verifier_ops;
1748 #define BPF_MAP_TYPE(_id, _ops) \
1749 	extern const struct bpf_map_ops _ops;
1750 #define BPF_LINK_TYPE(_id, _name)
1751 #include <linux/bpf_types.h>
1752 #undef BPF_PROG_TYPE
1753 #undef BPF_MAP_TYPE
1754 #undef BPF_LINK_TYPE
1755 
1756 extern const struct bpf_prog_ops bpf_offload_prog_ops;
1757 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
1758 extern const struct bpf_verifier_ops xdp_analyzer_ops;
1759 
1760 struct bpf_prog *bpf_prog_get(u32 ufd);
1761 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1762 				       bool attach_drv);
1763 void bpf_prog_add(struct bpf_prog *prog, int i);
1764 void bpf_prog_sub(struct bpf_prog *prog, int i);
1765 void bpf_prog_inc(struct bpf_prog *prog);
1766 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
1767 void bpf_prog_put(struct bpf_prog *prog);
1768 
1769 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
1770 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
1771 
1772 struct btf_field *btf_record_find(const struct btf_record *rec,
1773 				  u32 offset, enum btf_field_type type);
1774 void btf_record_free(struct btf_record *rec);
1775 void bpf_map_free_record(struct bpf_map *map);
1776 struct btf_record *btf_record_dup(const struct btf_record *rec);
1777 bool btf_record_equal(const struct btf_record *rec_a, const struct btf_record *rec_b);
1778 void bpf_obj_free_timer(const struct btf_record *rec, void *obj);
1779 void bpf_obj_free_fields(const struct btf_record *rec, void *obj);
1780 
1781 struct bpf_map *bpf_map_get(u32 ufd);
1782 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
1783 struct bpf_map *__bpf_map_get(struct fd f);
1784 void bpf_map_inc(struct bpf_map *map);
1785 void bpf_map_inc_with_uref(struct bpf_map *map);
1786 struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
1787 void bpf_map_put_with_uref(struct bpf_map *map);
1788 void bpf_map_put(struct bpf_map *map);
1789 void *bpf_map_area_alloc(u64 size, int numa_node);
1790 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
1791 void bpf_map_area_free(void *base);
1792 bool bpf_map_write_active(const struct bpf_map *map);
1793 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
1794 int  generic_map_lookup_batch(struct bpf_map *map,
1795 			      const union bpf_attr *attr,
1796 			      union bpf_attr __user *uattr);
1797 int  generic_map_update_batch(struct bpf_map *map,
1798 			      const union bpf_attr *attr,
1799 			      union bpf_attr __user *uattr);
1800 int  generic_map_delete_batch(struct bpf_map *map,
1801 			      const union bpf_attr *attr,
1802 			      union bpf_attr __user *uattr);
1803 struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
1804 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id);
1805 
1806 #ifdef CONFIG_MEMCG_KMEM
1807 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1808 			   int node);
1809 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags);
1810 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
1811 				    size_t align, gfp_t flags);
1812 #else
1813 static inline void *
1814 bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1815 		     int node)
1816 {
1817 	return kmalloc_node(size, flags, node);
1818 }
1819 
1820 static inline void *
1821 bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
1822 {
1823 	return kzalloc(size, flags);
1824 }
1825 
1826 static inline void __percpu *
1827 bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align,
1828 		     gfp_t flags)
1829 {
1830 	return __alloc_percpu_gfp(size, align, flags);
1831 }
1832 #endif
1833 
1834 extern int sysctl_unprivileged_bpf_disabled;
1835 
1836 static inline bool bpf_allow_ptr_leaks(void)
1837 {
1838 	return perfmon_capable();
1839 }
1840 
1841 static inline bool bpf_allow_uninit_stack(void)
1842 {
1843 	return perfmon_capable();
1844 }
1845 
1846 static inline bool bpf_allow_ptr_to_map_access(void)
1847 {
1848 	return perfmon_capable();
1849 }
1850 
1851 static inline bool bpf_bypass_spec_v1(void)
1852 {
1853 	return perfmon_capable();
1854 }
1855 
1856 static inline bool bpf_bypass_spec_v4(void)
1857 {
1858 	return perfmon_capable();
1859 }
1860 
1861 int bpf_map_new_fd(struct bpf_map *map, int flags);
1862 int bpf_prog_new_fd(struct bpf_prog *prog);
1863 
1864 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
1865 		   const struct bpf_link_ops *ops, struct bpf_prog *prog);
1866 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
1867 int bpf_link_settle(struct bpf_link_primer *primer);
1868 void bpf_link_cleanup(struct bpf_link_primer *primer);
1869 void bpf_link_inc(struct bpf_link *link);
1870 void bpf_link_put(struct bpf_link *link);
1871 int bpf_link_new_fd(struct bpf_link *link);
1872 struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
1873 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
1874 struct bpf_link *bpf_link_get_curr_or_next(u32 *id);
1875 
1876 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
1877 int bpf_obj_get_user(const char __user *pathname, int flags);
1878 
1879 #define BPF_ITER_FUNC_PREFIX "bpf_iter_"
1880 #define DEFINE_BPF_ITER_FUNC(target, args...)			\
1881 	extern int bpf_iter_ ## target(args);			\
1882 	int __init bpf_iter_ ## target(args) { return 0; }
1883 
1884 /*
1885  * The task type of iterators.
1886  *
1887  * For BPF task iterators, they can be parameterized with various
1888  * parameters to visit only some of tasks.
1889  *
1890  * BPF_TASK_ITER_ALL (default)
1891  *	Iterate over resources of every task.
1892  *
1893  * BPF_TASK_ITER_TID
1894  *	Iterate over resources of a task/tid.
1895  *
1896  * BPF_TASK_ITER_TGID
1897  *	Iterate over resources of every task of a process / task group.
1898  */
1899 enum bpf_iter_task_type {
1900 	BPF_TASK_ITER_ALL = 0,
1901 	BPF_TASK_ITER_TID,
1902 	BPF_TASK_ITER_TGID,
1903 };
1904 
1905 struct bpf_iter_aux_info {
1906 	/* for map_elem iter */
1907 	struct bpf_map *map;
1908 
1909 	/* for cgroup iter */
1910 	struct {
1911 		struct cgroup *start; /* starting cgroup */
1912 		enum bpf_cgroup_iter_order order;
1913 	} cgroup;
1914 	struct {
1915 		enum bpf_iter_task_type	type;
1916 		u32 pid;
1917 	} task;
1918 };
1919 
1920 typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
1921 					union bpf_iter_link_info *linfo,
1922 					struct bpf_iter_aux_info *aux);
1923 typedef void (*bpf_iter_detach_target_t)(struct bpf_iter_aux_info *aux);
1924 typedef void (*bpf_iter_show_fdinfo_t) (const struct bpf_iter_aux_info *aux,
1925 					struct seq_file *seq);
1926 typedef int (*bpf_iter_fill_link_info_t)(const struct bpf_iter_aux_info *aux,
1927 					 struct bpf_link_info *info);
1928 typedef const struct bpf_func_proto *
1929 (*bpf_iter_get_func_proto_t)(enum bpf_func_id func_id,
1930 			     const struct bpf_prog *prog);
1931 
1932 enum bpf_iter_feature {
1933 	BPF_ITER_RESCHED	= BIT(0),
1934 };
1935 
1936 #define BPF_ITER_CTX_ARG_MAX 2
1937 struct bpf_iter_reg {
1938 	const char *target;
1939 	bpf_iter_attach_target_t attach_target;
1940 	bpf_iter_detach_target_t detach_target;
1941 	bpf_iter_show_fdinfo_t show_fdinfo;
1942 	bpf_iter_fill_link_info_t fill_link_info;
1943 	bpf_iter_get_func_proto_t get_func_proto;
1944 	u32 ctx_arg_info_size;
1945 	u32 feature;
1946 	struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
1947 	const struct bpf_iter_seq_info *seq_info;
1948 };
1949 
1950 struct bpf_iter_meta {
1951 	__bpf_md_ptr(struct seq_file *, seq);
1952 	u64 session_id;
1953 	u64 seq_num;
1954 };
1955 
1956 struct bpf_iter__bpf_map_elem {
1957 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
1958 	__bpf_md_ptr(struct bpf_map *, map);
1959 	__bpf_md_ptr(void *, key);
1960 	__bpf_md_ptr(void *, value);
1961 };
1962 
1963 int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
1964 void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
1965 bool bpf_iter_prog_supported(struct bpf_prog *prog);
1966 const struct bpf_func_proto *
1967 bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
1968 int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_prog *prog);
1969 int bpf_iter_new_fd(struct bpf_link *link);
1970 bool bpf_link_is_iter(struct bpf_link *link);
1971 struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
1972 int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx);
1973 void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
1974 			      struct seq_file *seq);
1975 int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
1976 				struct bpf_link_info *info);
1977 
1978 int map_set_for_each_callback_args(struct bpf_verifier_env *env,
1979 				   struct bpf_func_state *caller,
1980 				   struct bpf_func_state *callee);
1981 
1982 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
1983 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
1984 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1985 			   u64 flags);
1986 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
1987 			    u64 flags);
1988 
1989 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
1990 
1991 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
1992 				 void *key, void *value, u64 map_flags);
1993 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1994 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1995 				void *key, void *value, u64 map_flags);
1996 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1997 
1998 int bpf_get_file_flag(int flags);
1999 int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size,
2000 			     size_t actual_size);
2001 
2002 /* verify correctness of eBPF program */
2003 int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr);
2004 
2005 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
2006 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
2007 #endif
2008 
2009 struct btf *bpf_get_btf_vmlinux(void);
2010 
2011 /* Map specifics */
2012 struct xdp_frame;
2013 struct sk_buff;
2014 struct bpf_dtab_netdev;
2015 struct bpf_cpu_map_entry;
2016 
2017 void __dev_flush(void);
2018 int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
2019 		    struct net_device *dev_rx);
2020 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
2021 		    struct net_device *dev_rx);
2022 int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
2023 			  struct bpf_map *map, bool exclude_ingress);
2024 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
2025 			     struct bpf_prog *xdp_prog);
2026 int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
2027 			   struct bpf_prog *xdp_prog, struct bpf_map *map,
2028 			   bool exclude_ingress);
2029 
2030 void __cpu_map_flush(void);
2031 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf,
2032 		    struct net_device *dev_rx);
2033 int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
2034 			     struct sk_buff *skb);
2035 
2036 /* Return map's numa specified by userspace */
2037 static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
2038 {
2039 	return (attr->map_flags & BPF_F_NUMA_NODE) ?
2040 		attr->numa_node : NUMA_NO_NODE;
2041 }
2042 
2043 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
2044 int array_map_alloc_check(union bpf_attr *attr);
2045 
2046 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
2047 			  union bpf_attr __user *uattr);
2048 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
2049 			  union bpf_attr __user *uattr);
2050 int bpf_prog_test_run_tracing(struct bpf_prog *prog,
2051 			      const union bpf_attr *kattr,
2052 			      union bpf_attr __user *uattr);
2053 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
2054 				     const union bpf_attr *kattr,
2055 				     union bpf_attr __user *uattr);
2056 int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
2057 			     const union bpf_attr *kattr,
2058 			     union bpf_attr __user *uattr);
2059 int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
2060 				const union bpf_attr *kattr,
2061 				union bpf_attr __user *uattr);
2062 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
2063 		    const struct bpf_prog *prog,
2064 		    struct bpf_insn_access_aux *info);
2065 
2066 static inline bool bpf_tracing_ctx_access(int off, int size,
2067 					  enum bpf_access_type type)
2068 {
2069 	if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
2070 		return false;
2071 	if (type != BPF_READ)
2072 		return false;
2073 	if (off % size != 0)
2074 		return false;
2075 	return true;
2076 }
2077 
2078 static inline bool bpf_tracing_btf_ctx_access(int off, int size,
2079 					      enum bpf_access_type type,
2080 					      const struct bpf_prog *prog,
2081 					      struct bpf_insn_access_aux *info)
2082 {
2083 	if (!bpf_tracing_ctx_access(off, size, type))
2084 		return false;
2085 	return btf_ctx_access(off, size, type, prog, info);
2086 }
2087 
2088 int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf,
2089 		      const struct btf_type *t, int off, int size,
2090 		      enum bpf_access_type atype,
2091 		      u32 *next_btf_id, enum bpf_type_flag *flag);
2092 bool btf_struct_ids_match(struct bpf_verifier_log *log,
2093 			  const struct btf *btf, u32 id, int off,
2094 			  const struct btf *need_btf, u32 need_type_id,
2095 			  bool strict);
2096 
2097 int btf_distill_func_proto(struct bpf_verifier_log *log,
2098 			   struct btf *btf,
2099 			   const struct btf_type *func_proto,
2100 			   const char *func_name,
2101 			   struct btf_func_model *m);
2102 
2103 struct bpf_kfunc_arg_meta {
2104 	u64 r0_size;
2105 	bool r0_rdonly;
2106 	int ref_obj_id;
2107 	u32 flags;
2108 };
2109 
2110 struct bpf_reg_state;
2111 int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
2112 				struct bpf_reg_state *regs);
2113 int btf_check_subprog_call(struct bpf_verifier_env *env, int subprog,
2114 			   struct bpf_reg_state *regs);
2115 int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
2116 			      const struct btf *btf, u32 func_id,
2117 			      struct bpf_reg_state *regs,
2118 			      struct bpf_kfunc_arg_meta *meta);
2119 int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
2120 			  struct bpf_reg_state *reg);
2121 int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
2122 			 struct btf *btf, const struct btf_type *t);
2123 
2124 struct bpf_prog *bpf_prog_by_id(u32 id);
2125 struct bpf_link *bpf_link_by_id(u32 id);
2126 
2127 const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
2128 void bpf_task_storage_free(struct task_struct *task);
2129 void bpf_cgrp_storage_free(struct cgroup *cgroup);
2130 bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);
2131 const struct btf_func_model *
2132 bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
2133 			 const struct bpf_insn *insn);
2134 struct bpf_core_ctx {
2135 	struct bpf_verifier_log *log;
2136 	const struct btf *btf;
2137 };
2138 
2139 int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
2140 		   int relo_idx, void *insn);
2141 
2142 static inline bool unprivileged_ebpf_enabled(void)
2143 {
2144 	return !sysctl_unprivileged_bpf_disabled;
2145 }
2146 
2147 /* Not all bpf prog type has the bpf_ctx.
2148  * For the bpf prog type that has initialized the bpf_ctx,
2149  * this function can be used to decide if a kernel function
2150  * is called by a bpf program.
2151  */
2152 static inline bool has_current_bpf_ctx(void)
2153 {
2154 	return !!current->bpf_ctx;
2155 }
2156 
2157 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
2158 #else /* !CONFIG_BPF_SYSCALL */
2159 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
2160 {
2161 	return ERR_PTR(-EOPNOTSUPP);
2162 }
2163 
2164 static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
2165 						     enum bpf_prog_type type,
2166 						     bool attach_drv)
2167 {
2168 	return ERR_PTR(-EOPNOTSUPP);
2169 }
2170 
2171 static inline void bpf_prog_add(struct bpf_prog *prog, int i)
2172 {
2173 }
2174 
2175 static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
2176 {
2177 }
2178 
2179 static inline void bpf_prog_put(struct bpf_prog *prog)
2180 {
2181 }
2182 
2183 static inline void bpf_prog_inc(struct bpf_prog *prog)
2184 {
2185 }
2186 
2187 static inline struct bpf_prog *__must_check
2188 bpf_prog_inc_not_zero(struct bpf_prog *prog)
2189 {
2190 	return ERR_PTR(-EOPNOTSUPP);
2191 }
2192 
2193 static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2194 				 const struct bpf_link_ops *ops,
2195 				 struct bpf_prog *prog)
2196 {
2197 }
2198 
2199 static inline int bpf_link_prime(struct bpf_link *link,
2200 				 struct bpf_link_primer *primer)
2201 {
2202 	return -EOPNOTSUPP;
2203 }
2204 
2205 static inline int bpf_link_settle(struct bpf_link_primer *primer)
2206 {
2207 	return -EOPNOTSUPP;
2208 }
2209 
2210 static inline void bpf_link_cleanup(struct bpf_link_primer *primer)
2211 {
2212 }
2213 
2214 static inline void bpf_link_inc(struct bpf_link *link)
2215 {
2216 }
2217 
2218 static inline void bpf_link_put(struct bpf_link *link)
2219 {
2220 }
2221 
2222 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
2223 {
2224 	return -EOPNOTSUPP;
2225 }
2226 
2227 static inline void __dev_flush(void)
2228 {
2229 }
2230 
2231 struct xdp_frame;
2232 struct bpf_dtab_netdev;
2233 struct bpf_cpu_map_entry;
2234 
2235 static inline
2236 int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
2237 		    struct net_device *dev_rx)
2238 {
2239 	return 0;
2240 }
2241 
2242 static inline
2243 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
2244 		    struct net_device *dev_rx)
2245 {
2246 	return 0;
2247 }
2248 
2249 static inline
2250 int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
2251 			  struct bpf_map *map, bool exclude_ingress)
2252 {
2253 	return 0;
2254 }
2255 
2256 struct sk_buff;
2257 
2258 static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
2259 					   struct sk_buff *skb,
2260 					   struct bpf_prog *xdp_prog)
2261 {
2262 	return 0;
2263 }
2264 
2265 static inline
2266 int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
2267 			   struct bpf_prog *xdp_prog, struct bpf_map *map,
2268 			   bool exclude_ingress)
2269 {
2270 	return 0;
2271 }
2272 
2273 static inline void __cpu_map_flush(void)
2274 {
2275 }
2276 
2277 static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
2278 				  struct xdp_frame *xdpf,
2279 				  struct net_device *dev_rx)
2280 {
2281 	return 0;
2282 }
2283 
2284 static inline int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
2285 					   struct sk_buff *skb)
2286 {
2287 	return -EOPNOTSUPP;
2288 }
2289 
2290 static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
2291 				enum bpf_prog_type type)
2292 {
2293 	return ERR_PTR(-EOPNOTSUPP);
2294 }
2295 
2296 static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
2297 					const union bpf_attr *kattr,
2298 					union bpf_attr __user *uattr)
2299 {
2300 	return -ENOTSUPP;
2301 }
2302 
2303 static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
2304 					const union bpf_attr *kattr,
2305 					union bpf_attr __user *uattr)
2306 {
2307 	return -ENOTSUPP;
2308 }
2309 
2310 static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
2311 					    const union bpf_attr *kattr,
2312 					    union bpf_attr __user *uattr)
2313 {
2314 	return -ENOTSUPP;
2315 }
2316 
2317 static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
2318 						   const union bpf_attr *kattr,
2319 						   union bpf_attr __user *uattr)
2320 {
2321 	return -ENOTSUPP;
2322 }
2323 
2324 static inline int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
2325 					      const union bpf_attr *kattr,
2326 					      union bpf_attr __user *uattr)
2327 {
2328 	return -ENOTSUPP;
2329 }
2330 
2331 static inline void bpf_map_put(struct bpf_map *map)
2332 {
2333 }
2334 
2335 static inline struct bpf_prog *bpf_prog_by_id(u32 id)
2336 {
2337 	return ERR_PTR(-ENOTSUPP);
2338 }
2339 
2340 static inline int btf_struct_access(struct bpf_verifier_log *log,
2341 				    const struct btf *btf,
2342 				    const struct btf_type *t, int off, int size,
2343 				    enum bpf_access_type atype,
2344 				    u32 *next_btf_id, enum bpf_type_flag *flag)
2345 {
2346 	return -EACCES;
2347 }
2348 
2349 static inline const struct bpf_func_proto *
2350 bpf_base_func_proto(enum bpf_func_id func_id)
2351 {
2352 	return NULL;
2353 }
2354 
2355 static inline void bpf_task_storage_free(struct task_struct *task)
2356 {
2357 }
2358 
2359 static inline bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog)
2360 {
2361 	return false;
2362 }
2363 
2364 static inline const struct btf_func_model *
2365 bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
2366 			 const struct bpf_insn *insn)
2367 {
2368 	return NULL;
2369 }
2370 
2371 static inline bool unprivileged_ebpf_enabled(void)
2372 {
2373 	return false;
2374 }
2375 
2376 static inline bool has_current_bpf_ctx(void)
2377 {
2378 	return false;
2379 }
2380 
2381 static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
2382 {
2383 }
2384 
2385 static inline void bpf_cgrp_storage_free(struct cgroup *cgroup)
2386 {
2387 }
2388 #endif /* CONFIG_BPF_SYSCALL */
2389 
2390 void __bpf_free_used_btfs(struct bpf_prog_aux *aux,
2391 			  struct btf_mod_pair *used_btfs, u32 len);
2392 
2393 static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
2394 						 enum bpf_prog_type type)
2395 {
2396 	return bpf_prog_get_type_dev(ufd, type, false);
2397 }
2398 
2399 void __bpf_free_used_maps(struct bpf_prog_aux *aux,
2400 			  struct bpf_map **used_maps, u32 len);
2401 
2402 bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
2403 
2404 int bpf_prog_offload_compile(struct bpf_prog *prog);
2405 void bpf_prog_offload_destroy(struct bpf_prog *prog);
2406 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
2407 			       struct bpf_prog *prog);
2408 
2409 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
2410 
2411 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
2412 int bpf_map_offload_update_elem(struct bpf_map *map,
2413 				void *key, void *value, u64 flags);
2414 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
2415 int bpf_map_offload_get_next_key(struct bpf_map *map,
2416 				 void *key, void *next_key);
2417 
2418 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
2419 
2420 struct bpf_offload_dev *
2421 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
2422 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
2423 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
2424 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
2425 				    struct net_device *netdev);
2426 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
2427 				       struct net_device *netdev);
2428 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
2429 
2430 void unpriv_ebpf_notify(int new_state);
2431 
2432 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
2433 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
2434 
2435 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
2436 {
2437 	return aux->offload_requested;
2438 }
2439 
2440 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
2441 {
2442 	return unlikely(map->ops == &bpf_map_offload_ops);
2443 }
2444 
2445 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
2446 void bpf_map_offload_map_free(struct bpf_map *map);
2447 int bpf_prog_test_run_syscall(struct bpf_prog *prog,
2448 			      const union bpf_attr *kattr,
2449 			      union bpf_attr __user *uattr);
2450 
2451 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
2452 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype);
2453 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags);
2454 int sock_map_bpf_prog_query(const union bpf_attr *attr,
2455 			    union bpf_attr __user *uattr);
2456 
2457 void sock_map_unhash(struct sock *sk);
2458 void sock_map_destroy(struct sock *sk);
2459 void sock_map_close(struct sock *sk, long timeout);
2460 #else
2461 static inline int bpf_prog_offload_init(struct bpf_prog *prog,
2462 					union bpf_attr *attr)
2463 {
2464 	return -EOPNOTSUPP;
2465 }
2466 
2467 static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
2468 {
2469 	return false;
2470 }
2471 
2472 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
2473 {
2474 	return false;
2475 }
2476 
2477 static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
2478 {
2479 	return ERR_PTR(-EOPNOTSUPP);
2480 }
2481 
2482 static inline void bpf_map_offload_map_free(struct bpf_map *map)
2483 {
2484 }
2485 
2486 static inline int bpf_prog_test_run_syscall(struct bpf_prog *prog,
2487 					    const union bpf_attr *kattr,
2488 					    union bpf_attr __user *uattr)
2489 {
2490 	return -ENOTSUPP;
2491 }
2492 
2493 #ifdef CONFIG_BPF_SYSCALL
2494 static inline int sock_map_get_from_fd(const union bpf_attr *attr,
2495 				       struct bpf_prog *prog)
2496 {
2497 	return -EINVAL;
2498 }
2499 
2500 static inline int sock_map_prog_detach(const union bpf_attr *attr,
2501 				       enum bpf_prog_type ptype)
2502 {
2503 	return -EOPNOTSUPP;
2504 }
2505 
2506 static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
2507 					   u64 flags)
2508 {
2509 	return -EOPNOTSUPP;
2510 }
2511 
2512 static inline int sock_map_bpf_prog_query(const union bpf_attr *attr,
2513 					  union bpf_attr __user *uattr)
2514 {
2515 	return -EINVAL;
2516 }
2517 #endif /* CONFIG_BPF_SYSCALL */
2518 #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
2519 
2520 #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
2521 void bpf_sk_reuseport_detach(struct sock *sk);
2522 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
2523 				       void *value);
2524 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
2525 				       void *value, u64 map_flags);
2526 #else
2527 static inline void bpf_sk_reuseport_detach(struct sock *sk)
2528 {
2529 }
2530 
2531 #ifdef CONFIG_BPF_SYSCALL
2532 static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
2533 						     void *key, void *value)
2534 {
2535 	return -EOPNOTSUPP;
2536 }
2537 
2538 static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
2539 						     void *key, void *value,
2540 						     u64 map_flags)
2541 {
2542 	return -EOPNOTSUPP;
2543 }
2544 #endif /* CONFIG_BPF_SYSCALL */
2545 #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
2546 
2547 /* verifier prototypes for helper functions called from eBPF programs */
2548 extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
2549 extern const struct bpf_func_proto bpf_map_update_elem_proto;
2550 extern const struct bpf_func_proto bpf_map_delete_elem_proto;
2551 extern const struct bpf_func_proto bpf_map_push_elem_proto;
2552 extern const struct bpf_func_proto bpf_map_pop_elem_proto;
2553 extern const struct bpf_func_proto bpf_map_peek_elem_proto;
2554 extern const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto;
2555 
2556 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
2557 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
2558 extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
2559 extern const struct bpf_func_proto bpf_tail_call_proto;
2560 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
2561 extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
2562 extern const struct bpf_func_proto bpf_ktime_get_tai_ns_proto;
2563 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
2564 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
2565 extern const struct bpf_func_proto bpf_get_current_comm_proto;
2566 extern const struct bpf_func_proto bpf_get_stackid_proto;
2567 extern const struct bpf_func_proto bpf_get_stack_proto;
2568 extern const struct bpf_func_proto bpf_get_task_stack_proto;
2569 extern const struct bpf_func_proto bpf_get_stackid_proto_pe;
2570 extern const struct bpf_func_proto bpf_get_stack_proto_pe;
2571 extern const struct bpf_func_proto bpf_sock_map_update_proto;
2572 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
2573 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
2574 extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
2575 extern const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto;
2576 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
2577 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
2578 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
2579 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
2580 extern const struct bpf_func_proto bpf_spin_lock_proto;
2581 extern const struct bpf_func_proto bpf_spin_unlock_proto;
2582 extern const struct bpf_func_proto bpf_get_local_storage_proto;
2583 extern const struct bpf_func_proto bpf_strtol_proto;
2584 extern const struct bpf_func_proto bpf_strtoul_proto;
2585 extern const struct bpf_func_proto bpf_tcp_sock_proto;
2586 extern const struct bpf_func_proto bpf_jiffies64_proto;
2587 extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
2588 extern const struct bpf_func_proto bpf_event_output_data_proto;
2589 extern const struct bpf_func_proto bpf_ringbuf_output_proto;
2590 extern const struct bpf_func_proto bpf_ringbuf_reserve_proto;
2591 extern const struct bpf_func_proto bpf_ringbuf_submit_proto;
2592 extern const struct bpf_func_proto bpf_ringbuf_discard_proto;
2593 extern const struct bpf_func_proto bpf_ringbuf_query_proto;
2594 extern const struct bpf_func_proto bpf_ringbuf_reserve_dynptr_proto;
2595 extern const struct bpf_func_proto bpf_ringbuf_submit_dynptr_proto;
2596 extern const struct bpf_func_proto bpf_ringbuf_discard_dynptr_proto;
2597 extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
2598 extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
2599 extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
2600 extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
2601 extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
2602 extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;
2603 extern const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto;
2604 extern const struct bpf_func_proto bpf_copy_from_user_proto;
2605 extern const struct bpf_func_proto bpf_snprintf_btf_proto;
2606 extern const struct bpf_func_proto bpf_snprintf_proto;
2607 extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
2608 extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
2609 extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;
2610 extern const struct bpf_func_proto bpf_sock_from_file_proto;
2611 extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;
2612 extern const struct bpf_func_proto bpf_task_storage_get_recur_proto;
2613 extern const struct bpf_func_proto bpf_task_storage_get_proto;
2614 extern const struct bpf_func_proto bpf_task_storage_delete_recur_proto;
2615 extern const struct bpf_func_proto bpf_task_storage_delete_proto;
2616 extern const struct bpf_func_proto bpf_for_each_map_elem_proto;
2617 extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto;
2618 extern const struct bpf_func_proto bpf_sk_setsockopt_proto;
2619 extern const struct bpf_func_proto bpf_sk_getsockopt_proto;
2620 extern const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto;
2621 extern const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto;
2622 extern const struct bpf_func_proto bpf_find_vma_proto;
2623 extern const struct bpf_func_proto bpf_loop_proto;
2624 extern const struct bpf_func_proto bpf_copy_from_user_task_proto;
2625 extern const struct bpf_func_proto bpf_set_retval_proto;
2626 extern const struct bpf_func_proto bpf_get_retval_proto;
2627 extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto;
2628 extern const struct bpf_func_proto bpf_cgrp_storage_get_proto;
2629 extern const struct bpf_func_proto bpf_cgrp_storage_delete_proto;
2630 
2631 const struct bpf_func_proto *tracing_prog_func_proto(
2632   enum bpf_func_id func_id, const struct bpf_prog *prog);
2633 
2634 /* Shared helpers among cBPF and eBPF. */
2635 void bpf_user_rnd_init_once(void);
2636 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
2637 u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
2638 
2639 #if defined(CONFIG_NET)
2640 bool bpf_sock_common_is_valid_access(int off, int size,
2641 				     enum bpf_access_type type,
2642 				     struct bpf_insn_access_aux *info);
2643 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2644 			      struct bpf_insn_access_aux *info);
2645 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
2646 				const struct bpf_insn *si,
2647 				struct bpf_insn *insn_buf,
2648 				struct bpf_prog *prog,
2649 				u32 *target_size);
2650 #else
2651 static inline bool bpf_sock_common_is_valid_access(int off, int size,
2652 						   enum bpf_access_type type,
2653 						   struct bpf_insn_access_aux *info)
2654 {
2655 	return false;
2656 }
2657 static inline bool bpf_sock_is_valid_access(int off, int size,
2658 					    enum bpf_access_type type,
2659 					    struct bpf_insn_access_aux *info)
2660 {
2661 	return false;
2662 }
2663 static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
2664 					      const struct bpf_insn *si,
2665 					      struct bpf_insn *insn_buf,
2666 					      struct bpf_prog *prog,
2667 					      u32 *target_size)
2668 {
2669 	return 0;
2670 }
2671 #endif
2672 
2673 #ifdef CONFIG_INET
2674 struct sk_reuseport_kern {
2675 	struct sk_buff *skb;
2676 	struct sock *sk;
2677 	struct sock *selected_sk;
2678 	struct sock *migrating_sk;
2679 	void *data_end;
2680 	u32 hash;
2681 	u32 reuseport_id;
2682 	bool bind_inany;
2683 };
2684 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2685 				  struct bpf_insn_access_aux *info);
2686 
2687 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
2688 				    const struct bpf_insn *si,
2689 				    struct bpf_insn *insn_buf,
2690 				    struct bpf_prog *prog,
2691 				    u32 *target_size);
2692 
2693 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2694 				  struct bpf_insn_access_aux *info);
2695 
2696 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2697 				    const struct bpf_insn *si,
2698 				    struct bpf_insn *insn_buf,
2699 				    struct bpf_prog *prog,
2700 				    u32 *target_size);
2701 #else
2702 static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
2703 						enum bpf_access_type type,
2704 						struct bpf_insn_access_aux *info)
2705 {
2706 	return false;
2707 }
2708 
2709 static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
2710 						  const struct bpf_insn *si,
2711 						  struct bpf_insn *insn_buf,
2712 						  struct bpf_prog *prog,
2713 						  u32 *target_size)
2714 {
2715 	return 0;
2716 }
2717 static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
2718 						enum bpf_access_type type,
2719 						struct bpf_insn_access_aux *info)
2720 {
2721 	return false;
2722 }
2723 
2724 static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2725 						  const struct bpf_insn *si,
2726 						  struct bpf_insn *insn_buf,
2727 						  struct bpf_prog *prog,
2728 						  u32 *target_size)
2729 {
2730 	return 0;
2731 }
2732 #endif /* CONFIG_INET */
2733 
2734 enum bpf_text_poke_type {
2735 	BPF_MOD_CALL,
2736 	BPF_MOD_JUMP,
2737 };
2738 
2739 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
2740 		       void *addr1, void *addr2);
2741 
2742 void *bpf_arch_text_copy(void *dst, void *src, size_t len);
2743 int bpf_arch_text_invalidate(void *dst, size_t len);
2744 
2745 struct btf_id_set;
2746 bool btf_id_set_contains(const struct btf_id_set *set, u32 id);
2747 
2748 #define MAX_BPRINTF_VARARGS		12
2749 
2750 int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
2751 			u32 **bin_buf, u32 num_args);
2752 void bpf_bprintf_cleanup(void);
2753 
2754 /* the implementation of the opaque uapi struct bpf_dynptr */
2755 struct bpf_dynptr_kern {
2756 	void *data;
2757 	/* Size represents the number of usable bytes of dynptr data.
2758 	 * If for example the offset is at 4 for a local dynptr whose data is
2759 	 * of type u64, the number of usable bytes is 4.
2760 	 *
2761 	 * The upper 8 bits are reserved. It is as follows:
2762 	 * Bits 0 - 23 = size
2763 	 * Bits 24 - 30 = dynptr type
2764 	 * Bit 31 = whether dynptr is read-only
2765 	 */
2766 	u32 size;
2767 	u32 offset;
2768 } __aligned(8);
2769 
2770 enum bpf_dynptr_type {
2771 	BPF_DYNPTR_TYPE_INVALID,
2772 	/* Points to memory that is local to the bpf program */
2773 	BPF_DYNPTR_TYPE_LOCAL,
2774 	/* Underlying data is a kernel-produced ringbuf record */
2775 	BPF_DYNPTR_TYPE_RINGBUF,
2776 };
2777 
2778 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
2779 		     enum bpf_dynptr_type type, u32 offset, u32 size);
2780 void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr);
2781 int bpf_dynptr_check_size(u32 size);
2782 u32 bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr);
2783 
2784 #ifdef CONFIG_BPF_LSM
2785 void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype);
2786 void bpf_cgroup_atype_put(int cgroup_atype);
2787 #else
2788 static inline void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype) {}
2789 static inline void bpf_cgroup_atype_put(int cgroup_atype) {}
2790 #endif /* CONFIG_BPF_LSM */
2791 
2792 struct key;
2793 
2794 #ifdef CONFIG_KEYS
2795 struct bpf_key {
2796 	struct key *key;
2797 	bool has_ref;
2798 };
2799 #endif /* CONFIG_KEYS */
2800 #endif /* _LINUX_BPF_H */
2801