Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
0003  */
0004 #ifndef _LINUX_BPF_H
0005 #define _LINUX_BPF_H 1
0006 
0007 #include <uapi/linux/bpf.h>
0008 #include <uapi/linux/filter.h>
0009 
0010 #include <linux/workqueue.h>
0011 #include <linux/file.h>
0012 #include <linux/percpu.h>
0013 #include <linux/err.h>
0014 #include <linux/rbtree_latch.h>
0015 #include <linux/numa.h>
0016 #include <linux/mm_types.h>
0017 #include <linux/wait.h>
0018 #include <linux/refcount.h>
0019 #include <linux/mutex.h>
0020 #include <linux/module.h>
0021 #include <linux/kallsyms.h>
0022 #include <linux/capability.h>
0023 #include <linux/sched/mm.h>
0024 #include <linux/slab.h>
0025 #include <linux/percpu-refcount.h>
0026 #include <linux/stddef.h>
0027 #include <linux/bpfptr.h>
0028 #include <linux/btf.h>
0029 #include <linux/rcupdate_trace.h>
0030 
0031 struct bpf_verifier_env;
0032 struct bpf_verifier_log;
0033 struct perf_event;
0034 struct bpf_prog;
0035 struct bpf_prog_aux;
0036 struct bpf_map;
0037 struct sock;
0038 struct seq_file;
0039 struct btf;
0040 struct btf_type;
0041 struct exception_table_entry;
0042 struct seq_operations;
0043 struct bpf_iter_aux_info;
0044 struct bpf_local_storage;
0045 struct bpf_local_storage_map;
0046 struct kobject;
0047 struct mem_cgroup;
0048 struct module;
0049 struct bpf_func_state;
0050 struct ftrace_ops;
0051 
0052 extern struct idr btf_idr;
0053 extern spinlock_t btf_idr_lock;
0054 extern struct kobject *btf_kobj;
0055 
0056 typedef u64 (*bpf_callback_t)(u64, u64, u64, u64, u64);
0057 typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,
0058                     struct bpf_iter_aux_info *aux);
0059 typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
0060 typedef unsigned int (*bpf_func_t)(const void *,
0061                    const struct bpf_insn *);
0062 struct bpf_iter_seq_info {
0063     const struct seq_operations *seq_ops;
0064     bpf_iter_init_seq_priv_t init_seq_private;
0065     bpf_iter_fini_seq_priv_t fini_seq_private;
0066     u32 seq_priv_size;
0067 };
0068 
0069 /* map is generic key/value storage optionally accessible by eBPF programs */
0070 struct bpf_map_ops {
0071     /* funcs callable from userspace (via syscall) */
0072     int (*map_alloc_check)(union bpf_attr *attr);
0073     struct bpf_map *(*map_alloc)(union bpf_attr *attr);
0074     void (*map_release)(struct bpf_map *map, struct file *map_file);
0075     void (*map_free)(struct bpf_map *map);
0076     int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
0077     void (*map_release_uref)(struct bpf_map *map);
0078     void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
0079     int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr,
0080                 union bpf_attr __user *uattr);
0081     int (*map_lookup_and_delete_elem)(struct bpf_map *map, void *key,
0082                       void *value, u64 flags);
0083     int (*map_lookup_and_delete_batch)(struct bpf_map *map,
0084                        const union bpf_attr *attr,
0085                        union bpf_attr __user *uattr);
0086     int (*map_update_batch)(struct bpf_map *map, const union bpf_attr *attr,
0087                 union bpf_attr __user *uattr);
0088     int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr,
0089                 union bpf_attr __user *uattr);
0090 
0091     /* funcs callable from userspace and from eBPF programs */
0092     void *(*map_lookup_elem)(struct bpf_map *map, void *key);
0093     int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
0094     int (*map_delete_elem)(struct bpf_map *map, void *key);
0095     int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
0096     int (*map_pop_elem)(struct bpf_map *map, void *value);
0097     int (*map_peek_elem)(struct bpf_map *map, void *value);
0098     void *(*map_lookup_percpu_elem)(struct bpf_map *map, void *key, u32 cpu);
0099 
0100     /* funcs called by prog_array and perf_event_array map */
0101     void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
0102                 int fd);
0103     void (*map_fd_put_ptr)(void *ptr);
0104     int (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
0105     u32 (*map_fd_sys_lookup_elem)(void *ptr);
0106     void (*map_seq_show_elem)(struct bpf_map *map, void *key,
0107                   struct seq_file *m);
0108     int (*map_check_btf)(const struct bpf_map *map,
0109                  const struct btf *btf,
0110                  const struct btf_type *key_type,
0111                  const struct btf_type *value_type);
0112 
0113     /* Prog poke tracking helpers. */
0114     int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux);
0115     void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux);
0116     void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old,
0117                  struct bpf_prog *new);
0118 
0119     /* Direct value access helpers. */
0120     int (*map_direct_value_addr)(const struct bpf_map *map,
0121                      u64 *imm, u32 off);
0122     int (*map_direct_value_meta)(const struct bpf_map *map,
0123                      u64 imm, u32 *off);
0124     int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
0125     __poll_t (*map_poll)(struct bpf_map *map, struct file *filp,
0126                  struct poll_table_struct *pts);
0127 
0128     /* Functions called by bpf_local_storage maps */
0129     int (*map_local_storage_charge)(struct bpf_local_storage_map *smap,
0130                     void *owner, u32 size);
0131     void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap,
0132                        void *owner, u32 size);
0133     struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(void *owner);
0134 
0135     /* Misc helpers.*/
0136     int (*map_redirect)(struct bpf_map *map, u32 ifindex, u64 flags);
0137 
0138     /* map_meta_equal must be implemented for maps that can be
0139      * used as an inner map.  It is a runtime check to ensure
0140      * an inner map can be inserted to an outer map.
0141      *
0142      * Some properties of the inner map has been used during the
0143      * verification time.  When inserting an inner map at the runtime,
0144      * map_meta_equal has to ensure the inserting map has the same
0145      * properties that the verifier has used earlier.
0146      */
0147     bool (*map_meta_equal)(const struct bpf_map *meta0,
0148                    const struct bpf_map *meta1);
0149 
0150 
0151     int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,
0152                           struct bpf_func_state *caller,
0153                           struct bpf_func_state *callee);
0154     int (*map_for_each_callback)(struct bpf_map *map,
0155                      bpf_callback_t callback_fn,
0156                      void *callback_ctx, u64 flags);
0157 
0158     /* BTF id of struct allocated by map_alloc */
0159     int *map_btf_id;
0160 
0161     /* bpf_iter info used to open a seq_file */
0162     const struct bpf_iter_seq_info *iter_seq_info;
0163 };
0164 
0165 enum {
0166     /* Support at most 8 pointers in a BPF map value */
0167     BPF_MAP_VALUE_OFF_MAX = 8,
0168     BPF_MAP_OFF_ARR_MAX   = BPF_MAP_VALUE_OFF_MAX +
0169                 1 + /* for bpf_spin_lock */
0170                 1,  /* for bpf_timer */
0171 };
0172 
0173 enum bpf_kptr_type {
0174     BPF_KPTR_UNREF,
0175     BPF_KPTR_REF,
0176 };
0177 
0178 struct bpf_map_value_off_desc {
0179     u32 offset;
0180     enum bpf_kptr_type type;
0181     struct {
0182         struct btf *btf;
0183         struct module *module;
0184         btf_dtor_kfunc_t dtor;
0185         u32 btf_id;
0186     } kptr;
0187 };
0188 
0189 struct bpf_map_value_off {
0190     u32 nr_off;
0191     struct bpf_map_value_off_desc off[];
0192 };
0193 
0194 struct bpf_map_off_arr {
0195     u32 cnt;
0196     u32 field_off[BPF_MAP_OFF_ARR_MAX];
0197     u8 field_sz[BPF_MAP_OFF_ARR_MAX];
0198 };
0199 
0200 struct bpf_map {
0201     /* The first two cachelines with read-mostly members of which some
0202      * are also accessed in fast-path (e.g. ops, max_entries).
0203      */
0204     const struct bpf_map_ops *ops ____cacheline_aligned;
0205     struct bpf_map *inner_map_meta;
0206 #ifdef CONFIG_SECURITY
0207     void *security;
0208 #endif
0209     enum bpf_map_type map_type;
0210     u32 key_size;
0211     u32 value_size;
0212     u32 max_entries;
0213     u64 map_extra; /* any per-map-type extra fields */
0214     u32 map_flags;
0215     int spin_lock_off; /* >=0 valid offset, <0 error */
0216     struct bpf_map_value_off *kptr_off_tab;
0217     int timer_off; /* >=0 valid offset, <0 error */
0218     u32 id;
0219     int numa_node;
0220     u32 btf_key_type_id;
0221     u32 btf_value_type_id;
0222     u32 btf_vmlinux_value_type_id;
0223     struct btf *btf;
0224 #ifdef CONFIG_MEMCG_KMEM
0225     struct obj_cgroup *objcg;
0226 #endif
0227     char name[BPF_OBJ_NAME_LEN];
0228     struct bpf_map_off_arr *off_arr;
0229     /* The 3rd and 4th cacheline with misc members to avoid false sharing
0230      * particularly with refcounting.
0231      */
0232     atomic64_t refcnt ____cacheline_aligned;
0233     atomic64_t usercnt;
0234     struct work_struct work;
0235     struct mutex freeze_mutex;
0236     atomic64_t writecnt;
0237     /* 'Ownership' of program-containing map is claimed by the first program
0238      * that is going to use this map or by the first program which FD is
0239      * stored in the map to make sure that all callers and callees have the
0240      * same prog type, JITed flag and xdp_has_frags flag.
0241      */
0242     struct {
0243         spinlock_t lock;
0244         enum bpf_prog_type type;
0245         bool jited;
0246         bool xdp_has_frags;
0247     } owner;
0248     bool bypass_spec_v1;
0249     bool frozen; /* write-once; write-protected by freeze_mutex */
0250 };
0251 
0252 static inline bool map_value_has_spin_lock(const struct bpf_map *map)
0253 {
0254     return map->spin_lock_off >= 0;
0255 }
0256 
0257 static inline bool map_value_has_timer(const struct bpf_map *map)
0258 {
0259     return map->timer_off >= 0;
0260 }
0261 
0262 static inline bool map_value_has_kptrs(const struct bpf_map *map)
0263 {
0264     return !IS_ERR_OR_NULL(map->kptr_off_tab);
0265 }
0266 
0267 static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
0268 {
0269     if (unlikely(map_value_has_spin_lock(map)))
0270         memset(dst + map->spin_lock_off, 0, sizeof(struct bpf_spin_lock));
0271     if (unlikely(map_value_has_timer(map)))
0272         memset(dst + map->timer_off, 0, sizeof(struct bpf_timer));
0273     if (unlikely(map_value_has_kptrs(map))) {
0274         struct bpf_map_value_off *tab = map->kptr_off_tab;
0275         int i;
0276 
0277         for (i = 0; i < tab->nr_off; i++)
0278             *(u64 *)(dst + tab->off[i].offset) = 0;
0279     }
0280 }
0281 
0282 /* copy everything but bpf_spin_lock and bpf_timer. There could be one of each. */
0283 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
0284 {
0285     u32 curr_off = 0;
0286     int i;
0287 
0288     if (likely(!map->off_arr)) {
0289         memcpy(dst, src, map->value_size);
0290         return;
0291     }
0292 
0293     for (i = 0; i < map->off_arr->cnt; i++) {
0294         u32 next_off = map->off_arr->field_off[i];
0295 
0296         memcpy(dst + curr_off, src + curr_off, next_off - curr_off);
0297         curr_off += map->off_arr->field_sz[i];
0298     }
0299     memcpy(dst + curr_off, src + curr_off, map->value_size - curr_off);
0300 }
0301 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
0302                bool lock_src);
0303 void bpf_timer_cancel_and_free(void *timer);
0304 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
0305 
0306 struct bpf_offload_dev;
0307 struct bpf_offloaded_map;
0308 
0309 struct bpf_map_dev_ops {
0310     int (*map_get_next_key)(struct bpf_offloaded_map *map,
0311                 void *key, void *next_key);
0312     int (*map_lookup_elem)(struct bpf_offloaded_map *map,
0313                    void *key, void *value);
0314     int (*map_update_elem)(struct bpf_offloaded_map *map,
0315                    void *key, void *value, u64 flags);
0316     int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
0317 };
0318 
0319 struct bpf_offloaded_map {
0320     struct bpf_map map;
0321     struct net_device *netdev;
0322     const struct bpf_map_dev_ops *dev_ops;
0323     void *dev_priv;
0324     struct list_head offloads;
0325 };
0326 
0327 static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
0328 {
0329     return container_of(map, struct bpf_offloaded_map, map);
0330 }
0331 
0332 static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
0333 {
0334     return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
0335 }
0336 
0337 static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
0338 {
0339     return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) &&
0340         map->ops->map_seq_show_elem;
0341 }
0342 
0343 int map_check_no_btf(const struct bpf_map *map,
0344              const struct btf *btf,
0345              const struct btf_type *key_type,
0346              const struct btf_type *value_type);
0347 
0348 bool bpf_map_meta_equal(const struct bpf_map *meta0,
0349             const struct bpf_map *meta1);
0350 
0351 extern const struct bpf_map_ops bpf_map_offload_ops;
0352 
0353 /* bpf_type_flag contains a set of flags that are applicable to the values of
0354  * arg_type, ret_type and reg_type. For example, a pointer value may be null,
0355  * or a memory is read-only. We classify types into two categories: base types
0356  * and extended types. Extended types are base types combined with a type flag.
0357  *
0358  * Currently there are no more than 32 base types in arg_type, ret_type and
0359  * reg_types.
0360  */
0361 #define BPF_BASE_TYPE_BITS  8
0362 
0363 enum bpf_type_flag {
0364     /* PTR may be NULL. */
0365     PTR_MAYBE_NULL      = BIT(0 + BPF_BASE_TYPE_BITS),
0366 
0367     /* MEM is read-only. When applied on bpf_arg, it indicates the arg is
0368      * compatible with both mutable and immutable memory.
0369      */
0370     MEM_RDONLY      = BIT(1 + BPF_BASE_TYPE_BITS),
0371 
0372     /* MEM was "allocated" from a different helper, and cannot be mixed
0373      * with regular non-MEM_ALLOC'ed MEM types.
0374      */
0375     MEM_ALLOC       = BIT(2 + BPF_BASE_TYPE_BITS),
0376 
0377     /* MEM is in user address space. */
0378     MEM_USER        = BIT(3 + BPF_BASE_TYPE_BITS),
0379 
0380     /* MEM is a percpu memory. MEM_PERCPU tags PTR_TO_BTF_ID. When tagged
0381      * with MEM_PERCPU, PTR_TO_BTF_ID _cannot_ be directly accessed. In
0382      * order to drop this tag, it must be passed into bpf_per_cpu_ptr()
0383      * or bpf_this_cpu_ptr(), which will return the pointer corresponding
0384      * to the specified cpu.
0385      */
0386     MEM_PERCPU      = BIT(4 + BPF_BASE_TYPE_BITS),
0387 
0388     /* Indicates that the argument will be released. */
0389     OBJ_RELEASE     = BIT(5 + BPF_BASE_TYPE_BITS),
0390 
0391     /* PTR is not trusted. This is only used with PTR_TO_BTF_ID, to mark
0392      * unreferenced and referenced kptr loaded from map value using a load
0393      * instruction, so that they can only be dereferenced but not escape the
0394      * BPF program into the kernel (i.e. cannot be passed as arguments to
0395      * kfunc or bpf helpers).
0396      */
0397     PTR_UNTRUSTED       = BIT(6 + BPF_BASE_TYPE_BITS),
0398 
0399     MEM_UNINIT      = BIT(7 + BPF_BASE_TYPE_BITS),
0400 
0401     /* DYNPTR points to memory local to the bpf program. */
0402     DYNPTR_TYPE_LOCAL   = BIT(8 + BPF_BASE_TYPE_BITS),
0403 
0404     /* DYNPTR points to a ringbuf record. */
0405     DYNPTR_TYPE_RINGBUF = BIT(9 + BPF_BASE_TYPE_BITS),
0406 
0407     /* Size is known at compile time. */
0408     MEM_FIXED_SIZE      = BIT(10 + BPF_BASE_TYPE_BITS),
0409 
0410     __BPF_TYPE_FLAG_MAX,
0411     __BPF_TYPE_LAST_FLAG    = __BPF_TYPE_FLAG_MAX - 1,
0412 };
0413 
0414 #define DYNPTR_TYPE_FLAG_MASK   (DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF)
0415 
0416 /* Max number of base types. */
0417 #define BPF_BASE_TYPE_LIMIT (1UL << BPF_BASE_TYPE_BITS)
0418 
0419 /* Max number of all types. */
0420 #define BPF_TYPE_LIMIT      (__BPF_TYPE_LAST_FLAG | (__BPF_TYPE_LAST_FLAG - 1))
0421 
0422 /* function argument constraints */
0423 enum bpf_arg_type {
0424     ARG_DONTCARE = 0,   /* unused argument in helper function */
0425 
0426     /* the following constraints used to prototype
0427      * bpf_map_lookup/update/delete_elem() functions
0428      */
0429     ARG_CONST_MAP_PTR,  /* const argument used as pointer to bpf_map */
0430     ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
0431     ARG_PTR_TO_MAP_VALUE,   /* pointer to stack used as map value */
0432 
0433     /* Used to prototype bpf_memcmp() and other functions that access data
0434      * on eBPF program stack
0435      */
0436     ARG_PTR_TO_MEM,     /* pointer to valid memory (stack, packet, map value) */
0437 
0438     ARG_CONST_SIZE,     /* number of bytes accessed from memory */
0439     ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
0440 
0441     ARG_PTR_TO_CTX,     /* pointer to context */
0442     ARG_ANYTHING,       /* any (initialized) argument is ok */
0443     ARG_PTR_TO_SPIN_LOCK,   /* pointer to bpf_spin_lock */
0444     ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
0445     ARG_PTR_TO_INT,     /* pointer to int */
0446     ARG_PTR_TO_LONG,    /* pointer to long */
0447     ARG_PTR_TO_SOCKET,  /* pointer to bpf_sock (fullsock) */
0448     ARG_PTR_TO_BTF_ID,  /* pointer to in-kernel struct */
0449     ARG_PTR_TO_ALLOC_MEM,   /* pointer to dynamically allocated memory */
0450     ARG_CONST_ALLOC_SIZE_OR_ZERO,   /* number of allocated bytes requested */
0451     ARG_PTR_TO_BTF_ID_SOCK_COMMON,  /* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */
0452     ARG_PTR_TO_PERCPU_BTF_ID,   /* pointer to in-kernel percpu type */
0453     ARG_PTR_TO_FUNC,    /* pointer to a bpf program function */
0454     ARG_PTR_TO_STACK,   /* pointer to stack */
0455     ARG_PTR_TO_CONST_STR,   /* pointer to a null terminated read-only string */
0456     ARG_PTR_TO_TIMER,   /* pointer to bpf_timer */
0457     ARG_PTR_TO_KPTR,    /* pointer to referenced kptr */
0458     ARG_PTR_TO_DYNPTR,      /* pointer to bpf_dynptr. See bpf_type_flag for dynptr type */
0459     __BPF_ARG_TYPE_MAX,
0460 
0461     /* Extended arg_types. */
0462     ARG_PTR_TO_MAP_VALUE_OR_NULL    = PTR_MAYBE_NULL | ARG_PTR_TO_MAP_VALUE,
0463     ARG_PTR_TO_MEM_OR_NULL      = PTR_MAYBE_NULL | ARG_PTR_TO_MEM,
0464     ARG_PTR_TO_CTX_OR_NULL      = PTR_MAYBE_NULL | ARG_PTR_TO_CTX,
0465     ARG_PTR_TO_SOCKET_OR_NULL   = PTR_MAYBE_NULL | ARG_PTR_TO_SOCKET,
0466     ARG_PTR_TO_ALLOC_MEM_OR_NULL    = PTR_MAYBE_NULL | ARG_PTR_TO_ALLOC_MEM,
0467     ARG_PTR_TO_STACK_OR_NULL    = PTR_MAYBE_NULL | ARG_PTR_TO_STACK,
0468     ARG_PTR_TO_BTF_ID_OR_NULL   = PTR_MAYBE_NULL | ARG_PTR_TO_BTF_ID,
0469     /* pointer to memory does not need to be initialized, helper function must fill
0470      * all bytes or clear them in error case.
0471      */
0472     ARG_PTR_TO_UNINIT_MEM       = MEM_UNINIT | ARG_PTR_TO_MEM,
0473     /* Pointer to valid memory of size known at compile time. */
0474     ARG_PTR_TO_FIXED_SIZE_MEM   = MEM_FIXED_SIZE | ARG_PTR_TO_MEM,
0475 
0476     /* This must be the last entry. Its purpose is to ensure the enum is
0477      * wide enough to hold the higher bits reserved for bpf_type_flag.
0478      */
0479     __BPF_ARG_TYPE_LIMIT    = BPF_TYPE_LIMIT,
0480 };
0481 static_assert(__BPF_ARG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
0482 
0483 /* type of values returned from helper functions */
0484 enum bpf_return_type {
0485     RET_INTEGER,            /* function returns integer */
0486     RET_VOID,           /* function doesn't return anything */
0487     RET_PTR_TO_MAP_VALUE,       /* returns a pointer to map elem value */
0488     RET_PTR_TO_SOCKET,      /* returns a pointer to a socket */
0489     RET_PTR_TO_TCP_SOCK,        /* returns a pointer to a tcp_sock */
0490     RET_PTR_TO_SOCK_COMMON,     /* returns a pointer to a sock_common */
0491     RET_PTR_TO_ALLOC_MEM,       /* returns a pointer to dynamically allocated memory */
0492     RET_PTR_TO_MEM_OR_BTF_ID,   /* returns a pointer to a valid memory or a btf_id */
0493     RET_PTR_TO_BTF_ID,      /* returns a pointer to a btf_id */
0494     __BPF_RET_TYPE_MAX,
0495 
0496     /* Extended ret_types. */
0497     RET_PTR_TO_MAP_VALUE_OR_NULL    = PTR_MAYBE_NULL | RET_PTR_TO_MAP_VALUE,
0498     RET_PTR_TO_SOCKET_OR_NULL   = PTR_MAYBE_NULL | RET_PTR_TO_SOCKET,
0499     RET_PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_TCP_SOCK,
0500     RET_PTR_TO_SOCK_COMMON_OR_NULL  = PTR_MAYBE_NULL | RET_PTR_TO_SOCK_COMMON,
0501     RET_PTR_TO_ALLOC_MEM_OR_NULL    = PTR_MAYBE_NULL | MEM_ALLOC | RET_PTR_TO_ALLOC_MEM,
0502     RET_PTR_TO_DYNPTR_MEM_OR_NULL   = PTR_MAYBE_NULL | RET_PTR_TO_ALLOC_MEM,
0503     RET_PTR_TO_BTF_ID_OR_NULL   = PTR_MAYBE_NULL | RET_PTR_TO_BTF_ID,
0504 
0505     /* This must be the last entry. Its purpose is to ensure the enum is
0506      * wide enough to hold the higher bits reserved for bpf_type_flag.
0507      */
0508     __BPF_RET_TYPE_LIMIT    = BPF_TYPE_LIMIT,
0509 };
0510 static_assert(__BPF_RET_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
0511 
0512 /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
0513  * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
0514  * instructions after verifying
0515  */
0516 struct bpf_func_proto {
0517     u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
0518     bool gpl_only;
0519     bool pkt_access;
0520     enum bpf_return_type ret_type;
0521     union {
0522         struct {
0523             enum bpf_arg_type arg1_type;
0524             enum bpf_arg_type arg2_type;
0525             enum bpf_arg_type arg3_type;
0526             enum bpf_arg_type arg4_type;
0527             enum bpf_arg_type arg5_type;
0528         };
0529         enum bpf_arg_type arg_type[5];
0530     };
0531     union {
0532         struct {
0533             u32 *arg1_btf_id;
0534             u32 *arg2_btf_id;
0535             u32 *arg3_btf_id;
0536             u32 *arg4_btf_id;
0537             u32 *arg5_btf_id;
0538         };
0539         u32 *arg_btf_id[5];
0540         struct {
0541             size_t arg1_size;
0542             size_t arg2_size;
0543             size_t arg3_size;
0544             size_t arg4_size;
0545             size_t arg5_size;
0546         };
0547         size_t arg_size[5];
0548     };
0549     int *ret_btf_id; /* return value btf_id */
0550     bool (*allowed)(const struct bpf_prog *prog);
0551 };
0552 
0553 /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
0554  * the first argument to eBPF programs.
0555  * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
0556  */
0557 struct bpf_context;
0558 
0559 enum bpf_access_type {
0560     BPF_READ = 1,
0561     BPF_WRITE = 2
0562 };
0563 
0564 /* types of values stored in eBPF registers */
0565 /* Pointer types represent:
0566  * pointer
0567  * pointer + imm
0568  * pointer + (u16) var
0569  * pointer + (u16) var + imm
0570  * if (range > 0) then [ptr, ptr + range - off) is safe to access
0571  * if (id > 0) means that some 'var' was added
0572  * if (off > 0) means that 'imm' was added
0573  */
0574 enum bpf_reg_type {
0575     NOT_INIT = 0,        /* nothing was written into register */
0576     SCALAR_VALUE,        /* reg doesn't contain a valid pointer */
0577     PTR_TO_CTX,      /* reg points to bpf_context */
0578     CONST_PTR_TO_MAP,    /* reg points to struct bpf_map */
0579     PTR_TO_MAP_VALUE,    /* reg points to map element value */
0580     PTR_TO_MAP_KEY,      /* reg points to a map element key */
0581     PTR_TO_STACK,        /* reg == frame_pointer + offset */
0582     PTR_TO_PACKET_META,  /* skb->data - meta_len */
0583     PTR_TO_PACKET,       /* reg points to skb->data */
0584     PTR_TO_PACKET_END,   /* skb->data + headlen */
0585     PTR_TO_FLOW_KEYS,    /* reg points to bpf_flow_keys */
0586     PTR_TO_SOCKET,       /* reg points to struct bpf_sock */
0587     PTR_TO_SOCK_COMMON,  /* reg points to sock_common */
0588     PTR_TO_TCP_SOCK,     /* reg points to struct tcp_sock */
0589     PTR_TO_TP_BUFFER,    /* reg points to a writable raw tp's buffer */
0590     PTR_TO_XDP_SOCK,     /* reg points to struct xdp_sock */
0591     /* PTR_TO_BTF_ID points to a kernel struct that does not need
0592      * to be null checked by the BPF program. This does not imply the
0593      * pointer is _not_ null and in practice this can easily be a null
0594      * pointer when reading pointer chains. The assumption is program
0595      * context will handle null pointer dereference typically via fault
0596      * handling. The verifier must keep this in mind and can make no
0597      * assumptions about null or non-null when doing branch analysis.
0598      * Further, when passed into helpers the helpers can not, without
0599      * additional context, assume the value is non-null.
0600      */
0601     PTR_TO_BTF_ID,
0602     /* PTR_TO_BTF_ID_OR_NULL points to a kernel struct that has not
0603      * been checked for null. Used primarily to inform the verifier
0604      * an explicit null check is required for this struct.
0605      */
0606     PTR_TO_MEM,      /* reg points to valid memory region */
0607     PTR_TO_BUF,      /* reg points to a read/write buffer */
0608     PTR_TO_FUNC,         /* reg points to a bpf program function */
0609     __BPF_REG_TYPE_MAX,
0610 
0611     /* Extended reg_types. */
0612     PTR_TO_MAP_VALUE_OR_NULL    = PTR_MAYBE_NULL | PTR_TO_MAP_VALUE,
0613     PTR_TO_SOCKET_OR_NULL       = PTR_MAYBE_NULL | PTR_TO_SOCKET,
0614     PTR_TO_SOCK_COMMON_OR_NULL  = PTR_MAYBE_NULL | PTR_TO_SOCK_COMMON,
0615     PTR_TO_TCP_SOCK_OR_NULL     = PTR_MAYBE_NULL | PTR_TO_TCP_SOCK,
0616     PTR_TO_BTF_ID_OR_NULL       = PTR_MAYBE_NULL | PTR_TO_BTF_ID,
0617 
0618     /* This must be the last entry. Its purpose is to ensure the enum is
0619      * wide enough to hold the higher bits reserved for bpf_type_flag.
0620      */
0621     __BPF_REG_TYPE_LIMIT    = BPF_TYPE_LIMIT,
0622 };
0623 static_assert(__BPF_REG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
0624 
0625 /* The information passed from prog-specific *_is_valid_access
0626  * back to the verifier.
0627  */
0628 struct bpf_insn_access_aux {
0629     enum bpf_reg_type reg_type;
0630     union {
0631         int ctx_field_size;
0632         struct {
0633             struct btf *btf;
0634             u32 btf_id;
0635         };
0636     };
0637     struct bpf_verifier_log *log; /* for verbose logs */
0638 };
0639 
0640 static inline void
0641 bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
0642 {
0643     aux->ctx_field_size = size;
0644 }
0645 
0646 static inline bool bpf_pseudo_func(const struct bpf_insn *insn)
0647 {
0648     return insn->code == (BPF_LD | BPF_IMM | BPF_DW) &&
0649            insn->src_reg == BPF_PSEUDO_FUNC;
0650 }
0651 
0652 struct bpf_prog_ops {
0653     int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
0654             union bpf_attr __user *uattr);
0655 };
0656 
0657 struct bpf_verifier_ops {
0658     /* return eBPF function prototype for verification */
0659     const struct bpf_func_proto *
0660     (*get_func_proto)(enum bpf_func_id func_id,
0661               const struct bpf_prog *prog);
0662 
0663     /* return true if 'size' wide access at offset 'off' within bpf_context
0664      * with 'type' (read or write) is allowed
0665      */
0666     bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
0667                 const struct bpf_prog *prog,
0668                 struct bpf_insn_access_aux *info);
0669     int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
0670                 const struct bpf_prog *prog);
0671     int (*gen_ld_abs)(const struct bpf_insn *orig,
0672               struct bpf_insn *insn_buf);
0673     u32 (*convert_ctx_access)(enum bpf_access_type type,
0674                   const struct bpf_insn *src,
0675                   struct bpf_insn *dst,
0676                   struct bpf_prog *prog, u32 *target_size);
0677     int (*btf_struct_access)(struct bpf_verifier_log *log,
0678                  const struct btf *btf,
0679                  const struct btf_type *t, int off, int size,
0680                  enum bpf_access_type atype,
0681                  u32 *next_btf_id, enum bpf_type_flag *flag);
0682 };
0683 
0684 struct bpf_prog_offload_ops {
0685     /* verifier basic callbacks */
0686     int (*insn_hook)(struct bpf_verifier_env *env,
0687              int insn_idx, int prev_insn_idx);
0688     int (*finalize)(struct bpf_verifier_env *env);
0689     /* verifier optimization callbacks (called after .finalize) */
0690     int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
0691                 struct bpf_insn *insn);
0692     int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
0693     /* program management callbacks */
0694     int (*prepare)(struct bpf_prog *prog);
0695     int (*translate)(struct bpf_prog *prog);
0696     void (*destroy)(struct bpf_prog *prog);
0697 };
0698 
0699 struct bpf_prog_offload {
0700     struct bpf_prog     *prog;
0701     struct net_device   *netdev;
0702     struct bpf_offload_dev  *offdev;
0703     void            *dev_priv;
0704     struct list_head    offloads;
0705     bool            dev_state;
0706     bool            opt_failed;
0707     void            *jited_image;
0708     u32         jited_len;
0709 };
0710 
0711 enum bpf_cgroup_storage_type {
0712     BPF_CGROUP_STORAGE_SHARED,
0713     BPF_CGROUP_STORAGE_PERCPU,
0714     __BPF_CGROUP_STORAGE_MAX
0715 };
0716 
0717 #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
0718 
0719 /* The longest tracepoint has 12 args.
0720  * See include/trace/bpf_probe.h
0721  */
0722 #define MAX_BPF_FUNC_ARGS 12
0723 
0724 /* The maximum number of arguments passed through registers
0725  * a single function may have.
0726  */
0727 #define MAX_BPF_FUNC_REG_ARGS 5
0728 
0729 struct btf_func_model {
0730     u8 ret_size;
0731     u8 nr_args;
0732     u8 arg_size[MAX_BPF_FUNC_ARGS];
0733 };
0734 
0735 /* Restore arguments before returning from trampoline to let original function
0736  * continue executing. This flag is used for fentry progs when there are no
0737  * fexit progs.
0738  */
0739 #define BPF_TRAMP_F_RESTORE_REGS    BIT(0)
0740 /* Call original function after fentry progs, but before fexit progs.
0741  * Makes sense for fentry/fexit, normal calls and indirect calls.
0742  */
0743 #define BPF_TRAMP_F_CALL_ORIG       BIT(1)
0744 /* Skip current frame and return to parent.  Makes sense for fentry/fexit
0745  * programs only. Should not be used with normal calls and indirect calls.
0746  */
0747 #define BPF_TRAMP_F_SKIP_FRAME      BIT(2)
0748 /* Store IP address of the caller on the trampoline stack,
0749  * so it's available for trampoline's programs.
0750  */
0751 #define BPF_TRAMP_F_IP_ARG      BIT(3)
0752 /* Return the return value of fentry prog. Only used by bpf_struct_ops. */
0753 #define BPF_TRAMP_F_RET_FENTRY_RET  BIT(4)
0754 
0755 /* Get original function from stack instead of from provided direct address.
0756  * Makes sense for trampolines with fexit or fmod_ret programs.
0757  */
0758 #define BPF_TRAMP_F_ORIG_STACK      BIT(5)
0759 
0760 /* This trampoline is on a function with another ftrace_ops with IPMODIFY,
0761  * e.g., a live patch. This flag is set and cleared by ftrace call backs,
0762  */
0763 #define BPF_TRAMP_F_SHARE_IPMODIFY  BIT(6)
0764 
0765 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
0766  * bytes on x86.
0767  */
0768 #define BPF_MAX_TRAMP_LINKS 38
0769 
0770 struct bpf_tramp_links {
0771     struct bpf_tramp_link *links[BPF_MAX_TRAMP_LINKS];
0772     int nr_links;
0773 };
0774 
0775 struct bpf_tramp_run_ctx;
0776 
0777 /* Different use cases for BPF trampoline:
0778  * 1. replace nop at the function entry (kprobe equivalent)
0779  *    flags = BPF_TRAMP_F_RESTORE_REGS
0780  *    fentry = a set of programs to run before returning from trampoline
0781  *
0782  * 2. replace nop at the function entry (kprobe + kretprobe equivalent)
0783  *    flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME
0784  *    orig_call = fentry_ip + MCOUNT_INSN_SIZE
0785  *    fentry = a set of program to run before calling original function
0786  *    fexit = a set of program to run after original function
0787  *
0788  * 3. replace direct call instruction anywhere in the function body
0789  *    or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid)
0790  *    With flags = 0
0791  *      fentry = a set of programs to run before returning from trampoline
0792  *    With flags = BPF_TRAMP_F_CALL_ORIG
0793  *      orig_call = original callback addr or direct function addr
0794  *      fentry = a set of program to run before calling original function
0795  *      fexit = a set of program to run after original function
0796  */
0797 struct bpf_tramp_image;
0798 int arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end,
0799                 const struct btf_func_model *m, u32 flags,
0800                 struct bpf_tramp_links *tlinks,
0801                 void *orig_call);
0802 /* these two functions are called from generated trampoline */
0803 u64 notrace __bpf_prog_enter(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx);
0804 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start, struct bpf_tramp_run_ctx *run_ctx);
0805 u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx);
0806 void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start,
0807                        struct bpf_tramp_run_ctx *run_ctx);
0808 u64 notrace __bpf_prog_enter_lsm_cgroup(struct bpf_prog *prog,
0809                     struct bpf_tramp_run_ctx *run_ctx);
0810 void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start,
0811                     struct bpf_tramp_run_ctx *run_ctx);
0812 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr);
0813 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr);
0814 
0815 struct bpf_ksym {
0816     unsigned long        start;
0817     unsigned long        end;
0818     char             name[KSYM_NAME_LEN];
0819     struct list_head     lnode;
0820     struct latch_tree_node   tnode;
0821     bool             prog;
0822 };
0823 
0824 enum bpf_tramp_prog_type {
0825     BPF_TRAMP_FENTRY,
0826     BPF_TRAMP_FEXIT,
0827     BPF_TRAMP_MODIFY_RETURN,
0828     BPF_TRAMP_MAX,
0829     BPF_TRAMP_REPLACE, /* more than MAX */
0830 };
0831 
0832 struct bpf_tramp_image {
0833     void *image;
0834     struct bpf_ksym ksym;
0835     struct percpu_ref pcref;
0836     void *ip_after_call;
0837     void *ip_epilogue;
0838     union {
0839         struct rcu_head rcu;
0840         struct work_struct work;
0841     };
0842 };
0843 
0844 struct bpf_trampoline {
0845     /* hlist for trampoline_table */
0846     struct hlist_node hlist;
0847     struct ftrace_ops *fops;
0848     /* serializes access to fields of this trampoline */
0849     struct mutex mutex;
0850     refcount_t refcnt;
0851     u32 flags;
0852     u64 key;
0853     struct {
0854         struct btf_func_model model;
0855         void *addr;
0856         bool ftrace_managed;
0857     } func;
0858     /* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF
0859      * program by replacing one of its functions. func.addr is the address
0860      * of the function it replaced.
0861      */
0862     struct bpf_prog *extension_prog;
0863     /* list of BPF programs using this trampoline */
0864     struct hlist_head progs_hlist[BPF_TRAMP_MAX];
0865     /* Number of attached programs. A counter per kind. */
0866     int progs_cnt[BPF_TRAMP_MAX];
0867     /* Executable image of trampoline */
0868     struct bpf_tramp_image *cur_image;
0869     u64 selector;
0870     struct module *mod;
0871 };
0872 
0873 struct bpf_attach_target_info {
0874     struct btf_func_model fmodel;
0875     long tgt_addr;
0876     const char *tgt_name;
0877     const struct btf_type *tgt_type;
0878 };
0879 
0880 #define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
0881 
0882 struct bpf_dispatcher_prog {
0883     struct bpf_prog *prog;
0884     refcount_t users;
0885 };
0886 
0887 struct bpf_dispatcher {
0888     /* dispatcher mutex */
0889     struct mutex mutex;
0890     void *func;
0891     struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX];
0892     int num_progs;
0893     void *image;
0894     u32 image_off;
0895     struct bpf_ksym ksym;
0896 };
0897 
0898 static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(
0899     const void *ctx,
0900     const struct bpf_insn *insnsi,
0901     bpf_func_t bpf_func)
0902 {
0903     return bpf_func(ctx, insnsi);
0904 }
0905 
0906 #ifdef CONFIG_BPF_JIT
0907 int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
0908 int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
0909 struct bpf_trampoline *bpf_trampoline_get(u64 key,
0910                       struct bpf_attach_target_info *tgt_info);
0911 void bpf_trampoline_put(struct bpf_trampoline *tr);
0912 int arch_prepare_bpf_dispatcher(void *image, s64 *funcs, int num_funcs);
0913 #define BPF_DISPATCHER_INIT(_name) {                \
0914     .mutex = __MUTEX_INITIALIZER(_name.mutex),      \
0915     .func = &_name##_func,                  \
0916     .progs = {},                        \
0917     .num_progs = 0,                     \
0918     .image = NULL,                      \
0919     .image_off = 0,                     \
0920     .ksym = {                       \
0921         .name  = #_name,                \
0922         .lnode = LIST_HEAD_INIT(_name.ksym.lnode),  \
0923     },                          \
0924 }
0925 
0926 #define DEFINE_BPF_DISPATCHER(name)                 \
0927     noinline __nocfi unsigned int bpf_dispatcher_##name##_func( \
0928         const void *ctx,                    \
0929         const struct bpf_insn *insnsi,              \
0930         bpf_func_t bpf_func)                    \
0931     {                               \
0932         return bpf_func(ctx, insnsi);               \
0933     }                               \
0934     EXPORT_SYMBOL(bpf_dispatcher_##name##_func);            \
0935     struct bpf_dispatcher bpf_dispatcher_##name =           \
0936         BPF_DISPATCHER_INIT(bpf_dispatcher_##name);
0937 #define DECLARE_BPF_DISPATCHER(name)                    \
0938     unsigned int bpf_dispatcher_##name##_func(          \
0939         const void *ctx,                    \
0940         const struct bpf_insn *insnsi,              \
0941         bpf_func_t bpf_func);                   \
0942     extern struct bpf_dispatcher bpf_dispatcher_##name;
0943 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func
0944 #define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
0945 void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
0946                 struct bpf_prog *to);
0947 /* Called only from JIT-enabled code, so there's no need for stubs. */
0948 void *bpf_jit_alloc_exec_page(void);
0949 void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
0950 void bpf_image_ksym_del(struct bpf_ksym *ksym);
0951 void bpf_ksym_add(struct bpf_ksym *ksym);
0952 void bpf_ksym_del(struct bpf_ksym *ksym);
0953 int bpf_jit_charge_modmem(u32 size);
0954 void bpf_jit_uncharge_modmem(u32 size);
0955 bool bpf_prog_has_trampoline(const struct bpf_prog *prog);
0956 #else
0957 static inline int bpf_trampoline_link_prog(struct bpf_tramp_link *link,
0958                        struct bpf_trampoline *tr)
0959 {
0960     return -ENOTSUPP;
0961 }
0962 static inline int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link,
0963                          struct bpf_trampoline *tr)
0964 {
0965     return -ENOTSUPP;
0966 }
0967 static inline struct bpf_trampoline *bpf_trampoline_get(u64 key,
0968                             struct bpf_attach_target_info *tgt_info)
0969 {
0970     return ERR_PTR(-EOPNOTSUPP);
0971 }
0972 static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
0973 #define DEFINE_BPF_DISPATCHER(name)
0974 #define DECLARE_BPF_DISPATCHER(name)
0975 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nop_func
0976 #define BPF_DISPATCHER_PTR(name) NULL
0977 static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
0978                           struct bpf_prog *from,
0979                           struct bpf_prog *to) {}
0980 static inline bool is_bpf_image_address(unsigned long address)
0981 {
0982     return false;
0983 }
0984 static inline bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
0985 {
0986     return false;
0987 }
0988 #endif
0989 
0990 struct bpf_func_info_aux {
0991     u16 linkage;
0992     bool unreliable;
0993 };
0994 
0995 enum bpf_jit_poke_reason {
0996     BPF_POKE_REASON_TAIL_CALL,
0997 };
0998 
0999 /* Descriptor of pokes pointing /into/ the JITed image. */
1000 struct bpf_jit_poke_descriptor {
1001     void *tailcall_target;
1002     void *tailcall_bypass;
1003     void *bypass_addr;
1004     void *aux;
1005     union {
1006         struct {
1007             struct bpf_map *map;
1008             u32 key;
1009         } tail_call;
1010     };
1011     bool tailcall_target_stable;
1012     u8 adj_off;
1013     u16 reason;
1014     u32 insn_idx;
1015 };
1016 
1017 /* reg_type info for ctx arguments */
1018 struct bpf_ctx_arg_aux {
1019     u32 offset;
1020     enum bpf_reg_type reg_type;
1021     u32 btf_id;
1022 };
1023 
1024 struct btf_mod_pair {
1025     struct btf *btf;
1026     struct module *module;
1027 };
1028 
1029 struct bpf_kfunc_desc_tab;
1030 
1031 struct bpf_prog_aux {
1032     atomic64_t refcnt;
1033     u32 used_map_cnt;
1034     u32 used_btf_cnt;
1035     u32 max_ctx_offset;
1036     u32 max_pkt_offset;
1037     u32 max_tp_access;
1038     u32 stack_depth;
1039     u32 id;
1040     u32 func_cnt; /* used by non-func prog as the number of func progs */
1041     u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
1042     u32 attach_btf_id; /* in-kernel BTF type id to attach to */
1043     u32 ctx_arg_info_size;
1044     u32 max_rdonly_access;
1045     u32 max_rdwr_access;
1046     struct btf *attach_btf;
1047     const struct bpf_ctx_arg_aux *ctx_arg_info;
1048     struct mutex dst_mutex; /* protects dst_* pointers below, *after* prog becomes visible */
1049     struct bpf_prog *dst_prog;
1050     struct bpf_trampoline *dst_trampoline;
1051     enum bpf_prog_type saved_dst_prog_type;
1052     enum bpf_attach_type saved_dst_attach_type;
1053     bool verifier_zext; /* Zero extensions has been inserted by verifier. */
1054     bool offload_requested;
1055     bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
1056     bool func_proto_unreliable;
1057     bool sleepable;
1058     bool tail_call_reachable;
1059     bool xdp_has_frags;
1060     /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
1061     const struct btf_type *attach_func_proto;
1062     /* function name for valid attach_btf_id */
1063     const char *attach_func_name;
1064     struct bpf_prog **func;
1065     void *jit_data; /* JIT specific data. arch dependent */
1066     struct bpf_jit_poke_descriptor *poke_tab;
1067     struct bpf_kfunc_desc_tab *kfunc_tab;
1068     struct bpf_kfunc_btf_tab *kfunc_btf_tab;
1069     u32 size_poke_tab;
1070     struct bpf_ksym ksym;
1071     const struct bpf_prog_ops *ops;
1072     struct bpf_map **used_maps;
1073     struct mutex used_maps_mutex; /* mutex for used_maps and used_map_cnt */
1074     struct btf_mod_pair *used_btfs;
1075     struct bpf_prog *prog;
1076     struct user_struct *user;
1077     u64 load_time; /* ns since boottime */
1078     u32 verified_insns;
1079     int cgroup_atype; /* enum cgroup_bpf_attach_type */
1080     struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1081     char name[BPF_OBJ_NAME_LEN];
1082 #ifdef CONFIG_SECURITY
1083     void *security;
1084 #endif
1085     struct bpf_prog_offload *offload;
1086     struct btf *btf;
1087     struct bpf_func_info *func_info;
1088     struct bpf_func_info_aux *func_info_aux;
1089     /* bpf_line_info loaded from userspace.  linfo->insn_off
1090      * has the xlated insn offset.
1091      * Both the main and sub prog share the same linfo.
1092      * The subprog can access its first linfo by
1093      * using the linfo_idx.
1094      */
1095     struct bpf_line_info *linfo;
1096     /* jited_linfo is the jited addr of the linfo.  It has a
1097      * one to one mapping to linfo:
1098      * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
1099      * Both the main and sub prog share the same jited_linfo.
1100      * The subprog can access its first jited_linfo by
1101      * using the linfo_idx.
1102      */
1103     void **jited_linfo;
1104     u32 func_info_cnt;
1105     u32 nr_linfo;
1106     /* subprog can use linfo_idx to access its first linfo and
1107      * jited_linfo.
1108      * main prog always has linfo_idx == 0
1109      */
1110     u32 linfo_idx;
1111     u32 num_exentries;
1112     struct exception_table_entry *extable;
1113     union {
1114         struct work_struct work;
1115         struct rcu_head rcu;
1116     };
1117 };
1118 
1119 struct bpf_prog {
1120     u16         pages;      /* Number of allocated pages */
1121     u16         jited:1,    /* Is our filter JIT'ed? */
1122                 jit_requested:1,/* archs need to JIT the prog */
1123                 gpl_compatible:1, /* Is filter GPL compatible? */
1124                 cb_access:1,    /* Is control block accessed? */
1125                 dst_needed:1,   /* Do we need dst entry? */
1126                 blinding_requested:1, /* needs constant blinding */
1127                 blinded:1,  /* Was blinded */
1128                 is_func:1,  /* program is a bpf function */
1129                 kprobe_override:1, /* Do we override a kprobe? */
1130                 has_callchain_buf:1, /* callchain buffer allocated? */
1131                 enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */
1132                 call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */
1133                 call_get_func_ip:1, /* Do we call get_func_ip() */
1134                 tstamp_type_access:1; /* Accessed __sk_buff->tstamp_type */
1135     enum bpf_prog_type  type;       /* Type of BPF program */
1136     enum bpf_attach_type    expected_attach_type; /* For some prog types */
1137     u32         len;        /* Number of filter blocks */
1138     u32         jited_len;  /* Size of jited insns in bytes */
1139     u8          tag[BPF_TAG_SIZE];
1140     struct bpf_prog_stats __percpu *stats;
1141     int __percpu        *active;
1142     unsigned int        (*bpf_func)(const void *ctx,
1143                         const struct bpf_insn *insn);
1144     struct bpf_prog_aux *aux;       /* Auxiliary fields */
1145     struct sock_fprog_kern  *orig_prog; /* Original BPF program */
1146     /* Instructions for interpreter */
1147     union {
1148         DECLARE_FLEX_ARRAY(struct sock_filter, insns);
1149         DECLARE_FLEX_ARRAY(struct bpf_insn, insnsi);
1150     };
1151 };
1152 
1153 struct bpf_array_aux {
1154     /* Programs with direct jumps into programs part of this array. */
1155     struct list_head poke_progs;
1156     struct bpf_map *map;
1157     struct mutex poke_mutex;
1158     struct work_struct work;
1159 };
1160 
1161 struct bpf_link {
1162     atomic64_t refcnt;
1163     u32 id;
1164     enum bpf_link_type type;
1165     const struct bpf_link_ops *ops;
1166     struct bpf_prog *prog;
1167     struct work_struct work;
1168 };
1169 
1170 struct bpf_link_ops {
1171     void (*release)(struct bpf_link *link);
1172     void (*dealloc)(struct bpf_link *link);
1173     int (*detach)(struct bpf_link *link);
1174     int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
1175                struct bpf_prog *old_prog);
1176     void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);
1177     int (*fill_link_info)(const struct bpf_link *link,
1178                   struct bpf_link_info *info);
1179 };
1180 
1181 struct bpf_tramp_link {
1182     struct bpf_link link;
1183     struct hlist_node tramp_hlist;
1184     u64 cookie;
1185 };
1186 
1187 struct bpf_shim_tramp_link {
1188     struct bpf_tramp_link link;
1189     struct bpf_trampoline *trampoline;
1190 };
1191 
1192 struct bpf_tracing_link {
1193     struct bpf_tramp_link link;
1194     enum bpf_attach_type attach_type;
1195     struct bpf_trampoline *trampoline;
1196     struct bpf_prog *tgt_prog;
1197 };
1198 
1199 struct bpf_link_primer {
1200     struct bpf_link *link;
1201     struct file *file;
1202     int fd;
1203     u32 id;
1204 };
1205 
1206 struct bpf_struct_ops_value;
1207 struct btf_member;
1208 
1209 #define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
1210 struct bpf_struct_ops {
1211     const struct bpf_verifier_ops *verifier_ops;
1212     int (*init)(struct btf *btf);
1213     int (*check_member)(const struct btf_type *t,
1214                 const struct btf_member *member);
1215     int (*init_member)(const struct btf_type *t,
1216                const struct btf_member *member,
1217                void *kdata, const void *udata);
1218     int (*reg)(void *kdata);
1219     void (*unreg)(void *kdata);
1220     const struct btf_type *type;
1221     const struct btf_type *value_type;
1222     const char *name;
1223     struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
1224     u32 type_id;
1225     u32 value_id;
1226 };
1227 
1228 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
1229 #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
1230 const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
1231 void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
1232 bool bpf_struct_ops_get(const void *kdata);
1233 void bpf_struct_ops_put(const void *kdata);
1234 int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
1235                        void *value);
1236 int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks,
1237                       struct bpf_tramp_link *link,
1238                       const struct btf_func_model *model,
1239                       void *image, void *image_end);
1240 static inline bool bpf_try_module_get(const void *data, struct module *owner)
1241 {
1242     if (owner == BPF_MODULE_OWNER)
1243         return bpf_struct_ops_get(data);
1244     else
1245         return try_module_get(owner);
1246 }
1247 static inline void bpf_module_put(const void *data, struct module *owner)
1248 {
1249     if (owner == BPF_MODULE_OWNER)
1250         bpf_struct_ops_put(data);
1251     else
1252         module_put(owner);
1253 }
1254 
1255 #ifdef CONFIG_NET
1256 /* Define it here to avoid the use of forward declaration */
1257 struct bpf_dummy_ops_state {
1258     int val;
1259 };
1260 
1261 struct bpf_dummy_ops {
1262     int (*test_1)(struct bpf_dummy_ops_state *cb);
1263     int (*test_2)(struct bpf_dummy_ops_state *cb, int a1, unsigned short a2,
1264               char a3, unsigned long a4);
1265 };
1266 
1267 int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
1268                 union bpf_attr __user *uattr);
1269 #endif
1270 #else
1271 static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
1272 {
1273     return NULL;
1274 }
1275 static inline void bpf_struct_ops_init(struct btf *btf,
1276                        struct bpf_verifier_log *log)
1277 {
1278 }
1279 static inline bool bpf_try_module_get(const void *data, struct module *owner)
1280 {
1281     return try_module_get(owner);
1282 }
1283 static inline void bpf_module_put(const void *data, struct module *owner)
1284 {
1285     module_put(owner);
1286 }
1287 static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
1288                              void *key,
1289                              void *value)
1290 {
1291     return -EINVAL;
1292 }
1293 #endif
1294 
1295 #if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM)
1296 int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
1297                     int cgroup_atype);
1298 void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog);
1299 #else
1300 static inline int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
1301                           int cgroup_atype)
1302 {
1303     return -EOPNOTSUPP;
1304 }
1305 static inline void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog)
1306 {
1307 }
1308 #endif
1309 
1310 struct bpf_array {
1311     struct bpf_map map;
1312     u32 elem_size;
1313     u32 index_mask;
1314     struct bpf_array_aux *aux;
1315     union {
1316         char value[0] __aligned(8);
1317         void *ptrs[0] __aligned(8);
1318         void __percpu *pptrs[0] __aligned(8);
1319     };
1320 };
1321 
1322 #define BPF_COMPLEXITY_LIMIT_INSNS      1000000 /* yes. 1M insns */
1323 #define MAX_TAIL_CALL_CNT 33
1324 
1325 /* Maximum number of loops for bpf_loop */
1326 #define BPF_MAX_LOOPS   BIT(23)
1327 
1328 #define BPF_F_ACCESS_MASK   (BPF_F_RDONLY |     \
1329                  BPF_F_RDONLY_PROG |    \
1330                  BPF_F_WRONLY |     \
1331                  BPF_F_WRONLY_PROG)
1332 
1333 #define BPF_MAP_CAN_READ    BIT(0)
1334 #define BPF_MAP_CAN_WRITE   BIT(1)
1335 
1336 static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
1337 {
1338     u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1339 
1340     /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
1341      * not possible.
1342      */
1343     if (access_flags & BPF_F_RDONLY_PROG)
1344         return BPF_MAP_CAN_READ;
1345     else if (access_flags & BPF_F_WRONLY_PROG)
1346         return BPF_MAP_CAN_WRITE;
1347     else
1348         return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
1349 }
1350 
1351 static inline bool bpf_map_flags_access_ok(u32 access_flags)
1352 {
1353     return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
1354            (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1355 }
1356 
1357 struct bpf_event_entry {
1358     struct perf_event *event;
1359     struct file *perf_file;
1360     struct file *map_file;
1361     struct rcu_head rcu;
1362 };
1363 
1364 static inline bool map_type_contains_progs(struct bpf_map *map)
1365 {
1366     return map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
1367            map->map_type == BPF_MAP_TYPE_DEVMAP ||
1368            map->map_type == BPF_MAP_TYPE_CPUMAP;
1369 }
1370 
1371 bool bpf_prog_map_compatible(struct bpf_map *map, const struct bpf_prog *fp);
1372 int bpf_prog_calc_tag(struct bpf_prog *fp);
1373 
1374 const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
1375 const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void);
1376 
1377 typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
1378                     unsigned long off, unsigned long len);
1379 typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
1380                     const struct bpf_insn *src,
1381                     struct bpf_insn *dst,
1382                     struct bpf_prog *prog,
1383                     u32 *target_size);
1384 
1385 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1386              void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
1387 
1388 /* an array of programs to be executed under rcu_lock.
1389  *
1390  * Typical usage:
1391  * ret = bpf_prog_run_array(rcu_dereference(&bpf_prog_array), ctx, bpf_prog_run);
1392  *
1393  * the structure returned by bpf_prog_array_alloc() should be populated
1394  * with program pointers and the last pointer must be NULL.
1395  * The user has to keep refcnt on the program and make sure the program
1396  * is removed from the array before bpf_prog_put().
1397  * The 'struct bpf_prog_array *' should only be replaced with xchg()
1398  * since other cpus are walking the array of pointers in parallel.
1399  */
1400 struct bpf_prog_array_item {
1401     struct bpf_prog *prog;
1402     union {
1403         struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1404         u64 bpf_cookie;
1405     };
1406 };
1407 
1408 struct bpf_prog_array {
1409     struct rcu_head rcu;
1410     struct bpf_prog_array_item items[];
1411 };
1412 
1413 struct bpf_empty_prog_array {
1414     struct bpf_prog_array hdr;
1415     struct bpf_prog *null_prog;
1416 };
1417 
1418 /* to avoid allocating empty bpf_prog_array for cgroups that
1419  * don't have bpf program attached use one global 'bpf_empty_prog_array'
1420  * It will not be modified the caller of bpf_prog_array_alloc()
1421  * (since caller requested prog_cnt == 0)
1422  * that pointer should be 'freed' by bpf_prog_array_free()
1423  */
1424 extern struct bpf_empty_prog_array bpf_empty_prog_array;
1425 
1426 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
1427 void bpf_prog_array_free(struct bpf_prog_array *progs);
1428 /* Use when traversal over the bpf_prog_array uses tasks_trace rcu */
1429 void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs);
1430 int bpf_prog_array_length(struct bpf_prog_array *progs);
1431 bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
1432 int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
1433                 __u32 __user *prog_ids, u32 cnt);
1434 
1435 void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
1436                 struct bpf_prog *old_prog);
1437 int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index);
1438 int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
1439                  struct bpf_prog *prog);
1440 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
1441                  u32 *prog_ids, u32 request_cnt,
1442                  u32 *prog_cnt);
1443 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
1444             struct bpf_prog *exclude_prog,
1445             struct bpf_prog *include_prog,
1446             u64 bpf_cookie,
1447             struct bpf_prog_array **new_array);
1448 
1449 struct bpf_run_ctx {};
1450 
1451 struct bpf_cg_run_ctx {
1452     struct bpf_run_ctx run_ctx;
1453     const struct bpf_prog_array_item *prog_item;
1454     int retval;
1455 };
1456 
1457 struct bpf_trace_run_ctx {
1458     struct bpf_run_ctx run_ctx;
1459     u64 bpf_cookie;
1460 };
1461 
1462 struct bpf_tramp_run_ctx {
1463     struct bpf_run_ctx run_ctx;
1464     u64 bpf_cookie;
1465     struct bpf_run_ctx *saved_run_ctx;
1466 };
1467 
1468 static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
1469 {
1470     struct bpf_run_ctx *old_ctx = NULL;
1471 
1472 #ifdef CONFIG_BPF_SYSCALL
1473     old_ctx = current->bpf_ctx;
1474     current->bpf_ctx = new_ctx;
1475 #endif
1476     return old_ctx;
1477 }
1478 
1479 static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)
1480 {
1481 #ifdef CONFIG_BPF_SYSCALL
1482     current->bpf_ctx = old_ctx;
1483 #endif
1484 }
1485 
1486 /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
1487 #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE            (1 << 0)
1488 /* BPF program asks to set CN on the packet. */
1489 #define BPF_RET_SET_CN                      (1 << 0)
1490 
1491 typedef u32 (*bpf_prog_run_fn)(const struct bpf_prog *prog, const void *ctx);
1492 
1493 static __always_inline u32
1494 bpf_prog_run_array(const struct bpf_prog_array *array,
1495            const void *ctx, bpf_prog_run_fn run_prog)
1496 {
1497     const struct bpf_prog_array_item *item;
1498     const struct bpf_prog *prog;
1499     struct bpf_run_ctx *old_run_ctx;
1500     struct bpf_trace_run_ctx run_ctx;
1501     u32 ret = 1;
1502 
1503     RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held");
1504 
1505     if (unlikely(!array))
1506         return ret;
1507 
1508     migrate_disable();
1509     old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
1510     item = &array->items[0];
1511     while ((prog = READ_ONCE(item->prog))) {
1512         run_ctx.bpf_cookie = item->bpf_cookie;
1513         ret &= run_prog(prog, ctx);
1514         item++;
1515     }
1516     bpf_reset_run_ctx(old_run_ctx);
1517     migrate_enable();
1518     return ret;
1519 }
1520 
1521 /* Notes on RCU design for bpf_prog_arrays containing sleepable programs:
1522  *
1523  * We use the tasks_trace rcu flavor read section to protect the bpf_prog_array
1524  * overall. As a result, we must use the bpf_prog_array_free_sleepable
1525  * in order to use the tasks_trace rcu grace period.
1526  *
1527  * When a non-sleepable program is inside the array, we take the rcu read
1528  * section and disable preemption for that program alone, so it can access
1529  * rcu-protected dynamically sized maps.
1530  */
1531 static __always_inline u32
1532 bpf_prog_run_array_sleepable(const struct bpf_prog_array __rcu *array_rcu,
1533                  const void *ctx, bpf_prog_run_fn run_prog)
1534 {
1535     const struct bpf_prog_array_item *item;
1536     const struct bpf_prog *prog;
1537     const struct bpf_prog_array *array;
1538     struct bpf_run_ctx *old_run_ctx;
1539     struct bpf_trace_run_ctx run_ctx;
1540     u32 ret = 1;
1541 
1542     might_fault();
1543 
1544     rcu_read_lock_trace();
1545     migrate_disable();
1546 
1547     array = rcu_dereference_check(array_rcu, rcu_read_lock_trace_held());
1548     if (unlikely(!array))
1549         goto out;
1550     old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
1551     item = &array->items[0];
1552     while ((prog = READ_ONCE(item->prog))) {
1553         if (!prog->aux->sleepable)
1554             rcu_read_lock();
1555 
1556         run_ctx.bpf_cookie = item->bpf_cookie;
1557         ret &= run_prog(prog, ctx);
1558         item++;
1559 
1560         if (!prog->aux->sleepable)
1561             rcu_read_unlock();
1562     }
1563     bpf_reset_run_ctx(old_run_ctx);
1564 out:
1565     migrate_enable();
1566     rcu_read_unlock_trace();
1567     return ret;
1568 }
1569 
1570 #ifdef CONFIG_BPF_SYSCALL
1571 DECLARE_PER_CPU(int, bpf_prog_active);
1572 extern struct mutex bpf_stats_enabled_mutex;
1573 
1574 /*
1575  * Block execution of BPF programs attached to instrumentation (perf,
1576  * kprobes, tracepoints) to prevent deadlocks on map operations as any of
1577  * these events can happen inside a region which holds a map bucket lock
1578  * and can deadlock on it.
1579  */
1580 static inline void bpf_disable_instrumentation(void)
1581 {
1582     migrate_disable();
1583     this_cpu_inc(bpf_prog_active);
1584 }
1585 
1586 static inline void bpf_enable_instrumentation(void)
1587 {
1588     this_cpu_dec(bpf_prog_active);
1589     migrate_enable();
1590 }
1591 
1592 extern const struct file_operations bpf_map_fops;
1593 extern const struct file_operations bpf_prog_fops;
1594 extern const struct file_operations bpf_iter_fops;
1595 
1596 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1597     extern const struct bpf_prog_ops _name ## _prog_ops; \
1598     extern const struct bpf_verifier_ops _name ## _verifier_ops;
1599 #define BPF_MAP_TYPE(_id, _ops) \
1600     extern const struct bpf_map_ops _ops;
1601 #define BPF_LINK_TYPE(_id, _name)
1602 #include <linux/bpf_types.h>
1603 #undef BPF_PROG_TYPE
1604 #undef BPF_MAP_TYPE
1605 #undef BPF_LINK_TYPE
1606 
1607 extern const struct bpf_prog_ops bpf_offload_prog_ops;
1608 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
1609 extern const struct bpf_verifier_ops xdp_analyzer_ops;
1610 
1611 struct bpf_prog *bpf_prog_get(u32 ufd);
1612 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1613                        bool attach_drv);
1614 void bpf_prog_add(struct bpf_prog *prog, int i);
1615 void bpf_prog_sub(struct bpf_prog *prog, int i);
1616 void bpf_prog_inc(struct bpf_prog *prog);
1617 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
1618 void bpf_prog_put(struct bpf_prog *prog);
1619 
1620 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
1621 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
1622 
1623 struct bpf_map_value_off_desc *bpf_map_kptr_off_contains(struct bpf_map *map, u32 offset);
1624 void bpf_map_free_kptr_off_tab(struct bpf_map *map);
1625 struct bpf_map_value_off *bpf_map_copy_kptr_off_tab(const struct bpf_map *map);
1626 bool bpf_map_equal_kptr_off_tab(const struct bpf_map *map_a, const struct bpf_map *map_b);
1627 void bpf_map_free_kptrs(struct bpf_map *map, void *map_value);
1628 
1629 struct bpf_map *bpf_map_get(u32 ufd);
1630 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
1631 struct bpf_map *__bpf_map_get(struct fd f);
1632 void bpf_map_inc(struct bpf_map *map);
1633 void bpf_map_inc_with_uref(struct bpf_map *map);
1634 struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
1635 void bpf_map_put_with_uref(struct bpf_map *map);
1636 void bpf_map_put(struct bpf_map *map);
1637 void *bpf_map_area_alloc(u64 size, int numa_node);
1638 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
1639 void bpf_map_area_free(void *base);
1640 bool bpf_map_write_active(const struct bpf_map *map);
1641 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
1642 int  generic_map_lookup_batch(struct bpf_map *map,
1643                   const union bpf_attr *attr,
1644                   union bpf_attr __user *uattr);
1645 int  generic_map_update_batch(struct bpf_map *map,
1646                   const union bpf_attr *attr,
1647                   union bpf_attr __user *uattr);
1648 int  generic_map_delete_batch(struct bpf_map *map,
1649                   const union bpf_attr *attr,
1650                   union bpf_attr __user *uattr);
1651 struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
1652 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id);
1653 
1654 #ifdef CONFIG_MEMCG_KMEM
1655 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1656                int node);
1657 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags);
1658 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
1659                     size_t align, gfp_t flags);
1660 #else
1661 static inline void *
1662 bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1663              int node)
1664 {
1665     return kmalloc_node(size, flags, node);
1666 }
1667 
1668 static inline void *
1669 bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
1670 {
1671     return kzalloc(size, flags);
1672 }
1673 
1674 static inline void __percpu *
1675 bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align,
1676              gfp_t flags)
1677 {
1678     return __alloc_percpu_gfp(size, align, flags);
1679 }
1680 #endif
1681 
1682 extern int sysctl_unprivileged_bpf_disabled;
1683 
1684 static inline bool bpf_allow_ptr_leaks(void)
1685 {
1686     return perfmon_capable();
1687 }
1688 
1689 static inline bool bpf_allow_uninit_stack(void)
1690 {
1691     return perfmon_capable();
1692 }
1693 
1694 static inline bool bpf_allow_ptr_to_map_access(void)
1695 {
1696     return perfmon_capable();
1697 }
1698 
1699 static inline bool bpf_bypass_spec_v1(void)
1700 {
1701     return perfmon_capable();
1702 }
1703 
1704 static inline bool bpf_bypass_spec_v4(void)
1705 {
1706     return perfmon_capable();
1707 }
1708 
1709 int bpf_map_new_fd(struct bpf_map *map, int flags);
1710 int bpf_prog_new_fd(struct bpf_prog *prog);
1711 
1712 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
1713            const struct bpf_link_ops *ops, struct bpf_prog *prog);
1714 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
1715 int bpf_link_settle(struct bpf_link_primer *primer);
1716 void bpf_link_cleanup(struct bpf_link_primer *primer);
1717 void bpf_link_inc(struct bpf_link *link);
1718 void bpf_link_put(struct bpf_link *link);
1719 int bpf_link_new_fd(struct bpf_link *link);
1720 struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
1721 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
1722 struct bpf_link *bpf_link_get_curr_or_next(u32 *id);
1723 
1724 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
1725 int bpf_obj_get_user(const char __user *pathname, int flags);
1726 
1727 #define BPF_ITER_FUNC_PREFIX "bpf_iter_"
1728 #define DEFINE_BPF_ITER_FUNC(target, args...)           \
1729     extern int bpf_iter_ ## target(args);           \
1730     int __init bpf_iter_ ## target(args) { return 0; }
1731 
1732 struct bpf_iter_aux_info {
1733     struct bpf_map *map;
1734 };
1735 
1736 typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
1737                     union bpf_iter_link_info *linfo,
1738                     struct bpf_iter_aux_info *aux);
1739 typedef void (*bpf_iter_detach_target_t)(struct bpf_iter_aux_info *aux);
1740 typedef void (*bpf_iter_show_fdinfo_t) (const struct bpf_iter_aux_info *aux,
1741                     struct seq_file *seq);
1742 typedef int (*bpf_iter_fill_link_info_t)(const struct bpf_iter_aux_info *aux,
1743                      struct bpf_link_info *info);
1744 typedef const struct bpf_func_proto *
1745 (*bpf_iter_get_func_proto_t)(enum bpf_func_id func_id,
1746                  const struct bpf_prog *prog);
1747 
1748 enum bpf_iter_feature {
1749     BPF_ITER_RESCHED    = BIT(0),
1750 };
1751 
1752 #define BPF_ITER_CTX_ARG_MAX 2
1753 struct bpf_iter_reg {
1754     const char *target;
1755     bpf_iter_attach_target_t attach_target;
1756     bpf_iter_detach_target_t detach_target;
1757     bpf_iter_show_fdinfo_t show_fdinfo;
1758     bpf_iter_fill_link_info_t fill_link_info;
1759     bpf_iter_get_func_proto_t get_func_proto;
1760     u32 ctx_arg_info_size;
1761     u32 feature;
1762     struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
1763     const struct bpf_iter_seq_info *seq_info;
1764 };
1765 
1766 struct bpf_iter_meta {
1767     __bpf_md_ptr(struct seq_file *, seq);
1768     u64 session_id;
1769     u64 seq_num;
1770 };
1771 
1772 struct bpf_iter__bpf_map_elem {
1773     __bpf_md_ptr(struct bpf_iter_meta *, meta);
1774     __bpf_md_ptr(struct bpf_map *, map);
1775     __bpf_md_ptr(void *, key);
1776     __bpf_md_ptr(void *, value);
1777 };
1778 
1779 int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
1780 void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
1781 bool bpf_iter_prog_supported(struct bpf_prog *prog);
1782 const struct bpf_func_proto *
1783 bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
1784 int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_prog *prog);
1785 int bpf_iter_new_fd(struct bpf_link *link);
1786 bool bpf_link_is_iter(struct bpf_link *link);
1787 struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
1788 int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx);
1789 void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
1790                   struct seq_file *seq);
1791 int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
1792                 struct bpf_link_info *info);
1793 
1794 int map_set_for_each_callback_args(struct bpf_verifier_env *env,
1795                    struct bpf_func_state *caller,
1796                    struct bpf_func_state *callee);
1797 
1798 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
1799 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
1800 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1801                u64 flags);
1802 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
1803                 u64 flags);
1804 
1805 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
1806 
1807 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
1808                  void *key, void *value, u64 map_flags);
1809 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1810 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1811                 void *key, void *value, u64 map_flags);
1812 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
1813 
1814 int bpf_get_file_flag(int flags);
1815 int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size,
1816                  size_t actual_size);
1817 
1818 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
1819  * forced to use 'long' read/writes to try to atomically copy long counters.
1820  * Best-effort only.  No barriers here, since it _will_ race with concurrent
1821  * updates from BPF programs. Called from bpf syscall and mostly used with
1822  * size 8 or 16 bytes, so ask compiler to inline it.
1823  */
1824 static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
1825 {
1826     const long *lsrc = src;
1827     long *ldst = dst;
1828 
1829     size /= sizeof(long);
1830     while (size--)
1831         *ldst++ = *lsrc++;
1832 }
1833 
1834 /* verify correctness of eBPF program */
1835 int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr);
1836 
1837 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1838 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
1839 #endif
1840 
1841 struct btf *bpf_get_btf_vmlinux(void);
1842 
1843 /* Map specifics */
1844 struct xdp_frame;
1845 struct sk_buff;
1846 struct bpf_dtab_netdev;
1847 struct bpf_cpu_map_entry;
1848 
1849 void __dev_flush(void);
1850 int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
1851             struct net_device *dev_rx);
1852 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
1853             struct net_device *dev_rx);
1854 int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
1855               struct bpf_map *map, bool exclude_ingress);
1856 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
1857                  struct bpf_prog *xdp_prog);
1858 int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
1859                struct bpf_prog *xdp_prog, struct bpf_map *map,
1860                bool exclude_ingress);
1861 
1862 void __cpu_map_flush(void);
1863 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf,
1864             struct net_device *dev_rx);
1865 int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
1866                  struct sk_buff *skb);
1867 
1868 /* Return map's numa specified by userspace */
1869 static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
1870 {
1871     return (attr->map_flags & BPF_F_NUMA_NODE) ?
1872         attr->numa_node : NUMA_NO_NODE;
1873 }
1874 
1875 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
1876 int array_map_alloc_check(union bpf_attr *attr);
1877 
1878 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1879               union bpf_attr __user *uattr);
1880 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1881               union bpf_attr __user *uattr);
1882 int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1883                   const union bpf_attr *kattr,
1884                   union bpf_attr __user *uattr);
1885 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1886                      const union bpf_attr *kattr,
1887                      union bpf_attr __user *uattr);
1888 int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
1889                  const union bpf_attr *kattr,
1890                  union bpf_attr __user *uattr);
1891 int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
1892                 const union bpf_attr *kattr,
1893                 union bpf_attr __user *uattr);
1894 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
1895             const struct bpf_prog *prog,
1896             struct bpf_insn_access_aux *info);
1897 
1898 static inline bool bpf_tracing_ctx_access(int off, int size,
1899                       enum bpf_access_type type)
1900 {
1901     if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1902         return false;
1903     if (type != BPF_READ)
1904         return false;
1905     if (off % size != 0)
1906         return false;
1907     return true;
1908 }
1909 
1910 static inline bool bpf_tracing_btf_ctx_access(int off, int size,
1911                           enum bpf_access_type type,
1912                           const struct bpf_prog *prog,
1913                           struct bpf_insn_access_aux *info)
1914 {
1915     if (!bpf_tracing_ctx_access(off, size, type))
1916         return false;
1917     return btf_ctx_access(off, size, type, prog, info);
1918 }
1919 
1920 int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf,
1921               const struct btf_type *t, int off, int size,
1922               enum bpf_access_type atype,
1923               u32 *next_btf_id, enum bpf_type_flag *flag);
1924 bool btf_struct_ids_match(struct bpf_verifier_log *log,
1925               const struct btf *btf, u32 id, int off,
1926               const struct btf *need_btf, u32 need_type_id,
1927               bool strict);
1928 
1929 int btf_distill_func_proto(struct bpf_verifier_log *log,
1930                struct btf *btf,
1931                const struct btf_type *func_proto,
1932                const char *func_name,
1933                struct btf_func_model *m);
1934 
1935 struct bpf_reg_state;
1936 int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
1937                 struct bpf_reg_state *regs);
1938 int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
1939                   const struct btf *btf, u32 func_id,
1940                   struct bpf_reg_state *regs,
1941                   u32 kfunc_flags);
1942 int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
1943               struct bpf_reg_state *reg);
1944 int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
1945              struct btf *btf, const struct btf_type *t);
1946 
1947 struct bpf_prog *bpf_prog_by_id(u32 id);
1948 struct bpf_link *bpf_link_by_id(u32 id);
1949 
1950 const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
1951 void bpf_task_storage_free(struct task_struct *task);
1952 bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);
1953 const struct btf_func_model *
1954 bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
1955              const struct bpf_insn *insn);
1956 struct bpf_core_ctx {
1957     struct bpf_verifier_log *log;
1958     const struct btf *btf;
1959 };
1960 
1961 int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
1962            int relo_idx, void *insn);
1963 
1964 static inline bool unprivileged_ebpf_enabled(void)
1965 {
1966     return !sysctl_unprivileged_bpf_disabled;
1967 }
1968 
1969 #else /* !CONFIG_BPF_SYSCALL */
1970 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
1971 {
1972     return ERR_PTR(-EOPNOTSUPP);
1973 }
1974 
1975 static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
1976                              enum bpf_prog_type type,
1977                              bool attach_drv)
1978 {
1979     return ERR_PTR(-EOPNOTSUPP);
1980 }
1981 
1982 static inline void bpf_prog_add(struct bpf_prog *prog, int i)
1983 {
1984 }
1985 
1986 static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
1987 {
1988 }
1989 
1990 static inline void bpf_prog_put(struct bpf_prog *prog)
1991 {
1992 }
1993 
1994 static inline void bpf_prog_inc(struct bpf_prog *prog)
1995 {
1996 }
1997 
1998 static inline struct bpf_prog *__must_check
1999 bpf_prog_inc_not_zero(struct bpf_prog *prog)
2000 {
2001     return ERR_PTR(-EOPNOTSUPP);
2002 }
2003 
2004 static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2005                  const struct bpf_link_ops *ops,
2006                  struct bpf_prog *prog)
2007 {
2008 }
2009 
2010 static inline int bpf_link_prime(struct bpf_link *link,
2011                  struct bpf_link_primer *primer)
2012 {
2013     return -EOPNOTSUPP;
2014 }
2015 
2016 static inline int bpf_link_settle(struct bpf_link_primer *primer)
2017 {
2018     return -EOPNOTSUPP;
2019 }
2020 
2021 static inline void bpf_link_cleanup(struct bpf_link_primer *primer)
2022 {
2023 }
2024 
2025 static inline void bpf_link_inc(struct bpf_link *link)
2026 {
2027 }
2028 
2029 static inline void bpf_link_put(struct bpf_link *link)
2030 {
2031 }
2032 
2033 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
2034 {
2035     return -EOPNOTSUPP;
2036 }
2037 
2038 static inline void __dev_flush(void)
2039 {
2040 }
2041 
2042 struct xdp_frame;
2043 struct bpf_dtab_netdev;
2044 struct bpf_cpu_map_entry;
2045 
2046 static inline
2047 int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
2048             struct net_device *dev_rx)
2049 {
2050     return 0;
2051 }
2052 
2053 static inline
2054 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
2055             struct net_device *dev_rx)
2056 {
2057     return 0;
2058 }
2059 
2060 static inline
2061 int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
2062               struct bpf_map *map, bool exclude_ingress)
2063 {
2064     return 0;
2065 }
2066 
2067 struct sk_buff;
2068 
2069 static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
2070                        struct sk_buff *skb,
2071                        struct bpf_prog *xdp_prog)
2072 {
2073     return 0;
2074 }
2075 
2076 static inline
2077 int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
2078                struct bpf_prog *xdp_prog, struct bpf_map *map,
2079                bool exclude_ingress)
2080 {
2081     return 0;
2082 }
2083 
2084 static inline void __cpu_map_flush(void)
2085 {
2086 }
2087 
2088 static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
2089                   struct xdp_frame *xdpf,
2090                   struct net_device *dev_rx)
2091 {
2092     return 0;
2093 }
2094 
2095 static inline int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
2096                        struct sk_buff *skb)
2097 {
2098     return -EOPNOTSUPP;
2099 }
2100 
2101 static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
2102                 enum bpf_prog_type type)
2103 {
2104     return ERR_PTR(-EOPNOTSUPP);
2105 }
2106 
2107 static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
2108                     const union bpf_attr *kattr,
2109                     union bpf_attr __user *uattr)
2110 {
2111     return -ENOTSUPP;
2112 }
2113 
2114 static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
2115                     const union bpf_attr *kattr,
2116                     union bpf_attr __user *uattr)
2117 {
2118     return -ENOTSUPP;
2119 }
2120 
2121 static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
2122                         const union bpf_attr *kattr,
2123                         union bpf_attr __user *uattr)
2124 {
2125     return -ENOTSUPP;
2126 }
2127 
2128 static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
2129                            const union bpf_attr *kattr,
2130                            union bpf_attr __user *uattr)
2131 {
2132     return -ENOTSUPP;
2133 }
2134 
2135 static inline int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
2136                           const union bpf_attr *kattr,
2137                           union bpf_attr __user *uattr)
2138 {
2139     return -ENOTSUPP;
2140 }
2141 
2142 static inline void bpf_map_put(struct bpf_map *map)
2143 {
2144 }
2145 
2146 static inline struct bpf_prog *bpf_prog_by_id(u32 id)
2147 {
2148     return ERR_PTR(-ENOTSUPP);
2149 }
2150 
2151 static inline const struct bpf_func_proto *
2152 bpf_base_func_proto(enum bpf_func_id func_id)
2153 {
2154     return NULL;
2155 }
2156 
2157 static inline void bpf_task_storage_free(struct task_struct *task)
2158 {
2159 }
2160 
2161 static inline bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog)
2162 {
2163     return false;
2164 }
2165 
2166 static inline const struct btf_func_model *
2167 bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
2168              const struct bpf_insn *insn)
2169 {
2170     return NULL;
2171 }
2172 
2173 static inline bool unprivileged_ebpf_enabled(void)
2174 {
2175     return false;
2176 }
2177 
2178 #endif /* CONFIG_BPF_SYSCALL */
2179 
2180 void __bpf_free_used_btfs(struct bpf_prog_aux *aux,
2181               struct btf_mod_pair *used_btfs, u32 len);
2182 
2183 static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
2184                          enum bpf_prog_type type)
2185 {
2186     return bpf_prog_get_type_dev(ufd, type, false);
2187 }
2188 
2189 void __bpf_free_used_maps(struct bpf_prog_aux *aux,
2190               struct bpf_map **used_maps, u32 len);
2191 
2192 bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
2193 
2194 int bpf_prog_offload_compile(struct bpf_prog *prog);
2195 void bpf_prog_offload_destroy(struct bpf_prog *prog);
2196 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
2197                    struct bpf_prog *prog);
2198 
2199 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
2200 
2201 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
2202 int bpf_map_offload_update_elem(struct bpf_map *map,
2203                 void *key, void *value, u64 flags);
2204 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
2205 int bpf_map_offload_get_next_key(struct bpf_map *map,
2206                  void *key, void *next_key);
2207 
2208 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
2209 
2210 struct bpf_offload_dev *
2211 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
2212 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
2213 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
2214 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
2215                     struct net_device *netdev);
2216 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
2217                        struct net_device *netdev);
2218 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
2219 
2220 void unpriv_ebpf_notify(int new_state);
2221 
2222 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
2223 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
2224 
2225 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
2226 {
2227     return aux->offload_requested;
2228 }
2229 
2230 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
2231 {
2232     return unlikely(map->ops == &bpf_map_offload_ops);
2233 }
2234 
2235 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
2236 void bpf_map_offload_map_free(struct bpf_map *map);
2237 int bpf_prog_test_run_syscall(struct bpf_prog *prog,
2238                   const union bpf_attr *kattr,
2239                   union bpf_attr __user *uattr);
2240 
2241 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
2242 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype);
2243 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags);
2244 int sock_map_bpf_prog_query(const union bpf_attr *attr,
2245                 union bpf_attr __user *uattr);
2246 
2247 void sock_map_unhash(struct sock *sk);
2248 void sock_map_destroy(struct sock *sk);
2249 void sock_map_close(struct sock *sk, long timeout);
2250 #else
2251 static inline int bpf_prog_offload_init(struct bpf_prog *prog,
2252                     union bpf_attr *attr)
2253 {
2254     return -EOPNOTSUPP;
2255 }
2256 
2257 static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
2258 {
2259     return false;
2260 }
2261 
2262 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
2263 {
2264     return false;
2265 }
2266 
2267 static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
2268 {
2269     return ERR_PTR(-EOPNOTSUPP);
2270 }
2271 
2272 static inline void bpf_map_offload_map_free(struct bpf_map *map)
2273 {
2274 }
2275 
2276 static inline int bpf_prog_test_run_syscall(struct bpf_prog *prog,
2277                         const union bpf_attr *kattr,
2278                         union bpf_attr __user *uattr)
2279 {
2280     return -ENOTSUPP;
2281 }
2282 
2283 #ifdef CONFIG_BPF_SYSCALL
2284 static inline int sock_map_get_from_fd(const union bpf_attr *attr,
2285                        struct bpf_prog *prog)
2286 {
2287     return -EINVAL;
2288 }
2289 
2290 static inline int sock_map_prog_detach(const union bpf_attr *attr,
2291                        enum bpf_prog_type ptype)
2292 {
2293     return -EOPNOTSUPP;
2294 }
2295 
2296 static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
2297                        u64 flags)
2298 {
2299     return -EOPNOTSUPP;
2300 }
2301 
2302 static inline int sock_map_bpf_prog_query(const union bpf_attr *attr,
2303                       union bpf_attr __user *uattr)
2304 {
2305     return -EINVAL;
2306 }
2307 #endif /* CONFIG_BPF_SYSCALL */
2308 #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
2309 
2310 #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
2311 void bpf_sk_reuseport_detach(struct sock *sk);
2312 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
2313                        void *value);
2314 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
2315                        void *value, u64 map_flags);
2316 #else
2317 static inline void bpf_sk_reuseport_detach(struct sock *sk)
2318 {
2319 }
2320 
2321 #ifdef CONFIG_BPF_SYSCALL
2322 static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
2323                              void *key, void *value)
2324 {
2325     return -EOPNOTSUPP;
2326 }
2327 
2328 static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
2329                              void *key, void *value,
2330                              u64 map_flags)
2331 {
2332     return -EOPNOTSUPP;
2333 }
2334 #endif /* CONFIG_BPF_SYSCALL */
2335 #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
2336 
2337 /* verifier prototypes for helper functions called from eBPF programs */
2338 extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
2339 extern const struct bpf_func_proto bpf_map_update_elem_proto;
2340 extern const struct bpf_func_proto bpf_map_delete_elem_proto;
2341 extern const struct bpf_func_proto bpf_map_push_elem_proto;
2342 extern const struct bpf_func_proto bpf_map_pop_elem_proto;
2343 extern const struct bpf_func_proto bpf_map_peek_elem_proto;
2344 extern const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto;
2345 
2346 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
2347 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
2348 extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
2349 extern const struct bpf_func_proto bpf_tail_call_proto;
2350 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
2351 extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
2352 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
2353 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
2354 extern const struct bpf_func_proto bpf_get_current_comm_proto;
2355 extern const struct bpf_func_proto bpf_get_stackid_proto;
2356 extern const struct bpf_func_proto bpf_get_stack_proto;
2357 extern const struct bpf_func_proto bpf_get_task_stack_proto;
2358 extern const struct bpf_func_proto bpf_get_stackid_proto_pe;
2359 extern const struct bpf_func_proto bpf_get_stack_proto_pe;
2360 extern const struct bpf_func_proto bpf_sock_map_update_proto;
2361 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
2362 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
2363 extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
2364 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
2365 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
2366 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
2367 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
2368 extern const struct bpf_func_proto bpf_spin_lock_proto;
2369 extern const struct bpf_func_proto bpf_spin_unlock_proto;
2370 extern const struct bpf_func_proto bpf_get_local_storage_proto;
2371 extern const struct bpf_func_proto bpf_strtol_proto;
2372 extern const struct bpf_func_proto bpf_strtoul_proto;
2373 extern const struct bpf_func_proto bpf_tcp_sock_proto;
2374 extern const struct bpf_func_proto bpf_jiffies64_proto;
2375 extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
2376 extern const struct bpf_func_proto bpf_event_output_data_proto;
2377 extern const struct bpf_func_proto bpf_ringbuf_output_proto;
2378 extern const struct bpf_func_proto bpf_ringbuf_reserve_proto;
2379 extern const struct bpf_func_proto bpf_ringbuf_submit_proto;
2380 extern const struct bpf_func_proto bpf_ringbuf_discard_proto;
2381 extern const struct bpf_func_proto bpf_ringbuf_query_proto;
2382 extern const struct bpf_func_proto bpf_ringbuf_reserve_dynptr_proto;
2383 extern const struct bpf_func_proto bpf_ringbuf_submit_dynptr_proto;
2384 extern const struct bpf_func_proto bpf_ringbuf_discard_dynptr_proto;
2385 extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
2386 extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
2387 extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
2388 extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
2389 extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
2390 extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;
2391 extern const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto;
2392 extern const struct bpf_func_proto bpf_copy_from_user_proto;
2393 extern const struct bpf_func_proto bpf_snprintf_btf_proto;
2394 extern const struct bpf_func_proto bpf_snprintf_proto;
2395 extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
2396 extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
2397 extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;
2398 extern const struct bpf_func_proto bpf_sock_from_file_proto;
2399 extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;
2400 extern const struct bpf_func_proto bpf_task_storage_get_proto;
2401 extern const struct bpf_func_proto bpf_task_storage_delete_proto;
2402 extern const struct bpf_func_proto bpf_for_each_map_elem_proto;
2403 extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto;
2404 extern const struct bpf_func_proto bpf_sk_setsockopt_proto;
2405 extern const struct bpf_func_proto bpf_sk_getsockopt_proto;
2406 extern const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto;
2407 extern const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto;
2408 extern const struct bpf_func_proto bpf_find_vma_proto;
2409 extern const struct bpf_func_proto bpf_loop_proto;
2410 extern const struct bpf_func_proto bpf_copy_from_user_task_proto;
2411 extern const struct bpf_func_proto bpf_set_retval_proto;
2412 extern const struct bpf_func_proto bpf_get_retval_proto;
2413 
2414 const struct bpf_func_proto *tracing_prog_func_proto(
2415   enum bpf_func_id func_id, const struct bpf_prog *prog);
2416 
2417 /* Shared helpers among cBPF and eBPF. */
2418 void bpf_user_rnd_init_once(void);
2419 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
2420 u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
2421 
2422 #if defined(CONFIG_NET)
2423 bool bpf_sock_common_is_valid_access(int off, int size,
2424                      enum bpf_access_type type,
2425                      struct bpf_insn_access_aux *info);
2426 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2427                   struct bpf_insn_access_aux *info);
2428 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
2429                 const struct bpf_insn *si,
2430                 struct bpf_insn *insn_buf,
2431                 struct bpf_prog *prog,
2432                 u32 *target_size);
2433 #else
2434 static inline bool bpf_sock_common_is_valid_access(int off, int size,
2435                            enum bpf_access_type type,
2436                            struct bpf_insn_access_aux *info)
2437 {
2438     return false;
2439 }
2440 static inline bool bpf_sock_is_valid_access(int off, int size,
2441                         enum bpf_access_type type,
2442                         struct bpf_insn_access_aux *info)
2443 {
2444     return false;
2445 }
2446 static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
2447                           const struct bpf_insn *si,
2448                           struct bpf_insn *insn_buf,
2449                           struct bpf_prog *prog,
2450                           u32 *target_size)
2451 {
2452     return 0;
2453 }
2454 #endif
2455 
2456 #ifdef CONFIG_INET
2457 struct sk_reuseport_kern {
2458     struct sk_buff *skb;
2459     struct sock *sk;
2460     struct sock *selected_sk;
2461     struct sock *migrating_sk;
2462     void *data_end;
2463     u32 hash;
2464     u32 reuseport_id;
2465     bool bind_inany;
2466 };
2467 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2468                   struct bpf_insn_access_aux *info);
2469 
2470 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
2471                     const struct bpf_insn *si,
2472                     struct bpf_insn *insn_buf,
2473                     struct bpf_prog *prog,
2474                     u32 *target_size);
2475 
2476 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2477                   struct bpf_insn_access_aux *info);
2478 
2479 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2480                     const struct bpf_insn *si,
2481                     struct bpf_insn *insn_buf,
2482                     struct bpf_prog *prog,
2483                     u32 *target_size);
2484 #else
2485 static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
2486                         enum bpf_access_type type,
2487                         struct bpf_insn_access_aux *info)
2488 {
2489     return false;
2490 }
2491 
2492 static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
2493                           const struct bpf_insn *si,
2494                           struct bpf_insn *insn_buf,
2495                           struct bpf_prog *prog,
2496                           u32 *target_size)
2497 {
2498     return 0;
2499 }
2500 static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
2501                         enum bpf_access_type type,
2502                         struct bpf_insn_access_aux *info)
2503 {
2504     return false;
2505 }
2506 
2507 static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2508                           const struct bpf_insn *si,
2509                           struct bpf_insn *insn_buf,
2510                           struct bpf_prog *prog,
2511                           u32 *target_size)
2512 {
2513     return 0;
2514 }
2515 #endif /* CONFIG_INET */
2516 
2517 enum bpf_text_poke_type {
2518     BPF_MOD_CALL,
2519     BPF_MOD_JUMP,
2520 };
2521 
2522 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
2523                void *addr1, void *addr2);
2524 
2525 void *bpf_arch_text_copy(void *dst, void *src, size_t len);
2526 int bpf_arch_text_invalidate(void *dst, size_t len);
2527 
2528 struct btf_id_set;
2529 bool btf_id_set_contains(const struct btf_id_set *set, u32 id);
2530 
2531 #define MAX_BPRINTF_VARARGS     12
2532 
2533 int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
2534             u32 **bin_buf, u32 num_args);
2535 void bpf_bprintf_cleanup(void);
2536 
2537 /* the implementation of the opaque uapi struct bpf_dynptr */
2538 struct bpf_dynptr_kern {
2539     void *data;
2540     /* Size represents the number of usable bytes of dynptr data.
2541      * If for example the offset is at 4 for a local dynptr whose data is
2542      * of type u64, the number of usable bytes is 4.
2543      *
2544      * The upper 8 bits are reserved. It is as follows:
2545      * Bits 0 - 23 = size
2546      * Bits 24 - 30 = dynptr type
2547      * Bit 31 = whether dynptr is read-only
2548      */
2549     u32 size;
2550     u32 offset;
2551 } __aligned(8);
2552 
2553 enum bpf_dynptr_type {
2554     BPF_DYNPTR_TYPE_INVALID,
2555     /* Points to memory that is local to the bpf program */
2556     BPF_DYNPTR_TYPE_LOCAL,
2557     /* Underlying data is a ringbuf record */
2558     BPF_DYNPTR_TYPE_RINGBUF,
2559 };
2560 
2561 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
2562              enum bpf_dynptr_type type, u32 offset, u32 size);
2563 void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr);
2564 int bpf_dynptr_check_size(u32 size);
2565 
2566 #ifdef CONFIG_BPF_LSM
2567 void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype);
2568 void bpf_cgroup_atype_put(int cgroup_atype);
2569 #else
2570 static inline void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype) {}
2571 static inline void bpf_cgroup_atype_put(int cgroup_atype) {}
2572 #endif /* CONFIG_BPF_LSM */
2573 
2574 #endif /* _LINUX_BPF_H */