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