From: Mykyta Yatsenko A lookup of a value that the owner does not have walks the whole list of its storage, with one cache miss for each element when the owner is cold. A prefetch cannot hide these misses, because each address comes from the element before it. Fill the cache[] slot when an element is linked, and refill it from the list when the element in the slot is unlinked. Then a NULL slot means that no element has this slot, and the lookup stops at the first load. This applies to all local storage types. With more than 16 maps, maps share slots, and a lookup can still walk the list. A delete and map_free() now walk the list once under the lock, to refill the slot. An update could already walk the whole list under this lock. A create of an absent value no longer walks the list. destroy() does not refill the slots, because the owner goes away. On 64-bit, the copy of cache_idx goes into a hole of the element. On a 32-bit CPU with 32-byte cache lines, the element grows by 32 bytes. The local storage benchmarks in selftests (1 to 1000 task storage maps, and creates) show no change larger than the noise between runs. Suggested-by: Alexei Starovoitov Link: https://lore.kernel.org/bpf/DN92BUAQMX7H.3NKLFY0FRORT7@gmail.com/ Signed-off-by: Mykyta Yatsenko --- include/linux/bpf_local_storage.h | 14 +++++++++++--- kernel/bpf/bpf_local_storage.c | 23 ++++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h index 6c643ac510799..83d849595805f 100644 --- a/include/linux/bpf_local_storage.h +++ b/include/linux/bpf_local_storage.h @@ -85,7 +85,8 @@ struct bpf_local_storage_elem { */ }; atomic_t state; - /* 4 bytes hole */ + u16 cache_idx; /* Copy of smap->cache_idx. smap can be NULL */ + /* 2 bytes hole */ /* The data is stored in another cacheline to minimize * the number of cachelines access during a cache hit. */ @@ -93,6 +94,10 @@ struct bpf_local_storage_elem { }; struct bpf_local_storage { + /* + * A NULL slot means that no selem on the list has this cache_idx. Link + * fills the slot, and unlink refills it from the list, under the lock. + */ struct bpf_local_storage_data __rcu *cache[BPF_LOCAL_STORAGE_CACHE_SIZE]; struct hlist_head list; /* List of bpf_local_storage_elem */ void *owner; /* The object that owns the above "list" of @@ -164,10 +169,13 @@ bpf_local_storage_lookup(struct bpf_local_storage *local_storage, /* Fast path (cache hit) */ sdata = rcu_dereference_check(local_storage->cache[smap->cache_idx], bpf_rcu_lock_held()); - if (sdata && rcu_access_pointer(sdata->smap) == smap) + /* The slot is NULL only if no selem on the list has this cache_idx */ + if (!sdata) + return NULL; + if (rcu_access_pointer(sdata->smap) == smap) return sdata; - /* Slow path (cache miss) */ + /* Slow path (another map has the slot) */ hlist_for_each_entry_rcu(selem, &local_storage->list, snode, rcu_read_lock_trace_held()) if (rcu_access_pointer(SDATA(selem)->smap) == smap) diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c index ed849546afd1f..b61183c0c17b4 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -172,6 +172,20 @@ static void bpf_selem_free_list(struct hlist_head *list, bool reuse_now) bpf_selem_free(selem, reuse_now); } +/* Another sdata for the cache[] slot of @selem. local_storage->lock is held. */ +static struct bpf_local_storage_data * +selem_cache_sibling(struct bpf_local_storage *local_storage, + struct bpf_local_storage_elem *selem) +{ + struct bpf_local_storage_elem *pos; + + hlist_for_each_entry(pos, &local_storage->list, snode) + if (pos != selem && pos->cache_idx == selem->cache_idx) + return SDATA(pos); + + return NULL; +} + static void bpf_selem_unlink_storage_nolock_misc(struct bpf_local_storage_elem *selem, struct bpf_local_storage_map *smap, struct bpf_local_storage *local_storage, @@ -182,7 +196,8 @@ static void bpf_selem_unlink_storage_nolock_misc(struct bpf_local_storage_elem * if (rcu_access_pointer(local_storage->cache[smap->cache_idx]) == SDATA(selem)) - RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL); + rcu_assign_pointer(local_storage->cache[smap->cache_idx], + selem_cache_sibling(local_storage, selem)); if (pin_owner && !refcount_inc_not_zero(&local_storage->owner_refcnt)) return; @@ -228,6 +243,10 @@ static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_stor return free_local_storage; } +/* + * The caller holds local_storage->lock, or no other CPU can unlink from the + * storage yet: a new storage before it is published, or the storage of a clone. + */ void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage, struct bpf_local_storage_elem *selem) { @@ -236,8 +255,10 @@ void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage, smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held()); local_storage->mem_charge += smap->elem_size; + selem->cache_idx = smap->cache_idx; RCU_INIT_POINTER(selem->local_storage, local_storage); hlist_add_head_rcu(&selem->snode, &local_storage->list); + rcu_assign_pointer(local_storage->cache[smap->cache_idx], SDATA(selem)); } static void bpf_selem_unlink_map_nolock(struct bpf_local_storage_map *smap, -- 2.53.1-Meta