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