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 /* Trusted arguments are those which are guaranteed to be valid when passed to
22 * the kfunc. It is used to enforce that pointers obtained from either acquire
23 * kfuncs, or from the main kernel on a tracepoint or struct_ops callback
24 * invocation, remain unmodified when being passed to helpers taking trusted
25 * args.
26 *
27 * Consider, for example, the following new task tracepoint:
28 *
29 * SEC("tp_btf/task_newtask")
30 * int BPF_PROG(new_task_tp, struct task_struct *task, u64 clone_flags)
31 * {
32 * ...
33 * }
34 *
35 * And the following kfunc:
36 *
37 * BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE)
38 *
39 * All invocations to the kfunc must pass the unmodified, unwalked task:
40 *
41 * bpf_task_acquire(task); // Allowed
42 * bpf_task_acquire(task->last_wakee); // Rejected, walked task
43 *
44 * Programs may also pass referenced tasks directly to the kfunc:
45 *
46 * struct task_struct *acquired;
47 *
48 * acquired = bpf_task_acquire(task); // Allowed, same as above
49 * bpf_task_acquire(acquired); // Allowed
50 * bpf_task_acquire(task); // Allowed
51 * bpf_task_acquire(acquired->last_wakee); // Rejected, walked task
52 *
53 * Programs may _not_, however, pass a task from an arbitrary fentry/fexit, or
54 * kprobe/kretprobe to the kfunc, as BPF cannot guarantee that all of these
55 * pointers are guaranteed to be safe. For example, the following BPF program
56 * would be rejected:
57 *
58 * SEC("kretprobe/free_task")
59 * int BPF_PROG(free_task_probe, struct task_struct *tsk)
60 * {
61 * struct task_struct *acquired;
62 *
63 * acquired = bpf_task_acquire(acquired); // Rejected, not a trusted pointer
64 * bpf_task_release(acquired);
65 *
66 * return 0;
67 * }
68 */
69 #define KF_SLEEPABLE (1 << 5) /* kfunc may sleep */
70 #define KF_DESTRUCTIVE (1 << 6) /* kfunc performs destructive actions */
71 #define KF_RCU (1 << 7) /* kfunc takes either rcu or trusted pointer arguments */
72 /* only one of KF_ITER_{NEW,NEXT,DESTROY} could be specified per kfunc */
73 #define KF_ITER_NEW (1 << 8) /* kfunc implements BPF iter constructor */
74 #define KF_ITER_NEXT (1 << 9) /* kfunc implements BPF iter next method */
75 #define KF_ITER_DESTROY (1 << 10) /* kfunc implements BPF iter destructor */
76 #define KF_RCU_PROTECTED (1 << 11) /* kfunc should be protected by rcu cs when they are invoked */
77 #define KF_FASTCALL (1 << 12) /* kfunc supports bpf_fastcall protocol */
78 #define KF_ARENA_RET (1 << 13) /* kfunc returns an arena pointer */
79 #define KF_ARENA_ARG1 (1 << 14) /* kfunc takes an arena pointer as its first argument */
80 #define KF_ARENA_ARG2 (1 << 15) /* kfunc takes an arena pointer as its second argument */
81 #define KF_IMPLICIT_ARGS (1 << 16) /* kfunc has implicit arguments supplied by the verifier */
82 #define KF_SPINLOCK_SAFE (1 << 17) /* kfunc is allowed inside bpf_spin_lock-ed region */
83 #define KF_PERFMON (1 << 18) /* kfunc requires CAP_PERFMON */
84
85 /*
86 * Tag marking a kernel function as a kfunc. This is meant to minimize the
87 * amount of copy-paste that kfunc authors have to include for correctness so
88 * as to avoid issues such as the compiler inlining or eliding either a static
89 * kfunc, or a global kfunc in an LTO build.
90 */
91 #define __bpf_kfunc __used __retain __noclone noinline
92
93 #define __bpf_kfunc_start_defs() \
94 __diag_push(); \
95 __diag_ignore_all("-Wmissing-declarations", \
96 "Global kfuncs as their definitions will be in BTF");\
97 __diag_ignore_all("-Wmissing-prototypes", \
98 "Global kfuncs as their definitions will be in BTF")
99
100 #define __bpf_kfunc_end_defs() __diag_pop()
101 #define __bpf_hook_start() __bpf_kfunc_start_defs()
102 #define __bpf_hook_end() __bpf_kfunc_end_defs()
103
104 /*
105 * Return the name of the passed struct, if exists, or halt the build if for
106 * example the structure gets renamed. In this way, developers have to revisit
107 * the code using that structure name, and update it accordingly.
108 */
109 #define stringify_struct(x) \
110 ({ BUILD_BUG_ON(sizeof(struct x) < 0); \
111 __stringify(x); })
112
113 struct btf;
114 struct btf_member;
115 struct btf_type;
116 union bpf_attr;
117 struct btf_show;
118 struct btf_id_set;
119 struct bpf_prog;
120
121 typedef int (*btf_kfunc_filter_t)(const struct bpf_prog *prog, u32 kfunc_id);
122
123 struct btf_kfunc_id_set {
124 struct module *owner;
125 struct btf_id_set8 *set;
126 btf_kfunc_filter_t filter;
127 };
128
129 struct btf_id_dtor_kfunc {
130 u32 btf_id;
131 u32 kfunc_btf_id;
132 };
133
134 struct btf_struct_meta {
135 u32 btf_id;
136 struct btf_record *record;
137 };
138
139 struct btf_struct_metas {
140 u32 cnt;
141 struct btf_struct_meta types[];
142 };
143
144 extern const struct file_operations btf_fops;
145
146 const char *btf_get_name(const struct btf *btf);
147 void btf_get(struct btf *btf);
148 void btf_put(struct btf *btf);
149 const struct btf_header *btf_header(const struct btf *btf);
150 struct bpf_log_attr;
151 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log);
152 struct btf *btf_get_by_fd(int fd);
153 int btf_get_info_by_fd(const struct btf *btf,
154 const union bpf_attr *attr,
155 union bpf_attr __user *uattr);
156 /* Figure out the size of a type_id. If type_id is a modifier
157 * (e.g. const), it will be resolved to find out the type with size.
158 *
159 * For example:
160 * In describing "const void *", type_id is "const" and "const"
161 * refers to "void *". The return type will be "void *".
162 *
163 * If type_id is a simple "int", then return type will be "int".
164 *
165 * @btf: struct btf object
166 * @type_id: Find out the size of type_id. The type_id of the return
167 * type is set to *type_id.
168 * @ret_size: It can be NULL. If not NULL, the size of the return
169 * type is set to *ret_size.
170 * Return: The btf_type (resolved to another type with size info if needed).
171 * NULL is returned if type_id itself does not have size info
172 * (e.g. void) or it cannot be resolved to another type that
173 * has size info.
174 * *type_id and *ret_size will not be changed in the
175 * NULL return case.
176 */
177 const struct btf_type *btf_type_id_size(const struct btf *btf,
178 u32 *type_id,
179 u32 *ret_size);
180
181 /*
182 * Options to control show behaviour.
183 * - BTF_SHOW_COMPACT: no formatting around type information
184 * - BTF_SHOW_NONAME: no struct/union member names/types
185 * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
186 * equivalent to %px.
187 * - BTF_SHOW_ZERO: show zero-valued struct/union members; they
188 * are not displayed by default
189 * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
190 * data before displaying it.
191 */
192 #define BTF_SHOW_COMPACT BTF_F_COMPACT
193 #define BTF_SHOW_NONAME BTF_F_NONAME
194 #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW
195 #define BTF_SHOW_ZERO BTF_F_ZERO
196 #define BTF_SHOW_UNSAFE (1ULL << 4)
197
198 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
199 struct seq_file *m);
200 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
201 struct seq_file *m, u64 flags);
202
203 /*
204 * Copy len bytes of string representation of obj of BTF type_id into buf.
205 *
206 * @btf: struct btf object
207 * @type_id: type id of type obj points to
208 * @obj: pointer to typed data
209 * @buf: buffer to write to
210 * @len: maximum length to write to buf
211 * @flags: show options (see above)
212 *
213 * Return: length that would have been/was copied as per snprintf, or
214 * negative error.
215 */
216 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
217 char *buf, int len, u64 flags);
218 int btf_type_name_to_buf(const struct btf *btf, u32 type_id, char *buf, int len);
219
220 int btf_get_fd_by_id(u32 id);
221 u32 btf_obj_id(const struct btf *btf);
222 bool btf_is_kernel(const struct btf *btf);
223 bool btf_is_module(const struct btf *btf);
224 bool btf_is_vmlinux(const struct btf *btf);
225 struct module *btf_try_get_module(const struct btf *btf);
226 u32 btf_nr_types(const struct btf *btf);
227 u32 btf_named_start_id(const struct btf *btf, bool own);
228 struct btf *btf_base_btf(const struct btf *btf);
229 bool btf_type_is_i32(const struct btf_type *t);
230 bool btf_type_is_i64(const struct btf_type *t);
231 bool btf_type_is_primitive(const struct btf_type *t);
232 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
233 const struct btf_member *m,
234 u32 expected_offset, u32 expected_size);
235 struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t,
236 u32 field_mask, u32 value_size);
237 int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec);
238 bool btf_type_is_void(const struct btf_type *t);
239 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
240 s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p);
241 struct btf *btf_get_module_btf(const struct module *module);
242 __u32 btf_relocate_id(const struct btf *btf, __u32 id);
243 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
244 u32 id, u32 *res_id);
245 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
246 u32 id, u32 *res_id);
247 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
248 u32 id, u32 *res_id);
249 const struct btf_type *
250 btf_resolve_size(const struct btf *btf, const struct btf_type *type,
251 u32 *type_size);
252 const char *btf_type_str(const struct btf_type *t);
253
254 #define for_each_member(i, struct_type, member) \
255 for (i = 0, member = btf_type_member(struct_type); \
256 i < btf_type_vlen(struct_type); \
257 i++, member++)
258
259 #define for_each_vsi(i, datasec_type, member) \
260 for (i = 0, member = btf_type_var_secinfo(datasec_type); \
261 i < btf_type_vlen(datasec_type); \
262 i++, member++)
263
btf_type_is_ptr(const struct btf_type * t)264 static inline bool btf_type_is_ptr(const struct btf_type *t)
265 {
266 return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
267 }
268
btf_type_is_int(const struct btf_type * t)269 static inline bool btf_type_is_int(const struct btf_type *t)
270 {
271 return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
272 }
273
btf_type_is_small_int(const struct btf_type * t)274 static inline bool btf_type_is_small_int(const struct btf_type *t)
275 {
276 return btf_type_is_int(t) && t->size <= sizeof(u64);
277 }
278
btf_int_encoding(const struct btf_type * t)279 static inline u8 btf_int_encoding(const struct btf_type *t)
280 {
281 return BTF_INT_ENCODING(*(u32 *)(t + 1));
282 }
283
btf_type_is_signed_int(const struct btf_type * t)284 static inline bool btf_type_is_signed_int(const struct btf_type *t)
285 {
286 return btf_type_is_int(t) && (btf_int_encoding(t) & BTF_INT_SIGNED);
287 }
288
btf_type_is_enum(const struct btf_type * t)289 static inline bool btf_type_is_enum(const struct btf_type *t)
290 {
291 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
292 }
293
btf_is_any_enum(const struct btf_type * t)294 static inline bool btf_is_any_enum(const struct btf_type *t)
295 {
296 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
297 BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64;
298 }
299
btf_kind_core_compat(const struct btf_type * t1,const struct btf_type * t2)300 static inline bool btf_kind_core_compat(const struct btf_type *t1,
301 const struct btf_type *t2)
302 {
303 return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) ||
304 (btf_is_any_enum(t1) && btf_is_any_enum(t2));
305 }
306
str_is_empty(const char * s)307 static inline bool str_is_empty(const char *s)
308 {
309 return !s || !s[0];
310 }
311
btf_kind(const struct btf_type * t)312 static inline u16 btf_kind(const struct btf_type *t)
313 {
314 return BTF_INFO_KIND(t->info);
315 }
316
btf_is_enum(const struct btf_type * t)317 static inline bool btf_is_enum(const struct btf_type *t)
318 {
319 return btf_kind(t) == BTF_KIND_ENUM;
320 }
321
btf_is_enum64(const struct btf_type * t)322 static inline bool btf_is_enum64(const struct btf_type *t)
323 {
324 return btf_kind(t) == BTF_KIND_ENUM64;
325 }
326
btf_enum64_value(const struct btf_enum64 * e)327 static inline u64 btf_enum64_value(const struct btf_enum64 *e)
328 {
329 return ((u64)e->val_hi32 << 32) | e->val_lo32;
330 }
331
btf_is_composite(const struct btf_type * t)332 static inline bool btf_is_composite(const struct btf_type *t)
333 {
334 u16 kind = btf_kind(t);
335
336 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
337 }
338
btf_is_array(const struct btf_type * t)339 static inline bool btf_is_array(const struct btf_type *t)
340 {
341 return btf_kind(t) == BTF_KIND_ARRAY;
342 }
343
btf_is_int(const struct btf_type * t)344 static inline bool btf_is_int(const struct btf_type *t)
345 {
346 return btf_kind(t) == BTF_KIND_INT;
347 }
348
btf_is_ptr(const struct btf_type * t)349 static inline bool btf_is_ptr(const struct btf_type *t)
350 {
351 return btf_kind(t) == BTF_KIND_PTR;
352 }
353
btf_int_offset(const struct btf_type * t)354 static inline u8 btf_int_offset(const struct btf_type *t)
355 {
356 return BTF_INT_OFFSET(*(u32 *)(t + 1));
357 }
358
btf_int_bits(const struct btf_type * t)359 static inline __u8 btf_int_bits(const struct btf_type *t)
360 {
361 return BTF_INT_BITS(*(__u32 *)(t + 1));
362 }
363
btf_type_is_scalar(const struct btf_type * t)364 static inline bool btf_type_is_scalar(const struct btf_type *t)
365 {
366 return btf_type_is_int(t) || btf_type_is_enum(t);
367 }
368
btf_type_is_fwd(const struct btf_type * t)369 static inline bool btf_type_is_fwd(const struct btf_type *t)
370 {
371 return BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
372 }
373
btf_type_is_typedef(const struct btf_type * t)374 static inline bool btf_type_is_typedef(const struct btf_type *t)
375 {
376 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
377 }
378
btf_type_is_volatile(const struct btf_type * t)379 static inline bool btf_type_is_volatile(const struct btf_type *t)
380 {
381 return BTF_INFO_KIND(t->info) == BTF_KIND_VOLATILE;
382 }
383
btf_type_is_func(const struct btf_type * t)384 static inline bool btf_type_is_func(const struct btf_type *t)
385 {
386 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
387 }
388
btf_type_is_func_proto(const struct btf_type * t)389 static inline bool btf_type_is_func_proto(const struct btf_type *t)
390 {
391 return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
392 }
393
btf_type_is_var(const struct btf_type * t)394 static inline bool btf_type_is_var(const struct btf_type *t)
395 {
396 return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
397 }
398
btf_type_is_type_tag(const struct btf_type * t)399 static inline bool btf_type_is_type_tag(const struct btf_type *t)
400 {
401 return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
402 }
403
404 /* union is only a special case of struct:
405 * all its offsetof(member) == 0
406 */
btf_type_is_struct(const struct btf_type * t)407 static inline bool btf_type_is_struct(const struct btf_type *t)
408 {
409 u8 kind = BTF_INFO_KIND(t->info);
410
411 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
412 }
413
__btf_type_is_struct(const struct btf_type * t)414 static inline bool __btf_type_is_struct(const struct btf_type *t)
415 {
416 return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT;
417 }
418
btf_type_is_array(const struct btf_type * t)419 static inline bool btf_type_is_array(const struct btf_type *t)
420 {
421 return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
422 }
423
btf_type_vlen(const struct btf_type * t)424 static inline u32 btf_type_vlen(const struct btf_type *t)
425 {
426 return BTF_INFO_VLEN(t->info);
427 }
428
btf_vlen(const struct btf_type * t)429 static inline u32 btf_vlen(const struct btf_type *t)
430 {
431 return btf_type_vlen(t);
432 }
433
btf_func_linkage(const struct btf_type * t)434 static inline u16 btf_func_linkage(const struct btf_type *t)
435 {
436 return BTF_INFO_VLEN(t->info);
437 }
438
btf_type_kflag(const struct btf_type * t)439 static inline bool btf_type_kflag(const struct btf_type *t)
440 {
441 return BTF_INFO_KFLAG(t->info);
442 }
443
__btf_member_bit_offset(const struct btf_type * struct_type,const struct btf_member * member)444 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
445 const struct btf_member *member)
446 {
447 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
448 : member->offset;
449 }
450
__btf_member_bitfield_size(const struct btf_type * struct_type,const struct btf_member * member)451 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
452 const struct btf_member *member)
453 {
454 return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
455 : 0;
456 }
457
btf_members(const struct btf_type * t)458 static inline struct btf_member *btf_members(const struct btf_type *t)
459 {
460 return (struct btf_member *)(t + 1);
461 }
462
btf_member_bit_offset(const struct btf_type * t,u32 member_idx)463 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
464 {
465 const struct btf_member *m = btf_members(t) + member_idx;
466
467 return __btf_member_bit_offset(t, m);
468 }
469
btf_member_bitfield_size(const struct btf_type * t,u32 member_idx)470 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
471 {
472 const struct btf_member *m = btf_members(t) + member_idx;
473
474 return __btf_member_bitfield_size(t, m);
475 }
476
btf_type_member(const struct btf_type * t)477 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
478 {
479 return (const struct btf_member *)(t + 1);
480 }
481
btf_array(const struct btf_type * t)482 static inline struct btf_array *btf_array(const struct btf_type *t)
483 {
484 return (struct btf_array *)(t + 1);
485 }
486
btf_enum(const struct btf_type * t)487 static inline struct btf_enum *btf_enum(const struct btf_type *t)
488 {
489 return (struct btf_enum *)(t + 1);
490 }
491
btf_enum64(const struct btf_type * t)492 static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
493 {
494 return (struct btf_enum64 *)(t + 1);
495 }
496
btf_type_var_secinfo(const struct btf_type * t)497 static inline const struct btf_var_secinfo *btf_type_var_secinfo(
498 const struct btf_type *t)
499 {
500 return (const struct btf_var_secinfo *)(t + 1);
501 }
502
btf_params(const struct btf_type * t)503 static inline struct btf_param *btf_params(const struct btf_type *t)
504 {
505 return (struct btf_param *)(t + 1);
506 }
507
btf_decl_tag(const struct btf_type * t)508 static inline struct btf_decl_tag *btf_decl_tag(const struct btf_type *t)
509 {
510 return (struct btf_decl_tag *)(t + 1);
511 }
512
btf_id_cmp_func(const void * a,const void * b)513 static inline int btf_id_cmp_func(const void *a, const void *b)
514 {
515 const int *pa = a, *pb = b;
516
517 return *pa - *pb;
518 }
519
btf_id_set_contains(const struct btf_id_set * set,u32 id)520 static inline bool btf_id_set_contains(const struct btf_id_set *set, u32 id)
521 {
522 return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL;
523 }
524
btf_id_set8_contains(const struct btf_id_set8 * set,u32 id)525 static inline void *btf_id_set8_contains(const struct btf_id_set8 *set, u32 id)
526 {
527 return bsearch(&id, set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func);
528 }
529
530 bool btf_param_match_suffix(const struct btf *btf,
531 const struct btf_param *arg,
532 const char *suffix);
533 int btf_ctx_arg_offset(const struct btf *btf, const struct btf_type *func_proto,
534 u32 arg_no);
535 u32 btf_ctx_arg_idx(struct btf *btf, const struct btf_type *func_proto, int off);
536
537 struct bpf_verifier_log;
538
539 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
540 struct bpf_struct_ops;
541 int __register_bpf_struct_ops(struct bpf_struct_ops *st_ops);
542 const struct bpf_struct_ops_desc *bpf_struct_ops_find_value(struct btf *btf, u32 value_id);
543 const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *btf, u32 type_id);
544 #else
bpf_struct_ops_find(struct btf * btf,u32 type_id)545 static inline const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *btf, u32 type_id)
546 {
547 return NULL;
548 }
549 #endif
550
551 enum btf_field_iter_kind {
552 BTF_FIELD_ITER_IDS,
553 BTF_FIELD_ITER_STRS,
554 };
555
556 struct btf_field_desc {
557 /* once-per-type offsets */
558 int t_off_cnt, t_offs[2];
559 /* member struct size, or zero, if no members */
560 int m_sz;
561 /* repeated per-member offsets */
562 int m_off_cnt, m_offs[1];
563 };
564
565 struct btf_field_iter {
566 struct btf_field_desc desc;
567 void *p;
568 int m_idx;
569 int off_idx;
570 int vlen;
571 };
572
573 #ifdef CONFIG_BPF_SYSCALL
574 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
575 void btf_set_base_btf(struct btf *btf, const struct btf *base_btf);
576 int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **map_ids);
577 int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,
578 enum btf_field_iter_kind iter_kind);
579 __u32 *btf_field_iter_next(struct btf_field_iter *it);
580
581 const char *btf_name_by_offset(const struct btf *btf, u32 offset);
582 const char *btf_str_by_offset(const struct btf *btf, u32 offset);
583 struct btf *btf_parse_vmlinux(void);
584 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
585 u32 *btf_kfunc_flags(const struct btf *btf, u32 kfunc_btf_id, const struct bpf_prog *prog);
586 int btf_kfunc_check_flag(const struct btf *btf, u32 kfunc_btf_id, u32 flag);
587 bool btf_kfunc_is_allowed(const struct btf *btf, u32 kfunc_btf_id, const struct bpf_prog *prog);
588 u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id,
589 const struct bpf_prog *prog);
590 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
591 const struct btf_kfunc_id_set *s);
592 int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset);
593 s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
594 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
595 struct module *owner);
596 struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id);
597 bool btf_is_projection_of(const char *pname, const char *tname);
598 bool btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
599 const struct btf_type *t, enum bpf_prog_type prog_type,
600 int arg);
601 int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type);
602 bool btf_types_are_same(const struct btf *btf1, u32 id1,
603 const struct btf *btf2, u32 id2);
604 int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx);
605
btf_type_is_struct_ptr(struct btf * btf,const struct btf_type * t)606 static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t)
607 {
608 if (!btf_type_is_ptr(t))
609 return false;
610
611 t = btf_type_skip_modifiers(btf, t->type, NULL);
612
613 return btf_type_is_struct(t);
614 }
615 #else
btf_type_by_id(const struct btf * btf,u32 type_id)616 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
617 u32 type_id)
618 {
619 return NULL;
620 }
621
btf_set_base_btf(struct btf * btf,const struct btf * base_btf)622 static inline void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
623 {
624 }
625
btf_relocate(void * log,struct btf * btf,const struct btf * base_btf,__u32 ** map_ids)626 static inline int btf_relocate(void *log, struct btf *btf, const struct btf *base_btf,
627 __u32 **map_ids)
628 {
629 return -EOPNOTSUPP;
630 }
631
btf_field_iter_init(struct btf_field_iter * it,struct btf_type * t,enum btf_field_iter_kind iter_kind)632 static inline int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t,
633 enum btf_field_iter_kind iter_kind)
634 {
635 return -EOPNOTSUPP;
636 }
637
btf_field_iter_next(struct btf_field_iter * it)638 static inline __u32 *btf_field_iter_next(struct btf_field_iter *it)
639 {
640 return NULL;
641 }
642
btf_name_by_offset(const struct btf * btf,u32 offset)643 static inline const char *btf_name_by_offset(const struct btf *btf,
644 u32 offset)
645 {
646 return NULL;
647 }
btf_kfunc_id_set_contains(const struct btf * btf,u32 kfunc_btf_id,struct bpf_prog * prog)648 static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf,
649 u32 kfunc_btf_id,
650 struct bpf_prog *prog)
651
652 {
653 return NULL;
654 }
register_btf_kfunc_id_set(enum bpf_prog_type prog_type,const struct btf_kfunc_id_set * s)655 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
656 const struct btf_kfunc_id_set *s)
657 {
658 return 0;
659 }
btf_find_dtor_kfunc(struct btf * btf,u32 btf_id)660 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
661 {
662 return -ENOENT;
663 }
register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc * dtors,u32 add_cnt,struct module * owner)664 static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
665 u32 add_cnt, struct module *owner)
666 {
667 return 0;
668 }
btf_find_struct_meta(const struct btf * btf,u32 btf_id)669 static inline struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id)
670 {
671 return NULL;
672 }
673 static inline bool
btf_is_prog_ctx_type(struct bpf_verifier_log * log,const struct btf * btf,const struct btf_type * t,enum bpf_prog_type prog_type,int arg)674 btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
675 const struct btf_type *t, enum bpf_prog_type prog_type,
676 int arg)
677 {
678 return false;
679 }
get_kern_ctx_btf_id(struct bpf_verifier_log * log,enum bpf_prog_type prog_type)680 static inline int get_kern_ctx_btf_id(struct bpf_verifier_log *log,
681 enum bpf_prog_type prog_type) {
682 return -EINVAL;
683 }
btf_types_are_same(const struct btf * btf1,u32 id1,const struct btf * btf2,u32 id2)684 static inline bool btf_types_are_same(const struct btf *btf1, u32 id1,
685 const struct btf *btf2, u32 id2)
686 {
687 return false;
688 }
btf_check_iter_arg(struct btf * btf,const struct btf_type * func,int arg_idx)689 static inline int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx)
690 {
691 return -EOPNOTSUPP;
692 }
693 #endif
694 #endif
695