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