xref: /openbmc/linux/kernel/bpf/bpf_local_storage.c (revision 450af8d0f6be2e7dd2a528a3fb054bb726bf1747)
1*450af8d0SKP Singh // SPDX-License-Identifier: GPL-2.0
2*450af8d0SKP Singh /* Copyright (c) 2019 Facebook  */
3*450af8d0SKP Singh #include <linux/rculist.h>
4*450af8d0SKP Singh #include <linux/list.h>
5*450af8d0SKP Singh #include <linux/hash.h>
6*450af8d0SKP Singh #include <linux/types.h>
7*450af8d0SKP Singh #include <linux/spinlock.h>
8*450af8d0SKP Singh #include <linux/bpf.h>
9*450af8d0SKP Singh #include <linux/btf_ids.h>
10*450af8d0SKP Singh #include <linux/bpf_local_storage.h>
11*450af8d0SKP Singh #include <net/sock.h>
12*450af8d0SKP Singh #include <uapi/linux/sock_diag.h>
13*450af8d0SKP Singh #include <uapi/linux/btf.h>
14*450af8d0SKP Singh 
15*450af8d0SKP Singh #define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE)
16*450af8d0SKP Singh 
17*450af8d0SKP Singh static struct bpf_local_storage_map_bucket *
18*450af8d0SKP Singh select_bucket(struct bpf_local_storage_map *smap,
19*450af8d0SKP Singh 	      struct bpf_local_storage_elem *selem)
20*450af8d0SKP Singh {
21*450af8d0SKP Singh 	return &smap->buckets[hash_ptr(selem, smap->bucket_log)];
22*450af8d0SKP Singh }
23*450af8d0SKP Singh 
24*450af8d0SKP Singh static int mem_charge(struct bpf_local_storage_map *smap, void *owner, u32 size)
25*450af8d0SKP Singh {
26*450af8d0SKP Singh 	struct bpf_map *map = &smap->map;
27*450af8d0SKP Singh 
28*450af8d0SKP Singh 	if (!map->ops->map_local_storage_charge)
29*450af8d0SKP Singh 		return 0;
30*450af8d0SKP Singh 
31*450af8d0SKP Singh 	return map->ops->map_local_storage_charge(smap, owner, size);
32*450af8d0SKP Singh }
33*450af8d0SKP Singh 
34*450af8d0SKP Singh static void mem_uncharge(struct bpf_local_storage_map *smap, void *owner,
35*450af8d0SKP Singh 			 u32 size)
36*450af8d0SKP Singh {
37*450af8d0SKP Singh 	struct bpf_map *map = &smap->map;
38*450af8d0SKP Singh 
39*450af8d0SKP Singh 	if (map->ops->map_local_storage_uncharge)
40*450af8d0SKP Singh 		map->ops->map_local_storage_uncharge(smap, owner, size);
41*450af8d0SKP Singh }
42*450af8d0SKP Singh 
43*450af8d0SKP Singh static struct bpf_local_storage __rcu **
44*450af8d0SKP Singh owner_storage(struct bpf_local_storage_map *smap, void *owner)
45*450af8d0SKP Singh {
46*450af8d0SKP Singh 	struct bpf_map *map = &smap->map;
47*450af8d0SKP Singh 
48*450af8d0SKP Singh 	return map->ops->map_owner_storage_ptr(owner);
49*450af8d0SKP Singh }
50*450af8d0SKP Singh 
51*450af8d0SKP Singh static bool selem_linked_to_storage(const struct bpf_local_storage_elem *selem)
52*450af8d0SKP Singh {
53*450af8d0SKP Singh 	return !hlist_unhashed(&selem->snode);
54*450af8d0SKP Singh }
55*450af8d0SKP Singh 
56*450af8d0SKP Singh static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem)
57*450af8d0SKP Singh {
58*450af8d0SKP Singh 	return !hlist_unhashed(&selem->map_node);
59*450af8d0SKP Singh }
60*450af8d0SKP Singh 
61*450af8d0SKP Singh struct bpf_local_storage_elem *
62*450af8d0SKP Singh bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
63*450af8d0SKP Singh 		void *value, bool charge_mem)
64*450af8d0SKP Singh {
65*450af8d0SKP Singh 	struct bpf_local_storage_elem *selem;
66*450af8d0SKP Singh 
67*450af8d0SKP Singh 	if (charge_mem && mem_charge(smap, owner, smap->elem_size))
68*450af8d0SKP Singh 		return NULL;
69*450af8d0SKP Singh 
70*450af8d0SKP Singh 	selem = kzalloc(smap->elem_size, GFP_ATOMIC | __GFP_NOWARN);
71*450af8d0SKP Singh 	if (selem) {
72*450af8d0SKP Singh 		if (value)
73*450af8d0SKP Singh 			memcpy(SDATA(selem)->data, value, smap->map.value_size);
74*450af8d0SKP Singh 		return selem;
75*450af8d0SKP Singh 	}
76*450af8d0SKP Singh 
77*450af8d0SKP Singh 	if (charge_mem)
78*450af8d0SKP Singh 		mem_uncharge(smap, owner, smap->elem_size);
79*450af8d0SKP Singh 
80*450af8d0SKP Singh 	return NULL;
81*450af8d0SKP Singh }
82*450af8d0SKP Singh 
83*450af8d0SKP Singh /* local_storage->lock must be held and selem->local_storage == local_storage.
84*450af8d0SKP Singh  * The caller must ensure selem->smap is still valid to be
85*450af8d0SKP Singh  * dereferenced for its smap->elem_size and smap->cache_idx.
86*450af8d0SKP Singh  */
87*450af8d0SKP Singh bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage,
88*450af8d0SKP Singh 				     struct bpf_local_storage_elem *selem,
89*450af8d0SKP Singh 				     bool uncharge_mem)
90*450af8d0SKP Singh {
91*450af8d0SKP Singh 	struct bpf_local_storage_map *smap;
92*450af8d0SKP Singh 	bool free_local_storage;
93*450af8d0SKP Singh 	void *owner;
94*450af8d0SKP Singh 
95*450af8d0SKP Singh 	smap = rcu_dereference(SDATA(selem)->smap);
96*450af8d0SKP Singh 	owner = local_storage->owner;
97*450af8d0SKP Singh 
98*450af8d0SKP Singh 	/* All uncharging on the owner must be done first.
99*450af8d0SKP Singh 	 * The owner may be freed once the last selem is unlinked
100*450af8d0SKP Singh 	 * from local_storage.
101*450af8d0SKP Singh 	 */
102*450af8d0SKP Singh 	if (uncharge_mem)
103*450af8d0SKP Singh 		mem_uncharge(smap, owner, smap->elem_size);
104*450af8d0SKP Singh 
105*450af8d0SKP Singh 	free_local_storage = hlist_is_singular_node(&selem->snode,
106*450af8d0SKP Singh 						    &local_storage->list);
107*450af8d0SKP Singh 	if (free_local_storage) {
108*450af8d0SKP Singh 		mem_uncharge(smap, owner, sizeof(struct bpf_local_storage));
109*450af8d0SKP Singh 		local_storage->owner = NULL;
110*450af8d0SKP Singh 
111*450af8d0SKP Singh 		/* After this RCU_INIT, owner may be freed and cannot be used */
112*450af8d0SKP Singh 		RCU_INIT_POINTER(*owner_storage(smap, owner), NULL);
113*450af8d0SKP Singh 
114*450af8d0SKP Singh 		/* local_storage is not freed now.  local_storage->lock is
115*450af8d0SKP Singh 		 * still held and raw_spin_unlock_bh(&local_storage->lock)
116*450af8d0SKP Singh 		 * will be done by the caller.
117*450af8d0SKP Singh 		 *
118*450af8d0SKP Singh 		 * Although the unlock will be done under
119*450af8d0SKP Singh 		 * rcu_read_lock(),  it is more intutivie to
120*450af8d0SKP Singh 		 * read if kfree_rcu(local_storage, rcu) is done
121*450af8d0SKP Singh 		 * after the raw_spin_unlock_bh(&local_storage->lock).
122*450af8d0SKP Singh 		 *
123*450af8d0SKP Singh 		 * Hence, a "bool free_local_storage" is returned
124*450af8d0SKP Singh 		 * to the caller which then calls the kfree_rcu()
125*450af8d0SKP Singh 		 * after unlock.
126*450af8d0SKP Singh 		 */
127*450af8d0SKP Singh 	}
128*450af8d0SKP Singh 	hlist_del_init_rcu(&selem->snode);
129*450af8d0SKP Singh 	if (rcu_access_pointer(local_storage->cache[smap->cache_idx]) ==
130*450af8d0SKP Singh 	    SDATA(selem))
131*450af8d0SKP Singh 		RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL);
132*450af8d0SKP Singh 
133*450af8d0SKP Singh 	kfree_rcu(selem, rcu);
134*450af8d0SKP Singh 
135*450af8d0SKP Singh 	return free_local_storage;
136*450af8d0SKP Singh }
137*450af8d0SKP Singh 
138*450af8d0SKP Singh static void __bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem)
139*450af8d0SKP Singh {
140*450af8d0SKP Singh 	struct bpf_local_storage *local_storage;
141*450af8d0SKP Singh 	bool free_local_storage = false;
142*450af8d0SKP Singh 
143*450af8d0SKP Singh 	if (unlikely(!selem_linked_to_storage(selem)))
144*450af8d0SKP Singh 		/* selem has already been unlinked from sk */
145*450af8d0SKP Singh 		return;
146*450af8d0SKP Singh 
147*450af8d0SKP Singh 	local_storage = rcu_dereference(selem->local_storage);
148*450af8d0SKP Singh 	raw_spin_lock_bh(&local_storage->lock);
149*450af8d0SKP Singh 	if (likely(selem_linked_to_storage(selem)))
150*450af8d0SKP Singh 		free_local_storage = bpf_selem_unlink_storage_nolock(
151*450af8d0SKP Singh 			local_storage, selem, true);
152*450af8d0SKP Singh 	raw_spin_unlock_bh(&local_storage->lock);
153*450af8d0SKP Singh 
154*450af8d0SKP Singh 	if (free_local_storage)
155*450af8d0SKP Singh 		kfree_rcu(local_storage, rcu);
156*450af8d0SKP Singh }
157*450af8d0SKP Singh 
158*450af8d0SKP Singh void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
159*450af8d0SKP Singh 				   struct bpf_local_storage_elem *selem)
160*450af8d0SKP Singh {
161*450af8d0SKP Singh 	RCU_INIT_POINTER(selem->local_storage, local_storage);
162*450af8d0SKP Singh 	hlist_add_head(&selem->snode, &local_storage->list);
163*450af8d0SKP Singh }
164*450af8d0SKP Singh 
165*450af8d0SKP Singh void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
166*450af8d0SKP Singh {
167*450af8d0SKP Singh 	struct bpf_local_storage_map *smap;
168*450af8d0SKP Singh 	struct bpf_local_storage_map_bucket *b;
169*450af8d0SKP Singh 
170*450af8d0SKP Singh 	if (unlikely(!selem_linked_to_map(selem)))
171*450af8d0SKP Singh 		/* selem has already be unlinked from smap */
172*450af8d0SKP Singh 		return;
173*450af8d0SKP Singh 
174*450af8d0SKP Singh 	smap = rcu_dereference(SDATA(selem)->smap);
175*450af8d0SKP Singh 	b = select_bucket(smap, selem);
176*450af8d0SKP Singh 	raw_spin_lock_bh(&b->lock);
177*450af8d0SKP Singh 	if (likely(selem_linked_to_map(selem)))
178*450af8d0SKP Singh 		hlist_del_init_rcu(&selem->map_node);
179*450af8d0SKP Singh 	raw_spin_unlock_bh(&b->lock);
180*450af8d0SKP Singh }
181*450af8d0SKP Singh 
182*450af8d0SKP Singh void bpf_selem_link_map(struct bpf_local_storage_map *smap,
183*450af8d0SKP Singh 			struct bpf_local_storage_elem *selem)
184*450af8d0SKP Singh {
185*450af8d0SKP Singh 	struct bpf_local_storage_map_bucket *b = select_bucket(smap, selem);
186*450af8d0SKP Singh 
187*450af8d0SKP Singh 	raw_spin_lock_bh(&b->lock);
188*450af8d0SKP Singh 	RCU_INIT_POINTER(SDATA(selem)->smap, smap);
189*450af8d0SKP Singh 	hlist_add_head_rcu(&selem->map_node, &b->list);
190*450af8d0SKP Singh 	raw_spin_unlock_bh(&b->lock);
191*450af8d0SKP Singh }
192*450af8d0SKP Singh 
193*450af8d0SKP Singh void bpf_selem_unlink(struct bpf_local_storage_elem *selem)
194*450af8d0SKP Singh {
195*450af8d0SKP Singh 	/* Always unlink from map before unlinking from local_storage
196*450af8d0SKP Singh 	 * because selem will be freed after successfully unlinked from
197*450af8d0SKP Singh 	 * the local_storage.
198*450af8d0SKP Singh 	 */
199*450af8d0SKP Singh 	bpf_selem_unlink_map(selem);
200*450af8d0SKP Singh 	__bpf_selem_unlink_storage(selem);
201*450af8d0SKP Singh }
202*450af8d0SKP Singh 
203*450af8d0SKP Singh struct bpf_local_storage_data *
204*450af8d0SKP Singh bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
205*450af8d0SKP Singh 			 struct bpf_local_storage_map *smap,
206*450af8d0SKP Singh 			 bool cacheit_lockit)
207*450af8d0SKP Singh {
208*450af8d0SKP Singh 	struct bpf_local_storage_data *sdata;
209*450af8d0SKP Singh 	struct bpf_local_storage_elem *selem;
210*450af8d0SKP Singh 
211*450af8d0SKP Singh 	/* Fast path (cache hit) */
212*450af8d0SKP Singh 	sdata = rcu_dereference(local_storage->cache[smap->cache_idx]);
213*450af8d0SKP Singh 	if (sdata && rcu_access_pointer(sdata->smap) == smap)
214*450af8d0SKP Singh 		return sdata;
215*450af8d0SKP Singh 
216*450af8d0SKP Singh 	/* Slow path (cache miss) */
217*450af8d0SKP Singh 	hlist_for_each_entry_rcu(selem, &local_storage->list, snode)
218*450af8d0SKP Singh 		if (rcu_access_pointer(SDATA(selem)->smap) == smap)
219*450af8d0SKP Singh 			break;
220*450af8d0SKP Singh 
221*450af8d0SKP Singh 	if (!selem)
222*450af8d0SKP Singh 		return NULL;
223*450af8d0SKP Singh 
224*450af8d0SKP Singh 	sdata = SDATA(selem);
225*450af8d0SKP Singh 	if (cacheit_lockit) {
226*450af8d0SKP Singh 		/* spinlock is needed to avoid racing with the
227*450af8d0SKP Singh 		 * parallel delete.  Otherwise, publishing an already
228*450af8d0SKP Singh 		 * deleted sdata to the cache will become a use-after-free
229*450af8d0SKP Singh 		 * problem in the next bpf_local_storage_lookup().
230*450af8d0SKP Singh 		 */
231*450af8d0SKP Singh 		raw_spin_lock_bh(&local_storage->lock);
232*450af8d0SKP Singh 		if (selem_linked_to_storage(selem))
233*450af8d0SKP Singh 			rcu_assign_pointer(local_storage->cache[smap->cache_idx],
234*450af8d0SKP Singh 					   sdata);
235*450af8d0SKP Singh 		raw_spin_unlock_bh(&local_storage->lock);
236*450af8d0SKP Singh 	}
237*450af8d0SKP Singh 
238*450af8d0SKP Singh 	return sdata;
239*450af8d0SKP Singh }
240*450af8d0SKP Singh 
241*450af8d0SKP Singh static int check_flags(const struct bpf_local_storage_data *old_sdata,
242*450af8d0SKP Singh 		       u64 map_flags)
243*450af8d0SKP Singh {
244*450af8d0SKP Singh 	if (old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
245*450af8d0SKP Singh 		/* elem already exists */
246*450af8d0SKP Singh 		return -EEXIST;
247*450af8d0SKP Singh 
248*450af8d0SKP Singh 	if (!old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
249*450af8d0SKP Singh 		/* elem doesn't exist, cannot update it */
250*450af8d0SKP Singh 		return -ENOENT;
251*450af8d0SKP Singh 
252*450af8d0SKP Singh 	return 0;
253*450af8d0SKP Singh }
254*450af8d0SKP Singh 
255*450af8d0SKP Singh int bpf_local_storage_alloc(void *owner,
256*450af8d0SKP Singh 			    struct bpf_local_storage_map *smap,
257*450af8d0SKP Singh 			    struct bpf_local_storage_elem *first_selem)
258*450af8d0SKP Singh {
259*450af8d0SKP Singh 	struct bpf_local_storage *prev_storage, *storage;
260*450af8d0SKP Singh 	struct bpf_local_storage **owner_storage_ptr;
261*450af8d0SKP Singh 	int err;
262*450af8d0SKP Singh 
263*450af8d0SKP Singh 	err = mem_charge(smap, owner, sizeof(*storage));
264*450af8d0SKP Singh 	if (err)
265*450af8d0SKP Singh 		return err;
266*450af8d0SKP Singh 
267*450af8d0SKP Singh 	storage = kzalloc(sizeof(*storage), GFP_ATOMIC | __GFP_NOWARN);
268*450af8d0SKP Singh 	if (!storage) {
269*450af8d0SKP Singh 		err = -ENOMEM;
270*450af8d0SKP Singh 		goto uncharge;
271*450af8d0SKP Singh 	}
272*450af8d0SKP Singh 
273*450af8d0SKP Singh 	INIT_HLIST_HEAD(&storage->list);
274*450af8d0SKP Singh 	raw_spin_lock_init(&storage->lock);
275*450af8d0SKP Singh 	storage->owner = owner;
276*450af8d0SKP Singh 
277*450af8d0SKP Singh 	bpf_selem_link_storage_nolock(storage, first_selem);
278*450af8d0SKP Singh 	bpf_selem_link_map(smap, first_selem);
279*450af8d0SKP Singh 
280*450af8d0SKP Singh 	owner_storage_ptr =
281*450af8d0SKP Singh 		(struct bpf_local_storage **)owner_storage(smap, owner);
282*450af8d0SKP Singh 	/* Publish storage to the owner.
283*450af8d0SKP Singh 	 * Instead of using any lock of the kernel object (i.e. owner),
284*450af8d0SKP Singh 	 * cmpxchg will work with any kernel object regardless what
285*450af8d0SKP Singh 	 * the running context is, bh, irq...etc.
286*450af8d0SKP Singh 	 *
287*450af8d0SKP Singh 	 * From now on, the owner->storage pointer (e.g. sk->sk_bpf_storage)
288*450af8d0SKP Singh 	 * is protected by the storage->lock.  Hence, when freeing
289*450af8d0SKP Singh 	 * the owner->storage, the storage->lock must be held before
290*450af8d0SKP Singh 	 * setting owner->storage ptr to NULL.
291*450af8d0SKP Singh 	 */
292*450af8d0SKP Singh 	prev_storage = cmpxchg(owner_storage_ptr, NULL, storage);
293*450af8d0SKP Singh 	if (unlikely(prev_storage)) {
294*450af8d0SKP Singh 		bpf_selem_unlink_map(first_selem);
295*450af8d0SKP Singh 		err = -EAGAIN;
296*450af8d0SKP Singh 		goto uncharge;
297*450af8d0SKP Singh 
298*450af8d0SKP Singh 		/* Note that even first_selem was linked to smap's
299*450af8d0SKP Singh 		 * bucket->list, first_selem can be freed immediately
300*450af8d0SKP Singh 		 * (instead of kfree_rcu) because
301*450af8d0SKP Singh 		 * bpf_local_storage_map_free() does a
302*450af8d0SKP Singh 		 * synchronize_rcu() before walking the bucket->list.
303*450af8d0SKP Singh 		 * Hence, no one is accessing selem from the
304*450af8d0SKP Singh 		 * bucket->list under rcu_read_lock().
305*450af8d0SKP Singh 		 */
306*450af8d0SKP Singh 	}
307*450af8d0SKP Singh 
308*450af8d0SKP Singh 	return 0;
309*450af8d0SKP Singh 
310*450af8d0SKP Singh uncharge:
311*450af8d0SKP Singh 	kfree(storage);
312*450af8d0SKP Singh 	mem_uncharge(smap, owner, sizeof(*storage));
313*450af8d0SKP Singh 	return err;
314*450af8d0SKP Singh }
315*450af8d0SKP Singh 
316*450af8d0SKP Singh /* sk cannot be going away because it is linking new elem
317*450af8d0SKP Singh  * to sk->sk_bpf_storage. (i.e. sk->sk_refcnt cannot be 0).
318*450af8d0SKP Singh  * Otherwise, it will become a leak (and other memory issues
319*450af8d0SKP Singh  * during map destruction).
320*450af8d0SKP Singh  */
321*450af8d0SKP Singh struct bpf_local_storage_data *
322*450af8d0SKP Singh bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
323*450af8d0SKP Singh 			 void *value, u64 map_flags)
324*450af8d0SKP Singh {
325*450af8d0SKP Singh 	struct bpf_local_storage_data *old_sdata = NULL;
326*450af8d0SKP Singh 	struct bpf_local_storage_elem *selem;
327*450af8d0SKP Singh 	struct bpf_local_storage *local_storage;
328*450af8d0SKP Singh 	int err;
329*450af8d0SKP Singh 
330*450af8d0SKP Singh 	/* BPF_EXIST and BPF_NOEXIST cannot be both set */
331*450af8d0SKP Singh 	if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST) ||
332*450af8d0SKP Singh 	    /* BPF_F_LOCK can only be used in a value with spin_lock */
333*450af8d0SKP Singh 	    unlikely((map_flags & BPF_F_LOCK) &&
334*450af8d0SKP Singh 		     !map_value_has_spin_lock(&smap->map)))
335*450af8d0SKP Singh 		return ERR_PTR(-EINVAL);
336*450af8d0SKP Singh 
337*450af8d0SKP Singh 	local_storage = rcu_dereference(*owner_storage(smap, owner));
338*450af8d0SKP Singh 	if (!local_storage || hlist_empty(&local_storage->list)) {
339*450af8d0SKP Singh 		/* Very first elem for the owner */
340*450af8d0SKP Singh 		err = check_flags(NULL, map_flags);
341*450af8d0SKP Singh 		if (err)
342*450af8d0SKP Singh 			return ERR_PTR(err);
343*450af8d0SKP Singh 
344*450af8d0SKP Singh 		selem = bpf_selem_alloc(smap, owner, value, true);
345*450af8d0SKP Singh 		if (!selem)
346*450af8d0SKP Singh 			return ERR_PTR(-ENOMEM);
347*450af8d0SKP Singh 
348*450af8d0SKP Singh 		err = bpf_local_storage_alloc(owner, smap, selem);
349*450af8d0SKP Singh 		if (err) {
350*450af8d0SKP Singh 			kfree(selem);
351*450af8d0SKP Singh 			mem_uncharge(smap, owner, smap->elem_size);
352*450af8d0SKP Singh 			return ERR_PTR(err);
353*450af8d0SKP Singh 		}
354*450af8d0SKP Singh 
355*450af8d0SKP Singh 		return SDATA(selem);
356*450af8d0SKP Singh 	}
357*450af8d0SKP Singh 
358*450af8d0SKP Singh 	if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) {
359*450af8d0SKP Singh 		/* Hoping to find an old_sdata to do inline update
360*450af8d0SKP Singh 		 * such that it can avoid taking the local_storage->lock
361*450af8d0SKP Singh 		 * and changing the lists.
362*450af8d0SKP Singh 		 */
363*450af8d0SKP Singh 		old_sdata =
364*450af8d0SKP Singh 			bpf_local_storage_lookup(local_storage, smap, false);
365*450af8d0SKP Singh 		err = check_flags(old_sdata, map_flags);
366*450af8d0SKP Singh 		if (err)
367*450af8d0SKP Singh 			return ERR_PTR(err);
368*450af8d0SKP Singh 		if (old_sdata && selem_linked_to_storage(SELEM(old_sdata))) {
369*450af8d0SKP Singh 			copy_map_value_locked(&smap->map, old_sdata->data,
370*450af8d0SKP Singh 					      value, false);
371*450af8d0SKP Singh 			return old_sdata;
372*450af8d0SKP Singh 		}
373*450af8d0SKP Singh 	}
374*450af8d0SKP Singh 
375*450af8d0SKP Singh 	raw_spin_lock_bh(&local_storage->lock);
376*450af8d0SKP Singh 
377*450af8d0SKP Singh 	/* Recheck local_storage->list under local_storage->lock */
378*450af8d0SKP Singh 	if (unlikely(hlist_empty(&local_storage->list))) {
379*450af8d0SKP Singh 		/* A parallel del is happening and local_storage is going
380*450af8d0SKP Singh 		 * away.  It has just been checked before, so very
381*450af8d0SKP Singh 		 * unlikely.  Return instead of retry to keep things
382*450af8d0SKP Singh 		 * simple.
383*450af8d0SKP Singh 		 */
384*450af8d0SKP Singh 		err = -EAGAIN;
385*450af8d0SKP Singh 		goto unlock_err;
386*450af8d0SKP Singh 	}
387*450af8d0SKP Singh 
388*450af8d0SKP Singh 	old_sdata = bpf_local_storage_lookup(local_storage, smap, false);
389*450af8d0SKP Singh 	err = check_flags(old_sdata, map_flags);
390*450af8d0SKP Singh 	if (err)
391*450af8d0SKP Singh 		goto unlock_err;
392*450af8d0SKP Singh 
393*450af8d0SKP Singh 	if (old_sdata && (map_flags & BPF_F_LOCK)) {
394*450af8d0SKP Singh 		copy_map_value_locked(&smap->map, old_sdata->data, value,
395*450af8d0SKP Singh 				      false);
396*450af8d0SKP Singh 		selem = SELEM(old_sdata);
397*450af8d0SKP Singh 		goto unlock;
398*450af8d0SKP Singh 	}
399*450af8d0SKP Singh 
400*450af8d0SKP Singh 	/* local_storage->lock is held.  Hence, we are sure
401*450af8d0SKP Singh 	 * we can unlink and uncharge the old_sdata successfully
402*450af8d0SKP Singh 	 * later.  Hence, instead of charging the new selem now
403*450af8d0SKP Singh 	 * and then uncharge the old selem later (which may cause
404*450af8d0SKP Singh 	 * a potential but unnecessary charge failure),  avoid taking
405*450af8d0SKP Singh 	 * a charge at all here (the "!old_sdata" check) and the
406*450af8d0SKP Singh 	 * old_sdata will not be uncharged later during
407*450af8d0SKP Singh 	 * bpf_selem_unlink_storage_nolock().
408*450af8d0SKP Singh 	 */
409*450af8d0SKP Singh 	selem = bpf_selem_alloc(smap, owner, value, !old_sdata);
410*450af8d0SKP Singh 	if (!selem) {
411*450af8d0SKP Singh 		err = -ENOMEM;
412*450af8d0SKP Singh 		goto unlock_err;
413*450af8d0SKP Singh 	}
414*450af8d0SKP Singh 
415*450af8d0SKP Singh 	/* First, link the new selem to the map */
416*450af8d0SKP Singh 	bpf_selem_link_map(smap, selem);
417*450af8d0SKP Singh 
418*450af8d0SKP Singh 	/* Second, link (and publish) the new selem to local_storage */
419*450af8d0SKP Singh 	bpf_selem_link_storage_nolock(local_storage, selem);
420*450af8d0SKP Singh 
421*450af8d0SKP Singh 	/* Third, remove old selem, SELEM(old_sdata) */
422*450af8d0SKP Singh 	if (old_sdata) {
423*450af8d0SKP Singh 		bpf_selem_unlink_map(SELEM(old_sdata));
424*450af8d0SKP Singh 		bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata),
425*450af8d0SKP Singh 						false);
426*450af8d0SKP Singh 	}
427*450af8d0SKP Singh 
428*450af8d0SKP Singh unlock:
429*450af8d0SKP Singh 	raw_spin_unlock_bh(&local_storage->lock);
430*450af8d0SKP Singh 	return SDATA(selem);
431*450af8d0SKP Singh 
432*450af8d0SKP Singh unlock_err:
433*450af8d0SKP Singh 	raw_spin_unlock_bh(&local_storage->lock);
434*450af8d0SKP Singh 	return ERR_PTR(err);
435*450af8d0SKP Singh }
436*450af8d0SKP Singh 
437*450af8d0SKP Singh u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache)
438*450af8d0SKP Singh {
439*450af8d0SKP Singh 	u64 min_usage = U64_MAX;
440*450af8d0SKP Singh 	u16 i, res = 0;
441*450af8d0SKP Singh 
442*450af8d0SKP Singh 	spin_lock(&cache->idx_lock);
443*450af8d0SKP Singh 
444*450af8d0SKP Singh 	for (i = 0; i < BPF_LOCAL_STORAGE_CACHE_SIZE; i++) {
445*450af8d0SKP Singh 		if (cache->idx_usage_counts[i] < min_usage) {
446*450af8d0SKP Singh 			min_usage = cache->idx_usage_counts[i];
447*450af8d0SKP Singh 			res = i;
448*450af8d0SKP Singh 
449*450af8d0SKP Singh 			/* Found a free cache_idx */
450*450af8d0SKP Singh 			if (!min_usage)
451*450af8d0SKP Singh 				break;
452*450af8d0SKP Singh 		}
453*450af8d0SKP Singh 	}
454*450af8d0SKP Singh 	cache->idx_usage_counts[res]++;
455*450af8d0SKP Singh 
456*450af8d0SKP Singh 	spin_unlock(&cache->idx_lock);
457*450af8d0SKP Singh 
458*450af8d0SKP Singh 	return res;
459*450af8d0SKP Singh }
460*450af8d0SKP Singh 
461*450af8d0SKP Singh void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache,
462*450af8d0SKP Singh 				      u16 idx)
463*450af8d0SKP Singh {
464*450af8d0SKP Singh 	spin_lock(&cache->idx_lock);
465*450af8d0SKP Singh 	cache->idx_usage_counts[idx]--;
466*450af8d0SKP Singh 	spin_unlock(&cache->idx_lock);
467*450af8d0SKP Singh }
468*450af8d0SKP Singh 
469*450af8d0SKP Singh void bpf_local_storage_map_free(struct bpf_local_storage_map *smap)
470*450af8d0SKP Singh {
471*450af8d0SKP Singh 	struct bpf_local_storage_elem *selem;
472*450af8d0SKP Singh 	struct bpf_local_storage_map_bucket *b;
473*450af8d0SKP Singh 	unsigned int i;
474*450af8d0SKP Singh 
475*450af8d0SKP Singh 	/* Note that this map might be concurrently cloned from
476*450af8d0SKP Singh 	 * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone
477*450af8d0SKP Singh 	 * RCU read section to finish before proceeding. New RCU
478*450af8d0SKP Singh 	 * read sections should be prevented via bpf_map_inc_not_zero.
479*450af8d0SKP Singh 	 */
480*450af8d0SKP Singh 	synchronize_rcu();
481*450af8d0SKP Singh 
482*450af8d0SKP Singh 	/* bpf prog and the userspace can no longer access this map
483*450af8d0SKP Singh 	 * now.  No new selem (of this map) can be added
484*450af8d0SKP Singh 	 * to the owner->storage or to the map bucket's list.
485*450af8d0SKP Singh 	 *
486*450af8d0SKP Singh 	 * The elem of this map can be cleaned up here
487*450af8d0SKP Singh 	 * or when the storage is freed e.g.
488*450af8d0SKP Singh 	 * by bpf_sk_storage_free() during __sk_destruct().
489*450af8d0SKP Singh 	 */
490*450af8d0SKP Singh 	for (i = 0; i < (1U << smap->bucket_log); i++) {
491*450af8d0SKP Singh 		b = &smap->buckets[i];
492*450af8d0SKP Singh 
493*450af8d0SKP Singh 		rcu_read_lock();
494*450af8d0SKP Singh 		/* No one is adding to b->list now */
495*450af8d0SKP Singh 		while ((selem = hlist_entry_safe(
496*450af8d0SKP Singh 				rcu_dereference_raw(hlist_first_rcu(&b->list)),
497*450af8d0SKP Singh 				struct bpf_local_storage_elem, map_node))) {
498*450af8d0SKP Singh 			bpf_selem_unlink(selem);
499*450af8d0SKP Singh 			cond_resched_rcu();
500*450af8d0SKP Singh 		}
501*450af8d0SKP Singh 		rcu_read_unlock();
502*450af8d0SKP Singh 	}
503*450af8d0SKP Singh 
504*450af8d0SKP Singh 	/* While freeing the storage we may still need to access the map.
505*450af8d0SKP Singh 	 *
506*450af8d0SKP Singh 	 * e.g. when bpf_sk_storage_free() has unlinked selem from the map
507*450af8d0SKP Singh 	 * which then made the above while((selem = ...)) loop
508*450af8d0SKP Singh 	 * exit immediately.
509*450af8d0SKP Singh 	 *
510*450af8d0SKP Singh 	 * However, while freeing the storage one still needs to access the
511*450af8d0SKP Singh 	 * smap->elem_size to do the uncharging in
512*450af8d0SKP Singh 	 * bpf_selem_unlink_storage_nolock().
513*450af8d0SKP Singh 	 *
514*450af8d0SKP Singh 	 * Hence, wait another rcu grace period for the storage to be freed.
515*450af8d0SKP Singh 	 */
516*450af8d0SKP Singh 	synchronize_rcu();
517*450af8d0SKP Singh 
518*450af8d0SKP Singh 	kvfree(smap->buckets);
519*450af8d0SKP Singh 	kfree(smap);
520*450af8d0SKP Singh }
521*450af8d0SKP Singh 
522*450af8d0SKP Singh int bpf_local_storage_map_alloc_check(union bpf_attr *attr)
523*450af8d0SKP Singh {
524*450af8d0SKP Singh 	if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK ||
525*450af8d0SKP Singh 	    !(attr->map_flags & BPF_F_NO_PREALLOC) ||
526*450af8d0SKP Singh 	    attr->max_entries ||
527*450af8d0SKP Singh 	    attr->key_size != sizeof(int) || !attr->value_size ||
528*450af8d0SKP Singh 	    /* Enforce BTF for userspace sk dumping */
529*450af8d0SKP Singh 	    !attr->btf_key_type_id || !attr->btf_value_type_id)
530*450af8d0SKP Singh 		return -EINVAL;
531*450af8d0SKP Singh 
532*450af8d0SKP Singh 	if (!bpf_capable())
533*450af8d0SKP Singh 		return -EPERM;
534*450af8d0SKP Singh 
535*450af8d0SKP Singh 	if (attr->value_size > BPF_LOCAL_STORAGE_MAX_VALUE_SIZE)
536*450af8d0SKP Singh 		return -E2BIG;
537*450af8d0SKP Singh 
538*450af8d0SKP Singh 	return 0;
539*450af8d0SKP Singh }
540*450af8d0SKP Singh 
541*450af8d0SKP Singh struct bpf_local_storage_map *bpf_local_storage_map_alloc(union bpf_attr *attr)
542*450af8d0SKP Singh {
543*450af8d0SKP Singh 	struct bpf_local_storage_map *smap;
544*450af8d0SKP Singh 	unsigned int i;
545*450af8d0SKP Singh 	u32 nbuckets;
546*450af8d0SKP Singh 	u64 cost;
547*450af8d0SKP Singh 	int ret;
548*450af8d0SKP Singh 
549*450af8d0SKP Singh 	smap = kzalloc(sizeof(*smap), GFP_USER | __GFP_NOWARN);
550*450af8d0SKP Singh 	if (!smap)
551*450af8d0SKP Singh 		return ERR_PTR(-ENOMEM);
552*450af8d0SKP Singh 	bpf_map_init_from_attr(&smap->map, attr);
553*450af8d0SKP Singh 
554*450af8d0SKP Singh 	nbuckets = roundup_pow_of_two(num_possible_cpus());
555*450af8d0SKP Singh 	/* Use at least 2 buckets, select_bucket() is undefined behavior with 1 bucket */
556*450af8d0SKP Singh 	nbuckets = max_t(u32, 2, nbuckets);
557*450af8d0SKP Singh 	smap->bucket_log = ilog2(nbuckets);
558*450af8d0SKP Singh 	cost = sizeof(*smap->buckets) * nbuckets + sizeof(*smap);
559*450af8d0SKP Singh 
560*450af8d0SKP Singh 	ret = bpf_map_charge_init(&smap->map.memory, cost);
561*450af8d0SKP Singh 	if (ret < 0) {
562*450af8d0SKP Singh 		kfree(smap);
563*450af8d0SKP Singh 		return ERR_PTR(ret);
564*450af8d0SKP Singh 	}
565*450af8d0SKP Singh 
566*450af8d0SKP Singh 	smap->buckets = kvcalloc(sizeof(*smap->buckets), nbuckets,
567*450af8d0SKP Singh 				 GFP_USER | __GFP_NOWARN);
568*450af8d0SKP Singh 	if (!smap->buckets) {
569*450af8d0SKP Singh 		bpf_map_charge_finish(&smap->map.memory);
570*450af8d0SKP Singh 		kfree(smap);
571*450af8d0SKP Singh 		return ERR_PTR(-ENOMEM);
572*450af8d0SKP Singh 	}
573*450af8d0SKP Singh 
574*450af8d0SKP Singh 	for (i = 0; i < nbuckets; i++) {
575*450af8d0SKP Singh 		INIT_HLIST_HEAD(&smap->buckets[i].list);
576*450af8d0SKP Singh 		raw_spin_lock_init(&smap->buckets[i].lock);
577*450af8d0SKP Singh 	}
578*450af8d0SKP Singh 
579*450af8d0SKP Singh 	smap->elem_size =
580*450af8d0SKP Singh 		sizeof(struct bpf_local_storage_elem) + attr->value_size;
581*450af8d0SKP Singh 
582*450af8d0SKP Singh 	return smap;
583*450af8d0SKP Singh }
584*450af8d0SKP Singh 
585*450af8d0SKP Singh int bpf_local_storage_map_check_btf(const struct bpf_map *map,
586*450af8d0SKP Singh 				    const struct btf *btf,
587*450af8d0SKP Singh 				    const struct btf_type *key_type,
588*450af8d0SKP Singh 				    const struct btf_type *value_type)
589*450af8d0SKP Singh {
590*450af8d0SKP Singh 	u32 int_data;
591*450af8d0SKP Singh 
592*450af8d0SKP Singh 	if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
593*450af8d0SKP Singh 		return -EINVAL;
594*450af8d0SKP Singh 
595*450af8d0SKP Singh 	int_data = *(u32 *)(key_type + 1);
596*450af8d0SKP Singh 	if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data))
597*450af8d0SKP Singh 		return -EINVAL;
598*450af8d0SKP Singh 
599*450af8d0SKP Singh 	return 0;
600*450af8d0SKP Singh }
601