xref: /linux/include/linux/bpf_local_storage.h (revision 9e4e86a604dfd06402933467578c4b79f5412b2c)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2019 Facebook
4  * Copyright 2020 Google LLC.
5  */
6 
7 #ifndef _BPF_LOCAL_STORAGE_H
8 #define _BPF_LOCAL_STORAGE_H
9 
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 <linux/bpf_mem_alloc.h>
17 #include <uapi/linux/btf.h>
18 #include <asm/rqspinlock.h>
19 
20 #define BPF_LOCAL_STORAGE_CACHE_SIZE	16
21 
22 struct bpf_local_storage_map_bucket {
23 	struct hlist_head list;
24 	rqspinlock_t lock;
25 };
26 
27 /* Thp map is not the primary owner of a bpf_local_storage_elem.
28  * Instead, the container object (eg. sk->sk_bpf_storage) is.
29  *
30  * The map (bpf_local_storage_map) is for two purposes
31  * 1. Define the size of the "local storage".  It is
32  *    the map's value_size.
33  *
34  * 2. Maintain a list to keep track of all elems such
35  *    that they can be cleaned up during the map destruction.
36  *
37  * When a bpf local storage is being looked up for a
38  * particular object,  the "bpf_map" pointer is actually used
39  * as the "key" to search in the list of elem in
40  * the respective bpf_local_storage owned by the object.
41  *
42  * e.g. sk->sk_bpf_storage is the mini-map with the "bpf_map" pointer
43  * as the searching key.
44  */
45 struct bpf_local_storage_map {
46 	struct bpf_map map;
47 	/* Lookup elem does not require accessing the map.
48 	 *
49 	 * Updating/Deleting requires a bucket lock to
50 	 * link/unlink the elem from the map.  Having
51 	 * multiple buckets to improve contention.
52 	 */
53 	struct bpf_local_storage_map_bucket *buckets;
54 	u32 bucket_log;
55 	u16 elem_size;
56 	u16 cache_idx;
57 };
58 
59 struct bpf_local_storage_data {
60 	/* smap is used as the searching key when looking up
61 	 * from the object's bpf_local_storage.
62 	 *
63 	 * Put it in the same cacheline as the data to minimize
64 	 * the number of cachelines accessed during the cache hit case.
65 	 */
66 	struct bpf_local_storage_map __rcu *smap;
67 	u8 data[] __aligned(8);
68 };
69 
70 #define SELEM_MAP_UNLINKED	(1 << 0)
71 #define SELEM_STORAGE_UNLINKED	(1 << 1)
72 #define SELEM_UNLINKED		(SELEM_MAP_UNLINKED | SELEM_STORAGE_UNLINKED)
73 #define SELEM_TOFREE		(1 << 2)
74 
75 /* Linked to bpf_local_storage and bpf_local_storage_map */
76 struct bpf_local_storage_elem {
77 	struct hlist_node map_node;	/* Linked to bpf_local_storage_map */
78 	struct hlist_node snode;	/* Linked to bpf_local_storage */
79 	struct bpf_local_storage __rcu *local_storage;
80 	union {
81 		struct rcu_head rcu;
82 		struct hlist_node free_node;	/* used to postpone
83 						 * bpf_selem_free
84 						 * after raw_spin_unlock
85 						 */
86 	};
87 	atomic_t state;
88 	/* 4 bytes hole */
89 	/* The data is stored in another cacheline to minimize
90 	 * the number of cachelines access during a cache hit.
91 	 */
92 	struct bpf_local_storage_data sdata ____cacheline_aligned;
93 };
94 
95 struct bpf_local_storage {
96 	struct bpf_local_storage_data __rcu *cache[BPF_LOCAL_STORAGE_CACHE_SIZE];
97 	struct hlist_head list; /* List of bpf_local_storage_elem */
98 	void *owner;		/* The object that owns the above "list" of
99 				 * bpf_local_storage_elem.
100 				 */
101 	struct rcu_head rcu;
102 	rqspinlock_t lock;	/* Protect adding/removing from the "list" */
103 	u64 mem_charge;		/* Copy of mem charged to owner. Protected by "lock" */
104 	refcount_t owner_refcnt;/* Used to pin owner when map_free is uncharging */
105 };
106 
107 /* U16_MAX is much more than enough for sk local storage
108  * considering a tcp_sock is ~2k.
109  */
110 #define BPF_LOCAL_STORAGE_MAX_VALUE_SIZE				       \
111 	min_t(u32,                                                             \
112 	      (KMALLOC_MAX_SIZE - MAX_BPF_STACK -                              \
113 	       sizeof(struct bpf_local_storage_elem)),                         \
114 	      (U16_MAX - sizeof(struct bpf_local_storage_elem)))
115 
116 #define SELEM(_SDATA)                                                          \
117 	container_of((_SDATA), struct bpf_local_storage_elem, sdata)
118 #define SDATA(_SELEM) (&(_SELEM)->sdata)
119 
120 #define BPF_LOCAL_STORAGE_CACHE_SIZE	16
121 
122 struct bpf_local_storage_cache {
123 	spinlock_t idx_lock;
124 	u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE];
125 };
126 
127 #define DEFINE_BPF_STORAGE_CACHE(name)				\
128 static struct bpf_local_storage_cache name = {			\
129 	.idx_lock = __SPIN_LOCK_UNLOCKED(name.idx_lock),	\
130 }
131 
132 /* Helper functions for bpf_local_storage */
133 int bpf_local_storage_map_alloc_check(union bpf_attr *attr);
134 
135 struct bpf_map *
136 bpf_local_storage_map_alloc(union bpf_attr *attr,
137 			    struct bpf_local_storage_cache *cache);
138 
139 void __bpf_local_storage_insert_cache(struct bpf_local_storage *local_storage,
140 				      struct bpf_local_storage_map *smap,
141 				      struct bpf_local_storage_elem *selem);
142 /* If cacheit_lockit is false, this lookup function is lockless */
143 static inline struct bpf_local_storage_data *
bpf_local_storage_lookup(struct bpf_local_storage * local_storage,struct bpf_local_storage_map * smap,bool cacheit_lockit)144 bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
145 			 struct bpf_local_storage_map *smap,
146 			 bool cacheit_lockit)
147 {
148 	struct bpf_local_storage_data *sdata;
149 	struct bpf_local_storage_elem *selem;
150 
151 	/* Fast path (cache hit) */
152 	sdata = rcu_dereference_check(local_storage->cache[smap->cache_idx],
153 				      bpf_rcu_lock_held());
154 	if (sdata && rcu_access_pointer(sdata->smap) == smap)
155 		return sdata;
156 
157 	/* Slow path (cache miss) */
158 	hlist_for_each_entry_rcu(selem, &local_storage->list, snode,
159 				  rcu_read_lock_trace_held())
160 		if (rcu_access_pointer(SDATA(selem)->smap) == smap)
161 			break;
162 
163 	if (!selem)
164 		return NULL;
165 	if (cacheit_lockit)
166 		__bpf_local_storage_insert_cache(local_storage, smap, selem);
167 	return SDATA(selem);
168 }
169 
170 u32 bpf_local_storage_destroy(struct bpf_local_storage *local_storage);
171 
172 void bpf_local_storage_map_free(struct bpf_map *map,
173 				struct bpf_local_storage_cache *cache);
174 
175 int bpf_local_storage_map_check_btf(struct bpf_map *map,
176 				    const struct btf *btf,
177 				    const struct btf_type *key_type,
178 				    const struct btf_type *value_type);
179 
180 void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
181 				   struct bpf_local_storage_elem *selem);
182 
183 int bpf_selem_unlink(struct bpf_local_storage_elem *selem);
184 
185 int bpf_selem_link_map(struct bpf_local_storage_map *smap,
186 		       struct bpf_local_storage *local_storage,
187 		       struct bpf_local_storage_elem *selem);
188 
189 struct bpf_local_storage_elem *
190 bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,
191 		bool swap_uptrs);
192 
193 void bpf_selem_free(struct bpf_local_storage_elem *selem,
194 		    bool reuse_now);
195 
196 int
197 bpf_local_storage_alloc(void *owner,
198 			struct bpf_local_storage_map *smap,
199 			struct bpf_local_storage_elem *first_selem);
200 
201 struct bpf_local_storage_data *
202 bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
203 			 void *value, u64 map_flags, bool swap_uptrs);
204 
205 u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map);
206 
207 #endif /* _BPF_LOCAL_STORAGE_H */
208