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