]>
Commit | Line | Data |
---|---|---|
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 | |
a4703e31 KKD |
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 */ | |
56e948ff KKD |
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 */ | |
4dd48c6f AS |
52 | #define KF_SLEEPABLE (1 << 5) /* kfunc may sleep */ |
53 | #define KF_DESTRUCTIVE (1 << 6) /* kfunc performs destructive actions */ | |
dee872e1 | 54 | |
b8d31762 RS |
55 | /* |
56 | * Return the name of the passed struct, if exists, or halt the build if for | |
57 | * example the structure gets renamed. In this way, developers have to revisit | |
58 | * the code using that structure name, and update it accordingly. | |
59 | */ | |
60 | #define stringify_struct(x) \ | |
61 | ({ BUILD_BUG_ON(sizeof(struct x) < 0); \ | |
62 | __stringify(x); }) | |
63 | ||
eb3f595d | 64 | struct btf; |
ffa0c1cf | 65 | struct btf_member; |
eb3f595d | 66 | struct btf_type; |
f56a653c | 67 | union bpf_attr; |
31d0bc81 | 68 | struct btf_show; |
dee872e1 KKD |
69 | struct btf_id_set; |
70 | ||
71 | struct btf_kfunc_id_set { | |
72 | struct module *owner; | |
a4703e31 | 73 | struct btf_id_set8 *set; |
dee872e1 | 74 | }; |
eb3f595d | 75 | |
5ce937d6 KKD |
76 | struct btf_id_dtor_kfunc { |
77 | u32 btf_id; | |
78 | u32 kfunc_btf_id; | |
79 | }; | |
80 | ||
14a324f6 KKD |
81 | typedef void (*btf_dtor_kfunc_t)(void *); |
82 | ||
60197cfb MKL |
83 | extern const struct file_operations btf_fops; |
84 | ||
22dc4a0f | 85 | void btf_get(struct btf *btf); |
f56a653c | 86 | void btf_put(struct btf *btf); |
c571bd75 | 87 | int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr); |
f56a653c | 88 | struct btf *btf_get_by_fd(int fd); |
60197cfb MKL |
89 | int btf_get_info_by_fd(const struct btf *btf, |
90 | const union bpf_attr *attr, | |
91 | union bpf_attr __user *uattr); | |
eb3f595d MKL |
92 | /* Figure out the size of a type_id. If type_id is a modifier |
93 | * (e.g. const), it will be resolved to find out the type with size. | |
94 | * | |
95 | * For example: | |
96 | * In describing "const void *", type_id is "const" and "const" | |
97 | * refers to "void *". The return type will be "void *". | |
98 | * | |
99 | * If type_id is a simple "int", then return type will be "int". | |
100 | * | |
101 | * @btf: struct btf object | |
102 | * @type_id: Find out the size of type_id. The type_id of the return | |
103 | * type is set to *type_id. | |
104 | * @ret_size: It can be NULL. If not NULL, the size of the return | |
105 | * type is set to *ret_size. | |
106 | * Return: The btf_type (resolved to another type with size info if needed). | |
107 | * NULL is returned if type_id itself does not have size info | |
108 | * (e.g. void) or it cannot be resolved to another type that | |
109 | * has size info. | |
110 | * *type_id and *ret_size will not be changed in the | |
111 | * NULL return case. | |
112 | */ | |
113 | const struct btf_type *btf_type_id_size(const struct btf *btf, | |
114 | u32 *type_id, | |
115 | u32 *ret_size); | |
31d0bc81 AM |
116 | |
117 | /* | |
118 | * Options to control show behaviour. | |
119 | * - BTF_SHOW_COMPACT: no formatting around type information | |
120 | * - BTF_SHOW_NONAME: no struct/union member names/types | |
121 | * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values; | |
122 | * equivalent to %px. | |
123 | * - BTF_SHOW_ZERO: show zero-valued struct/union members; they | |
124 | * are not displayed by default | |
125 | * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read | |
126 | * data before displaying it. | |
127 | */ | |
c4d0bfb4 AM |
128 | #define BTF_SHOW_COMPACT BTF_F_COMPACT |
129 | #define BTF_SHOW_NONAME BTF_F_NONAME | |
130 | #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW | |
131 | #define BTF_SHOW_ZERO BTF_F_ZERO | |
31d0bc81 AM |
132 | #define BTF_SHOW_UNSAFE (1ULL << 4) |
133 | ||
b00b8dae MKL |
134 | void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, |
135 | struct seq_file *m); | |
eb411377 AM |
136 | int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj, |
137 | struct seq_file *m, u64 flags); | |
31d0bc81 AM |
138 | |
139 | /* | |
140 | * Copy len bytes of string representation of obj of BTF type_id into buf. | |
141 | * | |
142 | * @btf: struct btf object | |
143 | * @type_id: type id of type obj points to | |
144 | * @obj: pointer to typed data | |
145 | * @buf: buffer to write to | |
146 | * @len: maximum length to write to buf | |
147 | * @flags: show options (see above) | |
148 | * | |
149 | * Return: length that would have been/was copied as per snprintf, or | |
150 | * negative error. | |
151 | */ | |
152 | int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, | |
153 | char *buf, int len, u64 flags); | |
154 | ||
78958fca | 155 | int btf_get_fd_by_id(u32 id); |
22dc4a0f | 156 | u32 btf_obj_id(const struct btf *btf); |
290248a5 | 157 | bool btf_is_kernel(const struct btf *btf); |
541c3bad AN |
158 | bool btf_is_module(const struct btf *btf); |
159 | struct module *btf_try_get_module(const struct btf *btf); | |
160 | u32 btf_nr_types(const struct btf *btf); | |
ffa0c1cf YS |
161 | bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, |
162 | const struct btf_member *m, | |
163 | u32 expected_offset, u32 expected_size); | |
d83525ca | 164 | int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t); |
68134668 | 165 | int btf_find_timer(const struct btf *btf, const struct btf_type *t); |
aa3496ac | 166 | struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t); |
2824ecb7 | 167 | bool btf_type_is_void(const struct btf_type *t); |
27ae7997 MKL |
168 | s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); |
169 | const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, | |
170 | u32 id, u32 *res_id); | |
171 | const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, | |
172 | u32 id, u32 *res_id); | |
173 | const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, | |
174 | u32 id, u32 *res_id); | |
85d33df3 MKL |
175 | const struct btf_type * |
176 | btf_resolve_size(const struct btf *btf, const struct btf_type *type, | |
6298399b | 177 | u32 *type_size); |
e6ac2450 | 178 | const char *btf_type_str(const struct btf_type *t); |
27ae7997 MKL |
179 | |
180 | #define for_each_member(i, struct_type, member) \ | |
181 | for (i = 0, member = btf_type_member(struct_type); \ | |
182 | i < btf_type_vlen(struct_type); \ | |
183 | i++, member++) | |
f6161a8f | 184 | |
eaa6bcb7 HL |
185 | #define for_each_vsi(i, datasec_type, member) \ |
186 | for (i = 0, member = btf_type_var_secinfo(datasec_type); \ | |
187 | i < btf_type_vlen(datasec_type); \ | |
188 | i++, member++) | |
189 | ||
38207291 MKL |
190 | static inline bool btf_type_is_ptr(const struct btf_type *t) |
191 | { | |
192 | return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; | |
193 | } | |
194 | ||
195 | static inline bool btf_type_is_int(const struct btf_type *t) | |
196 | { | |
197 | return BTF_INFO_KIND(t->info) == BTF_KIND_INT; | |
198 | } | |
199 | ||
a9b59159 JF |
200 | static inline bool btf_type_is_small_int(const struct btf_type *t) |
201 | { | |
202 | return btf_type_is_int(t) && t->size <= sizeof(u64); | |
203 | } | |
204 | ||
38207291 MKL |
205 | static inline bool btf_type_is_enum(const struct btf_type *t) |
206 | { | |
207 | return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM; | |
208 | } | |
209 | ||
6089fb32 YS |
210 | static inline bool btf_is_any_enum(const struct btf_type *t) |
211 | { | |
212 | return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM || | |
213 | BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64; | |
214 | } | |
215 | ||
216 | static inline bool btf_kind_core_compat(const struct btf_type *t1, | |
217 | const struct btf_type *t2) | |
218 | { | |
219 | return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) || | |
220 | (btf_is_any_enum(t1) && btf_is_any_enum(t2)); | |
221 | } | |
222 | ||
29db4bea AS |
223 | static inline bool str_is_empty(const char *s) |
224 | { | |
225 | return !s || !s[0]; | |
226 | } | |
227 | ||
228 | static inline u16 btf_kind(const struct btf_type *t) | |
229 | { | |
230 | return BTF_INFO_KIND(t->info); | |
231 | } | |
232 | ||
233 | static inline bool btf_is_enum(const struct btf_type *t) | |
234 | { | |
235 | return btf_kind(t) == BTF_KIND_ENUM; | |
236 | } | |
237 | ||
6089fb32 YS |
238 | static inline bool btf_is_enum64(const struct btf_type *t) |
239 | { | |
240 | return btf_kind(t) == BTF_KIND_ENUM64; | |
241 | } | |
242 | ||
243 | static inline u64 btf_enum64_value(const struct btf_enum64 *e) | |
244 | { | |
245 | return ((u64)e->val_hi32 << 32) | e->val_lo32; | |
246 | } | |
247 | ||
29db4bea AS |
248 | static inline bool btf_is_composite(const struct btf_type *t) |
249 | { | |
250 | u16 kind = btf_kind(t); | |
251 | ||
252 | return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; | |
253 | } | |
254 | ||
255 | static inline bool btf_is_array(const struct btf_type *t) | |
256 | { | |
257 | return btf_kind(t) == BTF_KIND_ARRAY; | |
258 | } | |
259 | ||
260 | static inline bool btf_is_int(const struct btf_type *t) | |
261 | { | |
262 | return btf_kind(t) == BTF_KIND_INT; | |
263 | } | |
264 | ||
265 | static inline bool btf_is_ptr(const struct btf_type *t) | |
266 | { | |
267 | return btf_kind(t) == BTF_KIND_PTR; | |
268 | } | |
269 | ||
270 | static inline u8 btf_int_offset(const struct btf_type *t) | |
271 | { | |
272 | return BTF_INT_OFFSET(*(u32 *)(t + 1)); | |
273 | } | |
274 | ||
275 | static inline u8 btf_int_encoding(const struct btf_type *t) | |
276 | { | |
277 | return BTF_INT_ENCODING(*(u32 *)(t + 1)); | |
278 | } | |
279 | ||
34747c41 MKL |
280 | static inline bool btf_type_is_scalar(const struct btf_type *t) |
281 | { | |
282 | return btf_type_is_int(t) || btf_type_is_enum(t); | |
283 | } | |
284 | ||
38207291 MKL |
285 | static inline bool btf_type_is_typedef(const struct btf_type *t) |
286 | { | |
287 | return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF; | |
288 | } | |
289 | ||
23da464d KKD |
290 | static inline bool btf_type_is_volatile(const struct btf_type *t) |
291 | { | |
292 | return BTF_INFO_KIND(t->info) == BTF_KIND_VOLATILE; | |
293 | } | |
294 | ||
38207291 MKL |
295 | static inline bool btf_type_is_func(const struct btf_type *t) |
296 | { | |
297 | return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC; | |
298 | } | |
299 | ||
300 | static inline bool btf_type_is_func_proto(const struct btf_type *t) | |
301 | { | |
302 | return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO; | |
303 | } | |
304 | ||
4976b718 HL |
305 | static inline bool btf_type_is_var(const struct btf_type *t) |
306 | { | |
307 | return BTF_INFO_KIND(t->info) == BTF_KIND_VAR; | |
308 | } | |
309 | ||
c6f1bfe8 YS |
310 | static inline bool btf_type_is_type_tag(const struct btf_type *t) |
311 | { | |
312 | return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG; | |
313 | } | |
314 | ||
4976b718 HL |
315 | /* union is only a special case of struct: |
316 | * all its offsetof(member) == 0 | |
317 | */ | |
318 | static inline bool btf_type_is_struct(const struct btf_type *t) | |
319 | { | |
320 | u8 kind = BTF_INFO_KIND(t->info); | |
321 | ||
322 | return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; | |
323 | } | |
324 | ||
27ae7997 MKL |
325 | static inline u16 btf_type_vlen(const struct btf_type *t) |
326 | { | |
327 | return BTF_INFO_VLEN(t->info); | |
328 | } | |
329 | ||
29db4bea AS |
330 | static inline u16 btf_vlen(const struct btf_type *t) |
331 | { | |
332 | return btf_type_vlen(t); | |
333 | } | |
334 | ||
be8704ff AS |
335 | static inline u16 btf_func_linkage(const struct btf_type *t) |
336 | { | |
337 | return BTF_INFO_VLEN(t->info); | |
338 | } | |
339 | ||
27ae7997 MKL |
340 | static inline bool btf_type_kflag(const struct btf_type *t) |
341 | { | |
342 | return BTF_INFO_KFLAG(t->info); | |
343 | } | |
344 | ||
8293eb99 AS |
345 | static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type, |
346 | const struct btf_member *member) | |
85d33df3 MKL |
347 | { |
348 | return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset) | |
349 | : member->offset; | |
350 | } | |
351 | ||
8293eb99 AS |
352 | static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type, |
353 | const struct btf_member *member) | |
27ae7997 MKL |
354 | { |
355 | return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset) | |
356 | : 0; | |
357 | } | |
358 | ||
29db4bea AS |
359 | static inline struct btf_member *btf_members(const struct btf_type *t) |
360 | { | |
361 | return (struct btf_member *)(t + 1); | |
362 | } | |
363 | ||
364 | static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx) | |
365 | { | |
366 | const struct btf_member *m = btf_members(t) + member_idx; | |
367 | ||
368 | return __btf_member_bit_offset(t, m); | |
369 | } | |
370 | ||
371 | static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx) | |
372 | { | |
373 | const struct btf_member *m = btf_members(t) + member_idx; | |
374 | ||
375 | return __btf_member_bitfield_size(t, m); | |
376 | } | |
377 | ||
27ae7997 MKL |
378 | static inline const struct btf_member *btf_type_member(const struct btf_type *t) |
379 | { | |
380 | return (const struct btf_member *)(t + 1); | |
381 | } | |
382 | ||
29db4bea AS |
383 | static inline struct btf_array *btf_array(const struct btf_type *t) |
384 | { | |
385 | return (struct btf_array *)(t + 1); | |
386 | } | |
387 | ||
388 | static inline struct btf_enum *btf_enum(const struct btf_type *t) | |
389 | { | |
390 | return (struct btf_enum *)(t + 1); | |
391 | } | |
392 | ||
6089fb32 YS |
393 | static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) |
394 | { | |
395 | return (struct btf_enum64 *)(t + 1); | |
396 | } | |
397 | ||
eaa6bcb7 HL |
398 | static inline const struct btf_var_secinfo *btf_type_var_secinfo( |
399 | const struct btf_type *t) | |
400 | { | |
401 | return (const struct btf_var_secinfo *)(t + 1); | |
402 | } | |
403 | ||
e70e13e7 MC |
404 | static inline struct btf_param *btf_params(const struct btf_type *t) |
405 | { | |
406 | return (struct btf_param *)(t + 1); | |
407 | } | |
408 | ||
f6161a8f | 409 | #ifdef CONFIG_BPF_SYSCALL |
22dc4a0f AN |
410 | struct bpf_prog; |
411 | ||
838e9690 YS |
412 | const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); |
413 | const char *btf_name_by_offset(const struct btf *btf, u32 offset); | |
8580ac94 | 414 | struct btf *btf_parse_vmlinux(void); |
5b92a28a | 415 | struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); |
a4703e31 | 416 | u32 *btf_kfunc_id_set_contains(const struct btf *btf, |
dee872e1 | 417 | enum bpf_prog_type prog_type, |
a4703e31 | 418 | u32 kfunc_btf_id); |
dee872e1 KKD |
419 | int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, |
420 | const struct btf_kfunc_id_set *s); | |
5ce937d6 KKD |
421 | s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id); |
422 | int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt, | |
423 | struct module *owner); | |
f6161a8f YS |
424 | #else |
425 | static inline const struct btf_type *btf_type_by_id(const struct btf *btf, | |
426 | u32 type_id) | |
427 | { | |
428 | return NULL; | |
429 | } | |
430 | static inline const char *btf_name_by_offset(const struct btf *btf, | |
431 | u32 offset) | |
432 | { | |
433 | return NULL; | |
434 | } | |
a4703e31 | 435 | static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf, |
dee872e1 | 436 | enum bpf_prog_type prog_type, |
dee872e1 KKD |
437 | u32 kfunc_btf_id) |
438 | { | |
a4703e31 | 439 | return NULL; |
dee872e1 KKD |
440 | } |
441 | static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, | |
442 | const struct btf_kfunc_id_set *s) | |
443 | { | |
444 | return 0; | |
445 | } | |
5ce937d6 KKD |
446 | static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id) |
447 | { | |
448 | return -ENOENT; | |
449 | } | |
450 | static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, | |
451 | u32 add_cnt, struct module *owner) | |
452 | { | |
453 | return 0; | |
454 | } | |
f6161a8f | 455 | #endif |
eb3f595d | 456 | |
eb1f7f71 BT |
457 | static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t) |
458 | { | |
459 | if (!btf_type_is_ptr(t)) | |
460 | return false; | |
461 | ||
462 | t = btf_type_skip_modifiers(btf, t->type, NULL); | |
463 | ||
464 | return btf_type_is_struct(t); | |
465 | } | |
466 | ||
eb3f595d | 467 | #endif |