xref: /openbmc/linux/kernel/bpf/bpf_local_storage.c (revision 6ae9d5e99e1dd26babdd9502759fa25a3fd348ad)
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>
140fe4b381SKP Singh #include <linux/rcupdate.h>
150fe4b381SKP Singh #include <linux/rcupdate_trace.h>
160fe4b381SKP Singh #include <linux/rcupdate_wait.h>
17450af8d0SKP Singh 
18450af8d0SKP Singh #define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE)
19450af8d0SKP Singh 
20450af8d0SKP Singh static struct bpf_local_storage_map_bucket *
21450af8d0SKP Singh select_bucket(struct bpf_local_storage_map *smap,
22450af8d0SKP Singh 	      struct bpf_local_storage_elem *selem)
23450af8d0SKP Singh {
24450af8d0SKP Singh 	return &smap->buckets[hash_ptr(selem, smap->bucket_log)];
25450af8d0SKP Singh }
26450af8d0SKP Singh 
27450af8d0SKP Singh static int mem_charge(struct bpf_local_storage_map *smap, void *owner, u32 size)
28450af8d0SKP Singh {
29450af8d0SKP Singh 	struct bpf_map *map = &smap->map;
30450af8d0SKP Singh 
31450af8d0SKP Singh 	if (!map->ops->map_local_storage_charge)
32450af8d0SKP Singh 		return 0;
33450af8d0SKP Singh 
34450af8d0SKP Singh 	return map->ops->map_local_storage_charge(smap, owner, size);
35450af8d0SKP Singh }
36450af8d0SKP Singh 
37450af8d0SKP Singh static void mem_uncharge(struct bpf_local_storage_map *smap, void *owner,
38450af8d0SKP Singh 			 u32 size)
39450af8d0SKP Singh {
40450af8d0SKP Singh 	struct bpf_map *map = &smap->map;
41450af8d0SKP Singh 
42450af8d0SKP Singh 	if (map->ops->map_local_storage_uncharge)
43450af8d0SKP Singh 		map->ops->map_local_storage_uncharge(smap, owner, size);
44450af8d0SKP Singh }
45450af8d0SKP Singh 
46450af8d0SKP Singh static struct bpf_local_storage __rcu **
47450af8d0SKP Singh owner_storage(struct bpf_local_storage_map *smap, void *owner)
48450af8d0SKP Singh {
49450af8d0SKP Singh 	struct bpf_map *map = &smap->map;
50450af8d0SKP Singh 
51450af8d0SKP Singh 	return map->ops->map_owner_storage_ptr(owner);
52450af8d0SKP Singh }
53450af8d0SKP Singh 
540a09a2f9SKumar Kartikeya Dwivedi static bool selem_linked_to_storage_lockless(const struct bpf_local_storage_elem *selem)
550a09a2f9SKumar Kartikeya Dwivedi {
560a09a2f9SKumar Kartikeya Dwivedi 	return !hlist_unhashed_lockless(&selem->snode);
570a09a2f9SKumar Kartikeya Dwivedi }
580a09a2f9SKumar Kartikeya Dwivedi 
59450af8d0SKP Singh static bool selem_linked_to_storage(const struct bpf_local_storage_elem *selem)
60450af8d0SKP Singh {
61450af8d0SKP Singh 	return !hlist_unhashed(&selem->snode);
62450af8d0SKP Singh }
63450af8d0SKP Singh 
640a09a2f9SKumar Kartikeya Dwivedi static bool selem_linked_to_map_lockless(const struct bpf_local_storage_elem *selem)
650a09a2f9SKumar Kartikeya Dwivedi {
660a09a2f9SKumar Kartikeya Dwivedi 	return !hlist_unhashed_lockless(&selem->map_node);
670a09a2f9SKumar Kartikeya Dwivedi }
680a09a2f9SKumar Kartikeya Dwivedi 
69450af8d0SKP Singh static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem)
70450af8d0SKP Singh {
71450af8d0SKP Singh 	return !hlist_unhashed(&selem->map_node);
72450af8d0SKP Singh }
73450af8d0SKP Singh 
74450af8d0SKP Singh struct bpf_local_storage_elem *
75450af8d0SKP Singh bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
76b00fa38aSJoanne Koong 		void *value, bool charge_mem, gfp_t gfp_flags)
77450af8d0SKP Singh {
78450af8d0SKP Singh 	struct bpf_local_storage_elem *selem;
79450af8d0SKP Singh 
80450af8d0SKP Singh 	if (charge_mem && mem_charge(smap, owner, smap->elem_size))
81450af8d0SKP Singh 		return NULL;
82450af8d0SKP Singh 
8308a7ce38SMartin KaFai Lau 	if (smap->bpf_ma) {
8408a7ce38SMartin KaFai Lau 		migrate_disable();
8508a7ce38SMartin KaFai Lau 		selem = bpf_mem_cache_alloc_flags(&smap->selem_ma, gfp_flags);
8608a7ce38SMartin KaFai Lau 		migrate_enable();
8708a7ce38SMartin KaFai Lau 		if (selem)
8808a7ce38SMartin KaFai Lau 			/* Keep the original bpf_map_kzalloc behavior
8908a7ce38SMartin KaFai Lau 			 * before started using the bpf_mem_cache_alloc.
9008a7ce38SMartin KaFai Lau 			 *
9108a7ce38SMartin KaFai Lau 			 * No need to use zero_map_value. The bpf_selem_free()
9208a7ce38SMartin KaFai Lau 			 * only does bpf_mem_cache_free when there is
9308a7ce38SMartin KaFai Lau 			 * no other bpf prog is using the selem.
9408a7ce38SMartin KaFai Lau 			 */
9508a7ce38SMartin KaFai Lau 			memset(SDATA(selem)->data, 0, smap->map.value_size);
9608a7ce38SMartin KaFai Lau 	} else {
97e9aae8beSRoman Gushchin 		selem = bpf_map_kzalloc(&smap->map, smap->elem_size,
98b00fa38aSJoanne Koong 					gfp_flags | __GFP_NOWARN);
9908a7ce38SMartin KaFai Lau 	}
10008a7ce38SMartin KaFai Lau 
101450af8d0SKP Singh 	if (selem) {
102450af8d0SKP Singh 		if (value)
103836e49e1SXu Kuohai 			copy_map_value(&smap->map, SDATA(selem)->data, value);
1049db44fddSKumar Kartikeya Dwivedi 		/* No need to call check_and_init_map_value as memory is zero init */
105450af8d0SKP Singh 		return selem;
106450af8d0SKP Singh 	}
107450af8d0SKP Singh 
108450af8d0SKP Singh 	if (charge_mem)
109450af8d0SKP Singh 		mem_uncharge(smap, owner, smap->elem_size);
110450af8d0SKP Singh 
111450af8d0SKP Singh 	return NULL;
112450af8d0SKP Singh }
113450af8d0SKP Singh 
114*6ae9d5e9SMartin KaFai Lau /* rcu tasks trace callback for bpf_ma == false */
115*6ae9d5e9SMartin KaFai Lau static void __bpf_local_storage_free_trace_rcu(struct rcu_head *rcu)
116*6ae9d5e9SMartin KaFai Lau {
117*6ae9d5e9SMartin KaFai Lau 	struct bpf_local_storage *local_storage;
118*6ae9d5e9SMartin KaFai Lau 
119*6ae9d5e9SMartin KaFai Lau 	/* If RCU Tasks Trace grace period implies RCU grace period, do
120*6ae9d5e9SMartin KaFai Lau 	 * kfree(), else do kfree_rcu().
121*6ae9d5e9SMartin KaFai Lau 	 */
122*6ae9d5e9SMartin KaFai Lau 	local_storage = container_of(rcu, struct bpf_local_storage, rcu);
123*6ae9d5e9SMartin KaFai Lau 	if (rcu_trace_implies_rcu_gp())
124*6ae9d5e9SMartin KaFai Lau 		kfree(local_storage);
125*6ae9d5e9SMartin KaFai Lau 	else
126*6ae9d5e9SMartin KaFai Lau 		kfree_rcu(local_storage, rcu);
127*6ae9d5e9SMartin KaFai Lau }
128*6ae9d5e9SMartin KaFai Lau 
1294cbd23ccSMartin KaFai Lau static void bpf_local_storage_free_rcu(struct rcu_head *rcu)
1300fe4b381SKP Singh {
1310fe4b381SKP Singh 	struct bpf_local_storage *local_storage;
1320fe4b381SKP Singh 
1331288aaa2SMartin KaFai Lau 	local_storage = container_of(rcu, struct bpf_local_storage, rcu);
134*6ae9d5e9SMartin KaFai Lau 	bpf_mem_cache_raw_free(local_storage);
1351288aaa2SMartin KaFai Lau }
1361288aaa2SMartin KaFai Lau 
1371288aaa2SMartin KaFai Lau static void bpf_local_storage_free_trace_rcu(struct rcu_head *rcu)
1381288aaa2SMartin KaFai Lau {
139d39d1445SHou Tao 	if (rcu_trace_implies_rcu_gp())
1401288aaa2SMartin KaFai Lau 		bpf_local_storage_free_rcu(rcu);
141d39d1445SHou Tao 	else
1421288aaa2SMartin KaFai Lau 		call_rcu(rcu, bpf_local_storage_free_rcu);
1430fe4b381SKP Singh }
1440fe4b381SKP Singh 
145*6ae9d5e9SMartin KaFai Lau /* Handle bpf_ma == false */
146*6ae9d5e9SMartin KaFai Lau static void __bpf_local_storage_free(struct bpf_local_storage *local_storage,
147*6ae9d5e9SMartin KaFai Lau 				     bool vanilla_rcu)
1487e30a847SMartin KaFai Lau {
149*6ae9d5e9SMartin KaFai Lau 	if (vanilla_rcu)
150*6ae9d5e9SMartin KaFai Lau 		kfree_rcu(local_storage, rcu);
151*6ae9d5e9SMartin KaFai Lau 	else
152*6ae9d5e9SMartin KaFai Lau 		call_rcu_tasks_trace(&local_storage->rcu,
153*6ae9d5e9SMartin KaFai Lau 				     __bpf_local_storage_free_trace_rcu);
154*6ae9d5e9SMartin KaFai Lau }
155*6ae9d5e9SMartin KaFai Lau 
156*6ae9d5e9SMartin KaFai Lau static void bpf_local_storage_free(struct bpf_local_storage *local_storage,
157*6ae9d5e9SMartin KaFai Lau 				   struct bpf_local_storage_map *smap,
158*6ae9d5e9SMartin KaFai Lau 				   bool bpf_ma, bool reuse_now)
159*6ae9d5e9SMartin KaFai Lau {
160*6ae9d5e9SMartin KaFai Lau 	if (!bpf_ma) {
161*6ae9d5e9SMartin KaFai Lau 		__bpf_local_storage_free(local_storage, reuse_now);
162*6ae9d5e9SMartin KaFai Lau 		return;
163*6ae9d5e9SMartin KaFai Lau 	}
164*6ae9d5e9SMartin KaFai Lau 
165*6ae9d5e9SMartin KaFai Lau 	if (!reuse_now) {
1667e30a847SMartin KaFai Lau 		call_rcu_tasks_trace(&local_storage->rcu,
1677e30a847SMartin KaFai Lau 				     bpf_local_storage_free_trace_rcu);
168*6ae9d5e9SMartin KaFai Lau 		return;
169*6ae9d5e9SMartin KaFai Lau 	}
170*6ae9d5e9SMartin KaFai Lau 
171*6ae9d5e9SMartin KaFai Lau 	if (smap) {
172*6ae9d5e9SMartin KaFai Lau 		migrate_disable();
173*6ae9d5e9SMartin KaFai Lau 		bpf_mem_cache_free(&smap->storage_ma, local_storage);
174*6ae9d5e9SMartin KaFai Lau 		migrate_enable();
175*6ae9d5e9SMartin KaFai Lau 	} else {
176*6ae9d5e9SMartin KaFai Lau 		/* smap could be NULL if the selem that triggered
177*6ae9d5e9SMartin KaFai Lau 		 * this 'local_storage' creation had been long gone.
178*6ae9d5e9SMartin KaFai Lau 		 * In this case, directly do call_rcu().
179*6ae9d5e9SMartin KaFai Lau 		 */
1807e30a847SMartin KaFai Lau 		call_rcu(&local_storage->rcu, bpf_local_storage_free_rcu);
1817e30a847SMartin KaFai Lau 	}
182*6ae9d5e9SMartin KaFai Lau }
1837e30a847SMartin KaFai Lau 
18408a7ce38SMartin KaFai Lau /* rcu tasks trace callback for bpf_ma == false */
18508a7ce38SMartin KaFai Lau static void __bpf_selem_free_trace_rcu(struct rcu_head *rcu)
18608a7ce38SMartin KaFai Lau {
18708a7ce38SMartin KaFai Lau 	struct bpf_local_storage_elem *selem;
18808a7ce38SMartin KaFai Lau 
18908a7ce38SMartin KaFai Lau 	selem = container_of(rcu, struct bpf_local_storage_elem, rcu);
19008a7ce38SMartin KaFai Lau 	if (rcu_trace_implies_rcu_gp())
19108a7ce38SMartin KaFai Lau 		kfree(selem);
19208a7ce38SMartin KaFai Lau 	else
19308a7ce38SMartin KaFai Lau 		kfree_rcu(selem, rcu);
19408a7ce38SMartin KaFai Lau }
19508a7ce38SMartin KaFai Lau 
19608a7ce38SMartin KaFai Lau /* Handle bpf_ma == false */
19708a7ce38SMartin KaFai Lau static void __bpf_selem_free(struct bpf_local_storage_elem *selem,
19808a7ce38SMartin KaFai Lau 			     bool vanilla_rcu)
19908a7ce38SMartin KaFai Lau {
20008a7ce38SMartin KaFai Lau 	if (vanilla_rcu)
20108a7ce38SMartin KaFai Lau 		kfree_rcu(selem, rcu);
20208a7ce38SMartin KaFai Lau 	else
20308a7ce38SMartin KaFai Lau 		call_rcu_tasks_trace(&selem->rcu, __bpf_selem_free_trace_rcu);
20408a7ce38SMartin KaFai Lau }
20508a7ce38SMartin KaFai Lau 
206f8ccf30cSMartin KaFai Lau static void bpf_selem_free_rcu(struct rcu_head *rcu)
2070fe4b381SKP Singh {
2080fe4b381SKP Singh 	struct bpf_local_storage_elem *selem;
2090fe4b381SKP Singh 
2100fe4b381SKP Singh 	selem = container_of(rcu, struct bpf_local_storage_elem, rcu);
21108a7ce38SMartin KaFai Lau 	bpf_mem_cache_raw_free(selem);
212f8ccf30cSMartin KaFai Lau }
213f8ccf30cSMartin KaFai Lau 
214f8ccf30cSMartin KaFai Lau static void bpf_selem_free_trace_rcu(struct rcu_head *rcu)
215f8ccf30cSMartin KaFai Lau {
216f8ccf30cSMartin KaFai Lau 	if (rcu_trace_implies_rcu_gp())
217f8ccf30cSMartin KaFai Lau 		bpf_selem_free_rcu(rcu);
218d39d1445SHou Tao 	else
219f8ccf30cSMartin KaFai Lau 		call_rcu(rcu, bpf_selem_free_rcu);
2200fe4b381SKP Singh }
2210fe4b381SKP Singh 
222c0d63f30SMartin KaFai Lau void bpf_selem_free(struct bpf_local_storage_elem *selem,
223c0d63f30SMartin KaFai Lau 		    struct bpf_local_storage_map *smap,
224c0d63f30SMartin KaFai Lau 		    bool reuse_now)
225c0d63f30SMartin KaFai Lau {
226c0d63f30SMartin KaFai Lau 	bpf_obj_free_fields(smap->map.record, SDATA(selem)->data);
22708a7ce38SMartin KaFai Lau 
22808a7ce38SMartin KaFai Lau 	if (!smap->bpf_ma) {
22908a7ce38SMartin KaFai Lau 		__bpf_selem_free(selem, reuse_now);
23008a7ce38SMartin KaFai Lau 		return;
23108a7ce38SMartin KaFai Lau 	}
23208a7ce38SMartin KaFai Lau 
23308a7ce38SMartin KaFai Lau 	if (!reuse_now) {
234c0d63f30SMartin KaFai Lau 		call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_trace_rcu);
23508a7ce38SMartin KaFai Lau 	} else {
23608a7ce38SMartin KaFai Lau 		/* Instead of using the vanilla call_rcu(),
23708a7ce38SMartin KaFai Lau 		 * bpf_mem_cache_free will be able to reuse selem
23808a7ce38SMartin KaFai Lau 		 * immediately.
23908a7ce38SMartin KaFai Lau 		 */
24008a7ce38SMartin KaFai Lau 		migrate_disable();
24108a7ce38SMartin KaFai Lau 		bpf_mem_cache_free(&smap->selem_ma, selem);
24208a7ce38SMartin KaFai Lau 		migrate_enable();
24308a7ce38SMartin KaFai Lau 	}
244c0d63f30SMartin KaFai Lau }
245c0d63f30SMartin KaFai Lau 
246450af8d0SKP Singh /* local_storage->lock must be held and selem->local_storage == local_storage.
247450af8d0SKP Singh  * The caller must ensure selem->smap is still valid to be
248450af8d0SKP Singh  * dereferenced for its smap->elem_size and smap->cache_idx.
249450af8d0SKP Singh  */
250c83597faSYonghong Song static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage,
251450af8d0SKP Singh 					    struct bpf_local_storage_elem *selem,
252a47eabf2SMartin KaFai Lau 					    bool uncharge_mem, bool reuse_now)
253450af8d0SKP Singh {
254450af8d0SKP Singh 	struct bpf_local_storage_map *smap;
255450af8d0SKP Singh 	bool free_local_storage;
256450af8d0SKP Singh 	void *owner;
257450af8d0SKP Singh 
2580fe4b381SKP Singh 	smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
259450af8d0SKP Singh 	owner = local_storage->owner;
260450af8d0SKP Singh 
261450af8d0SKP Singh 	/* All uncharging on the owner must be done first.
262450af8d0SKP Singh 	 * The owner may be freed once the last selem is unlinked
263450af8d0SKP Singh 	 * from local_storage.
264450af8d0SKP Singh 	 */
265450af8d0SKP Singh 	if (uncharge_mem)
266450af8d0SKP Singh 		mem_uncharge(smap, owner, smap->elem_size);
267450af8d0SKP Singh 
268450af8d0SKP Singh 	free_local_storage = hlist_is_singular_node(&selem->snode,
269450af8d0SKP Singh 						    &local_storage->list);
270450af8d0SKP Singh 	if (free_local_storage) {
271450af8d0SKP Singh 		mem_uncharge(smap, owner, sizeof(struct bpf_local_storage));
272450af8d0SKP Singh 		local_storage->owner = NULL;
273450af8d0SKP Singh 
274450af8d0SKP Singh 		/* After this RCU_INIT, owner may be freed and cannot be used */
275450af8d0SKP Singh 		RCU_INIT_POINTER(*owner_storage(smap, owner), NULL);
276450af8d0SKP Singh 
277450af8d0SKP Singh 		/* local_storage is not freed now.  local_storage->lock is
278450af8d0SKP Singh 		 * still held and raw_spin_unlock_bh(&local_storage->lock)
279450af8d0SKP Singh 		 * will be done by the caller.
280450af8d0SKP Singh 		 *
281450af8d0SKP Singh 		 * Although the unlock will be done under
282c561d110STom Rix 		 * rcu_read_lock(),  it is more intuitive to
2830fe4b381SKP Singh 		 * read if the freeing of the storage is done
284450af8d0SKP Singh 		 * after the raw_spin_unlock_bh(&local_storage->lock).
285450af8d0SKP Singh 		 *
286450af8d0SKP Singh 		 * Hence, a "bool free_local_storage" is returned
2870fe4b381SKP Singh 		 * to the caller which then calls then frees the storage after
2880fe4b381SKP Singh 		 * all the RCU grace periods have expired.
289450af8d0SKP Singh 		 */
290450af8d0SKP Singh 	}
291450af8d0SKP Singh 	hlist_del_init_rcu(&selem->snode);
292450af8d0SKP Singh 	if (rcu_access_pointer(local_storage->cache[smap->cache_idx]) ==
293450af8d0SKP Singh 	    SDATA(selem))
294450af8d0SKP Singh 		RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL);
295450af8d0SKP Singh 
296c0d63f30SMartin KaFai Lau 	bpf_selem_free(selem, smap, reuse_now);
297dcf456c9SKP Singh 
298fc6652aaSMartin KaFai Lau 	if (rcu_access_pointer(local_storage->smap) == smap)
299fc6652aaSMartin KaFai Lau 		RCU_INIT_POINTER(local_storage->smap, NULL);
300fc6652aaSMartin KaFai Lau 
301450af8d0SKP Singh 	return free_local_storage;
302450af8d0SKP Singh }
303450af8d0SKP Singh 
304*6ae9d5e9SMartin KaFai Lau static bool check_storage_bpf_ma(struct bpf_local_storage *local_storage,
305*6ae9d5e9SMartin KaFai Lau 				 struct bpf_local_storage_map *storage_smap,
306*6ae9d5e9SMartin KaFai Lau 				 struct bpf_local_storage_elem *selem)
307*6ae9d5e9SMartin KaFai Lau {
308*6ae9d5e9SMartin KaFai Lau 
309*6ae9d5e9SMartin KaFai Lau 	struct bpf_local_storage_map *selem_smap;
310*6ae9d5e9SMartin KaFai Lau 
311*6ae9d5e9SMartin KaFai Lau 	/* local_storage->smap may be NULL. If it is, get the bpf_ma
312*6ae9d5e9SMartin KaFai Lau 	 * from any selem in the local_storage->list. The bpf_ma of all
313*6ae9d5e9SMartin KaFai Lau 	 * local_storage and selem should have the same value
314*6ae9d5e9SMartin KaFai Lau 	 * for the same map type.
315*6ae9d5e9SMartin KaFai Lau 	 *
316*6ae9d5e9SMartin KaFai Lau 	 * If the local_storage->list is already empty, the caller will not
317*6ae9d5e9SMartin KaFai Lau 	 * care about the bpf_ma value also because the caller is not
318*6ae9d5e9SMartin KaFai Lau 	 * responsibile to free the local_storage.
319*6ae9d5e9SMartin KaFai Lau 	 */
320*6ae9d5e9SMartin KaFai Lau 
321*6ae9d5e9SMartin KaFai Lau 	if (storage_smap)
322*6ae9d5e9SMartin KaFai Lau 		return storage_smap->bpf_ma;
323*6ae9d5e9SMartin KaFai Lau 
324*6ae9d5e9SMartin KaFai Lau 	if (!selem) {
325*6ae9d5e9SMartin KaFai Lau 		struct hlist_node *n;
326*6ae9d5e9SMartin KaFai Lau 
327*6ae9d5e9SMartin KaFai Lau 		n = rcu_dereference_check(hlist_first_rcu(&local_storage->list),
328*6ae9d5e9SMartin KaFai Lau 					  bpf_rcu_lock_held());
329*6ae9d5e9SMartin KaFai Lau 		if (!n)
330*6ae9d5e9SMartin KaFai Lau 			return false;
331*6ae9d5e9SMartin KaFai Lau 
332*6ae9d5e9SMartin KaFai Lau 		selem = hlist_entry(n, struct bpf_local_storage_elem, snode);
333*6ae9d5e9SMartin KaFai Lau 	}
334*6ae9d5e9SMartin KaFai Lau 	selem_smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
335*6ae9d5e9SMartin KaFai Lau 
336*6ae9d5e9SMartin KaFai Lau 	return selem_smap->bpf_ma;
337*6ae9d5e9SMartin KaFai Lau }
338*6ae9d5e9SMartin KaFai Lau 
339121f31f3SMartin KaFai Lau static void bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem,
340a47eabf2SMartin KaFai Lau 				     bool reuse_now)
341450af8d0SKP Singh {
342*6ae9d5e9SMartin KaFai Lau 	struct bpf_local_storage_map *storage_smap;
343450af8d0SKP Singh 	struct bpf_local_storage *local_storage;
344*6ae9d5e9SMartin KaFai Lau 	bool bpf_ma, free_local_storage = false;
345a10787e6SSong Liu 	unsigned long flags;
346450af8d0SKP Singh 
3470a09a2f9SKumar Kartikeya Dwivedi 	if (unlikely(!selem_linked_to_storage_lockless(selem)))
348450af8d0SKP Singh 		/* selem has already been unlinked from sk */
349450af8d0SKP Singh 		return;
350450af8d0SKP Singh 
3510fe4b381SKP Singh 	local_storage = rcu_dereference_check(selem->local_storage,
3520fe4b381SKP Singh 					      bpf_rcu_lock_held());
353*6ae9d5e9SMartin KaFai Lau 	storage_smap = rcu_dereference_check(local_storage->smap,
354*6ae9d5e9SMartin KaFai Lau 					     bpf_rcu_lock_held());
355*6ae9d5e9SMartin KaFai Lau 	bpf_ma = check_storage_bpf_ma(local_storage, storage_smap, selem);
356*6ae9d5e9SMartin KaFai Lau 
357a10787e6SSong Liu 	raw_spin_lock_irqsave(&local_storage->lock, flags);
358450af8d0SKP Singh 	if (likely(selem_linked_to_storage(selem)))
359450af8d0SKP Singh 		free_local_storage = bpf_selem_unlink_storage_nolock(
360a47eabf2SMartin KaFai Lau 			local_storage, selem, true, reuse_now);
361a10787e6SSong Liu 	raw_spin_unlock_irqrestore(&local_storage->lock, flags);
362450af8d0SKP Singh 
3637e30a847SMartin KaFai Lau 	if (free_local_storage)
364*6ae9d5e9SMartin KaFai Lau 		bpf_local_storage_free(local_storage, storage_smap, bpf_ma, reuse_now);
365450af8d0SKP Singh }
366450af8d0SKP Singh 
367450af8d0SKP Singh void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
368450af8d0SKP Singh 				   struct bpf_local_storage_elem *selem)
369450af8d0SKP Singh {
370450af8d0SKP Singh 	RCU_INIT_POINTER(selem->local_storage, local_storage);
37170b97111SMartin KaFai Lau 	hlist_add_head_rcu(&selem->snode, &local_storage->list);
372450af8d0SKP Singh }
373450af8d0SKP Singh 
3744cbd23ccSMartin KaFai Lau static void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
375450af8d0SKP Singh {
376450af8d0SKP Singh 	struct bpf_local_storage_map *smap;
377450af8d0SKP Singh 	struct bpf_local_storage_map_bucket *b;
378a10787e6SSong Liu 	unsigned long flags;
379450af8d0SKP Singh 
3800a09a2f9SKumar Kartikeya Dwivedi 	if (unlikely(!selem_linked_to_map_lockless(selem)))
381450af8d0SKP Singh 		/* selem has already be unlinked from smap */
382450af8d0SKP Singh 		return;
383450af8d0SKP Singh 
3840fe4b381SKP Singh 	smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
385450af8d0SKP Singh 	b = select_bucket(smap, selem);
386a10787e6SSong Liu 	raw_spin_lock_irqsave(&b->lock, flags);
387450af8d0SKP Singh 	if (likely(selem_linked_to_map(selem)))
388450af8d0SKP Singh 		hlist_del_init_rcu(&selem->map_node);
389a10787e6SSong Liu 	raw_spin_unlock_irqrestore(&b->lock, flags);
390450af8d0SKP Singh }
391450af8d0SKP Singh 
392450af8d0SKP Singh void bpf_selem_link_map(struct bpf_local_storage_map *smap,
393450af8d0SKP Singh 			struct bpf_local_storage_elem *selem)
394450af8d0SKP Singh {
395450af8d0SKP Singh 	struct bpf_local_storage_map_bucket *b = select_bucket(smap, selem);
396a10787e6SSong Liu 	unsigned long flags;
397450af8d0SKP Singh 
398a10787e6SSong Liu 	raw_spin_lock_irqsave(&b->lock, flags);
399450af8d0SKP Singh 	RCU_INIT_POINTER(SDATA(selem)->smap, smap);
400450af8d0SKP Singh 	hlist_add_head_rcu(&selem->map_node, &b->list);
401a10787e6SSong Liu 	raw_spin_unlock_irqrestore(&b->lock, flags);
402450af8d0SKP Singh }
403450af8d0SKP Singh 
404a47eabf2SMartin KaFai Lau void bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool reuse_now)
405450af8d0SKP Singh {
406450af8d0SKP Singh 	/* Always unlink from map before unlinking from local_storage
407450af8d0SKP Singh 	 * because selem will be freed after successfully unlinked from
408450af8d0SKP Singh 	 * the local_storage.
409450af8d0SKP Singh 	 */
410450af8d0SKP Singh 	bpf_selem_unlink_map(selem);
411a47eabf2SMartin KaFai Lau 	bpf_selem_unlink_storage(selem, reuse_now);
412450af8d0SKP Singh }
413450af8d0SKP Singh 
414e8b02296SMartin KaFai Lau /* If cacheit_lockit is false, this lookup function is lockless */
415450af8d0SKP Singh struct bpf_local_storage_data *
416450af8d0SKP Singh bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
417450af8d0SKP Singh 			 struct bpf_local_storage_map *smap,
418450af8d0SKP Singh 			 bool cacheit_lockit)
419450af8d0SKP Singh {
420450af8d0SKP Singh 	struct bpf_local_storage_data *sdata;
421450af8d0SKP Singh 	struct bpf_local_storage_elem *selem;
422450af8d0SKP Singh 
423450af8d0SKP Singh 	/* Fast path (cache hit) */
4240fe4b381SKP Singh 	sdata = rcu_dereference_check(local_storage->cache[smap->cache_idx],
4250fe4b381SKP Singh 				      bpf_rcu_lock_held());
426450af8d0SKP Singh 	if (sdata && rcu_access_pointer(sdata->smap) == smap)
427450af8d0SKP Singh 		return sdata;
428450af8d0SKP Singh 
429450af8d0SKP Singh 	/* Slow path (cache miss) */
4300fe4b381SKP Singh 	hlist_for_each_entry_rcu(selem, &local_storage->list, snode,
4310fe4b381SKP Singh 				  rcu_read_lock_trace_held())
432450af8d0SKP Singh 		if (rcu_access_pointer(SDATA(selem)->smap) == smap)
433450af8d0SKP Singh 			break;
434450af8d0SKP Singh 
435450af8d0SKP Singh 	if (!selem)
436450af8d0SKP Singh 		return NULL;
437450af8d0SKP Singh 
438450af8d0SKP Singh 	sdata = SDATA(selem);
439450af8d0SKP Singh 	if (cacheit_lockit) {
440a10787e6SSong Liu 		unsigned long flags;
441a10787e6SSong Liu 
442450af8d0SKP Singh 		/* spinlock is needed to avoid racing with the
443450af8d0SKP Singh 		 * parallel delete.  Otherwise, publishing an already
444450af8d0SKP Singh 		 * deleted sdata to the cache will become a use-after-free
445450af8d0SKP Singh 		 * problem in the next bpf_local_storage_lookup().
446450af8d0SKP Singh 		 */
447a10787e6SSong Liu 		raw_spin_lock_irqsave(&local_storage->lock, flags);
448450af8d0SKP Singh 		if (selem_linked_to_storage(selem))
449450af8d0SKP Singh 			rcu_assign_pointer(local_storage->cache[smap->cache_idx],
450450af8d0SKP Singh 					   sdata);
451a10787e6SSong Liu 		raw_spin_unlock_irqrestore(&local_storage->lock, flags);
452450af8d0SKP Singh 	}
453450af8d0SKP Singh 
454450af8d0SKP Singh 	return sdata;
455450af8d0SKP Singh }
456450af8d0SKP Singh 
457450af8d0SKP Singh static int check_flags(const struct bpf_local_storage_data *old_sdata,
458450af8d0SKP Singh 		       u64 map_flags)
459450af8d0SKP Singh {
460450af8d0SKP Singh 	if (old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
461450af8d0SKP Singh 		/* elem already exists */
462450af8d0SKP Singh 		return -EEXIST;
463450af8d0SKP Singh 
464450af8d0SKP Singh 	if (!old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
465450af8d0SKP Singh 		/* elem doesn't exist, cannot update it */
466450af8d0SKP Singh 		return -ENOENT;
467450af8d0SKP Singh 
468450af8d0SKP Singh 	return 0;
469450af8d0SKP Singh }
470450af8d0SKP Singh 
471450af8d0SKP Singh int bpf_local_storage_alloc(void *owner,
472450af8d0SKP Singh 			    struct bpf_local_storage_map *smap,
473b00fa38aSJoanne Koong 			    struct bpf_local_storage_elem *first_selem,
474b00fa38aSJoanne Koong 			    gfp_t gfp_flags)
475450af8d0SKP Singh {
476450af8d0SKP Singh 	struct bpf_local_storage *prev_storage, *storage;
477450af8d0SKP Singh 	struct bpf_local_storage **owner_storage_ptr;
478450af8d0SKP Singh 	int err;
479450af8d0SKP Singh 
480450af8d0SKP Singh 	err = mem_charge(smap, owner, sizeof(*storage));
481450af8d0SKP Singh 	if (err)
482450af8d0SKP Singh 		return err;
483450af8d0SKP Singh 
484*6ae9d5e9SMartin KaFai Lau 	if (smap->bpf_ma) {
485*6ae9d5e9SMartin KaFai Lau 		migrate_disable();
486*6ae9d5e9SMartin KaFai Lau 		storage = bpf_mem_cache_alloc_flags(&smap->storage_ma, gfp_flags);
487*6ae9d5e9SMartin KaFai Lau 		migrate_enable();
488*6ae9d5e9SMartin KaFai Lau 	} else {
489e9aae8beSRoman Gushchin 		storage = bpf_map_kzalloc(&smap->map, sizeof(*storage),
490b00fa38aSJoanne Koong 					  gfp_flags | __GFP_NOWARN);
491*6ae9d5e9SMartin KaFai Lau 	}
492*6ae9d5e9SMartin KaFai Lau 
493450af8d0SKP Singh 	if (!storage) {
494450af8d0SKP Singh 		err = -ENOMEM;
495450af8d0SKP Singh 		goto uncharge;
496450af8d0SKP Singh 	}
497450af8d0SKP Singh 
498fc6652aaSMartin KaFai Lau 	RCU_INIT_POINTER(storage->smap, smap);
499450af8d0SKP Singh 	INIT_HLIST_HEAD(&storage->list);
500450af8d0SKP Singh 	raw_spin_lock_init(&storage->lock);
501450af8d0SKP Singh 	storage->owner = owner;
502450af8d0SKP Singh 
503450af8d0SKP Singh 	bpf_selem_link_storage_nolock(storage, first_selem);
504450af8d0SKP Singh 	bpf_selem_link_map(smap, first_selem);
505450af8d0SKP Singh 
506450af8d0SKP Singh 	owner_storage_ptr =
507450af8d0SKP Singh 		(struct bpf_local_storage **)owner_storage(smap, owner);
508450af8d0SKP Singh 	/* Publish storage to the owner.
509450af8d0SKP Singh 	 * Instead of using any lock of the kernel object (i.e. owner),
510450af8d0SKP Singh 	 * cmpxchg will work with any kernel object regardless what
511450af8d0SKP Singh 	 * the running context is, bh, irq...etc.
512450af8d0SKP Singh 	 *
513450af8d0SKP Singh 	 * From now on, the owner->storage pointer (e.g. sk->sk_bpf_storage)
514450af8d0SKP Singh 	 * is protected by the storage->lock.  Hence, when freeing
515450af8d0SKP Singh 	 * the owner->storage, the storage->lock must be held before
516450af8d0SKP Singh 	 * setting owner->storage ptr to NULL.
517450af8d0SKP Singh 	 */
518450af8d0SKP Singh 	prev_storage = cmpxchg(owner_storage_ptr, NULL, storage);
519450af8d0SKP Singh 	if (unlikely(prev_storage)) {
520450af8d0SKP Singh 		bpf_selem_unlink_map(first_selem);
521450af8d0SKP Singh 		err = -EAGAIN;
522450af8d0SKP Singh 		goto uncharge;
523450af8d0SKP Singh 
524450af8d0SKP Singh 		/* Note that even first_selem was linked to smap's
525450af8d0SKP Singh 		 * bucket->list, first_selem can be freed immediately
526450af8d0SKP Singh 		 * (instead of kfree_rcu) because
527450af8d0SKP Singh 		 * bpf_local_storage_map_free() does a
5280fe4b381SKP Singh 		 * synchronize_rcu_mult (waiting for both sleepable and
5290fe4b381SKP Singh 		 * normal programs) before walking the bucket->list.
530450af8d0SKP Singh 		 * Hence, no one is accessing selem from the
531450af8d0SKP Singh 		 * bucket->list under rcu_read_lock().
532450af8d0SKP Singh 		 */
533450af8d0SKP Singh 	}
534450af8d0SKP Singh 
535450af8d0SKP Singh 	return 0;
536450af8d0SKP Singh 
537450af8d0SKP Singh uncharge:
538*6ae9d5e9SMartin KaFai Lau 	bpf_local_storage_free(storage, smap, smap->bpf_ma, true);
539450af8d0SKP Singh 	mem_uncharge(smap, owner, sizeof(*storage));
540450af8d0SKP Singh 	return err;
541450af8d0SKP Singh }
542450af8d0SKP Singh 
543450af8d0SKP Singh /* sk cannot be going away because it is linking new elem
544450af8d0SKP Singh  * to sk->sk_bpf_storage. (i.e. sk->sk_refcnt cannot be 0).
545450af8d0SKP Singh  * Otherwise, it will become a leak (and other memory issues
546450af8d0SKP Singh  * during map destruction).
547450af8d0SKP Singh  */
548450af8d0SKP Singh struct bpf_local_storage_data *
549450af8d0SKP Singh bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
550b00fa38aSJoanne Koong 			 void *value, u64 map_flags, gfp_t gfp_flags)
551450af8d0SKP Singh {
552450af8d0SKP Singh 	struct bpf_local_storage_data *old_sdata = NULL;
553b00fa38aSJoanne Koong 	struct bpf_local_storage_elem *selem = NULL;
554450af8d0SKP Singh 	struct bpf_local_storage *local_storage;
555a10787e6SSong Liu 	unsigned long flags;
556450af8d0SKP Singh 	int err;
557450af8d0SKP Singh 
558450af8d0SKP Singh 	/* BPF_EXIST and BPF_NOEXIST cannot be both set */
559450af8d0SKP Singh 	if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST) ||
560450af8d0SKP Singh 	    /* BPF_F_LOCK can only be used in a value with spin_lock */
561450af8d0SKP Singh 	    unlikely((map_flags & BPF_F_LOCK) &&
562db559117SKumar Kartikeya Dwivedi 		     !btf_record_has_field(smap->map.record, BPF_SPIN_LOCK)))
563450af8d0SKP Singh 		return ERR_PTR(-EINVAL);
564450af8d0SKP Singh 
565b00fa38aSJoanne Koong 	if (gfp_flags == GFP_KERNEL && (map_flags & ~BPF_F_LOCK) != BPF_NOEXIST)
566b00fa38aSJoanne Koong 		return ERR_PTR(-EINVAL);
567b00fa38aSJoanne Koong 
5680fe4b381SKP Singh 	local_storage = rcu_dereference_check(*owner_storage(smap, owner),
5690fe4b381SKP Singh 					      bpf_rcu_lock_held());
570450af8d0SKP Singh 	if (!local_storage || hlist_empty(&local_storage->list)) {
571450af8d0SKP Singh 		/* Very first elem for the owner */
572450af8d0SKP Singh 		err = check_flags(NULL, map_flags);
573450af8d0SKP Singh 		if (err)
574450af8d0SKP Singh 			return ERR_PTR(err);
575450af8d0SKP Singh 
576b00fa38aSJoanne Koong 		selem = bpf_selem_alloc(smap, owner, value, true, gfp_flags);
577450af8d0SKP Singh 		if (!selem)
578450af8d0SKP Singh 			return ERR_PTR(-ENOMEM);
579450af8d0SKP Singh 
580b00fa38aSJoanne Koong 		err = bpf_local_storage_alloc(owner, smap, selem, gfp_flags);
581450af8d0SKP Singh 		if (err) {
582c0d63f30SMartin KaFai Lau 			bpf_selem_free(selem, smap, true);
583450af8d0SKP Singh 			mem_uncharge(smap, owner, smap->elem_size);
584450af8d0SKP Singh 			return ERR_PTR(err);
585450af8d0SKP Singh 		}
586450af8d0SKP Singh 
587450af8d0SKP Singh 		return SDATA(selem);
588450af8d0SKP Singh 	}
589450af8d0SKP Singh 
590450af8d0SKP Singh 	if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) {
591450af8d0SKP Singh 		/* Hoping to find an old_sdata to do inline update
592450af8d0SKP Singh 		 * such that it can avoid taking the local_storage->lock
593450af8d0SKP Singh 		 * and changing the lists.
594450af8d0SKP Singh 		 */
595450af8d0SKP Singh 		old_sdata =
596450af8d0SKP Singh 			bpf_local_storage_lookup(local_storage, smap, false);
597450af8d0SKP Singh 		err = check_flags(old_sdata, map_flags);
598450af8d0SKP Singh 		if (err)
599450af8d0SKP Singh 			return ERR_PTR(err);
6000a09a2f9SKumar Kartikeya Dwivedi 		if (old_sdata && selem_linked_to_storage_lockless(SELEM(old_sdata))) {
601450af8d0SKP Singh 			copy_map_value_locked(&smap->map, old_sdata->data,
602450af8d0SKP Singh 					      value, false);
603450af8d0SKP Singh 			return old_sdata;
604450af8d0SKP Singh 		}
605450af8d0SKP Singh 	}
606450af8d0SKP Singh 
607b00fa38aSJoanne Koong 	if (gfp_flags == GFP_KERNEL) {
608b00fa38aSJoanne Koong 		selem = bpf_selem_alloc(smap, owner, value, true, gfp_flags);
609b00fa38aSJoanne Koong 		if (!selem)
610b00fa38aSJoanne Koong 			return ERR_PTR(-ENOMEM);
611b00fa38aSJoanne Koong 	}
612b00fa38aSJoanne Koong 
613a10787e6SSong Liu 	raw_spin_lock_irqsave(&local_storage->lock, flags);
614450af8d0SKP Singh 
615450af8d0SKP Singh 	/* Recheck local_storage->list under local_storage->lock */
616450af8d0SKP Singh 	if (unlikely(hlist_empty(&local_storage->list))) {
617450af8d0SKP Singh 		/* A parallel del is happening and local_storage is going
618450af8d0SKP Singh 		 * away.  It has just been checked before, so very
619450af8d0SKP Singh 		 * unlikely.  Return instead of retry to keep things
620450af8d0SKP Singh 		 * simple.
621450af8d0SKP Singh 		 */
622450af8d0SKP Singh 		err = -EAGAIN;
623450af8d0SKP Singh 		goto unlock_err;
624450af8d0SKP Singh 	}
625450af8d0SKP Singh 
626450af8d0SKP Singh 	old_sdata = bpf_local_storage_lookup(local_storage, smap, false);
627450af8d0SKP Singh 	err = check_flags(old_sdata, map_flags);
628450af8d0SKP Singh 	if (err)
629450af8d0SKP Singh 		goto unlock_err;
630450af8d0SKP Singh 
631450af8d0SKP Singh 	if (old_sdata && (map_flags & BPF_F_LOCK)) {
632450af8d0SKP Singh 		copy_map_value_locked(&smap->map, old_sdata->data, value,
633450af8d0SKP Singh 				      false);
634450af8d0SKP Singh 		selem = SELEM(old_sdata);
635450af8d0SKP Singh 		goto unlock;
636450af8d0SKP Singh 	}
637450af8d0SKP Singh 
638b00fa38aSJoanne Koong 	if (gfp_flags != GFP_KERNEL) {
639450af8d0SKP Singh 		/* local_storage->lock is held.  Hence, we are sure
640450af8d0SKP Singh 		 * we can unlink and uncharge the old_sdata successfully
641450af8d0SKP Singh 		 * later.  Hence, instead of charging the new selem now
642450af8d0SKP Singh 		 * and then uncharge the old selem later (which may cause
643450af8d0SKP Singh 		 * a potential but unnecessary charge failure),  avoid taking
644450af8d0SKP Singh 		 * a charge at all here (the "!old_sdata" check) and the
645450af8d0SKP Singh 		 * old_sdata will not be uncharged later during
646450af8d0SKP Singh 		 * bpf_selem_unlink_storage_nolock().
647450af8d0SKP Singh 		 */
648b00fa38aSJoanne Koong 		selem = bpf_selem_alloc(smap, owner, value, !old_sdata, gfp_flags);
649450af8d0SKP Singh 		if (!selem) {
650450af8d0SKP Singh 			err = -ENOMEM;
651450af8d0SKP Singh 			goto unlock_err;
652450af8d0SKP Singh 		}
653b00fa38aSJoanne Koong 	}
654450af8d0SKP Singh 
655450af8d0SKP Singh 	/* First, link the new selem to the map */
656450af8d0SKP Singh 	bpf_selem_link_map(smap, selem);
657450af8d0SKP Singh 
658450af8d0SKP Singh 	/* Second, link (and publish) the new selem to local_storage */
659450af8d0SKP Singh 	bpf_selem_link_storage_nolock(local_storage, selem);
660450af8d0SKP Singh 
661450af8d0SKP Singh 	/* Third, remove old selem, SELEM(old_sdata) */
662450af8d0SKP Singh 	if (old_sdata) {
663450af8d0SKP Singh 		bpf_selem_unlink_map(SELEM(old_sdata));
664450af8d0SKP Singh 		bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata),
665a47eabf2SMartin KaFai Lau 						false, false);
666450af8d0SKP Singh 	}
667450af8d0SKP Singh 
668450af8d0SKP Singh unlock:
669a10787e6SSong Liu 	raw_spin_unlock_irqrestore(&local_storage->lock, flags);
670450af8d0SKP Singh 	return SDATA(selem);
671450af8d0SKP Singh 
672450af8d0SKP Singh unlock_err:
673a10787e6SSong Liu 	raw_spin_unlock_irqrestore(&local_storage->lock, flags);
674b00fa38aSJoanne Koong 	if (selem) {
675b00fa38aSJoanne Koong 		mem_uncharge(smap, owner, smap->elem_size);
676c0d63f30SMartin KaFai Lau 		bpf_selem_free(selem, smap, true);
677b00fa38aSJoanne Koong 	}
678450af8d0SKP Singh 	return ERR_PTR(err);
679450af8d0SKP Singh }
680450af8d0SKP Singh 
681c83597faSYonghong Song static u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache)
682450af8d0SKP Singh {
683450af8d0SKP Singh 	u64 min_usage = U64_MAX;
684450af8d0SKP Singh 	u16 i, res = 0;
685450af8d0SKP Singh 
686450af8d0SKP Singh 	spin_lock(&cache->idx_lock);
687450af8d0SKP Singh 
688450af8d0SKP Singh 	for (i = 0; i < BPF_LOCAL_STORAGE_CACHE_SIZE; i++) {
689450af8d0SKP Singh 		if (cache->idx_usage_counts[i] < min_usage) {
690450af8d0SKP Singh 			min_usage = cache->idx_usage_counts[i];
691450af8d0SKP Singh 			res = i;
692450af8d0SKP Singh 
693450af8d0SKP Singh 			/* Found a free cache_idx */
694450af8d0SKP Singh 			if (!min_usage)
695450af8d0SKP Singh 				break;
696450af8d0SKP Singh 		}
697450af8d0SKP Singh 	}
698450af8d0SKP Singh 	cache->idx_usage_counts[res]++;
699450af8d0SKP Singh 
700450af8d0SKP Singh 	spin_unlock(&cache->idx_lock);
701450af8d0SKP Singh 
702450af8d0SKP Singh 	return res;
703450af8d0SKP Singh }
704450af8d0SKP Singh 
705c83597faSYonghong Song static void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache,
706450af8d0SKP Singh 					     u16 idx)
707450af8d0SKP Singh {
708450af8d0SKP Singh 	spin_lock(&cache->idx_lock);
709450af8d0SKP Singh 	cache->idx_usage_counts[idx]--;
710450af8d0SKP Singh 	spin_unlock(&cache->idx_lock);
711450af8d0SKP Singh }
712450af8d0SKP Singh 
713c83597faSYonghong Song int bpf_local_storage_map_alloc_check(union bpf_attr *attr)
714c83597faSYonghong Song {
715c83597faSYonghong Song 	if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK ||
716c83597faSYonghong Song 	    !(attr->map_flags & BPF_F_NO_PREALLOC) ||
717c83597faSYonghong Song 	    attr->max_entries ||
718c83597faSYonghong Song 	    attr->key_size != sizeof(int) || !attr->value_size ||
719c83597faSYonghong Song 	    /* Enforce BTF for userspace sk dumping */
720c83597faSYonghong Song 	    !attr->btf_key_type_id || !attr->btf_value_type_id)
721c83597faSYonghong Song 		return -EINVAL;
722c83597faSYonghong Song 
723c83597faSYonghong Song 	if (!bpf_capable())
724c83597faSYonghong Song 		return -EPERM;
725c83597faSYonghong Song 
726c83597faSYonghong Song 	if (attr->value_size > BPF_LOCAL_STORAGE_MAX_VALUE_SIZE)
727c83597faSYonghong Song 		return -E2BIG;
728c83597faSYonghong Song 
729c83597faSYonghong Song 	return 0;
730c83597faSYonghong Song }
731c83597faSYonghong Song 
732c83597faSYonghong Song int bpf_local_storage_map_check_btf(const struct bpf_map *map,
733c83597faSYonghong Song 				    const struct btf *btf,
734c83597faSYonghong Song 				    const struct btf_type *key_type,
735c83597faSYonghong Song 				    const struct btf_type *value_type)
736c83597faSYonghong Song {
737c83597faSYonghong Song 	u32 int_data;
738c83597faSYonghong Song 
739c83597faSYonghong Song 	if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
740c83597faSYonghong Song 		return -EINVAL;
741c83597faSYonghong Song 
742c83597faSYonghong Song 	int_data = *(u32 *)(key_type + 1);
743c83597faSYonghong Song 	if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data))
744c83597faSYonghong Song 		return -EINVAL;
745c83597faSYonghong Song 
746c83597faSYonghong Song 	return 0;
747c83597faSYonghong Song }
748c83597faSYonghong Song 
7492ffcb6fcSMartin KaFai Lau void bpf_local_storage_destroy(struct bpf_local_storage *local_storage)
750450af8d0SKP Singh {
751*6ae9d5e9SMartin KaFai Lau 	struct bpf_local_storage_map *storage_smap;
752450af8d0SKP Singh 	struct bpf_local_storage_elem *selem;
753*6ae9d5e9SMartin KaFai Lau 	bool bpf_ma, free_storage = false;
754c83597faSYonghong Song 	struct hlist_node *n;
7552ffcb6fcSMartin KaFai Lau 	unsigned long flags;
756c83597faSYonghong Song 
757*6ae9d5e9SMartin KaFai Lau 	storage_smap = rcu_dereference_check(local_storage->smap, bpf_rcu_lock_held());
758*6ae9d5e9SMartin KaFai Lau 	bpf_ma = check_storage_bpf_ma(local_storage, storage_smap, NULL);
759*6ae9d5e9SMartin KaFai Lau 
760c83597faSYonghong Song 	/* Neither the bpf_prog nor the bpf_map's syscall
761c83597faSYonghong Song 	 * could be modifying the local_storage->list now.
762c83597faSYonghong Song 	 * Thus, no elem can be added to or deleted from the
763c83597faSYonghong Song 	 * local_storage->list by the bpf_prog or by the bpf_map's syscall.
764c83597faSYonghong Song 	 *
765c83597faSYonghong Song 	 * It is racing with bpf_local_storage_map_free() alone
766c83597faSYonghong Song 	 * when unlinking elem from the local_storage->list and
767c83597faSYonghong Song 	 * the map's bucket->list.
768c83597faSYonghong Song 	 */
7692ffcb6fcSMartin KaFai Lau 	raw_spin_lock_irqsave(&local_storage->lock, flags);
770c83597faSYonghong Song 	hlist_for_each_entry_safe(selem, n, &local_storage->list, snode) {
771c83597faSYonghong Song 		/* Always unlink from map before unlinking from
772c83597faSYonghong Song 		 * local_storage.
773c83597faSYonghong Song 		 */
774c83597faSYonghong Song 		bpf_selem_unlink_map(selem);
775c83597faSYonghong Song 		/* If local_storage list has only one element, the
776c83597faSYonghong Song 		 * bpf_selem_unlink_storage_nolock() will return true.
777c83597faSYonghong Song 		 * Otherwise, it will return false. The current loop iteration
778c83597faSYonghong Song 		 * intends to remove all local storage. So the last iteration
779c83597faSYonghong Song 		 * of the loop will set the free_cgroup_storage to true.
780c83597faSYonghong Song 		 */
781c83597faSYonghong Song 		free_storage = bpf_selem_unlink_storage_nolock(
782a47eabf2SMartin KaFai Lau 			local_storage, selem, false, true);
783c83597faSYonghong Song 	}
7842ffcb6fcSMartin KaFai Lau 	raw_spin_unlock_irqrestore(&local_storage->lock, flags);
785c83597faSYonghong Song 
7862ffcb6fcSMartin KaFai Lau 	if (free_storage)
787*6ae9d5e9SMartin KaFai Lau 		bpf_local_storage_free(local_storage, storage_smap, bpf_ma, true);
788c83597faSYonghong Song }
789c83597faSYonghong Song 
7907490b7f1SYafang Shao u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map)
7917490b7f1SYafang Shao {
7927490b7f1SYafang Shao 	struct bpf_local_storage_map *smap = (struct bpf_local_storage_map *)map;
7937490b7f1SYafang Shao 	u64 usage = sizeof(*smap);
7947490b7f1SYafang Shao 
7957490b7f1SYafang Shao 	/* The dynamically callocated selems are not counted currently. */
7967490b7f1SYafang Shao 	usage += sizeof(*smap->buckets) * (1ULL << smap->bucket_log);
7977490b7f1SYafang Shao 	return usage;
7987490b7f1SYafang Shao }
7997490b7f1SYafang Shao 
80008a7ce38SMartin KaFai Lau /* When bpf_ma == true, the bpf_mem_alloc is used to allocate and free memory.
80108a7ce38SMartin KaFai Lau  * A deadlock free allocator is useful for storage that the bpf prog can easily
80208a7ce38SMartin KaFai Lau  * get a hold of the owner PTR_TO_BTF_ID in any context. eg. bpf_get_current_task_btf.
80308a7ce38SMartin KaFai Lau  * The task and cgroup storage fall into this case. The bpf_mem_alloc reuses
80408a7ce38SMartin KaFai Lau  * memory immediately. To be reuse-immediate safe, the owner destruction
80508a7ce38SMartin KaFai Lau  * code path needs to go through a rcu grace period before calling
80608a7ce38SMartin KaFai Lau  * bpf_local_storage_destroy().
80708a7ce38SMartin KaFai Lau  *
80808a7ce38SMartin KaFai Lau  * When bpf_ma == false, the kmalloc and kfree are used.
80908a7ce38SMartin KaFai Lau  */
810c83597faSYonghong Song struct bpf_map *
811c83597faSYonghong Song bpf_local_storage_map_alloc(union bpf_attr *attr,
81208a7ce38SMartin KaFai Lau 			    struct bpf_local_storage_cache *cache,
81308a7ce38SMartin KaFai Lau 			    bool bpf_ma)
814c83597faSYonghong Song {
815c83597faSYonghong Song 	struct bpf_local_storage_map *smap;
81662827d61SMartin KaFai Lau 	unsigned int i;
81762827d61SMartin KaFai Lau 	u32 nbuckets;
81808a7ce38SMartin KaFai Lau 	int err;
819c83597faSYonghong Song 
82062827d61SMartin KaFai Lau 	smap = bpf_map_area_alloc(sizeof(*smap), NUMA_NO_NODE);
82162827d61SMartin KaFai Lau 	if (!smap)
82262827d61SMartin KaFai Lau 		return ERR_PTR(-ENOMEM);
82362827d61SMartin KaFai Lau 	bpf_map_init_from_attr(&smap->map, attr);
82462827d61SMartin KaFai Lau 
82562827d61SMartin KaFai Lau 	nbuckets = roundup_pow_of_two(num_possible_cpus());
82662827d61SMartin KaFai Lau 	/* Use at least 2 buckets, select_bucket() is undefined behavior with 1 bucket */
82762827d61SMartin KaFai Lau 	nbuckets = max_t(u32, 2, nbuckets);
82862827d61SMartin KaFai Lau 	smap->bucket_log = ilog2(nbuckets);
82962827d61SMartin KaFai Lau 
83062827d61SMartin KaFai Lau 	smap->buckets = bpf_map_kvcalloc(&smap->map, sizeof(*smap->buckets),
83162827d61SMartin KaFai Lau 					 nbuckets, GFP_USER | __GFP_NOWARN);
83262827d61SMartin KaFai Lau 	if (!smap->buckets) {
83308a7ce38SMartin KaFai Lau 		err = -ENOMEM;
83408a7ce38SMartin KaFai Lau 		goto free_smap;
83562827d61SMartin KaFai Lau 	}
83662827d61SMartin KaFai Lau 
83762827d61SMartin KaFai Lau 	for (i = 0; i < nbuckets; i++) {
83862827d61SMartin KaFai Lau 		INIT_HLIST_HEAD(&smap->buckets[i].list);
83962827d61SMartin KaFai Lau 		raw_spin_lock_init(&smap->buckets[i].lock);
84062827d61SMartin KaFai Lau 	}
84162827d61SMartin KaFai Lau 
84262827d61SMartin KaFai Lau 	smap->elem_size = offsetof(struct bpf_local_storage_elem,
84362827d61SMartin KaFai Lau 				   sdata.data[attr->value_size]);
844c83597faSYonghong Song 
84508a7ce38SMartin KaFai Lau 	smap->bpf_ma = bpf_ma;
84608a7ce38SMartin KaFai Lau 	if (bpf_ma) {
84708a7ce38SMartin KaFai Lau 		err = bpf_mem_alloc_init(&smap->selem_ma, smap->elem_size, false);
84808a7ce38SMartin KaFai Lau 		if (err)
84908a7ce38SMartin KaFai Lau 			goto free_smap;
850*6ae9d5e9SMartin KaFai Lau 
851*6ae9d5e9SMartin KaFai Lau 		err = bpf_mem_alloc_init(&smap->storage_ma, sizeof(struct bpf_local_storage), false);
852*6ae9d5e9SMartin KaFai Lau 		if (err) {
853*6ae9d5e9SMartin KaFai Lau 			bpf_mem_alloc_destroy(&smap->selem_ma);
854*6ae9d5e9SMartin KaFai Lau 			goto free_smap;
855*6ae9d5e9SMartin KaFai Lau 		}
85608a7ce38SMartin KaFai Lau 	}
85708a7ce38SMartin KaFai Lau 
858c83597faSYonghong Song 	smap->cache_idx = bpf_local_storage_cache_idx_get(cache);
859c83597faSYonghong Song 	return &smap->map;
86008a7ce38SMartin KaFai Lau 
86108a7ce38SMartin KaFai Lau free_smap:
86208a7ce38SMartin KaFai Lau 	kvfree(smap->buckets);
86308a7ce38SMartin KaFai Lau 	bpf_map_area_free(smap);
86408a7ce38SMartin KaFai Lau 	return ERR_PTR(err);
865c83597faSYonghong Song }
866c83597faSYonghong Song 
867c83597faSYonghong Song void bpf_local_storage_map_free(struct bpf_map *map,
868c83597faSYonghong Song 				struct bpf_local_storage_cache *cache,
869c83597faSYonghong Song 				int __percpu *busy_counter)
870c83597faSYonghong Song {
871450af8d0SKP Singh 	struct bpf_local_storage_map_bucket *b;
872c83597faSYonghong Song 	struct bpf_local_storage_elem *selem;
873c83597faSYonghong Song 	struct bpf_local_storage_map *smap;
874450af8d0SKP Singh 	unsigned int i;
875450af8d0SKP Singh 
876c83597faSYonghong Song 	smap = (struct bpf_local_storage_map *)map;
877c83597faSYonghong Song 	bpf_local_storage_cache_idx_free(cache, smap->cache_idx);
878c83597faSYonghong Song 
879450af8d0SKP Singh 	/* Note that this map might be concurrently cloned from
880450af8d0SKP Singh 	 * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone
881450af8d0SKP Singh 	 * RCU read section to finish before proceeding. New RCU
882450af8d0SKP Singh 	 * read sections should be prevented via bpf_map_inc_not_zero.
883450af8d0SKP Singh 	 */
884450af8d0SKP Singh 	synchronize_rcu();
885450af8d0SKP Singh 
886450af8d0SKP Singh 	/* bpf prog and the userspace can no longer access this map
887450af8d0SKP Singh 	 * now.  No new selem (of this map) can be added
888450af8d0SKP Singh 	 * to the owner->storage or to the map bucket's list.
889450af8d0SKP Singh 	 *
890450af8d0SKP Singh 	 * The elem of this map can be cleaned up here
891450af8d0SKP Singh 	 * or when the storage is freed e.g.
892450af8d0SKP Singh 	 * by bpf_sk_storage_free() during __sk_destruct().
893450af8d0SKP Singh 	 */
894450af8d0SKP Singh 	for (i = 0; i < (1U << smap->bucket_log); i++) {
895450af8d0SKP Singh 		b = &smap->buckets[i];
896450af8d0SKP Singh 
897450af8d0SKP Singh 		rcu_read_lock();
898450af8d0SKP Singh 		/* No one is adding to b->list now */
899450af8d0SKP Singh 		while ((selem = hlist_entry_safe(
900450af8d0SKP Singh 				rcu_dereference_raw(hlist_first_rcu(&b->list)),
901450af8d0SKP Singh 				struct bpf_local_storage_elem, map_node))) {
902bc235cdbSSong Liu 			if (busy_counter) {
903bc235cdbSSong Liu 				migrate_disable();
904197827a0SHou Tao 				this_cpu_inc(*busy_counter);
905bc235cdbSSong Liu 			}
906a47eabf2SMartin KaFai Lau 			bpf_selem_unlink(selem, true);
907bc235cdbSSong Liu 			if (busy_counter) {
908197827a0SHou Tao 				this_cpu_dec(*busy_counter);
909bc235cdbSSong Liu 				migrate_enable();
910bc235cdbSSong Liu 			}
911450af8d0SKP Singh 			cond_resched_rcu();
912450af8d0SKP Singh 		}
913450af8d0SKP Singh 		rcu_read_unlock();
914450af8d0SKP Singh 	}
915450af8d0SKP Singh 
916450af8d0SKP Singh 	/* While freeing the storage we may still need to access the map.
917450af8d0SKP Singh 	 *
918450af8d0SKP Singh 	 * e.g. when bpf_sk_storage_free() has unlinked selem from the map
919450af8d0SKP Singh 	 * which then made the above while((selem = ...)) loop
920450af8d0SKP Singh 	 * exit immediately.
921450af8d0SKP Singh 	 *
922450af8d0SKP Singh 	 * However, while freeing the storage one still needs to access the
923450af8d0SKP Singh 	 * smap->elem_size to do the uncharging in
924450af8d0SKP Singh 	 * bpf_selem_unlink_storage_nolock().
925450af8d0SKP Singh 	 *
926450af8d0SKP Singh 	 * Hence, wait another rcu grace period for the storage to be freed.
927450af8d0SKP Singh 	 */
928450af8d0SKP Singh 	synchronize_rcu();
929450af8d0SKP Singh 
930*6ae9d5e9SMartin KaFai Lau 	if (smap->bpf_ma) {
93108a7ce38SMartin KaFai Lau 		bpf_mem_alloc_destroy(&smap->selem_ma);
932*6ae9d5e9SMartin KaFai Lau 		bpf_mem_alloc_destroy(&smap->storage_ma);
933*6ae9d5e9SMartin KaFai Lau 	}
934450af8d0SKP Singh 	kvfree(smap->buckets);
93573cf09a3SYafang Shao 	bpf_map_area_free(smap);
936450af8d0SKP Singh }
937