Merge tag 'mm-hotfixes-stable-2023-05-03-16-27' of git://git.kernel.org/pub/scm/linux...
[linux-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
KKD
100struct btf_id_set;
101
102struct btf_kfunc_id_set {
103 struct module *owner;
a4703e31 104 struct btf_id_set8 *set;
dee872e1 105};
eb3f595d 106
5ce937d6
KKD
107struct btf_id_dtor_kfunc {
108 u32 btf_id;
109 u32 kfunc_btf_id;
110};
111
8ffa5cc1
KKD
112struct btf_struct_meta {
113 u32 btf_id;
114 struct btf_record *record;
8ffa5cc1
KKD
115};
116
117struct btf_struct_metas {
118 u32 cnt;
119 struct btf_struct_meta types[];
120};
121
60197cfb
MKL
122extern const struct file_operations btf_fops;
123
22dc4a0f 124void btf_get(struct btf *btf);
f56a653c 125void btf_put(struct btf *btf);
47a71c1f 126int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_sz);
f56a653c 127struct btf *btf_get_by_fd(int fd);
60197cfb
MKL
128int btf_get_info_by_fd(const struct btf *btf,
129 const union bpf_attr *attr,
130 union bpf_attr __user *uattr);
eb3f595d
MKL
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);
31d0bc81
AM
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 */
c4d0bfb4
AM
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
31d0bc81
AM
171#define BTF_SHOW_UNSAFE (1ULL << 4)
172
b00b8dae
MKL
173void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
174 struct seq_file *m);
eb411377
AM
175int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
176 struct seq_file *m, u64 flags);
31d0bc81
AM
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
78958fca 194int btf_get_fd_by_id(u32 id);
22dc4a0f 195u32 btf_obj_id(const struct btf *btf);
290248a5 196bool btf_is_kernel(const struct btf *btf);
541c3bad
AN
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);
ffa0c1cf
YS
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);
d83525ca 203int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
68134668 204int btf_find_timer(const struct btf *btf, const struct btf_type *t);
db559117
KKD
205struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t,
206 u32 field_mask, u32 value_size);
865ce09a 207int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec);
2824ecb7 208bool btf_type_is_void(const struct btf_type *t);
27ae7997
MKL
209s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
210const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
211 u32 id, u32 *res_id);
212const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
213 u32 id, u32 *res_id);
214const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
215 u32 id, u32 *res_id);
85d33df3
MKL
216const struct btf_type *
217btf_resolve_size(const struct btf *btf, const struct btf_type *type,
6298399b 218 u32 *type_size);
e6ac2450 219const char *btf_type_str(const struct btf_type *t);
27ae7997
MKL
220
221#define for_each_member(i, struct_type, member) \
222 for (i = 0, member = btf_type_member(struct_type); \
223 i < btf_type_vlen(struct_type); \
224 i++, member++)
f6161a8f 225
eaa6bcb7
HL
226#define for_each_vsi(i, datasec_type, member) \
227 for (i = 0, member = btf_type_var_secinfo(datasec_type); \
228 i < btf_type_vlen(datasec_type); \
229 i++, member++)
230
38207291
MKL
231static inline bool btf_type_is_ptr(const struct btf_type *t)
232{
233 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
234}
235
236static inline bool btf_type_is_int(const struct btf_type *t)
237{
238 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
239}
240
a9b59159
JF
241static inline bool btf_type_is_small_int(const struct btf_type *t)
242{
243 return btf_type_is_int(t) && t->size <= sizeof(u64);
244}
245
49f67f39
IL
246static inline u8 btf_int_encoding(const struct btf_type *t)
247{
248 return BTF_INT_ENCODING(*(u32 *)(t + 1));
249}
250
251static inline bool btf_type_is_signed_int(const struct btf_type *t)
252{
253 return btf_type_is_int(t) && (btf_int_encoding(t) & BTF_INT_SIGNED);
254}
255
38207291
MKL
256static inline bool btf_type_is_enum(const struct btf_type *t)
257{
258 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
259}
260
6089fb32
YS
261static inline bool btf_is_any_enum(const struct btf_type *t)
262{
263 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
264 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64;
265}
266
267static inline bool btf_kind_core_compat(const struct btf_type *t1,
268 const struct btf_type *t2)
269{
270 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) ||
271 (btf_is_any_enum(t1) && btf_is_any_enum(t2));
272}
273
29db4bea
AS
274static inline bool str_is_empty(const char *s)
275{
276 return !s || !s[0];
277}
278
279static inline u16 btf_kind(const struct btf_type *t)
280{
281 return BTF_INFO_KIND(t->info);
282}
283
284static inline bool btf_is_enum(const struct btf_type *t)
285{
286 return btf_kind(t) == BTF_KIND_ENUM;
287}
288
6089fb32
YS
289static inline bool btf_is_enum64(const struct btf_type *t)
290{
291 return btf_kind(t) == BTF_KIND_ENUM64;
292}
293
294static inline u64 btf_enum64_value(const struct btf_enum64 *e)
295{
296 return ((u64)e->val_hi32 << 32) | e->val_lo32;
297}
298
29db4bea
AS
299static inline bool btf_is_composite(const struct btf_type *t)
300{
301 u16 kind = btf_kind(t);
302
303 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
304}
305
306static inline bool btf_is_array(const struct btf_type *t)
307{
308 return btf_kind(t) == BTF_KIND_ARRAY;
309}
310
311static inline bool btf_is_int(const struct btf_type *t)
312{
313 return btf_kind(t) == BTF_KIND_INT;
314}
315
316static inline bool btf_is_ptr(const struct btf_type *t)
317{
318 return btf_kind(t) == BTF_KIND_PTR;
319}
320
321static inline u8 btf_int_offset(const struct btf_type *t)
322{
323 return BTF_INT_OFFSET(*(u32 *)(t + 1));
324}
325
34747c41
MKL
326static inline bool btf_type_is_scalar(const struct btf_type *t)
327{
328 return btf_type_is_int(t) || btf_type_is_enum(t);
329}
330
38207291
MKL
331static inline bool btf_type_is_typedef(const struct btf_type *t)
332{
333 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
334}
335
23da464d
KKD
336static inline bool btf_type_is_volatile(const struct btf_type *t)
337{
338 return BTF_INFO_KIND(t->info) == BTF_KIND_VOLATILE;
339}
340
38207291
MKL
341static inline bool btf_type_is_func(const struct btf_type *t)
342{
343 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
344}
345
346static inline bool btf_type_is_func_proto(const struct btf_type *t)
347{
348 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
349}
350
4976b718
HL
351static inline bool btf_type_is_var(const struct btf_type *t)
352{
353 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
354}
355
c6f1bfe8
YS
356static inline bool btf_type_is_type_tag(const struct btf_type *t)
357{
358 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
359}
360
4976b718
HL
361/* union is only a special case of struct:
362 * all its offsetof(member) == 0
363 */
364static inline bool btf_type_is_struct(const struct btf_type *t)
365{
366 u8 kind = BTF_INFO_KIND(t->info);
367
368 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
369}
370
00b85860
KKD
371static inline bool __btf_type_is_struct(const struct btf_type *t)
372{
373 return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT;
374}
375
376static inline bool btf_type_is_array(const struct btf_type *t)
377{
378 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
379}
380
27ae7997
MKL
381static inline u16 btf_type_vlen(const struct btf_type *t)
382{
383 return BTF_INFO_VLEN(t->info);
384}
385
29db4bea
AS
386static inline u16 btf_vlen(const struct btf_type *t)
387{
388 return btf_type_vlen(t);
389}
390
be8704ff
AS
391static inline u16 btf_func_linkage(const struct btf_type *t)
392{
393 return BTF_INFO_VLEN(t->info);
394}
395
27ae7997
MKL
396static inline bool btf_type_kflag(const struct btf_type *t)
397{
398 return BTF_INFO_KFLAG(t->info);
399}
400
8293eb99
AS
401static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
402 const struct btf_member *member)
85d33df3
MKL
403{
404 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
405 : member->offset;
406}
407
8293eb99
AS
408static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
409 const struct btf_member *member)
27ae7997
MKL
410{
411 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
412 : 0;
413}
414
29db4bea
AS
415static inline struct btf_member *btf_members(const struct btf_type *t)
416{
417 return (struct btf_member *)(t + 1);
418}
419
420static inline u32 btf_member_bit_offset(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_bit_offset(t, m);
425}
426
427static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
428{
429 const struct btf_member *m = btf_members(t) + member_idx;
430
431 return __btf_member_bitfield_size(t, m);
432}
433
27ae7997
MKL
434static inline const struct btf_member *btf_type_member(const struct btf_type *t)
435{
436 return (const struct btf_member *)(t + 1);
437}
438
29db4bea
AS
439static inline struct btf_array *btf_array(const struct btf_type *t)
440{
441 return (struct btf_array *)(t + 1);
442}
443
444static inline struct btf_enum *btf_enum(const struct btf_type *t)
445{
446 return (struct btf_enum *)(t + 1);
447}
448
6089fb32
YS
449static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
450{
451 return (struct btf_enum64 *)(t + 1);
452}
453
eaa6bcb7
HL
454static inline const struct btf_var_secinfo *btf_type_var_secinfo(
455 const struct btf_type *t)
456{
457 return (const struct btf_var_secinfo *)(t + 1);
458}
459
e70e13e7
MC
460static inline struct btf_param *btf_params(const struct btf_type *t)
461{
462 return (struct btf_param *)(t + 1);
463}
464
8ffa5cc1
KKD
465static inline int btf_id_cmp_func(const void *a, const void *b)
466{
467 const int *pa = a, *pb = b;
468
469 return *pa - *pb;
470}
471
472static inline bool btf_id_set_contains(const struct btf_id_set *set, u32 id)
473{
474 return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL;
475}
476
477static inline void *btf_id_set8_contains(const struct btf_id_set8 *set, u32 id)
478{
479 return bsearch(&id, set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func);
480}
481
22dc4a0f 482struct bpf_prog;
00b85860 483struct bpf_verifier_log;
22dc4a0f 484
00b85860 485#ifdef CONFIG_BPF_SYSCALL
838e9690
YS
486const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
487const char *btf_name_by_offset(const struct btf *btf, u32 offset);
8580ac94 488struct btf *btf_parse_vmlinux(void);
5b92a28a 489struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
a4703e31 490u32 *btf_kfunc_id_set_contains(const struct btf *btf,
dee872e1 491 enum bpf_prog_type prog_type,
a4703e31 492 u32 kfunc_btf_id);
5b481aca 493u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id);
dee872e1
KKD
494int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
495 const struct btf_kfunc_id_set *s);
5b481aca 496int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset);
5ce937d6
KKD
497s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
498int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
499 struct module *owner);
8ffa5cc1 500struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id);
00b85860
KKD
501const struct btf_member *
502btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
503 const struct btf_type *t, enum bpf_prog_type prog_type,
504 int arg);
fd264ca0 505int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type);
00b85860
KKD
506bool btf_types_are_same(const struct btf *btf1, u32 id1,
507 const struct btf *btf2, u32 id2);
f6161a8f
YS
508#else
509static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
510 u32 type_id)
511{
512 return NULL;
513}
514static inline const char *btf_name_by_offset(const struct btf *btf,
515 u32 offset)
516{
517 return NULL;
518}
a4703e31 519static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf,
dee872e1 520 enum bpf_prog_type prog_type,
dee872e1
KKD
521 u32 kfunc_btf_id)
522{
a4703e31 523 return NULL;
dee872e1
KKD
524}
525static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
526 const struct btf_kfunc_id_set *s)
527{
528 return 0;
529}
5ce937d6
KKD
530static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
531{
532 return -ENOENT;
533}
534static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
535 u32 add_cnt, struct module *owner)
536{
537 return 0;
538}
8ffa5cc1
KKD
539static inline struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id)
540{
541 return NULL;
542}
00b85860
KKD
543static inline const struct btf_member *
544btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
545 const struct btf_type *t, enum bpf_prog_type prog_type,
546 int arg)
547{
548 return NULL;
549}
fd264ca0
YS
550static inline int get_kern_ctx_btf_id(struct bpf_verifier_log *log,
551 enum bpf_prog_type prog_type) {
552 return -EINVAL;
553}
00b85860
KKD
554static inline bool btf_types_are_same(const struct btf *btf1, u32 id1,
555 const struct btf *btf2, u32 id2)
556{
557 return false;
558}
f6161a8f 559#endif
eb3f595d 560
eb1f7f71
BT
561static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t)
562{
563 if (!btf_type_is_ptr(t))
564 return false;
565
566 t = btf_type_skip_modifiers(btf, t->type, NULL);
567
568 return btf_type_is_struct(t);
569}
570
eb3f595d 571#endif