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>
Subject: Re: [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object
Date: Mon, 30 Nov 2026 00:57:07 +0000 [thread overview]
Message-ID: <DN84Y298SZZL.3GHOLS9WYETHQ@gmail.com> (raw)
In-Reply-To: <20261123-sls_f1-v2-0-76474de9b052@meta.com>
On Mon, Nov 23, 2026 at 07:15 AM Mykyta Yatsenko <mykyta.yatsenko5@gmail.com> wrote:
> This series adds the map flag BPF_F_INLINE_VALUE for sk, task, inode
> and cgroup storage. The value of such a map is in the storage object,
> at a fixed offset, so a lookup has one dependent load less.
Sorry for the delay. I guess the whole thing is sort of an alternative
to increasing 'struct sock'? But it's not equivalent in terms of
reducing cache misses...
There are 3 misses:
sk->sk_bpf_storage -> storage->cache[idx] -> sdata (smap + value)
This series remove the last one... just like a simpler diff below..
There are other low hanging fruits that I'm prototyping that
should help more than removal of the last miss:
1. Move sk_bpf_storage to a tx-hot line. It's a one-line change that may remove the first miss.
2. Prefetch early. Under a static key, prefetch sk->sk_bpf_storage in the TCP transmit path, well before tc egress runs. That hides the second miss.
3. Prefetch the sibling. Once cache[] is loaded, the other map's pointer is already there, so the first lookup can do prefetch(cache[B]).
> - The tag is predictable, and claim numbers wrap after 2^32 claims. A
> user that can create inline maps can make another map see bytes that
> it did not write, in objects made before that map.
> - The area and the 16-map limit are shared by the whole host, also
> with BPF token users.
tbh not excited about new uapi and new limit just to remove one miss.
I think it's better to keep the selem and change only where its memory
comes from. The storage gets a tail. The selem of an inline map takes
its memory from the tail, at the same offset in every storage.
With the two maps of your example workload of 2 maps:
+----------------------------------+ <- sk->sk_bpf_storage
0 | struct bpf_local_storage, 184 b |
| cache[16], list, lock, ... |
+----------------------------------+ <- tail, units of 64 b
192 | selem of map A: header |
256 | sdata: smap, value of 124 b | <- A->sdata_off
+----------------------------------+
448 | selem of map B: header |
512 | sdata: smap, value of 2 b | <- B->sdata_off
+----------------------------------+
576
It is a normal selem: in storage->list, in the bucket of the map and
in cache[]. The lookup stays as it is.
The key idea is to add one prefetch of storage + smap->sdata_off in front.
Your lookup reads obj_size and the tag. These two loads don't depend on
each other. With the prefetch the loads of cache[] and sdata don't
depend on each other either. A wrong guess costs nothing.
No tag, no claim number, no obj_size, no dying, no new branch,
and a weaker uapi addition.
this series selem in the tail
-------------------------- -------------------- -----------------------
syscall update in place, torn reads new selem, like today
BPF_F_LOCK, BPF_F_CLONE no yes
kptr, uptr in the value no yes
pointer kept after delete sees the next value sees the old value
values of a freed map stay behind a tag map_free() unlinks them
no room in the area -ENOSPC, -E2BIG selem from kmalloc
storage is freed at owner death with its last selem
sk iter, sk diag refused, new code no change
lookup of a normal map 1 more branch 1 prefetch
memory of a value 8 b + value 72 b + value
The uapi flag is necessary, but as a hint.
It picks the maps that get room in the tail.
The whole idea depends on the prefetch in bpf_local_storage_lookup().
Could you pls benchmark it?
diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h
index 6c643ac..c58305a 100644
--- a/include/linux/bpf_local_storage.h
+++ b/include/linux/bpf_local_storage.h
@@ -13,6 +13,7 @@
#include <linux/list.h>
#include <linux/hash.h>
#include <linux/types.h>
+#include <linux/prefetch.h>
#include <linux/bpf_mem_alloc.h>
#include <uapi/linux/btf.h>
#include <asm/rqspinlock.h>
@@ -51,9 +52,16 @@ struct bpf_local_storage_map {
* multiple buckets to improve contention.
*/
struct bpf_local_storage_map_bucket *buckets;
+ struct bpf_local_storage_cache *cache;
u32 bucket_log;
u16 elem_size;
u16 cache_idx;
+ u16 inline_mask; /* Units of the tail of a storage for the selem
+ * of this map. 0: none.
+ */
+ u16 sdata_off; /* Offset of its sdata in the storage then.
+ * Else of cache[cache_idx].
+ */
};
struct bpf_local_storage_data {
@@ -85,7 +93,8 @@ struct bpf_local_storage_elem {
*/
};
atomic_t state;
- /* 4 bytes hole */
+ u16 inline_mask; /* Not 0: the memory is these units of the tail */
+ u16 inline_off; /* of the storage that starts inline_off before */
/* The data is stored in another cacheline to minimize
* the number of cachelines access during a cache hit.
*/
@@ -100,10 +109,24 @@ struct bpf_local_storage {
*/
struct rcu_head rcu;
rqspinlock_t lock; /* Protect adding/removing from the "list" */
+ atomic_t inline_busy; /* Units of the tail that are in use or not there */
u64 mem_charge; /* Copy of mem charged to owner. Protected by "lock" */
refcount_t owner_refcnt;/* Used to pin owner when map_free is uncharging */
+ refcount_t refcnt; /* "list" and each inline selem hold the memory */
};
+/*
+ * A storage can have a tail with room for the selems of BPF_F_INLINE_VALUE
+ * maps. All storages of an owner type have the same layout, so that a lookup
+ * knows where the selem is before it has read cache[].
+ */
+#define BPF_LOCAL_STORAGE_INLINE_UNIT SMP_CACHE_BYTES
+#define BPF_LOCAL_STORAGE_INLINE_START \
+ ALIGN(sizeof(struct bpf_local_storage), BPF_LOCAL_STORAGE_INLINE_UNIT)
+#define BPF_LOCAL_STORAGE_INLINE_UNITS \
+ MIN(16, (1024 - BPF_LOCAL_STORAGE_INLINE_START) / \
+ BPF_LOCAL_STORAGE_INLINE_UNIT)
+
/*
* Drop a reference got by refcount_inc_not_zero(&local_storage->owner_refcnt).
* bpf_local_storage_destroy() may have dropped the owner's reference meanwhile
@@ -135,6 +158,7 @@ bpf_local_storage_unpin_owner(struct bpf_local_storage *local_storage)
struct bpf_local_storage_cache {
spinlock_t idx_lock;
u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE];
+ unsigned long inline_units; /* Units of the tail that maps have */
};
#define DEFINE_BPF_STORAGE_CACHE(name) \
@@ -161,6 +185,9 @@ bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
struct bpf_local_storage_data *sdata;
struct bpf_local_storage_elem *selem;
+ /* Load an inline sdata together with cache[], and not after it */
+ prefetch((void *)local_storage + smap->sdata_off);
+
/* Fast path (cache hit) */
sdata = rcu_dereference_check(local_storage->cache[smap->cache_idx],
bpf_rcu_lock_held());
@@ -200,7 +227,8 @@ int bpf_selem_link_map(struct bpf_local_storage_map *smap,
struct bpf_local_storage_elem *selem);
struct bpf_local_storage_elem *
-bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,
+bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
+ struct bpf_local_storage *local_storage, void *value,
bool swap_uptrs);
void bpf_selem_free(struct bpf_local_storage_elem *selem,
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 62de091..377cdc4 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1513,6 +1513,11 @@ enum {
/* Enable BPF ringbuf overwrite mode */
BPF_F_RB_OVERWRITE = (1U << 19),
+
+/* Local storage: most owners have a value of this map. Keep it next to the
+ * other values of the owner, if there is room. A hint: nothing else changes.
+ */
+ BPF_F_INLINE_VALUE = (1U << 20),
};
/* Flags for BPF_PROG_QUERY. */
diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index b12fcec..77c6ce7 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -15,7 +15,11 @@
#include <linux/rcupdate_trace.h>
#include <linux/rcupdate_wait.h>
-#define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE)
+#define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK \
+ (BPF_F_NO_PREALLOC | BPF_F_CLONE | BPF_F_INLINE_VALUE)
+
+static void bpf_local_storage_free(struct bpf_local_storage *local_storage,
+ bool reuse_now);
static struct bpf_local_storage_map_bucket *
select_bucket(struct bpf_local_storage_map *smap,
@@ -66,17 +70,84 @@ static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem)
return !hlist_unhashed(&selem->map_node);
}
+/* The storage that has the memory of an inline selem */
+static struct bpf_local_storage *
+selem_inline_storage(struct bpf_local_storage_elem *selem)
+{
+ return selem->inline_mask ? (void *)selem - selem->inline_off : NULL;
+}
+
+/* With room for the selems of all inline maps that the owner type has now */
+static struct bpf_local_storage *
+bpf_local_storage_kmalloc(struct bpf_local_storage_map *smap)
+{
+ u32 units = fls_long(READ_ONCE(smap->cache->inline_units));
+ struct bpf_local_storage *storage;
+ u32 size = sizeof(*storage);
+
+ if (units)
+ size = BPF_LOCAL_STORAGE_INLINE_START +
+ units * BPF_LOCAL_STORAGE_INLINE_UNIT;
+
+ storage = bpf_map_kmalloc_nolock(&smap->map, size, __GFP_ZERO,
+ NUMA_NO_NODE);
+ if (!storage)
+ return NULL;
+
+ atomic_set(&storage->inline_busy, ~0U << units);
+ refcount_set(&storage->refcnt, 1);
+ return storage;
+}
+
+/*
+ * Take the memory of the selem from the tail of the storage. Without a storage
+ * make one: bpf_local_storage_alloc() finds it through the selem.
+ */
+static struct bpf_local_storage_elem *
+bpf_selem_alloc_inline(struct bpf_local_storage_map *smap,
+ struct bpf_local_storage *storage)
+{
+ struct bpf_local_storage_elem *selem;
+ int busy;
+
+ if (!smap->inline_mask)
+ return NULL;
+
+ if (!storage)
+ storage = bpf_local_storage_kmalloc(smap);
+ if (!storage || !refcount_inc_not_zero(&storage->refcnt))
+ return NULL;
+
+ busy = atomic_read(&storage->inline_busy);
+ do {
+ if (busy & smap->inline_mask) {
+ bpf_local_storage_free(storage, false);
+ return NULL;
+ }
+ } while (!atomic_try_cmpxchg(&storage->inline_busy, &busy,
+ busy | smap->inline_mask));
+
+ selem = SELEM((void *)storage + smap->sdata_off);
+ memset(selem, 0, smap->elem_size);
+ selem->inline_mask = smap->inline_mask;
+ selem->inline_off = (void *)selem - (void *)storage;
+ return selem;
+}
+
struct bpf_local_storage_elem *
bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
- void *value, bool swap_uptrs)
+ struct bpf_local_storage *local_storage, void *value,
+ bool swap_uptrs)
{
struct bpf_local_storage_elem *selem;
if (mem_charge(smap, owner, smap->elem_size))
return NULL;
- selem = bpf_map_kmalloc_nolock(&smap->map, smap->elem_size,
- __GFP_ZERO, NUMA_NO_NODE);
+ selem = bpf_selem_alloc_inline(smap, local_storage);
+ if (!selem)
+ selem = bpf_map_kmalloc_nolock(&smap->map, smap->elem_size,
+ __GFP_ZERO, NUMA_NO_NODE);
if (selem) {
RCU_INIT_POINTER(SDATA(selem)->smap, smap);
@@ -111,7 +182,7 @@ static void bpf_local_storage_free_trace_rcu(struct rcu_head *rcu)
static void bpf_local_storage_free(struct bpf_local_storage *local_storage,
bool reuse_now)
{
- if (!local_storage)
+ if (!local_storage || !refcount_dec_and_test(&local_storage->refcnt))
return;
if (reuse_now) {
@@ -123,6 +194,20 @@ static void bpf_local_storage_free(struct bpf_local_storage *local_storage,
bpf_local_storage_free_trace_rcu);
}
+/* Give the memory of an inline selem back to its storage */
+static bool bpf_selem_free_inline(struct bpf_local_storage_elem *selem)
+{
+ struct bpf_local_storage *local_storage = selem_inline_storage(selem);
+
+ if (!local_storage)
+ return false;
+
+ atomic_fetch_andnot_release(selem->inline_mask,
+ &local_storage->inline_busy);
+ bpf_local_storage_free(local_storage, false);
+ return true;
+}
+
static void bpf_selem_free_trace_rcu(struct rcu_head *rcu)
{
struct bpf_local_storage_elem *selem;
@@ -138,7 +223,8 @@ static void bpf_selem_free_trace_rcu(struct rcu_head *rcu)
* RCU Tasks Trace grace period implies RCU grace period, do
* kfree() directly.
*/
- kfree(selem);
+ if (!bpf_selem_free_inline(selem))
+ kfree(selem);
}
void bpf_selem_free(struct bpf_local_storage_elem *selem,
@@ -151,7 +237,12 @@ void bpf_selem_free(struct bpf_local_storage_elem *selem,
if (reuse_now) {
if (smap)
bpf_obj_free_fields(smap->map.record, SDATA(selem)->data);
- kfree_rcu(selem, rcu);
+ /*
+ * Nobody has seen the selem, or its owner is dead and gets no
+ * new selem. The storage waits for RCU readers.
+ */
+ if (!bpf_selem_free_inline(selem))
+ kfree_rcu(selem, rcu);
return;
}
@@ -508,12 +599,14 @@ int bpf_local_storage_alloc(void *owner,
unsigned long flags;
int err;
+ storage = selem_inline_storage(first_selem);
+
err = mem_charge(smap, owner, sizeof(*storage));
if (err)
- return err;
+ goto free;
- storage = bpf_map_kmalloc_nolock(&smap->map, sizeof(*storage),
- __GFP_ZERO, NUMA_NO_NODE);
+ if (!storage)
+ storage = bpf_local_storage_kmalloc(smap);
if (!storage) {
err = -ENOMEM;
goto uncharge;
@@ -562,8 +655,9 @@ int bpf_local_storage_alloc(void *owner,
return 0;
uncharge:
- bpf_local_storage_free(storage, true);
mem_uncharge(smap, owner, sizeof(*storage));
+free:
+ bpf_local_storage_free(storage, true);
return err;
}
@@ -599,7 +693,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
if (err)
return ERR_PTR(err);
- selem = bpf_selem_alloc(smap, owner, value, swap_uptrs);
+ selem = bpf_selem_alloc(smap, owner, NULL, value, swap_uptrs);
if (!selem)
return ERR_PTR(-ENOMEM);
@@ -633,7 +727,8 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
/* A lookup has just been done before and concluded a new selem is
* needed. The chance of an unnecessary alloc is unlikely.
*/
- alloc_selem = selem = bpf_selem_alloc(smap, owner, value, swap_uptrs);
+ alloc_selem = selem = bpf_selem_alloc(smap, owner, local_storage, value,
+ swap_uptrs);
if (!alloc_selem)
return ERR_PTR(-ENOMEM);
@@ -729,6 +824,38 @@ static void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cac
spin_unlock(&cache->idx_lock);
}
+/* No room is not an error: all selems of the map come from kmalloc then */
+static void bpf_local_storage_inline_get(struct bpf_local_storage_map *smap)
+{
+ u32 nr = DIV_ROUND_UP(smap->elem_size, BPF_LOCAL_STORAGE_INLINE_UNIT);
+ struct bpf_local_storage_cache *cache = smap->cache;
+ unsigned long start;
+
+ spin_lock(&cache->idx_lock);
+ start = bitmap_find_next_zero_area(&cache->inline_units,
+ BPF_LOCAL_STORAGE_INLINE_UNITS,
+ 0, nr, 0);
+ if (start < BPF_LOCAL_STORAGE_INLINE_UNITS) {
+ smap->inline_mask = GENMASK(start + nr - 1, start);
+ smap->sdata_off = BPF_LOCAL_STORAGE_INLINE_START +
+ start * BPF_LOCAL_STORAGE_INLINE_UNIT +
+ offsetof(struct bpf_local_storage_elem, sdata);
+ WRITE_ONCE(cache->inline_units,
+ cache->inline_units | smap->inline_mask);
+ }
+ spin_unlock(&cache->idx_lock);
+}
+
+static void bpf_local_storage_inline_put(struct bpf_local_storage_map *smap)
+{
+ struct bpf_local_storage_cache *cache = smap->cache;
+
+ spin_lock(&cache->idx_lock);
+ WRITE_ONCE(cache->inline_units,
+ cache->inline_units & ~smap->inline_mask);
+ spin_unlock(&cache->idx_lock);
+}
+
int bpf_local_storage_map_alloc_check(union bpf_attr *attr)
{
if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK ||
@@ -833,7 +960,12 @@ bpf_local_storage_map_alloc(union bpf_attr *attr,
smap->elem_size = offsetof(struct bpf_local_storage_elem,
sdata.data[attr->value_size]);
+ smap->cache = cache;
smap->cache_idx = bpf_local_storage_cache_idx_get(cache);
+ smap->sdata_off = offsetof(struct bpf_local_storage,
+ cache[smap->cache_idx]);
+ if (attr->map_flags & BPF_F_INLINE_VALUE)
+ bpf_local_storage_inline_get(smap);
return &smap->map;
free_smap:
@@ -902,6 +1034,8 @@ restart:
/* smap remains in use regardless of kmalloc_nolock, so wait unconditionally. */
rcu_barrier_tasks_trace();
rcu_barrier();
+ /* The callbacks gave back the memory of the inline selems */
+ bpf_local_storage_inline_put(smap);
kvfree(smap->buckets);
bpf_map_area_free(smap);
}
diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
index 1c59300..41e93ba 100644
--- a/net/core/bpf_sk_storage.c
+++ b/net/core/bpf_sk_storage.c
@@ -132,12 +132,13 @@ static long bpf_fd_sk_storage_delete_elem(struct bpf_map *map, void *key)
static struct bpf_local_storage_elem *
bpf_sk_storage_clone_elem(struct sock *newsk,
+ struct bpf_local_storage *new_sk_storage,
struct bpf_local_storage_map *smap,
struct bpf_local_storage_elem *selem)
{
struct bpf_local_storage_elem *copy_selem;
- copy_selem = bpf_selem_alloc(smap, newsk, NULL, false);
+ copy_selem = bpf_selem_alloc(smap, newsk, new_sk_storage, NULL, false);
if (!copy_selem)
return NULL;
@@ -189,7 +190,8 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
if (IS_ERR(map))
continue;
- copy_selem = bpf_sk_storage_clone_elem(newsk, smap, selem);
+ copy_selem = bpf_sk_storage_clone_elem(newsk, new_sk_storage,
+ smap, selem);
if (!copy_selem) {
ret = -ENOMEM;
bpf_map_put(map);
pw-bot: cr
prev parent reply other threads:[~2026-11-30 0:57 UTC|newest]
Thread overview: 15+ 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 ` 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=DN84Y298SZZL.3GHOLS9WYETHQ@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=kernel-team@meta.com \
--cc=mykyta.yatsenko5@gmail.com \
--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