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