1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
7 #include <linux/types.h>
8 #include <linux/bpfptr.h>
9 #include <linux/bsearch.h>
10 #include <linux/btf_ids.h>
11 #include <uapi/linux/btf.h>
12 #include <uapi/linux/bpf.h>
14 #define BTF_TYPE_EMIT(type) ((void)(type *)0)
15 #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val)
17 /* These need to be macros, as the expressions are used in assembler input */
18 #define KF_ACQUIRE (1 << 0) /* kfunc is an acquire function */
19 #define KF_RELEASE (1 << 1) /* kfunc is a release function */
20 #define KF_RET_NULL (1 << 2) /* kfunc returns a pointer that may be NULL */
21 /* Trusted arguments are those which are guaranteed to be valid when passed to
22 * the kfunc. It is used to enforce that pointers obtained from either acquire
23 * kfuncs, or from the main kernel on a tracepoint or struct_ops callback
24 * invocation, remain unmodified when being passed to helpers taking trusted
27 * Consider, for example, the following new task tracepoint:
29 * SEC("tp_btf/task_newtask")
30 * int BPF_PROG(new_task_tp, struct task_struct *task, u64 clone_flags)
35 * And the following kfunc:
37 * BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS)
39 * All invocations to the kfunc must pass the unmodified, unwalked task:
41 * bpf_task_acquire(task); // Allowed
42 * bpf_task_acquire(task->last_wakee); // Rejected, walked task
44 * Programs may also pass referenced tasks directly to the kfunc:
46 * struct task_struct *acquired;
48 * acquired = bpf_task_acquire(task); // Allowed, same as above
49 * bpf_task_acquire(acquired); // Allowed
50 * bpf_task_acquire(task); // Allowed
51 * bpf_task_acquire(acquired->last_wakee); // Rejected, walked task
53 * Programs may _not_, however, pass a task from an arbitrary fentry/fexit, or
54 * kprobe/kretprobe to the kfunc, as BPF cannot guarantee that all of these
55 * pointers are guaranteed to be safe. For example, the following BPF program
58 * SEC("kretprobe/free_task")
59 * int BPF_PROG(free_task_probe, struct task_struct *tsk)
61 * struct task_struct *acquired;
63 * acquired = bpf_task_acquire(acquired); // Rejected, not a trusted pointer
64 * bpf_task_release(acquired);
69 #define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
70 #define KF_SLEEPABLE (1 << 5) /* kfunc may sleep */
71 #define KF_DESTRUCTIVE (1 << 6) /* kfunc performs destructive actions */
72 #define KF_RCU (1 << 7) /* kfunc takes either rcu or trusted pointer arguments */
73 /* only one of KF_ITER_{NEW,NEXT,DESTROY} could be specified per kfunc */
74 #define KF_ITER_NEW (1 << 8) /* kfunc implements BPF iter constructor */
75 #define KF_ITER_NEXT (1 << 9) /* kfunc implements BPF iter next method */
76 #define KF_ITER_DESTROY (1 << 10) /* kfunc implements BPF iter destructor */
77 #define KF_RCU_PROTECTED (1 << 11) /* kfunc should be protected by rcu cs when they are invoked */
80 * Tag marking a kernel function as a kfunc. This is meant to minimize the
81 * amount of copy-paste that kfunc authors have to include for correctness so
82 * as to avoid issues such as the compiler inlining or eliding either a static
83 * kfunc, or a global kfunc in an LTO build.
85 #define __bpf_kfunc __used noinline
88 * Return the name of the passed struct, if exists, or halt the build if for
89 * example the structure gets renamed. In this way, developers have to revisit
90 * the code using that structure name, and update it accordingly.
92 #define stringify_struct(x) \
93 ({ BUILD_BUG_ON(sizeof(struct x) < 0); \
104 typedef int (*btf_kfunc_filter_t)(const struct bpf_prog *prog, u32 kfunc_id);
106 struct btf_kfunc_id_set {
107 struct module *owner;
108 struct btf_id_set8 *set;
109 btf_kfunc_filter_t filter;
112 struct btf_id_dtor_kfunc {
117 struct btf_struct_meta {
119 struct btf_record *record;
122 struct btf_struct_metas {
124 struct btf_struct_meta types[];
127 extern const struct file_operations btf_fops;
129 void btf_get(struct btf *btf);
130 void btf_put(struct btf *btf);
131 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_sz);
132 struct btf *btf_get_by_fd(int fd);
133 int btf_get_info_by_fd(const struct btf *btf,
134 const union bpf_attr *attr,
135 union bpf_attr __user *uattr);
136 /* Figure out the size of a type_id. If type_id is a modifier
137 * (e.g. const), it will be resolved to find out the type with size.
140 * In describing "const void *", type_id is "const" and "const"
141 * refers to "void *". The return type will be "void *".
143 * If type_id is a simple "int", then return type will be "int".
145 * @btf: struct btf object
146 * @type_id: Find out the size of type_id. The type_id of the return
147 * type is set to *type_id.
148 * @ret_size: It can be NULL. If not NULL, the size of the return
149 * type is set to *ret_size.
150 * Return: The btf_type (resolved to another type with size info if needed).
151 * NULL is returned if type_id itself does not have size info
152 * (e.g. void) or it cannot be resolved to another type that
154 * *type_id and *ret_size will not be changed in the
157 const struct btf_type *btf_type_id_size(const struct btf *btf,
162 * Options to control show behaviour.
163 * - BTF_SHOW_COMPACT: no formatting around type information
164 * - BTF_SHOW_NONAME: no struct/union member names/types
165 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
167 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they
168 * are not displayed by default
169 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
170 * data before displaying it.
172 #define BTF_SHOW_COMPACT BTF_F_COMPACT
173 #define BTF_SHOW_NONAME BTF_F_NONAME
174 #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW
175 #define BTF_SHOW_ZERO BTF_F_ZERO
176 #define BTF_SHOW_UNSAFE (1ULL << 4)
178 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
180 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
181 struct seq_file *m, u64 flags);
184 * Copy len bytes of string representation of obj of BTF type_id into buf.
186 * @btf: struct btf object
187 * @type_id: type id of type obj points to
188 * @obj: pointer to typed data
189 * @buf: buffer to write to
190 * @len: maximum length to write to buf
191 * @flags: show options (see above)
193 * Return: length that would have been/was copied as per snprintf, or
196 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
197 char *buf, int len, u64 flags);
199 int btf_get_fd_by_id(u32 id);
200 u32 btf_obj_id(const struct btf *btf);
201 bool btf_is_kernel(const struct btf *btf);
202 bool btf_is_module(const struct btf *btf);
203 struct module *btf_try_get_module(const struct btf *btf);
204 u32 btf_nr_types(const struct btf *btf);
205 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
206 const struct btf_member *m,
207 u32 expected_offset, u32 expected_size);
208 struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t,
209 u32 field_mask, u32 value_size);
210 int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec);
211 bool btf_type_is_void(const struct btf_type *t);
212 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
213 s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p);
214 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
215 u32 id, u32 *res_id);
216 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
217 u32 id, u32 *res_id);
218 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
219 u32 id, u32 *res_id);
220 const struct btf_type *
221 btf_resolve_size(const struct btf *btf, const struct btf_type *type,
223 const char *btf_type_str(const struct btf_type *t);
225 #define for_each_member(i, struct_type, member) \
226 for (i = 0, member = btf_type_member(struct_type); \
227 i < btf_type_vlen(struct_type); \
230 #define for_each_vsi(i, datasec_type, member) \
231 for (i = 0, member = btf_type_var_secinfo(datasec_type); \
232 i < btf_type_vlen(datasec_type); \
235 static inline bool btf_type_is_ptr(const struct btf_type *t)
237 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
240 static inline bool btf_type_is_int(const struct btf_type *t)
242 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
245 static inline bool btf_type_is_small_int(const struct btf_type *t)
247 return btf_type_is_int(t) && t->size <= sizeof(u64);
250 static inline u8 btf_int_encoding(const struct btf_type *t)
252 return BTF_INT_ENCODING(*(u32 *)(t + 1));
255 static inline bool btf_type_is_signed_int(const struct btf_type *t)
257 return btf_type_is_int(t) && (btf_int_encoding(t) & BTF_INT_SIGNED);
260 static inline bool btf_type_is_enum(const struct btf_type *t)
262 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
265 static inline bool btf_is_any_enum(const struct btf_type *t)
267 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
268 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64;
271 static inline bool btf_kind_core_compat(const struct btf_type *t1,
272 const struct btf_type *t2)
274 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) ||
275 (btf_is_any_enum(t1) && btf_is_any_enum(t2));
278 static inline bool str_is_empty(const char *s)
283 static inline u16 btf_kind(const struct btf_type *t)
285 return BTF_INFO_KIND(t->info);
288 static inline bool btf_is_enum(const struct btf_type *t)
290 return btf_kind(t) == BTF_KIND_ENUM;
293 static inline bool btf_is_enum64(const struct btf_type *t)
295 return btf_kind(t) == BTF_KIND_ENUM64;
298 static inline u64 btf_enum64_value(const struct btf_enum64 *e)
300 return ((u64)e->val_hi32 << 32) | e->val_lo32;
303 static inline bool btf_is_composite(const struct btf_type *t)
305 u16 kind = btf_kind(t);
307 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
310 static inline bool btf_is_array(const struct btf_type *t)
312 return btf_kind(t) == BTF_KIND_ARRAY;
315 static inline bool btf_is_int(const struct btf_type *t)
317 return btf_kind(t) == BTF_KIND_INT;
320 static inline bool btf_is_ptr(const struct btf_type *t)
322 return btf_kind(t) == BTF_KIND_PTR;
325 static inline u8 btf_int_offset(const struct btf_type *t)
327 return BTF_INT_OFFSET(*(u32 *)(t + 1));
330 static inline bool btf_type_is_scalar(const struct btf_type *t)
332 return btf_type_is_int(t) || btf_type_is_enum(t);
335 static inline bool btf_type_is_typedef(const struct btf_type *t)
337 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
340 static inline bool btf_type_is_volatile(const struct btf_type *t)
342 return BTF_INFO_KIND(t->info) == BTF_KIND_VOLATILE;
345 static inline bool btf_type_is_func(const struct btf_type *t)
347 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
350 static inline bool btf_type_is_func_proto(const struct btf_type *t)
352 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
355 static inline bool btf_type_is_var(const struct btf_type *t)
357 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
360 static inline bool btf_type_is_type_tag(const struct btf_type *t)
362 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
365 /* union is only a special case of struct:
366 * all its offsetof(member) == 0
368 static inline bool btf_type_is_struct(const struct btf_type *t)
370 u8 kind = BTF_INFO_KIND(t->info);
372 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
375 static inline bool __btf_type_is_struct(const struct btf_type *t)
377 return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT;
380 static inline bool btf_type_is_array(const struct btf_type *t)
382 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
385 static inline u16 btf_type_vlen(const struct btf_type *t)
387 return BTF_INFO_VLEN(t->info);
390 static inline u16 btf_vlen(const struct btf_type *t)
392 return btf_type_vlen(t);
395 static inline u16 btf_func_linkage(const struct btf_type *t)
397 return BTF_INFO_VLEN(t->info);
400 static inline bool btf_type_kflag(const struct btf_type *t)
402 return BTF_INFO_KFLAG(t->info);
405 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
406 const struct btf_member *member)
408 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
412 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
413 const struct btf_member *member)
415 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
419 static inline struct btf_member *btf_members(const struct btf_type *t)
421 return (struct btf_member *)(t + 1);
424 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
426 const struct btf_member *m = btf_members(t) + member_idx;
428 return __btf_member_bit_offset(t, m);
431 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
433 const struct btf_member *m = btf_members(t) + member_idx;
435 return __btf_member_bitfield_size(t, m);
438 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
440 return (const struct btf_member *)(t + 1);
443 static inline struct btf_array *btf_array(const struct btf_type *t)
445 return (struct btf_array *)(t + 1);
448 static inline struct btf_enum *btf_enum(const struct btf_type *t)
450 return (struct btf_enum *)(t + 1);
453 static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
455 return (struct btf_enum64 *)(t + 1);
458 static inline const struct btf_var_secinfo *btf_type_var_secinfo(
459 const struct btf_type *t)
461 return (const struct btf_var_secinfo *)(t + 1);
464 static inline struct btf_param *btf_params(const struct btf_type *t)
466 return (struct btf_param *)(t + 1);
469 static inline int btf_id_cmp_func(const void *a, const void *b)
471 const int *pa = a, *pb = b;
476 static inline bool btf_id_set_contains(const struct btf_id_set *set, u32 id)
478 return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL;
481 static inline void *btf_id_set8_contains(const struct btf_id_set8 *set, u32 id)
483 return bsearch(&id, set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func);
486 struct bpf_verifier_log;
488 #ifdef CONFIG_BPF_SYSCALL
489 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
490 const char *btf_name_by_offset(const struct btf *btf, u32 offset);
491 struct btf *btf_parse_vmlinux(void);
492 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
493 u32 *btf_kfunc_id_set_contains(const struct btf *btf, u32 kfunc_btf_id,
494 const struct bpf_prog *prog);
495 u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id,
496 const struct bpf_prog *prog);
497 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
498 const struct btf_kfunc_id_set *s);
499 int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset);
500 s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
501 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
502 struct module *owner);
503 struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id);
504 const struct btf_member *
505 btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
506 const struct btf_type *t, enum bpf_prog_type prog_type,
508 int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type);
509 bool btf_types_are_same(const struct btf *btf1, u32 id1,
510 const struct btf *btf2, u32 id2);
512 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
517 static inline const char *btf_name_by_offset(const struct btf *btf,
522 static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf,
524 struct bpf_prog *prog)
529 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
530 const struct btf_kfunc_id_set *s)
534 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
538 static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
539 u32 add_cnt, struct module *owner)
543 static inline struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id)
547 static inline const struct btf_member *
548 btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
549 const struct btf_type *t, enum bpf_prog_type prog_type,
554 static inline int get_kern_ctx_btf_id(struct bpf_verifier_log *log,
555 enum bpf_prog_type prog_type) {
558 static inline bool btf_types_are_same(const struct btf *btf1, u32 id1,
559 const struct btf *btf2, u32 id2)
565 static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t)
567 if (!btf_type_is_ptr(t))
570 t = btf_type_skip_modifiers(btf, t->type, NULL);
572 return btf_type_is_struct(t);