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