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_lockless(const struct bpf_local_storage_elem *selem)
53 return !hlist_unhashed_lockless(&selem->snode);
56 static bool selem_linked_to_storage(const struct bpf_local_storage_elem *selem)
58 return !hlist_unhashed(&selem->snode);
61 static bool selem_linked_to_map_lockless(const struct bpf_local_storage_elem *selem)
63 return !hlist_unhashed_lockless(&selem->map_node);
66 static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem)
68 return !hlist_unhashed(&selem->map_node);
71 struct bpf_local_storage_elem *
72 bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
73 void *value, bool charge_mem)
75 struct bpf_local_storage_elem *selem;
77 if (charge_mem && mem_charge(smap, owner, smap->elem_size))
80 selem = kzalloc(smap->elem_size, GFP_ATOMIC | __GFP_NOWARN);
83 copy_map_value(&smap->map, SDATA(selem)->data, value);
88 mem_uncharge(smap, owner, smap->elem_size);
93 /* local_storage->lock must be held and selem->local_storage == local_storage.
94 * The caller must ensure selem->smap is still valid to be
95 * dereferenced for its smap->elem_size and smap->cache_idx.
97 bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage,
98 struct bpf_local_storage_elem *selem,
101 struct bpf_local_storage_map *smap;
102 bool free_local_storage;
105 smap = rcu_dereference(SDATA(selem)->smap);
106 owner = local_storage->owner;
108 /* All uncharging on the owner must be done first.
109 * The owner may be freed once the last selem is unlinked
110 * from local_storage.
113 mem_uncharge(smap, owner, smap->elem_size);
115 free_local_storage = hlist_is_singular_node(&selem->snode,
116 &local_storage->list);
117 if (free_local_storage) {
118 mem_uncharge(smap, owner, sizeof(struct bpf_local_storage));
119 local_storage->owner = NULL;
121 /* After this RCU_INIT, owner may be freed and cannot be used */
122 RCU_INIT_POINTER(*owner_storage(smap, owner), NULL);
124 /* local_storage is not freed now. local_storage->lock is
125 * still held and raw_spin_unlock_bh(&local_storage->lock)
126 * will be done by the caller.
128 * Although the unlock will be done under
129 * rcu_read_lock(), it is more intutivie to
130 * read if kfree_rcu(local_storage, rcu) is done
131 * after the raw_spin_unlock_bh(&local_storage->lock).
133 * Hence, a "bool free_local_storage" is returned
134 * to the caller which then calls the kfree_rcu()
138 hlist_del_init_rcu(&selem->snode);
139 if (rcu_access_pointer(local_storage->cache[smap->cache_idx]) ==
141 RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL);
143 kfree_rcu(selem, rcu);
145 return free_local_storage;
148 static void __bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem)
150 struct bpf_local_storage *local_storage;
151 bool free_local_storage = false;
153 if (unlikely(!selem_linked_to_storage_lockless(selem)))
154 /* selem has already been unlinked from sk */
157 local_storage = rcu_dereference(selem->local_storage);
158 raw_spin_lock_bh(&local_storage->lock);
159 if (likely(selem_linked_to_storage(selem)))
160 free_local_storage = bpf_selem_unlink_storage_nolock(
161 local_storage, selem, true);
162 raw_spin_unlock_bh(&local_storage->lock);
164 if (free_local_storage)
165 kfree_rcu(local_storage, rcu);
168 void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
169 struct bpf_local_storage_elem *selem)
171 RCU_INIT_POINTER(selem->local_storage, local_storage);
172 hlist_add_head_rcu(&selem->snode, &local_storage->list);
175 void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
177 struct bpf_local_storage_map *smap;
178 struct bpf_local_storage_map_bucket *b;
180 if (unlikely(!selem_linked_to_map_lockless(selem)))
181 /* selem has already be unlinked from smap */
184 smap = rcu_dereference(SDATA(selem)->smap);
185 b = select_bucket(smap, selem);
186 raw_spin_lock_bh(&b->lock);
187 if (likely(selem_linked_to_map(selem)))
188 hlist_del_init_rcu(&selem->map_node);
189 raw_spin_unlock_bh(&b->lock);
192 void bpf_selem_link_map(struct bpf_local_storage_map *smap,
193 struct bpf_local_storage_elem *selem)
195 struct bpf_local_storage_map_bucket *b = select_bucket(smap, selem);
197 raw_spin_lock_bh(&b->lock);
198 RCU_INIT_POINTER(SDATA(selem)->smap, smap);
199 hlist_add_head_rcu(&selem->map_node, &b->list);
200 raw_spin_unlock_bh(&b->lock);
203 void bpf_selem_unlink(struct bpf_local_storage_elem *selem)
205 /* Always unlink from map before unlinking from local_storage
206 * because selem will be freed after successfully unlinked from
209 bpf_selem_unlink_map(selem);
210 __bpf_selem_unlink_storage(selem);
213 struct bpf_local_storage_data *
214 bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
215 struct bpf_local_storage_map *smap,
218 struct bpf_local_storage_data *sdata;
219 struct bpf_local_storage_elem *selem;
221 /* Fast path (cache hit) */
222 sdata = rcu_dereference(local_storage->cache[smap->cache_idx]);
223 if (sdata && rcu_access_pointer(sdata->smap) == smap)
226 /* Slow path (cache miss) */
227 hlist_for_each_entry_rcu(selem, &local_storage->list, snode)
228 if (rcu_access_pointer(SDATA(selem)->smap) == smap)
234 sdata = SDATA(selem);
235 if (cacheit_lockit) {
236 /* spinlock is needed to avoid racing with the
237 * parallel delete. Otherwise, publishing an already
238 * deleted sdata to the cache will become a use-after-free
239 * problem in the next bpf_local_storage_lookup().
241 raw_spin_lock_bh(&local_storage->lock);
242 if (selem_linked_to_storage(selem))
243 rcu_assign_pointer(local_storage->cache[smap->cache_idx],
245 raw_spin_unlock_bh(&local_storage->lock);
251 static int check_flags(const struct bpf_local_storage_data *old_sdata,
254 if (old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
255 /* elem already exists */
258 if (!old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
259 /* elem doesn't exist, cannot update it */
265 int bpf_local_storage_alloc(void *owner,
266 struct bpf_local_storage_map *smap,
267 struct bpf_local_storage_elem *first_selem)
269 struct bpf_local_storage *prev_storage, *storage;
270 struct bpf_local_storage **owner_storage_ptr;
273 err = mem_charge(smap, owner, sizeof(*storage));
277 storage = kzalloc(sizeof(*storage), GFP_ATOMIC | __GFP_NOWARN);
283 INIT_HLIST_HEAD(&storage->list);
284 raw_spin_lock_init(&storage->lock);
285 storage->owner = owner;
287 bpf_selem_link_storage_nolock(storage, first_selem);
288 bpf_selem_link_map(smap, first_selem);
291 (struct bpf_local_storage **)owner_storage(smap, owner);
292 /* Publish storage to the owner.
293 * Instead of using any lock of the kernel object (i.e. owner),
294 * cmpxchg will work with any kernel object regardless what
295 * the running context is, bh, irq...etc.
297 * From now on, the owner->storage pointer (e.g. sk->sk_bpf_storage)
298 * is protected by the storage->lock. Hence, when freeing
299 * the owner->storage, the storage->lock must be held before
300 * setting owner->storage ptr to NULL.
302 prev_storage = cmpxchg(owner_storage_ptr, NULL, storage);
303 if (unlikely(prev_storage)) {
304 bpf_selem_unlink_map(first_selem);
308 /* Note that even first_selem was linked to smap's
309 * bucket->list, first_selem can be freed immediately
310 * (instead of kfree_rcu) because
311 * bpf_local_storage_map_free() does a
312 * synchronize_rcu() before walking the bucket->list.
313 * Hence, no one is accessing selem from the
314 * bucket->list under rcu_read_lock().
322 mem_uncharge(smap, owner, sizeof(*storage));
326 /* sk cannot be going away because it is linking new elem
327 * to sk->sk_bpf_storage. (i.e. sk->sk_refcnt cannot be 0).
328 * Otherwise, it will become a leak (and other memory issues
329 * during map destruction).
331 struct bpf_local_storage_data *
332 bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
333 void *value, u64 map_flags)
335 struct bpf_local_storage_data *old_sdata = NULL;
336 struct bpf_local_storage_elem *selem;
337 struct bpf_local_storage *local_storage;
340 /* BPF_EXIST and BPF_NOEXIST cannot be both set */
341 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST) ||
342 /* BPF_F_LOCK can only be used in a value with spin_lock */
343 unlikely((map_flags & BPF_F_LOCK) &&
344 !map_value_has_spin_lock(&smap->map)))
345 return ERR_PTR(-EINVAL);
347 local_storage = rcu_dereference(*owner_storage(smap, owner));
348 if (!local_storage || hlist_empty(&local_storage->list)) {
349 /* Very first elem for the owner */
350 err = check_flags(NULL, map_flags);
354 selem = bpf_selem_alloc(smap, owner, value, true);
356 return ERR_PTR(-ENOMEM);
358 err = bpf_local_storage_alloc(owner, smap, selem);
361 mem_uncharge(smap, owner, smap->elem_size);
368 if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) {
369 /* Hoping to find an old_sdata to do inline update
370 * such that it can avoid taking the local_storage->lock
371 * and changing the lists.
374 bpf_local_storage_lookup(local_storage, smap, false);
375 err = check_flags(old_sdata, map_flags);
378 if (old_sdata && selem_linked_to_storage_lockless(SELEM(old_sdata))) {
379 copy_map_value_locked(&smap->map, old_sdata->data,
385 raw_spin_lock_bh(&local_storage->lock);
387 /* Recheck local_storage->list under local_storage->lock */
388 if (unlikely(hlist_empty(&local_storage->list))) {
389 /* A parallel del is happening and local_storage is going
390 * away. It has just been checked before, so very
391 * unlikely. Return instead of retry to keep things
398 old_sdata = bpf_local_storage_lookup(local_storage, smap, false);
399 err = check_flags(old_sdata, map_flags);
403 if (old_sdata && (map_flags & BPF_F_LOCK)) {
404 copy_map_value_locked(&smap->map, old_sdata->data, value,
406 selem = SELEM(old_sdata);
410 /* local_storage->lock is held. Hence, we are sure
411 * we can unlink and uncharge the old_sdata successfully
412 * later. Hence, instead of charging the new selem now
413 * and then uncharge the old selem later (which may cause
414 * a potential but unnecessary charge failure), avoid taking
415 * a charge at all here (the "!old_sdata" check) and the
416 * old_sdata will not be uncharged later during
417 * bpf_selem_unlink_storage_nolock().
419 selem = bpf_selem_alloc(smap, owner, value, !old_sdata);
425 /* First, link the new selem to the map */
426 bpf_selem_link_map(smap, selem);
428 /* Second, link (and publish) the new selem to local_storage */
429 bpf_selem_link_storage_nolock(local_storage, selem);
431 /* Third, remove old selem, SELEM(old_sdata) */
433 bpf_selem_unlink_map(SELEM(old_sdata));
434 bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata),
439 raw_spin_unlock_bh(&local_storage->lock);
443 raw_spin_unlock_bh(&local_storage->lock);
447 u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache)
449 u64 min_usage = U64_MAX;
452 spin_lock(&cache->idx_lock);
454 for (i = 0; i < BPF_LOCAL_STORAGE_CACHE_SIZE; i++) {
455 if (cache->idx_usage_counts[i] < min_usage) {
456 min_usage = cache->idx_usage_counts[i];
459 /* Found a free cache_idx */
464 cache->idx_usage_counts[res]++;
466 spin_unlock(&cache->idx_lock);
471 void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache,
474 spin_lock(&cache->idx_lock);
475 cache->idx_usage_counts[idx]--;
476 spin_unlock(&cache->idx_lock);
479 void bpf_local_storage_map_free(struct bpf_local_storage_map *smap)
481 struct bpf_local_storage_elem *selem;
482 struct bpf_local_storage_map_bucket *b;
485 /* Note that this map might be concurrently cloned from
486 * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone
487 * RCU read section to finish before proceeding. New RCU
488 * read sections should be prevented via bpf_map_inc_not_zero.
492 /* bpf prog and the userspace can no longer access this map
493 * now. No new selem (of this map) can be added
494 * to the owner->storage or to the map bucket's list.
496 * The elem of this map can be cleaned up here
497 * or when the storage is freed e.g.
498 * by bpf_sk_storage_free() during __sk_destruct().
500 for (i = 0; i < (1U << smap->bucket_log); i++) {
501 b = &smap->buckets[i];
504 /* No one is adding to b->list now */
505 while ((selem = hlist_entry_safe(
506 rcu_dereference_raw(hlist_first_rcu(&b->list)),
507 struct bpf_local_storage_elem, map_node))) {
508 bpf_selem_unlink(selem);
514 /* While freeing the storage we may still need to access the map.
516 * e.g. when bpf_sk_storage_free() has unlinked selem from the map
517 * which then made the above while((selem = ...)) loop
520 * However, while freeing the storage one still needs to access the
521 * smap->elem_size to do the uncharging in
522 * bpf_selem_unlink_storage_nolock().
524 * Hence, wait another rcu grace period for the storage to be freed.
528 kvfree(smap->buckets);
532 int bpf_local_storage_map_alloc_check(union bpf_attr *attr)
534 if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK ||
535 !(attr->map_flags & BPF_F_NO_PREALLOC) ||
537 attr->key_size != sizeof(int) || !attr->value_size ||
538 /* Enforce BTF for userspace sk dumping */
539 !attr->btf_key_type_id || !attr->btf_value_type_id)
545 if (attr->value_size > BPF_LOCAL_STORAGE_MAX_VALUE_SIZE)
551 struct bpf_local_storage_map *bpf_local_storage_map_alloc(union bpf_attr *attr)
553 struct bpf_local_storage_map *smap;
559 smap = kzalloc(sizeof(*smap), GFP_USER | __GFP_NOWARN);
561 return ERR_PTR(-ENOMEM);
562 bpf_map_init_from_attr(&smap->map, attr);
564 nbuckets = roundup_pow_of_two(num_possible_cpus());
565 /* Use at least 2 buckets, select_bucket() is undefined behavior with 1 bucket */
566 nbuckets = max_t(u32, 2, nbuckets);
567 smap->bucket_log = ilog2(nbuckets);
568 cost = sizeof(*smap->buckets) * nbuckets + sizeof(*smap);
570 ret = bpf_map_charge_init(&smap->map.memory, cost);
576 smap->buckets = kvcalloc(sizeof(*smap->buckets), nbuckets,
577 GFP_USER | __GFP_NOWARN);
578 if (!smap->buckets) {
579 bpf_map_charge_finish(&smap->map.memory);
581 return ERR_PTR(-ENOMEM);
584 for (i = 0; i < nbuckets; i++) {
585 INIT_HLIST_HEAD(&smap->buckets[i].list);
586 raw_spin_lock_init(&smap->buckets[i].lock);
590 sizeof(struct bpf_local_storage_elem) + attr->value_size;
595 int bpf_local_storage_map_check_btf(const struct bpf_map *map,
596 const struct btf *btf,
597 const struct btf_type *key_type,
598 const struct btf_type *value_type)
602 if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
605 int_data = *(u32 *)(key_type + 1);
606 if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data))