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