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