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