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