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 bpf_map_value_off *btf_parse_kptrs(const struct btf *btf, 167 const struct btf_type *t); 168 bool btf_type_is_void(const struct btf_type *t); 169 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); 170 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, 171 u32 id, u32 *res_id); 172 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, 173 u32 id, u32 *res_id); 174 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, 175 u32 id, u32 *res_id); 176 const struct btf_type * 177 btf_resolve_size(const struct btf *btf, const struct btf_type *type, 178 u32 *type_size); 179 const 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 191 static inline bool btf_type_is_ptr(const struct btf_type *t) 192 { 193 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; 194 } 195 196 static inline bool btf_type_is_int(const struct btf_type *t) 197 { 198 return BTF_INFO_KIND(t->info) == BTF_KIND_INT; 199 } 200 201 static 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 206 static inline bool btf_type_is_enum(const struct btf_type *t) 207 { 208 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM; 209 } 210 211 static 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 217 static 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 224 static inline bool str_is_empty(const char *s) 225 { 226 return !s || !s[0]; 227 } 228 229 static inline u16 btf_kind(const struct btf_type *t) 230 { 231 return BTF_INFO_KIND(t->info); 232 } 233 234 static inline bool btf_is_enum(const struct btf_type *t) 235 { 236 return btf_kind(t) == BTF_KIND_ENUM; 237 } 238 239 static inline bool btf_is_enum64(const struct btf_type *t) 240 { 241 return btf_kind(t) == BTF_KIND_ENUM64; 242 } 243 244 static inline u64 btf_enum64_value(const struct btf_enum64 *e) 245 { 246 return ((u64)e->val_hi32 << 32) | e->val_lo32; 247 } 248 249 static 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 256 static inline bool btf_is_array(const struct btf_type *t) 257 { 258 return btf_kind(t) == BTF_KIND_ARRAY; 259 } 260 261 static inline bool btf_is_int(const struct btf_type *t) 262 { 263 return btf_kind(t) == BTF_KIND_INT; 264 } 265 266 static inline bool btf_is_ptr(const struct btf_type *t) 267 { 268 return btf_kind(t) == BTF_KIND_PTR; 269 } 270 271 static inline u8 btf_int_offset(const struct btf_type *t) 272 { 273 return BTF_INT_OFFSET(*(u32 *)(t + 1)); 274 } 275 276 static inline u8 btf_int_encoding(const struct btf_type *t) 277 { 278 return BTF_INT_ENCODING(*(u32 *)(t + 1)); 279 } 280 281 static 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 286 static inline bool btf_type_is_typedef(const struct btf_type *t) 287 { 288 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF; 289 } 290 291 static inline bool btf_type_is_func(const struct btf_type *t) 292 { 293 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC; 294 } 295 296 static 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 301 static inline bool btf_type_is_var(const struct btf_type *t) 302 { 303 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR; 304 } 305 306 static 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 */ 314 static 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 321 static inline u16 btf_type_vlen(const struct btf_type *t) 322 { 323 return BTF_INFO_VLEN(t->info); 324 } 325 326 static inline u16 btf_vlen(const struct btf_type *t) 327 { 328 return btf_type_vlen(t); 329 } 330 331 static inline u16 btf_func_linkage(const struct btf_type *t) 332 { 333 return BTF_INFO_VLEN(t->info); 334 } 335 336 static inline bool btf_type_kflag(const struct btf_type *t) 337 { 338 return BTF_INFO_KFLAG(t->info); 339 } 340 341 static 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 348 static 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 355 static inline struct btf_member *btf_members(const struct btf_type *t) 356 { 357 return (struct btf_member *)(t + 1); 358 } 359 360 static 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 367 static 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 374 static inline const struct btf_member *btf_type_member(const struct btf_type *t) 375 { 376 return (const struct btf_member *)(t + 1); 377 } 378 379 static inline struct btf_array *btf_array(const struct btf_type *t) 380 { 381 return (struct btf_array *)(t + 1); 382 } 383 384 static inline struct btf_enum *btf_enum(const struct btf_type *t) 385 { 386 return (struct btf_enum *)(t + 1); 387 } 388 389 static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) 390 { 391 return (struct btf_enum64 *)(t + 1); 392 } 393 394 static 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 400 static 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 406 struct bpf_prog; 407 408 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); 409 const char *btf_name_by_offset(const struct btf *btf, u32 offset); 410 struct btf *btf_parse_vmlinux(void); 411 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); 412 u32 *btf_kfunc_id_set_contains(const struct btf *btf, 413 enum bpf_prog_type prog_type, 414 u32 kfunc_btf_id); 415 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 416 const struct btf_kfunc_id_set *s); 417 s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id); 418 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt, 419 struct module *owner); 420 #else 421 static inline const struct btf_type *btf_type_by_id(const struct btf *btf, 422 u32 type_id) 423 { 424 return NULL; 425 } 426 static inline const char *btf_name_by_offset(const struct btf *btf, 427 u32 offset) 428 { 429 return NULL; 430 } 431 static 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 } 437 static 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 } 442 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id) 443 { 444 return -ENOENT; 445 } 446 static 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 453 static 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 464