Merge tag 'for-linus-6.4-1' of https://github.com/cminyard/linux-ipmi
[linux-block.git] / kernel / bpf / bpf_local_storage.c
CommitLineData
450af8d0
KS
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2019 Facebook */
3#include <linux/rculist.h>
4#include <linux/list.h>
5#include <linux/hash.h>
6#include <linux/types.h>
7#include <linux/spinlock.h>
8#include <linux/bpf.h>
9#include <linux/btf_ids.h>
10#include <linux/bpf_local_storage.h>
11#include <net/sock.h>
12#include <uapi/linux/sock_diag.h>
13#include <uapi/linux/btf.h>
0fe4b381
KS
14#include <linux/rcupdate.h>
15#include <linux/rcupdate_trace.h>
16#include <linux/rcupdate_wait.h>
450af8d0
KS
17
18#define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE)
19
20static struct bpf_local_storage_map_bucket *
21select_bucket(struct bpf_local_storage_map *smap,
22 struct bpf_local_storage_elem *selem)
23{
24 return &smap->buckets[hash_ptr(selem, smap->bucket_log)];
25}
26
27static int mem_charge(struct bpf_local_storage_map *smap, void *owner, u32 size)
28{
29 struct bpf_map *map = &smap->map;
30
31 if (!map->ops->map_local_storage_charge)
32 return 0;
33
34 return map->ops->map_local_storage_charge(smap, owner, size);
35}
36
37static void mem_uncharge(struct bpf_local_storage_map *smap, void *owner,
38 u32 size)
39{
40 struct bpf_map *map = &smap->map;
41
42 if (map->ops->map_local_storage_uncharge)
43 map->ops->map_local_storage_uncharge(smap, owner, size);
44}
45
46static struct bpf_local_storage __rcu **
47owner_storage(struct bpf_local_storage_map *smap, void *owner)
48{
49 struct bpf_map *map = &smap->map;
50
51 return map->ops->map_owner_storage_ptr(owner);
52}
53
0a09a2f9
KKD
54static bool selem_linked_to_storage_lockless(const struct bpf_local_storage_elem *selem)
55{
56 return !hlist_unhashed_lockless(&selem->snode);
57}
58
450af8d0
KS
59static bool selem_linked_to_storage(const struct bpf_local_storage_elem *selem)
60{
61 return !hlist_unhashed(&selem->snode);
62}
63
0a09a2f9
KKD
64static bool selem_linked_to_map_lockless(const struct bpf_local_storage_elem *selem)
65{
66 return !hlist_unhashed_lockless(&selem->map_node);
67}
68
450af8d0
KS
69static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem)
70{
71 return !hlist_unhashed(&selem->map_node);
72}
73
74struct bpf_local_storage_elem *
75bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
b00fa38a 76 void *value, bool charge_mem, gfp_t gfp_flags)
450af8d0
KS
77{
78 struct bpf_local_storage_elem *selem;
79
80 if (charge_mem && mem_charge(smap, owner, smap->elem_size))
81 return NULL;
82
08a7ce38
MKL
83 if (smap->bpf_ma) {
84 migrate_disable();
85 selem = bpf_mem_cache_alloc_flags(&smap->selem_ma, gfp_flags);
86 migrate_enable();
87 if (selem)
88 /* Keep the original bpf_map_kzalloc behavior
89 * before started using the bpf_mem_cache_alloc.
90 *
91 * No need to use zero_map_value. The bpf_selem_free()
92 * only does bpf_mem_cache_free when there is
93 * no other bpf prog is using the selem.
94 */
95 memset(SDATA(selem)->data, 0, smap->map.value_size);
96 } else {
97 selem = bpf_map_kzalloc(&smap->map, smap->elem_size,
98 gfp_flags | __GFP_NOWARN);
99 }
100
450af8d0
KS
101 if (selem) {
102 if (value)
836e49e1 103 copy_map_value(&smap->map, SDATA(selem)->data, value);
9db44fdd 104 /* No need to call check_and_init_map_value as memory is zero init */
450af8d0
KS
105 return selem;
106 }
107
108 if (charge_mem)
109 mem_uncharge(smap, owner, smap->elem_size);
110
111 return NULL;
112}
113
6ae9d5e9
MKL
114/* rcu tasks trace callback for bpf_ma == false */
115static void __bpf_local_storage_free_trace_rcu(struct rcu_head *rcu)
116{
117 struct bpf_local_storage *local_storage;
118
119 /* If RCU Tasks Trace grace period implies RCU grace period, do
120 * kfree(), else do kfree_rcu().
121 */
122 local_storage = container_of(rcu, struct bpf_local_storage, rcu);
123 if (rcu_trace_implies_rcu_gp())
124 kfree(local_storage);
125 else
126 kfree_rcu(local_storage, rcu);
127}
128
4cbd23cc 129static void bpf_local_storage_free_rcu(struct rcu_head *rcu)
0fe4b381
KS
130{
131 struct bpf_local_storage *local_storage;
132
1288aaa2 133 local_storage = container_of(rcu, struct bpf_local_storage, rcu);
6ae9d5e9 134 bpf_mem_cache_raw_free(local_storage);
1288aaa2
MKL
135}
136
137static void bpf_local_storage_free_trace_rcu(struct rcu_head *rcu)
138{
d39d1445 139 if (rcu_trace_implies_rcu_gp())
1288aaa2 140 bpf_local_storage_free_rcu(rcu);
d39d1445 141 else
1288aaa2 142 call_rcu(rcu, bpf_local_storage_free_rcu);
0fe4b381
KS
143}
144
6ae9d5e9
MKL
145/* Handle bpf_ma == false */
146static void __bpf_local_storage_free(struct bpf_local_storage *local_storage,
147 bool vanilla_rcu)
148{
149 if (vanilla_rcu)
150 kfree_rcu(local_storage, rcu);
151 else
152 call_rcu_tasks_trace(&local_storage->rcu,
153 __bpf_local_storage_free_trace_rcu);
154}
155
7e30a847 156static void bpf_local_storage_free(struct bpf_local_storage *local_storage,
6ae9d5e9
MKL
157 struct bpf_local_storage_map *smap,
158 bool bpf_ma, bool reuse_now)
7e30a847 159{
10fd5f70
AS
160 if (!local_storage)
161 return;
162
6ae9d5e9
MKL
163 if (!bpf_ma) {
164 __bpf_local_storage_free(local_storage, reuse_now);
165 return;
166 }
167
168 if (!reuse_now) {
7e30a847
MKL
169 call_rcu_tasks_trace(&local_storage->rcu,
170 bpf_local_storage_free_trace_rcu);
6ae9d5e9
MKL
171 return;
172 }
173
174 if (smap) {
175 migrate_disable();
176 bpf_mem_cache_free(&smap->storage_ma, local_storage);
177 migrate_enable();
178 } else {
179 /* smap could be NULL if the selem that triggered
180 * this 'local_storage' creation had been long gone.
181 * In this case, directly do call_rcu().
182 */
7e30a847 183 call_rcu(&local_storage->rcu, bpf_local_storage_free_rcu);
6ae9d5e9 184 }
7e30a847
MKL
185}
186
08a7ce38
MKL
187/* rcu tasks trace callback for bpf_ma == false */
188static void __bpf_selem_free_trace_rcu(struct rcu_head *rcu)
189{
190 struct bpf_local_storage_elem *selem;
191
192 selem = container_of(rcu, struct bpf_local_storage_elem, rcu);
193 if (rcu_trace_implies_rcu_gp())
194 kfree(selem);
195 else
196 kfree_rcu(selem, rcu);
197}
198
199/* Handle bpf_ma == false */
200static void __bpf_selem_free(struct bpf_local_storage_elem *selem,
201 bool vanilla_rcu)
202{
203 if (vanilla_rcu)
204 kfree_rcu(selem, rcu);
205 else
206 call_rcu_tasks_trace(&selem->rcu, __bpf_selem_free_trace_rcu);
207}
208
f8ccf30c 209static void bpf_selem_free_rcu(struct rcu_head *rcu)
e768e3c5
KKD
210{
211 struct bpf_local_storage_elem *selem;
212
213 selem = container_of(rcu, struct bpf_local_storage_elem, rcu);
08a7ce38 214 bpf_mem_cache_raw_free(selem);
f8ccf30c
MKL
215}
216
217static void bpf_selem_free_trace_rcu(struct rcu_head *rcu)
218{
e768e3c5 219 if (rcu_trace_implies_rcu_gp())
f8ccf30c 220 bpf_selem_free_rcu(rcu);
d39d1445 221 else
f8ccf30c 222 call_rcu(rcu, bpf_selem_free_rcu);
0fe4b381
KS
223}
224
c0d63f30
MKL
225void bpf_selem_free(struct bpf_local_storage_elem *selem,
226 struct bpf_local_storage_map *smap,
227 bool reuse_now)
228{
229 bpf_obj_free_fields(smap->map.record, SDATA(selem)->data);
08a7ce38
MKL
230
231 if (!smap->bpf_ma) {
232 __bpf_selem_free(selem, reuse_now);
233 return;
234 }
235
236 if (!reuse_now) {
c0d63f30 237 call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_trace_rcu);
08a7ce38
MKL
238 } else {
239 /* Instead of using the vanilla call_rcu(),
240 * bpf_mem_cache_free will be able to reuse selem
241 * immediately.
242 */
243 migrate_disable();
244 bpf_mem_cache_free(&smap->selem_ma, selem);
245 migrate_enable();
246 }
c0d63f30
MKL
247}
248
450af8d0
KS
249/* local_storage->lock must be held and selem->local_storage == local_storage.
250 * The caller must ensure selem->smap is still valid to be
251 * dereferenced for its smap->elem_size and smap->cache_idx.
252 */
c83597fa
YS
253static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage,
254 struct bpf_local_storage_elem *selem,
a47eabf2 255 bool uncharge_mem, bool reuse_now)
450af8d0
KS
256{
257 struct bpf_local_storage_map *smap;
258 bool free_local_storage;
259 void *owner;
260
0fe4b381 261 smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
450af8d0
KS
262 owner = local_storage->owner;
263
264 /* All uncharging on the owner must be done first.
265 * The owner may be freed once the last selem is unlinked
266 * from local_storage.
267 */
268 if (uncharge_mem)
269 mem_uncharge(smap, owner, smap->elem_size);
270
271 free_local_storage = hlist_is_singular_node(&selem->snode,
272 &local_storage->list);
273 if (free_local_storage) {
274 mem_uncharge(smap, owner, sizeof(struct bpf_local_storage));
275 local_storage->owner = NULL;
276
277 /* After this RCU_INIT, owner may be freed and cannot be used */
278 RCU_INIT_POINTER(*owner_storage(smap, owner), NULL);
279
280 /* local_storage is not freed now. local_storage->lock is
281 * still held and raw_spin_unlock_bh(&local_storage->lock)
282 * will be done by the caller.
283 *
284 * Although the unlock will be done under
c561d110 285 * rcu_read_lock(), it is more intuitive to
0fe4b381 286 * read if the freeing of the storage is done
450af8d0
KS
287 * after the raw_spin_unlock_bh(&local_storage->lock).
288 *
289 * Hence, a "bool free_local_storage" is returned
0fe4b381
KS
290 * to the caller which then calls then frees the storage after
291 * all the RCU grace periods have expired.
450af8d0
KS
292 */
293 }
294 hlist_del_init_rcu(&selem->snode);
295 if (rcu_access_pointer(local_storage->cache[smap->cache_idx]) ==
296 SDATA(selem))
297 RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL);
298
c0d63f30 299 bpf_selem_free(selem, smap, reuse_now);
dcf456c9 300
fc6652aa
MKL
301 if (rcu_access_pointer(local_storage->smap) == smap)
302 RCU_INIT_POINTER(local_storage->smap, NULL);
303
450af8d0
KS
304 return free_local_storage;
305}
306
6ae9d5e9
MKL
307static bool check_storage_bpf_ma(struct bpf_local_storage *local_storage,
308 struct bpf_local_storage_map *storage_smap,
309 struct bpf_local_storage_elem *selem)
310{
311
312 struct bpf_local_storage_map *selem_smap;
313
314 /* local_storage->smap may be NULL. If it is, get the bpf_ma
315 * from any selem in the local_storage->list. The bpf_ma of all
316 * local_storage and selem should have the same value
317 * for the same map type.
318 *
319 * If the local_storage->list is already empty, the caller will not
320 * care about the bpf_ma value also because the caller is not
321 * responsibile to free the local_storage.
322 */
323
324 if (storage_smap)
325 return storage_smap->bpf_ma;
326
327 if (!selem) {
328 struct hlist_node *n;
329
330 n = rcu_dereference_check(hlist_first_rcu(&local_storage->list),
331 bpf_rcu_lock_held());
332 if (!n)
333 return false;
334
335 selem = hlist_entry(n, struct bpf_local_storage_elem, snode);
336 }
337 selem_smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
338
339 return selem_smap->bpf_ma;
340}
341
121f31f3 342static void bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem,
a47eabf2 343 bool reuse_now)
450af8d0 344{
6ae9d5e9 345 struct bpf_local_storage_map *storage_smap;
450af8d0 346 struct bpf_local_storage *local_storage;
6ae9d5e9 347 bool bpf_ma, free_local_storage = false;
a10787e6 348 unsigned long flags;
450af8d0 349
0a09a2f9 350 if (unlikely(!selem_linked_to_storage_lockless(selem)))
450af8d0
KS
351 /* selem has already been unlinked from sk */
352 return;
353
0fe4b381
KS
354 local_storage = rcu_dereference_check(selem->local_storage,
355 bpf_rcu_lock_held());
6ae9d5e9
MKL
356 storage_smap = rcu_dereference_check(local_storage->smap,
357 bpf_rcu_lock_held());
358 bpf_ma = check_storage_bpf_ma(local_storage, storage_smap, selem);
359
a10787e6 360 raw_spin_lock_irqsave(&local_storage->lock, flags);
450af8d0
KS
361 if (likely(selem_linked_to_storage(selem)))
362 free_local_storage = bpf_selem_unlink_storage_nolock(
a47eabf2 363 local_storage, selem, true, reuse_now);
a10787e6 364 raw_spin_unlock_irqrestore(&local_storage->lock, flags);
450af8d0 365
7e30a847 366 if (free_local_storage)
6ae9d5e9 367 bpf_local_storage_free(local_storage, storage_smap, bpf_ma, reuse_now);
450af8d0
KS
368}
369
370void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
371 struct bpf_local_storage_elem *selem)
372{
373 RCU_INIT_POINTER(selem->local_storage, local_storage);
70b97111 374 hlist_add_head_rcu(&selem->snode, &local_storage->list);
450af8d0
KS
375}
376
4cbd23cc 377static void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
450af8d0
KS
378{
379 struct bpf_local_storage_map *smap;
380 struct bpf_local_storage_map_bucket *b;
a10787e6 381 unsigned long flags;
450af8d0 382
0a09a2f9 383 if (unlikely(!selem_linked_to_map_lockless(selem)))
450af8d0
KS
384 /* selem has already be unlinked from smap */
385 return;
386
0fe4b381 387 smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
450af8d0 388 b = select_bucket(smap, selem);
a10787e6 389 raw_spin_lock_irqsave(&b->lock, flags);
450af8d0
KS
390 if (likely(selem_linked_to_map(selem)))
391 hlist_del_init_rcu(&selem->map_node);
a10787e6 392 raw_spin_unlock_irqrestore(&b->lock, flags);
450af8d0
KS
393}
394
395void bpf_selem_link_map(struct bpf_local_storage_map *smap,
396 struct bpf_local_storage_elem *selem)
397{
398 struct bpf_local_storage_map_bucket *b = select_bucket(smap, selem);
a10787e6 399 unsigned long flags;
450af8d0 400
a10787e6 401 raw_spin_lock_irqsave(&b->lock, flags);
450af8d0
KS
402 RCU_INIT_POINTER(SDATA(selem)->smap, smap);
403 hlist_add_head_rcu(&selem->map_node, &b->list);
a10787e6 404 raw_spin_unlock_irqrestore(&b->lock, flags);
450af8d0
KS
405}
406
a47eabf2 407void bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool reuse_now)
450af8d0
KS
408{
409 /* Always unlink from map before unlinking from local_storage
410 * because selem will be freed after successfully unlinked from
411 * the local_storage.
412 */
413 bpf_selem_unlink_map(selem);
a47eabf2 414 bpf_selem_unlink_storage(selem, reuse_now);
450af8d0
KS
415}
416
e8b02296 417/* If cacheit_lockit is false, this lookup function is lockless */
450af8d0
KS
418struct bpf_local_storage_data *
419bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
420 struct bpf_local_storage_map *smap,
421 bool cacheit_lockit)
422{
423 struct bpf_local_storage_data *sdata;
424 struct bpf_local_storage_elem *selem;
425
426 /* Fast path (cache hit) */
0fe4b381
KS
427 sdata = rcu_dereference_check(local_storage->cache[smap->cache_idx],
428 bpf_rcu_lock_held());
450af8d0
KS
429 if (sdata && rcu_access_pointer(sdata->smap) == smap)
430 return sdata;
431
432 /* Slow path (cache miss) */
0fe4b381
KS
433 hlist_for_each_entry_rcu(selem, &local_storage->list, snode,
434 rcu_read_lock_trace_held())
450af8d0
KS
435 if (rcu_access_pointer(SDATA(selem)->smap) == smap)
436 break;
437
438 if (!selem)
439 return NULL;
440
441 sdata = SDATA(selem);
442 if (cacheit_lockit) {
a10787e6
SL
443 unsigned long flags;
444
450af8d0
KS
445 /* spinlock is needed to avoid racing with the
446 * parallel delete. Otherwise, publishing an already
447 * deleted sdata to the cache will become a use-after-free
448 * problem in the next bpf_local_storage_lookup().
449 */
a10787e6 450 raw_spin_lock_irqsave(&local_storage->lock, flags);
450af8d0
KS
451 if (selem_linked_to_storage(selem))
452 rcu_assign_pointer(local_storage->cache[smap->cache_idx],
453 sdata);
a10787e6 454 raw_spin_unlock_irqrestore(&local_storage->lock, flags);
450af8d0
KS
455 }
456
457 return sdata;
458}
459
460static int check_flags(const struct bpf_local_storage_data *old_sdata,
461 u64 map_flags)
462{
463 if (old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
464 /* elem already exists */
465 return -EEXIST;
466
467 if (!old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
468 /* elem doesn't exist, cannot update it */
469 return -ENOENT;
470
471 return 0;
472}
473
474int bpf_local_storage_alloc(void *owner,
475 struct bpf_local_storage_map *smap,
b00fa38a
JK
476 struct bpf_local_storage_elem *first_selem,
477 gfp_t gfp_flags)
450af8d0
KS
478{
479 struct bpf_local_storage *prev_storage, *storage;
480 struct bpf_local_storage **owner_storage_ptr;
481 int err;
482
483 err = mem_charge(smap, owner, sizeof(*storage));
484 if (err)
485 return err;
486
6ae9d5e9
MKL
487 if (smap->bpf_ma) {
488 migrate_disable();
489 storage = bpf_mem_cache_alloc_flags(&smap->storage_ma, gfp_flags);
490 migrate_enable();
491 } else {
492 storage = bpf_map_kzalloc(&smap->map, sizeof(*storage),
493 gfp_flags | __GFP_NOWARN);
494 }
495
450af8d0
KS
496 if (!storage) {
497 err = -ENOMEM;
498 goto uncharge;
499 }
500
fc6652aa 501 RCU_INIT_POINTER(storage->smap, smap);
450af8d0
KS
502 INIT_HLIST_HEAD(&storage->list);
503 raw_spin_lock_init(&storage->lock);
504 storage->owner = owner;
505
506 bpf_selem_link_storage_nolock(storage, first_selem);
507 bpf_selem_link_map(smap, first_selem);
508
509 owner_storage_ptr =
510 (struct bpf_local_storage **)owner_storage(smap, owner);
511 /* Publish storage to the owner.
512 * Instead of using any lock of the kernel object (i.e. owner),
513 * cmpxchg will work with any kernel object regardless what
514 * the running context is, bh, irq...etc.
515 *
516 * From now on, the owner->storage pointer (e.g. sk->sk_bpf_storage)
517 * is protected by the storage->lock. Hence, when freeing
518 * the owner->storage, the storage->lock must be held before
519 * setting owner->storage ptr to NULL.
520 */
521 prev_storage = cmpxchg(owner_storage_ptr, NULL, storage);
522 if (unlikely(prev_storage)) {
523 bpf_selem_unlink_map(first_selem);
524 err = -EAGAIN;
525 goto uncharge;
526
527 /* Note that even first_selem was linked to smap's
528 * bucket->list, first_selem can be freed immediately
529 * (instead of kfree_rcu) because
530 * bpf_local_storage_map_free() does a
0fe4b381
KS
531 * synchronize_rcu_mult (waiting for both sleepable and
532 * normal programs) before walking the bucket->list.
450af8d0
KS
533 * Hence, no one is accessing selem from the
534 * bucket->list under rcu_read_lock().
535 */
536 }
537
538 return 0;
539
540uncharge:
6ae9d5e9 541 bpf_local_storage_free(storage, smap, smap->bpf_ma, true);
450af8d0
KS
542 mem_uncharge(smap, owner, sizeof(*storage));
543 return err;
544}
545
546/* sk cannot be going away because it is linking new elem
547 * to sk->sk_bpf_storage. (i.e. sk->sk_refcnt cannot be 0).
548 * Otherwise, it will become a leak (and other memory issues
549 * during map destruction).
550 */
551struct bpf_local_storage_data *
552bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
b00fa38a 553 void *value, u64 map_flags, gfp_t gfp_flags)
450af8d0
KS
554{
555 struct bpf_local_storage_data *old_sdata = NULL;
b00fa38a 556 struct bpf_local_storage_elem *selem = NULL;
450af8d0 557 struct bpf_local_storage *local_storage;
a10787e6 558 unsigned long flags;
450af8d0
KS
559 int err;
560
561 /* BPF_EXIST and BPF_NOEXIST cannot be both set */
562 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST) ||
563 /* BPF_F_LOCK can only be used in a value with spin_lock */
564 unlikely((map_flags & BPF_F_LOCK) &&
db559117 565 !btf_record_has_field(smap->map.record, BPF_SPIN_LOCK)))
450af8d0
KS
566 return ERR_PTR(-EINVAL);
567
b00fa38a
JK
568 if (gfp_flags == GFP_KERNEL && (map_flags & ~BPF_F_LOCK) != BPF_NOEXIST)
569 return ERR_PTR(-EINVAL);
570
0fe4b381
KS
571 local_storage = rcu_dereference_check(*owner_storage(smap, owner),
572 bpf_rcu_lock_held());
450af8d0
KS
573 if (!local_storage || hlist_empty(&local_storage->list)) {
574 /* Very first elem for the owner */
575 err = check_flags(NULL, map_flags);
576 if (err)
577 return ERR_PTR(err);
578
b00fa38a 579 selem = bpf_selem_alloc(smap, owner, value, true, gfp_flags);
450af8d0
KS
580 if (!selem)
581 return ERR_PTR(-ENOMEM);
582
b00fa38a 583 err = bpf_local_storage_alloc(owner, smap, selem, gfp_flags);
450af8d0 584 if (err) {
c0d63f30 585 bpf_selem_free(selem, smap, true);
450af8d0
KS
586 mem_uncharge(smap, owner, smap->elem_size);
587 return ERR_PTR(err);
588 }
589
590 return SDATA(selem);
591 }
592
593 if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) {
594 /* Hoping to find an old_sdata to do inline update
595 * such that it can avoid taking the local_storage->lock
596 * and changing the lists.
597 */
598 old_sdata =
599 bpf_local_storage_lookup(local_storage, smap, false);
600 err = check_flags(old_sdata, map_flags);
601 if (err)
602 return ERR_PTR(err);
0a09a2f9 603 if (old_sdata && selem_linked_to_storage_lockless(SELEM(old_sdata))) {
450af8d0
KS
604 copy_map_value_locked(&smap->map, old_sdata->data,
605 value, false);
606 return old_sdata;
607 }
608 }
609
b00fa38a
JK
610 if (gfp_flags == GFP_KERNEL) {
611 selem = bpf_selem_alloc(smap, owner, value, true, gfp_flags);
612 if (!selem)
613 return ERR_PTR(-ENOMEM);
614 }
615
a10787e6 616 raw_spin_lock_irqsave(&local_storage->lock, flags);
450af8d0
KS
617
618 /* Recheck local_storage->list under local_storage->lock */
619 if (unlikely(hlist_empty(&local_storage->list))) {
620 /* A parallel del is happening and local_storage is going
621 * away. It has just been checked before, so very
622 * unlikely. Return instead of retry to keep things
623 * simple.
624 */
625 err = -EAGAIN;
626 goto unlock_err;
627 }
628
629 old_sdata = bpf_local_storage_lookup(local_storage, smap, false);
630 err = check_flags(old_sdata, map_flags);
631 if (err)
632 goto unlock_err;
633
634 if (old_sdata && (map_flags & BPF_F_LOCK)) {
635 copy_map_value_locked(&smap->map, old_sdata->data, value,
636 false);
637 selem = SELEM(old_sdata);
638 goto unlock;
639 }
640
b00fa38a
JK
641 if (gfp_flags != GFP_KERNEL) {
642 /* local_storage->lock is held. Hence, we are sure
643 * we can unlink and uncharge the old_sdata successfully
644 * later. Hence, instead of charging the new selem now
645 * and then uncharge the old selem later (which may cause
646 * a potential but unnecessary charge failure), avoid taking
647 * a charge at all here (the "!old_sdata" check) and the
648 * old_sdata will not be uncharged later during
649 * bpf_selem_unlink_storage_nolock().
650 */
651 selem = bpf_selem_alloc(smap, owner, value, !old_sdata, gfp_flags);
652 if (!selem) {
653 err = -ENOMEM;
654 goto unlock_err;
655 }
450af8d0
KS
656 }
657
658 /* First, link the new selem to the map */
659 bpf_selem_link_map(smap, selem);
660
661 /* Second, link (and publish) the new selem to local_storage */
662 bpf_selem_link_storage_nolock(local_storage, selem);
663
664 /* Third, remove old selem, SELEM(old_sdata) */
665 if (old_sdata) {
666 bpf_selem_unlink_map(SELEM(old_sdata));
667 bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata),
a47eabf2 668 false, false);
450af8d0
KS
669 }
670
671unlock:
a10787e6 672 raw_spin_unlock_irqrestore(&local_storage->lock, flags);
450af8d0
KS
673 return SDATA(selem);
674
675unlock_err:
a10787e6 676 raw_spin_unlock_irqrestore(&local_storage->lock, flags);
b00fa38a
JK
677 if (selem) {
678 mem_uncharge(smap, owner, smap->elem_size);
c0d63f30 679 bpf_selem_free(selem, smap, true);
b00fa38a 680 }
450af8d0
KS
681 return ERR_PTR(err);
682}
683
c83597fa 684static u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache)
450af8d0
KS
685{
686 u64 min_usage = U64_MAX;
687 u16 i, res = 0;
688
689 spin_lock(&cache->idx_lock);
690
691 for (i = 0; i < BPF_LOCAL_STORAGE_CACHE_SIZE; i++) {
692 if (cache->idx_usage_counts[i] < min_usage) {
693 min_usage = cache->idx_usage_counts[i];
694 res = i;
695
696 /* Found a free cache_idx */
697 if (!min_usage)
698 break;
699 }
700 }
701 cache->idx_usage_counts[res]++;
702
703 spin_unlock(&cache->idx_lock);
704
705 return res;
706}
707
c83597fa
YS
708static void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache,
709 u16 idx)
450af8d0
KS
710{
711 spin_lock(&cache->idx_lock);
712 cache->idx_usage_counts[idx]--;
713 spin_unlock(&cache->idx_lock);
714}
715
450af8d0
KS
716int bpf_local_storage_map_alloc_check(union bpf_attr *attr)
717{
718 if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK ||
719 !(attr->map_flags & BPF_F_NO_PREALLOC) ||
720 attr->max_entries ||
721 attr->key_size != sizeof(int) || !attr->value_size ||
722 /* Enforce BTF for userspace sk dumping */
723 !attr->btf_key_type_id || !attr->btf_value_type_id)
724 return -EINVAL;
725
726 if (!bpf_capable())
727 return -EPERM;
728
729 if (attr->value_size > BPF_LOCAL_STORAGE_MAX_VALUE_SIZE)
730 return -E2BIG;
731
732 return 0;
733}
734
450af8d0
KS
735int bpf_local_storage_map_check_btf(const struct bpf_map *map,
736 const struct btf *btf,
737 const struct btf_type *key_type,
738 const struct btf_type *value_type)
739{
740 u32 int_data;
741
742 if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
743 return -EINVAL;
744
745 int_data = *(u32 *)(key_type + 1);
746 if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data))
747 return -EINVAL;
748
749 return 0;
750}
c83597fa 751
2ffcb6fc 752void bpf_local_storage_destroy(struct bpf_local_storage *local_storage)
c83597fa 753{
6ae9d5e9 754 struct bpf_local_storage_map *storage_smap;
c83597fa 755 struct bpf_local_storage_elem *selem;
6ae9d5e9 756 bool bpf_ma, free_storage = false;
c83597fa 757 struct hlist_node *n;
2ffcb6fc 758 unsigned long flags;
c83597fa 759
6ae9d5e9
MKL
760 storage_smap = rcu_dereference_check(local_storage->smap, bpf_rcu_lock_held());
761 bpf_ma = check_storage_bpf_ma(local_storage, storage_smap, NULL);
762
c83597fa
YS
763 /* Neither the bpf_prog nor the bpf_map's syscall
764 * could be modifying the local_storage->list now.
765 * Thus, no elem can be added to or deleted from the
766 * local_storage->list by the bpf_prog or by the bpf_map's syscall.
767 *
768 * It is racing with bpf_local_storage_map_free() alone
769 * when unlinking elem from the local_storage->list and
770 * the map's bucket->list.
771 */
2ffcb6fc 772 raw_spin_lock_irqsave(&local_storage->lock, flags);
c83597fa
YS
773 hlist_for_each_entry_safe(selem, n, &local_storage->list, snode) {
774 /* Always unlink from map before unlinking from
775 * local_storage.
776 */
777 bpf_selem_unlink_map(selem);
778 /* If local_storage list has only one element, the
779 * bpf_selem_unlink_storage_nolock() will return true.
780 * Otherwise, it will return false. The current loop iteration
781 * intends to remove all local storage. So the last iteration
782 * of the loop will set the free_cgroup_storage to true.
783 */
784 free_storage = bpf_selem_unlink_storage_nolock(
a47eabf2 785 local_storage, selem, false, true);
c83597fa 786 }
2ffcb6fc 787 raw_spin_unlock_irqrestore(&local_storage->lock, flags);
c83597fa 788
2ffcb6fc 789 if (free_storage)
6ae9d5e9 790 bpf_local_storage_free(local_storage, storage_smap, bpf_ma, true);
c83597fa
YS
791}
792
7490b7f1
YS
793u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map)
794{
795 struct bpf_local_storage_map *smap = (struct bpf_local_storage_map *)map;
796 u64 usage = sizeof(*smap);
797
798 /* The dynamically callocated selems are not counted currently. */
799 usage += sizeof(*smap->buckets) * (1ULL << smap->bucket_log);
800 return usage;
801}
802
08a7ce38
MKL
803/* When bpf_ma == true, the bpf_mem_alloc is used to allocate and free memory.
804 * A deadlock free allocator is useful for storage that the bpf prog can easily
805 * get a hold of the owner PTR_TO_BTF_ID in any context. eg. bpf_get_current_task_btf.
806 * The task and cgroup storage fall into this case. The bpf_mem_alloc reuses
807 * memory immediately. To be reuse-immediate safe, the owner destruction
808 * code path needs to go through a rcu grace period before calling
809 * bpf_local_storage_destroy().
810 *
811 * When bpf_ma == false, the kmalloc and kfree are used.
812 */
c83597fa
YS
813struct bpf_map *
814bpf_local_storage_map_alloc(union bpf_attr *attr,
08a7ce38
MKL
815 struct bpf_local_storage_cache *cache,
816 bool bpf_ma)
c83597fa
YS
817{
818 struct bpf_local_storage_map *smap;
62827d61
MKL
819 unsigned int i;
820 u32 nbuckets;
08a7ce38 821 int err;
62827d61
MKL
822
823 smap = bpf_map_area_alloc(sizeof(*smap), NUMA_NO_NODE);
824 if (!smap)
825 return ERR_PTR(-ENOMEM);
826 bpf_map_init_from_attr(&smap->map, attr);
827
828 nbuckets = roundup_pow_of_two(num_possible_cpus());
829 /* Use at least 2 buckets, select_bucket() is undefined behavior with 1 bucket */
830 nbuckets = max_t(u32, 2, nbuckets);
831 smap->bucket_log = ilog2(nbuckets);
c83597fa 832
62827d61
MKL
833 smap->buckets = bpf_map_kvcalloc(&smap->map, sizeof(*smap->buckets),
834 nbuckets, GFP_USER | __GFP_NOWARN);
835 if (!smap->buckets) {
08a7ce38
MKL
836 err = -ENOMEM;
837 goto free_smap;
62827d61
MKL
838 }
839
840 for (i = 0; i < nbuckets; i++) {
841 INIT_HLIST_HEAD(&smap->buckets[i].list);
842 raw_spin_lock_init(&smap->buckets[i].lock);
843 }
844
845 smap->elem_size = offsetof(struct bpf_local_storage_elem,
846 sdata.data[attr->value_size]);
c83597fa 847
08a7ce38
MKL
848 smap->bpf_ma = bpf_ma;
849 if (bpf_ma) {
850 err = bpf_mem_alloc_init(&smap->selem_ma, smap->elem_size, false);
851 if (err)
852 goto free_smap;
6ae9d5e9
MKL
853
854 err = bpf_mem_alloc_init(&smap->storage_ma, sizeof(struct bpf_local_storage), false);
855 if (err) {
856 bpf_mem_alloc_destroy(&smap->selem_ma);
857 goto free_smap;
858 }
08a7ce38
MKL
859 }
860
c83597fa
YS
861 smap->cache_idx = bpf_local_storage_cache_idx_get(cache);
862 return &smap->map;
08a7ce38
MKL
863
864free_smap:
865 kvfree(smap->buckets);
866 bpf_map_area_free(smap);
867 return ERR_PTR(err);
c83597fa
YS
868}
869
870void bpf_local_storage_map_free(struct bpf_map *map,
871 struct bpf_local_storage_cache *cache,
872 int __percpu *busy_counter)
873{
874 struct bpf_local_storage_map_bucket *b;
875 struct bpf_local_storage_elem *selem;
876 struct bpf_local_storage_map *smap;
877 unsigned int i;
878
879 smap = (struct bpf_local_storage_map *)map;
880 bpf_local_storage_cache_idx_free(cache, smap->cache_idx);
881
882 /* Note that this map might be concurrently cloned from
883 * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone
884 * RCU read section to finish before proceeding. New RCU
885 * read sections should be prevented via bpf_map_inc_not_zero.
886 */
887 synchronize_rcu();
888
889 /* bpf prog and the userspace can no longer access this map
890 * now. No new selem (of this map) can be added
891 * to the owner->storage or to the map bucket's list.
892 *
893 * The elem of this map can be cleaned up here
894 * or when the storage is freed e.g.
895 * by bpf_sk_storage_free() during __sk_destruct().
896 */
897 for (i = 0; i < (1U << smap->bucket_log); i++) {
898 b = &smap->buckets[i];
899
900 rcu_read_lock();
901 /* No one is adding to b->list now */
902 while ((selem = hlist_entry_safe(
903 rcu_dereference_raw(hlist_first_rcu(&b->list)),
904 struct bpf_local_storage_elem, map_node))) {
905 if (busy_counter) {
906 migrate_disable();
907 this_cpu_inc(*busy_counter);
908 }
a47eabf2 909 bpf_selem_unlink(selem, true);
c83597fa
YS
910 if (busy_counter) {
911 this_cpu_dec(*busy_counter);
912 migrate_enable();
913 }
914 cond_resched_rcu();
915 }
916 rcu_read_unlock();
917 }
918
919 /* While freeing the storage we may still need to access the map.
920 *
921 * e.g. when bpf_sk_storage_free() has unlinked selem from the map
922 * which then made the above while((selem = ...)) loop
923 * exit immediately.
924 *
925 * However, while freeing the storage one still needs to access the
926 * smap->elem_size to do the uncharging in
927 * bpf_selem_unlink_storage_nolock().
928 *
929 * Hence, wait another rcu grace period for the storage to be freed.
930 */
931 synchronize_rcu();
932
6ae9d5e9 933 if (smap->bpf_ma) {
08a7ce38 934 bpf_mem_alloc_destroy(&smap->selem_ma);
6ae9d5e9
MKL
935 bpf_mem_alloc_destroy(&smap->storage_ma);
936 }
c83597fa
YS
937 kvfree(smap->buckets);
938 bpf_map_area_free(smap);
939}