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