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
15struct btf;
16struct btf_member;
17struct btf_type;
18union bpf_attr;
19struct btf_show;
20
21extern const struct file_operations btf_fops;
22
23void btf_get(struct btf *btf);
24void btf_put(struct btf *btf);
25int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr);
26struct btf *btf_get_by_fd(int fd);
27int btf_get_info_by_fd(const struct btf *btf,
28 const union bpf_attr *attr,
29 union bpf_attr __user *uattr);
30/* Figure out the size of a type_id. If type_id is a modifier
31 * (e.g. const), it will be resolved to find out the type with size.
32 *
33 * For example:
34 * In describing "const void *", type_id is "const" and "const"
35 * refers to "void *". The return type will be "void *".
36 *
37 * If type_id is a simple "int", then return type will be "int".
38 *
39 * @btf: struct btf object
40 * @type_id: Find out the size of type_id. The type_id of the return
41 * type is set to *type_id.
42 * @ret_size: It can be NULL. If not NULL, the size of the return
43 * type is set to *ret_size.
44 * Return: The btf_type (resolved to another type with size info if needed).
45 * NULL is returned if type_id itself does not have size info
46 * (e.g. void) or it cannot be resolved to another type that
47 * has size info.
48 * *type_id and *ret_size will not be changed in the
49 * NULL return case.
50 */
51const struct btf_type *btf_type_id_size(const struct btf *btf,
52 u32 *type_id,
53 u32 *ret_size);
54
55/*
56 * Options to control show behaviour.
57 * - BTF_SHOW_COMPACT: no formatting around type information
58 * - BTF_SHOW_NONAME: no struct/union member names/types
59 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
60 * equivalent to %px.
61 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they
62 * are not displayed by default
63 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
64 * data before displaying it.
65 */
66#define BTF_SHOW_COMPACT BTF_F_COMPACT
67#define BTF_SHOW_NONAME BTF_F_NONAME
68#define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW
69#define BTF_SHOW_ZERO BTF_F_ZERO
70#define BTF_SHOW_UNSAFE (1ULL << 4)
71
72void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
73 struct seq_file *m);
74int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
75 struct seq_file *m, u64 flags);
76
77/*
78 * Copy len bytes of string representation of obj of BTF type_id into buf.
79 *
80 * @btf: struct btf object
81 * @type_id: type id of type obj points to
82 * @obj: pointer to typed data
83 * @buf: buffer to write to
84 * @len: maximum length to write to buf
85 * @flags: show options (see above)
86 *
87 * Return: length that would have been/was copied as per snprintf, or
88 * negative error.
89 */
90int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
91 char *buf, int len, u64 flags);
92
93int btf_get_fd_by_id(u32 id);
94u32 btf_obj_id(const struct btf *btf);
95bool btf_is_kernel(const struct btf *btf);
96bool btf_is_module(const struct btf *btf);
97struct module *btf_try_get_module(const struct btf *btf);
98u32 btf_nr_types(const struct btf *btf);
99bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
100 const struct btf_member *m,
101 u32 expected_offset, u32 expected_size);
102int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
103int btf_find_timer(const struct btf *btf, const struct btf_type *t);
104bool btf_type_is_void(const struct btf_type *t);
105s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
106const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
107 u32 id, u32 *res_id);
108const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
109 u32 id, u32 *res_id);
110const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
111 u32 id, u32 *res_id);
112const struct btf_type *
113btf_resolve_size(const struct btf *btf, const struct btf_type *type,
114 u32 *type_size);
115const char *btf_type_str(const struct btf_type *t);
116
117#define for_each_member(i, struct_type, member) \
118 for (i = 0, member = btf_type_member(struct_type); \
119 i < btf_type_vlen(struct_type); \
120 i++, member++)
121
122#define for_each_vsi(i, datasec_type, member) \
123 for (i = 0, member = btf_type_var_secinfo(datasec_type); \
124 i < btf_type_vlen(datasec_type); \
125 i++, member++)
126
127static inline bool btf_type_is_ptr(const struct btf_type *t)
128{
129 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
130}
131
132static inline bool btf_type_is_int(const struct btf_type *t)
133{
134 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
135}
136
137static inline bool btf_type_is_small_int(const struct btf_type *t)
138{
139 return btf_type_is_int(t) && t->size <= sizeof(u64);
140}
141
142static inline bool btf_type_is_enum(const struct btf_type *t)
143{
144 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
145}
146
147static inline bool btf_type_is_scalar(const struct btf_type *t)
148{
149 return btf_type_is_int(t) || btf_type_is_enum(t);
150}
151
152static inline bool btf_type_is_typedef(const struct btf_type *t)
153{
154 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
155}
156
157static inline bool btf_type_is_func(const struct btf_type *t)
158{
159 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
160}
161
162static inline bool btf_type_is_func_proto(const struct btf_type *t)
163{
164 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
165}
166
167static inline bool btf_type_is_var(const struct btf_type *t)
168{
169 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
170}
171
172/* union is only a special case of struct:
173 * all its offsetof(member) == 0
174 */
175static inline bool btf_type_is_struct(const struct btf_type *t)
176{
177 u8 kind = BTF_INFO_KIND(t->info);
178
179 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
180}
181
182static inline u16 btf_type_vlen(const struct btf_type *t)
183{
184 return BTF_INFO_VLEN(t->info);
185}
186
187static inline u16 btf_func_linkage(const struct btf_type *t)
188{
189 return BTF_INFO_VLEN(t->info);
190}
191
192static inline bool btf_type_kflag(const struct btf_type *t)
193{
194 return BTF_INFO_KFLAG(t->info);
195}
196
197static inline u32 btf_member_bit_offset(const struct btf_type *struct_type,
198 const struct btf_member *member)
199{
200 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
201 : member->offset;
202}
203
204static inline u32 btf_member_bitfield_size(const struct btf_type *struct_type,
205 const struct btf_member *member)
206{
207 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
208 : 0;
209}
210
211static inline const struct btf_member *btf_type_member(const struct btf_type *t)
212{
213 return (const struct btf_member *)(t + 1);
214}
215
216static inline const struct btf_var_secinfo *btf_type_var_secinfo(
217 const struct btf_type *t)
218{
219 return (const struct btf_var_secinfo *)(t + 1);
220}
221
222#ifdef CONFIG_BPF_SYSCALL
223struct bpf_prog;
224
225const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
226const char *btf_name_by_offset(const struct btf *btf, u32 offset);
227struct btf *btf_parse_vmlinux(void);
228struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
229#else
230static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
231 u32 type_id)
232{
233 return NULL;
234}
235static inline const char *btf_name_by_offset(const struct btf *btf,
236 u32 offset)
237{
238 return NULL;
239}
240#endif
241
242struct kfunc_btf_id_set {
243 struct list_head list;
244 struct btf_id_set *set;
245 struct module *owner;
246};
247
248struct kfunc_btf_id_list {
249 struct list_head list;
250 struct mutex mutex;
251};
252
253#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
254void register_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
255 struct kfunc_btf_id_set *s);
256void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
257 struct kfunc_btf_id_set *s);
258bool bpf_check_mod_kfunc_call(struct kfunc_btf_id_list *klist, u32 kfunc_id,
259 struct module *owner);
260
261extern struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list;
262extern struct kfunc_btf_id_list prog_test_kfunc_list;
263#else
264static inline void register_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
265 struct kfunc_btf_id_set *s)
266{
267}
268static inline void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l,
269 struct kfunc_btf_id_set *s)
270{
271}
272static inline bool bpf_check_mod_kfunc_call(struct kfunc_btf_id_list *klist,
273 u32 kfunc_id, struct module *owner)
274{
275 return false;
276}
277
278static struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list __maybe_unused;
279static struct kfunc_btf_id_list prog_test_kfunc_list __maybe_unused;
280#endif
281
282#define DEFINE_KFUNC_BTF_ID_SET(set, name) \
283 struct kfunc_btf_id_set name = { LIST_HEAD_INIT(name.list), (set), \
284 THIS_MODULE }
285
286#endif