1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
7 #include <linux/types.h>
8 #include <linux/bpfptr.h>
9 #include <uapi/linux/btf.h>
10 #include <uapi/linux/bpf.h>
12 #define BTF_TYPE_EMIT(type) ((void)(type *)0)
13 #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val)
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.
35 * struct foo *f = alloc_foo(); // Acquire kfunc
36 * struct bar *b = alloc_bar(); // Acquire kfunc
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:
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
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
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 */
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.
60 #define stringify_struct(x) \
61 ({ BUILD_BUG_ON(sizeof(struct x) < 0); \
71 struct btf_kfunc_id_set {
73 struct btf_id_set8 *set;
76 struct btf_id_dtor_kfunc {
81 typedef void (*btf_dtor_kfunc_t)(void *);
83 extern const struct file_operations btf_fops;
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.
96 * In describing "const void *", type_id is "const" and "const"
97 * refers to "void *". The return type will be "void *".
99 * If type_id is a simple "int", then return type will be "int".
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
110 * *type_id and *ret_size will not be changed in the
113 const struct btf_type *btf_type_id_size(const struct btf *btf,
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;
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.
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)
134 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
136 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
137 struct seq_file *m, u64 flags);
140 * Copy len bytes of string representation of obj of BTF type_id into buf.
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)
149 * Return: length that would have been/was copied as per snprintf, or
152 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
153 char *buf, int len, u64 flags);
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,
179 const char *btf_type_str(const struct btf_type *t);
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); \
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); \
191 static inline bool btf_type_is_ptr(const struct btf_type *t)
193 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
196 static inline bool btf_type_is_int(const struct btf_type *t)
198 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
201 static inline bool btf_type_is_small_int(const struct btf_type *t)
203 return btf_type_is_int(t) && t->size <= sizeof(u64);
206 static inline bool btf_type_is_enum(const struct btf_type *t)
208 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
211 static inline bool btf_is_any_enum(const struct btf_type *t)
213 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
214 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64;
217 static inline bool btf_kind_core_compat(const struct btf_type *t1,
218 const struct btf_type *t2)
220 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) ||
221 (btf_is_any_enum(t1) && btf_is_any_enum(t2));
224 static inline bool str_is_empty(const char *s)
229 static inline u16 btf_kind(const struct btf_type *t)
231 return BTF_INFO_KIND(t->info);
234 static inline bool btf_is_enum(const struct btf_type *t)
236 return btf_kind(t) == BTF_KIND_ENUM;
239 static inline bool btf_is_enum64(const struct btf_type *t)
241 return btf_kind(t) == BTF_KIND_ENUM64;
244 static inline u64 btf_enum64_value(const struct btf_enum64 *e)
246 return ((u64)e->val_hi32 << 32) | e->val_lo32;
249 static inline bool btf_is_composite(const struct btf_type *t)
251 u16 kind = btf_kind(t);
253 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
256 static inline bool btf_is_array(const struct btf_type *t)
258 return btf_kind(t) == BTF_KIND_ARRAY;
261 static inline bool btf_is_int(const struct btf_type *t)
263 return btf_kind(t) == BTF_KIND_INT;
266 static inline bool btf_is_ptr(const struct btf_type *t)
268 return btf_kind(t) == BTF_KIND_PTR;
271 static inline u8 btf_int_offset(const struct btf_type *t)
273 return BTF_INT_OFFSET(*(u32 *)(t + 1));
276 static inline u8 btf_int_encoding(const struct btf_type *t)
278 return BTF_INT_ENCODING(*(u32 *)(t + 1));
281 static inline bool btf_type_is_scalar(const struct btf_type *t)
283 return btf_type_is_int(t) || btf_type_is_enum(t);
286 static inline bool btf_type_is_typedef(const struct btf_type *t)
288 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
291 static inline bool btf_type_is_func(const struct btf_type *t)
293 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
296 static inline bool btf_type_is_func_proto(const struct btf_type *t)
298 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
301 static inline bool btf_type_is_var(const struct btf_type *t)
303 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
306 static inline bool btf_type_is_type_tag(const struct btf_type *t)
308 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
311 /* union is only a special case of struct:
312 * all its offsetof(member) == 0
314 static inline bool btf_type_is_struct(const struct btf_type *t)
316 u8 kind = BTF_INFO_KIND(t->info);
318 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
321 static inline u16 btf_type_vlen(const struct btf_type *t)
323 return BTF_INFO_VLEN(t->info);
326 static inline u16 btf_vlen(const struct btf_type *t)
328 return btf_type_vlen(t);
331 static inline u16 btf_func_linkage(const struct btf_type *t)
333 return BTF_INFO_VLEN(t->info);
336 static inline bool btf_type_kflag(const struct btf_type *t)
338 return BTF_INFO_KFLAG(t->info);
341 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
342 const struct btf_member *member)
344 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
348 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
349 const struct btf_member *member)
351 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
355 static inline struct btf_member *btf_members(const struct btf_type *t)
357 return (struct btf_member *)(t + 1);
360 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
362 const struct btf_member *m = btf_members(t) + member_idx;
364 return __btf_member_bit_offset(t, m);
367 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
369 const struct btf_member *m = btf_members(t) + member_idx;
371 return __btf_member_bitfield_size(t, m);
374 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
376 return (const struct btf_member *)(t + 1);
379 static inline struct btf_array *btf_array(const struct btf_type *t)
381 return (struct btf_array *)(t + 1);
384 static inline struct btf_enum *btf_enum(const struct btf_type *t)
386 return (struct btf_enum *)(t + 1);
389 static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
391 return (struct btf_enum64 *)(t + 1);
394 static inline const struct btf_var_secinfo *btf_type_var_secinfo(
395 const struct btf_type *t)
397 return (const struct btf_var_secinfo *)(t + 1);
400 static inline struct btf_param *btf_params(const struct btf_type *t)
402 return (struct btf_param *)(t + 1);
405 #ifdef CONFIG_BPF_SYSCALL
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,
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);
421 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
426 static inline const char *btf_name_by_offset(const struct btf *btf,
431 static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf,
432 enum bpf_prog_type prog_type,
437 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
438 const struct btf_kfunc_id_set *s)
442 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
446 static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
447 u32 add_cnt, struct module *owner)
453 static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t)
455 if (!btf_type_is_ptr(t))
458 t = btf_type_skip_modifiers(btf, t->type, NULL);
460 return btf_type_is_struct(t);