xref: /openbmc/linux/kernel/bpf/bpf_local_storage.c (revision 70b971118e074d5042715587953f27929e99117a)
1450af8d0SKP Singh // SPDX-License-Identifier: GPL-2.0
2450af8d0SKP Singh /* Copyright (c) 2019 Facebook  */
3450af8d0SKP Singh #include <linux/rculist.h>
4450af8d0SKP Singh #include <linux/list.h>
5450af8d0SKP Singh #include <linux/hash.h>
6450af8d0SKP Singh #include <linux/types.h>
7450af8d0SKP Singh #include <linux/spinlock.h>
8450af8d0SKP Singh #include <linux/bpf.h>
9450af8d0SKP Singh #include <linux/btf_ids.h>
10450af8d0SKP Singh #include <linux/bpf_local_storage.h>
11450af8d0SKP Singh #include <net/sock.h>
12450af8d0SKP Singh #include <uapi/linux/sock_diag.h>
13450af8d0SKP Singh #include <uapi/linux/btf.h>
14450af8d0SKP Singh 
15450af8d0SKP Singh #define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE)
16450af8d0SKP Singh 
17450af8d0SKP Singh static struct bpf_local_storage_map_bucket *
18450af8d0SKP Singh select_bucket(struct bpf_local_storage_map *smap,
19450af8d0SKP Singh 	      struct bpf_local_storage_elem *selem)
20450af8d0SKP Singh {
21450af8d0SKP Singh 	return &smap->buckets[hash_ptr(selem, smap->bucket_log)];
22450af8d0SKP Singh }
23450af8d0SKP Singh 
24450af8d0SKP Singh static int mem_charge(struct bpf_local_storage_map *smap, void *owner, u32 size)
25450af8d0SKP Singh {
26450af8d0SKP Singh 	struct bpf_map *map = &smap->map;
27450af8d0SKP Singh 
28450af8d0SKP Singh 	if (!map->ops->map_local_storage_charge)
29450af8d0SKP Singh 		return 0;
30450af8d0SKP Singh 
31450af8d0SKP Singh 	return map->ops->map_local_storage_charge(smap, owner, size);
32450af8d0SKP Singh }
33450af8d0SKP Singh 
34450af8d0SKP Singh static void mem_uncharge(struct bpf_local_storage_map *smap, void *owner,
35450af8d0SKP Singh 			 u32 size)
36450af8d0SKP Singh {
37450af8d0SKP Singh 	struct bpf_map *map = &smap->map;
38450af8d0SKP Singh 
39450af8d0SKP Singh 	if (map->ops->map_local_storage_uncharge)
40450af8d0SKP Singh 		map->ops->map_local_storage_uncharge(smap, owner, size);
41450af8d0SKP Singh }
42450af8d0SKP Singh 
43450af8d0SKP Singh static struct bpf_local_storage __rcu **
44450af8d0SKP Singh owner_storage(struct bpf_local_storage_map *smap, void *owner)
45450af8d0SKP Singh {
46450af8d0SKP Singh 	struct bpf_map *map = &smap->map;
47450af8d0SKP Singh 
48450af8d0SKP Singh 	return map->ops->map_owner_storage_ptr(owner);
49450af8d0SKP Singh }
50450af8d0SKP Singh 
51450af8d0SKP Singh static bool selem_linked_to_storage(const struct bpf_local_storage_elem *selem)
52450af8d0SKP Singh {
53450af8d0SKP Singh 	return !hlist_unhashed(&selem->snode);
54450af8d0SKP Singh }
55450af8d0SKP Singh 
56450af8d0SKP Singh static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem)
57450af8d0SKP Singh {
58450af8d0SKP Singh 	return !hlist_unhashed(&selem->map_node);
59450af8d0SKP Singh }
60450af8d0SKP Singh 
61450af8d0SKP Singh struct bpf_local_storage_elem *
62450af8d0SKP Singh bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
63450af8d0SKP Singh 		void *value, bool charge_mem)
64450af8d0SKP Singh {
65450af8d0SKP Singh 	struct bpf_local_storage_elem *selem;
66450af8d0SKP Singh 
67450af8d0SKP Singh 	if (charge_mem && mem_charge(smap, owner, smap->elem_size))
68450af8d0SKP Singh 		return NULL;
69450af8d0SKP Singh 
70450af8d0SKP Singh 	selem = kzalloc(smap->elem_size, GFP_ATOMIC | __GFP_NOWARN);
71450af8d0SKP Singh 	if (selem) {
72450af8d0SKP Singh 		if (value)
73450af8d0SKP Singh 			memcpy(SDATA(selem)->data, value, smap->map.value_size);
74450af8d0SKP Singh 		return selem;
75450af8d0SKP Singh 	}
76450af8d0SKP Singh 
77450af8d0SKP Singh 	if (charge_mem)
78450af8d0SKP Singh 		mem_uncharge(smap, owner, smap->elem_size);
79450af8d0SKP Singh 
80450af8d0SKP Singh 	return NULL;
81450af8d0SKP Singh }
82450af8d0SKP Singh 
83450af8d0SKP Singh /* local_storage->lock must be held and selem->local_storage == local_storage.
84450af8d0SKP Singh  * The caller must ensure selem->smap is still valid to be
85450af8d0SKP Singh  * dereferenced for its smap->elem_size and smap->cache_idx.
86450af8d0SKP Singh  */
87450af8d0SKP Singh bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage,
88450af8d0SKP Singh 				     struct bpf_local_storage_elem *selem,
89450af8d0SKP Singh 				     bool uncharge_mem)
90450af8d0SKP Singh {
91450af8d0SKP Singh 	struct bpf_local_storage_map *smap;
92450af8d0SKP Singh 	bool free_local_storage;
93450af8d0SKP Singh 	void *owner;
94450af8d0SKP Singh 
95450af8d0SKP Singh 	smap = rcu_dereference(SDATA(selem)->smap);
96450af8d0SKP Singh 	owner = local_storage->owner;
97450af8d0SKP Singh 
98450af8d0SKP Singh 	/* All uncharging on the owner must be done first.
99450af8d0SKP Singh 	 * The owner may be freed once the last selem is unlinked
100450af8d0SKP Singh 	 * from local_storage.
101450af8d0SKP Singh 	 */
102450af8d0SKP Singh 	if (uncharge_mem)
103450af8d0SKP Singh 		mem_uncharge(smap, owner, smap->elem_size);
104450af8d0SKP Singh 
105450af8d0SKP Singh 	free_local_storage = hlist_is_singular_node(&selem->snode,
106450af8d0SKP Singh 						    &local_storage->list);
107450af8d0SKP Singh 	if (free_local_storage) {
108450af8d0SKP Singh 		mem_uncharge(smap, owner, sizeof(struct bpf_local_storage));
109450af8d0SKP Singh 		local_storage->owner = NULL;
110450af8d0SKP Singh 
111450af8d0SKP Singh 		/* After this RCU_INIT, owner may be freed and cannot be used */
112450af8d0SKP Singh 		RCU_INIT_POINTER(*owner_storage(smap, owner), NULL);
113450af8d0SKP Singh 
114450af8d0SKP Singh 		/* local_storage is not freed now.  local_storage->lock is
115450af8d0SKP Singh 		 * still held and raw_spin_unlock_bh(&local_storage->lock)
116450af8d0SKP Singh 		 * will be done by the caller.
117450af8d0SKP Singh 		 *
118450af8d0SKP Singh 		 * Although the unlock will be done under
119450af8d0SKP Singh 		 * rcu_read_lock(),  it is more intutivie to
120450af8d0SKP Singh 		 * read if kfree_rcu(local_storage, rcu) is done
121450af8d0SKP Singh 		 * after the raw_spin_unlock_bh(&local_storage->lock).
122450af8d0SKP Singh 		 *
123450af8d0SKP Singh 		 * Hence, a "bool free_local_storage" is returned
124450af8d0SKP Singh 		 * to the caller which then calls the kfree_rcu()
125450af8d0SKP Singh 		 * after unlock.
126450af8d0SKP Singh 		 */
127450af8d0SKP Singh 	}
128450af8d0SKP Singh 	hlist_del_init_rcu(&selem->snode);
129450af8d0SKP Singh 	if (rcu_access_pointer(local_storage->cache[smap->cache_idx]) ==
130450af8d0SKP Singh 	    SDATA(selem))
131450af8d0SKP Singh 		RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL);
132450af8d0SKP Singh 
133450af8d0SKP Singh 	kfree_rcu(selem, rcu);
134450af8d0SKP Singh 
135450af8d0SKP Singh 	return free_local_storage;
136450af8d0SKP Singh }
137450af8d0SKP Singh 
138450af8d0SKP Singh static void __bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem)
139450af8d0SKP Singh {
140450af8d0SKP Singh 	struct bpf_local_storage *local_storage;
141450af8d0SKP Singh 	bool free_local_storage = false;
142450af8d0SKP Singh 
143450af8d0SKP Singh 	if (unlikely(!selem_linked_to_storage(selem)))
144450af8d0SKP Singh 		/* selem has already been unlinked from sk */
145450af8d0SKP Singh 		return;
146450af8d0SKP Singh 
147450af8d0SKP Singh 	local_storage = rcu_dereference(selem->local_storage);
148450af8d0SKP Singh 	raw_spin_lock_bh(&local_storage->lock);
149450af8d0SKP Singh 	if (likely(selem_linked_to_storage(selem)))
150450af8d0SKP Singh 		free_local_storage = bpf_selem_unlink_storage_nolock(
151450af8d0SKP Singh 			local_storage, selem, true);
152450af8d0SKP Singh 	raw_spin_unlock_bh(&local_storage->lock);
153450af8d0SKP Singh 
154450af8d0SKP Singh 	if (free_local_storage)
155450af8d0SKP Singh 		kfree_rcu(local_storage, rcu);
156450af8d0SKP Singh }
157450af8d0SKP Singh 
158450af8d0SKP Singh void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
159450af8d0SKP Singh 				   struct bpf_local_storage_elem *selem)
160450af8d0SKP Singh {
161450af8d0SKP Singh 	RCU_INIT_POINTER(selem->local_storage, local_storage);
162*70b97111SMartin KaFai Lau 	hlist_add_head_rcu(&selem->snode, &local_storage->list);
163450af8d0SKP Singh }
164450af8d0SKP Singh 
165450af8d0SKP Singh void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
166450af8d0SKP Singh {
167450af8d0SKP Singh 	struct bpf_local_storage_map *smap;
168450af8d0SKP Singh 	struct bpf_local_storage_map_bucket *b;
169450af8d0SKP Singh 
170450af8d0SKP Singh 	if (unlikely(!selem_linked_to_map(selem)))
171450af8d0SKP Singh 		/* selem has already be unlinked from smap */
172450af8d0SKP Singh 		return;
173450af8d0SKP Singh 
174450af8d0SKP Singh 	smap = rcu_dereference(SDATA(selem)->smap);
175450af8d0SKP Singh 	b = select_bucket(smap, selem);
176450af8d0SKP Singh 	raw_spin_lock_bh(&b->lock);
177450af8d0SKP Singh 	if (likely(selem_linked_to_map(selem)))
178450af8d0SKP Singh 		hlist_del_init_rcu(&selem->map_node);
179450af8d0SKP Singh 	raw_spin_unlock_bh(&b->lock);
180450af8d0SKP Singh }
181450af8d0SKP Singh 
182450af8d0SKP Singh void bpf_selem_link_map(struct bpf_local_storage_map *smap,
183450af8d0SKP Singh 			struct bpf_local_storage_elem *selem)
184450af8d0SKP Singh {
185450af8d0SKP Singh 	struct bpf_local_storage_map_bucket *b = select_bucket(smap, selem);
186450af8d0SKP Singh 
187450af8d0SKP Singh 	raw_spin_lock_bh(&b->lock);
188450af8d0SKP Singh 	RCU_INIT_POINTER(SDATA(selem)->smap, smap);
189450af8d0SKP Singh 	hlist_add_head_rcu(&selem->map_node, &b->list);
190450af8d0SKP Singh 	raw_spin_unlock_bh(&b->lock);
191450af8d0SKP Singh }
192450af8d0SKP Singh 
193450af8d0SKP Singh void bpf_selem_unlink(struct bpf_local_storage_elem *selem)
194450af8d0SKP Singh {
195450af8d0SKP Singh 	/* Always unlink from map before unlinking from local_storage
196450af8d0SKP Singh 	 * because selem will be freed after successfully unlinked from
197450af8d0SKP Singh 	 * the local_storage.
198450af8d0SKP Singh 	 */
199450af8d0SKP Singh 	bpf_selem_unlink_map(selem);
200450af8d0SKP Singh 	__bpf_selem_unlink_storage(selem);
201450af8d0SKP Singh }
202450af8d0SKP Singh 
203450af8d0SKP Singh struct bpf_local_storage_data *
204450af8d0SKP Singh bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
205450af8d0SKP Singh 			 struct bpf_local_storage_map *smap,
206450af8d0SKP Singh 			 bool cacheit_lockit)
207450af8d0SKP Singh {
208450af8d0SKP Singh 	struct bpf_local_storage_data *sdata;
209450af8d0SKP Singh 	struct bpf_local_storage_elem *selem;
210450af8d0SKP Singh 
211450af8d0SKP Singh 	/* Fast path (cache hit) */
212450af8d0SKP Singh 	sdata = rcu_dereference(local_storage->cache[smap->cache_idx]);
213450af8d0SKP Singh 	if (sdata && rcu_access_pointer(sdata->smap) == smap)
214450af8d0SKP Singh 		return sdata;
215450af8d0SKP Singh 
216450af8d0SKP Singh 	/* Slow path (cache miss) */
217450af8d0SKP Singh 	hlist_for_each_entry_rcu(selem, &local_storage->list, snode)
218450af8d0SKP Singh 		if (rcu_access_pointer(SDATA(selem)->smap) == smap)
219450af8d0SKP Singh 			break;
220450af8d0SKP Singh 
221450af8d0SKP Singh 	if (!selem)
222450af8d0SKP Singh 		return NULL;
223450af8d0SKP Singh 
224450af8d0SKP Singh 	sdata = SDATA(selem);
225450af8d0SKP Singh 	if (cacheit_lockit) {
226450af8d0SKP Singh 		/* spinlock is needed to avoid racing with the
227450af8d0SKP Singh 		 * parallel delete.  Otherwise, publishing an already
228450af8d0SKP Singh 		 * deleted sdata to the cache will become a use-after-free
229450af8d0SKP Singh 		 * problem in the next bpf_local_storage_lookup().
230450af8d0SKP Singh 		 */
231450af8d0SKP Singh 		raw_spin_lock_bh(&local_storage->lock);
232450af8d0SKP Singh 		if (selem_linked_to_storage(selem))
233450af8d0SKP Singh 			rcu_assign_pointer(local_storage->cache[smap->cache_idx],
234450af8d0SKP Singh 					   sdata);
235450af8d0SKP Singh 		raw_spin_unlock_bh(&local_storage->lock);
236450af8d0SKP Singh 	}
237450af8d0SKP Singh 
238450af8d0SKP Singh 	return sdata;
239450af8d0SKP Singh }
240450af8d0SKP Singh 
241450af8d0SKP Singh static int check_flags(const struct bpf_local_storage_data *old_sdata,
242450af8d0SKP Singh 		       u64 map_flags)
243450af8d0SKP Singh {
244450af8d0SKP Singh 	if (old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
245450af8d0SKP Singh 		/* elem already exists */
246450af8d0SKP Singh 		return -EEXIST;
247450af8d0SKP Singh 
248450af8d0SKP Singh 	if (!old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
249450af8d0SKP Singh 		/* elem doesn't exist, cannot update it */
250450af8d0SKP Singh 		return -ENOENT;
251450af8d0SKP Singh 
252450af8d0SKP Singh 	return 0;
253450af8d0SKP Singh }
254450af8d0SKP Singh 
255450af8d0SKP Singh int bpf_local_storage_alloc(void *owner,
256450af8d0SKP Singh 			    struct bpf_local_storage_map *smap,
257450af8d0SKP Singh 			    struct bpf_local_storage_elem *first_selem)
258450af8d0SKP Singh {
259450af8d0SKP Singh 	struct bpf_local_storage *prev_storage, *storage;
260450af8d0SKP Singh 	struct bpf_local_storage **owner_storage_ptr;
261450af8d0SKP Singh 	int err;
262450af8d0SKP Singh 
263450af8d0SKP Singh 	err = mem_charge(smap, owner, sizeof(*storage));
264450af8d0SKP Singh 	if (err)
265450af8d0SKP Singh 		return err;
266450af8d0SKP Singh 
267450af8d0SKP Singh 	storage = kzalloc(sizeof(*storage), GFP_ATOMIC | __GFP_NOWARN);
268450af8d0SKP Singh 	if (!storage) {
269450af8d0SKP Singh 		err = -ENOMEM;
270450af8d0SKP Singh 		goto uncharge;
271450af8d0SKP Singh 	}
272450af8d0SKP Singh 
273450af8d0SKP Singh 	INIT_HLIST_HEAD(&storage->list);
274450af8d0SKP Singh 	raw_spin_lock_init(&storage->lock);
275450af8d0SKP Singh 	storage->owner = owner;
276450af8d0SKP Singh 
277450af8d0SKP Singh 	bpf_selem_link_storage_nolock(storage, first_selem);
278450af8d0SKP Singh 	bpf_selem_link_map(smap, first_selem);
279450af8d0SKP Singh 
280450af8d0SKP Singh 	owner_storage_ptr =
281450af8d0SKP Singh 		(struct bpf_local_storage **)owner_storage(smap, owner);
282450af8d0SKP Singh 	/* Publish storage to the owner.
283450af8d0SKP Singh 	 * Instead of using any lock of the kernel object (i.e. owner),
284450af8d0SKP Singh 	 * cmpxchg will work with any kernel object regardless what
285450af8d0SKP Singh 	 * the running context is, bh, irq...etc.
286450af8d0SKP Singh 	 *
287450af8d0SKP Singh 	 * From now on, the owner->storage pointer (e.g. sk->sk_bpf_storage)
288450af8d0SKP Singh 	 * is protected by the storage->lock.  Hence, when freeing
289450af8d0SKP Singh 	 * the owner->storage, the storage->lock must be held before
290450af8d0SKP Singh 	 * setting owner->storage ptr to NULL.
291450af8d0SKP Singh 	 */
292450af8d0SKP Singh 	prev_storage = cmpxchg(owner_storage_ptr, NULL, storage);
293450af8d0SKP Singh 	if (unlikely(prev_storage)) {
294450af8d0SKP Singh 		bpf_selem_unlink_map(first_selem);
295450af8d0SKP Singh 		err = -EAGAIN;
296450af8d0SKP Singh 		goto uncharge;
297450af8d0SKP Singh 
298450af8d0SKP Singh 		/* Note that even first_selem was linked to smap's
299450af8d0SKP Singh 		 * bucket->list, first_selem can be freed immediately
300450af8d0SKP Singh 		 * (instead of kfree_rcu) because
301450af8d0SKP Singh 		 * bpf_local_storage_map_free() does a
302450af8d0SKP Singh 		 * synchronize_rcu() before walking the bucket->list.
303450af8d0SKP Singh 		 * Hence, no one is accessing selem from the
304450af8d0SKP Singh 		 * bucket->list under rcu_read_lock().
305450af8d0SKP Singh 		 */
306450af8d0SKP Singh 	}
307450af8d0SKP Singh 
308450af8d0SKP Singh 	return 0;
309450af8d0SKP Singh 
310450af8d0SKP Singh uncharge:
311450af8d0SKP Singh 	kfree(storage);
312450af8d0SKP Singh 	mem_uncharge(smap, owner, sizeof(*storage));
313450af8d0SKP Singh 	return err;
314450af8d0SKP Singh }
315450af8d0SKP Singh 
316450af8d0SKP Singh /* sk cannot be going away because it is linking new elem
317450af8d0SKP Singh  * to sk->sk_bpf_storage. (i.e. sk->sk_refcnt cannot be 0).
318450af8d0SKP Singh  * Otherwise, it will become a leak (and other memory issues
319450af8d0SKP Singh  * during map destruction).
320450af8d0SKP Singh  */
321450af8d0SKP Singh struct bpf_local_storage_data *
322450af8d0SKP Singh bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
323450af8d0SKP Singh 			 void *value, u64 map_flags)
324450af8d0SKP Singh {
325450af8d0SKP Singh 	struct bpf_local_storage_data *old_sdata = NULL;
326450af8d0SKP Singh 	struct bpf_local_storage_elem *selem;
327450af8d0SKP Singh 	struct bpf_local_storage *local_storage;
328450af8d0SKP Singh 	int err;
329450af8d0SKP Singh 
330450af8d0SKP Singh 	/* BPF_EXIST and BPF_NOEXIST cannot be both set */
331450af8d0SKP Singh 	if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST) ||
332450af8d0SKP Singh 	    /* BPF_F_LOCK can only be used in a value with spin_lock */
333450af8d0SKP Singh 	    unlikely((map_flags & BPF_F_LOCK) &&
334450af8d0SKP Singh 		     !map_value_has_spin_lock(&smap->map)))
335450af8d0SKP Singh 		return ERR_PTR(-EINVAL);
336450af8d0SKP Singh 
337450af8d0SKP Singh 	local_storage = rcu_dereference(*owner_storage(smap, owner));
338450af8d0SKP Singh 	if (!local_storage || hlist_empty(&local_storage->list)) {
339450af8d0SKP Singh 		/* Very first elem for the owner */
340450af8d0SKP Singh 		err = check_flags(NULL, map_flags);
341450af8d0SKP Singh 		if (err)
342450af8d0SKP Singh 			return ERR_PTR(err);
343450af8d0SKP Singh 
344450af8d0SKP Singh 		selem = bpf_selem_alloc(smap, owner, value, true);
345450af8d0SKP Singh 		if (!selem)
346450af8d0SKP Singh 			return ERR_PTR(-ENOMEM);
347450af8d0SKP Singh 
348450af8d0SKP Singh 		err = bpf_local_storage_alloc(owner, smap, selem);
349450af8d0SKP Singh 		if (err) {
350450af8d0SKP Singh 			kfree(selem);
351450af8d0SKP Singh 			mem_uncharge(smap, owner, smap->elem_size);
352450af8d0SKP Singh 			return ERR_PTR(err);
353450af8d0SKP Singh 		}
354450af8d0SKP Singh 
355450af8d0SKP Singh 		return SDATA(selem);
356450af8d0SKP Singh 	}
357450af8d0SKP Singh 
358450af8d0SKP Singh 	if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) {
359450af8d0SKP Singh 		/* Hoping to find an old_sdata to do inline update
360450af8d0SKP Singh 		 * such that it can avoid taking the local_storage->lock
361450af8d0SKP Singh 		 * and changing the lists.
362450af8d0SKP Singh 		 */
363450af8d0SKP Singh 		old_sdata =
364450af8d0SKP Singh 			bpf_local_storage_lookup(local_storage, smap, false);
365450af8d0SKP Singh 		err = check_flags(old_sdata, map_flags);
366450af8d0SKP Singh 		if (err)
367450af8d0SKP Singh 			return ERR_PTR(err);
368450af8d0SKP Singh 		if (old_sdata && selem_linked_to_storage(SELEM(old_sdata))) {
369450af8d0SKP Singh 			copy_map_value_locked(&smap->map, old_sdata->data,
370450af8d0SKP Singh 					      value, false);
371450af8d0SKP Singh 			return old_sdata;
372450af8d0SKP Singh 		}
373450af8d0SKP Singh 	}
374450af8d0SKP Singh 
375450af8d0SKP Singh 	raw_spin_lock_bh(&local_storage->lock);
376450af8d0SKP Singh 
377450af8d0SKP Singh 	/* Recheck local_storage->list under local_storage->lock */
378450af8d0SKP Singh 	if (unlikely(hlist_empty(&local_storage->list))) {
379450af8d0SKP Singh 		/* A parallel del is happening and local_storage is going
380450af8d0SKP Singh 		 * away.  It has just been checked before, so very
381450af8d0SKP Singh 		 * unlikely.  Return instead of retry to keep things
382450af8d0SKP Singh 		 * simple.
383450af8d0SKP Singh 		 */
384450af8d0SKP Singh 		err = -EAGAIN;
385450af8d0SKP Singh 		goto unlock_err;
386450af8d0SKP Singh 	}
387450af8d0SKP Singh 
388450af8d0SKP Singh 	old_sdata = bpf_local_storage_lookup(local_storage, smap, false);
389450af8d0SKP Singh 	err = check_flags(old_sdata, map_flags);
390450af8d0SKP Singh 	if (err)
391450af8d0SKP Singh 		goto unlock_err;
392450af8d0SKP Singh 
393450af8d0SKP Singh 	if (old_sdata && (map_flags & BPF_F_LOCK)) {
394450af8d0SKP Singh 		copy_map_value_locked(&smap->map, old_sdata->data, value,
395450af8d0SKP Singh 				      false);
396450af8d0SKP Singh 		selem = SELEM(old_sdata);
397450af8d0SKP Singh 		goto unlock;
398450af8d0SKP Singh 	}
399450af8d0SKP Singh 
400450af8d0SKP Singh 	/* local_storage->lock is held.  Hence, we are sure
401450af8d0SKP Singh 	 * we can unlink and uncharge the old_sdata successfully
402450af8d0SKP Singh 	 * later.  Hence, instead of charging the new selem now
403450af8d0SKP Singh 	 * and then uncharge the old selem later (which may cause
404450af8d0SKP Singh 	 * a potential but unnecessary charge failure),  avoid taking
405450af8d0SKP Singh 	 * a charge at all here (the "!old_sdata" check) and the
406450af8d0SKP Singh 	 * old_sdata will not be uncharged later during
407450af8d0SKP Singh 	 * bpf_selem_unlink_storage_nolock().
408450af8d0SKP Singh 	 */
409450af8d0SKP Singh 	selem = bpf_selem_alloc(smap, owner, value, !old_sdata);
410450af8d0SKP Singh 	if (!selem) {
411450af8d0SKP Singh 		err = -ENOMEM;
412450af8d0SKP Singh 		goto unlock_err;
413450af8d0SKP Singh 	}
414450af8d0SKP Singh 
415450af8d0SKP Singh 	/* First, link the new selem to the map */
416450af8d0SKP Singh 	bpf_selem_link_map(smap, selem);
417450af8d0SKP Singh 
418450af8d0SKP Singh 	/* Second, link (and publish) the new selem to local_storage */
419450af8d0SKP Singh 	bpf_selem_link_storage_nolock(local_storage, selem);
420450af8d0SKP Singh 
421450af8d0SKP Singh 	/* Third, remove old selem, SELEM(old_sdata) */
422450af8d0SKP Singh 	if (old_sdata) {
423450af8d0SKP Singh 		bpf_selem_unlink_map(SELEM(old_sdata));
424450af8d0SKP Singh 		bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata),
425450af8d0SKP Singh 						false);
426450af8d0SKP Singh 	}
427450af8d0SKP Singh 
428450af8d0SKP Singh unlock:
429450af8d0SKP Singh 	raw_spin_unlock_bh(&local_storage->lock);
430450af8d0SKP Singh 	return SDATA(selem);
431450af8d0SKP Singh 
432450af8d0SKP Singh unlock_err:
433450af8d0SKP Singh 	raw_spin_unlock_bh(&local_storage->lock);
434450af8d0SKP Singh 	return ERR_PTR(err);
435450af8d0SKP Singh }
436450af8d0SKP Singh 
437450af8d0SKP Singh u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache)
438450af8d0SKP Singh {
439450af8d0SKP Singh 	u64 min_usage = U64_MAX;
440450af8d0SKP Singh 	u16 i, res = 0;
441450af8d0SKP Singh 
442450af8d0SKP Singh 	spin_lock(&cache->idx_lock);
443450af8d0SKP Singh 
444450af8d0SKP Singh 	for (i = 0; i < BPF_LOCAL_STORAGE_CACHE_SIZE; i++) {
445450af8d0SKP Singh 		if (cache->idx_usage_counts[i] < min_usage) {
446450af8d0SKP Singh 			min_usage = cache->idx_usage_counts[i];
447450af8d0SKP Singh 			res = i;
448450af8d0SKP Singh 
449450af8d0SKP Singh 			/* Found a free cache_idx */
450450af8d0SKP Singh 			if (!min_usage)
451450af8d0SKP Singh 				break;
452450af8d0SKP Singh 		}
453450af8d0SKP Singh 	}
454450af8d0SKP Singh 	cache->idx_usage_counts[res]++;
455450af8d0SKP Singh 
456450af8d0SKP Singh 	spin_unlock(&cache->idx_lock);
457450af8d0SKP Singh 
458450af8d0SKP Singh 	return res;
459450af8d0SKP Singh }
460450af8d0SKP Singh 
461450af8d0SKP Singh void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache,
462450af8d0SKP Singh 				      u16 idx)
463450af8d0SKP Singh {
464450af8d0SKP Singh 	spin_lock(&cache->idx_lock);
465450af8d0SKP Singh 	cache->idx_usage_counts[idx]--;
466450af8d0SKP Singh 	spin_unlock(&cache->idx_lock);
467450af8d0SKP Singh }
468450af8d0SKP Singh 
469450af8d0SKP Singh void bpf_local_storage_map_free(struct bpf_local_storage_map *smap)
470450af8d0SKP Singh {
471450af8d0SKP Singh 	struct bpf_local_storage_elem *selem;
472450af8d0SKP Singh 	struct bpf_local_storage_map_bucket *b;
473450af8d0SKP Singh 	unsigned int i;
474450af8d0SKP Singh 
475450af8d0SKP Singh 	/* Note that this map might be concurrently cloned from
476450af8d0SKP Singh 	 * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone
477450af8d0SKP Singh 	 * RCU read section to finish before proceeding. New RCU
478450af8d0SKP Singh 	 * read sections should be prevented via bpf_map_inc_not_zero.
479450af8d0SKP Singh 	 */
480450af8d0SKP Singh 	synchronize_rcu();
481450af8d0SKP Singh 
482450af8d0SKP Singh 	/* bpf prog and the userspace can no longer access this map
483450af8d0SKP Singh 	 * now.  No new selem (of this map) can be added
484450af8d0SKP Singh 	 * to the owner->storage or to the map bucket's list.
485450af8d0SKP Singh 	 *
486450af8d0SKP Singh 	 * The elem of this map can be cleaned up here
487450af8d0SKP Singh 	 * or when the storage is freed e.g.
488450af8d0SKP Singh 	 * by bpf_sk_storage_free() during __sk_destruct().
489450af8d0SKP Singh 	 */
490450af8d0SKP Singh 	for (i = 0; i < (1U << smap->bucket_log); i++) {
491450af8d0SKP Singh 		b = &smap->buckets[i];
492450af8d0SKP Singh 
493450af8d0SKP Singh 		rcu_read_lock();
494450af8d0SKP Singh 		/* No one is adding to b->list now */
495450af8d0SKP Singh 		while ((selem = hlist_entry_safe(
496450af8d0SKP Singh 				rcu_dereference_raw(hlist_first_rcu(&b->list)),
497450af8d0SKP Singh 				struct bpf_local_storage_elem, map_node))) {
498450af8d0SKP Singh 			bpf_selem_unlink(selem);
499450af8d0SKP Singh 			cond_resched_rcu();
500450af8d0SKP Singh 		}
501450af8d0SKP Singh 		rcu_read_unlock();
502450af8d0SKP Singh 	}
503450af8d0SKP Singh 
504450af8d0SKP Singh 	/* While freeing the storage we may still need to access the map.
505450af8d0SKP Singh 	 *
506450af8d0SKP Singh 	 * e.g. when bpf_sk_storage_free() has unlinked selem from the map
507450af8d0SKP Singh 	 * which then made the above while((selem = ...)) loop
508450af8d0SKP Singh 	 * exit immediately.
509450af8d0SKP Singh 	 *
510450af8d0SKP Singh 	 * However, while freeing the storage one still needs to access the
511450af8d0SKP Singh 	 * smap->elem_size to do the uncharging in
512450af8d0SKP Singh 	 * bpf_selem_unlink_storage_nolock().
513450af8d0SKP Singh 	 *
514450af8d0SKP Singh 	 * Hence, wait another rcu grace period for the storage to be freed.
515450af8d0SKP Singh 	 */
516450af8d0SKP Singh 	synchronize_rcu();
517450af8d0SKP Singh 
518450af8d0SKP Singh 	kvfree(smap->buckets);
519450af8d0SKP Singh 	kfree(smap);
520450af8d0SKP Singh }
521450af8d0SKP Singh 
522450af8d0SKP Singh int bpf_local_storage_map_alloc_check(union bpf_attr *attr)
523450af8d0SKP Singh {
524450af8d0SKP Singh 	if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK ||
525450af8d0SKP Singh 	    !(attr->map_flags & BPF_F_NO_PREALLOC) ||
526450af8d0SKP Singh 	    attr->max_entries ||
527450af8d0SKP Singh 	    attr->key_size != sizeof(int) || !attr->value_size ||
528450af8d0SKP Singh 	    /* Enforce BTF for userspace sk dumping */
529450af8d0SKP Singh 	    !attr->btf_key_type_id || !attr->btf_value_type_id)
530450af8d0SKP Singh 		return -EINVAL;
531450af8d0SKP Singh 
532450af8d0SKP Singh 	if (!bpf_capable())
533450af8d0SKP Singh 		return -EPERM;
534450af8d0SKP Singh 
535450af8d0SKP Singh 	if (attr->value_size > BPF_LOCAL_STORAGE_MAX_VALUE_SIZE)
536450af8d0SKP Singh 		return -E2BIG;
537450af8d0SKP Singh 
538450af8d0SKP Singh 	return 0;
539450af8d0SKP Singh }
540450af8d0SKP Singh 
541450af8d0SKP Singh struct bpf_local_storage_map *bpf_local_storage_map_alloc(union bpf_attr *attr)
542450af8d0SKP Singh {
543450af8d0SKP Singh 	struct bpf_local_storage_map *smap;
544450af8d0SKP Singh 	unsigned int i;
545450af8d0SKP Singh 	u32 nbuckets;
546450af8d0SKP Singh 	u64 cost;
547450af8d0SKP Singh 	int ret;
548450af8d0SKP Singh 
549450af8d0SKP Singh 	smap = kzalloc(sizeof(*smap), GFP_USER | __GFP_NOWARN);
550450af8d0SKP Singh 	if (!smap)
551450af8d0SKP Singh 		return ERR_PTR(-ENOMEM);
552450af8d0SKP Singh 	bpf_map_init_from_attr(&smap->map, attr);
553450af8d0SKP Singh 
554450af8d0SKP Singh 	nbuckets = roundup_pow_of_two(num_possible_cpus());
555450af8d0SKP Singh 	/* Use at least 2 buckets, select_bucket() is undefined behavior with 1 bucket */
556450af8d0SKP Singh 	nbuckets = max_t(u32, 2, nbuckets);
557450af8d0SKP Singh 	smap->bucket_log = ilog2(nbuckets);
558450af8d0SKP Singh 	cost = sizeof(*smap->buckets) * nbuckets + sizeof(*smap);
559450af8d0SKP Singh 
560450af8d0SKP Singh 	ret = bpf_map_charge_init(&smap->map.memory, cost);
561450af8d0SKP Singh 	if (ret < 0) {
562450af8d0SKP Singh 		kfree(smap);
563450af8d0SKP Singh 		return ERR_PTR(ret);
564450af8d0SKP Singh 	}
565450af8d0SKP Singh 
566450af8d0SKP Singh 	smap->buckets = kvcalloc(sizeof(*smap->buckets), nbuckets,
567450af8d0SKP Singh 				 GFP_USER | __GFP_NOWARN);
568450af8d0SKP Singh 	if (!smap->buckets) {
569450af8d0SKP Singh 		bpf_map_charge_finish(&smap->map.memory);
570450af8d0SKP Singh 		kfree(smap);
571450af8d0SKP Singh 		return ERR_PTR(-ENOMEM);
572450af8d0SKP Singh 	}
573450af8d0SKP Singh 
574450af8d0SKP Singh 	for (i = 0; i < nbuckets; i++) {
575450af8d0SKP Singh 		INIT_HLIST_HEAD(&smap->buckets[i].list);
576450af8d0SKP Singh 		raw_spin_lock_init(&smap->buckets[i].lock);
577450af8d0SKP Singh 	}
578450af8d0SKP Singh 
579450af8d0SKP Singh 	smap->elem_size =
580450af8d0SKP Singh 		sizeof(struct bpf_local_storage_elem) + attr->value_size;
581450af8d0SKP Singh 
582450af8d0SKP Singh 	return smap;
583450af8d0SKP Singh }
584450af8d0SKP Singh 
585450af8d0SKP Singh int bpf_local_storage_map_check_btf(const struct bpf_map *map,
586450af8d0SKP Singh 				    const struct btf *btf,
587450af8d0SKP Singh 				    const struct btf_type *key_type,
588450af8d0SKP Singh 				    const struct btf_type *value_type)
589450af8d0SKP Singh {
590450af8d0SKP Singh 	u32 int_data;
591450af8d0SKP Singh 
592450af8d0SKP Singh 	if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
593450af8d0SKP Singh 		return -EINVAL;
594450af8d0SKP Singh 
595450af8d0SKP Singh 	int_data = *(u32 *)(key_type + 1);
596450af8d0SKP Singh 	if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data))
597450af8d0SKP Singh 		return -EINVAL;
598450af8d0SKP Singh 
599450af8d0SKP Singh 	return 0;
600450af8d0SKP Singh }
601