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