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