Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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