4220 lines · 130.1 KB · C
RawDownload
/* SPDX-License-Identifier: GPL-2.0-only *//* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com */#ifndef _LINUX_BPF_H#define _LINUX_BPF_H 1 #include <uapi/linux/bpf.h>#include <uapi/linux/filter.h>#include <linux/bpf_defs.h> #include <crypto/sha2.h>#include <linux/workqueue.h>#include <linux/file.h>#include <linux/percpu.h>#include <linux/err.h>#include <linux/rbtree_latch.h>#include <linux/numa.h>#include <linux/mm_types.h>#include <linux/wait.h>#include <linux/refcount.h>#include <linux/mutex.h>#include <linux/module.h>#include <linux/kallsyms.h>#include <linux/capability.h>#include <linux/sched/mm.h>#include <linux/slab.h>#include <linux/percpu-refcount.h>#include <linux/stddef.h>#include <linux/bpfptr.h>#include <linux/btf.h>#include <linux/rcupdate_trace.h>#include <linux/static_call.h>#include <linux/memcontrol.h>#include <linux/cfi.h>#include <linux/xattr.h>#include <linux/key.h>#include <linux/ftrace.h>#include <asm/rqspinlock.h> struct bpf_verifier_env;struct bpf_verifier_log;struct perf_event;struct bpf_prog;struct bpf_prog_aux;struct bpf_map;struct bpf_arena;struct sock;struct seq_file;struct btf;struct btf_type;struct exception_table_entry;struct seq_operations;struct bpf_iter_aux_info;struct bpf_local_storage;struct bpf_local_storage_map;struct kobject;struct mem_cgroup;struct module;struct bpf_func_state;struct ftrace_ops;struct cgroup;struct bpf_token;struct user_namespace;struct super_block;struct inode; extern struct idr btf_idr;extern spinlock_t btf_idr_lock;extern struct kobject *btf_kobj;extern struct bpf_mem_alloc bpf_global_ma, bpf_global_percpu_ma;extern bool bpf_global_ma_set; typedef u64 (*bpf_callback_t)(u64, u64, u64, u64, u64);typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,					struct bpf_iter_aux_info *aux);typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);typedef unsigned int (*bpf_func_t)(const void *,				   const struct bpf_insn *);struct bpf_iter_seq_info {	const struct seq_operations *seq_ops;	bpf_iter_init_seq_priv_t init_seq_private;	bpf_iter_fini_seq_priv_t fini_seq_private;	u32 seq_priv_size;}; /* map is generic key/value storage optionally accessible by eBPF programs */struct bpf_map_ops {	/* funcs callable from userspace (via syscall) */	int (*map_alloc_check)(union bpf_attr *attr);	struct bpf_map *(*map_alloc)(union bpf_attr *attr);	void (*map_release)(struct bpf_map *map, struct file *map_file);	void (*map_free)(struct bpf_map *map);	int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);	void (*map_release_uref)(struct bpf_map *map);	void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);	int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr,				union bpf_attr __user *uattr);	int (*map_lookup_and_delete_elem)(struct bpf_map *map, void *key,					  void *value, u64 flags);	int (*map_lookup_and_delete_batch)(struct bpf_map *map,					   const union bpf_attr *attr,					   union bpf_attr __user *uattr);	int (*map_update_batch)(struct bpf_map *map, struct file *map_file,				const union bpf_attr *attr,				union bpf_attr __user *uattr);	int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr,				union bpf_attr __user *uattr); 	/* funcs callable from userspace and from eBPF programs */	void *(*map_lookup_elem)(struct bpf_map *map, void *key);	long (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);	long (*map_delete_elem)(struct bpf_map *map, void *key);	long (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);	long (*map_pop_elem)(struct bpf_map *map, void *value);	long (*map_peek_elem)(struct bpf_map *map, void *value);	void *(*map_lookup_percpu_elem)(struct bpf_map *map, void *key, u32 cpu);	int (*map_get_hash)(struct bpf_map *map); 	/* funcs called by prog_array and perf_event_array map */	void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,				int fd);	/* If need_defer is true, the implementation should guarantee that	 * the to-be-put element is still alive before the bpf program, which	 * may manipulate it, exists.	 */	void (*map_fd_put_ptr)(struct bpf_map *map, void *ptr, bool need_defer);	int (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);	u32 (*map_fd_sys_lookup_elem)(void *ptr);	void (*map_seq_show_elem)(struct bpf_map *map, void *key,				  struct seq_file *m);	int (*map_check_btf)(struct bpf_map *map,			     const struct btf *btf,			     const struct btf_type *key_type,			     const struct btf_type *value_type); 	/* Prog poke tracking helpers. */	int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux);	void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux);	void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old,			     struct bpf_prog *new); 	/* Direct value access helpers. */	int (*map_direct_value_addr)(const struct bpf_map *map,				     u64 *imm, u32 off);	int (*map_direct_value_meta)(const struct bpf_map *map,				     u64 imm, u32 *off);	int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);	vm_fault_t (*map_mmap_fault)(struct bpf_map *map, struct vm_fault *vmf);	__poll_t (*map_poll)(struct bpf_map *map, struct file *filp,			     struct poll_table_struct *pts);	unsigned long (*map_get_unmapped_area)(struct file *filep, unsigned long addr,					       unsigned long len, unsigned long pgoff,					       unsigned long flags); 	/* Functions called by bpf_local_storage maps */	int (*map_local_storage_charge)(struct bpf_local_storage_map *smap,					void *owner, u32 size);	void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap,					   void *owner, u32 size);	struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(void *owner); 	/* Misc helpers.*/	long (*map_redirect)(struct bpf_map *map, u64 key, u64 flags); 	/* map_meta_equal must be implemented for maps that can be	 * used as an inner map.  It is a runtime check to ensure	 * an inner map can be inserted to an outer map.	 *	 * Some properties of the inner map has been used during the	 * verification time.  When inserting an inner map at the runtime,	 * map_meta_equal has to ensure the inserting map has the same	 * properties that the verifier has used earlier.	 */	bool (*map_meta_equal)(const struct bpf_map *meta0,			       const struct bpf_map *meta1);  	int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,					      struct bpf_func_state *caller,					      struct bpf_func_state *callee);	long (*map_for_each_callback)(struct bpf_map *map,				     bpf_callback_t callback_fn,				     void *callback_ctx, u64 flags); 	u64 (*map_mem_usage)(const struct bpf_map *map); 	/* BTF id of struct allocated by map_alloc */	int *map_btf_id; 	/* bpf_iter info used to open a seq_file */	const struct bpf_iter_seq_info *iter_seq_info;}; enum {	/* Support at most 11 fields in a BTF type */	BTF_FIELDS_MAX	   = 11,}; enum btf_field_type {	BPF_SPIN_LOCK  = (1 << 0),	BPF_TIMER      = (1 << 1),	BPF_KPTR_UNREF = (1 << 2),	BPF_KPTR_REF   = (1 << 3),	BPF_KPTR_PERCPU = (1 << 4),	BPF_KPTR       = BPF_KPTR_UNREF | BPF_KPTR_REF | BPF_KPTR_PERCPU,	BPF_LIST_HEAD  = (1 << 5),	BPF_LIST_NODE  = (1 << 6),	BPF_RB_ROOT    = (1 << 7),	BPF_RB_NODE    = (1 << 8),	BPF_GRAPH_NODE = BPF_RB_NODE | BPF_LIST_NODE,	BPF_GRAPH_ROOT = BPF_RB_ROOT | BPF_LIST_HEAD,	BPF_REFCOUNT   = (1 << 9),	BPF_WORKQUEUE  = (1 << 10),	BPF_UPTR       = (1 << 11),	BPF_RES_SPIN_LOCK = (1 << 12),	BPF_TASK_WORK  = (1 << 13),}; enum bpf_cgroup_storage_type {	BPF_CGROUP_STORAGE_SHARED,	BPF_CGROUP_STORAGE_PERCPU,	__BPF_CGROUP_STORAGE_MAX#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX}; #ifdef CONFIG_CGROUP_BPF# define for_each_cgroup_storage_type(stype) \	for (stype = 0; stype < MAX_BPF_CGROUP_STORAGE_TYPE; stype++)#else# define for_each_cgroup_storage_type(stype) for (; false; )#endif /* CONFIG_CGROUP_BPF */ typedef void (*btf_dtor_kfunc_t)(void *); struct btf_field_kptr {	struct btf *btf;	struct module *module;	/* dtor used if btf_is_kernel(btf), otherwise the type is	 * program-allocated, dtor is NULL,  and __bpf_obj_drop_impl is used	 */	btf_dtor_kfunc_t dtor;	u32 btf_id;}; struct btf_field_graph_root {	struct btf *btf;	u32 value_btf_id;	u32 node_offset;	struct btf_record *value_rec;}; struct btf_field {	u32 offset;	u32 size;	enum btf_field_type type;	union {		struct btf_field_kptr kptr;		struct btf_field_graph_root graph_root;	};}; struct btf_record {	u32 cnt;	u32 field_mask;	int spin_lock_off;	int res_spin_lock_off;	int timer_off;	int wq_off;	int refcount_off;	int task_work_off;	struct btf_field fields[];}; /* Non-opaque version of bpf_rb_node in uapi/linux/bpf.h */struct bpf_rb_node_kern {	struct rb_node rb_node;	void *owner;} __attribute__((aligned(8))); /* Non-opaque version of bpf_list_node in uapi/linux/bpf.h */struct bpf_list_node_kern {	struct list_head list_head;	void *owner;} __attribute__((aligned(8))); /* 'Ownership' of program-containing map is claimed by the first program * that is going to use this map or by the first program which FD is * stored in the map to make sure that all callers and callees have the * same prog type, JITed flag and xdp_has_frags flag. */struct bpf_map_owner {	enum bpf_prog_type type;	bool jited;	bool xdp_has_frags;	bool sleepable;	u64 storage_cookie[MAX_BPF_CGROUP_STORAGE_TYPE];	const struct btf_type *attach_func_proto;	enum bpf_attach_type expected_attach_type;}; struct bpf_map {	u8 sha[SHA256_DIGEST_SIZE];	const struct bpf_map_ops *ops;	struct bpf_map *inner_map_meta;#ifdef CONFIG_SECURITY	void *security;#endif	enum bpf_map_type map_type;	u32 key_size;	u32 value_size;	u32 max_entries;	u64 map_extra; /* any per-map-type extra fields */	u32 map_flags;	u32 id;	struct btf_record *record;	int numa_node;	u32 btf_key_type_id;	u32 btf_value_type_id;	u32 btf_vmlinux_value_type_id;	struct btf *btf;#ifdef CONFIG_MEMCG	struct obj_cgroup *objcg;#endif	char name[BPF_OBJ_NAME_LEN];	struct mutex freeze_mutex;	atomic64_t refcnt;	atomic64_t usercnt;	/* rcu is used before freeing and work is only used during freeing */	union {		struct work_struct work;		struct rcu_head rcu;	};	atomic64_t writecnt;	spinlock_t owner_lock;	struct bpf_map_owner *owner;	bool bypass_spec_v1;	bool frozen; /* write-once; write-protected by freeze_mutex */	bool free_after_mult_rcu_gp;	bool free_after_rcu_gp;	atomic64_t sleepable_refcnt;	s64 __percpu *elem_count;	u64 cookie; /* write-once */	char *excl_prog_sha;}; static inline const char *btf_field_type_name(enum btf_field_type type){	switch (type) {	case BPF_SPIN_LOCK:		return "bpf_spin_lock";	case BPF_RES_SPIN_LOCK:		return "bpf_res_spin_lock";	case BPF_TIMER:		return "bpf_timer";	case BPF_WORKQUEUE:		return "bpf_wq";	case BPF_KPTR_UNREF:	case BPF_KPTR_REF:		return "kptr";	case BPF_KPTR_PERCPU:		return "percpu_kptr";	case BPF_UPTR:		return "uptr";	case BPF_LIST_HEAD:		return "bpf_list_head";	case BPF_LIST_NODE:		return "bpf_list_node";	case BPF_RB_ROOT:		return "bpf_rb_root";	case BPF_RB_NODE:		return "bpf_rb_node";	case BPF_REFCOUNT:		return "bpf_refcount";	case BPF_TASK_WORK:		return "bpf_task_work";	default:		WARN_ON_ONCE(1);		return "unknown";	}} #if IS_ENABLED(CONFIG_DEBUG_KERNEL)#define BPF_WARN_ONCE(cond, format...) WARN_ONCE(cond, format)#else#define BPF_WARN_ONCE(cond, format...) BUILD_BUG_ON_INVALID(cond)#endif static inline u32 btf_field_type_size(enum btf_field_type type){	switch (type) {	case BPF_SPIN_LOCK:		return sizeof(struct bpf_spin_lock);	case BPF_RES_SPIN_LOCK:		return sizeof(struct bpf_res_spin_lock);	case BPF_TIMER:		return sizeof(struct bpf_timer);	case BPF_WORKQUEUE:		return sizeof(struct bpf_wq);	case BPF_KPTR_UNREF:	case BPF_KPTR_REF:	case BPF_KPTR_PERCPU:	case BPF_UPTR:		return sizeof(u64);	case BPF_LIST_HEAD:		return sizeof(struct bpf_list_head);	case BPF_LIST_NODE:		return sizeof(struct bpf_list_node);	case BPF_RB_ROOT:		return sizeof(struct bpf_rb_root);	case BPF_RB_NODE:		return sizeof(struct bpf_rb_node);	case BPF_REFCOUNT:		return sizeof(struct bpf_refcount);	case BPF_TASK_WORK:		return sizeof(struct bpf_task_work);	default:		WARN_ON_ONCE(1);		return 0;	}} static inline u32 btf_field_type_align(enum btf_field_type type){	switch (type) {	case BPF_SPIN_LOCK:		return __alignof__(struct bpf_spin_lock);	case BPF_RES_SPIN_LOCK:		return __alignof__(struct bpf_res_spin_lock);	case BPF_TIMER:		return __alignof__(struct bpf_timer);	case BPF_WORKQUEUE:		return __alignof__(struct bpf_wq);	case BPF_KPTR_UNREF:	case BPF_KPTR_REF:	case BPF_KPTR_PERCPU:	case BPF_UPTR:		return __alignof__(u64);	case BPF_LIST_HEAD:		return __alignof__(struct bpf_list_head);	case BPF_LIST_NODE:		return __alignof__(struct bpf_list_node);	case BPF_RB_ROOT:		return __alignof__(struct bpf_rb_root);	case BPF_RB_NODE:		return __alignof__(struct bpf_rb_node);	case BPF_REFCOUNT:		return __alignof__(struct bpf_refcount);	case BPF_TASK_WORK:		return __alignof__(struct bpf_task_work);	default:		WARN_ON_ONCE(1);		return 0;	}} static inline void bpf_obj_init_field(const struct btf_field *field, void *addr){	memset(addr, 0, field->size); 	switch (field->type) {	case BPF_REFCOUNT:		refcount_set((refcount_t *)addr, 1);		break;	case BPF_RB_NODE:		RB_CLEAR_NODE((struct rb_node *)addr);		break;	case BPF_LIST_HEAD:	case BPF_LIST_NODE:		INIT_LIST_HEAD((struct list_head *)addr);		break;	case BPF_RB_ROOT:		/* RB_ROOT_CACHED 0-inits, no need to do anything after memset */	case BPF_SPIN_LOCK:	case BPF_RES_SPIN_LOCK:	case BPF_TIMER:	case BPF_WORKQUEUE:	case BPF_KPTR_UNREF:	case BPF_KPTR_REF:	case BPF_KPTR_PERCPU:	case BPF_UPTR:	case BPF_TASK_WORK:		break;	default:		WARN_ON_ONCE(1);		return;	}} static inline bool btf_record_has_field(const struct btf_record *rec, enum btf_field_type type){	if (IS_ERR_OR_NULL(rec))		return false;	return rec->field_mask & type;} static inline bool btf_field_is_nmi_safe(enum btf_field_type type){	switch (type) {	case BPF_SPIN_LOCK:	case BPF_RES_SPIN_LOCK:

Lines 1–500 of 4220.