at v6.1 14 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 <uapi/linux/btf.h> 10#include <uapi/linux/bpf.h> 11 12#define BTF_TYPE_EMIT(type) ((void)(type *)0) 13#define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val) 14 15/* These need to be macros, as the expressions are used in assembler input */ 16#define KF_ACQUIRE (1 << 0) /* kfunc is an acquire function */ 17#define KF_RELEASE (1 << 1) /* kfunc is a release function */ 18#define KF_RET_NULL (1 << 2) /* kfunc returns a pointer that may be NULL */ 19#define KF_KPTR_GET (1 << 3) /* kfunc returns reference to a kptr */ 20/* Trusted arguments are those which are meant to be referenced arguments with 21 * unchanged offset. It is used to enforce that pointers obtained from acquire 22 * kfuncs remain unmodified when being passed to helpers taking trusted args. 23 * 24 * Consider 25 * struct foo { 26 * int data; 27 * struct foo *next; 28 * }; 29 * 30 * struct bar { 31 * int data; 32 * struct foo f; 33 * }; 34 * 35 * struct foo *f = alloc_foo(); // Acquire kfunc 36 * struct bar *b = alloc_bar(); // Acquire kfunc 37 * 38 * If a kfunc set_foo_data() wants to operate only on the allocated object, it 39 * will set the KF_TRUSTED_ARGS flag, which will prevent unsafe usage like: 40 * 41 * set_foo_data(f, 42); // Allowed 42 * set_foo_data(f->next, 42); // Rejected, non-referenced pointer 43 * set_foo_data(&f->next, 42);// Rejected, referenced, but wrong type 44 * set_foo_data(&b->f, 42); // Rejected, referenced, but bad offset 45 * 46 * In the final case, usually for the purposes of type matching, it is deduced 47 * by looking at the type of the member at the offset, but due to the 48 * requirement of trusted argument, this deduction will be strict and not done 49 * for this case. 50 */ 51#define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */ 52#define KF_SLEEPABLE (1 << 5) /* kfunc may sleep */ 53#define KF_DESTRUCTIVE (1 << 6) /* kfunc performs destructive actions */ 54 55/* 56 * Return the name of the passed struct, if exists, or halt the build if for 57 * example the structure gets renamed. In this way, developers have to revisit 58 * the code using that structure name, and update it accordingly. 59 */ 60#define stringify_struct(x) \ 61 ({ BUILD_BUG_ON(sizeof(struct x) < 0); \ 62 __stringify(x); }) 63 64struct btf; 65struct btf_member; 66struct btf_type; 67union bpf_attr; 68struct btf_show; 69struct btf_id_set; 70 71struct btf_kfunc_id_set { 72 struct module *owner; 73 struct btf_id_set8 *set; 74}; 75 76struct btf_id_dtor_kfunc { 77 u32 btf_id; 78 u32 kfunc_btf_id; 79}; 80 81typedef void (*btf_dtor_kfunc_t)(void *); 82 83extern const struct file_operations btf_fops; 84 85void btf_get(struct btf *btf); 86void btf_put(struct btf *btf); 87int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr); 88struct btf *btf_get_by_fd(int fd); 89int btf_get_info_by_fd(const struct btf *btf, 90 const union bpf_attr *attr, 91 union bpf_attr __user *uattr); 92/* Figure out the size of a type_id. If type_id is a modifier 93 * (e.g. const), it will be resolved to find out the type with size. 94 * 95 * For example: 96 * In describing "const void *", type_id is "const" and "const" 97 * refers to "void *". The return type will be "void *". 98 * 99 * If type_id is a simple "int", then return type will be "int". 100 * 101 * @btf: struct btf object 102 * @type_id: Find out the size of type_id. The type_id of the return 103 * type is set to *type_id. 104 * @ret_size: It can be NULL. If not NULL, the size of the return 105 * type is set to *ret_size. 106 * Return: The btf_type (resolved to another type with size info if needed). 107 * NULL is returned if type_id itself does not have size info 108 * (e.g. void) or it cannot be resolved to another type that 109 * has size info. 110 * *type_id and *ret_size will not be changed in the 111 * NULL return case. 112 */ 113const struct btf_type *btf_type_id_size(const struct btf *btf, 114 u32 *type_id, 115 u32 *ret_size); 116 117/* 118 * Options to control show behaviour. 119 * - BTF_SHOW_COMPACT: no formatting around type information 120 * - BTF_SHOW_NONAME: no struct/union member names/types 121 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values; 122 * equivalent to %px. 123 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they 124 * are not displayed by default 125 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read 126 * data before displaying it. 127 */ 128#define BTF_SHOW_COMPACT BTF_F_COMPACT 129#define BTF_SHOW_NONAME BTF_F_NONAME 130#define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW 131#define BTF_SHOW_ZERO BTF_F_ZERO 132#define BTF_SHOW_UNSAFE (1ULL << 4) 133 134void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, 135 struct seq_file *m); 136int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj, 137 struct seq_file *m, u64 flags); 138 139/* 140 * Copy len bytes of string representation of obj of BTF type_id into buf. 141 * 142 * @btf: struct btf object 143 * @type_id: type id of type obj points to 144 * @obj: pointer to typed data 145 * @buf: buffer to write to 146 * @len: maximum length to write to buf 147 * @flags: show options (see above) 148 * 149 * Return: length that would have been/was copied as per snprintf, or 150 * negative error. 151 */ 152int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, 153 char *buf, int len, u64 flags); 154 155int btf_get_fd_by_id(u32 id); 156u32 btf_obj_id(const struct btf *btf); 157bool btf_is_kernel(const struct btf *btf); 158bool btf_is_module(const struct btf *btf); 159struct module *btf_try_get_module(const struct btf *btf); 160u32 btf_nr_types(const struct btf *btf); 161bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, 162 const struct btf_member *m, 163 u32 expected_offset, u32 expected_size); 164int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t); 165int btf_find_timer(const struct btf *btf, const struct btf_type *t); 166struct bpf_map_value_off *btf_parse_kptrs(const struct btf *btf, 167 const struct btf_type *t); 168bool btf_type_is_void(const struct btf_type *t); 169s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); 170const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, 171 u32 id, u32 *res_id); 172const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, 173 u32 id, u32 *res_id); 174const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, 175 u32 id, u32 *res_id); 176const struct btf_type * 177btf_resolve_size(const struct btf *btf, const struct btf_type *type, 178 u32 *type_size); 179const char *btf_type_str(const struct btf_type *t); 180 181#define for_each_member(i, struct_type, member) \ 182 for (i = 0, member = btf_type_member(struct_type); \ 183 i < btf_type_vlen(struct_type); \ 184 i++, member++) 185 186#define for_each_vsi(i, datasec_type, member) \ 187 for (i = 0, member = btf_type_var_secinfo(datasec_type); \ 188 i < btf_type_vlen(datasec_type); \ 189 i++, member++) 190 191static inline bool btf_type_is_ptr(const struct btf_type *t) 192{ 193 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; 194} 195 196static inline bool btf_type_is_int(const struct btf_type *t) 197{ 198 return BTF_INFO_KIND(t->info) == BTF_KIND_INT; 199} 200 201static inline bool btf_type_is_small_int(const struct btf_type *t) 202{ 203 return btf_type_is_int(t) && t->size <= sizeof(u64); 204} 205 206static inline bool btf_type_is_enum(const struct btf_type *t) 207{ 208 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM; 209} 210 211static inline bool btf_is_any_enum(const struct btf_type *t) 212{ 213 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM || 214 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64; 215} 216 217static inline bool btf_kind_core_compat(const struct btf_type *t1, 218 const struct btf_type *t2) 219{ 220 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) || 221 (btf_is_any_enum(t1) && btf_is_any_enum(t2)); 222} 223 224static inline bool str_is_empty(const char *s) 225{ 226 return !s || !s[0]; 227} 228 229static inline u16 btf_kind(const struct btf_type *t) 230{ 231 return BTF_INFO_KIND(t->info); 232} 233 234static inline bool btf_is_enum(const struct btf_type *t) 235{ 236 return btf_kind(t) == BTF_KIND_ENUM; 237} 238 239static inline bool btf_is_enum64(const struct btf_type *t) 240{ 241 return btf_kind(t) == BTF_KIND_ENUM64; 242} 243 244static inline u64 btf_enum64_value(const struct btf_enum64 *e) 245{ 246 return ((u64)e->val_hi32 << 32) | e->val_lo32; 247} 248 249static inline bool btf_is_composite(const struct btf_type *t) 250{ 251 u16 kind = btf_kind(t); 252 253 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 254} 255 256static inline bool btf_is_array(const struct btf_type *t) 257{ 258 return btf_kind(t) == BTF_KIND_ARRAY; 259} 260 261static inline bool btf_is_int(const struct btf_type *t) 262{ 263 return btf_kind(t) == BTF_KIND_INT; 264} 265 266static inline bool btf_is_ptr(const struct btf_type *t) 267{ 268 return btf_kind(t) == BTF_KIND_PTR; 269} 270 271static inline u8 btf_int_offset(const struct btf_type *t) 272{ 273 return BTF_INT_OFFSET(*(u32 *)(t + 1)); 274} 275 276static inline u8 btf_int_encoding(const struct btf_type *t) 277{ 278 return BTF_INT_ENCODING(*(u32 *)(t + 1)); 279} 280 281static inline bool btf_type_is_scalar(const struct btf_type *t) 282{ 283 return btf_type_is_int(t) || btf_type_is_enum(t); 284} 285 286static inline bool btf_type_is_typedef(const struct btf_type *t) 287{ 288 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF; 289} 290 291static inline bool btf_type_is_func(const struct btf_type *t) 292{ 293 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC; 294} 295 296static inline bool btf_type_is_func_proto(const struct btf_type *t) 297{ 298 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO; 299} 300 301static inline bool btf_type_is_var(const struct btf_type *t) 302{ 303 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR; 304} 305 306static inline bool btf_type_is_type_tag(const struct btf_type *t) 307{ 308 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG; 309} 310 311/* union is only a special case of struct: 312 * all its offsetof(member) == 0 313 */ 314static inline bool btf_type_is_struct(const struct btf_type *t) 315{ 316 u8 kind = BTF_INFO_KIND(t->info); 317 318 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 319} 320 321static inline u16 btf_type_vlen(const struct btf_type *t) 322{ 323 return BTF_INFO_VLEN(t->info); 324} 325 326static inline u16 btf_vlen(const struct btf_type *t) 327{ 328 return btf_type_vlen(t); 329} 330 331static inline u16 btf_func_linkage(const struct btf_type *t) 332{ 333 return BTF_INFO_VLEN(t->info); 334} 335 336static inline bool btf_type_kflag(const struct btf_type *t) 337{ 338 return BTF_INFO_KFLAG(t->info); 339} 340 341static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type, 342 const struct btf_member *member) 343{ 344 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset) 345 : member->offset; 346} 347 348static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type, 349 const struct btf_member *member) 350{ 351 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset) 352 : 0; 353} 354 355static inline struct btf_member *btf_members(const struct btf_type *t) 356{ 357 return (struct btf_member *)(t + 1); 358} 359 360static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx) 361{ 362 const struct btf_member *m = btf_members(t) + member_idx; 363 364 return __btf_member_bit_offset(t, m); 365} 366 367static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx) 368{ 369 const struct btf_member *m = btf_members(t) + member_idx; 370 371 return __btf_member_bitfield_size(t, m); 372} 373 374static inline const struct btf_member *btf_type_member(const struct btf_type *t) 375{ 376 return (const struct btf_member *)(t + 1); 377} 378 379static inline struct btf_array *btf_array(const struct btf_type *t) 380{ 381 return (struct btf_array *)(t + 1); 382} 383 384static inline struct btf_enum *btf_enum(const struct btf_type *t) 385{ 386 return (struct btf_enum *)(t + 1); 387} 388 389static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) 390{ 391 return (struct btf_enum64 *)(t + 1); 392} 393 394static inline const struct btf_var_secinfo *btf_type_var_secinfo( 395 const struct btf_type *t) 396{ 397 return (const struct btf_var_secinfo *)(t + 1); 398} 399 400static inline struct btf_param *btf_params(const struct btf_type *t) 401{ 402 return (struct btf_param *)(t + 1); 403} 404 405#ifdef CONFIG_BPF_SYSCALL 406struct bpf_prog; 407 408const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); 409const char *btf_name_by_offset(const struct btf *btf, u32 offset); 410struct btf *btf_parse_vmlinux(void); 411struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); 412u32 *btf_kfunc_id_set_contains(const struct btf *btf, 413 enum bpf_prog_type prog_type, 414 u32 kfunc_btf_id); 415int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 416 const struct btf_kfunc_id_set *s); 417s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id); 418int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt, 419 struct module *owner); 420#else 421static inline const struct btf_type *btf_type_by_id(const struct btf *btf, 422 u32 type_id) 423{ 424 return NULL; 425} 426static inline const char *btf_name_by_offset(const struct btf *btf, 427 u32 offset) 428{ 429 return NULL; 430} 431static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf, 432 enum bpf_prog_type prog_type, 433 u32 kfunc_btf_id) 434{ 435 return NULL; 436} 437static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 438 const struct btf_kfunc_id_set *s) 439{ 440 return 0; 441} 442static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id) 443{ 444 return -ENOENT; 445} 446static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, 447 u32 add_cnt, struct module *owner) 448{ 449 return 0; 450} 451#endif 452 453static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t) 454{ 455 if (!btf_type_is_ptr(t)) 456 return false; 457 458 t = btf_type_skip_modifiers(btf, t->type, NULL); 459 460 return btf_type_is_struct(t); 461} 462 463#endif