xref: /linux-6.15/include/linux/btf.h (revision c8e18754)
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 #define KF_KPTR_GET	(1 << 3) /* kfunc returns reference to a kptr */
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 | KF_TRUSTED_ARGS)
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_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
71 #define KF_SLEEPABLE    (1 << 5) /* kfunc may sleep */
72 #define KF_DESTRUCTIVE  (1 << 6) /* kfunc performs destructive actions */
73 #define KF_RCU          (1 << 7) /* kfunc takes either rcu or trusted pointer arguments */
74 /* only one of KF_ITER_{NEW,NEXT,DESTROY} could be specified per kfunc */
75 #define KF_ITER_NEW     (1 << 8) /* kfunc implements BPF iter constructor */
76 #define KF_ITER_NEXT    (1 << 9) /* kfunc implements BPF iter next method */
77 #define KF_ITER_DESTROY (1 << 10) /* kfunc implements BPF iter destructor */
78 
79 /*
80  * Tag marking a kernel function as a kfunc. This is meant to minimize the
81  * amount of copy-paste that kfunc authors have to include for correctness so
82  * as to avoid issues such as the compiler inlining or eliding either a static
83  * kfunc, or a global kfunc in an LTO build.
84  */
85 #define __bpf_kfunc __used noinline
86 
87 /*
88  * Return the name of the passed struct, if exists, or halt the build if for
89  * example the structure gets renamed. In this way, developers have to revisit
90  * the code using that structure name, and update it accordingly.
91  */
92 #define stringify_struct(x)			\
93 	({ BUILD_BUG_ON(sizeof(struct x) < 0);	\
94 	   __stringify(x); })
95 
96 struct btf;
97 struct btf_member;
98 struct btf_type;
99 union bpf_attr;
100 struct btf_show;
101 struct btf_id_set;
102 
103 struct btf_kfunc_id_set {
104 	struct module *owner;
105 	struct btf_id_set8 *set;
106 };
107 
108 struct btf_id_dtor_kfunc {
109 	u32 btf_id;
110 	u32 kfunc_btf_id;
111 };
112 
113 struct btf_struct_meta {
114 	u32 btf_id;
115 	struct btf_record *record;
116 	struct btf_field_offs *field_offs;
117 };
118 
119 struct btf_struct_metas {
120 	u32 cnt;
121 	struct btf_struct_meta types[];
122 };
123 
124 extern const struct file_operations btf_fops;
125 
126 void btf_get(struct btf *btf);
127 void btf_put(struct btf *btf);
128 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr);
129 struct btf *btf_get_by_fd(int fd);
130 int btf_get_info_by_fd(const struct btf *btf,
131 		       const union bpf_attr *attr,
132 		       union bpf_attr __user *uattr);
133 /* Figure out the size of a type_id.  If type_id is a modifier
134  * (e.g. const), it will be resolved to find out the type with size.
135  *
136  * For example:
137  * In describing "const void *",  type_id is "const" and "const"
138  * refers to "void *".  The return type will be "void *".
139  *
140  * If type_id is a simple "int", then return type will be "int".
141  *
142  * @btf: struct btf object
143  * @type_id: Find out the size of type_id. The type_id of the return
144  *           type is set to *type_id.
145  * @ret_size: It can be NULL.  If not NULL, the size of the return
146  *            type is set to *ret_size.
147  * Return: The btf_type (resolved to another type with size info if needed).
148  *         NULL is returned if type_id itself does not have size info
149  *         (e.g. void) or it cannot be resolved to another type that
150  *         has size info.
151  *         *type_id and *ret_size will not be changed in the
152  *         NULL return case.
153  */
154 const struct btf_type *btf_type_id_size(const struct btf *btf,
155 					u32 *type_id,
156 					u32 *ret_size);
157 
158 /*
159  * Options to control show behaviour.
160  *	- BTF_SHOW_COMPACT: no formatting around type information
161  *	- BTF_SHOW_NONAME: no struct/union member names/types
162  *	- BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
163  *	  equivalent to %px.
164  *	- BTF_SHOW_ZERO: show zero-valued struct/union members; they
165  *	  are not displayed by default
166  *	- BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
167  *	  data before displaying it.
168  */
169 #define BTF_SHOW_COMPACT	BTF_F_COMPACT
170 #define BTF_SHOW_NONAME		BTF_F_NONAME
171 #define BTF_SHOW_PTR_RAW	BTF_F_PTR_RAW
172 #define BTF_SHOW_ZERO		BTF_F_ZERO
173 #define BTF_SHOW_UNSAFE		(1ULL << 4)
174 
175 void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
176 		       struct seq_file *m);
177 int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
178 			    struct seq_file *m, u64 flags);
179 
180 /*
181  * Copy len bytes of string representation of obj of BTF type_id into buf.
182  *
183  * @btf: struct btf object
184  * @type_id: type id of type obj points to
185  * @obj: pointer to typed data
186  * @buf: buffer to write to
187  * @len: maximum length to write to buf
188  * @flags: show options (see above)
189  *
190  * Return: length that would have been/was copied as per snprintf, or
191  *	   negative error.
192  */
193 int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
194 			   char *buf, int len, u64 flags);
195 
196 int btf_get_fd_by_id(u32 id);
197 u32 btf_obj_id(const struct btf *btf);
198 bool btf_is_kernel(const struct btf *btf);
199 bool btf_is_module(const struct btf *btf);
200 struct module *btf_try_get_module(const struct btf *btf);
201 u32 btf_nr_types(const struct btf *btf);
202 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
203 			   const struct btf_member *m,
204 			   u32 expected_offset, u32 expected_size);
205 int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
206 int btf_find_timer(const struct btf *btf, const struct btf_type *t);
207 struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t,
208 				    u32 field_mask, u32 value_size);
209 int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec);
210 struct btf_field_offs *btf_parse_field_offs(struct btf_record *rec);
211 bool btf_type_is_void(const struct btf_type *t);
212 s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
213 const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
214 					       u32 id, u32 *res_id);
215 const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
216 					    u32 id, u32 *res_id);
217 const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
218 						 u32 id, u32 *res_id);
219 const struct btf_type *
220 btf_resolve_size(const struct btf *btf, const struct btf_type *type,
221 		 u32 *type_size);
222 const char *btf_type_str(const struct btf_type *t);
223 
224 #define for_each_member(i, struct_type, member)			\
225 	for (i = 0, member = btf_type_member(struct_type);	\
226 	     i < btf_type_vlen(struct_type);			\
227 	     i++, member++)
228 
229 #define for_each_vsi(i, datasec_type, member)			\
230 	for (i = 0, member = btf_type_var_secinfo(datasec_type);	\
231 	     i < btf_type_vlen(datasec_type);			\
232 	     i++, member++)
233 
234 static inline bool btf_type_is_ptr(const struct btf_type *t)
235 {
236 	return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
237 }
238 
239 static inline bool btf_type_is_int(const struct btf_type *t)
240 {
241 	return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
242 }
243 
244 static inline bool btf_type_is_small_int(const struct btf_type *t)
245 {
246 	return btf_type_is_int(t) && t->size <= sizeof(u64);
247 }
248 
249 static inline u8 btf_int_encoding(const struct btf_type *t)
250 {
251 	return BTF_INT_ENCODING(*(u32 *)(t + 1));
252 }
253 
254 static inline bool btf_type_is_signed_int(const struct btf_type *t)
255 {
256 	return btf_type_is_int(t) && (btf_int_encoding(t) & BTF_INT_SIGNED);
257 }
258 
259 static inline bool btf_type_is_enum(const struct btf_type *t)
260 {
261 	return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
262 }
263 
264 static inline bool btf_is_any_enum(const struct btf_type *t)
265 {
266 	return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
267 	       BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64;
268 }
269 
270 static inline bool btf_kind_core_compat(const struct btf_type *t1,
271 					const struct btf_type *t2)
272 {
273 	return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) ||
274 	       (btf_is_any_enum(t1) && btf_is_any_enum(t2));
275 }
276 
277 static inline bool str_is_empty(const char *s)
278 {
279 	return !s || !s[0];
280 }
281 
282 static inline u16 btf_kind(const struct btf_type *t)
283 {
284 	return BTF_INFO_KIND(t->info);
285 }
286 
287 static inline bool btf_is_enum(const struct btf_type *t)
288 {
289 	return btf_kind(t) == BTF_KIND_ENUM;
290 }
291 
292 static inline bool btf_is_enum64(const struct btf_type *t)
293 {
294 	return btf_kind(t) == BTF_KIND_ENUM64;
295 }
296 
297 static inline u64 btf_enum64_value(const struct btf_enum64 *e)
298 {
299 	return ((u64)e->val_hi32 << 32) | e->val_lo32;
300 }
301 
302 static inline bool btf_is_composite(const struct btf_type *t)
303 {
304 	u16 kind = btf_kind(t);
305 
306 	return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
307 }
308 
309 static inline bool btf_is_array(const struct btf_type *t)
310 {
311 	return btf_kind(t) == BTF_KIND_ARRAY;
312 }
313 
314 static inline bool btf_is_int(const struct btf_type *t)
315 {
316 	return btf_kind(t) == BTF_KIND_INT;
317 }
318 
319 static inline bool btf_is_ptr(const struct btf_type *t)
320 {
321 	return btf_kind(t) == BTF_KIND_PTR;
322 }
323 
324 static inline u8 btf_int_offset(const struct btf_type *t)
325 {
326 	return BTF_INT_OFFSET(*(u32 *)(t + 1));
327 }
328 
329 static inline bool btf_type_is_scalar(const struct btf_type *t)
330 {
331 	return btf_type_is_int(t) || btf_type_is_enum(t);
332 }
333 
334 static inline bool btf_type_is_typedef(const struct btf_type *t)
335 {
336 	return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
337 }
338 
339 static inline bool btf_type_is_volatile(const struct btf_type *t)
340 {
341 	return BTF_INFO_KIND(t->info) == BTF_KIND_VOLATILE;
342 }
343 
344 static inline bool btf_type_is_func(const struct btf_type *t)
345 {
346 	return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
347 }
348 
349 static inline bool btf_type_is_func_proto(const struct btf_type *t)
350 {
351 	return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
352 }
353 
354 static inline bool btf_type_is_var(const struct btf_type *t)
355 {
356 	return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
357 }
358 
359 static inline bool btf_type_is_type_tag(const struct btf_type *t)
360 {
361 	return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
362 }
363 
364 /* union is only a special case of struct:
365  * all its offsetof(member) == 0
366  */
367 static inline bool btf_type_is_struct(const struct btf_type *t)
368 {
369 	u8 kind = BTF_INFO_KIND(t->info);
370 
371 	return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
372 }
373 
374 static inline bool __btf_type_is_struct(const struct btf_type *t)
375 {
376 	return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT;
377 }
378 
379 static inline bool btf_type_is_array(const struct btf_type *t)
380 {
381 	return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
382 }
383 
384 static inline u16 btf_type_vlen(const struct btf_type *t)
385 {
386 	return BTF_INFO_VLEN(t->info);
387 }
388 
389 static inline u16 btf_vlen(const struct btf_type *t)
390 {
391 	return btf_type_vlen(t);
392 }
393 
394 static inline u16 btf_func_linkage(const struct btf_type *t)
395 {
396 	return BTF_INFO_VLEN(t->info);
397 }
398 
399 static inline bool btf_type_kflag(const struct btf_type *t)
400 {
401 	return BTF_INFO_KFLAG(t->info);
402 }
403 
404 static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
405 					  const struct btf_member *member)
406 {
407 	return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
408 					   : member->offset;
409 }
410 
411 static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
412 					     const struct btf_member *member)
413 {
414 	return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
415 					   : 0;
416 }
417 
418 static inline struct btf_member *btf_members(const struct btf_type *t)
419 {
420 	return (struct btf_member *)(t + 1);
421 }
422 
423 static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
424 {
425 	const struct btf_member *m = btf_members(t) + member_idx;
426 
427 	return __btf_member_bit_offset(t, m);
428 }
429 
430 static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
431 {
432 	const struct btf_member *m = btf_members(t) + member_idx;
433 
434 	return __btf_member_bitfield_size(t, m);
435 }
436 
437 static inline const struct btf_member *btf_type_member(const struct btf_type *t)
438 {
439 	return (const struct btf_member *)(t + 1);
440 }
441 
442 static inline struct btf_array *btf_array(const struct btf_type *t)
443 {
444 	return (struct btf_array *)(t + 1);
445 }
446 
447 static inline struct btf_enum *btf_enum(const struct btf_type *t)
448 {
449 	return (struct btf_enum *)(t + 1);
450 }
451 
452 static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
453 {
454 	return (struct btf_enum64 *)(t + 1);
455 }
456 
457 static inline const struct btf_var_secinfo *btf_type_var_secinfo(
458 		const struct btf_type *t)
459 {
460 	return (const struct btf_var_secinfo *)(t + 1);
461 }
462 
463 static inline struct btf_param *btf_params(const struct btf_type *t)
464 {
465 	return (struct btf_param *)(t + 1);
466 }
467 
468 static inline int btf_id_cmp_func(const void *a, const void *b)
469 {
470 	const int *pa = a, *pb = b;
471 
472 	return *pa - *pb;
473 }
474 
475 static inline bool btf_id_set_contains(const struct btf_id_set *set, u32 id)
476 {
477 	return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL;
478 }
479 
480 static inline void *btf_id_set8_contains(const struct btf_id_set8 *set, u32 id)
481 {
482 	return bsearch(&id, set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func);
483 }
484 
485 struct bpf_prog;
486 struct bpf_verifier_log;
487 
488 #ifdef CONFIG_BPF_SYSCALL
489 const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
490 const char *btf_name_by_offset(const struct btf *btf, u32 offset);
491 struct btf *btf_parse_vmlinux(void);
492 struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
493 u32 *btf_kfunc_id_set_contains(const struct btf *btf,
494 			       enum bpf_prog_type prog_type,
495 			       u32 kfunc_btf_id);
496 u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id);
497 int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
498 			      const struct btf_kfunc_id_set *s);
499 int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset);
500 s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
501 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
502 				struct module *owner);
503 struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id);
504 const struct btf_member *
505 btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
506 		      const struct btf_type *t, enum bpf_prog_type prog_type,
507 		      int arg);
508 int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type);
509 bool btf_types_are_same(const struct btf *btf1, u32 id1,
510 			const struct btf *btf2, u32 id2);
511 #else
512 static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
513 						    u32 type_id)
514 {
515 	return NULL;
516 }
517 static inline const char *btf_name_by_offset(const struct btf *btf,
518 					     u32 offset)
519 {
520 	return NULL;
521 }
522 static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf,
523 					     enum bpf_prog_type prog_type,
524 					     u32 kfunc_btf_id)
525 {
526 	return NULL;
527 }
528 static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
529 					    const struct btf_kfunc_id_set *s)
530 {
531 	return 0;
532 }
533 static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
534 {
535 	return -ENOENT;
536 }
537 static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
538 					      u32 add_cnt, struct module *owner)
539 {
540 	return 0;
541 }
542 static inline struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id)
543 {
544 	return NULL;
545 }
546 static inline const struct btf_member *
547 btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
548 		      const struct btf_type *t, enum bpf_prog_type prog_type,
549 		      int arg)
550 {
551 	return NULL;
552 }
553 static inline int get_kern_ctx_btf_id(struct bpf_verifier_log *log,
554 				      enum bpf_prog_type prog_type) {
555 	return -EINVAL;
556 }
557 static inline bool btf_types_are_same(const struct btf *btf1, u32 id1,
558 				      const struct btf *btf2, u32 id2)
559 {
560 	return false;
561 }
562 #endif
563 
564 static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t)
565 {
566 	if (!btf_type_is_ptr(t))
567 		return false;
568 
569 	t = btf_type_skip_modifiers(btf, t->type, NULL);
570 
571 	return btf_type_is_struct(t);
572 }
573 
574 #endif
575