Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 <uapi/linux/btf.h>
10#include <uapi/linux/bpf.h>
11
12#define BTF_TYPE_EMIT(type) ((void)(type *)0)
13#define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val)
14
15/* These need to be macros, as the expressions are used in assembler input */
16#define KF_ACQUIRE (1 << 0) /* kfunc is an acquire function */
17#define KF_RELEASE (1 << 1) /* kfunc is a release function */
18#define KF_RET_NULL (1 << 2) /* kfunc returns a pointer that may be NULL */
19#define KF_KPTR_GET (1 << 3) /* kfunc returns reference to a kptr */
20/* Trusted arguments are those which are meant to be referenced arguments with
21 * unchanged offset. It is used to enforce that pointers obtained from acquire
22 * kfuncs remain unmodified when being passed to helpers taking trusted args.
23 *
24 * Consider
25 * struct foo {
26 * int data;
27 * struct foo *next;
28 * };
29 *
30 * struct bar {
31 * int data;
32 * struct foo f;
33 * };
34 *
35 * struct foo *f = alloc_foo(); // Acquire kfunc
36 * struct bar *b = alloc_bar(); // Acquire kfunc
37 *
38 * If a kfunc set_foo_data() wants to operate only on the allocated object, it
39 * will set the KF_TRUSTED_ARGS flag, which will prevent unsafe usage like:
40 *
41 * set_foo_data(f, 42); // Allowed
42 * set_foo_data(f->next, 42); // Rejected, non-referenced pointer
43 * set_foo_data(&f->next, 42);// Rejected, referenced, but wrong type
44 * set_foo_data(&b->f, 42); // Rejected, referenced, but bad offset
45 *
46 * In the final case, usually for the purposes of type matching, it is deduced
47 * by looking at the type of the member at the offset, but due to the
48 * requirement of trusted argument, this deduction will be strict and not done
49 * for this case.
50 */
51#define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
52
53struct btf;
54struct btf_member;
55struct btf_type;
56union bpf_attr;
57struct btf_show;
58struct btf_id_set;
59
60struct btf_kfunc_id_set {
61 struct module *owner;
62 struct btf_id_set8 *set;
63};
64
65struct btf_id_dtor_kfunc {
66 u32 btf_id;
67 u32 kfunc_btf_id;
68};
69
70typedef void (*btf_dtor_kfunc_t)(void *);
71
72extern const struct file_operations btf_fops;
73
74void btf_get(struct btf *btf);
75void btf_put(struct btf *btf);
76int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr);
77struct btf *btf_get_by_fd(int fd);
78int btf_get_info_by_fd(const struct btf *btf,
79 const union bpf_attr *attr,
80 union bpf_attr __user *uattr);
81/* Figure out the size of a type_id. If type_id is a modifier
82 * (e.g. const), it will be resolved to find out the type with size.
83 *
84 * For example:
85 * In describing "const void *", type_id is "const" and "const"
86 * refers to "void *". The return type will be "void *".
87 *
88 * If type_id is a simple "int", then return type will be "int".
89 *
90 * @btf: struct btf object
91 * @type_id: Find out the size of type_id. The type_id of the return
92 * type is set to *type_id.
93 * @ret_size: It can be NULL. If not NULL, the size of the return
94 * type is set to *ret_size.
95 * Return: The btf_type (resolved to another type with size info if needed).
96 * NULL is returned if type_id itself does not have size info
97 * (e.g. void) or it cannot be resolved to another type that
98 * has size info.
99 * *type_id and *ret_size will not be changed in the
100 * NULL return case.
101 */
102const struct btf_type *btf_type_id_size(const struct btf *btf,
103 u32 *type_id,
104 u32 *ret_size);
105
106/*
107 * Options to control show behaviour.
108 * - BTF_SHOW_COMPACT: no formatting around type information
109 * - BTF_SHOW_NONAME: no struct/union member names/types
110 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
111 * equivalent to %px.
112 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they
113 * are not displayed by default
114 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
115 * data before displaying it.
116 */
117#define BTF_SHOW_COMPACT BTF_F_COMPACT
118#define BTF_SHOW_NONAME BTF_F_NONAME
119#define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW
120#define BTF_SHOW_ZERO BTF_F_ZERO
121#define BTF_SHOW_UNSAFE (1ULL << 4)
122
123void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
124 struct seq_file *m);
125int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
126 struct seq_file *m, u64 flags);
127
128/*
129 * Copy len bytes of string representation of obj of BTF type_id into buf.
130 *
131 * @btf: struct btf object
132 * @type_id: type id of type obj points to
133 * @obj: pointer to typed data
134 * @buf: buffer to write to
135 * @len: maximum length to write to buf
136 * @flags: show options (see above)
137 *
138 * Return: length that would have been/was copied as per snprintf, or
139 * negative error.
140 */
141int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
142 char *buf, int len, u64 flags);
143
144int btf_get_fd_by_id(u32 id);
145u32 btf_obj_id(const struct btf *btf);
146bool btf_is_kernel(const struct btf *btf);
147bool btf_is_module(const struct btf *btf);
148struct module *btf_try_get_module(const struct btf *btf);
149u32 btf_nr_types(const struct btf *btf);
150bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
151 const struct btf_member *m,
152 u32 expected_offset, u32 expected_size);
153int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
154int btf_find_timer(const struct btf *btf, const struct btf_type *t);
155struct bpf_map_value_off *btf_parse_kptrs(const struct btf *btf,
156 const struct btf_type *t);
157bool btf_type_is_void(const struct btf_type *t);
158s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
159const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
160 u32 id, u32 *res_id);
161const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
162 u32 id, u32 *res_id);
163const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
164 u32 id, u32 *res_id);
165const struct btf_type *
166btf_resolve_size(const struct btf *btf, const struct btf_type *type,
167 u32 *type_size);
168const char *btf_type_str(const struct btf_type *t);
169
170#define for_each_member(i, struct_type, member) \
171 for (i = 0, member = btf_type_member(struct_type); \
172 i < btf_type_vlen(struct_type); \
173 i++, member++)
174
175#define for_each_vsi(i, datasec_type, member) \
176 for (i = 0, member = btf_type_var_secinfo(datasec_type); \
177 i < btf_type_vlen(datasec_type); \
178 i++, member++)
179
180static inline bool btf_type_is_ptr(const struct btf_type *t)
181{
182 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
183}
184
185static inline bool btf_type_is_int(const struct btf_type *t)
186{
187 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
188}
189
190static inline bool btf_type_is_small_int(const struct btf_type *t)
191{
192 return btf_type_is_int(t) && t->size <= sizeof(u64);
193}
194
195static inline bool btf_type_is_enum(const struct btf_type *t)
196{
197 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
198}
199
200static inline bool btf_is_any_enum(const struct btf_type *t)
201{
202 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
203 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64;
204}
205
206static inline bool btf_kind_core_compat(const struct btf_type *t1,
207 const struct btf_type *t2)
208{
209 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) ||
210 (btf_is_any_enum(t1) && btf_is_any_enum(t2));
211}
212
213static inline bool str_is_empty(const char *s)
214{
215 return !s || !s[0];
216}
217
218static inline u16 btf_kind(const struct btf_type *t)
219{
220 return BTF_INFO_KIND(t->info);
221}
222
223static inline bool btf_is_enum(const struct btf_type *t)
224{
225 return btf_kind(t) == BTF_KIND_ENUM;
226}
227
228static inline bool btf_is_enum64(const struct btf_type *t)
229{
230 return btf_kind(t) == BTF_KIND_ENUM64;
231}
232
233static inline u64 btf_enum64_value(const struct btf_enum64 *e)
234{
235 return ((u64)e->val_hi32 << 32) | e->val_lo32;
236}
237
238static inline bool btf_is_composite(const struct btf_type *t)
239{
240 u16 kind = btf_kind(t);
241
242 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
243}
244
245static inline bool btf_is_array(const struct btf_type *t)
246{
247 return btf_kind(t) == BTF_KIND_ARRAY;
248}
249
250static inline bool btf_is_int(const struct btf_type *t)
251{
252 return btf_kind(t) == BTF_KIND_INT;
253}
254
255static inline bool btf_is_ptr(const struct btf_type *t)
256{
257 return btf_kind(t) == BTF_KIND_PTR;
258}
259
260static inline u8 btf_int_offset(const struct btf_type *t)
261{
262 return BTF_INT_OFFSET(*(u32 *)(t + 1));
263}
264
265static inline u8 btf_int_encoding(const struct btf_type *t)
266{
267 return BTF_INT_ENCODING(*(u32 *)(t + 1));
268}
269
270static inline bool btf_type_is_scalar(const struct btf_type *t)
271{
272 return btf_type_is_int(t) || btf_type_is_enum(t);
273}
274
275static inline bool btf_type_is_typedef(const struct btf_type *t)
276{
277 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
278}
279
280static inline bool btf_type_is_func(const struct btf_type *t)
281{
282 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
283}
284
285static inline bool btf_type_is_func_proto(const struct btf_type *t)
286{
287 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
288}
289
290static inline bool btf_type_is_var(const struct btf_type *t)
291{
292 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
293}
294
295static inline bool btf_type_is_type_tag(const struct btf_type *t)
296{
297 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
298}
299
300/* union is only a special case of struct:
301 * all its offsetof(member) == 0
302 */
303static inline bool btf_type_is_struct(const struct btf_type *t)
304{
305 u8 kind = BTF_INFO_KIND(t->info);
306
307 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
308}
309
310static inline u16 btf_type_vlen(const struct btf_type *t)
311{
312 return BTF_INFO_VLEN(t->info);
313}
314
315static inline u16 btf_vlen(const struct btf_type *t)
316{
317 return btf_type_vlen(t);
318}
319
320static inline u16 btf_func_linkage(const struct btf_type *t)
321{
322 return BTF_INFO_VLEN(t->info);
323}
324
325static inline bool btf_type_kflag(const struct btf_type *t)
326{
327 return BTF_INFO_KFLAG(t->info);
328}
329
330static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
331 const struct btf_member *member)
332{
333 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
334 : member->offset;
335}
336
337static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
338 const struct btf_member *member)
339{
340 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
341 : 0;
342}
343
344static inline struct btf_member *btf_members(const struct btf_type *t)
345{
346 return (struct btf_member *)(t + 1);
347}
348
349static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
350{
351 const struct btf_member *m = btf_members(t) + member_idx;
352
353 return __btf_member_bit_offset(t, m);
354}
355
356static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
357{
358 const struct btf_member *m = btf_members(t) + member_idx;
359
360 return __btf_member_bitfield_size(t, m);
361}
362
363static inline const struct btf_member *btf_type_member(const struct btf_type *t)
364{
365 return (const struct btf_member *)(t + 1);
366}
367
368static inline struct btf_array *btf_array(const struct btf_type *t)
369{
370 return (struct btf_array *)(t + 1);
371}
372
373static inline struct btf_enum *btf_enum(const struct btf_type *t)
374{
375 return (struct btf_enum *)(t + 1);
376}
377
378static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
379{
380 return (struct btf_enum64 *)(t + 1);
381}
382
383static inline const struct btf_var_secinfo *btf_type_var_secinfo(
384 const struct btf_type *t)
385{
386 return (const struct btf_var_secinfo *)(t + 1);
387}
388
389static inline struct btf_param *btf_params(const struct btf_type *t)
390{
391 return (struct btf_param *)(t + 1);
392}
393
394#ifdef CONFIG_BPF_SYSCALL
395struct bpf_prog;
396
397const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
398const char *btf_name_by_offset(const struct btf *btf, u32 offset);
399struct btf *btf_parse_vmlinux(void);
400struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
401u32 *btf_kfunc_id_set_contains(const struct btf *btf,
402 enum bpf_prog_type prog_type,
403 u32 kfunc_btf_id);
404int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
405 const struct btf_kfunc_id_set *s);
406s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
407int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
408 struct module *owner);
409#else
410static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
411 u32 type_id)
412{
413 return NULL;
414}
415static inline const char *btf_name_by_offset(const struct btf *btf,
416 u32 offset)
417{
418 return NULL;
419}
420static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf,
421 enum bpf_prog_type prog_type,
422 u32 kfunc_btf_id)
423{
424 return NULL;
425}
426static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
427 const struct btf_kfunc_id_set *s)
428{
429 return 0;
430}
431static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
432{
433 return -ENOENT;
434}
435static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
436 u32 add_cnt, struct module *owner)
437{
438 return 0;
439}
440#endif
441
442#endif