xref: /openbmc/linux/kernel/bpf/bpf_local_storage.c (revision 4cbd23cc92c49173e402753cab62b8a7754ed18f)
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 
83e9aae8beSRoman Gushchin 	selem = bpf_map_kzalloc(&smap->map, smap->elem_size,
84b00fa38aSJoanne Koong 				gfp_flags | __GFP_NOWARN);
85450af8d0SKP Singh 	if (selem) {
86450af8d0SKP Singh 		if (value)
87836e49e1SXu Kuohai 			copy_map_value(&smap->map, SDATA(selem)->data, value);
889db44fddSKumar Kartikeya Dwivedi 		/* No need to call check_and_init_map_value as memory is zero init */
89450af8d0SKP Singh 		return selem;
90450af8d0SKP Singh 	}
91450af8d0SKP Singh 
92450af8d0SKP Singh 	if (charge_mem)
93450af8d0SKP Singh 		mem_uncharge(smap, owner, smap->elem_size);
94450af8d0SKP Singh 
95450af8d0SKP Singh 	return NULL;
96450af8d0SKP Singh }
97450af8d0SKP Singh 
98*4cbd23ccSMartin KaFai Lau static void bpf_local_storage_free_rcu(struct rcu_head *rcu)
990fe4b381SKP Singh {
1000fe4b381SKP Singh 	struct bpf_local_storage *local_storage;
1010fe4b381SKP Singh 
102d39d1445SHou Tao 	/* If RCU Tasks Trace grace period implies RCU grace period, do
103d39d1445SHou Tao 	 * kfree(), else do kfree_rcu().
104d39d1445SHou Tao 	 */
1050fe4b381SKP Singh 	local_storage = container_of(rcu, struct bpf_local_storage, rcu);
106d39d1445SHou Tao 	if (rcu_trace_implies_rcu_gp())
107d39d1445SHou Tao 		kfree(local_storage);
108d39d1445SHou Tao 	else
1090fe4b381SKP Singh 		kfree_rcu(local_storage, rcu);
1100fe4b381SKP Singh }
1110fe4b381SKP Singh 
112e768e3c5SKumar Kartikeya Dwivedi static void bpf_selem_free_fields_rcu(struct rcu_head *rcu)
113e768e3c5SKumar Kartikeya Dwivedi {
114e768e3c5SKumar Kartikeya Dwivedi 	struct bpf_local_storage_elem *selem;
115e768e3c5SKumar Kartikeya Dwivedi 	struct bpf_local_storage_map *smap;
116e768e3c5SKumar Kartikeya Dwivedi 
117e768e3c5SKumar Kartikeya Dwivedi 	selem = container_of(rcu, struct bpf_local_storage_elem, rcu);
118e768e3c5SKumar Kartikeya Dwivedi 	/* protected by the rcu_barrier*() */
119e768e3c5SKumar Kartikeya Dwivedi 	smap = rcu_dereference_protected(SDATA(selem)->smap, true);
120e768e3c5SKumar Kartikeya Dwivedi 	bpf_obj_free_fields(smap->map.record, SDATA(selem)->data);
121e768e3c5SKumar Kartikeya Dwivedi 	kfree(selem);
122e768e3c5SKumar Kartikeya Dwivedi }
123e768e3c5SKumar Kartikeya Dwivedi 
124e768e3c5SKumar Kartikeya Dwivedi static void bpf_selem_free_fields_trace_rcu(struct rcu_head *rcu)
125e768e3c5SKumar Kartikeya Dwivedi {
126e768e3c5SKumar Kartikeya Dwivedi 	/* Free directly if Tasks Trace RCU GP also implies RCU GP */
127e768e3c5SKumar Kartikeya Dwivedi 	if (rcu_trace_implies_rcu_gp())
128e768e3c5SKumar Kartikeya Dwivedi 		bpf_selem_free_fields_rcu(rcu);
129e768e3c5SKumar Kartikeya Dwivedi 	else
130e768e3c5SKumar Kartikeya Dwivedi 		call_rcu(rcu, bpf_selem_free_fields_rcu);
131e768e3c5SKumar Kartikeya Dwivedi }
132e768e3c5SKumar Kartikeya Dwivedi 
133e768e3c5SKumar Kartikeya Dwivedi static void bpf_selem_free_trace_rcu(struct rcu_head *rcu)
1340fe4b381SKP Singh {
1350fe4b381SKP Singh 	struct bpf_local_storage_elem *selem;
1360fe4b381SKP Singh 
1370fe4b381SKP Singh 	selem = container_of(rcu, struct bpf_local_storage_elem, rcu);
1389db44fddSKumar Kartikeya Dwivedi 	if (rcu_trace_implies_rcu_gp())
139e768e3c5SKumar Kartikeya Dwivedi 		kfree(selem);
140d39d1445SHou Tao 	else
141e768e3c5SKumar Kartikeya Dwivedi 		kfree_rcu(selem, rcu);
1420fe4b381SKP Singh }
1430fe4b381SKP Singh 
144450af8d0SKP Singh /* local_storage->lock must be held and selem->local_storage == local_storage.
145450af8d0SKP Singh  * The caller must ensure selem->smap is still valid to be
146450af8d0SKP Singh  * dereferenced for its smap->elem_size and smap->cache_idx.
147450af8d0SKP Singh  */
148c83597faSYonghong Song static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage,
149450af8d0SKP Singh 					    struct bpf_local_storage_elem *selem,
150dcf456c9SKP Singh 					    bool uncharge_mem, bool use_trace_rcu)
151450af8d0SKP Singh {
152450af8d0SKP Singh 	struct bpf_local_storage_map *smap;
153450af8d0SKP Singh 	bool free_local_storage;
154e768e3c5SKumar Kartikeya Dwivedi 	struct btf_record *rec;
155450af8d0SKP Singh 	void *owner;
156450af8d0SKP Singh 
1570fe4b381SKP Singh 	smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
158450af8d0SKP Singh 	owner = local_storage->owner;
159450af8d0SKP Singh 
160450af8d0SKP Singh 	/* All uncharging on the owner must be done first.
161450af8d0SKP Singh 	 * The owner may be freed once the last selem is unlinked
162450af8d0SKP Singh 	 * from local_storage.
163450af8d0SKP Singh 	 */
164450af8d0SKP Singh 	if (uncharge_mem)
165450af8d0SKP Singh 		mem_uncharge(smap, owner, smap->elem_size);
166450af8d0SKP Singh 
167450af8d0SKP Singh 	free_local_storage = hlist_is_singular_node(&selem->snode,
168450af8d0SKP Singh 						    &local_storage->list);
169450af8d0SKP Singh 	if (free_local_storage) {
170450af8d0SKP Singh 		mem_uncharge(smap, owner, sizeof(struct bpf_local_storage));
171450af8d0SKP Singh 		local_storage->owner = NULL;
172450af8d0SKP Singh 
173450af8d0SKP Singh 		/* After this RCU_INIT, owner may be freed and cannot be used */
174450af8d0SKP Singh 		RCU_INIT_POINTER(*owner_storage(smap, owner), NULL);
175450af8d0SKP Singh 
176450af8d0SKP Singh 		/* local_storage is not freed now.  local_storage->lock is
177450af8d0SKP Singh 		 * still held and raw_spin_unlock_bh(&local_storage->lock)
178450af8d0SKP Singh 		 * will be done by the caller.
179450af8d0SKP Singh 		 *
180450af8d0SKP Singh 		 * Although the unlock will be done under
181c561d110STom Rix 		 * rcu_read_lock(),  it is more intuitive to
1820fe4b381SKP Singh 		 * read if the freeing of the storage is done
183450af8d0SKP Singh 		 * after the raw_spin_unlock_bh(&local_storage->lock).
184450af8d0SKP Singh 		 *
185450af8d0SKP Singh 		 * Hence, a "bool free_local_storage" is returned
1860fe4b381SKP Singh 		 * to the caller which then calls then frees the storage after
1870fe4b381SKP Singh 		 * all the RCU grace periods have expired.
188450af8d0SKP Singh 		 */
189450af8d0SKP Singh 	}
190450af8d0SKP Singh 	hlist_del_init_rcu(&selem->snode);
191450af8d0SKP Singh 	if (rcu_access_pointer(local_storage->cache[smap->cache_idx]) ==
192450af8d0SKP Singh 	    SDATA(selem))
193450af8d0SKP Singh 		RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL);
194450af8d0SKP Singh 
195e768e3c5SKumar Kartikeya Dwivedi 	/* A different RCU callback is chosen whenever we need to free
196e768e3c5SKumar Kartikeya Dwivedi 	 * additional fields in selem data before freeing selem.
197e768e3c5SKumar Kartikeya Dwivedi 	 * bpf_local_storage_map_free only executes rcu_barrier to wait for RCU
198e768e3c5SKumar Kartikeya Dwivedi 	 * callbacks when it has special fields, hence we can only conditionally
199e768e3c5SKumar Kartikeya Dwivedi 	 * dereference smap, as by this time the map might have already been
200e768e3c5SKumar Kartikeya Dwivedi 	 * freed without waiting for our call_rcu callback if it did not have
201e768e3c5SKumar Kartikeya Dwivedi 	 * any special fields.
202e768e3c5SKumar Kartikeya Dwivedi 	 */
203e768e3c5SKumar Kartikeya Dwivedi 	rec = smap->map.record;
204e768e3c5SKumar Kartikeya Dwivedi 	if (use_trace_rcu) {
205e768e3c5SKumar Kartikeya Dwivedi 		if (!IS_ERR_OR_NULL(rec))
206e768e3c5SKumar Kartikeya Dwivedi 			call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_fields_trace_rcu);
207dcf456c9SKP Singh 		else
208e768e3c5SKumar Kartikeya Dwivedi 			call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_trace_rcu);
209e768e3c5SKumar Kartikeya Dwivedi 	} else {
210e768e3c5SKumar Kartikeya Dwivedi 		if (!IS_ERR_OR_NULL(rec))
211e768e3c5SKumar Kartikeya Dwivedi 			call_rcu(&selem->rcu, bpf_selem_free_fields_rcu);
212e768e3c5SKumar Kartikeya Dwivedi 		else
213e768e3c5SKumar Kartikeya Dwivedi 			kfree_rcu(selem, rcu);
214e768e3c5SKumar Kartikeya Dwivedi 	}
215dcf456c9SKP Singh 
216450af8d0SKP Singh 	return free_local_storage;
217450af8d0SKP Singh }
218450af8d0SKP Singh 
219dcf456c9SKP Singh static void __bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem,
220dcf456c9SKP Singh 				       bool use_trace_rcu)
221450af8d0SKP Singh {
222450af8d0SKP Singh 	struct bpf_local_storage *local_storage;
223450af8d0SKP Singh 	bool free_local_storage = false;
224a10787e6SSong Liu 	unsigned long flags;
225450af8d0SKP Singh 
2260a09a2f9SKumar Kartikeya Dwivedi 	if (unlikely(!selem_linked_to_storage_lockless(selem)))
227450af8d0SKP Singh 		/* selem has already been unlinked from sk */
228450af8d0SKP Singh 		return;
229450af8d0SKP Singh 
2300fe4b381SKP Singh 	local_storage = rcu_dereference_check(selem->local_storage,
2310fe4b381SKP Singh 					      bpf_rcu_lock_held());
232a10787e6SSong Liu 	raw_spin_lock_irqsave(&local_storage->lock, flags);
233450af8d0SKP Singh 	if (likely(selem_linked_to_storage(selem)))
234450af8d0SKP Singh 		free_local_storage = bpf_selem_unlink_storage_nolock(
235dcf456c9SKP Singh 			local_storage, selem, true, use_trace_rcu);
236a10787e6SSong Liu 	raw_spin_unlock_irqrestore(&local_storage->lock, flags);
237450af8d0SKP Singh 
238dcf456c9SKP Singh 	if (free_local_storage) {
239dcf456c9SKP Singh 		if (use_trace_rcu)
2400fe4b381SKP Singh 			call_rcu_tasks_trace(&local_storage->rcu,
2410fe4b381SKP Singh 				     bpf_local_storage_free_rcu);
242dcf456c9SKP Singh 		else
243dcf456c9SKP Singh 			kfree_rcu(local_storage, rcu);
244dcf456c9SKP Singh 	}
245450af8d0SKP Singh }
246450af8d0SKP Singh 
247450af8d0SKP Singh void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
248450af8d0SKP Singh 				   struct bpf_local_storage_elem *selem)
249450af8d0SKP Singh {
250450af8d0SKP Singh 	RCU_INIT_POINTER(selem->local_storage, local_storage);
25170b97111SMartin KaFai Lau 	hlist_add_head_rcu(&selem->snode, &local_storage->list);
252450af8d0SKP Singh }
253450af8d0SKP Singh 
254*4cbd23ccSMartin KaFai Lau static void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
255450af8d0SKP Singh {
256450af8d0SKP Singh 	struct bpf_local_storage_map *smap;
257450af8d0SKP Singh 	struct bpf_local_storage_map_bucket *b;
258a10787e6SSong Liu 	unsigned long flags;
259450af8d0SKP Singh 
2600a09a2f9SKumar Kartikeya Dwivedi 	if (unlikely(!selem_linked_to_map_lockless(selem)))
261450af8d0SKP Singh 		/* selem has already be unlinked from smap */
262450af8d0SKP Singh 		return;
263450af8d0SKP Singh 
2640fe4b381SKP Singh 	smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
265450af8d0SKP Singh 	b = select_bucket(smap, selem);
266a10787e6SSong Liu 	raw_spin_lock_irqsave(&b->lock, flags);
267450af8d0SKP Singh 	if (likely(selem_linked_to_map(selem)))
268450af8d0SKP Singh 		hlist_del_init_rcu(&selem->map_node);
269a10787e6SSong Liu 	raw_spin_unlock_irqrestore(&b->lock, flags);
270450af8d0SKP Singh }
271450af8d0SKP Singh 
272450af8d0SKP Singh void bpf_selem_link_map(struct bpf_local_storage_map *smap,
273450af8d0SKP Singh 			struct bpf_local_storage_elem *selem)
274450af8d0SKP Singh {
275450af8d0SKP Singh 	struct bpf_local_storage_map_bucket *b = select_bucket(smap, selem);
276a10787e6SSong Liu 	unsigned long flags;
277450af8d0SKP Singh 
278a10787e6SSong Liu 	raw_spin_lock_irqsave(&b->lock, flags);
279450af8d0SKP Singh 	RCU_INIT_POINTER(SDATA(selem)->smap, smap);
280450af8d0SKP Singh 	hlist_add_head_rcu(&selem->map_node, &b->list);
281a10787e6SSong Liu 	raw_spin_unlock_irqrestore(&b->lock, flags);
282450af8d0SKP Singh }
283450af8d0SKP Singh 
284dcf456c9SKP Singh void bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool use_trace_rcu)
285450af8d0SKP Singh {
286450af8d0SKP Singh 	/* Always unlink from map before unlinking from local_storage
287450af8d0SKP Singh 	 * because selem will be freed after successfully unlinked from
288450af8d0SKP Singh 	 * the local_storage.
289450af8d0SKP Singh 	 */
290450af8d0SKP Singh 	bpf_selem_unlink_map(selem);
291dcf456c9SKP Singh 	__bpf_selem_unlink_storage(selem, use_trace_rcu);
292450af8d0SKP Singh }
293450af8d0SKP Singh 
294e8b02296SMartin KaFai Lau /* If cacheit_lockit is false, this lookup function is lockless */
295450af8d0SKP Singh struct bpf_local_storage_data *
296450af8d0SKP Singh bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
297450af8d0SKP Singh 			 struct bpf_local_storage_map *smap,
298450af8d0SKP Singh 			 bool cacheit_lockit)
299450af8d0SKP Singh {
300450af8d0SKP Singh 	struct bpf_local_storage_data *sdata;
301450af8d0SKP Singh 	struct bpf_local_storage_elem *selem;
302450af8d0SKP Singh 
303450af8d0SKP Singh 	/* Fast path (cache hit) */
3040fe4b381SKP Singh 	sdata = rcu_dereference_check(local_storage->cache[smap->cache_idx],
3050fe4b381SKP Singh 				      bpf_rcu_lock_held());
306450af8d0SKP Singh 	if (sdata && rcu_access_pointer(sdata->smap) == smap)
307450af8d0SKP Singh 		return sdata;
308450af8d0SKP Singh 
309450af8d0SKP Singh 	/* Slow path (cache miss) */
3100fe4b381SKP Singh 	hlist_for_each_entry_rcu(selem, &local_storage->list, snode,
3110fe4b381SKP Singh 				  rcu_read_lock_trace_held())
312450af8d0SKP Singh 		if (rcu_access_pointer(SDATA(selem)->smap) == smap)
313450af8d0SKP Singh 			break;
314450af8d0SKP Singh 
315450af8d0SKP Singh 	if (!selem)
316450af8d0SKP Singh 		return NULL;
317450af8d0SKP Singh 
318450af8d0SKP Singh 	sdata = SDATA(selem);
319450af8d0SKP Singh 	if (cacheit_lockit) {
320a10787e6SSong Liu 		unsigned long flags;
321a10787e6SSong Liu 
322450af8d0SKP Singh 		/* spinlock is needed to avoid racing with the
323450af8d0SKP Singh 		 * parallel delete.  Otherwise, publishing an already
324450af8d0SKP Singh 		 * deleted sdata to the cache will become a use-after-free
325450af8d0SKP Singh 		 * problem in the next bpf_local_storage_lookup().
326450af8d0SKP Singh 		 */
327a10787e6SSong Liu 		raw_spin_lock_irqsave(&local_storage->lock, flags);
328450af8d0SKP Singh 		if (selem_linked_to_storage(selem))
329450af8d0SKP Singh 			rcu_assign_pointer(local_storage->cache[smap->cache_idx],
330450af8d0SKP Singh 					   sdata);
331a10787e6SSong Liu 		raw_spin_unlock_irqrestore(&local_storage->lock, flags);
332450af8d0SKP Singh 	}
333450af8d0SKP Singh 
334450af8d0SKP Singh 	return sdata;
335450af8d0SKP Singh }
336450af8d0SKP Singh 
337450af8d0SKP Singh static int check_flags(const struct bpf_local_storage_data *old_sdata,
338450af8d0SKP Singh 		       u64 map_flags)
339450af8d0SKP Singh {
340450af8d0SKP Singh 	if (old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
341450af8d0SKP Singh 		/* elem already exists */
342450af8d0SKP Singh 		return -EEXIST;
343450af8d0SKP Singh 
344450af8d0SKP Singh 	if (!old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
345450af8d0SKP Singh 		/* elem doesn't exist, cannot update it */
346450af8d0SKP Singh 		return -ENOENT;
347450af8d0SKP Singh 
348450af8d0SKP Singh 	return 0;
349450af8d0SKP Singh }
350450af8d0SKP Singh 
351450af8d0SKP Singh int bpf_local_storage_alloc(void *owner,
352450af8d0SKP Singh 			    struct bpf_local_storage_map *smap,
353b00fa38aSJoanne Koong 			    struct bpf_local_storage_elem *first_selem,
354b00fa38aSJoanne Koong 			    gfp_t gfp_flags)
355450af8d0SKP Singh {
356450af8d0SKP Singh 	struct bpf_local_storage *prev_storage, *storage;
357450af8d0SKP Singh 	struct bpf_local_storage **owner_storage_ptr;
358450af8d0SKP Singh 	int err;
359450af8d0SKP Singh 
360450af8d0SKP Singh 	err = mem_charge(smap, owner, sizeof(*storage));
361450af8d0SKP Singh 	if (err)
362450af8d0SKP Singh 		return err;
363450af8d0SKP Singh 
364e9aae8beSRoman Gushchin 	storage = bpf_map_kzalloc(&smap->map, sizeof(*storage),
365b00fa38aSJoanne Koong 				  gfp_flags | __GFP_NOWARN);
366450af8d0SKP Singh 	if (!storage) {
367450af8d0SKP Singh 		err = -ENOMEM;
368450af8d0SKP Singh 		goto uncharge;
369450af8d0SKP Singh 	}
370450af8d0SKP Singh 
371450af8d0SKP Singh 	INIT_HLIST_HEAD(&storage->list);
372450af8d0SKP Singh 	raw_spin_lock_init(&storage->lock);
373450af8d0SKP Singh 	storage->owner = owner;
374450af8d0SKP Singh 
375450af8d0SKP Singh 	bpf_selem_link_storage_nolock(storage, first_selem);
376450af8d0SKP Singh 	bpf_selem_link_map(smap, first_selem);
377450af8d0SKP Singh 
378450af8d0SKP Singh 	owner_storage_ptr =
379450af8d0SKP Singh 		(struct bpf_local_storage **)owner_storage(smap, owner);
380450af8d0SKP Singh 	/* Publish storage to the owner.
381450af8d0SKP Singh 	 * Instead of using any lock of the kernel object (i.e. owner),
382450af8d0SKP Singh 	 * cmpxchg will work with any kernel object regardless what
383450af8d0SKP Singh 	 * the running context is, bh, irq...etc.
384450af8d0SKP Singh 	 *
385450af8d0SKP Singh 	 * From now on, the owner->storage pointer (e.g. sk->sk_bpf_storage)
386450af8d0SKP Singh 	 * is protected by the storage->lock.  Hence, when freeing
387450af8d0SKP Singh 	 * the owner->storage, the storage->lock must be held before
388450af8d0SKP Singh 	 * setting owner->storage ptr to NULL.
389450af8d0SKP Singh 	 */
390450af8d0SKP Singh 	prev_storage = cmpxchg(owner_storage_ptr, NULL, storage);
391450af8d0SKP Singh 	if (unlikely(prev_storage)) {
392450af8d0SKP Singh 		bpf_selem_unlink_map(first_selem);
393450af8d0SKP Singh 		err = -EAGAIN;
394450af8d0SKP Singh 		goto uncharge;
395450af8d0SKP Singh 
396450af8d0SKP Singh 		/* Note that even first_selem was linked to smap's
397450af8d0SKP Singh 		 * bucket->list, first_selem can be freed immediately
398450af8d0SKP Singh 		 * (instead of kfree_rcu) because
399450af8d0SKP Singh 		 * bpf_local_storage_map_free() does a
4000fe4b381SKP Singh 		 * synchronize_rcu_mult (waiting for both sleepable and
4010fe4b381SKP Singh 		 * normal programs) before walking the bucket->list.
402450af8d0SKP Singh 		 * Hence, no one is accessing selem from the
403450af8d0SKP Singh 		 * bucket->list under rcu_read_lock().
404450af8d0SKP Singh 		 */
405450af8d0SKP Singh 	}
406450af8d0SKP Singh 
407450af8d0SKP Singh 	return 0;
408450af8d0SKP Singh 
409450af8d0SKP Singh uncharge:
410450af8d0SKP Singh 	kfree(storage);
411450af8d0SKP Singh 	mem_uncharge(smap, owner, sizeof(*storage));
412450af8d0SKP Singh 	return err;
413450af8d0SKP Singh }
414450af8d0SKP Singh 
415450af8d0SKP Singh /* sk cannot be going away because it is linking new elem
416450af8d0SKP Singh  * to sk->sk_bpf_storage. (i.e. sk->sk_refcnt cannot be 0).
417450af8d0SKP Singh  * Otherwise, it will become a leak (and other memory issues
418450af8d0SKP Singh  * during map destruction).
419450af8d0SKP Singh  */
420450af8d0SKP Singh struct bpf_local_storage_data *
421450af8d0SKP Singh bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
422b00fa38aSJoanne Koong 			 void *value, u64 map_flags, gfp_t gfp_flags)
423450af8d0SKP Singh {
424450af8d0SKP Singh 	struct bpf_local_storage_data *old_sdata = NULL;
425b00fa38aSJoanne Koong 	struct bpf_local_storage_elem *selem = NULL;
426450af8d0SKP Singh 	struct bpf_local_storage *local_storage;
427a10787e6SSong Liu 	unsigned long flags;
428450af8d0SKP Singh 	int err;
429450af8d0SKP Singh 
430450af8d0SKP Singh 	/* BPF_EXIST and BPF_NOEXIST cannot be both set */
431450af8d0SKP Singh 	if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST) ||
432450af8d0SKP Singh 	    /* BPF_F_LOCK can only be used in a value with spin_lock */
433450af8d0SKP Singh 	    unlikely((map_flags & BPF_F_LOCK) &&
434db559117SKumar Kartikeya Dwivedi 		     !btf_record_has_field(smap->map.record, BPF_SPIN_LOCK)))
435450af8d0SKP Singh 		return ERR_PTR(-EINVAL);
436450af8d0SKP Singh 
437b00fa38aSJoanne Koong 	if (gfp_flags == GFP_KERNEL && (map_flags & ~BPF_F_LOCK) != BPF_NOEXIST)
438b00fa38aSJoanne Koong 		return ERR_PTR(-EINVAL);
439b00fa38aSJoanne Koong 
4400fe4b381SKP Singh 	local_storage = rcu_dereference_check(*owner_storage(smap, owner),
4410fe4b381SKP Singh 					      bpf_rcu_lock_held());
442450af8d0SKP Singh 	if (!local_storage || hlist_empty(&local_storage->list)) {
443450af8d0SKP Singh 		/* Very first elem for the owner */
444450af8d0SKP Singh 		err = check_flags(NULL, map_flags);
445450af8d0SKP Singh 		if (err)
446450af8d0SKP Singh 			return ERR_PTR(err);
447450af8d0SKP Singh 
448b00fa38aSJoanne Koong 		selem = bpf_selem_alloc(smap, owner, value, true, gfp_flags);
449450af8d0SKP Singh 		if (!selem)
450450af8d0SKP Singh 			return ERR_PTR(-ENOMEM);
451450af8d0SKP Singh 
452b00fa38aSJoanne Koong 		err = bpf_local_storage_alloc(owner, smap, selem, gfp_flags);
453450af8d0SKP Singh 		if (err) {
454450af8d0SKP Singh 			kfree(selem);
455450af8d0SKP Singh 			mem_uncharge(smap, owner, smap->elem_size);
456450af8d0SKP Singh 			return ERR_PTR(err);
457450af8d0SKP Singh 		}
458450af8d0SKP Singh 
459450af8d0SKP Singh 		return SDATA(selem);
460450af8d0SKP Singh 	}
461450af8d0SKP Singh 
462450af8d0SKP Singh 	if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) {
463450af8d0SKP Singh 		/* Hoping to find an old_sdata to do inline update
464450af8d0SKP Singh 		 * such that it can avoid taking the local_storage->lock
465450af8d0SKP Singh 		 * and changing the lists.
466450af8d0SKP Singh 		 */
467450af8d0SKP Singh 		old_sdata =
468450af8d0SKP Singh 			bpf_local_storage_lookup(local_storage, smap, false);
469450af8d0SKP Singh 		err = check_flags(old_sdata, map_flags);
470450af8d0SKP Singh 		if (err)
471450af8d0SKP Singh 			return ERR_PTR(err);
4720a09a2f9SKumar Kartikeya Dwivedi 		if (old_sdata && selem_linked_to_storage_lockless(SELEM(old_sdata))) {
473450af8d0SKP Singh 			copy_map_value_locked(&smap->map, old_sdata->data,
474450af8d0SKP Singh 					      value, false);
475450af8d0SKP Singh 			return old_sdata;
476450af8d0SKP Singh 		}
477450af8d0SKP Singh 	}
478450af8d0SKP Singh 
479b00fa38aSJoanne Koong 	if (gfp_flags == GFP_KERNEL) {
480b00fa38aSJoanne Koong 		selem = bpf_selem_alloc(smap, owner, value, true, gfp_flags);
481b00fa38aSJoanne Koong 		if (!selem)
482b00fa38aSJoanne Koong 			return ERR_PTR(-ENOMEM);
483b00fa38aSJoanne Koong 	}
484b00fa38aSJoanne Koong 
485a10787e6SSong Liu 	raw_spin_lock_irqsave(&local_storage->lock, flags);
486450af8d0SKP Singh 
487450af8d0SKP Singh 	/* Recheck local_storage->list under local_storage->lock */
488450af8d0SKP Singh 	if (unlikely(hlist_empty(&local_storage->list))) {
489450af8d0SKP Singh 		/* A parallel del is happening and local_storage is going
490450af8d0SKP Singh 		 * away.  It has just been checked before, so very
491450af8d0SKP Singh 		 * unlikely.  Return instead of retry to keep things
492450af8d0SKP Singh 		 * simple.
493450af8d0SKP Singh 		 */
494450af8d0SKP Singh 		err = -EAGAIN;
495450af8d0SKP Singh 		goto unlock_err;
496450af8d0SKP Singh 	}
497450af8d0SKP Singh 
498450af8d0SKP Singh 	old_sdata = bpf_local_storage_lookup(local_storage, smap, false);
499450af8d0SKP Singh 	err = check_flags(old_sdata, map_flags);
500450af8d0SKP Singh 	if (err)
501450af8d0SKP Singh 		goto unlock_err;
502450af8d0SKP Singh 
503450af8d0SKP Singh 	if (old_sdata && (map_flags & BPF_F_LOCK)) {
504450af8d0SKP Singh 		copy_map_value_locked(&smap->map, old_sdata->data, value,
505450af8d0SKP Singh 				      false);
506450af8d0SKP Singh 		selem = SELEM(old_sdata);
507450af8d0SKP Singh 		goto unlock;
508450af8d0SKP Singh 	}
509450af8d0SKP Singh 
510b00fa38aSJoanne Koong 	if (gfp_flags != GFP_KERNEL) {
511450af8d0SKP Singh 		/* local_storage->lock is held.  Hence, we are sure
512450af8d0SKP Singh 		 * we can unlink and uncharge the old_sdata successfully
513450af8d0SKP Singh 		 * later.  Hence, instead of charging the new selem now
514450af8d0SKP Singh 		 * and then uncharge the old selem later (which may cause
515450af8d0SKP Singh 		 * a potential but unnecessary charge failure),  avoid taking
516450af8d0SKP Singh 		 * a charge at all here (the "!old_sdata" check) and the
517450af8d0SKP Singh 		 * old_sdata will not be uncharged later during
518450af8d0SKP Singh 		 * bpf_selem_unlink_storage_nolock().
519450af8d0SKP Singh 		 */
520b00fa38aSJoanne Koong 		selem = bpf_selem_alloc(smap, owner, value, !old_sdata, gfp_flags);
521450af8d0SKP Singh 		if (!selem) {
522450af8d0SKP Singh 			err = -ENOMEM;
523450af8d0SKP Singh 			goto unlock_err;
524450af8d0SKP Singh 		}
525b00fa38aSJoanne Koong 	}
526450af8d0SKP Singh 
527450af8d0SKP Singh 	/* First, link the new selem to the map */
528450af8d0SKP Singh 	bpf_selem_link_map(smap, selem);
529450af8d0SKP Singh 
530450af8d0SKP Singh 	/* Second, link (and publish) the new selem to local_storage */
531450af8d0SKP Singh 	bpf_selem_link_storage_nolock(local_storage, selem);
532450af8d0SKP Singh 
533450af8d0SKP Singh 	/* Third, remove old selem, SELEM(old_sdata) */
534450af8d0SKP Singh 	if (old_sdata) {
535450af8d0SKP Singh 		bpf_selem_unlink_map(SELEM(old_sdata));
536450af8d0SKP Singh 		bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata),
537dcf456c9SKP Singh 						false, true);
538450af8d0SKP Singh 	}
539450af8d0SKP Singh 
540450af8d0SKP Singh unlock:
541a10787e6SSong Liu 	raw_spin_unlock_irqrestore(&local_storage->lock, flags);
542450af8d0SKP Singh 	return SDATA(selem);
543450af8d0SKP Singh 
544450af8d0SKP Singh unlock_err:
545a10787e6SSong Liu 	raw_spin_unlock_irqrestore(&local_storage->lock, flags);
546b00fa38aSJoanne Koong 	if (selem) {
547b00fa38aSJoanne Koong 		mem_uncharge(smap, owner, smap->elem_size);
548b00fa38aSJoanne Koong 		kfree(selem);
549b00fa38aSJoanne Koong 	}
550450af8d0SKP Singh 	return ERR_PTR(err);
551450af8d0SKP Singh }
552450af8d0SKP Singh 
553c83597faSYonghong Song static u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache)
554450af8d0SKP Singh {
555450af8d0SKP Singh 	u64 min_usage = U64_MAX;
556450af8d0SKP Singh 	u16 i, res = 0;
557450af8d0SKP Singh 
558450af8d0SKP Singh 	spin_lock(&cache->idx_lock);
559450af8d0SKP Singh 
560450af8d0SKP Singh 	for (i = 0; i < BPF_LOCAL_STORAGE_CACHE_SIZE; i++) {
561450af8d0SKP Singh 		if (cache->idx_usage_counts[i] < min_usage) {
562450af8d0SKP Singh 			min_usage = cache->idx_usage_counts[i];
563450af8d0SKP Singh 			res = i;
564450af8d0SKP Singh 
565450af8d0SKP Singh 			/* Found a free cache_idx */
566450af8d0SKP Singh 			if (!min_usage)
567450af8d0SKP Singh 				break;
568450af8d0SKP Singh 		}
569450af8d0SKP Singh 	}
570450af8d0SKP Singh 	cache->idx_usage_counts[res]++;
571450af8d0SKP Singh 
572450af8d0SKP Singh 	spin_unlock(&cache->idx_lock);
573450af8d0SKP Singh 
574450af8d0SKP Singh 	return res;
575450af8d0SKP Singh }
576450af8d0SKP Singh 
577c83597faSYonghong Song static void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache,
578450af8d0SKP Singh 					     u16 idx)
579450af8d0SKP Singh {
580450af8d0SKP Singh 	spin_lock(&cache->idx_lock);
581450af8d0SKP Singh 	cache->idx_usage_counts[idx]--;
582450af8d0SKP Singh 	spin_unlock(&cache->idx_lock);
583450af8d0SKP Singh }
584450af8d0SKP Singh 
585c83597faSYonghong Song int bpf_local_storage_map_alloc_check(union bpf_attr *attr)
586c83597faSYonghong Song {
587c83597faSYonghong Song 	if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK ||
588c83597faSYonghong Song 	    !(attr->map_flags & BPF_F_NO_PREALLOC) ||
589c83597faSYonghong Song 	    attr->max_entries ||
590c83597faSYonghong Song 	    attr->key_size != sizeof(int) || !attr->value_size ||
591c83597faSYonghong Song 	    /* Enforce BTF for userspace sk dumping */
592c83597faSYonghong Song 	    !attr->btf_key_type_id || !attr->btf_value_type_id)
593c83597faSYonghong Song 		return -EINVAL;
594c83597faSYonghong Song 
595c83597faSYonghong Song 	if (!bpf_capable())
596c83597faSYonghong Song 		return -EPERM;
597c83597faSYonghong Song 
598c83597faSYonghong Song 	if (attr->value_size > BPF_LOCAL_STORAGE_MAX_VALUE_SIZE)
599c83597faSYonghong Song 		return -E2BIG;
600c83597faSYonghong Song 
601c83597faSYonghong Song 	return 0;
602c83597faSYonghong Song }
603c83597faSYonghong Song 
604c83597faSYonghong Song static struct bpf_local_storage_map *__bpf_local_storage_map_alloc(union bpf_attr *attr)
605c83597faSYonghong Song {
606c83597faSYonghong Song 	struct bpf_local_storage_map *smap;
607c83597faSYonghong Song 	unsigned int i;
608c83597faSYonghong Song 	u32 nbuckets;
609c83597faSYonghong Song 
610c83597faSYonghong Song 	smap = bpf_map_area_alloc(sizeof(*smap), NUMA_NO_NODE);
611c83597faSYonghong Song 	if (!smap)
612c83597faSYonghong Song 		return ERR_PTR(-ENOMEM);
613c83597faSYonghong Song 	bpf_map_init_from_attr(&smap->map, attr);
614c83597faSYonghong Song 
615c83597faSYonghong Song 	nbuckets = roundup_pow_of_two(num_possible_cpus());
616c83597faSYonghong Song 	/* Use at least 2 buckets, select_bucket() is undefined behavior with 1 bucket */
617c83597faSYonghong Song 	nbuckets = max_t(u32, 2, nbuckets);
618c83597faSYonghong Song 	smap->bucket_log = ilog2(nbuckets);
619c83597faSYonghong Song 
620ddef81b5SYafang Shao 	smap->buckets = bpf_map_kvcalloc(&smap->map, sizeof(*smap->buckets),
621ddef81b5SYafang Shao 					 nbuckets, GFP_USER | __GFP_NOWARN);
622c83597faSYonghong Song 	if (!smap->buckets) {
623c83597faSYonghong Song 		bpf_map_area_free(smap);
624c83597faSYonghong Song 		return ERR_PTR(-ENOMEM);
625c83597faSYonghong Song 	}
626c83597faSYonghong Song 
627c83597faSYonghong Song 	for (i = 0; i < nbuckets; i++) {
628c83597faSYonghong Song 		INIT_HLIST_HEAD(&smap->buckets[i].list);
629c83597faSYonghong Song 		raw_spin_lock_init(&smap->buckets[i].lock);
630c83597faSYonghong Song 	}
631c83597faSYonghong Song 
632552d42a3SMartin KaFai Lau 	smap->elem_size = offsetof(struct bpf_local_storage_elem,
633552d42a3SMartin KaFai Lau 				   sdata.data[attr->value_size]);
634c83597faSYonghong Song 
635c83597faSYonghong Song 	return smap;
636c83597faSYonghong Song }
637c83597faSYonghong Song 
638c83597faSYonghong Song int bpf_local_storage_map_check_btf(const struct bpf_map *map,
639c83597faSYonghong Song 				    const struct btf *btf,
640c83597faSYonghong Song 				    const struct btf_type *key_type,
641c83597faSYonghong Song 				    const struct btf_type *value_type)
642c83597faSYonghong Song {
643c83597faSYonghong Song 	u32 int_data;
644c83597faSYonghong Song 
645c83597faSYonghong Song 	if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
646c83597faSYonghong Song 		return -EINVAL;
647c83597faSYonghong Song 
648c83597faSYonghong Song 	int_data = *(u32 *)(key_type + 1);
649c83597faSYonghong Song 	if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data))
650c83597faSYonghong Song 		return -EINVAL;
651c83597faSYonghong Song 
652c83597faSYonghong Song 	return 0;
653c83597faSYonghong Song }
654c83597faSYonghong Song 
655c83597faSYonghong Song bool bpf_local_storage_unlink_nolock(struct bpf_local_storage *local_storage)
656450af8d0SKP Singh {
657450af8d0SKP Singh 	struct bpf_local_storage_elem *selem;
658c83597faSYonghong Song 	bool free_storage = false;
659c83597faSYonghong Song 	struct hlist_node *n;
660c83597faSYonghong Song 
661c83597faSYonghong Song 	/* Neither the bpf_prog nor the bpf_map's syscall
662c83597faSYonghong Song 	 * could be modifying the local_storage->list now.
663c83597faSYonghong Song 	 * Thus, no elem can be added to or deleted from the
664c83597faSYonghong Song 	 * local_storage->list by the bpf_prog or by the bpf_map's syscall.
665c83597faSYonghong Song 	 *
666c83597faSYonghong Song 	 * It is racing with bpf_local_storage_map_free() alone
667c83597faSYonghong Song 	 * when unlinking elem from the local_storage->list and
668c83597faSYonghong Song 	 * the map's bucket->list.
669c83597faSYonghong Song 	 */
670c83597faSYonghong Song 	hlist_for_each_entry_safe(selem, n, &local_storage->list, snode) {
671c83597faSYonghong Song 		/* Always unlink from map before unlinking from
672c83597faSYonghong Song 		 * local_storage.
673c83597faSYonghong Song 		 */
674c83597faSYonghong Song 		bpf_selem_unlink_map(selem);
675c83597faSYonghong Song 		/* If local_storage list has only one element, the
676c83597faSYonghong Song 		 * bpf_selem_unlink_storage_nolock() will return true.
677c83597faSYonghong Song 		 * Otherwise, it will return false. The current loop iteration
678c83597faSYonghong Song 		 * intends to remove all local storage. So the last iteration
679c83597faSYonghong Song 		 * of the loop will set the free_cgroup_storage to true.
680c83597faSYonghong Song 		 */
681c83597faSYonghong Song 		free_storage = bpf_selem_unlink_storage_nolock(
682c83597faSYonghong Song 			local_storage, selem, false, false);
683c83597faSYonghong Song 	}
684c83597faSYonghong Song 
685c83597faSYonghong Song 	return free_storage;
686c83597faSYonghong Song }
687c83597faSYonghong Song 
6887490b7f1SYafang Shao u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map)
6897490b7f1SYafang Shao {
6907490b7f1SYafang Shao 	struct bpf_local_storage_map *smap = (struct bpf_local_storage_map *)map;
6917490b7f1SYafang Shao 	u64 usage = sizeof(*smap);
6927490b7f1SYafang Shao 
6937490b7f1SYafang Shao 	/* The dynamically callocated selems are not counted currently. */
6947490b7f1SYafang Shao 	usage += sizeof(*smap->buckets) * (1ULL << smap->bucket_log);
6957490b7f1SYafang Shao 	return usage;
6967490b7f1SYafang Shao }
6977490b7f1SYafang Shao 
698c83597faSYonghong Song struct bpf_map *
699c83597faSYonghong Song bpf_local_storage_map_alloc(union bpf_attr *attr,
700c83597faSYonghong Song 			    struct bpf_local_storage_cache *cache)
701c83597faSYonghong Song {
702c83597faSYonghong Song 	struct bpf_local_storage_map *smap;
703c83597faSYonghong Song 
704c83597faSYonghong Song 	smap = __bpf_local_storage_map_alloc(attr);
705c83597faSYonghong Song 	if (IS_ERR(smap))
706c83597faSYonghong Song 		return ERR_CAST(smap);
707c83597faSYonghong Song 
708c83597faSYonghong Song 	smap->cache_idx = bpf_local_storage_cache_idx_get(cache);
709c83597faSYonghong Song 	return &smap->map;
710c83597faSYonghong Song }
711c83597faSYonghong Song 
712c83597faSYonghong Song void bpf_local_storage_map_free(struct bpf_map *map,
713c83597faSYonghong Song 				struct bpf_local_storage_cache *cache,
714c83597faSYonghong Song 				int __percpu *busy_counter)
715c83597faSYonghong Song {
716450af8d0SKP Singh 	struct bpf_local_storage_map_bucket *b;
717c83597faSYonghong Song 	struct bpf_local_storage_elem *selem;
718c83597faSYonghong Song 	struct bpf_local_storage_map *smap;
719450af8d0SKP Singh 	unsigned int i;
720450af8d0SKP Singh 
721c83597faSYonghong Song 	smap = (struct bpf_local_storage_map *)map;
722c83597faSYonghong Song 	bpf_local_storage_cache_idx_free(cache, smap->cache_idx);
723c83597faSYonghong Song 
724450af8d0SKP Singh 	/* Note that this map might be concurrently cloned from
725450af8d0SKP Singh 	 * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone
726450af8d0SKP Singh 	 * RCU read section to finish before proceeding. New RCU
727450af8d0SKP Singh 	 * read sections should be prevented via bpf_map_inc_not_zero.
728450af8d0SKP Singh 	 */
729450af8d0SKP Singh 	synchronize_rcu();
730450af8d0SKP Singh 
731450af8d0SKP Singh 	/* bpf prog and the userspace can no longer access this map
732450af8d0SKP Singh 	 * now.  No new selem (of this map) can be added
733450af8d0SKP Singh 	 * to the owner->storage or to the map bucket's list.
734450af8d0SKP Singh 	 *
735450af8d0SKP Singh 	 * The elem of this map can be cleaned up here
736450af8d0SKP Singh 	 * or when the storage is freed e.g.
737450af8d0SKP Singh 	 * by bpf_sk_storage_free() during __sk_destruct().
738450af8d0SKP Singh 	 */
739450af8d0SKP Singh 	for (i = 0; i < (1U << smap->bucket_log); i++) {
740450af8d0SKP Singh 		b = &smap->buckets[i];
741450af8d0SKP Singh 
742450af8d0SKP Singh 		rcu_read_lock();
743450af8d0SKP Singh 		/* No one is adding to b->list now */
744450af8d0SKP Singh 		while ((selem = hlist_entry_safe(
745450af8d0SKP Singh 				rcu_dereference_raw(hlist_first_rcu(&b->list)),
746450af8d0SKP Singh 				struct bpf_local_storage_elem, map_node))) {
747bc235cdbSSong Liu 			if (busy_counter) {
748bc235cdbSSong Liu 				migrate_disable();
749197827a0SHou Tao 				this_cpu_inc(*busy_counter);
750bc235cdbSSong Liu 			}
751dcf456c9SKP Singh 			bpf_selem_unlink(selem, false);
752bc235cdbSSong Liu 			if (busy_counter) {
753197827a0SHou Tao 				this_cpu_dec(*busy_counter);
754bc235cdbSSong Liu 				migrate_enable();
755bc235cdbSSong Liu 			}
756450af8d0SKP Singh 			cond_resched_rcu();
757450af8d0SKP Singh 		}
758450af8d0SKP Singh 		rcu_read_unlock();
759450af8d0SKP Singh 	}
760450af8d0SKP Singh 
761450af8d0SKP Singh 	/* While freeing the storage we may still need to access the map.
762450af8d0SKP Singh 	 *
763450af8d0SKP Singh 	 * e.g. when bpf_sk_storage_free() has unlinked selem from the map
764450af8d0SKP Singh 	 * which then made the above while((selem = ...)) loop
765450af8d0SKP Singh 	 * exit immediately.
766450af8d0SKP Singh 	 *
767450af8d0SKP Singh 	 * However, while freeing the storage one still needs to access the
768450af8d0SKP Singh 	 * smap->elem_size to do the uncharging in
769450af8d0SKP Singh 	 * bpf_selem_unlink_storage_nolock().
770450af8d0SKP Singh 	 *
771450af8d0SKP Singh 	 * Hence, wait another rcu grace period for the storage to be freed.
772450af8d0SKP Singh 	 */
773450af8d0SKP Singh 	synchronize_rcu();
774450af8d0SKP Singh 
7759db44fddSKumar Kartikeya Dwivedi 	/* Only delay freeing of smap, buckets are not needed anymore */
776450af8d0SKP Singh 	kvfree(smap->buckets);
7779db44fddSKumar Kartikeya Dwivedi 
7789db44fddSKumar Kartikeya Dwivedi 	/* When local storage has special fields, callbacks for
779e768e3c5SKumar Kartikeya Dwivedi 	 * bpf_selem_free_fields_rcu and bpf_selem_free_fields_trace_rcu will
780e768e3c5SKumar Kartikeya Dwivedi 	 * keep using the map BTF record, we need to execute an RCU barrier to
781e768e3c5SKumar Kartikeya Dwivedi 	 * wait for them as the record will be freed right after our map_free
782e768e3c5SKumar Kartikeya Dwivedi 	 * callback.
7839db44fddSKumar Kartikeya Dwivedi 	 */
7849db44fddSKumar Kartikeya Dwivedi 	if (!IS_ERR_OR_NULL(smap->map.record)) {
7859db44fddSKumar Kartikeya Dwivedi 		rcu_barrier_tasks_trace();
7869db44fddSKumar Kartikeya Dwivedi 		/* We cannot skip rcu_barrier() when rcu_trace_implies_rcu_gp()
7879db44fddSKumar Kartikeya Dwivedi 		 * is true, because while call_rcu invocation is skipped in that
788e768e3c5SKumar Kartikeya Dwivedi 		 * case in bpf_selem_free_fields_trace_rcu (and all local
789e768e3c5SKumar Kartikeya Dwivedi 		 * storage maps pass use_trace_rcu = true), there can be
790e768e3c5SKumar Kartikeya Dwivedi 		 * call_rcu callbacks based on use_trace_rcu = false in the
791e768e3c5SKumar Kartikeya Dwivedi 		 * while ((selem = ...)) loop above or when owner's free path
792e768e3c5SKumar Kartikeya Dwivedi 		 * calls bpf_local_storage_unlink_nolock.
7939db44fddSKumar Kartikeya Dwivedi 		 */
7949db44fddSKumar Kartikeya Dwivedi 		rcu_barrier();
7959db44fddSKumar Kartikeya Dwivedi 	}
79673cf09a3SYafang Shao 	bpf_map_area_free(smap);
797450af8d0SKP Singh }
798