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