at v6.7 18 kB view raw
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 107struct btf; 108struct btf_member; 109struct btf_type; 110union bpf_attr; 111struct btf_show; 112struct btf_id_set; 113struct bpf_prog; 114 115typedef int (*btf_kfunc_filter_t)(const struct bpf_prog *prog, u32 kfunc_id); 116 117struct btf_kfunc_id_set { 118 struct module *owner; 119 struct btf_id_set8 *set; 120 btf_kfunc_filter_t filter; 121}; 122 123struct btf_id_dtor_kfunc { 124 u32 btf_id; 125 u32 kfunc_btf_id; 126}; 127 128struct btf_struct_meta { 129 u32 btf_id; 130 struct btf_record *record; 131}; 132 133struct btf_struct_metas { 134 u32 cnt; 135 struct btf_struct_meta types[]; 136}; 137 138extern const struct file_operations btf_fops; 139 140void btf_get(struct btf *btf); 141void btf_put(struct btf *btf); 142int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_sz); 143struct btf *btf_get_by_fd(int fd); 144int btf_get_info_by_fd(const struct btf *btf, 145 const union bpf_attr *attr, 146 union bpf_attr __user *uattr); 147/* Figure out the size of a type_id. If type_id is a modifier 148 * (e.g. const), it will be resolved to find out the type with size. 149 * 150 * For example: 151 * In describing "const void *", type_id is "const" and "const" 152 * refers to "void *". The return type will be "void *". 153 * 154 * If type_id is a simple "int", then return type will be "int". 155 * 156 * @btf: struct btf object 157 * @type_id: Find out the size of type_id. The type_id of the return 158 * type is set to *type_id. 159 * @ret_size: It can be NULL. If not NULL, the size of the return 160 * type is set to *ret_size. 161 * Return: The btf_type (resolved to another type with size info if needed). 162 * NULL is returned if type_id itself does not have size info 163 * (e.g. void) or it cannot be resolved to another type that 164 * has size info. 165 * *type_id and *ret_size will not be changed in the 166 * NULL return case. 167 */ 168const struct btf_type *btf_type_id_size(const struct btf *btf, 169 u32 *type_id, 170 u32 *ret_size); 171 172/* 173 * Options to control show behaviour. 174 * - BTF_SHOW_COMPACT: no formatting around type information 175 * - BTF_SHOW_NONAME: no struct/union member names/types 176 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values; 177 * equivalent to %px. 178 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they 179 * are not displayed by default 180 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read 181 * data before displaying it. 182 */ 183#define BTF_SHOW_COMPACT BTF_F_COMPACT 184#define BTF_SHOW_NONAME BTF_F_NONAME 185#define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW 186#define BTF_SHOW_ZERO BTF_F_ZERO 187#define BTF_SHOW_UNSAFE (1ULL << 4) 188 189void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, 190 struct seq_file *m); 191int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj, 192 struct seq_file *m, u64 flags); 193 194/* 195 * Copy len bytes of string representation of obj of BTF type_id into buf. 196 * 197 * @btf: struct btf object 198 * @type_id: type id of type obj points to 199 * @obj: pointer to typed data 200 * @buf: buffer to write to 201 * @len: maximum length to write to buf 202 * @flags: show options (see above) 203 * 204 * Return: length that would have been/was copied as per snprintf, or 205 * negative error. 206 */ 207int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, 208 char *buf, int len, u64 flags); 209 210int btf_get_fd_by_id(u32 id); 211u32 btf_obj_id(const struct btf *btf); 212bool btf_is_kernel(const struct btf *btf); 213bool btf_is_module(const struct btf *btf); 214struct module *btf_try_get_module(const struct btf *btf); 215u32 btf_nr_types(const struct btf *btf); 216bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, 217 const struct btf_member *m, 218 u32 expected_offset, u32 expected_size); 219struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t, 220 u32 field_mask, u32 value_size); 221int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec); 222bool btf_type_is_void(const struct btf_type *t); 223s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); 224s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p); 225const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, 226 u32 id, u32 *res_id); 227const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, 228 u32 id, u32 *res_id); 229const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, 230 u32 id, u32 *res_id); 231const struct btf_type * 232btf_resolve_size(const struct btf *btf, const struct btf_type *type, 233 u32 *type_size); 234const char *btf_type_str(const struct btf_type *t); 235 236#define for_each_member(i, struct_type, member) \ 237 for (i = 0, member = btf_type_member(struct_type); \ 238 i < btf_type_vlen(struct_type); \ 239 i++, member++) 240 241#define for_each_vsi(i, datasec_type, member) \ 242 for (i = 0, member = btf_type_var_secinfo(datasec_type); \ 243 i < btf_type_vlen(datasec_type); \ 244 i++, member++) 245 246static inline bool btf_type_is_ptr(const struct btf_type *t) 247{ 248 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; 249} 250 251static inline bool btf_type_is_int(const struct btf_type *t) 252{ 253 return BTF_INFO_KIND(t->info) == BTF_KIND_INT; 254} 255 256static inline bool btf_type_is_small_int(const struct btf_type *t) 257{ 258 return btf_type_is_int(t) && t->size <= sizeof(u64); 259} 260 261static inline u8 btf_int_encoding(const struct btf_type *t) 262{ 263 return BTF_INT_ENCODING(*(u32 *)(t + 1)); 264} 265 266static inline bool btf_type_is_signed_int(const struct btf_type *t) 267{ 268 return btf_type_is_int(t) && (btf_int_encoding(t) & BTF_INT_SIGNED); 269} 270 271static inline bool btf_type_is_enum(const struct btf_type *t) 272{ 273 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM; 274} 275 276static inline bool btf_is_any_enum(const struct btf_type *t) 277{ 278 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM || 279 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64; 280} 281 282static inline bool btf_kind_core_compat(const struct btf_type *t1, 283 const struct btf_type *t2) 284{ 285 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) || 286 (btf_is_any_enum(t1) && btf_is_any_enum(t2)); 287} 288 289static inline bool str_is_empty(const char *s) 290{ 291 return !s || !s[0]; 292} 293 294static inline u16 btf_kind(const struct btf_type *t) 295{ 296 return BTF_INFO_KIND(t->info); 297} 298 299static inline bool btf_is_enum(const struct btf_type *t) 300{ 301 return btf_kind(t) == BTF_KIND_ENUM; 302} 303 304static inline bool btf_is_enum64(const struct btf_type *t) 305{ 306 return btf_kind(t) == BTF_KIND_ENUM64; 307} 308 309static inline u64 btf_enum64_value(const struct btf_enum64 *e) 310{ 311 return ((u64)e->val_hi32 << 32) | e->val_lo32; 312} 313 314static inline bool btf_is_composite(const struct btf_type *t) 315{ 316 u16 kind = btf_kind(t); 317 318 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 319} 320 321static inline bool btf_is_array(const struct btf_type *t) 322{ 323 return btf_kind(t) == BTF_KIND_ARRAY; 324} 325 326static inline bool btf_is_int(const struct btf_type *t) 327{ 328 return btf_kind(t) == BTF_KIND_INT; 329} 330 331static inline bool btf_is_ptr(const struct btf_type *t) 332{ 333 return btf_kind(t) == BTF_KIND_PTR; 334} 335 336static inline u8 btf_int_offset(const struct btf_type *t) 337{ 338 return BTF_INT_OFFSET(*(u32 *)(t + 1)); 339} 340 341static inline bool btf_type_is_scalar(const struct btf_type *t) 342{ 343 return btf_type_is_int(t) || btf_type_is_enum(t); 344} 345 346static inline bool btf_type_is_typedef(const struct btf_type *t) 347{ 348 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF; 349} 350 351static inline bool btf_type_is_volatile(const struct btf_type *t) 352{ 353 return BTF_INFO_KIND(t->info) == BTF_KIND_VOLATILE; 354} 355 356static inline bool btf_type_is_func(const struct btf_type *t) 357{ 358 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC; 359} 360 361static inline bool btf_type_is_func_proto(const struct btf_type *t) 362{ 363 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO; 364} 365 366static inline bool btf_type_is_var(const struct btf_type *t) 367{ 368 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR; 369} 370 371static inline bool btf_type_is_type_tag(const struct btf_type *t) 372{ 373 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG; 374} 375 376/* union is only a special case of struct: 377 * all its offsetof(member) == 0 378 */ 379static inline bool btf_type_is_struct(const struct btf_type *t) 380{ 381 u8 kind = BTF_INFO_KIND(t->info); 382 383 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 384} 385 386static inline bool __btf_type_is_struct(const struct btf_type *t) 387{ 388 return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT; 389} 390 391static inline bool btf_type_is_array(const struct btf_type *t) 392{ 393 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY; 394} 395 396static inline u16 btf_type_vlen(const struct btf_type *t) 397{ 398 return BTF_INFO_VLEN(t->info); 399} 400 401static inline u16 btf_vlen(const struct btf_type *t) 402{ 403 return btf_type_vlen(t); 404} 405 406static inline u16 btf_func_linkage(const struct btf_type *t) 407{ 408 return BTF_INFO_VLEN(t->info); 409} 410 411static inline bool btf_type_kflag(const struct btf_type *t) 412{ 413 return BTF_INFO_KFLAG(t->info); 414} 415 416static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type, 417 const struct btf_member *member) 418{ 419 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset) 420 : member->offset; 421} 422 423static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type, 424 const struct btf_member *member) 425{ 426 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset) 427 : 0; 428} 429 430static inline struct btf_member *btf_members(const struct btf_type *t) 431{ 432 return (struct btf_member *)(t + 1); 433} 434 435static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx) 436{ 437 const struct btf_member *m = btf_members(t) + member_idx; 438 439 return __btf_member_bit_offset(t, m); 440} 441 442static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx) 443{ 444 const struct btf_member *m = btf_members(t) + member_idx; 445 446 return __btf_member_bitfield_size(t, m); 447} 448 449static inline const struct btf_member *btf_type_member(const struct btf_type *t) 450{ 451 return (const struct btf_member *)(t + 1); 452} 453 454static inline struct btf_array *btf_array(const struct btf_type *t) 455{ 456 return (struct btf_array *)(t + 1); 457} 458 459static inline struct btf_enum *btf_enum(const struct btf_type *t) 460{ 461 return (struct btf_enum *)(t + 1); 462} 463 464static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) 465{ 466 return (struct btf_enum64 *)(t + 1); 467} 468 469static inline const struct btf_var_secinfo *btf_type_var_secinfo( 470 const struct btf_type *t) 471{ 472 return (const struct btf_var_secinfo *)(t + 1); 473} 474 475static inline struct btf_param *btf_params(const struct btf_type *t) 476{ 477 return (struct btf_param *)(t + 1); 478} 479 480static inline int btf_id_cmp_func(const void *a, const void *b) 481{ 482 const int *pa = a, *pb = b; 483 484 return *pa - *pb; 485} 486 487static inline bool btf_id_set_contains(const struct btf_id_set *set, u32 id) 488{ 489 return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL; 490} 491 492static inline void *btf_id_set8_contains(const struct btf_id_set8 *set, u32 id) 493{ 494 return bsearch(&id, set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func); 495} 496 497struct bpf_verifier_log; 498 499#ifdef CONFIG_BPF_SYSCALL 500const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); 501const char *btf_name_by_offset(const struct btf *btf, u32 offset); 502struct btf *btf_parse_vmlinux(void); 503struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); 504u32 *btf_kfunc_id_set_contains(const struct btf *btf, u32 kfunc_btf_id, 505 const struct bpf_prog *prog); 506u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id, 507 const struct bpf_prog *prog); 508int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 509 const struct btf_kfunc_id_set *s); 510int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset); 511s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id); 512int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt, 513 struct module *owner); 514struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id); 515const struct btf_member * 516btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, 517 const struct btf_type *t, enum bpf_prog_type prog_type, 518 int arg); 519int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type); 520bool btf_types_are_same(const struct btf *btf1, u32 id1, 521 const struct btf *btf2, u32 id2); 522#else 523static inline const struct btf_type *btf_type_by_id(const struct btf *btf, 524 u32 type_id) 525{ 526 return NULL; 527} 528static inline const char *btf_name_by_offset(const struct btf *btf, 529 u32 offset) 530{ 531 return NULL; 532} 533static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf, 534 u32 kfunc_btf_id, 535 struct bpf_prog *prog) 536 537{ 538 return NULL; 539} 540static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 541 const struct btf_kfunc_id_set *s) 542{ 543 return 0; 544} 545static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id) 546{ 547 return -ENOENT; 548} 549static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, 550 u32 add_cnt, struct module *owner) 551{ 552 return 0; 553} 554static inline struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id) 555{ 556 return NULL; 557} 558static inline const struct btf_member * 559btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, 560 const struct btf_type *t, enum bpf_prog_type prog_type, 561 int arg) 562{ 563 return NULL; 564} 565static inline int get_kern_ctx_btf_id(struct bpf_verifier_log *log, 566 enum bpf_prog_type prog_type) { 567 return -EINVAL; 568} 569static inline bool btf_types_are_same(const struct btf *btf1, u32 id1, 570 const struct btf *btf2, u32 id2) 571{ 572 return false; 573} 574#endif 575 576static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t) 577{ 578 if (!btf_type_is_ptr(t)) 579 return false; 580 581 t = btf_type_skip_modifiers(btf, t->type, NULL); 582 583 return btf_type_is_struct(t); 584} 585 586#endif