BPF List
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Mykyta Yatsenko" <mykyta.yatsenko5@gmail.com>,
	<bpf@vger.kernel.org>, <andrii@kernel.org>,
	<daniel@iogearbox.net>, <kernel-team@meta.com>,
	<eddyz87@gmail.com>
Cc: "Mykyta Yatsenko" <yatsenko@meta.com>, <edumazet@kernel.org>,
	<ncardwell@google.com>, <kuniyu@google.com>,
	<martin.lau@linux.dev>, <netdev@vger.kernel.org>
Subject: Re: [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object
Date: Tue, 01 Dec 2026 03:06:43 +0000	[thread overview]
Message-ID: <DN92BUAQMX7H.3NKLFY0FRORT7@gmail.com> (raw)
In-Reply-To: <1f34507a-bca5-4c7a-83ad-19b9dd5b03ce@gmail.com>

On Mon, Nov 30, 2026 at 04:33 PM Mykyta Yatsenko <mykyta.yatsenko5@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. */

      reply	other threads:[~2026-12-01  3:06 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 15:15 [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 01/13] bpf: Free unpublished local storage with kfree_nolock() Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 02/13] bpf: Add obj_size to bpf_local_storage Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 03/13] bpf: Add bpf_local_storage_delete() Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 04/13] bpf: Add an inline slot allocator to local storage caches Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 05/13] bpf: Size new local storage objects for the inline area Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 06/13] bpf: Keep local storage objects with an inline area until owner death Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 07/13] bpf: Keep values of BPF_F_INLINE_VALUE maps in the storage object Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 08/13] bpf: Refuse the sk storage map iterator for BPF_F_INLINE_VALUE maps Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 09/13] bpf: Show inline values in sk diag Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 10/13] bpf: Allow BPF_F_INLINE_VALUE for local storage maps Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 11/13] libbpf: Do not retry local storage map create without BTF Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 12/13] selftests/bpf: Test BPF_F_INLINE_VALUE local storage maps Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 13/13] bpf, docs: Document BPF_F_INLINE_VALUE for " Mykyta Yatsenko
2026-11-30  0:57 ` [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Alexei Starovoitov
2026-11-30  4:58   ` Alexei Starovoitov
2026-12-01  0:33     ` Mykyta Yatsenko
2026-12-01  3:06       ` Alexei Starovoitov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DN92BUAQMX7H.3NKLFY0FRORT7@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@kernel.org \
    --cc=kernel-team@meta.com \
    --cc=kuniyu@google.com \
    --cc=martin.lau@linux.dev \
    --cc=mykyta.yatsenko5@gmail.com \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=yatsenko@meta.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox