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>
9 #include <linux/btf_ids.h>
10 #include <linux/bpf_local_storage.h>
12 #include <uapi/linux/sock_diag.h>
13 #include <uapi/linux/btf.h>
15 #define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE)
17 static struct bpf_local_storage_map_bucket *
18 select_bucket(struct bpf_local_storage_map *smap,
19 struct bpf_local_storage_elem *selem)
21 return &smap->buckets[hash_ptr(selem, smap->bucket_log)];
24 static int mem_charge(struct bpf_local_storage_map *smap, void *owner, u32 size)
26 struct bpf_map *map = &smap->map;
28 if (!map->ops->map_local_storage_charge)
31 return map->ops->map_local_storage_charge(smap, owner, size);
34 static void mem_uncharge(struct bpf_local_storage_map *smap, void *owner,
37 struct bpf_map *map = &smap->map;
39 if (map->ops->map_local_storage_uncharge)
40 map->ops->map_local_storage_uncharge(smap, owner, size);
43 static struct bpf_local_storage __rcu **
44 owner_storage(struct bpf_local_storage_map *smap, void *owner)
46 struct bpf_map *map = &smap->map;
48 return map->ops->map_owner_storage_ptr(owner);
51 static bool selem_linked_to_storage(const struct bpf_local_storage_elem *selem)
53 return !hlist_unhashed(&selem->snode);
56 static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem)
58 return !hlist_unhashed(&selem->map_node);
61 struct bpf_local_storage_elem *
62 bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
63 void *value, bool charge_mem)
65 struct bpf_local_storage_elem *selem;
67 if (charge_mem && mem_charge(smap, owner, smap->elem_size))
70 selem = kzalloc(smap->elem_size, GFP_ATOMIC | __GFP_NOWARN);
73 memcpy(SDATA(selem)->data, value, smap->map.value_size);
78 mem_uncharge(smap, owner, smap->elem_size);
83 /* local_storage->lock must be held and selem->local_storage == local_storage.
84 * The caller must ensure selem->smap is still valid to be
85 * dereferenced for its smap->elem_size and smap->cache_idx.
87 bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage,
88 struct bpf_local_storage_elem *selem,
91 struct bpf_local_storage_map *smap;
92 bool free_local_storage;
95 smap = rcu_dereference(SDATA(selem)->smap);
96 owner = local_storage->owner;
98 /* All uncharging on the owner must be done first.
99 * The owner may be freed once the last selem is unlinked
100 * from local_storage.
103 mem_uncharge(smap, owner, smap->elem_size);
105 free_local_storage = hlist_is_singular_node(&selem->snode,
106 &local_storage->list);
107 if (free_local_storage) {
108 mem_uncharge(smap, owner, sizeof(struct bpf_local_storage));
109 local_storage->owner = NULL;
111 /* After this RCU_INIT, owner may be freed and cannot be used */
112 RCU_INIT_POINTER(*owner_storage(smap, owner), NULL);
114 /* local_storage is not freed now. local_storage->lock is
115 * still held and raw_spin_unlock_bh(&local_storage->lock)
116 * will be done by the caller.
118 * Although the unlock will be done under
119 * rcu_read_lock(), it is more intutivie to
120 * read if kfree_rcu(local_storage, rcu) is done
121 * after the raw_spin_unlock_bh(&local_storage->lock).
123 * Hence, a "bool free_local_storage" is returned
124 * to the caller which then calls the kfree_rcu()
128 hlist_del_init_rcu(&selem->snode);
129 if (rcu_access_pointer(local_storage->cache[smap->cache_idx]) ==
131 RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL);
133 kfree_rcu(selem, rcu);
135 return free_local_storage;
138 static void __bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem)
140 struct bpf_local_storage *local_storage;
141 bool free_local_storage = false;
143 if (unlikely(!selem_linked_to_storage(selem)))
144 /* selem has already been unlinked from sk */
147 local_storage = rcu_dereference(selem->local_storage);
148 raw_spin_lock_bh(&local_storage->lock);
149 if (likely(selem_linked_to_storage(selem)))
150 free_local_storage = bpf_selem_unlink_storage_nolock(
151 local_storage, selem, true);
152 raw_spin_unlock_bh(&local_storage->lock);
154 if (free_local_storage)
155 kfree_rcu(local_storage, rcu);
158 void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
159 struct bpf_local_storage_elem *selem)
161 RCU_INIT_POINTER(selem->local_storage, local_storage);
162 hlist_add_head_rcu(&selem->snode, &local_storage->list);
165 void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
167 struct bpf_local_storage_map *smap;
168 struct bpf_local_storage_map_bucket *b;
170 if (unlikely(!selem_linked_to_map(selem)))
171 /* selem has already be unlinked from smap */
174 smap = rcu_dereference(SDATA(selem)->smap);
175 b = select_bucket(smap, selem);
176 raw_spin_lock_bh(&b->lock);
177 if (likely(selem_linked_to_map(selem)))
178 hlist_del_init_rcu(&selem->map_node);
179 raw_spin_unlock_bh(&b->lock);
182 void bpf_selem_link_map(struct bpf_local_storage_map *smap,
183 struct bpf_local_storage_elem *selem)
185 struct bpf_local_storage_map_bucket *b = select_bucket(smap, selem);
187 raw_spin_lock_bh(&b->lock);
188 RCU_INIT_POINTER(SDATA(selem)->smap, smap);
189 hlist_add_head_rcu(&selem->map_node, &b->list);
190 raw_spin_unlock_bh(&b->lock);
193 void bpf_selem_unlink(struct bpf_local_storage_elem *selem)
195 /* Always unlink from map before unlinking from local_storage
196 * because selem will be freed after successfully unlinked from
199 bpf_selem_unlink_map(selem);
200 __bpf_selem_unlink_storage(selem);
203 struct bpf_local_storage_data *
204 bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
205 struct bpf_local_storage_map *smap,
208 struct bpf_local_storage_data *sdata;
209 struct bpf_local_storage_elem *selem;
211 /* Fast path (cache hit) */
212 sdata = rcu_dereference(local_storage->cache[smap->cache_idx]);
213 if (sdata && rcu_access_pointer(sdata->smap) == smap)
216 /* Slow path (cache miss) */
217 hlist_for_each_entry_rcu(selem, &local_storage->list, snode)
218 if (rcu_access_pointer(SDATA(selem)->smap) == smap)
224 sdata = SDATA(selem);
225 if (cacheit_lockit) {
226 /* spinlock is needed to avoid racing with the
227 * parallel delete. Otherwise, publishing an already
228 * deleted sdata to the cache will become a use-after-free
229 * problem in the next bpf_local_storage_lookup().
231 raw_spin_lock_bh(&local_storage->lock);
232 if (selem_linked_to_storage(selem))
233 rcu_assign_pointer(local_storage->cache[smap->cache_idx],
235 raw_spin_unlock_bh(&local_storage->lock);
241 static int check_flags(const struct bpf_local_storage_data *old_sdata,
244 if (old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
245 /* elem already exists */
248 if (!old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
249 /* elem doesn't exist, cannot update it */
255 int bpf_local_storage_alloc(void *owner,
256 struct bpf_local_storage_map *smap,
257 struct bpf_local_storage_elem *first_selem)
259 struct bpf_local_storage *prev_storage, *storage;
260 struct bpf_local_storage **owner_storage_ptr;
263 err = mem_charge(smap, owner, sizeof(*storage));
267 storage = kzalloc(sizeof(*storage), GFP_ATOMIC | __GFP_NOWARN);
273 INIT_HLIST_HEAD(&storage->list);
274 raw_spin_lock_init(&storage->lock);
275 storage->owner = owner;
277 bpf_selem_link_storage_nolock(storage, first_selem);
278 bpf_selem_link_map(smap, first_selem);
281 (struct bpf_local_storage **)owner_storage(smap, owner);
282 /* Publish storage to the owner.
283 * Instead of using any lock of the kernel object (i.e. owner),
284 * cmpxchg will work with any kernel object regardless what
285 * the running context is, bh, irq...etc.
287 * From now on, the owner->storage pointer (e.g. sk->sk_bpf_storage)
288 * is protected by the storage->lock. Hence, when freeing
289 * the owner->storage, the storage->lock must be held before
290 * setting owner->storage ptr to NULL.
292 prev_storage = cmpxchg(owner_storage_ptr, NULL, storage);
293 if (unlikely(prev_storage)) {
294 bpf_selem_unlink_map(first_selem);
298 /* Note that even first_selem was linked to smap's
299 * bucket->list, first_selem can be freed immediately
300 * (instead of kfree_rcu) because
301 * bpf_local_storage_map_free() does a
302 * synchronize_rcu() before walking the bucket->list.
303 * Hence, no one is accessing selem from the
304 * bucket->list under rcu_read_lock().
312 mem_uncharge(smap, owner, sizeof(*storage));
316 /* sk cannot be going away because it is linking new elem
317 * to sk->sk_bpf_storage. (i.e. sk->sk_refcnt cannot be 0).
318 * Otherwise, it will become a leak (and other memory issues
319 * during map destruction).
321 struct bpf_local_storage_data *
322 bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
323 void *value, u64 map_flags)
325 struct bpf_local_storage_data *old_sdata = NULL;
326 struct bpf_local_storage_elem *selem;
327 struct bpf_local_storage *local_storage;
330 /* BPF_EXIST and BPF_NOEXIST cannot be both set */
331 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST) ||
332 /* BPF_F_LOCK can only be used in a value with spin_lock */
333 unlikely((map_flags & BPF_F_LOCK) &&
334 !map_value_has_spin_lock(&smap->map)))
335 return ERR_PTR(-EINVAL);
337 local_storage = rcu_dereference(*owner_storage(smap, owner));
338 if (!local_storage || hlist_empty(&local_storage->list)) {
339 /* Very first elem for the owner */
340 err = check_flags(NULL, map_flags);
344 selem = bpf_selem_alloc(smap, owner, value, true);
346 return ERR_PTR(-ENOMEM);
348 err = bpf_local_storage_alloc(owner, smap, selem);
351 mem_uncharge(smap, owner, smap->elem_size);
358 if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) {
359 /* Hoping to find an old_sdata to do inline update
360 * such that it can avoid taking the local_storage->lock
361 * and changing the lists.
364 bpf_local_storage_lookup(local_storage, smap, false);
365 err = check_flags(old_sdata, map_flags);
368 if (old_sdata && selem_linked_to_storage(SELEM(old_sdata))) {
369 copy_map_value_locked(&smap->map, old_sdata->data,
375 raw_spin_lock_bh(&local_storage->lock);
377 /* Recheck local_storage->list under local_storage->lock */
378 if (unlikely(hlist_empty(&local_storage->list))) {
379 /* A parallel del is happening and local_storage is going
380 * away. It has just been checked before, so very
381 * unlikely. Return instead of retry to keep things
388 old_sdata = bpf_local_storage_lookup(local_storage, smap, false);
389 err = check_flags(old_sdata, map_flags);
393 if (old_sdata && (map_flags & BPF_F_LOCK)) {
394 copy_map_value_locked(&smap->map, old_sdata->data, value,
396 selem = SELEM(old_sdata);
400 /* local_storage->lock is held. Hence, we are sure
401 * we can unlink and uncharge the old_sdata successfully
402 * later. Hence, instead of charging the new selem now
403 * and then uncharge the old selem later (which may cause
404 * a potential but unnecessary charge failure), avoid taking
405 * a charge at all here (the "!old_sdata" check) and the
406 * old_sdata will not be uncharged later during
407 * bpf_selem_unlink_storage_nolock().
409 selem = bpf_selem_alloc(smap, owner, value, !old_sdata);
415 /* First, link the new selem to the map */
416 bpf_selem_link_map(smap, selem);
418 /* Second, link (and publish) the new selem to local_storage */
419 bpf_selem_link_storage_nolock(local_storage, selem);
421 /* Third, remove old selem, SELEM(old_sdata) */
423 bpf_selem_unlink_map(SELEM(old_sdata));
424 bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata),
429 raw_spin_unlock_bh(&local_storage->lock);
433 raw_spin_unlock_bh(&local_storage->lock);
437 u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache)
439 u64 min_usage = U64_MAX;
442 spin_lock(&cache->idx_lock);
444 for (i = 0; i < BPF_LOCAL_STORAGE_CACHE_SIZE; i++) {
445 if (cache->idx_usage_counts[i] < min_usage) {
446 min_usage = cache->idx_usage_counts[i];
449 /* Found a free cache_idx */
454 cache->idx_usage_counts[res]++;
456 spin_unlock(&cache->idx_lock);
461 void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache,
464 spin_lock(&cache->idx_lock);
465 cache->idx_usage_counts[idx]--;
466 spin_unlock(&cache->idx_lock);
469 void bpf_local_storage_map_free(struct bpf_local_storage_map *smap)
471 struct bpf_local_storage_elem *selem;
472 struct bpf_local_storage_map_bucket *b;
475 /* Note that this map might be concurrently cloned from
476 * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone
477 * RCU read section to finish before proceeding. New RCU
478 * read sections should be prevented via bpf_map_inc_not_zero.
482 /* bpf prog and the userspace can no longer access this map
483 * now. No new selem (of this map) can be added
484 * to the owner->storage or to the map bucket's list.
486 * The elem of this map can be cleaned up here
487 * or when the storage is freed e.g.
488 * by bpf_sk_storage_free() during __sk_destruct().
490 for (i = 0; i < (1U << smap->bucket_log); i++) {
491 b = &smap->buckets[i];
494 /* No one is adding to b->list now */
495 while ((selem = hlist_entry_safe(
496 rcu_dereference_raw(hlist_first_rcu(&b->list)),
497 struct bpf_local_storage_elem, map_node))) {
498 bpf_selem_unlink(selem);
504 /* While freeing the storage we may still need to access the map.
506 * e.g. when bpf_sk_storage_free() has unlinked selem from the map
507 * which then made the above while((selem = ...)) loop
510 * However, while freeing the storage one still needs to access the
511 * smap->elem_size to do the uncharging in
512 * bpf_selem_unlink_storage_nolock().
514 * Hence, wait another rcu grace period for the storage to be freed.
518 kvfree(smap->buckets);
522 int bpf_local_storage_map_alloc_check(union bpf_attr *attr)
524 if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK ||
525 !(attr->map_flags & BPF_F_NO_PREALLOC) ||
527 attr->key_size != sizeof(int) || !attr->value_size ||
528 /* Enforce BTF for userspace sk dumping */
529 !attr->btf_key_type_id || !attr->btf_value_type_id)
535 if (attr->value_size > BPF_LOCAL_STORAGE_MAX_VALUE_SIZE)
541 struct bpf_local_storage_map *bpf_local_storage_map_alloc(union bpf_attr *attr)
543 struct bpf_local_storage_map *smap;
549 smap = kzalloc(sizeof(*smap), GFP_USER | __GFP_NOWARN);
551 return ERR_PTR(-ENOMEM);
552 bpf_map_init_from_attr(&smap->map, attr);
554 nbuckets = roundup_pow_of_two(num_possible_cpus());
555 /* Use at least 2 buckets, select_bucket() is undefined behavior with 1 bucket */
556 nbuckets = max_t(u32, 2, nbuckets);
557 smap->bucket_log = ilog2(nbuckets);
558 cost = sizeof(*smap->buckets) * nbuckets + sizeof(*smap);
560 ret = bpf_map_charge_init(&smap->map.memory, cost);
566 smap->buckets = kvcalloc(sizeof(*smap->buckets), nbuckets,
567 GFP_USER | __GFP_NOWARN);
568 if (!smap->buckets) {
569 bpf_map_charge_finish(&smap->map.memory);
571 return ERR_PTR(-ENOMEM);
574 for (i = 0; i < nbuckets; i++) {
575 INIT_HLIST_HEAD(&smap->buckets[i].list);
576 raw_spin_lock_init(&smap->buckets[i].lock);
580 sizeof(struct bpf_local_storage_elem) + attr->value_size;
585 int bpf_local_storage_map_check_btf(const struct bpf_map *map,
586 const struct btf *btf,
587 const struct btf_type *key_type,
588 const struct btf_type *value_type)
592 if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
595 int_data = *(u32 *)(key_type + 1);
596 if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data))