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/module.h> 9 #include <linux/bpfptr.h> 10 #include <linux/bsearch.h> 11 #include <linux/btf_ids.h> 12 #include <uapi/linux/btf.h> 13 #include <uapi/linux/bpf.h> 14 15 #define BTF_TYPE_EMIT(type) ((void)(type *)0) 16 #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val) 17 18 /* These need to be macros, as the expressions are used in assembler input */ 19 #define KF_ACQUIRE (1 << 0) /* kfunc is an acquire function */ 20 #define KF_RELEASE (1 << 1) /* kfunc is a release function */ 21 #define KF_RET_NULL (1 << 2) /* kfunc returns a pointer that may be NULL */ 22 /* Trusted arguments are those which are guaranteed to be valid when passed to 23 * the kfunc. It is used to enforce that pointers obtained from either acquire 24 * kfuncs, or from the main kernel on a tracepoint or struct_ops callback 25 * invocation, remain unmodified when being passed to helpers taking trusted 26 * args. 27 * 28 * Consider, for example, the following new task tracepoint: 29 * 30 * SEC("tp_btf/task_newtask") 31 * int BPF_PROG(new_task_tp, struct task_struct *task, u64 clone_flags) 32 * { 33 * ... 34 * } 35 * 36 * And the following kfunc: 37 * 38 * BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE) 39 * 40 * All invocations to the kfunc must pass the unmodified, unwalked task: 41 * 42 * bpf_task_acquire(task); // Allowed 43 * bpf_task_acquire(task->last_wakee); // Rejected, walked task 44 * 45 * Programs may also pass referenced tasks directly to the kfunc: 46 * 47 * struct task_struct *acquired; 48 * 49 * acquired = bpf_task_acquire(task); // Allowed, same as above 50 * bpf_task_acquire(acquired); // Allowed 51 * bpf_task_acquire(task); // Allowed 52 * bpf_task_acquire(acquired->last_wakee); // Rejected, walked task 53 * 54 * Programs may _not_, however, pass a task from an arbitrary fentry/fexit, or 55 * kprobe/kretprobe to the kfunc, as BPF cannot guarantee that all of these 56 * pointers are guaranteed to be safe. For example, the following BPF program 57 * would be rejected: 58 * 59 * SEC("kretprobe/free_task") 60 * int BPF_PROG(free_task_probe, struct task_struct *tsk) 61 * { 62 * struct task_struct *acquired; 63 * 64 * acquired = bpf_task_acquire(acquired); // Rejected, not a trusted pointer 65 * bpf_task_release(acquired); 66 * 67 * return 0; 68 * } 69 */ 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 #define KF_FASTCALL (1 << 12) /* kfunc supports bpf_fastcall protocol */ 79 #define KF_ARENA_RET (1 << 13) /* kfunc returns an arena pointer */ 80 #define KF_ARENA_ARG1 (1 << 14) /* kfunc takes an arena pointer as its first argument */ 81 #define KF_ARENA_ARG2 (1 << 15) /* kfunc takes an arena pointer as its second argument */ 82 #define KF_IMPLICIT_ARGS (1 << 16) /* kfunc has implicit arguments supplied by the verifier */ 83 #define KF_SPINLOCK_SAFE (1 << 17) /* kfunc is allowed inside bpf_spin_lock-ed region */ 84 #define KF_PERFMON (1 << 18) /* kfunc requires CAP_PERFMON */ 85 #define KF_ITER_RET_INDEPENDENT (1 << 19) /* next result outlives the current element */ 86 87 /* 88 * Tag marking a kernel function as a kfunc. This is meant to minimize the 89 * amount of copy-paste that kfunc authors have to include for correctness so 90 * as to avoid issues such as the compiler inlining or eliding either a static 91 * kfunc, or a global kfunc in an LTO build. 92 */ 93 #define __bpf_kfunc __used __retain __noclone noinline 94 95 #define __bpf_kfunc_start_defs() \ 96 __diag_push(); \ 97 __diag_ignore_all("-Wmissing-declarations", \ 98 "Global kfuncs as their definitions will be in BTF");\ 99 __diag_ignore_all("-Wmissing-prototypes", \ 100 "Global kfuncs as their definitions will be in BTF") 101 102 #define __bpf_kfunc_end_defs() __diag_pop() 103 #define __bpf_hook_start() __bpf_kfunc_start_defs() 104 #define __bpf_hook_end() __bpf_kfunc_end_defs() 105 106 /* 107 * Return the name of the passed struct, if exists, or halt the build if for 108 * example the structure gets renamed. In this way, developers have to revisit 109 * the code using that structure name, and update it accordingly. 110 */ 111 #define stringify_struct(x) \ 112 ({ BUILD_BUG_ON(sizeof(struct x) < 0); \ 113 __stringify(x); }) 114 115 struct btf; 116 struct btf_member; 117 struct btf_type; 118 union bpf_attr; 119 struct btf_show; 120 struct btf_id_set; 121 struct bpf_prog; 122 123 #define BTF_LINK_SHA256_LEN 32 124 125 /* 126 * A .BTF.link or .BTF.inline.link section identifies the module which 127 * carries a vmlinux BTF section. The module name is NUL terminated and the 128 * field is padded to __MODULE_NAME_LEN. 129 */ 130 struct btf_link { 131 char module_name[__MODULE_NAME_LEN]; 132 u8 sha256[BTF_LINK_SHA256_LEN]; 133 u32 btf_size; 134 } __packed; 135 136 typedef int (*btf_kfunc_filter_t)(const struct bpf_prog *prog, u32 kfunc_id); 137 138 struct btf_kfunc_id_set { 139 struct module *owner; 140 struct btf_id_set8 *set; 141 btf_kfunc_filter_t filter; 142 }; 143 144 struct btf_id_dtor_kfunc { 145 u32 btf_id; 146 u32 kfunc_btf_id; 147 }; 148 149 struct btf_struct_meta { 150 u32 btf_id; 151 struct btf_record *record; 152 }; 153 154 struct btf_struct_metas { 155 u32 cnt; 156 struct btf_struct_meta types[]; 157 }; 158 159 extern const struct file_operations btf_fops; 160 161 const char *btf_get_name(const struct btf *btf); 162 void btf_get(struct btf *btf); 163 void btf_put(struct btf *btf); 164 const struct btf_header *btf_header(const struct btf *btf); 165 struct bpf_log_attr; 166 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log); 167 struct btf *btf_get_by_fd(int fd); 168 int btf_get_info_by_fd(const struct btf *btf, 169 const union bpf_attr *attr, 170 union bpf_attr __user *uattr); 171 /* Figure out the size of a type_id. If type_id is a modifier 172 * (e.g. const), it will be resolved to find out the type with size. 173 * 174 * For example: 175 * In describing "const void *", type_id is "const" and "const" 176 * refers to "void *". The return type will be "void *". 177 * 178 * If type_id is a simple "int", then return type will be "int". 179 * 180 * @btf: struct btf object 181 * @type_id: Find out the size of type_id. The type_id of the return 182 * type is set to *type_id. 183 * @ret_size: It can be NULL. If not NULL, the size of the return 184 * type is set to *ret_size. 185 * Return: The btf_type (resolved to another type with size info if needed). 186 * NULL is returned if type_id itself does not have size info 187 * (e.g. void) or it cannot be resolved to another type that 188 * has size info. 189 * *type_id and *ret_size will not be changed in the 190 * NULL return case. 191 */ 192 const struct btf_type *btf_type_id_size(const struct btf *btf, 193 u32 *type_id, 194 u32 *ret_size); 195 196 /* 197 * Options to control show behaviour. 198 * - BTF_SHOW_COMPACT: no formatting around type information 199 * - BTF_SHOW_NONAME: no struct/union member names/types 200 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values; 201 * equivalent to %px. 202 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they 203 * are not displayed by default 204 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read 205 * data before displaying it. 206 */ 207 #define BTF_SHOW_COMPACT BTF_F_COMPACT 208 #define BTF_SHOW_NONAME BTF_F_NONAME 209 #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW 210 #define BTF_SHOW_ZERO BTF_F_ZERO 211 #define BTF_SHOW_UNSAFE (1ULL << 4) 212 213 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, 214 struct seq_file *m); 215 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj, 216 struct seq_file *m, u64 flags); 217 218 /* 219 * Copy len bytes of string representation of obj of BTF type_id into buf. 220 * 221 * @btf: struct btf object 222 * @type_id: type id of type obj points to 223 * @obj: pointer to typed data 224 * @buf: buffer to write to 225 * @len: maximum length to write to buf 226 * @flags: show options (see above) 227 * 228 * Return: length that would have been/was copied as per snprintf, or 229 * negative error. 230 */ 231 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, 232 char *buf, int len, u64 flags); 233 int btf_type_name_to_buf(const struct btf *btf, u32 type_id, char *buf, int len); 234 235 int btf_get_fd_by_id(u32 id); 236 u32 btf_obj_id(const struct btf *btf); 237 bool btf_is_kernel(const struct btf *btf); 238 bool btf_is_module(const struct btf *btf); 239 bool btf_is_vmlinux(const struct btf *btf); 240 struct module *btf_try_get_module(const struct btf *btf); 241 u32 btf_nr_types(const struct btf *btf); 242 u32 btf_named_start_id(const struct btf *btf, bool own); 243 struct btf *btf_base_btf(const struct btf *btf); 244 bool btf_type_is_i32(const struct btf_type *t); 245 bool btf_type_is_i64(const struct btf_type *t); 246 bool btf_type_is_primitive(const struct btf_type *t); 247 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, 248 const struct btf_member *m, 249 u32 expected_offset, u32 expected_size); 250 struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t, 251 u32 field_mask, u32 value_size); 252 int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec); 253 bool btf_type_is_void(const struct btf_type *t); 254 bool btf_type_is_arena_ptr(const struct btf *btf, const struct btf_type *t); 255 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); 256 s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p); 257 struct btf *btf_get_module_btf(const struct module *module); 258 __u32 btf_relocate_id(const struct btf *btf, __u32 id); 259 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, 260 u32 id, u32 *res_id); 261 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, 262 u32 id, u32 *res_id); 263 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, 264 u32 id, u32 *res_id); 265 const struct btf_type * 266 btf_resolve_size(const struct btf *btf, const struct btf_type *type, 267 u32 *type_size); 268 const char *btf_type_str(const struct btf_type *t); 269 270 #define for_each_member(i, struct_type, member) \ 271 for (i = 0, member = btf_type_member(struct_type); \ 272 i < btf_type_vlen(struct_type); \ 273 i++, member++) 274 275 #define for_each_vsi(i, datasec_type, member) \ 276 for (i = 0, member = btf_type_var_secinfo(datasec_type); \ 277 i < btf_type_vlen(datasec_type); \ 278 i++, member++) 279 280 #define for_each_loc(i, locsec_type, member) \ 281 for (i = 0, member = btf_type_loc_secinfo(locsec_type); \ 282 i < btf_type_vlen(locsec_type); \ 283 i++, member++) 284 285 static inline bool btf_type_is_ptr(const struct btf_type *t) 286 { 287 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; 288 } 289 290 static inline bool btf_type_is_int(const struct btf_type *t) 291 { 292 return BTF_INFO_KIND(t->info) == BTF_KIND_INT; 293 } 294 295 static inline bool btf_type_is_small_int(const struct btf_type *t) 296 { 297 return btf_type_is_int(t) && t->size <= sizeof(u64); 298 } 299 300 static inline u8 btf_int_encoding(const struct btf_type *t) 301 { 302 return BTF_INT_ENCODING(*(u32 *)(t + 1)); 303 } 304 305 static inline bool btf_type_is_signed_int(const struct btf_type *t) 306 { 307 return btf_type_is_int(t) && (btf_int_encoding(t) & BTF_INT_SIGNED); 308 } 309 310 static inline bool btf_type_is_enum(const struct btf_type *t) 311 { 312 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM; 313 } 314 315 static inline bool btf_is_any_enum(const struct btf_type *t) 316 { 317 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM || 318 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64; 319 } 320 321 static inline bool btf_kind_core_compat(const struct btf_type *t1, 322 const struct btf_type *t2) 323 { 324 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) || 325 (btf_is_any_enum(t1) && btf_is_any_enum(t2)); 326 } 327 328 static inline bool str_is_empty(const char *s) 329 { 330 return !s || !s[0]; 331 } 332 333 static inline u16 btf_kind(const struct btf_type *t) 334 { 335 return BTF_INFO_KIND(t->info); 336 } 337 338 static inline bool btf_is_enum(const struct btf_type *t) 339 { 340 return btf_kind(t) == BTF_KIND_ENUM; 341 } 342 343 static inline bool btf_is_enum64(const struct btf_type *t) 344 { 345 return btf_kind(t) == BTF_KIND_ENUM64; 346 } 347 348 static inline u64 btf_enum64_value(const struct btf_enum64 *e) 349 { 350 return ((u64)e->val_hi32 << 32) | e->val_lo32; 351 } 352 353 static inline struct btf_loc_param *btf_loc_param(const struct btf_type *t) 354 { 355 return (struct btf_loc_param *)(t + 1); 356 } 357 358 static inline __u32 *btf_loc_proto_params(const struct btf_type *t) 359 { 360 return (__u32 *)(t + 1); 361 } 362 363 static inline struct btf_loc *btf_type_loc_secinfo(const struct btf_type *t) 364 { 365 return (struct btf_loc *)(t + 1); 366 } 367 368 static inline bool btf_is_composite(const struct btf_type *t) 369 { 370 u16 kind = btf_kind(t); 371 372 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 373 } 374 375 static inline bool btf_is_array(const struct btf_type *t) 376 { 377 return btf_kind(t) == BTF_KIND_ARRAY; 378 } 379 380 static inline bool btf_is_int(const struct btf_type *t) 381 { 382 return btf_kind(t) == BTF_KIND_INT; 383 } 384 385 static inline bool btf_is_ptr(const struct btf_type *t) 386 { 387 return btf_kind(t) == BTF_KIND_PTR; 388 } 389 390 static inline u8 btf_int_offset(const struct btf_type *t) 391 { 392 return BTF_INT_OFFSET(*(u32 *)(t + 1)); 393 } 394 395 static inline __u8 btf_int_bits(const struct btf_type *t) 396 { 397 return BTF_INT_BITS(*(__u32 *)(t + 1)); 398 } 399 400 static inline bool btf_type_is_scalar(const struct btf_type *t) 401 { 402 return btf_type_is_int(t) || btf_type_is_enum(t); 403 } 404 405 static inline bool btf_type_is_fwd(const struct btf_type *t) 406 { 407 return BTF_INFO_KIND(t->info) == BTF_KIND_FWD; 408 } 409 410 static inline bool btf_type_is_typedef(const struct btf_type *t) 411 { 412 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF; 413 } 414 415 static inline bool btf_type_is_volatile(const struct btf_type *t) 416 { 417 return BTF_INFO_KIND(t->info) == BTF_KIND_VOLATILE; 418 } 419 420 static inline bool btf_type_is_func(const struct btf_type *t) 421 { 422 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC; 423 } 424 425 static inline bool btf_type_is_func_proto(const struct btf_type *t) 426 { 427 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO; 428 } 429 430 static inline bool btf_type_is_var(const struct btf_type *t) 431 { 432 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR; 433 } 434 435 static inline bool btf_type_is_type_tag(const struct btf_type *t) 436 { 437 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG; 438 } 439 440 /* union is only a special case of struct: 441 * all its offsetof(member) == 0 442 */ 443 static inline bool btf_type_is_struct(const struct btf_type *t) 444 { 445 u8 kind = BTF_INFO_KIND(t->info); 446 447 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 448 } 449 450 static inline bool __btf_type_is_struct(const struct btf_type *t) 451 { 452 return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT; 453 } 454 455 static inline bool btf_type_is_array(const struct btf_type *t) 456 { 457 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY; 458 } 459 460 static inline u32 btf_type_vlen(const struct btf_type *t) 461 { 462 return BTF_INFO_VLEN(t->info); 463 } 464 465 static inline u32 btf_vlen(const struct btf_type *t) 466 { 467 return btf_type_vlen(t); 468 } 469 470 static inline u16 btf_func_linkage(const struct btf_type *t) 471 { 472 return BTF_INFO_VLEN(t->info); 473 } 474 475 static inline bool btf_type_kflag(const struct btf_type *t) 476 { 477 return BTF_INFO_KFLAG(t->info); 478 } 479 480 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type, 481 const struct btf_member *member) 482 { 483 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset) 484 : member->offset; 485 } 486 487 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type, 488 const struct btf_member *member) 489 { 490 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset) 491 : 0; 492 } 493 494 static inline struct btf_member *btf_members(const struct btf_type *t) 495 { 496 return (struct btf_member *)(t + 1); 497 } 498 499 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx) 500 { 501 const struct btf_member *m = btf_members(t) + member_idx; 502 503 return __btf_member_bit_offset(t, m); 504 } 505 506 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx) 507 { 508 const struct btf_member *m = btf_members(t) + member_idx; 509 510 return __btf_member_bitfield_size(t, m); 511 } 512 513 static inline const struct btf_member *btf_type_member(const struct btf_type *t) 514 { 515 return (const struct btf_member *)(t + 1); 516 } 517 518 static inline struct btf_array *btf_array(const struct btf_type *t) 519 { 520 return (struct btf_array *)(t + 1); 521 } 522 523 static inline struct btf_enum *btf_enum(const struct btf_type *t) 524 { 525 return (struct btf_enum *)(t + 1); 526 } 527 528 static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) 529 { 530 return (struct btf_enum64 *)(t + 1); 531 } 532 533 static inline const struct btf_var_secinfo *btf_type_var_secinfo( 534 const struct btf_type *t) 535 { 536 return (const struct btf_var_secinfo *)(t + 1); 537 } 538 539 static inline struct btf_param *btf_params(const struct btf_type *t) 540 { 541 return (struct btf_param *)(t + 1); 542 } 543 544 static inline struct btf_decl_tag *btf_decl_tag(const struct btf_type *t) 545 { 546 return (struct btf_decl_tag *)(t + 1); 547 } 548 549 static inline int btf_id_cmp_func(const void *a, const void *b) 550 { 551 const int *pa = a, *pb = b; 552 553 return *pa - *pb; 554 } 555 556 static inline bool btf_id_set_contains(const struct btf_id_set *set, u32 id) 557 { 558 return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL; 559 } 560 561 static inline void *btf_id_set8_contains(const struct btf_id_set8 *set, u32 id) 562 { 563 return bsearch(&id, set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func); 564 } 565 566 bool btf_param_match_suffix(const struct btf *btf, 567 const struct btf_param *arg, 568 const char *suffix); 569 int btf_ctx_arg_offset(const struct btf *btf, const struct btf_type *func_proto, 570 u32 arg_no); 571 u32 btf_ctx_arg_idx(struct btf *btf, const struct btf_type *func_proto, int off); 572 573 struct bpf_verifier_log; 574 575 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL) 576 struct bpf_struct_ops; 577 int __register_bpf_struct_ops(struct bpf_struct_ops *st_ops); 578 const struct bpf_struct_ops_desc *bpf_struct_ops_find_value(struct btf *btf, u32 value_id); 579 const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *btf, u32 type_id); 580 #else 581 static inline const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *btf, u32 type_id) 582 { 583 return NULL; 584 } 585 #endif 586 587 enum btf_field_iter_kind { 588 BTF_FIELD_ITER_IDS, 589 BTF_FIELD_ITER_STRS, 590 }; 591 592 struct btf_field_desc { 593 /* once-per-type offsets */ 594 int t_off_cnt, t_offs[2]; 595 /* member struct size, or zero, if no members */ 596 int m_sz; 597 /* repeated per-member offsets */ 598 int m_off_cnt, m_offs[2]; 599 }; 600 601 struct btf_field_iter { 602 struct btf_field_desc desc; 603 void *p; 604 int m_idx; 605 int off_idx; 606 int vlen; 607 }; 608 609 #ifdef CONFIG_BPF_SYSCALL 610 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); 611 void btf_set_base_btf(struct btf *btf, const struct btf *base_btf); 612 int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **map_ids, 613 __u32 **map_strs); 614 int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t, 615 enum btf_field_iter_kind iter_kind); 616 __u32 *btf_field_iter_next(struct btf_field_iter *it); 617 618 const char *btf_name_by_offset(const struct btf *btf, u32 offset); 619 const char *btf_str_by_offset(const struct btf *btf, u32 offset); 620 struct btf *btf_parse_vmlinux(void); 621 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); 622 u32 *btf_kfunc_flags(const struct btf *btf, u32 kfunc_btf_id, const struct bpf_prog *prog); 623 int btf_kfunc_check_flag(const struct btf *btf, u32 kfunc_btf_id, u32 flag); 624 bool btf_kfunc_is_allowed(const struct btf *btf, u32 kfunc_btf_id, const struct bpf_prog *prog); 625 u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id, 626 const struct bpf_prog *prog); 627 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 628 const struct btf_kfunc_id_set *s); 629 int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset); 630 s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id); 631 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt, 632 struct module *owner); 633 struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id); 634 bool btf_is_projection_type(const char *pname); 635 bool btf_is_projection_of(const char *pname, const char *tname); 636 bool btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, 637 const struct btf_type *t, enum bpf_prog_type prog_type, 638 int arg); 639 int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type); 640 bool btf_types_are_same(const struct btf *btf1, u32 id1, 641 const struct btf *btf2, u32 id2); 642 int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx); 643 644 #if IS_ENABLED(CONFIG_SYSFS) 645 struct bin_attribute *sysfs_btf_add(const char *name, void *data, size_t data_size, 646 bool mmap, const char *lazy_module_name); 647 bool sysfs_btf_update(struct bin_attribute *attr, void *data); 648 void sysfs_btf_remove(struct bin_attribute *attr); 649 #else 650 static inline struct bin_attribute * 651 sysfs_btf_add(const char *name, void *data, size_t data_size, bool mmap, 652 const char *lazy_module_name) 653 { 654 return NULL; 655 } 656 657 static inline bool sysfs_btf_update(struct bin_attribute *attr, void *data) 658 { 659 return false; 660 } 661 662 static inline void sysfs_btf_remove(struct bin_attribute *attr) 663 { 664 } 665 #endif 666 667 static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t) 668 { 669 if (!btf_type_is_ptr(t)) 670 return false; 671 672 t = btf_type_skip_modifiers(btf, t->type, NULL); 673 674 return btf_type_is_struct(t); 675 } 676 #else 677 static inline const struct btf_type *btf_type_by_id(const struct btf *btf, 678 u32 type_id) 679 { 680 return NULL; 681 } 682 683 static inline void btf_set_base_btf(struct btf *btf, const struct btf *base_btf) 684 { 685 } 686 687 static inline int btf_relocate(void *log, struct btf *btf, const struct btf *base_btf, 688 __u32 **map_ids, __u32 **map_strs) 689 { 690 return -EOPNOTSUPP; 691 } 692 693 static inline int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t, 694 enum btf_field_iter_kind iter_kind) 695 { 696 return -EOPNOTSUPP; 697 } 698 699 static inline __u32 *btf_field_iter_next(struct btf_field_iter *it) 700 { 701 return NULL; 702 } 703 704 static inline const char *btf_name_by_offset(const struct btf *btf, 705 u32 offset) 706 { 707 return NULL; 708 } 709 static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf, 710 u32 kfunc_btf_id, 711 struct bpf_prog *prog) 712 713 { 714 return NULL; 715 } 716 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 717 const struct btf_kfunc_id_set *s) 718 { 719 return 0; 720 } 721 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id) 722 { 723 return -ENOENT; 724 } 725 static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, 726 u32 add_cnt, struct module *owner) 727 { 728 return 0; 729 } 730 static inline struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id) 731 { 732 return NULL; 733 } 734 static inline bool 735 btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, 736 const struct btf_type *t, enum bpf_prog_type prog_type, 737 int arg) 738 { 739 return false; 740 } 741 static inline int get_kern_ctx_btf_id(struct bpf_verifier_log *log, 742 enum bpf_prog_type prog_type) { 743 return -EINVAL; 744 } 745 static inline bool btf_types_are_same(const struct btf *btf1, u32 id1, 746 const struct btf *btf2, u32 id2) 747 { 748 return false; 749 } 750 static inline int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx) 751 { 752 return -EOPNOTSUPP; 753 } 754 #endif 755 #endif 756