1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* Copyright (c) 2018 Facebook */ 3 4 #ifndef _LINUX_BTF_H 5 #define _LINUX_BTF_H 1 6 7 #include <linux/types.h> 8 #include <linux/bpfptr.h> 9 #include <linux/bsearch.h> 10 #include <linux/btf_ids.h> 11 #include <uapi/linux/btf.h> 12 #include <uapi/linux/bpf.h> 13 14 #define BTF_TYPE_EMIT(type) ((void)(type *)0) 15 #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val) 16 17 /* These need to be macros, as the expressions are used in assembler input */ 18 #define KF_ACQUIRE (1 << 0) /* kfunc is an acquire function */ 19 #define KF_RELEASE (1 << 1) /* kfunc is a release function */ 20 #define KF_RET_NULL (1 << 2) /* kfunc returns a pointer that may be NULL */ 21 /* Trusted arguments are those which are guaranteed to be valid when passed to 22 * the kfunc. It is used to enforce that pointers obtained from either acquire 23 * kfuncs, or from the main kernel on a tracepoint or struct_ops callback 24 * invocation, remain unmodified when being passed to helpers taking trusted 25 * args. 26 * 27 * Consider, for example, the following new task tracepoint: 28 * 29 * SEC("tp_btf/task_newtask") 30 * int BPF_PROG(new_task_tp, struct task_struct *task, u64 clone_flags) 31 * { 32 * ... 33 * } 34 * 35 * And the following kfunc: 36 * 37 * BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS) 38 * 39 * All invocations to the kfunc must pass the unmodified, unwalked task: 40 * 41 * bpf_task_acquire(task); // Allowed 42 * bpf_task_acquire(task->last_wakee); // Rejected, walked task 43 * 44 * Programs may also pass referenced tasks directly to the kfunc: 45 * 46 * struct task_struct *acquired; 47 * 48 * acquired = bpf_task_acquire(task); // Allowed, same as above 49 * bpf_task_acquire(acquired); // Allowed 50 * bpf_task_acquire(task); // Allowed 51 * bpf_task_acquire(acquired->last_wakee); // Rejected, walked task 52 * 53 * Programs may _not_, however, pass a task from an arbitrary fentry/fexit, or 54 * kprobe/kretprobe to the kfunc, as BPF cannot guarantee that all of these 55 * pointers are guaranteed to be safe. For example, the following BPF program 56 * would be rejected: 57 * 58 * SEC("kretprobe/free_task") 59 * int BPF_PROG(free_task_probe, struct task_struct *tsk) 60 * { 61 * struct task_struct *acquired; 62 * 63 * acquired = bpf_task_acquire(acquired); // Rejected, not a trusted pointer 64 * bpf_task_release(acquired); 65 * 66 * return 0; 67 * } 68 */ 69 #define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */ 70 #define KF_SLEEPABLE (1 << 5) /* kfunc may sleep */ 71 #define KF_DESTRUCTIVE (1 << 6) /* kfunc performs destructive actions */ 72 #define KF_RCU (1 << 7) /* kfunc takes either rcu or trusted pointer arguments */ 73 /* only one of KF_ITER_{NEW,NEXT,DESTROY} could be specified per kfunc */ 74 #define KF_ITER_NEW (1 << 8) /* kfunc implements BPF iter constructor */ 75 #define KF_ITER_NEXT (1 << 9) /* kfunc implements BPF iter next method */ 76 #define KF_ITER_DESTROY (1 << 10) /* kfunc implements BPF iter destructor */ 77 #define KF_RCU_PROTECTED (1 << 11) /* kfunc should be protected by rcu cs when they are invoked */ 78 79 /* 80 * Tag marking a kernel function as a kfunc. This is meant to minimize the 81 * amount of copy-paste that kfunc authors have to include for correctness so 82 * as to avoid issues such as the compiler inlining or eliding either a static 83 * kfunc, or a global kfunc in an LTO build. 84 */ 85 #define __bpf_kfunc __used noinline 86 87 #define __bpf_kfunc_start_defs() \ 88 __diag_push(); \ 89 __diag_ignore_all("-Wmissing-declarations", \ 90 "Global kfuncs as their definitions will be in BTF");\ 91 __diag_ignore_all("-Wmissing-prototypes", \ 92 "Global kfuncs as their definitions will be in BTF") 93 94 #define __bpf_kfunc_end_defs() __diag_pop() 95 #define __bpf_hook_start() __bpf_kfunc_start_defs() 96 #define __bpf_hook_end() __bpf_kfunc_end_defs() 97 98 /* 99 * Return the name of the passed struct, if exists, or halt the build if for 100 * example the structure gets renamed. In this way, developers have to revisit 101 * the code using that structure name, and update it accordingly. 102 */ 103 #define stringify_struct(x) \ 104 ({ BUILD_BUG_ON(sizeof(struct x) < 0); \ 105 __stringify(x); }) 106 107 struct btf; 108 struct btf_member; 109 struct btf_type; 110 union bpf_attr; 111 struct btf_show; 112 struct btf_id_set; 113 struct bpf_prog; 114 115 typedef int (*btf_kfunc_filter_t)(const struct bpf_prog *prog, u32 kfunc_id); 116 117 struct btf_kfunc_id_set { 118 struct module *owner; 119 struct btf_id_set8 *set; 120 btf_kfunc_filter_t filter; 121 }; 122 123 struct btf_id_dtor_kfunc { 124 u32 btf_id; 125 u32 kfunc_btf_id; 126 }; 127 128 struct btf_struct_meta { 129 u32 btf_id; 130 struct btf_record *record; 131 }; 132 133 struct btf_struct_metas { 134 u32 cnt; 135 struct btf_struct_meta types[]; 136 }; 137 138 extern const struct file_operations btf_fops; 139 140 const char *btf_get_name(const struct btf *btf); 141 void btf_get(struct btf *btf); 142 void btf_put(struct btf *btf); 143 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_sz); 144 struct btf *btf_get_by_fd(int fd); 145 int btf_get_info_by_fd(const struct btf *btf, 146 const union bpf_attr *attr, 147 union bpf_attr __user *uattr); 148 /* Figure out the size of a type_id. If type_id is a modifier 149 * (e.g. const), it will be resolved to find out the type with size. 150 * 151 * For example: 152 * In describing "const void *", type_id is "const" and "const" 153 * refers to "void *". The return type will be "void *". 154 * 155 * If type_id is a simple "int", then return type will be "int". 156 * 157 * @btf: struct btf object 158 * @type_id: Find out the size of type_id. The type_id of the return 159 * type is set to *type_id. 160 * @ret_size: It can be NULL. If not NULL, the size of the return 161 * type is set to *ret_size. 162 * Return: The btf_type (resolved to another type with size info if needed). 163 * NULL is returned if type_id itself does not have size info 164 * (e.g. void) or it cannot be resolved to another type that 165 * has size info. 166 * *type_id and *ret_size will not be changed in the 167 * NULL return case. 168 */ 169 const struct btf_type *btf_type_id_size(const struct btf *btf, 170 u32 *type_id, 171 u32 *ret_size); 172 173 /* 174 * Options to control show behaviour. 175 * - BTF_SHOW_COMPACT: no formatting around type information 176 * - BTF_SHOW_NONAME: no struct/union member names/types 177 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values; 178 * equivalent to %px. 179 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they 180 * are not displayed by default 181 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read 182 * data before displaying it. 183 */ 184 #define BTF_SHOW_COMPACT BTF_F_COMPACT 185 #define BTF_SHOW_NONAME BTF_F_NONAME 186 #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW 187 #define BTF_SHOW_ZERO BTF_F_ZERO 188 #define BTF_SHOW_UNSAFE (1ULL << 4) 189 190 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, 191 struct seq_file *m); 192 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj, 193 struct seq_file *m, u64 flags); 194 195 /* 196 * Copy len bytes of string representation of obj of BTF type_id into buf. 197 * 198 * @btf: struct btf object 199 * @type_id: type id of type obj points to 200 * @obj: pointer to typed data 201 * @buf: buffer to write to 202 * @len: maximum length to write to buf 203 * @flags: show options (see above) 204 * 205 * Return: length that would have been/was copied as per snprintf, or 206 * negative error. 207 */ 208 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, 209 char *buf, int len, u64 flags); 210 211 int btf_get_fd_by_id(u32 id); 212 u32 btf_obj_id(const struct btf *btf); 213 bool btf_is_kernel(const struct btf *btf); 214 bool btf_is_module(const struct btf *btf); 215 struct module *btf_try_get_module(const struct btf *btf); 216 u32 btf_nr_types(const struct btf *btf); 217 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, 218 const struct btf_member *m, 219 u32 expected_offset, u32 expected_size); 220 struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t, 221 u32 field_mask, u32 value_size); 222 int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec); 223 bool btf_type_is_void(const struct btf_type *t); 224 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); 225 s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p); 226 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, 227 u32 id, u32 *res_id); 228 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, 229 u32 id, u32 *res_id); 230 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, 231 u32 id, u32 *res_id); 232 const struct btf_type * 233 btf_resolve_size(const struct btf *btf, const struct btf_type *type, 234 u32 *type_size); 235 const char *btf_type_str(const struct btf_type *t); 236 237 #define for_each_member(i, struct_type, member) \ 238 for (i = 0, member = btf_type_member(struct_type); \ 239 i < btf_type_vlen(struct_type); \ 240 i++, member++) 241 242 #define for_each_vsi(i, datasec_type, member) \ 243 for (i = 0, member = btf_type_var_secinfo(datasec_type); \ 244 i < btf_type_vlen(datasec_type); \ 245 i++, member++) 246 247 static inline bool btf_type_is_ptr(const struct btf_type *t) 248 { 249 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; 250 } 251 252 static inline bool btf_type_is_int(const struct btf_type *t) 253 { 254 return BTF_INFO_KIND(t->info) == BTF_KIND_INT; 255 } 256 257 static inline bool btf_type_is_small_int(const struct btf_type *t) 258 { 259 return btf_type_is_int(t) && t->size <= sizeof(u64); 260 } 261 262 static inline u8 btf_int_encoding(const struct btf_type *t) 263 { 264 return BTF_INT_ENCODING(*(u32 *)(t + 1)); 265 } 266 267 static inline bool btf_type_is_signed_int(const struct btf_type *t) 268 { 269 return btf_type_is_int(t) && (btf_int_encoding(t) & BTF_INT_SIGNED); 270 } 271 272 static inline bool btf_type_is_enum(const struct btf_type *t) 273 { 274 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM; 275 } 276 277 static inline bool btf_is_any_enum(const struct btf_type *t) 278 { 279 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM || 280 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64; 281 } 282 283 static inline bool btf_kind_core_compat(const struct btf_type *t1, 284 const struct btf_type *t2) 285 { 286 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) || 287 (btf_is_any_enum(t1) && btf_is_any_enum(t2)); 288 } 289 290 static inline bool str_is_empty(const char *s) 291 { 292 return !s || !s[0]; 293 } 294 295 static inline u16 btf_kind(const struct btf_type *t) 296 { 297 return BTF_INFO_KIND(t->info); 298 } 299 300 static inline bool btf_is_enum(const struct btf_type *t) 301 { 302 return btf_kind(t) == BTF_KIND_ENUM; 303 } 304 305 static inline bool btf_is_enum64(const struct btf_type *t) 306 { 307 return btf_kind(t) == BTF_KIND_ENUM64; 308 } 309 310 static inline u64 btf_enum64_value(const struct btf_enum64 *e) 311 { 312 return ((u64)e->val_hi32 << 32) | e->val_lo32; 313 } 314 315 static inline bool btf_is_composite(const struct btf_type *t) 316 { 317 u16 kind = btf_kind(t); 318 319 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 320 } 321 322 static inline bool btf_is_array(const struct btf_type *t) 323 { 324 return btf_kind(t) == BTF_KIND_ARRAY; 325 } 326 327 static inline bool btf_is_int(const struct btf_type *t) 328 { 329 return btf_kind(t) == BTF_KIND_INT; 330 } 331 332 static inline bool btf_is_ptr(const struct btf_type *t) 333 { 334 return btf_kind(t) == BTF_KIND_PTR; 335 } 336 337 static inline u8 btf_int_offset(const struct btf_type *t) 338 { 339 return BTF_INT_OFFSET(*(u32 *)(t + 1)); 340 } 341 342 static inline bool btf_type_is_scalar(const struct btf_type *t) 343 { 344 return btf_type_is_int(t) || btf_type_is_enum(t); 345 } 346 347 static inline bool btf_type_is_typedef(const struct btf_type *t) 348 { 349 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF; 350 } 351 352 static inline bool btf_type_is_volatile(const struct btf_type *t) 353 { 354 return BTF_INFO_KIND(t->info) == BTF_KIND_VOLATILE; 355 } 356 357 static inline bool btf_type_is_func(const struct btf_type *t) 358 { 359 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC; 360 } 361 362 static inline bool btf_type_is_func_proto(const struct btf_type *t) 363 { 364 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO; 365 } 366 367 static inline bool btf_type_is_var(const struct btf_type *t) 368 { 369 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR; 370 } 371 372 static inline bool btf_type_is_type_tag(const struct btf_type *t) 373 { 374 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG; 375 } 376 377 /* union is only a special case of struct: 378 * all its offsetof(member) == 0 379 */ 380 static inline bool btf_type_is_struct(const struct btf_type *t) 381 { 382 u8 kind = BTF_INFO_KIND(t->info); 383 384 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 385 } 386 387 static inline bool __btf_type_is_struct(const struct btf_type *t) 388 { 389 return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT; 390 } 391 392 static inline bool btf_type_is_array(const struct btf_type *t) 393 { 394 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY; 395 } 396 397 static inline u16 btf_type_vlen(const struct btf_type *t) 398 { 399 return BTF_INFO_VLEN(t->info); 400 } 401 402 static inline u16 btf_vlen(const struct btf_type *t) 403 { 404 return btf_type_vlen(t); 405 } 406 407 static inline u16 btf_func_linkage(const struct btf_type *t) 408 { 409 return BTF_INFO_VLEN(t->info); 410 } 411 412 static inline bool btf_type_kflag(const struct btf_type *t) 413 { 414 return BTF_INFO_KFLAG(t->info); 415 } 416 417 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type, 418 const struct btf_member *member) 419 { 420 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset) 421 : member->offset; 422 } 423 424 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type, 425 const struct btf_member *member) 426 { 427 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset) 428 : 0; 429 } 430 431 static inline struct btf_member *btf_members(const struct btf_type *t) 432 { 433 return (struct btf_member *)(t + 1); 434 } 435 436 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx) 437 { 438 const struct btf_member *m = btf_members(t) + member_idx; 439 440 return __btf_member_bit_offset(t, m); 441 } 442 443 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx) 444 { 445 const struct btf_member *m = btf_members(t) + member_idx; 446 447 return __btf_member_bitfield_size(t, m); 448 } 449 450 static inline const struct btf_member *btf_type_member(const struct btf_type *t) 451 { 452 return (const struct btf_member *)(t + 1); 453 } 454 455 static inline struct btf_array *btf_array(const struct btf_type *t) 456 { 457 return (struct btf_array *)(t + 1); 458 } 459 460 static inline struct btf_enum *btf_enum(const struct btf_type *t) 461 { 462 return (struct btf_enum *)(t + 1); 463 } 464 465 static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) 466 { 467 return (struct btf_enum64 *)(t + 1); 468 } 469 470 static inline const struct btf_var_secinfo *btf_type_var_secinfo( 471 const struct btf_type *t) 472 { 473 return (const struct btf_var_secinfo *)(t + 1); 474 } 475 476 static inline struct btf_param *btf_params(const struct btf_type *t) 477 { 478 return (struct btf_param *)(t + 1); 479 } 480 481 static inline int btf_id_cmp_func(const void *a, const void *b) 482 { 483 const int *pa = a, *pb = b; 484 485 return *pa - *pb; 486 } 487 488 static inline bool btf_id_set_contains(const struct btf_id_set *set, u32 id) 489 { 490 return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL; 491 } 492 493 static inline void *btf_id_set8_contains(const struct btf_id_set8 *set, u32 id) 494 { 495 return bsearch(&id, set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func); 496 } 497 498 bool btf_param_match_suffix(const struct btf *btf, 499 const struct btf_param *arg, 500 const char *suffix); 501 int btf_ctx_arg_offset(const struct btf *btf, const struct btf_type *func_proto, 502 u32 arg_no); 503 504 struct bpf_verifier_log; 505 506 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL) 507 struct bpf_struct_ops; 508 int __register_bpf_struct_ops(struct bpf_struct_ops *st_ops); 509 const struct bpf_struct_ops_desc *bpf_struct_ops_find_value(struct btf *btf, u32 value_id); 510 const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *btf, u32 type_id); 511 #else 512 static inline const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *btf, u32 type_id) 513 { 514 return NULL; 515 } 516 #endif 517 518 #ifdef CONFIG_BPF_SYSCALL 519 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); 520 const char *btf_name_by_offset(const struct btf *btf, u32 offset); 521 struct btf *btf_parse_vmlinux(void); 522 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); 523 u32 *btf_kfunc_id_set_contains(const struct btf *btf, u32 kfunc_btf_id, 524 const struct bpf_prog *prog); 525 u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id, 526 const struct bpf_prog *prog); 527 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 528 const struct btf_kfunc_id_set *s); 529 int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset); 530 s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id); 531 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt, 532 struct module *owner); 533 struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id); 534 bool btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, 535 const struct btf_type *t, enum bpf_prog_type prog_type, 536 int arg); 537 int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type); 538 bool btf_types_are_same(const struct btf *btf1, u32 id1, 539 const struct btf *btf2, u32 id2); 540 #else 541 static inline const struct btf_type *btf_type_by_id(const struct btf *btf, 542 u32 type_id) 543 { 544 return NULL; 545 } 546 static inline const char *btf_name_by_offset(const struct btf *btf, 547 u32 offset) 548 { 549 return NULL; 550 } 551 static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf, 552 u32 kfunc_btf_id, 553 struct bpf_prog *prog) 554 555 { 556 return NULL; 557 } 558 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 559 const struct btf_kfunc_id_set *s) 560 { 561 return 0; 562 } 563 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id) 564 { 565 return -ENOENT; 566 } 567 static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, 568 u32 add_cnt, struct module *owner) 569 { 570 return 0; 571 } 572 static inline struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id) 573 { 574 return NULL; 575 } 576 static inline bool 577 btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, 578 const struct btf_type *t, enum bpf_prog_type prog_type, 579 int arg) 580 { 581 return false; 582 } 583 static inline int get_kern_ctx_btf_id(struct bpf_verifier_log *log, 584 enum bpf_prog_type prog_type) { 585 return -EINVAL; 586 } 587 static inline bool btf_types_are_same(const struct btf *btf1, u32 id1, 588 const struct btf *btf2, u32 id2) 589 { 590 return false; 591 } 592 #endif 593 594 static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t) 595 { 596 if (!btf_type_is_ptr(t)) 597 return false; 598 599 t = btf_type_skip_modifiers(btf, t->type, NULL); 600 601 return btf_type_is_struct(t); 602 } 603 604 #endif 605