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 struct btf; 56 struct btf_member; 57 struct btf_type; 58 union bpf_attr; 59 struct btf_show; 60 struct btf_id_set; 61 62 struct btf_kfunc_id_set { 63 struct module *owner; 64 struct btf_id_set8 *set; 65 }; 66 67 struct btf_id_dtor_kfunc { 68 u32 btf_id; 69 u32 kfunc_btf_id; 70 }; 71 72 typedef void (*btf_dtor_kfunc_t)(void *); 73 74 extern const struct file_operations btf_fops; 75 76 void btf_get(struct btf *btf); 77 void btf_put(struct btf *btf); 78 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr); 79 struct btf *btf_get_by_fd(int fd); 80 int btf_get_info_by_fd(const struct btf *btf, 81 const union bpf_attr *attr, 82 union bpf_attr __user *uattr); 83 /* Figure out the size of a type_id. If type_id is a modifier 84 * (e.g. const), it will be resolved to find out the type with size. 85 * 86 * For example: 87 * In describing "const void *", type_id is "const" and "const" 88 * refers to "void *". The return type will be "void *". 89 * 90 * If type_id is a simple "int", then return type will be "int". 91 * 92 * @btf: struct btf object 93 * @type_id: Find out the size of type_id. The type_id of the return 94 * type is set to *type_id. 95 * @ret_size: It can be NULL. If not NULL, the size of the return 96 * type is set to *ret_size. 97 * Return: The btf_type (resolved to another type with size info if needed). 98 * NULL is returned if type_id itself does not have size info 99 * (e.g. void) or it cannot be resolved to another type that 100 * has size info. 101 * *type_id and *ret_size will not be changed in the 102 * NULL return case. 103 */ 104 const struct btf_type *btf_type_id_size(const struct btf *btf, 105 u32 *type_id, 106 u32 *ret_size); 107 108 /* 109 * Options to control show behaviour. 110 * - BTF_SHOW_COMPACT: no formatting around type information 111 * - BTF_SHOW_NONAME: no struct/union member names/types 112 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values; 113 * equivalent to %px. 114 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they 115 * are not displayed by default 116 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read 117 * data before displaying it. 118 */ 119 #define BTF_SHOW_COMPACT BTF_F_COMPACT 120 #define BTF_SHOW_NONAME BTF_F_NONAME 121 #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW 122 #define BTF_SHOW_ZERO BTF_F_ZERO 123 #define BTF_SHOW_UNSAFE (1ULL << 4) 124 125 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, 126 struct seq_file *m); 127 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj, 128 struct seq_file *m, u64 flags); 129 130 /* 131 * Copy len bytes of string representation of obj of BTF type_id into buf. 132 * 133 * @btf: struct btf object 134 * @type_id: type id of type obj points to 135 * @obj: pointer to typed data 136 * @buf: buffer to write to 137 * @len: maximum length to write to buf 138 * @flags: show options (see above) 139 * 140 * Return: length that would have been/was copied as per snprintf, or 141 * negative error. 142 */ 143 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, 144 char *buf, int len, u64 flags); 145 146 int btf_get_fd_by_id(u32 id); 147 u32 btf_obj_id(const struct btf *btf); 148 bool btf_is_kernel(const struct btf *btf); 149 bool btf_is_module(const struct btf *btf); 150 struct module *btf_try_get_module(const struct btf *btf); 151 u32 btf_nr_types(const struct btf *btf); 152 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, 153 const struct btf_member *m, 154 u32 expected_offset, u32 expected_size); 155 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t); 156 int btf_find_timer(const struct btf *btf, const struct btf_type *t); 157 struct bpf_map_value_off *btf_parse_kptrs(const struct btf *btf, 158 const struct btf_type *t); 159 bool btf_type_is_void(const struct btf_type *t); 160 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); 161 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, 162 u32 id, u32 *res_id); 163 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, 164 u32 id, u32 *res_id); 165 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, 166 u32 id, u32 *res_id); 167 const struct btf_type * 168 btf_resolve_size(const struct btf *btf, const struct btf_type *type, 169 u32 *type_size); 170 const char *btf_type_str(const struct btf_type *t); 171 172 #define for_each_member(i, struct_type, member) \ 173 for (i = 0, member = btf_type_member(struct_type); \ 174 i < btf_type_vlen(struct_type); \ 175 i++, member++) 176 177 #define for_each_vsi(i, datasec_type, member) \ 178 for (i = 0, member = btf_type_var_secinfo(datasec_type); \ 179 i < btf_type_vlen(datasec_type); \ 180 i++, member++) 181 182 static inline bool btf_type_is_ptr(const struct btf_type *t) 183 { 184 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; 185 } 186 187 static inline bool btf_type_is_int(const struct btf_type *t) 188 { 189 return BTF_INFO_KIND(t->info) == BTF_KIND_INT; 190 } 191 192 static inline bool btf_type_is_small_int(const struct btf_type *t) 193 { 194 return btf_type_is_int(t) && t->size <= sizeof(u64); 195 } 196 197 static inline bool btf_type_is_enum(const struct btf_type *t) 198 { 199 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM; 200 } 201 202 static inline bool btf_is_any_enum(const struct btf_type *t) 203 { 204 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM || 205 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64; 206 } 207 208 static inline bool btf_kind_core_compat(const struct btf_type *t1, 209 const struct btf_type *t2) 210 { 211 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) || 212 (btf_is_any_enum(t1) && btf_is_any_enum(t2)); 213 } 214 215 static inline bool str_is_empty(const char *s) 216 { 217 return !s || !s[0]; 218 } 219 220 static inline u16 btf_kind(const struct btf_type *t) 221 { 222 return BTF_INFO_KIND(t->info); 223 } 224 225 static inline bool btf_is_enum(const struct btf_type *t) 226 { 227 return btf_kind(t) == BTF_KIND_ENUM; 228 } 229 230 static inline bool btf_is_enum64(const struct btf_type *t) 231 { 232 return btf_kind(t) == BTF_KIND_ENUM64; 233 } 234 235 static inline u64 btf_enum64_value(const struct btf_enum64 *e) 236 { 237 return ((u64)e->val_hi32 << 32) | e->val_lo32; 238 } 239 240 static inline bool btf_is_composite(const struct btf_type *t) 241 { 242 u16 kind = btf_kind(t); 243 244 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 245 } 246 247 static inline bool btf_is_array(const struct btf_type *t) 248 { 249 return btf_kind(t) == BTF_KIND_ARRAY; 250 } 251 252 static inline bool btf_is_int(const struct btf_type *t) 253 { 254 return btf_kind(t) == BTF_KIND_INT; 255 } 256 257 static inline bool btf_is_ptr(const struct btf_type *t) 258 { 259 return btf_kind(t) == BTF_KIND_PTR; 260 } 261 262 static inline u8 btf_int_offset(const struct btf_type *t) 263 { 264 return BTF_INT_OFFSET(*(u32 *)(t + 1)); 265 } 266 267 static inline u8 btf_int_encoding(const struct btf_type *t) 268 { 269 return BTF_INT_ENCODING(*(u32 *)(t + 1)); 270 } 271 272 static inline bool btf_type_is_scalar(const struct btf_type *t) 273 { 274 return btf_type_is_int(t) || btf_type_is_enum(t); 275 } 276 277 static inline bool btf_type_is_typedef(const struct btf_type *t) 278 { 279 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF; 280 } 281 282 static inline bool btf_type_is_func(const struct btf_type *t) 283 { 284 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC; 285 } 286 287 static inline bool btf_type_is_func_proto(const struct btf_type *t) 288 { 289 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO; 290 } 291 292 static inline bool btf_type_is_var(const struct btf_type *t) 293 { 294 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR; 295 } 296 297 static inline bool btf_type_is_type_tag(const struct btf_type *t) 298 { 299 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG; 300 } 301 302 /* union is only a special case of struct: 303 * all its offsetof(member) == 0 304 */ 305 static inline bool btf_type_is_struct(const struct btf_type *t) 306 { 307 u8 kind = BTF_INFO_KIND(t->info); 308 309 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 310 } 311 312 static inline u16 btf_type_vlen(const struct btf_type *t) 313 { 314 return BTF_INFO_VLEN(t->info); 315 } 316 317 static inline u16 btf_vlen(const struct btf_type *t) 318 { 319 return btf_type_vlen(t); 320 } 321 322 static inline u16 btf_func_linkage(const struct btf_type *t) 323 { 324 return BTF_INFO_VLEN(t->info); 325 } 326 327 static inline bool btf_type_kflag(const struct btf_type *t) 328 { 329 return BTF_INFO_KFLAG(t->info); 330 } 331 332 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type, 333 const struct btf_member *member) 334 { 335 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset) 336 : member->offset; 337 } 338 339 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type, 340 const struct btf_member *member) 341 { 342 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset) 343 : 0; 344 } 345 346 static inline struct btf_member *btf_members(const struct btf_type *t) 347 { 348 return (struct btf_member *)(t + 1); 349 } 350 351 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx) 352 { 353 const struct btf_member *m = btf_members(t) + member_idx; 354 355 return __btf_member_bit_offset(t, m); 356 } 357 358 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx) 359 { 360 const struct btf_member *m = btf_members(t) + member_idx; 361 362 return __btf_member_bitfield_size(t, m); 363 } 364 365 static inline const struct btf_member *btf_type_member(const struct btf_type *t) 366 { 367 return (const struct btf_member *)(t + 1); 368 } 369 370 static inline struct btf_array *btf_array(const struct btf_type *t) 371 { 372 return (struct btf_array *)(t + 1); 373 } 374 375 static inline struct btf_enum *btf_enum(const struct btf_type *t) 376 { 377 return (struct btf_enum *)(t + 1); 378 } 379 380 static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) 381 { 382 return (struct btf_enum64 *)(t + 1); 383 } 384 385 static inline const struct btf_var_secinfo *btf_type_var_secinfo( 386 const struct btf_type *t) 387 { 388 return (const struct btf_var_secinfo *)(t + 1); 389 } 390 391 static inline struct btf_param *btf_params(const struct btf_type *t) 392 { 393 return (struct btf_param *)(t + 1); 394 } 395 396 #ifdef CONFIG_BPF_SYSCALL 397 struct bpf_prog; 398 399 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); 400 const char *btf_name_by_offset(const struct btf *btf, u32 offset); 401 struct btf *btf_parse_vmlinux(void); 402 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); 403 u32 *btf_kfunc_id_set_contains(const struct btf *btf, 404 enum bpf_prog_type prog_type, 405 u32 kfunc_btf_id); 406 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 407 const struct btf_kfunc_id_set *s); 408 s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id); 409 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt, 410 struct module *owner); 411 #else 412 static inline const struct btf_type *btf_type_by_id(const struct btf *btf, 413 u32 type_id) 414 { 415 return NULL; 416 } 417 static inline const char *btf_name_by_offset(const struct btf *btf, 418 u32 offset) 419 { 420 return NULL; 421 } 422 static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf, 423 enum bpf_prog_type prog_type, 424 u32 kfunc_btf_id) 425 { 426 return NULL; 427 } 428 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, 429 const struct btf_kfunc_id_set *s) 430 { 431 return 0; 432 } 433 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id) 434 { 435 return -ENOENT; 436 } 437 static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, 438 u32 add_cnt, struct module *owner) 439 { 440 return 0; 441 } 442 #endif 443 444 #endif 445