bpf: Remove bpf_selem_free_fields*_rcu
[platform/kernel/linux-starfive.git] / kernel / bpf / bpf_local_storage.c
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>
14 #include <linux/rcupdate.h>
15 #include <linux/rcupdate_trace.h>
16 #include <linux/rcupdate_wait.h>
17
18 #define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE)
19
20 static struct bpf_local_storage_map_bucket *
21 select_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
27 static 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
37 static 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
46 static struct bpf_local_storage __rcu **
47 owner_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
54 static bool selem_linked_to_storage_lockless(const struct bpf_local_storage_elem *selem)
55 {
56         return !hlist_unhashed_lockless(&selem->snode);
57 }
58
59 static bool selem_linked_to_storage(const struct bpf_local_storage_elem *selem)
60 {
61         return !hlist_unhashed(&selem->snode);
62 }
63
64 static bool selem_linked_to_map_lockless(const struct bpf_local_storage_elem *selem)
65 {
66         return !hlist_unhashed_lockless(&selem->map_node);
67 }
68
69 static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem)
70 {
71         return !hlist_unhashed(&selem->map_node);
72 }
73
74 struct bpf_local_storage_elem *
75 bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
76                 void *value, bool charge_mem, gfp_t gfp_flags)
77 {
78         struct bpf_local_storage_elem *selem;
79
80         if (charge_mem && mem_charge(smap, owner, smap->elem_size))
81                 return NULL;
82
83         selem = bpf_map_kzalloc(&smap->map, smap->elem_size,
84                                 gfp_flags | __GFP_NOWARN);
85         if (selem) {
86                 if (value)
87                         copy_map_value(&smap->map, SDATA(selem)->data, value);
88                 /* No need to call check_and_init_map_value as memory is zero init */
89                 return selem;
90         }
91
92         if (charge_mem)
93                 mem_uncharge(smap, owner, smap->elem_size);
94
95         return NULL;
96 }
97
98 static void bpf_local_storage_free_rcu(struct rcu_head *rcu)
99 {
100         struct bpf_local_storage *local_storage;
101
102         /* If RCU Tasks Trace grace period implies RCU grace period, do
103          * kfree(), else do kfree_rcu().
104          */
105         local_storage = container_of(rcu, struct bpf_local_storage, rcu);
106         if (rcu_trace_implies_rcu_gp())
107                 kfree(local_storage);
108         else
109                 kfree_rcu(local_storage, rcu);
110 }
111
112 static void bpf_selem_free_trace_rcu(struct rcu_head *rcu)
113 {
114         struct bpf_local_storage_elem *selem;
115
116         selem = container_of(rcu, struct bpf_local_storage_elem, rcu);
117         if (rcu_trace_implies_rcu_gp())
118                 kfree(selem);
119         else
120                 kfree_rcu(selem, rcu);
121 }
122
123 /* local_storage->lock must be held and selem->local_storage == local_storage.
124  * The caller must ensure selem->smap is still valid to be
125  * dereferenced for its smap->elem_size and smap->cache_idx.
126  */
127 static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage,
128                                             struct bpf_local_storage_elem *selem,
129                                             bool uncharge_mem, bool reuse_now)
130 {
131         struct bpf_local_storage_map *smap;
132         bool free_local_storage;
133         void *owner;
134
135         smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
136         owner = local_storage->owner;
137
138         /* All uncharging on the owner must be done first.
139          * The owner may be freed once the last selem is unlinked
140          * from local_storage.
141          */
142         if (uncharge_mem)
143                 mem_uncharge(smap, owner, smap->elem_size);
144
145         free_local_storage = hlist_is_singular_node(&selem->snode,
146                                                     &local_storage->list);
147         if (free_local_storage) {
148                 mem_uncharge(smap, owner, sizeof(struct bpf_local_storage));
149                 local_storage->owner = NULL;
150
151                 /* After this RCU_INIT, owner may be freed and cannot be used */
152                 RCU_INIT_POINTER(*owner_storage(smap, owner), NULL);
153
154                 /* local_storage is not freed now.  local_storage->lock is
155                  * still held and raw_spin_unlock_bh(&local_storage->lock)
156                  * will be done by the caller.
157                  *
158                  * Although the unlock will be done under
159                  * rcu_read_lock(),  it is more intuitive to
160                  * read if the freeing of the storage is done
161                  * after the raw_spin_unlock_bh(&local_storage->lock).
162                  *
163                  * Hence, a "bool free_local_storage" is returned
164                  * to the caller which then calls then frees the storage after
165                  * all the RCU grace periods have expired.
166                  */
167         }
168         hlist_del_init_rcu(&selem->snode);
169         if (rcu_access_pointer(local_storage->cache[smap->cache_idx]) ==
170             SDATA(selem))
171                 RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL);
172
173         bpf_obj_free_fields(smap->map.record, SDATA(selem)->data);
174         if (!reuse_now)
175                 call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_trace_rcu);
176         else
177                 kfree_rcu(selem, rcu);
178
179         if (rcu_access_pointer(local_storage->smap) == smap)
180                 RCU_INIT_POINTER(local_storage->smap, NULL);
181
182         return free_local_storage;
183 }
184
185 static void bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem,
186                                      bool reuse_now)
187 {
188         struct bpf_local_storage *local_storage;
189         bool free_local_storage = false;
190         unsigned long flags;
191
192         if (unlikely(!selem_linked_to_storage_lockless(selem)))
193                 /* selem has already been unlinked from sk */
194                 return;
195
196         local_storage = rcu_dereference_check(selem->local_storage,
197                                               bpf_rcu_lock_held());
198         raw_spin_lock_irqsave(&local_storage->lock, flags);
199         if (likely(selem_linked_to_storage(selem)))
200                 free_local_storage = bpf_selem_unlink_storage_nolock(
201                         local_storage, selem, true, reuse_now);
202         raw_spin_unlock_irqrestore(&local_storage->lock, flags);
203
204         if (free_local_storage) {
205                 if (!reuse_now)
206                         call_rcu_tasks_trace(&local_storage->rcu,
207                                      bpf_local_storage_free_rcu);
208                 else
209                         kfree_rcu(local_storage, rcu);
210         }
211 }
212
213 void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
214                                    struct bpf_local_storage_elem *selem)
215 {
216         RCU_INIT_POINTER(selem->local_storage, local_storage);
217         hlist_add_head_rcu(&selem->snode, &local_storage->list);
218 }
219
220 static void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
221 {
222         struct bpf_local_storage_map *smap;
223         struct bpf_local_storage_map_bucket *b;
224         unsigned long flags;
225
226         if (unlikely(!selem_linked_to_map_lockless(selem)))
227                 /* selem has already be unlinked from smap */
228                 return;
229
230         smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
231         b = select_bucket(smap, selem);
232         raw_spin_lock_irqsave(&b->lock, flags);
233         if (likely(selem_linked_to_map(selem)))
234                 hlist_del_init_rcu(&selem->map_node);
235         raw_spin_unlock_irqrestore(&b->lock, flags);
236 }
237
238 void bpf_selem_link_map(struct bpf_local_storage_map *smap,
239                         struct bpf_local_storage_elem *selem)
240 {
241         struct bpf_local_storage_map_bucket *b = select_bucket(smap, selem);
242         unsigned long flags;
243
244         raw_spin_lock_irqsave(&b->lock, flags);
245         RCU_INIT_POINTER(SDATA(selem)->smap, smap);
246         hlist_add_head_rcu(&selem->map_node, &b->list);
247         raw_spin_unlock_irqrestore(&b->lock, flags);
248 }
249
250 void bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool reuse_now)
251 {
252         /* Always unlink from map before unlinking from local_storage
253          * because selem will be freed after successfully unlinked from
254          * the local_storage.
255          */
256         bpf_selem_unlink_map(selem);
257         bpf_selem_unlink_storage(selem, reuse_now);
258 }
259
260 /* If cacheit_lockit is false, this lookup function is lockless */
261 struct bpf_local_storage_data *
262 bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
263                          struct bpf_local_storage_map *smap,
264                          bool cacheit_lockit)
265 {
266         struct bpf_local_storage_data *sdata;
267         struct bpf_local_storage_elem *selem;
268
269         /* Fast path (cache hit) */
270         sdata = rcu_dereference_check(local_storage->cache[smap->cache_idx],
271                                       bpf_rcu_lock_held());
272         if (sdata && rcu_access_pointer(sdata->smap) == smap)
273                 return sdata;
274
275         /* Slow path (cache miss) */
276         hlist_for_each_entry_rcu(selem, &local_storage->list, snode,
277                                   rcu_read_lock_trace_held())
278                 if (rcu_access_pointer(SDATA(selem)->smap) == smap)
279                         break;
280
281         if (!selem)
282                 return NULL;
283
284         sdata = SDATA(selem);
285         if (cacheit_lockit) {
286                 unsigned long flags;
287
288                 /* spinlock is needed to avoid racing with the
289                  * parallel delete.  Otherwise, publishing an already
290                  * deleted sdata to the cache will become a use-after-free
291                  * problem in the next bpf_local_storage_lookup().
292                  */
293                 raw_spin_lock_irqsave(&local_storage->lock, flags);
294                 if (selem_linked_to_storage(selem))
295                         rcu_assign_pointer(local_storage->cache[smap->cache_idx],
296                                            sdata);
297                 raw_spin_unlock_irqrestore(&local_storage->lock, flags);
298         }
299
300         return sdata;
301 }
302
303 static int check_flags(const struct bpf_local_storage_data *old_sdata,
304                        u64 map_flags)
305 {
306         if (old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
307                 /* elem already exists */
308                 return -EEXIST;
309
310         if (!old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
311                 /* elem doesn't exist, cannot update it */
312                 return -ENOENT;
313
314         return 0;
315 }
316
317 int bpf_local_storage_alloc(void *owner,
318                             struct bpf_local_storage_map *smap,
319                             struct bpf_local_storage_elem *first_selem,
320                             gfp_t gfp_flags)
321 {
322         struct bpf_local_storage *prev_storage, *storage;
323         struct bpf_local_storage **owner_storage_ptr;
324         int err;
325
326         err = mem_charge(smap, owner, sizeof(*storage));
327         if (err)
328                 return err;
329
330         storage = bpf_map_kzalloc(&smap->map, sizeof(*storage),
331                                   gfp_flags | __GFP_NOWARN);
332         if (!storage) {
333                 err = -ENOMEM;
334                 goto uncharge;
335         }
336
337         RCU_INIT_POINTER(storage->smap, smap);
338         INIT_HLIST_HEAD(&storage->list);
339         raw_spin_lock_init(&storage->lock);
340         storage->owner = owner;
341
342         bpf_selem_link_storage_nolock(storage, first_selem);
343         bpf_selem_link_map(smap, first_selem);
344
345         owner_storage_ptr =
346                 (struct bpf_local_storage **)owner_storage(smap, owner);
347         /* Publish storage to the owner.
348          * Instead of using any lock of the kernel object (i.e. owner),
349          * cmpxchg will work with any kernel object regardless what
350          * the running context is, bh, irq...etc.
351          *
352          * From now on, the owner->storage pointer (e.g. sk->sk_bpf_storage)
353          * is protected by the storage->lock.  Hence, when freeing
354          * the owner->storage, the storage->lock must be held before
355          * setting owner->storage ptr to NULL.
356          */
357         prev_storage = cmpxchg(owner_storage_ptr, NULL, storage);
358         if (unlikely(prev_storage)) {
359                 bpf_selem_unlink_map(first_selem);
360                 err = -EAGAIN;
361                 goto uncharge;
362
363                 /* Note that even first_selem was linked to smap's
364                  * bucket->list, first_selem can be freed immediately
365                  * (instead of kfree_rcu) because
366                  * bpf_local_storage_map_free() does a
367                  * synchronize_rcu_mult (waiting for both sleepable and
368                  * normal programs) before walking the bucket->list.
369                  * Hence, no one is accessing selem from the
370                  * bucket->list under rcu_read_lock().
371                  */
372         }
373
374         return 0;
375
376 uncharge:
377         kfree(storage);
378         mem_uncharge(smap, owner, sizeof(*storage));
379         return err;
380 }
381
382 /* sk cannot be going away because it is linking new elem
383  * to sk->sk_bpf_storage. (i.e. sk->sk_refcnt cannot be 0).
384  * Otherwise, it will become a leak (and other memory issues
385  * during map destruction).
386  */
387 struct bpf_local_storage_data *
388 bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
389                          void *value, u64 map_flags, gfp_t gfp_flags)
390 {
391         struct bpf_local_storage_data *old_sdata = NULL;
392         struct bpf_local_storage_elem *selem = NULL;
393         struct bpf_local_storage *local_storage;
394         unsigned long flags;
395         int err;
396
397         /* BPF_EXIST and BPF_NOEXIST cannot be both set */
398         if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST) ||
399             /* BPF_F_LOCK can only be used in a value with spin_lock */
400             unlikely((map_flags & BPF_F_LOCK) &&
401                      !btf_record_has_field(smap->map.record, BPF_SPIN_LOCK)))
402                 return ERR_PTR(-EINVAL);
403
404         if (gfp_flags == GFP_KERNEL && (map_flags & ~BPF_F_LOCK) != BPF_NOEXIST)
405                 return ERR_PTR(-EINVAL);
406
407         local_storage = rcu_dereference_check(*owner_storage(smap, owner),
408                                               bpf_rcu_lock_held());
409         if (!local_storage || hlist_empty(&local_storage->list)) {
410                 /* Very first elem for the owner */
411                 err = check_flags(NULL, map_flags);
412                 if (err)
413                         return ERR_PTR(err);
414
415                 selem = bpf_selem_alloc(smap, owner, value, true, gfp_flags);
416                 if (!selem)
417                         return ERR_PTR(-ENOMEM);
418
419                 err = bpf_local_storage_alloc(owner, smap, selem, gfp_flags);
420                 if (err) {
421                         kfree(selem);
422                         mem_uncharge(smap, owner, smap->elem_size);
423                         return ERR_PTR(err);
424                 }
425
426                 return SDATA(selem);
427         }
428
429         if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) {
430                 /* Hoping to find an old_sdata to do inline update
431                  * such that it can avoid taking the local_storage->lock
432                  * and changing the lists.
433                  */
434                 old_sdata =
435                         bpf_local_storage_lookup(local_storage, smap, false);
436                 err = check_flags(old_sdata, map_flags);
437                 if (err)
438                         return ERR_PTR(err);
439                 if (old_sdata && selem_linked_to_storage_lockless(SELEM(old_sdata))) {
440                         copy_map_value_locked(&smap->map, old_sdata->data,
441                                               value, false);
442                         return old_sdata;
443                 }
444         }
445
446         if (gfp_flags == GFP_KERNEL) {
447                 selem = bpf_selem_alloc(smap, owner, value, true, gfp_flags);
448                 if (!selem)
449                         return ERR_PTR(-ENOMEM);
450         }
451
452         raw_spin_lock_irqsave(&local_storage->lock, flags);
453
454         /* Recheck local_storage->list under local_storage->lock */
455         if (unlikely(hlist_empty(&local_storage->list))) {
456                 /* A parallel del is happening and local_storage is going
457                  * away.  It has just been checked before, so very
458                  * unlikely.  Return instead of retry to keep things
459                  * simple.
460                  */
461                 err = -EAGAIN;
462                 goto unlock_err;
463         }
464
465         old_sdata = bpf_local_storage_lookup(local_storage, smap, false);
466         err = check_flags(old_sdata, map_flags);
467         if (err)
468                 goto unlock_err;
469
470         if (old_sdata && (map_flags & BPF_F_LOCK)) {
471                 copy_map_value_locked(&smap->map, old_sdata->data, value,
472                                       false);
473                 selem = SELEM(old_sdata);
474                 goto unlock;
475         }
476
477         if (gfp_flags != GFP_KERNEL) {
478                 /* local_storage->lock is held.  Hence, we are sure
479                  * we can unlink and uncharge the old_sdata successfully
480                  * later.  Hence, instead of charging the new selem now
481                  * and then uncharge the old selem later (which may cause
482                  * a potential but unnecessary charge failure),  avoid taking
483                  * a charge at all here (the "!old_sdata" check) and the
484                  * old_sdata will not be uncharged later during
485                  * bpf_selem_unlink_storage_nolock().
486                  */
487                 selem = bpf_selem_alloc(smap, owner, value, !old_sdata, gfp_flags);
488                 if (!selem) {
489                         err = -ENOMEM;
490                         goto unlock_err;
491                 }
492         }
493
494         /* First, link the new selem to the map */
495         bpf_selem_link_map(smap, selem);
496
497         /* Second, link (and publish) the new selem to local_storage */
498         bpf_selem_link_storage_nolock(local_storage, selem);
499
500         /* Third, remove old selem, SELEM(old_sdata) */
501         if (old_sdata) {
502                 bpf_selem_unlink_map(SELEM(old_sdata));
503                 bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata),
504                                                 false, false);
505         }
506
507 unlock:
508         raw_spin_unlock_irqrestore(&local_storage->lock, flags);
509         return SDATA(selem);
510
511 unlock_err:
512         raw_spin_unlock_irqrestore(&local_storage->lock, flags);
513         if (selem) {
514                 mem_uncharge(smap, owner, smap->elem_size);
515                 kfree(selem);
516         }
517         return ERR_PTR(err);
518 }
519
520 static u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache)
521 {
522         u64 min_usage = U64_MAX;
523         u16 i, res = 0;
524
525         spin_lock(&cache->idx_lock);
526
527         for (i = 0; i < BPF_LOCAL_STORAGE_CACHE_SIZE; i++) {
528                 if (cache->idx_usage_counts[i] < min_usage) {
529                         min_usage = cache->idx_usage_counts[i];
530                         res = i;
531
532                         /* Found a free cache_idx */
533                         if (!min_usage)
534                                 break;
535                 }
536         }
537         cache->idx_usage_counts[res]++;
538
539         spin_unlock(&cache->idx_lock);
540
541         return res;
542 }
543
544 static void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache,
545                                              u16 idx)
546 {
547         spin_lock(&cache->idx_lock);
548         cache->idx_usage_counts[idx]--;
549         spin_unlock(&cache->idx_lock);
550 }
551
552 int bpf_local_storage_map_alloc_check(union bpf_attr *attr)
553 {
554         if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK ||
555             !(attr->map_flags & BPF_F_NO_PREALLOC) ||
556             attr->max_entries ||
557             attr->key_size != sizeof(int) || !attr->value_size ||
558             /* Enforce BTF for userspace sk dumping */
559             !attr->btf_key_type_id || !attr->btf_value_type_id)
560                 return -EINVAL;
561
562         if (!bpf_capable())
563                 return -EPERM;
564
565         if (attr->value_size > BPF_LOCAL_STORAGE_MAX_VALUE_SIZE)
566                 return -E2BIG;
567
568         return 0;
569 }
570
571 int bpf_local_storage_map_check_btf(const struct bpf_map *map,
572                                     const struct btf *btf,
573                                     const struct btf_type *key_type,
574                                     const struct btf_type *value_type)
575 {
576         u32 int_data;
577
578         if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
579                 return -EINVAL;
580
581         int_data = *(u32 *)(key_type + 1);
582         if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data))
583                 return -EINVAL;
584
585         return 0;
586 }
587
588 void bpf_local_storage_destroy(struct bpf_local_storage *local_storage)
589 {
590         struct bpf_local_storage_elem *selem;
591         bool free_storage = false;
592         struct hlist_node *n;
593         unsigned long flags;
594
595         /* Neither the bpf_prog nor the bpf_map's syscall
596          * could be modifying the local_storage->list now.
597          * Thus, no elem can be added to or deleted from the
598          * local_storage->list by the bpf_prog or by the bpf_map's syscall.
599          *
600          * It is racing with bpf_local_storage_map_free() alone
601          * when unlinking elem from the local_storage->list and
602          * the map's bucket->list.
603          */
604         raw_spin_lock_irqsave(&local_storage->lock, flags);
605         hlist_for_each_entry_safe(selem, n, &local_storage->list, snode) {
606                 /* Always unlink from map before unlinking from
607                  * local_storage.
608                  */
609                 bpf_selem_unlink_map(selem);
610                 /* If local_storage list has only one element, the
611                  * bpf_selem_unlink_storage_nolock() will return true.
612                  * Otherwise, it will return false. The current loop iteration
613                  * intends to remove all local storage. So the last iteration
614                  * of the loop will set the free_cgroup_storage to true.
615                  */
616                 free_storage = bpf_selem_unlink_storage_nolock(
617                         local_storage, selem, false, true);
618         }
619         raw_spin_unlock_irqrestore(&local_storage->lock, flags);
620
621         if (free_storage)
622                 kfree_rcu(local_storage, rcu);
623 }
624
625 u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map)
626 {
627         struct bpf_local_storage_map *smap = (struct bpf_local_storage_map *)map;
628         u64 usage = sizeof(*smap);
629
630         /* The dynamically callocated selems are not counted currently. */
631         usage += sizeof(*smap->buckets) * (1ULL << smap->bucket_log);
632         return usage;
633 }
634
635 struct bpf_map *
636 bpf_local_storage_map_alloc(union bpf_attr *attr,
637                             struct bpf_local_storage_cache *cache)
638 {
639         struct bpf_local_storage_map *smap;
640         unsigned int i;
641         u32 nbuckets;
642
643         smap = bpf_map_area_alloc(sizeof(*smap), NUMA_NO_NODE);
644         if (!smap)
645                 return ERR_PTR(-ENOMEM);
646         bpf_map_init_from_attr(&smap->map, attr);
647
648         nbuckets = roundup_pow_of_two(num_possible_cpus());
649         /* Use at least 2 buckets, select_bucket() is undefined behavior with 1 bucket */
650         nbuckets = max_t(u32, 2, nbuckets);
651         smap->bucket_log = ilog2(nbuckets);
652
653         smap->buckets = bpf_map_kvcalloc(&smap->map, sizeof(*smap->buckets),
654                                          nbuckets, GFP_USER | __GFP_NOWARN);
655         if (!smap->buckets) {
656                 bpf_map_area_free(smap);
657                 return ERR_PTR(-ENOMEM);
658         }
659
660         for (i = 0; i < nbuckets; i++) {
661                 INIT_HLIST_HEAD(&smap->buckets[i].list);
662                 raw_spin_lock_init(&smap->buckets[i].lock);
663         }
664
665         smap->elem_size = offsetof(struct bpf_local_storage_elem,
666                                    sdata.data[attr->value_size]);
667
668         smap->cache_idx = bpf_local_storage_cache_idx_get(cache);
669         return &smap->map;
670 }
671
672 void bpf_local_storage_map_free(struct bpf_map *map,
673                                 struct bpf_local_storage_cache *cache,
674                                 int __percpu *busy_counter)
675 {
676         struct bpf_local_storage_map_bucket *b;
677         struct bpf_local_storage_elem *selem;
678         struct bpf_local_storage_map *smap;
679         unsigned int i;
680
681         smap = (struct bpf_local_storage_map *)map;
682         bpf_local_storage_cache_idx_free(cache, smap->cache_idx);
683
684         /* Note that this map might be concurrently cloned from
685          * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone
686          * RCU read section to finish before proceeding. New RCU
687          * read sections should be prevented via bpf_map_inc_not_zero.
688          */
689         synchronize_rcu();
690
691         /* bpf prog and the userspace can no longer access this map
692          * now.  No new selem (of this map) can be added
693          * to the owner->storage or to the map bucket's list.
694          *
695          * The elem of this map can be cleaned up here
696          * or when the storage is freed e.g.
697          * by bpf_sk_storage_free() during __sk_destruct().
698          */
699         for (i = 0; i < (1U << smap->bucket_log); i++) {
700                 b = &smap->buckets[i];
701
702                 rcu_read_lock();
703                 /* No one is adding to b->list now */
704                 while ((selem = hlist_entry_safe(
705                                 rcu_dereference_raw(hlist_first_rcu(&b->list)),
706                                 struct bpf_local_storage_elem, map_node))) {
707                         if (busy_counter) {
708                                 migrate_disable();
709                                 this_cpu_inc(*busy_counter);
710                         }
711                         bpf_selem_unlink(selem, true);
712                         if (busy_counter) {
713                                 this_cpu_dec(*busy_counter);
714                                 migrate_enable();
715                         }
716                         cond_resched_rcu();
717                 }
718                 rcu_read_unlock();
719         }
720
721         /* While freeing the storage we may still need to access the map.
722          *
723          * e.g. when bpf_sk_storage_free() has unlinked selem from the map
724          * which then made the above while((selem = ...)) loop
725          * exit immediately.
726          *
727          * However, while freeing the storage one still needs to access the
728          * smap->elem_size to do the uncharging in
729          * bpf_selem_unlink_storage_nolock().
730          *
731          * Hence, wait another rcu grace period for the storage to be freed.
732          */
733         synchronize_rcu();
734
735         kvfree(smap->buckets);
736         bpf_map_area_free(smap);
737 }