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