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