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