brintos

brintos / linux-shallow public Read only

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