On Mon, Nov 30, 2026 at 04:33 PM Mykyta Yatsenko <myky…@gmail.com> wrote:
> On my setup prefetch works faster as well. The only case where my series > worked better was when value is absent, prefetch does not optimise that. > Either way, I'm happy to move forward with the prefetch approach.
Interesting. I explored couple ideas and the following looks the best to me... Lets fill the slot at link time and refill it at unlink time. Then a NULL slot means no value, and the lookup stops at the first load. More than 16 maps share slots. These still walk the list. Delete and map_free() do one walk under the lock instead. Could you pls try it on top of the prefetch and send both if it helps? It goes on bpf-next b7a509bd4470, with or without the prefetch diff. I haven't built this one yet. diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h index 6c643ac..4eaa76f 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. */ @@ -164,10 +165,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 b12fcec..6cff8ff 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; @@ -236,8 +251,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)); } /* Must be called with the owning local_storage->lock held. */