0001
0002
0003
0004 #ifndef _LINUX_BTF_H
0005 #define _LINUX_BTF_H 1
0006
0007 #include <linux/types.h>
0008 #include <linux/bpfptr.h>
0009 #include <uapi/linux/btf.h>
0010 #include <uapi/linux/bpf.h>
0011
0012 #define BTF_TYPE_EMIT(type) ((void)(type *)0)
0013 #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val)
0014
0015
0016 #define KF_ACQUIRE (1 << 0)
0017 #define KF_RELEASE (1 << 1)
0018 #define KF_RET_NULL (1 << 2)
0019 #define KF_KPTR_GET (1 << 3)
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051 #define KF_TRUSTED_ARGS (1 << 4)
0052
0053 struct btf;
0054 struct btf_member;
0055 struct btf_type;
0056 union bpf_attr;
0057 struct btf_show;
0058 struct btf_id_set;
0059
0060 struct btf_kfunc_id_set {
0061 struct module *owner;
0062 struct btf_id_set8 *set;
0063 };
0064
0065 struct btf_id_dtor_kfunc {
0066 u32 btf_id;
0067 u32 kfunc_btf_id;
0068 };
0069
0070 typedef void (*btf_dtor_kfunc_t)(void *);
0071
0072 extern const struct file_operations btf_fops;
0073
0074 void btf_get(struct btf *btf);
0075 void btf_put(struct btf *btf);
0076 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr);
0077 struct btf *btf_get_by_fd(int fd);
0078 int btf_get_info_by_fd(const struct btf *btf,
0079 const union bpf_attr *attr,
0080 union bpf_attr __user *uattr);
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102 const struct btf_type *btf_type_id_size(const struct btf *btf,
0103 u32 *type_id,
0104 u32 *ret_size);
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117 #define BTF_SHOW_COMPACT BTF_F_COMPACT
0118 #define BTF_SHOW_NONAME BTF_F_NONAME
0119 #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW
0120 #define BTF_SHOW_ZERO BTF_F_ZERO
0121 #define BTF_SHOW_UNSAFE (1ULL << 4)
0122
0123 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
0124 struct seq_file *m);
0125 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
0126 struct seq_file *m, u64 flags);
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
0142 char *buf, int len, u64 flags);
0143
0144 int btf_get_fd_by_id(u32 id);
0145 u32 btf_obj_id(const struct btf *btf);
0146 bool btf_is_kernel(const struct btf *btf);
0147 bool btf_is_module(const struct btf *btf);
0148 struct module *btf_try_get_module(const struct btf *btf);
0149 u32 btf_nr_types(const struct btf *btf);
0150 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
0151 const struct btf_member *m,
0152 u32 expected_offset, u32 expected_size);
0153 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
0154 int btf_find_timer(const struct btf *btf, const struct btf_type *t);
0155 struct bpf_map_value_off *btf_parse_kptrs(const struct btf *btf,
0156 const struct btf_type *t);
0157 bool btf_type_is_void(const struct btf_type *t);
0158 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
0159 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
0160 u32 id, u32 *res_id);
0161 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
0162 u32 id, u32 *res_id);
0163 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
0164 u32 id, u32 *res_id);
0165 const struct btf_type *
0166 btf_resolve_size(const struct btf *btf, const struct btf_type *type,
0167 u32 *type_size);
0168 const char *btf_type_str(const struct btf_type *t);
0169
0170 #define for_each_member(i, struct_type, member) \
0171 for (i = 0, member = btf_type_member(struct_type); \
0172 i < btf_type_vlen(struct_type); \
0173 i++, member++)
0174
0175 #define for_each_vsi(i, datasec_type, member) \
0176 for (i = 0, member = btf_type_var_secinfo(datasec_type); \
0177 i < btf_type_vlen(datasec_type); \
0178 i++, member++)
0179
0180 static inline bool btf_type_is_ptr(const struct btf_type *t)
0181 {
0182 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
0183 }
0184
0185 static inline bool btf_type_is_int(const struct btf_type *t)
0186 {
0187 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
0188 }
0189
0190 static inline bool btf_type_is_small_int(const struct btf_type *t)
0191 {
0192 return btf_type_is_int(t) && t->size <= sizeof(u64);
0193 }
0194
0195 static inline bool btf_type_is_enum(const struct btf_type *t)
0196 {
0197 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
0198 }
0199
0200 static inline bool btf_is_any_enum(const struct btf_type *t)
0201 {
0202 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
0203 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64;
0204 }
0205
0206 static inline bool btf_kind_core_compat(const struct btf_type *t1,
0207 const struct btf_type *t2)
0208 {
0209 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) ||
0210 (btf_is_any_enum(t1) && btf_is_any_enum(t2));
0211 }
0212
0213 static inline bool str_is_empty(const char *s)
0214 {
0215 return !s || !s[0];
0216 }
0217
0218 static inline u16 btf_kind(const struct btf_type *t)
0219 {
0220 return BTF_INFO_KIND(t->info);
0221 }
0222
0223 static inline bool btf_is_enum(const struct btf_type *t)
0224 {
0225 return btf_kind(t) == BTF_KIND_ENUM;
0226 }
0227
0228 static inline bool btf_is_enum64(const struct btf_type *t)
0229 {
0230 return btf_kind(t) == BTF_KIND_ENUM64;
0231 }
0232
0233 static inline u64 btf_enum64_value(const struct btf_enum64 *e)
0234 {
0235 return ((u64)e->val_hi32 << 32) | e->val_lo32;
0236 }
0237
0238 static inline bool btf_is_composite(const struct btf_type *t)
0239 {
0240 u16 kind = btf_kind(t);
0241
0242 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
0243 }
0244
0245 static inline bool btf_is_array(const struct btf_type *t)
0246 {
0247 return btf_kind(t) == BTF_KIND_ARRAY;
0248 }
0249
0250 static inline bool btf_is_int(const struct btf_type *t)
0251 {
0252 return btf_kind(t) == BTF_KIND_INT;
0253 }
0254
0255 static inline bool btf_is_ptr(const struct btf_type *t)
0256 {
0257 return btf_kind(t) == BTF_KIND_PTR;
0258 }
0259
0260 static inline u8 btf_int_offset(const struct btf_type *t)
0261 {
0262 return BTF_INT_OFFSET(*(u32 *)(t + 1));
0263 }
0264
0265 static inline u8 btf_int_encoding(const struct btf_type *t)
0266 {
0267 return BTF_INT_ENCODING(*(u32 *)(t + 1));
0268 }
0269
0270 static inline bool btf_type_is_scalar(const struct btf_type *t)
0271 {
0272 return btf_type_is_int(t) || btf_type_is_enum(t);
0273 }
0274
0275 static inline bool btf_type_is_typedef(const struct btf_type *t)
0276 {
0277 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
0278 }
0279
0280 static inline bool btf_type_is_func(const struct btf_type *t)
0281 {
0282 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
0283 }
0284
0285 static inline bool btf_type_is_func_proto(const struct btf_type *t)
0286 {
0287 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
0288 }
0289
0290 static inline bool btf_type_is_var(const struct btf_type *t)
0291 {
0292 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
0293 }
0294
0295 static inline bool btf_type_is_type_tag(const struct btf_type *t)
0296 {
0297 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
0298 }
0299
0300
0301
0302
0303 static inline bool btf_type_is_struct(const struct btf_type *t)
0304 {
0305 u8 kind = BTF_INFO_KIND(t->info);
0306
0307 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
0308 }
0309
0310 static inline u16 btf_type_vlen(const struct btf_type *t)
0311 {
0312 return BTF_INFO_VLEN(t->info);
0313 }
0314
0315 static inline u16 btf_vlen(const struct btf_type *t)
0316 {
0317 return btf_type_vlen(t);
0318 }
0319
0320 static inline u16 btf_func_linkage(const struct btf_type *t)
0321 {
0322 return BTF_INFO_VLEN(t->info);
0323 }
0324
0325 static inline bool btf_type_kflag(const struct btf_type *t)
0326 {
0327 return BTF_INFO_KFLAG(t->info);
0328 }
0329
0330 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
0331 const struct btf_member *member)
0332 {
0333 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
0334 : member->offset;
0335 }
0336
0337 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
0338 const struct btf_member *member)
0339 {
0340 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
0341 : 0;
0342 }
0343
0344 static inline struct btf_member *btf_members(const struct btf_type *t)
0345 {
0346 return (struct btf_member *)(t + 1);
0347 }
0348
0349 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
0350 {
0351 const struct btf_member *m = btf_members(t) + member_idx;
0352
0353 return __btf_member_bit_offset(t, m);
0354 }
0355
0356 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
0357 {
0358 const struct btf_member *m = btf_members(t) + member_idx;
0359
0360 return __btf_member_bitfield_size(t, m);
0361 }
0362
0363 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
0364 {
0365 return (const struct btf_member *)(t + 1);
0366 }
0367
0368 static inline struct btf_array *btf_array(const struct btf_type *t)
0369 {
0370 return (struct btf_array *)(t + 1);
0371 }
0372
0373 static inline struct btf_enum *btf_enum(const struct btf_type *t)
0374 {
0375 return (struct btf_enum *)(t + 1);
0376 }
0377
0378 static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
0379 {
0380 return (struct btf_enum64 *)(t + 1);
0381 }
0382
0383 static inline const struct btf_var_secinfo *btf_type_var_secinfo(
0384 const struct btf_type *t)
0385 {
0386 return (const struct btf_var_secinfo *)(t + 1);
0387 }
0388
0389 static inline struct btf_param *btf_params(const struct btf_type *t)
0390 {
0391 return (struct btf_param *)(t + 1);
0392 }
0393
0394 #ifdef CONFIG_BPF_SYSCALL
0395 struct bpf_prog;
0396
0397 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
0398 const char *btf_name_by_offset(const struct btf *btf, u32 offset);
0399 struct btf *btf_parse_vmlinux(void);
0400 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
0401 u32 *btf_kfunc_id_set_contains(const struct btf *btf,
0402 enum bpf_prog_type prog_type,
0403 u32 kfunc_btf_id);
0404 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
0405 const struct btf_kfunc_id_set *s);
0406 s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
0407 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
0408 struct module *owner);
0409 #else
0410 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
0411 u32 type_id)
0412 {
0413 return NULL;
0414 }
0415 static inline const char *btf_name_by_offset(const struct btf *btf,
0416 u32 offset)
0417 {
0418 return NULL;
0419 }
0420 static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf,
0421 enum bpf_prog_type prog_type,
0422 u32 kfunc_btf_id)
0423 {
0424 return NULL;
0425 }
0426 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
0427 const struct btf_kfunc_id_set *s)
0428 {
0429 return 0;
0430 }
0431 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
0432 {
0433 return -ENOENT;
0434 }
0435 static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
0436 u32 add_cnt, struct module *owner)
0437 {
0438 return 0;
0439 }
0440 #endif
0441
0442 #endif