bpf: Populate pairs of btf_id and destructor kfunc in btf
[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>
38207291 9#include <uapi/linux/btf.h>
c4d0bfb4 10#include <uapi/linux/bpf.h>
eb3f595d 11
85d33df3 12#define BTF_TYPE_EMIT(type) ((void)(type *)0)
97a19caf 13#define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val)
85d33df3 14
dee872e1
KKD
15enum btf_kfunc_type {
16 BTF_KFUNC_TYPE_CHECK,
17 BTF_KFUNC_TYPE_ACQUIRE,
18 BTF_KFUNC_TYPE_RELEASE,
19 BTF_KFUNC_TYPE_RET_NULL,
20 BTF_KFUNC_TYPE_MAX,
21};
22
eb3f595d 23struct btf;
ffa0c1cf 24struct btf_member;
eb3f595d 25struct btf_type;
f56a653c 26union bpf_attr;
31d0bc81 27struct btf_show;
dee872e1
KKD
28struct btf_id_set;
29
30struct btf_kfunc_id_set {
31 struct module *owner;
32 union {
33 struct {
34 struct btf_id_set *check_set;
35 struct btf_id_set *acquire_set;
36 struct btf_id_set *release_set;
37 struct btf_id_set *ret_null_set;
38 };
39 struct btf_id_set *sets[BTF_KFUNC_TYPE_MAX];
40 };
41};
eb3f595d 42
5ce937d6
KKD
43struct btf_id_dtor_kfunc {
44 u32 btf_id;
45 u32 kfunc_btf_id;
46};
47
60197cfb
MKL
48extern const struct file_operations btf_fops;
49
22dc4a0f 50void btf_get(struct btf *btf);
f56a653c 51void btf_put(struct btf *btf);
c571bd75 52int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr);
f56a653c 53struct btf *btf_get_by_fd(int fd);
60197cfb
MKL
54int btf_get_info_by_fd(const struct btf *btf,
55 const union bpf_attr *attr,
56 union bpf_attr __user *uattr);
eb3f595d
MKL
57/* Figure out the size of a type_id. If type_id is a modifier
58 * (e.g. const), it will be resolved to find out the type with size.
59 *
60 * For example:
61 * In describing "const void *", type_id is "const" and "const"
62 * refers to "void *". The return type will be "void *".
63 *
64 * If type_id is a simple "int", then return type will be "int".
65 *
66 * @btf: struct btf object
67 * @type_id: Find out the size of type_id. The type_id of the return
68 * type is set to *type_id.
69 * @ret_size: It can be NULL. If not NULL, the size of the return
70 * type is set to *ret_size.
71 * Return: The btf_type (resolved to another type with size info if needed).
72 * NULL is returned if type_id itself does not have size info
73 * (e.g. void) or it cannot be resolved to another type that
74 * has size info.
75 * *type_id and *ret_size will not be changed in the
76 * NULL return case.
77 */
78const struct btf_type *btf_type_id_size(const struct btf *btf,
79 u32 *type_id,
80 u32 *ret_size);
31d0bc81
AM
81
82/*
83 * Options to control show behaviour.
84 * - BTF_SHOW_COMPACT: no formatting around type information
85 * - BTF_SHOW_NONAME: no struct/union member names/types
86 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
87 * equivalent to %px.
88 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they
89 * are not displayed by default
90 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
91 * data before displaying it.
92 */
c4d0bfb4
AM
93#define BTF_SHOW_COMPACT BTF_F_COMPACT
94#define BTF_SHOW_NONAME BTF_F_NONAME
95#define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW
96#define BTF_SHOW_ZERO BTF_F_ZERO
31d0bc81
AM
97#define BTF_SHOW_UNSAFE (1ULL << 4)
98
b00b8dae
MKL
99void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
100 struct seq_file *m);
eb411377
AM
101int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
102 struct seq_file *m, u64 flags);
31d0bc81
AM
103
104/*
105 * Copy len bytes of string representation of obj of BTF type_id into buf.
106 *
107 * @btf: struct btf object
108 * @type_id: type id of type obj points to
109 * @obj: pointer to typed data
110 * @buf: buffer to write to
111 * @len: maximum length to write to buf
112 * @flags: show options (see above)
113 *
114 * Return: length that would have been/was copied as per snprintf, or
115 * negative error.
116 */
117int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
118 char *buf, int len, u64 flags);
119
78958fca 120int btf_get_fd_by_id(u32 id);
22dc4a0f 121u32 btf_obj_id(const struct btf *btf);
290248a5 122bool btf_is_kernel(const struct btf *btf);
541c3bad
AN
123bool btf_is_module(const struct btf *btf);
124struct module *btf_try_get_module(const struct btf *btf);
125u32 btf_nr_types(const struct btf *btf);
ffa0c1cf
YS
126bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
127 const struct btf_member *m,
128 u32 expected_offset, u32 expected_size);
d83525ca 129int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
68134668 130int btf_find_timer(const struct btf *btf, const struct btf_type *t);
61df10c7
KKD
131struct bpf_map_value_off *btf_parse_kptrs(const struct btf *btf,
132 const struct btf_type *t);
2824ecb7 133bool btf_type_is_void(const struct btf_type *t);
27ae7997
MKL
134s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
135const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
136 u32 id, u32 *res_id);
137const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
138 u32 id, u32 *res_id);
139const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
140 u32 id, u32 *res_id);
85d33df3
MKL
141const struct btf_type *
142btf_resolve_size(const struct btf *btf, const struct btf_type *type,
6298399b 143 u32 *type_size);
e6ac2450 144const char *btf_type_str(const struct btf_type *t);
27ae7997
MKL
145
146#define for_each_member(i, struct_type, member) \
147 for (i = 0, member = btf_type_member(struct_type); \
148 i < btf_type_vlen(struct_type); \
149 i++, member++)
f6161a8f 150
eaa6bcb7
HL
151#define for_each_vsi(i, datasec_type, member) \
152 for (i = 0, member = btf_type_var_secinfo(datasec_type); \
153 i < btf_type_vlen(datasec_type); \
154 i++, member++)
155
38207291
MKL
156static inline bool btf_type_is_ptr(const struct btf_type *t)
157{
158 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
159}
160
161static inline bool btf_type_is_int(const struct btf_type *t)
162{
163 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
164}
165
a9b59159
JF
166static inline bool btf_type_is_small_int(const struct btf_type *t)
167{
168 return btf_type_is_int(t) && t->size <= sizeof(u64);
169}
170
38207291
MKL
171static inline bool btf_type_is_enum(const struct btf_type *t)
172{
173 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
174}
175
29db4bea
AS
176static inline bool str_is_empty(const char *s)
177{
178 return !s || !s[0];
179}
180
181static inline u16 btf_kind(const struct btf_type *t)
182{
183 return BTF_INFO_KIND(t->info);
184}
185
186static inline bool btf_is_enum(const struct btf_type *t)
187{
188 return btf_kind(t) == BTF_KIND_ENUM;
189}
190
191static inline bool btf_is_composite(const struct btf_type *t)
192{
193 u16 kind = btf_kind(t);
194
195 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
196}
197
198static inline bool btf_is_array(const struct btf_type *t)
199{
200 return btf_kind(t) == BTF_KIND_ARRAY;
201}
202
203static inline bool btf_is_int(const struct btf_type *t)
204{
205 return btf_kind(t) == BTF_KIND_INT;
206}
207
208static inline bool btf_is_ptr(const struct btf_type *t)
209{
210 return btf_kind(t) == BTF_KIND_PTR;
211}
212
213static inline u8 btf_int_offset(const struct btf_type *t)
214{
215 return BTF_INT_OFFSET(*(u32 *)(t + 1));
216}
217
218static inline u8 btf_int_encoding(const struct btf_type *t)
219{
220 return BTF_INT_ENCODING(*(u32 *)(t + 1));
221}
222
34747c41
MKL
223static inline bool btf_type_is_scalar(const struct btf_type *t)
224{
225 return btf_type_is_int(t) || btf_type_is_enum(t);
226}
227
38207291
MKL
228static inline bool btf_type_is_typedef(const struct btf_type *t)
229{
230 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
231}
232
233static inline bool btf_type_is_func(const struct btf_type *t)
234{
235 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
236}
237
238static inline bool btf_type_is_func_proto(const struct btf_type *t)
239{
240 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
241}
242
4976b718
HL
243static inline bool btf_type_is_var(const struct btf_type *t)
244{
245 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
246}
247
c6f1bfe8
YS
248static inline bool btf_type_is_type_tag(const struct btf_type *t)
249{
250 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
251}
252
4976b718
HL
253/* union is only a special case of struct:
254 * all its offsetof(member) == 0
255 */
256static inline bool btf_type_is_struct(const struct btf_type *t)
257{
258 u8 kind = BTF_INFO_KIND(t->info);
259
260 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
261}
262
27ae7997
MKL
263static inline u16 btf_type_vlen(const struct btf_type *t)
264{
265 return BTF_INFO_VLEN(t->info);
266}
267
29db4bea
AS
268static inline u16 btf_vlen(const struct btf_type *t)
269{
270 return btf_type_vlen(t);
271}
272
be8704ff
AS
273static inline u16 btf_func_linkage(const struct btf_type *t)
274{
275 return BTF_INFO_VLEN(t->info);
276}
277
27ae7997
MKL
278static inline bool btf_type_kflag(const struct btf_type *t)
279{
280 return BTF_INFO_KFLAG(t->info);
281}
282
8293eb99
AS
283static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
284 const struct btf_member *member)
85d33df3
MKL
285{
286 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
287 : member->offset;
288}
289
8293eb99
AS
290static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
291 const struct btf_member *member)
27ae7997
MKL
292{
293 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
294 : 0;
295}
296
29db4bea
AS
297static inline struct btf_member *btf_members(const struct btf_type *t)
298{
299 return (struct btf_member *)(t + 1);
300}
301
302static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
303{
304 const struct btf_member *m = btf_members(t) + member_idx;
305
306 return __btf_member_bit_offset(t, m);
307}
308
309static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
310{
311 const struct btf_member *m = btf_members(t) + member_idx;
312
313 return __btf_member_bitfield_size(t, m);
314}
315
27ae7997
MKL
316static inline const struct btf_member *btf_type_member(const struct btf_type *t)
317{
318 return (const struct btf_member *)(t + 1);
319}
320
29db4bea
AS
321static inline struct btf_array *btf_array(const struct btf_type *t)
322{
323 return (struct btf_array *)(t + 1);
324}
325
326static inline struct btf_enum *btf_enum(const struct btf_type *t)
327{
328 return (struct btf_enum *)(t + 1);
329}
330
eaa6bcb7
HL
331static inline const struct btf_var_secinfo *btf_type_var_secinfo(
332 const struct btf_type *t)
333{
334 return (const struct btf_var_secinfo *)(t + 1);
335}
336
e70e13e7
MC
337static inline struct btf_param *btf_params(const struct btf_type *t)
338{
339 return (struct btf_param *)(t + 1);
340}
341
f6161a8f 342#ifdef CONFIG_BPF_SYSCALL
22dc4a0f
AN
343struct bpf_prog;
344
838e9690
YS
345const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
346const char *btf_name_by_offset(const struct btf *btf, u32 offset);
8580ac94 347struct btf *btf_parse_vmlinux(void);
5b92a28a 348struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
dee872e1
KKD
349bool btf_kfunc_id_set_contains(const struct btf *btf,
350 enum bpf_prog_type prog_type,
351 enum btf_kfunc_type type, u32 kfunc_btf_id);
352int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
353 const struct btf_kfunc_id_set *s);
5ce937d6
KKD
354s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
355int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
356 struct module *owner);
f6161a8f
YS
357#else
358static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
359 u32 type_id)
360{
361 return NULL;
362}
363static inline const char *btf_name_by_offset(const struct btf *btf,
364 u32 offset)
365{
366 return NULL;
367}
dee872e1
KKD
368static inline bool btf_kfunc_id_set_contains(const struct btf *btf,
369 enum bpf_prog_type prog_type,
370 enum btf_kfunc_type type,
371 u32 kfunc_btf_id)
372{
373 return false;
374}
375static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
376 const struct btf_kfunc_id_set *s)
377{
378 return 0;
379}
5ce937d6
KKD
380static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
381{
382 return -ENOENT;
383}
384static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
385 u32 add_cnt, struct module *owner)
386{
387 return 0;
388}
f6161a8f 389#endif
eb3f595d
MKL
390
391#endif