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