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 64 struct btf; 65 struct btf_member; 66 struct btf_type; 67 union bpf_attr; 68 struct btf_show; 69 struct btf_id_set; 70 71 struct btf_kfunc_id_set { 72 struct module *owner; 73 struct btf_id_set8 *set; 74 }; 75 76 struct btf_id_dtor_kfunc { 77 u32 btf_id; 78 u32 kfunc_btf_id; 79 }; 80 81 typedef void (*btf_dtor_kfunc_t)(void *); 82 83 extern const struct file_operations btf_fops; 84 85 void btf_get(struct btf *btf); 86 void btf_put(struct btf *btf); 87 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr); 88 struct btf *btf_get_by_fd(int fd); 89 int 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 */ 113 const 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 134 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, 135 struct seq_file *m); 136 int 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 */ 152 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, 153 char *buf, int len, u64 flags); 154 155 int btf_get_fd_by_id(u32 id); 156 u32 btf_obj_id(const struct btf *btf); 157 bool btf_is_kernel(const struct btf *btf); 158 bool btf_is_module(const struct btf *btf); 159 struct module *btf_try_get_module(const struct btf *btf); 160 u32 btf_nr_types(const struct btf *btf); 161 bool 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); 164 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t); 165 int btf_find_timer(const struct btf *btf, const struct btf_type *t); 166 struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t, 167 u32 field_mask, u32 value_size); 168 struct btf_field_offs *btf_parse_field_offs(struct btf_record *rec); 169 bool btf_type_is_void(const struct btf_type *t); 170 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); 171 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, 172 u32 id, u32 *res_id); 173 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, 174 u32 id, u32 *res_id); 175 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, 176 u32 id, u32 *res_id); 177 const struct btf_type * 178 btf_resolve_size(const struct btf *btf, const struct btf_type *type, 179 u32 *type_size); 180 const char *btf_type_str(const struct btf_type *t); 181 182 #define for_each_member(i, struct_type, member) \ 183 for (i = 0, member = btf_type_member(struct_type); \ 184 i < btf_type_vlen(struct_type); \ 185 i++, member++) 186 187 #define for_each_vsi(i, datasec_type, member) \ 188 for (i = 0, member = btf_type_var_secinfo(datasec_type); \ 189 i < btf_type_vlen(datasec_type); \ 190 i++, member++) 191 192 static inline bool btf_type_is_ptr(const struct btf_type *t) 193 { 194 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; 195 } 196 197 static inline bool btf_type_is_int(const struct btf_type *t) 198 { 199 return BTF_INFO_KIND(t->info) == BTF_KIND_INT; 200 } 201 202 static inline bool btf_type_is_small_int(const struct btf_type *t) 203 { 204 return btf_type_is_int(t) && t->size <= sizeof(u64); 205 } 206 207 static inline bool btf_type_is_enum(const struct btf_type *t) 208 { 209 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM; 210 } 211 212 static inline bool btf_is_any_enum(const struct btf_type *t) 213 { 214 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM || 215 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64; 216 } 217 218 static inline bool btf_kind_core_compat(const struct btf_type *t1, 219 const struct btf_type *t2) 220 { 221 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) || 222 (btf_is_any_enum(t1) && btf_is_any_enum(t2)); 223 } 224 225 static inline bool str_is_empty(const char *s) 226 { 227 return !s || !s[0]; 228 } 229 230 static inline u16 btf_kind(const struct btf_type *t) 231 { 232 return BTF_INFO_KIND(t->info); 233 } 234 235 static inline bool btf_is_enum(const struct btf_type *t) 236 { 237 return btf_kind(t) == BTF_KIND_ENUM; 238 } 239 240 static inline bool btf_is_enum64(const struct btf_type *t) 241 { 242 return btf_kind(t) == BTF_KIND_ENUM64; 243 } 244 245 static inline u64 btf_enum64_value(const struct btf_enum64 *e) 246 { 247 return ((u64)e->val_hi32 << 32) | e->val_lo32; 248 } 249 250 static inline bool btf_is_composite(const struct btf_type *t) 251 { 252 u16 kind = btf_kind(t); 253 254 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 255 } 256 257 static inline bool btf_is_array(const struct btf_type *t) 258 { 259 return btf_kind(t) == BTF_KIND_ARRAY; 260 } 261 262 static inline bool btf_is_int(const struct btf_type *t) 263 { 264 return btf_kind(t) == BTF_KIND_INT; 265 } 266 267 static inline bool btf_is_ptr(const struct btf_type *t) 268 { 269 return btf_kind(t) == BTF_KIND_PTR; 270 } 271 272 static inline u8 btf_int_offset(const struct btf_type *t) 273 { 274 return BTF_INT_OFFSET(*(u32 *)(t + 1)); 275 } 276 277 static inline u8 btf_int_encoding(const struct btf_type *t) 278 { 279 return BTF_INT_ENCODING(*(u32 *)(t + 1)); 280 } 281 282 static inline bool btf_type_is_scalar(const struct btf_type *t) 283 { 284 return btf_type_is_int(t) || btf_type_is_enum(t); 285 } 286 287 static inline bool btf_type_is_typedef(const struct btf_type *t) 288 { 289 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF; 290 } 291 292 static inline bool btf_type_is_volatile(const struct btf_type *t) 293 { 294 return BTF_INFO_KIND(t->info) == BTF_KIND_VOLATILE; 295 } 296 297 static inline bool btf_type_is_func(const struct btf_type *t) 298 { 299 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC; 300 } 301 302 static inline bool btf_type_is_func_proto(const struct btf_type *t) 303 { 304 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO; 305 } 306 307 static inline bool btf_type_is_var(const struct btf_type *t) 308 { 309 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR; 310 } 311 312 static inline bool btf_type_is_type_tag(const struct btf_type *t) 313 { 314 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG; 315 } 316 317 /* union is only a special case of struct: 318 * all its offsetof(member) == 0 319 */ 320 static inline bool btf_type_is_struct(const struct btf_type *t) 321 { 322 u8 kind = BTF_INFO_KIND(t->info); 323 324 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 325 } 326 327 static inline u16 btf_type_vlen(const struct btf_type *t) 328 { 329 return BTF_INFO_VLEN(t->info); 330 } 331 332 static inline u16 btf_vlen(const struct btf_type *t) 333 { 334 return btf_type_vlen(t); 335 } 336 337 static inline u16 btf_func_linkage(const struct btf_type *t) 338 { 339 return BTF_INFO_VLEN(t->info); 340 } 341 342 static inline bool btf_type_kflag(const struct btf_type *t) 343 { 344 return BTF_INFO_KFLAG(t->info); 345 } 346 347 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type, 348 const struct btf_member *member) 349 { 350 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset) 351 : member->offset; 352 } 353 354 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type, 355 const struct btf_member *member) 356 { 357 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset) 358 : 0; 359 } 360 361 static inline struct btf_member *btf_members(const struct btf_type *t) 362 { 363 return (struct btf_member *)(t + 1); 364 } 365 366 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx) 367 { 368 const struct btf_member *m = btf_members(t) + member_idx; 369 370 return __btf_member_bit_offset(t, m); 371 } 372 373 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx) 374 { 375 const struct btf_member *m = btf_members(t) + member_idx; 376 377 return __btf_member_bitfield_size(t, m); 378 } 379 380 static inline const struct btf_member *btf_type_member(const struct btf_type *t) 381 { 382 return (const struct btf_member *)(t + 1); 383 } 384 385 static inline struct btf_array *btf_array(const struct btf_type *t) 386 { 387 return (struct btf_array *)(t + 1); 388 } 389 390 static inline struct btf_enum *btf_enum(const struct btf_type *t) 391 { 392 return (struct btf_enum *)(t + 1); 393 } 394 395 static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) 396 { 397 return (struct btf_enum64 *)(t + 1); 398 } 399 400 static inline const struct btf_var_secinfo *btf_type_var_secinfo( 401 const struct btf_type *t) 402 { 403 return (const struct btf_var_secinfo *)(t + 1); 404 } 405 406 static inline struct btf_param *btf_params(const struct btf_type *t) 407 { 408 return (struct btf_param *)(t + 1); 409 } 410 411 #ifdef CONFIG_BPF_SYSCALL 412 struct bpf_prog; 413 414 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); 415 const char *btf_name_by_offset(const struct btf *btf, u32 offset); 416 struct btf *btf_parse_vmlinux(void); 417 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); 418 u32 *btf_kfunc_id_set_contains(const struct btf *btf, 419 enum bpf_prog_type prog_type, 420 u32 kfunc_btf_id); 421 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 422 const struct btf_kfunc_id_set *s); 423 s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id); 424 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt, 425 struct module *owner); 426 #else 427 static inline const struct btf_type *btf_type_by_id(const struct btf *btf, 428 u32 type_id) 429 { 430 return NULL; 431 } 432 static inline const char *btf_name_by_offset(const struct btf *btf, 433 u32 offset) 434 { 435 return NULL; 436 } 437 static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf, 438 enum bpf_prog_type prog_type, 439 u32 kfunc_btf_id) 440 { 441 return NULL; 442 } 443 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 444 const struct btf_kfunc_id_set *s) 445 { 446 return 0; 447 } 448 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id) 449 { 450 return -ENOENT; 451 } 452 static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, 453 u32 add_cnt, struct module *owner) 454 { 455 return 0; 456 } 457 #endif 458 459 static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t) 460 { 461 if (!btf_type_is_ptr(t)) 462 return false; 463 464 t = btf_type_skip_modifiers(btf, t->type, NULL); 465 466 return btf_type_is_struct(t); 467 } 468 469 #endif 470