1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2019 Facebook
4 * Copyright 2020 Google LLC.
7 #ifndef _BPF_LOCAL_STORAGE_H
8 #define _BPF_LOCAL_STORAGE_H
10 #include <linux/bpf.h>
11 #include <linux/filter.h>
12 #include <linux/rculist.h>
13 #include <linux/list.h>
14 #include <linux/hash.h>
15 #include <linux/types.h>
16 #include <uapi/linux/btf.h>
18 #define BPF_LOCAL_STORAGE_CACHE_SIZE 16
20 #define bpf_rcu_lock_held() \
21 (rcu_read_lock_held() || rcu_read_lock_trace_held() || \
22 rcu_read_lock_bh_held())
23 struct bpf_local_storage_map_bucket {
24 struct hlist_head list;
28 /* Thp map is not the primary owner of a bpf_local_storage_elem.
29 * Instead, the container object (eg. sk->sk_bpf_storage) is.
31 * The map (bpf_local_storage_map) is for two purposes
32 * 1. Define the size of the "local storage". It is
33 * the map's value_size.
35 * 2. Maintain a list to keep track of all elems such
36 * that they can be cleaned up during the map destruction.
38 * When a bpf local storage is being looked up for a
39 * particular object, the "bpf_map" pointer is actually used
40 * as the "key" to search in the list of elem in
41 * the respective bpf_local_storage owned by the object.
43 * e.g. sk->sk_bpf_storage is the mini-map with the "bpf_map" pointer
44 * as the searching key.
46 struct bpf_local_storage_map {
48 /* Lookup elem does not require accessing the map.
50 * Updating/Deleting requires a bucket lock to
51 * link/unlink the elem from the map. Having
52 * multiple buckets to improve contention.
54 struct bpf_local_storage_map_bucket *buckets;
60 struct bpf_local_storage_data {
61 /* smap is used as the searching key when looking up
62 * from the object's bpf_local_storage.
64 * Put it in the same cacheline as the data to minimize
65 * the number of cachelines accessed during the cache hit case.
67 struct bpf_local_storage_map __rcu *smap;
68 u8 data[] __aligned(8);
71 /* Linked to bpf_local_storage and bpf_local_storage_map */
72 struct bpf_local_storage_elem {
73 struct hlist_node map_node; /* Linked to bpf_local_storage_map */
74 struct hlist_node snode; /* Linked to bpf_local_storage */
75 struct bpf_local_storage __rcu *local_storage;
78 /* The data is stored in another cacheline to minimize
79 * the number of cachelines access during a cache hit.
81 struct bpf_local_storage_data sdata ____cacheline_aligned;
84 struct bpf_local_storage {
85 struct bpf_local_storage_data __rcu *cache[BPF_LOCAL_STORAGE_CACHE_SIZE];
86 struct bpf_local_storage_map __rcu *smap;
87 struct hlist_head list; /* List of bpf_local_storage_elem */
88 void *owner; /* The object that owns the above "list" of
89 * bpf_local_storage_elem.
92 raw_spinlock_t lock; /* Protect adding/removing from the "list" */
95 /* U16_MAX is much more than enough for sk local storage
96 * considering a tcp_sock is ~2k.
98 #define BPF_LOCAL_STORAGE_MAX_VALUE_SIZE \
100 (KMALLOC_MAX_SIZE - MAX_BPF_STACK - \
101 sizeof(struct bpf_local_storage_elem)), \
102 (U16_MAX - sizeof(struct bpf_local_storage_elem)))
104 #define SELEM(_SDATA) \
105 container_of((_SDATA), struct bpf_local_storage_elem, sdata)
106 #define SDATA(_SELEM) (&(_SELEM)->sdata)
108 #define BPF_LOCAL_STORAGE_CACHE_SIZE 16
110 struct bpf_local_storage_cache {
112 u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE];
115 #define DEFINE_BPF_STORAGE_CACHE(name) \
116 static struct bpf_local_storage_cache name = { \
117 .idx_lock = __SPIN_LOCK_UNLOCKED(name.idx_lock), \
120 /* Helper functions for bpf_local_storage */
121 int bpf_local_storage_map_alloc_check(union bpf_attr *attr);
124 bpf_local_storage_map_alloc(union bpf_attr *attr,
125 struct bpf_local_storage_cache *cache);
127 struct bpf_local_storage_data *
128 bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
129 struct bpf_local_storage_map *smap,
130 bool cacheit_lockit);
132 void bpf_local_storage_destroy(struct bpf_local_storage *local_storage);
134 void bpf_local_storage_map_free(struct bpf_map *map,
135 struct bpf_local_storage_cache *cache,
136 int __percpu *busy_counter);
138 int bpf_local_storage_map_check_btf(const struct bpf_map *map,
139 const struct btf *btf,
140 const struct btf_type *key_type,
141 const struct btf_type *value_type);
143 void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
144 struct bpf_local_storage_elem *selem);
146 void bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool reuse_now);
148 void bpf_selem_link_map(struct bpf_local_storage_map *smap,
149 struct bpf_local_storage_elem *selem);
151 struct bpf_local_storage_elem *
152 bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,
153 bool charge_mem, gfp_t gfp_flags);
155 void bpf_selem_free(struct bpf_local_storage_elem *selem,
156 struct bpf_local_storage_map *smap,
160 bpf_local_storage_alloc(void *owner,
161 struct bpf_local_storage_map *smap,
162 struct bpf_local_storage_elem *first_selem,
165 struct bpf_local_storage_data *
166 bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
167 void *value, u64 map_flags, gfp_t gfp_flags);
169 u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map);
171 #endif /* _BPF_LOCAL_STORAGE_H */