Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 <linux/bsearch.h>
10#include <linux/btf_ids.h>
11#include <uapi/linux/btf.h>
12#include <uapi/linux/bpf.h>
13
14#define BTF_TYPE_EMIT(type) ((void)(type *)0)
15#define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val)
16
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#define KF_KPTR_GET (1 << 3) /* kfunc returns reference to a kptr */
22/* Trusted arguments are those which are guaranteed to be valid when passed to
23 * the kfunc. It is used to enforce that pointers obtained from either acquire
24 * kfuncs, or from the main kernel on a tracepoint or struct_ops callback
25 * invocation, remain unmodified when being passed to helpers taking trusted
26 * args.
27 *
28 * Consider, for example, the following new task tracepoint:
29 *
30 * SEC("tp_btf/task_newtask")
31 * int BPF_PROG(new_task_tp, struct task_struct *task, u64 clone_flags)
32 * {
33 * ...
34 * }
35 *
36 * And the following kfunc:
37 *
38 * BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS)
39 *
40 * All invocations to the kfunc must pass the unmodified, unwalked task:
41 *
42 * bpf_task_acquire(task); // Allowed
43 * bpf_task_acquire(task->last_wakee); // Rejected, walked task
44 *
45 * Programs may also pass referenced tasks directly to the kfunc:
46 *
47 * struct task_struct *acquired;
48 *
49 * acquired = bpf_task_acquire(task); // Allowed, same as above
50 * bpf_task_acquire(acquired); // Allowed
51 * bpf_task_acquire(task); // Allowed
52 * bpf_task_acquire(acquired->last_wakee); // Rejected, walked task
53 *
54 * Programs may _not_, however, pass a task from an arbitrary fentry/fexit, or
55 * kprobe/kretprobe to the kfunc, as BPF cannot guarantee that all of these
56 * pointers are guaranteed to be safe. For example, the following BPF program
57 * would be rejected:
58 *
59 * SEC("kretprobe/free_task")
60 * int BPF_PROG(free_task_probe, struct task_struct *tsk)
61 * {
62 * struct task_struct *acquired;
63 *
64 * acquired = bpf_task_acquire(acquired); // Rejected, not a trusted pointer
65 * bpf_task_release(acquired);
66 *
67 * return 0;
68 * }
69 */
70#define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
71#define KF_SLEEPABLE (1 << 5) /* kfunc may sleep */
72#define KF_DESTRUCTIVE (1 << 6) /* kfunc performs destructive actions */
73#define KF_RCU (1 << 7) /* kfunc only takes rcu pointer arguments */
74
75/*
76 * Tag marking a kernel function as a kfunc. This is meant to minimize the
77 * amount of copy-paste that kfunc authors have to include for correctness so
78 * as to avoid issues such as the compiler inlining or eliding either a static
79 * kfunc, or a global kfunc in an LTO build.
80 */
81#define __bpf_kfunc __used noinline
82
83/*
84 * Return the name of the passed struct, if exists, or halt the build if for
85 * example the structure gets renamed. In this way, developers have to revisit
86 * the code using that structure name, and update it accordingly.
87 */
88#define stringify_struct(x) \
89 ({ BUILD_BUG_ON(sizeof(struct x) < 0); \
90 __stringify(x); })
91
92struct btf;
93struct btf_member;
94struct btf_type;
95union bpf_attr;
96struct btf_show;
97struct btf_id_set;
98
99struct btf_kfunc_id_set {
100 struct module *owner;
101 struct btf_id_set8 *set;
102};
103
104struct btf_id_dtor_kfunc {
105 u32 btf_id;
106 u32 kfunc_btf_id;
107};
108
109struct btf_struct_meta {
110 u32 btf_id;
111 struct btf_record *record;
112 struct btf_field_offs *field_offs;
113};
114
115struct btf_struct_metas {
116 u32 cnt;
117 struct btf_struct_meta types[];
118};
119
120typedef void (*btf_dtor_kfunc_t)(void *);
121
122extern const struct file_operations btf_fops;
123
124void btf_get(struct btf *btf);
125void btf_put(struct btf *btf);
126int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr);
127struct btf *btf_get_by_fd(int fd);
128int btf_get_info_by_fd(const struct btf *btf,
129 const union bpf_attr *attr,
130 union bpf_attr __user *uattr);
131/* Figure out the size of a type_id. If type_id is a modifier
132 * (e.g. const), it will be resolved to find out the type with size.
133 *
134 * For example:
135 * In describing "const void *", type_id is "const" and "const"
136 * refers to "void *". The return type will be "void *".
137 *
138 * If type_id is a simple "int", then return type will be "int".
139 *
140 * @btf: struct btf object
141 * @type_id: Find out the size of type_id. The type_id of the return
142 * type is set to *type_id.
143 * @ret_size: It can be NULL. If not NULL, the size of the return
144 * type is set to *ret_size.
145 * Return: The btf_type (resolved to another type with size info if needed).
146 * NULL is returned if type_id itself does not have size info
147 * (e.g. void) or it cannot be resolved to another type that
148 * has size info.
149 * *type_id and *ret_size will not be changed in the
150 * NULL return case.
151 */
152const struct btf_type *btf_type_id_size(const struct btf *btf,
153 u32 *type_id,
154 u32 *ret_size);
155
156/*
157 * Options to control show behaviour.
158 * - BTF_SHOW_COMPACT: no formatting around type information
159 * - BTF_SHOW_NONAME: no struct/union member names/types
160 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
161 * equivalent to %px.
162 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they
163 * are not displayed by default
164 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
165 * data before displaying it.
166 */
167#define BTF_SHOW_COMPACT BTF_F_COMPACT
168#define BTF_SHOW_NONAME BTF_F_NONAME
169#define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW
170#define BTF_SHOW_ZERO BTF_F_ZERO
171#define BTF_SHOW_UNSAFE (1ULL << 4)
172
173void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
174 struct seq_file *m);
175int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
176 struct seq_file *m, u64 flags);
177
178/*
179 * Copy len bytes of string representation of obj of BTF type_id into buf.
180 *
181 * @btf: struct btf object
182 * @type_id: type id of type obj points to
183 * @obj: pointer to typed data
184 * @buf: buffer to write to
185 * @len: maximum length to write to buf
186 * @flags: show options (see above)
187 *
188 * Return: length that would have been/was copied as per snprintf, or
189 * negative error.
190 */
191int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
192 char *buf, int len, u64 flags);
193
194int btf_get_fd_by_id(u32 id);
195u32 btf_obj_id(const struct btf *btf);
196bool btf_is_kernel(const struct btf *btf);
197bool btf_is_module(const struct btf *btf);
198struct module *btf_try_get_module(const struct btf *btf);
199u32 btf_nr_types(const struct btf *btf);
200bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
201 const struct btf_member *m,
202 u32 expected_offset, u32 expected_size);
203int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
204int btf_find_timer(const struct btf *btf, const struct btf_type *t);
205struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t,
206 u32 field_mask, u32 value_size);
207int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec);
208struct btf_field_offs *btf_parse_field_offs(struct btf_record *rec);
209bool btf_type_is_void(const struct btf_type *t);
210s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
211const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
212 u32 id, u32 *res_id);
213const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
214 u32 id, u32 *res_id);
215const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
216 u32 id, u32 *res_id);
217const struct btf_type *
218btf_resolve_size(const struct btf *btf, const struct btf_type *type,
219 u32 *type_size);
220const char *btf_type_str(const struct btf_type *t);
221
222#define for_each_member(i, struct_type, member) \
223 for (i = 0, member = btf_type_member(struct_type); \
224 i < btf_type_vlen(struct_type); \
225 i++, member++)
226
227#define for_each_vsi(i, datasec_type, member) \
228 for (i = 0, member = btf_type_var_secinfo(datasec_type); \
229 i < btf_type_vlen(datasec_type); \
230 i++, member++)
231
232static inline bool btf_type_is_ptr(const struct btf_type *t)
233{
234 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
235}
236
237static inline bool btf_type_is_int(const struct btf_type *t)
238{
239 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
240}
241
242static inline bool btf_type_is_small_int(const struct btf_type *t)
243{
244 return btf_type_is_int(t) && t->size <= sizeof(u64);
245}
246
247static inline u8 btf_int_encoding(const struct btf_type *t)
248{
249 return BTF_INT_ENCODING(*(u32 *)(t + 1));
250}
251
252static inline bool btf_type_is_signed_int(const struct btf_type *t)
253{
254 return btf_type_is_int(t) && (btf_int_encoding(t) & BTF_INT_SIGNED);
255}
256
257static inline bool btf_type_is_enum(const struct btf_type *t)
258{
259 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
260}
261
262static inline bool btf_is_any_enum(const struct btf_type *t)
263{
264 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
265 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64;
266}
267
268static inline bool btf_kind_core_compat(const struct btf_type *t1,
269 const struct btf_type *t2)
270{
271 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) ||
272 (btf_is_any_enum(t1) && btf_is_any_enum(t2));
273}
274
275static inline bool str_is_empty(const char *s)
276{
277 return !s || !s[0];
278}
279
280static inline u16 btf_kind(const struct btf_type *t)
281{
282 return BTF_INFO_KIND(t->info);
283}
284
285static inline bool btf_is_enum(const struct btf_type *t)
286{
287 return btf_kind(t) == BTF_KIND_ENUM;
288}
289
290static inline bool btf_is_enum64(const struct btf_type *t)
291{
292 return btf_kind(t) == BTF_KIND_ENUM64;
293}
294
295static inline u64 btf_enum64_value(const struct btf_enum64 *e)
296{
297 return ((u64)e->val_hi32 << 32) | e->val_lo32;
298}
299
300static inline bool btf_is_composite(const struct btf_type *t)
301{
302 u16 kind = btf_kind(t);
303
304 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
305}
306
307static inline bool btf_is_array(const struct btf_type *t)
308{
309 return btf_kind(t) == BTF_KIND_ARRAY;
310}
311
312static inline bool btf_is_int(const struct btf_type *t)
313{
314 return btf_kind(t) == BTF_KIND_INT;
315}
316
317static inline bool btf_is_ptr(const struct btf_type *t)
318{
319 return btf_kind(t) == BTF_KIND_PTR;
320}
321
322static inline u8 btf_int_offset(const struct btf_type *t)
323{
324 return BTF_INT_OFFSET(*(u32 *)(t + 1));
325}
326
327static inline bool btf_type_is_scalar(const struct btf_type *t)
328{
329 return btf_type_is_int(t) || btf_type_is_enum(t);
330}
331
332static inline bool btf_type_is_typedef(const struct btf_type *t)
333{
334 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
335}
336
337static inline bool btf_type_is_volatile(const struct btf_type *t)
338{
339 return BTF_INFO_KIND(t->info) == BTF_KIND_VOLATILE;
340}
341
342static inline bool btf_type_is_func(const struct btf_type *t)
343{
344 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
345}
346
347static inline bool btf_type_is_func_proto(const struct btf_type *t)
348{
349 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
350}
351
352static inline bool btf_type_is_var(const struct btf_type *t)
353{
354 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
355}
356
357static inline bool btf_type_is_type_tag(const struct btf_type *t)
358{
359 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
360}
361
362/* union is only a special case of struct:
363 * all its offsetof(member) == 0
364 */
365static inline bool btf_type_is_struct(const struct btf_type *t)
366{
367 u8 kind = BTF_INFO_KIND(t->info);
368
369 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
370}
371
372static inline bool __btf_type_is_struct(const struct btf_type *t)
373{
374 return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT;
375}
376
377static inline bool btf_type_is_array(const struct btf_type *t)
378{
379 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
380}
381
382static inline u16 btf_type_vlen(const struct btf_type *t)
383{
384 return BTF_INFO_VLEN(t->info);
385}
386
387static inline u16 btf_vlen(const struct btf_type *t)
388{
389 return btf_type_vlen(t);
390}
391
392static inline u16 btf_func_linkage(const struct btf_type *t)
393{
394 return BTF_INFO_VLEN(t->info);
395}
396
397static inline bool btf_type_kflag(const struct btf_type *t)
398{
399 return BTF_INFO_KFLAG(t->info);
400}
401
402static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
403 const struct btf_member *member)
404{
405 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
406 : member->offset;
407}
408
409static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
410 const struct btf_member *member)
411{
412 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
413 : 0;
414}
415
416static inline struct btf_member *btf_members(const struct btf_type *t)
417{
418 return (struct btf_member *)(t + 1);
419}
420
421static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
422{
423 const struct btf_member *m = btf_members(t) + member_idx;
424
425 return __btf_member_bit_offset(t, m);
426}
427
428static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
429{
430 const struct btf_member *m = btf_members(t) + member_idx;
431
432 return __btf_member_bitfield_size(t, m);
433}
434
435static inline const struct btf_member *btf_type_member(const struct btf_type *t)
436{
437 return (const struct btf_member *)(t + 1);
438}
439
440static inline struct btf_array *btf_array(const struct btf_type *t)
441{
442 return (struct btf_array *)(t + 1);
443}
444
445static inline struct btf_enum *btf_enum(const struct btf_type *t)
446{
447 return (struct btf_enum *)(t + 1);
448}
449
450static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
451{
452 return (struct btf_enum64 *)(t + 1);
453}
454
455static inline const struct btf_var_secinfo *btf_type_var_secinfo(
456 const struct btf_type *t)
457{
458 return (const struct btf_var_secinfo *)(t + 1);
459}
460
461static inline struct btf_param *btf_params(const struct btf_type *t)
462{
463 return (struct btf_param *)(t + 1);
464}
465
466static inline int btf_id_cmp_func(const void *a, const void *b)
467{
468 const int *pa = a, *pb = b;
469
470 return *pa - *pb;
471}
472
473static inline bool btf_id_set_contains(const struct btf_id_set *set, u32 id)
474{
475 return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL;
476}
477
478static inline void *btf_id_set8_contains(const struct btf_id_set8 *set, u32 id)
479{
480 return bsearch(&id, set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func);
481}
482
483struct bpf_prog;
484struct bpf_verifier_log;
485
486#ifdef CONFIG_BPF_SYSCALL
487const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
488const char *btf_name_by_offset(const struct btf *btf, u32 offset);
489struct btf *btf_parse_vmlinux(void);
490struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
491u32 *btf_kfunc_id_set_contains(const struct btf *btf,
492 enum bpf_prog_type prog_type,
493 u32 kfunc_btf_id);
494u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id);
495int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
496 const struct btf_kfunc_id_set *s);
497int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset);
498s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
499int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
500 struct module *owner);
501struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id);
502const struct btf_member *
503btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
504 const struct btf_type *t, enum bpf_prog_type prog_type,
505 int arg);
506int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type);
507bool btf_types_are_same(const struct btf *btf1, u32 id1,
508 const struct btf *btf2, u32 id2);
509#else
510static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
511 u32 type_id)
512{
513 return NULL;
514}
515static inline const char *btf_name_by_offset(const struct btf *btf,
516 u32 offset)
517{
518 return NULL;
519}
520static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf,
521 enum bpf_prog_type prog_type,
522 u32 kfunc_btf_id)
523{
524 return NULL;
525}
526static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
527 const struct btf_kfunc_id_set *s)
528{
529 return 0;
530}
531static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
532{
533 return -ENOENT;
534}
535static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
536 u32 add_cnt, struct module *owner)
537{
538 return 0;
539}
540static inline struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id)
541{
542 return NULL;
543}
544static inline const struct btf_member *
545btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
546 const struct btf_type *t, enum bpf_prog_type prog_type,
547 int arg)
548{
549 return NULL;
550}
551static inline int get_kern_ctx_btf_id(struct bpf_verifier_log *log,
552 enum bpf_prog_type prog_type) {
553 return -EINVAL;
554}
555static inline bool btf_types_are_same(const struct btf *btf1, u32 id1,
556 const struct btf *btf2, u32 id2)
557{
558 return false;
559}
560#endif
561
562static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t)
563{
564 if (!btf_type_is_ptr(t))
565 return false;
566
567 t = btf_type_skip_modifiers(btf, t->type, NULL);
568
569 return btf_type_is_struct(t);
570}
571
572#endif