at v5.17 10 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 15struct btf; 16struct btf_member; 17struct btf_type; 18union bpf_attr; 19struct btf_show; 20 21extern const struct file_operations btf_fops; 22 23void btf_get(struct btf *btf); 24void btf_put(struct btf *btf); 25int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr); 26struct btf *btf_get_by_fd(int fd); 27int btf_get_info_by_fd(const struct btf *btf, 28 const union bpf_attr *attr, 29 union bpf_attr __user *uattr); 30/* Figure out the size of a type_id. If type_id is a modifier 31 * (e.g. const), it will be resolved to find out the type with size. 32 * 33 * For example: 34 * In describing "const void *", type_id is "const" and "const" 35 * refers to "void *". The return type will be "void *". 36 * 37 * If type_id is a simple "int", then return type will be "int". 38 * 39 * @btf: struct btf object 40 * @type_id: Find out the size of type_id. The type_id of the return 41 * type is set to *type_id. 42 * @ret_size: It can be NULL. If not NULL, the size of the return 43 * type is set to *ret_size. 44 * Return: The btf_type (resolved to another type with size info if needed). 45 * NULL is returned if type_id itself does not have size info 46 * (e.g. void) or it cannot be resolved to another type that 47 * has size info. 48 * *type_id and *ret_size will not be changed in the 49 * NULL return case. 50 */ 51const struct btf_type *btf_type_id_size(const struct btf *btf, 52 u32 *type_id, 53 u32 *ret_size); 54 55/* 56 * Options to control show behaviour. 57 * - BTF_SHOW_COMPACT: no formatting around type information 58 * - BTF_SHOW_NONAME: no struct/union member names/types 59 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values; 60 * equivalent to %px. 61 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they 62 * are not displayed by default 63 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read 64 * data before displaying it. 65 */ 66#define BTF_SHOW_COMPACT BTF_F_COMPACT 67#define BTF_SHOW_NONAME BTF_F_NONAME 68#define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW 69#define BTF_SHOW_ZERO BTF_F_ZERO 70#define BTF_SHOW_UNSAFE (1ULL << 4) 71 72void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, 73 struct seq_file *m); 74int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj, 75 struct seq_file *m, u64 flags); 76 77/* 78 * Copy len bytes of string representation of obj of BTF type_id into buf. 79 * 80 * @btf: struct btf object 81 * @type_id: type id of type obj points to 82 * @obj: pointer to typed data 83 * @buf: buffer to write to 84 * @len: maximum length to write to buf 85 * @flags: show options (see above) 86 * 87 * Return: length that would have been/was copied as per snprintf, or 88 * negative error. 89 */ 90int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, 91 char *buf, int len, u64 flags); 92 93int btf_get_fd_by_id(u32 id); 94u32 btf_obj_id(const struct btf *btf); 95bool btf_is_kernel(const struct btf *btf); 96bool btf_is_module(const struct btf *btf); 97struct module *btf_try_get_module(const struct btf *btf); 98u32 btf_nr_types(const struct btf *btf); 99bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, 100 const struct btf_member *m, 101 u32 expected_offset, u32 expected_size); 102int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t); 103int btf_find_timer(const struct btf *btf, const struct btf_type *t); 104bool btf_type_is_void(const struct btf_type *t); 105s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); 106const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, 107 u32 id, u32 *res_id); 108const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, 109 u32 id, u32 *res_id); 110const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, 111 u32 id, u32 *res_id); 112const struct btf_type * 113btf_resolve_size(const struct btf *btf, const struct btf_type *type, 114 u32 *type_size); 115const char *btf_type_str(const struct btf_type *t); 116 117#define for_each_member(i, struct_type, member) \ 118 for (i = 0, member = btf_type_member(struct_type); \ 119 i < btf_type_vlen(struct_type); \ 120 i++, member++) 121 122#define for_each_vsi(i, datasec_type, member) \ 123 for (i = 0, member = btf_type_var_secinfo(datasec_type); \ 124 i < btf_type_vlen(datasec_type); \ 125 i++, member++) 126 127static inline bool btf_type_is_ptr(const struct btf_type *t) 128{ 129 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; 130} 131 132static inline bool btf_type_is_int(const struct btf_type *t) 133{ 134 return BTF_INFO_KIND(t->info) == BTF_KIND_INT; 135} 136 137static inline bool btf_type_is_small_int(const struct btf_type *t) 138{ 139 return btf_type_is_int(t) && t->size <= sizeof(u64); 140} 141 142static inline bool btf_type_is_enum(const struct btf_type *t) 143{ 144 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM; 145} 146 147static inline bool str_is_empty(const char *s) 148{ 149 return !s || !s[0]; 150} 151 152static inline u16 btf_kind(const struct btf_type *t) 153{ 154 return BTF_INFO_KIND(t->info); 155} 156 157static inline bool btf_is_enum(const struct btf_type *t) 158{ 159 return btf_kind(t) == BTF_KIND_ENUM; 160} 161 162static inline bool btf_is_composite(const struct btf_type *t) 163{ 164 u16 kind = btf_kind(t); 165 166 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 167} 168 169static inline bool btf_is_array(const struct btf_type *t) 170{ 171 return btf_kind(t) == BTF_KIND_ARRAY; 172} 173 174static inline bool btf_is_int(const struct btf_type *t) 175{ 176 return btf_kind(t) == BTF_KIND_INT; 177} 178 179static inline bool btf_is_ptr(const struct btf_type *t) 180{ 181 return btf_kind(t) == BTF_KIND_PTR; 182} 183 184static inline u8 btf_int_offset(const struct btf_type *t) 185{ 186 return BTF_INT_OFFSET(*(u32 *)(t + 1)); 187} 188 189static inline u8 btf_int_encoding(const struct btf_type *t) 190{ 191 return BTF_INT_ENCODING(*(u32 *)(t + 1)); 192} 193 194static inline bool btf_type_is_scalar(const struct btf_type *t) 195{ 196 return btf_type_is_int(t) || btf_type_is_enum(t); 197} 198 199static inline bool btf_type_is_typedef(const struct btf_type *t) 200{ 201 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF; 202} 203 204static inline bool btf_type_is_func(const struct btf_type *t) 205{ 206 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC; 207} 208 209static inline bool btf_type_is_func_proto(const struct btf_type *t) 210{ 211 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO; 212} 213 214static inline bool btf_type_is_var(const struct btf_type *t) 215{ 216 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR; 217} 218 219/* union is only a special case of struct: 220 * all its offsetof(member) == 0 221 */ 222static inline bool btf_type_is_struct(const struct btf_type *t) 223{ 224 u8 kind = BTF_INFO_KIND(t->info); 225 226 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; 227} 228 229static inline u16 btf_type_vlen(const struct btf_type *t) 230{ 231 return BTF_INFO_VLEN(t->info); 232} 233 234static inline u16 btf_vlen(const struct btf_type *t) 235{ 236 return btf_type_vlen(t); 237} 238 239static inline u16 btf_func_linkage(const struct btf_type *t) 240{ 241 return BTF_INFO_VLEN(t->info); 242} 243 244static inline bool btf_type_kflag(const struct btf_type *t) 245{ 246 return BTF_INFO_KFLAG(t->info); 247} 248 249static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type, 250 const struct btf_member *member) 251{ 252 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset) 253 : member->offset; 254} 255 256static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type, 257 const struct btf_member *member) 258{ 259 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset) 260 : 0; 261} 262 263static inline struct btf_member *btf_members(const struct btf_type *t) 264{ 265 return (struct btf_member *)(t + 1); 266} 267 268static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx) 269{ 270 const struct btf_member *m = btf_members(t) + member_idx; 271 272 return __btf_member_bit_offset(t, m); 273} 274 275static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx) 276{ 277 const struct btf_member *m = btf_members(t) + member_idx; 278 279 return __btf_member_bitfield_size(t, m); 280} 281 282static inline const struct btf_member *btf_type_member(const struct btf_type *t) 283{ 284 return (const struct btf_member *)(t + 1); 285} 286 287static inline struct btf_array *btf_array(const struct btf_type *t) 288{ 289 return (struct btf_array *)(t + 1); 290} 291 292static inline struct btf_enum *btf_enum(const struct btf_type *t) 293{ 294 return (struct btf_enum *)(t + 1); 295} 296 297static inline const struct btf_var_secinfo *btf_type_var_secinfo( 298 const struct btf_type *t) 299{ 300 return (const struct btf_var_secinfo *)(t + 1); 301} 302 303#ifdef CONFIG_BPF_SYSCALL 304struct bpf_prog; 305 306const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); 307const char *btf_name_by_offset(const struct btf *btf, u32 offset); 308struct btf *btf_parse_vmlinux(void); 309struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); 310#else 311static inline const struct btf_type *btf_type_by_id(const struct btf *btf, 312 u32 type_id) 313{ 314 return NULL; 315} 316static inline const char *btf_name_by_offset(const struct btf *btf, 317 u32 offset) 318{ 319 return NULL; 320} 321#endif 322 323struct kfunc_btf_id_set { 324 struct list_head list; 325 struct btf_id_set *set; 326 struct module *owner; 327}; 328 329struct kfunc_btf_id_list { 330 struct list_head list; 331 struct mutex mutex; 332}; 333 334#ifdef CONFIG_DEBUG_INFO_BTF_MODULES 335void register_kfunc_btf_id_set(struct kfunc_btf_id_list *l, 336 struct kfunc_btf_id_set *s); 337void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l, 338 struct kfunc_btf_id_set *s); 339bool bpf_check_mod_kfunc_call(struct kfunc_btf_id_list *klist, u32 kfunc_id, 340 struct module *owner); 341 342extern struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list; 343extern struct kfunc_btf_id_list prog_test_kfunc_list; 344#else 345static inline void register_kfunc_btf_id_set(struct kfunc_btf_id_list *l, 346 struct kfunc_btf_id_set *s) 347{ 348} 349static inline void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l, 350 struct kfunc_btf_id_set *s) 351{ 352} 353static inline bool bpf_check_mod_kfunc_call(struct kfunc_btf_id_list *klist, 354 u32 kfunc_id, struct module *owner) 355{ 356 return false; 357} 358 359static struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list __maybe_unused; 360static struct kfunc_btf_id_list prog_test_kfunc_list __maybe_unused; 361#endif 362 363#define DEFINE_KFUNC_BTF_ID_SET(set, name) \ 364 struct kfunc_btf_id_set name = { LIST_HEAD_INIT(name.list), (set), \ 365 THIS_MODULE } 366 367#endif