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