0001
0002
0003
0004
0005
0006
0007 #ifndef _BPF_LOCAL_STORAGE_H
0008 #define _BPF_LOCAL_STORAGE_H
0009
0010 #include <linux/bpf.h>
0011 #include <linux/filter.h>
0012 #include <linux/rculist.h>
0013 #include <linux/list.h>
0014 #include <linux/hash.h>
0015 #include <linux/types.h>
0016 #include <uapi/linux/btf.h>
0017
0018 #define BPF_LOCAL_STORAGE_CACHE_SIZE 16
0019
0020 #define bpf_rcu_lock_held() \
0021 (rcu_read_lock_held() || rcu_read_lock_trace_held() || \
0022 rcu_read_lock_bh_held())
0023 struct bpf_local_storage_map_bucket {
0024 struct hlist_head list;
0025 raw_spinlock_t lock;
0026 };
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 struct bpf_local_storage_map {
0047 struct bpf_map map;
0048
0049
0050
0051
0052
0053
0054 struct bpf_local_storage_map_bucket *buckets;
0055 u32 bucket_log;
0056 u16 elem_size;
0057 u16 cache_idx;
0058 };
0059
0060 struct bpf_local_storage_data {
0061
0062
0063
0064
0065
0066
0067 struct bpf_local_storage_map __rcu *smap;
0068 u8 data[] __aligned(8);
0069 };
0070
0071
0072 struct bpf_local_storage_elem {
0073 struct hlist_node map_node;
0074 struct hlist_node snode;
0075 struct bpf_local_storage __rcu *local_storage;
0076 struct rcu_head rcu;
0077
0078
0079
0080
0081 struct bpf_local_storage_data sdata ____cacheline_aligned;
0082 };
0083
0084 struct bpf_local_storage {
0085 struct bpf_local_storage_data __rcu *cache[BPF_LOCAL_STORAGE_CACHE_SIZE];
0086 struct hlist_head list;
0087 void *owner;
0088
0089
0090 struct rcu_head rcu;
0091 raw_spinlock_t lock;
0092 };
0093
0094
0095
0096
0097 #define BPF_LOCAL_STORAGE_MAX_VALUE_SIZE \
0098 min_t(u32, \
0099 (KMALLOC_MAX_SIZE - MAX_BPF_STACK - \
0100 sizeof(struct bpf_local_storage_elem)), \
0101 (U16_MAX - sizeof(struct bpf_local_storage_elem)))
0102
0103 #define SELEM(_SDATA) \
0104 container_of((_SDATA), struct bpf_local_storage_elem, sdata)
0105 #define SDATA(_SELEM) (&(_SELEM)->sdata)
0106
0107 #define BPF_LOCAL_STORAGE_CACHE_SIZE 16
0108
0109 struct bpf_local_storage_cache {
0110 spinlock_t idx_lock;
0111 u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE];
0112 };
0113
0114 #define DEFINE_BPF_STORAGE_CACHE(name) \
0115 static struct bpf_local_storage_cache name = { \
0116 .idx_lock = __SPIN_LOCK_UNLOCKED(name.idx_lock), \
0117 }
0118
0119 u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache);
0120 void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache,
0121 u16 idx);
0122
0123
0124 int bpf_local_storage_map_alloc_check(union bpf_attr *attr);
0125
0126 struct bpf_local_storage_map *bpf_local_storage_map_alloc(union bpf_attr *attr);
0127
0128 struct bpf_local_storage_data *
0129 bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
0130 struct bpf_local_storage_map *smap,
0131 bool cacheit_lockit);
0132
0133 void bpf_local_storage_map_free(struct bpf_local_storage_map *smap,
0134 int __percpu *busy_counter);
0135
0136 int bpf_local_storage_map_check_btf(const struct bpf_map *map,
0137 const struct btf *btf,
0138 const struct btf_type *key_type,
0139 const struct btf_type *value_type);
0140
0141 void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
0142 struct bpf_local_storage_elem *selem);
0143
0144 bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage,
0145 struct bpf_local_storage_elem *selem,
0146 bool uncharge_omem, bool use_trace_rcu);
0147
0148 void bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool use_trace_rcu);
0149
0150 void bpf_selem_link_map(struct bpf_local_storage_map *smap,
0151 struct bpf_local_storage_elem *selem);
0152
0153 void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem);
0154
0155 struct bpf_local_storage_elem *
0156 bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,
0157 bool charge_mem, gfp_t gfp_flags);
0158
0159 int
0160 bpf_local_storage_alloc(void *owner,
0161 struct bpf_local_storage_map *smap,
0162 struct bpf_local_storage_elem *first_selem,
0163 gfp_t gfp_flags);
0164
0165 struct bpf_local_storage_data *
0166 bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
0167 void *value, u64 map_flags, gfp_t gfp_flags);
0168
0169 void bpf_local_storage_free_rcu(struct rcu_head *rcu);
0170
0171 #endif