* [PATCH bpf-next v2 01/13] bpf: Free unpublished local storage with kfree_nolock()
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 ` Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 02/13] bpf: Add obj_size to bpf_local_storage Mykyta Yatsenko
` (12 subsequent siblings)
13 siblings, 0 replies; 16+ messages in thread
From: Mykyta Yatsenko @ 2026-11-23 15:15 UTC (permalink / raw)
To: bpf, ast, andrii, daniel, kernel-team, eddyz87; +Cc: Mykyta Yatsenko
From: Mykyta Yatsenko <yatsenko@meta.com>
A tracing program can create local storage in NMI. When the create
fails, the new storage and selem are freed with kfree_rcu(), which is
not safe in NMI. Nobody has seen them yet, so free them at once with
kfree_nolock(), which works in any context for kmalloc_nolock() memory.
This fix needs commit bd897919e4dc ("bpf: Link the first element after
its storage is published"). Before that commit, an iterator can find the
first selem before the storage is published, so an immediate free is not
safe.
Fixes: f484f4a3e058 ("bpf: Replace bpf memory allocator with kmalloc_nolock() in local storage")
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
kernel/bpf/bpf_local_storage.c | 19 ++++++++++++++++---
1 file changed, 16 insertions(+), 3 deletions(-)
diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index b12fcec47ffa..3abfe7034df8 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -158,6 +158,18 @@ void bpf_selem_free(struct bpf_local_storage_elem *selem,
call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_trace_rcu);
}
+/*
+ * selem was never published: no reader can see it, and this works in NMI.
+ * Only the syscall update puts special fields (uptrs) in a new selem, and it
+ * never runs in NMI, so freeing them here is safe too.
+ */
+static void bpf_selem_free_unpublished(struct bpf_local_storage_elem *selem,
+ struct bpf_local_storage_map *smap)
+{
+ bpf_obj_free_fields(smap->map.record, SDATA(selem)->data);
+ kfree_nolock(selem);
+}
+
static void bpf_selem_free_list(struct hlist_head *list, bool reuse_now)
{
struct bpf_local_storage_elem *selem;
@@ -562,7 +574,8 @@ int bpf_local_storage_alloc(void *owner,
return 0;
uncharge:
- bpf_local_storage_free(storage, true);
+ /* storage was never published: no reader can see it */
+ kfree_nolock(storage);
mem_uncharge(smap, owner, sizeof(*storage));
return err;
}
@@ -605,7 +618,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
err = bpf_local_storage_alloc(owner, smap, selem);
if (err) {
- bpf_selem_free(selem, true);
+ bpf_selem_free_unpublished(selem, smap);
mem_uncharge(smap, owner, smap->elem_size);
return ERR_PTR(err);
}
@@ -692,7 +705,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
bpf_selem_free_list(&old_selem_free_list, false);
if (alloc_selem) {
mem_uncharge(smap, owner, smap->elem_size);
- bpf_selem_free(alloc_selem, true);
+ bpf_selem_free_unpublished(alloc_selem, smap);
}
return err ? ERR_PTR(err) : SDATA(selem);
}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH bpf-next v2 02/13] bpf: Add obj_size to bpf_local_storage
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 ` Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 03/13] bpf: Add bpf_local_storage_delete() Mykyta Yatsenko
` (11 subsequent siblings)
13 siblings, 0 replies; 16+ messages in thread
From: Mykyta Yatsenko @ 2026-11-23 15:15 UTC (permalink / raw)
To: bpf, ast, andrii, daniel, kernel-team, eddyz87; +Cc: Mykyta Yatsenko
From: Mykyta Yatsenko <yatsenko@meta.com>
A later patch makes storage objects that are bigger than the struct.
Keep the size in the object, and charge and uncharge that size.
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
include/linux/bpf_local_storage.h | 1 +
kernel/bpf/bpf_local_storage.c | 14 ++++++++------
2 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h
index 6c643ac51079..d336fc051054 100644
--- a/include/linux/bpf_local_storage.h
+++ b/include/linux/bpf_local_storage.h
@@ -100,6 +100,7 @@ struct bpf_local_storage {
*/
struct rcu_head rcu;
rqspinlock_t lock; /* Protect adding/removing from the "list" */
+ u32 obj_size; /* Size of this object. It does not change. */
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 */
};
diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index 3abfe7034df8..6f884c9fdcea 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -199,7 +199,7 @@ static void bpf_selem_unlink_storage_nolock_misc(struct bpf_local_storage_elem *
if (pin_owner && !refcount_inc_not_zero(&local_storage->owner_refcnt))
return;
- uncharge += free_local_storage ? sizeof(*local_storage) : 0;
+ uncharge += free_local_storage ? local_storage->obj_size : 0;
mem_uncharge(smap, local_storage->owner, uncharge);
local_storage->mem_charge -= uncharge;
@@ -517,15 +517,16 @@ int bpf_local_storage_alloc(void *owner,
struct bpf_local_storage *prev_storage, *storage;
struct bpf_local_storage **owner_storage_ptr;
struct bpf_local_storage_map_bucket *b;
+ u32 size = sizeof(*storage);
unsigned long flags;
int err;
- err = mem_charge(smap, owner, sizeof(*storage));
+ err = mem_charge(smap, owner, size);
if (err)
return err;
- storage = bpf_map_kmalloc_nolock(&smap->map, sizeof(*storage),
- __GFP_ZERO, NUMA_NO_NODE);
+ storage = bpf_map_kmalloc_nolock(&smap->map, size, __GFP_ZERO,
+ NUMA_NO_NODE);
if (!storage) {
err = -ENOMEM;
goto uncharge;
@@ -534,7 +535,8 @@ int bpf_local_storage_alloc(void *owner,
INIT_HLIST_HEAD(&storage->list);
raw_res_spin_lock_init(&storage->lock);
storage->owner = owner;
- storage->mem_charge = sizeof(*storage);
+ storage->obj_size = size;
+ storage->mem_charge = size;
refcount_set(&storage->owner_refcnt, 1);
bpf_selem_link_storage_nolock(storage, first_selem);
@@ -576,7 +578,7 @@ int bpf_local_storage_alloc(void *owner,
uncharge:
/* storage was never published: no reader can see it */
kfree_nolock(storage);
- mem_uncharge(smap, owner, sizeof(*storage));
+ mem_uncharge(smap, owner, size);
return err;
}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH bpf-next v2 03/13] bpf: Add bpf_local_storage_delete()
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 ` 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
` (10 subsequent siblings)
13 siblings, 0 replies; 16+ messages in thread
From: Mykyta Yatsenko @ 2026-11-23 15:15 UTC (permalink / raw)
To: bpf, ast, andrii, daniel, kernel-team, eddyz87; +Cc: Mykyta Yatsenko
From: Mykyta Yatsenko <yatsenko@meta.com>
The four owner types have the same delete code. A later patch changes how
a value is deleted. One function keeps that change in one place.
No functional change intended.
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
include/linux/bpf_local_storage.h | 2 ++
kernel/bpf/bpf_cgrp_storage.c | 8 +-------
kernel/bpf/bpf_inode_storage.c | 8 +-------
kernel/bpf/bpf_local_storage.c | 15 +++++++++++++++
kernel/bpf/bpf_task_storage.c | 8 +-------
net/core/bpf_sk_storage.c | 8 +-------
6 files changed, 21 insertions(+), 28 deletions(-)
diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h
index d336fc051054..ba8de9f5ec33 100644
--- a/include/linux/bpf_local_storage.h
+++ b/include/linux/bpf_local_storage.h
@@ -196,6 +196,8 @@ void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
int bpf_selem_unlink(struct bpf_local_storage_elem *selem);
+int bpf_local_storage_delete(void *owner, struct bpf_local_storage_map *smap);
+
int bpf_selem_link_map(struct bpf_local_storage_map *smap,
struct bpf_local_storage *local_storage,
struct bpf_local_storage_elem *selem);
diff --git a/kernel/bpf/bpf_cgrp_storage.c b/kernel/bpf/bpf_cgrp_storage.c
index c76e9b0fabba..d335e2965ea8 100644
--- a/kernel/bpf/bpf_cgrp_storage.c
+++ b/kernel/bpf/bpf_cgrp_storage.c
@@ -83,13 +83,7 @@ static long bpf_cgrp_storage_update_elem(struct bpf_map *map, void *key,
static int cgroup_storage_delete(struct cgroup *cgroup, struct bpf_map *map)
{
- struct bpf_local_storage_data *sdata;
-
- sdata = cgroup_storage_lookup(cgroup, map, false);
- if (!sdata)
- return -ENOENT;
-
- return bpf_selem_unlink(SELEM(sdata));
+ return bpf_local_storage_delete(cgroup, (struct bpf_local_storage_map *)map);
}
static long bpf_cgrp_storage_delete_elem(struct bpf_map *map, void *key)
diff --git a/kernel/bpf/bpf_inode_storage.c b/kernel/bpf/bpf_inode_storage.c
index f9e81060c1f4..07ba08707979 100644
--- a/kernel/bpf/bpf_inode_storage.c
+++ b/kernel/bpf/bpf_inode_storage.c
@@ -104,13 +104,7 @@ static long bpf_fd_inode_storage_update_elem(struct bpf_map *map, void *key,
static int inode_storage_delete(struct inode *inode, struct bpf_map *map)
{
- struct bpf_local_storage_data *sdata;
-
- sdata = inode_storage_lookup(inode, map, false);
- if (!sdata)
- return -ENOENT;
-
- return bpf_selem_unlink(SELEM(sdata));
+ return bpf_local_storage_delete(inode, (struct bpf_local_storage_map *)map);
}
static long bpf_fd_inode_storage_delete_elem(struct bpf_map *map, void *key)
diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index 6f884c9fdcea..2d2a627360fe 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -356,6 +356,21 @@ int bpf_selem_unlink(struct bpf_local_storage_elem *selem)
return err;
}
+int bpf_local_storage_delete(void *owner, struct bpf_local_storage_map *smap)
+{
+ struct bpf_local_storage __rcu **owner_ptr = owner_storage(smap, owner);
+ struct bpf_local_storage *local_storage;
+ struct bpf_local_storage_data *sdata;
+
+ /* An inode can have no LSM blob, so no storage pointer */
+ local_storage = owner_ptr ? rcu_dereference_check(*owner_ptr, bpf_rcu_lock_held()) : NULL;
+ sdata = local_storage ? bpf_local_storage_lookup(local_storage, smap, false) : NULL;
+ if (!sdata)
+ return -ENOENT;
+
+ return bpf_selem_unlink(SELEM(sdata));
+}
+
/*
* Unlink an selem from map and local storage with lockless fallback if callers
* are racing or rqspinlock returns error. It should only be called by
diff --git a/kernel/bpf/bpf_task_storage.c b/kernel/bpf/bpf_task_storage.c
index 4b342be29eac..1f73292755b4 100644
--- a/kernel/bpf/bpf_task_storage.c
+++ b/kernel/bpf/bpf_task_storage.c
@@ -128,13 +128,7 @@ static long bpf_pid_task_storage_update_elem(struct bpf_map *map, void *key,
static int task_storage_delete(struct task_struct *task, struct bpf_map *map)
{
- struct bpf_local_storage_data *sdata;
-
- sdata = task_storage_lookup(task, map, false);
- if (!sdata)
- return -ENOENT;
-
- return bpf_selem_unlink(SELEM(sdata));
+ return bpf_local_storage_delete(task, (struct bpf_local_storage_map *)map);
}
static long bpf_pid_task_storage_delete_elem(struct bpf_map *map, void *key)
diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
index 1c59300af05c..a838be80ebbd 100644
--- a/net/core/bpf_sk_storage.c
+++ b/net/core/bpf_sk_storage.c
@@ -34,13 +34,7 @@ bpf_sk_storage_lookup(struct sock *sk, struct bpf_map *map, bool cacheit_lockit)
static int bpf_sk_storage_del(struct sock *sk, struct bpf_map *map)
{
- struct bpf_local_storage_data *sdata;
-
- sdata = bpf_sk_storage_lookup(sk, map, false);
- if (!sdata)
- return -ENOENT;
-
- return bpf_selem_unlink(SELEM(sdata));
+ return bpf_local_storage_delete(sk, (struct bpf_local_storage_map *)map);
}
/* Called by __sk_destruct() & bpf_sk_storage_clone() */
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH bpf-next v2 04/13] bpf: Add an inline slot allocator to local storage caches
2026-11-23 15:15 [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Mykyta Yatsenko
` (2 preceding siblings ...)
2026-11-23 15:15 ` [PATCH bpf-next v2 03/13] bpf: Add bpf_local_storage_delete() Mykyta Yatsenko
@ 2026-11-23 15:15 ` 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
` (9 subsequent siblings)
13 siblings, 0 replies; 16+ messages in thread
From: Mykyta Yatsenko @ 2026-11-23 15:15 UTC (permalink / raw)
To: bpf, ast, andrii, daniel, kernel-team, eddyz87; +Cc: Mykyta Yatsenko
From: Mykyta Yatsenko <yatsenko@meta.com>
BPF_F_INLINE_VALUE maps will keep their values in the storage object, so
that a lookup needs no selem. Each owner type has one layout of slots for
all its objects. A table of the live inline maps holds the layout. Fail
map create with -ENOSPC if the area has no room, or if the owner type has
16 inline maps: a small table keeps the scans cheap.
The claim is in map_alloc(), with the other resources of the map, so
map_free() gives the slot back on every path. Thus -ENOSPC can come
before -EINVAL for special fields or bad BTF, and before the LSM hook. A
create that fails later holds a slot for a short time.
Old objects keep the bytes of a freed map. Each claim gets a new number.
A later patch makes the slot tag from it, so that a new map rarely sees
these bytes as its values.
The flag is not in the UAPI and not allowed yet: a later patch does that.
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
include/linux/bpf_local_storage.h | 31 ++++++++++
kernel/bpf/bpf_local_storage.c | 119 ++++++++++++++++++++++++++++++++++++++
2 files changed, 150 insertions(+)
diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h
index ba8de9f5ec33..f8e4e47b47c6 100644
--- a/include/linux/bpf_local_storage.h
+++ b/include/linux/bpf_local_storage.h
@@ -51,9 +51,17 @@ 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;
+ /*
+ * Byte offset of the inline slot of a BPF_F_INLINE_VALUE map from the
+ * start of the storage object, and the end of the slot. 0: no slot.
+ */
+ u16 inline_off;
+ u16 inline_end;
+ u32 inline_claim; /* Claim number of the slot, the source of its tag */
};
struct bpf_local_storage_data {
@@ -133,9 +141,32 @@ bpf_local_storage_unpin_owner(struct bpf_local_storage *local_storage)
#define BPF_LOCAL_STORAGE_CACHE_SIZE 16
+/*
+ * The inline area of a storage object goes from the end of the struct to
+ * at most 1 KB. A slot is an 8 byte tag, then the value.
+ */
+#define BPF_LOCAL_STORAGE_INLINE_MAX 1024
+#define BPF_LOCAL_STORAGE_INLINE_START ALIGN(sizeof(struct bpf_local_storage), 8)
+/*
+ * Inline maps per owner type. The same as the cache size on purpose: more
+ * maps than that share cache[] entries, so they gain less from a slot.
+ */
+#define BPF_LOCAL_STORAGE_INLINE_MAPS 16
+
+/* A later patch adds this flag to the UAPI and allows it */
+#define BPF_F_INLINE_VALUE BIT(20)
+
struct bpf_local_storage_cache {
spinlock_t idx_lock;
u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE];
+ /*
+ * The live inline maps, NULL if free. Few entries keep the scans in
+ * map create and sk diag cheap. Written under idx_lock.
+ */
+ struct bpf_local_storage_map __rcu *inline_maps[BPF_LOCAL_STORAGE_INLINE_MAPS];
+ /* Largest slot end in inline_maps[], so new objects read no map */
+ u32 inline_end;
+ u32 inline_claims; /* Number of claims, the source of slot tags */
};
#define DEFINE_BPF_STORAGE_CACHE(name) \
diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index 2d2a627360fe..eed6f18bf52a 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -759,6 +759,110 @@ static void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cac
spin_unlock(&cache->idx_lock);
}
+static struct bpf_local_storage_map *
+inline_map(struct bpf_local_storage_cache *cache, u32 i)
+{
+ return rcu_dereference_protected(cache->inline_maps[i],
+ lockdep_is_held(&cache->idx_lock));
+}
+
+/* A live slot that overlaps [off, end), or NULL */
+static struct bpf_local_storage_map *
+inline_overlap(struct bpf_local_storage_cache *cache, u32 off, u32 end)
+{
+ struct bpf_local_storage_map *m;
+ u32 i;
+
+ for (i = 0; i < BPF_LOCAL_STORAGE_INLINE_MAPS; i++) {
+ m = inline_map(cache, i);
+ if (m && off < m->inline_end && m->inline_off < end)
+ return m;
+ }
+ return NULL;
+}
+
+/*
+ * First fit. Slots are not sorted: scan again after each overlap. An overlap
+ * ends after off, so off grows and the loop ends. 0: no room.
+ */
+static u32 bpf_local_storage_inline_fit(struct bpf_local_storage_cache *cache,
+ u32 size)
+{
+ u32 off = BPF_LOCAL_STORAGE_INLINE_START;
+ struct bpf_local_storage_map *m;
+
+ while (off + size <= BPF_LOCAL_STORAGE_INLINE_MAX) {
+ m = inline_overlap(cache, off, off + size);
+ if (!m)
+ return off;
+ off = m->inline_end;
+ }
+ return 0;
+}
+
+/* A slot is an 8 byte tag, then the value, 8 byte aligned */
+static u32 bpf_local_storage_slot_size(u32 value_size)
+{
+ return 8 + round_up(value_size, 8);
+}
+
+/*
+ * Claim a table entry and the first free range for the slot of smap. Old
+ * objects keep the tag and value bytes of a freed map. A new claim number
+ * gives a tag that does not match old tags until the counter wraps, and
+ * rarely matches old value bytes. A program can write a match on purpose,
+ * but that needs CAP_BPF or a BPF token. This is memory safe: no pointer is
+ * read from a slot.
+ */
+static int bpf_local_storage_inline_get(struct bpf_local_storage_map *smap)
+{
+ struct bpf_local_storage_cache *cache = smap->cache;
+ u32 size = bpf_local_storage_slot_size(smap->map.value_size);
+ u32 i, off;
+
+ guard(spinlock)(&cache->idx_lock);
+ for (i = 0; i < BPF_LOCAL_STORAGE_INLINE_MAPS; i++)
+ if (!inline_map(cache, i))
+ break;
+ if (i == BPF_LOCAL_STORAGE_INLINE_MAPS)
+ return -ENOSPC;
+
+ off = bpf_local_storage_inline_fit(cache, size);
+ if (!off)
+ return -ENOSPC;
+
+ /* Never 0, so that no claim number reads as unset */
+ cache->inline_claims = cache->inline_claims + 1 ?: 1;
+ smap->inline_claim = cache->inline_claims;
+ smap->inline_off = off;
+ smap->inline_end = off + size;
+ WRITE_ONCE(cache->inline_end, max(cache->inline_end, smap->inline_end));
+ rcu_assign_pointer(cache->inline_maps[i], smap);
+ return 0;
+}
+
+/* Clear the entry before map free waits for RCU readers of inline_maps[] */
+static void bpf_local_storage_inline_free(struct bpf_local_storage_map *smap)
+{
+ struct bpf_local_storage_cache *cache = smap->cache;
+ struct bpf_local_storage_map *m;
+ bool found = false;
+ u32 i, end = 0;
+
+ guard(spinlock)(&cache->idx_lock);
+ for (i = 0; i < BPF_LOCAL_STORAGE_INLINE_MAPS; i++) {
+ m = inline_map(cache, i);
+ if (m == smap) {
+ RCU_INIT_POINTER(cache->inline_maps[i], NULL);
+ found = true;
+ } else if (m) {
+ end = max(end, m->inline_end);
+ }
+ }
+ WRITE_ONCE(cache->inline_end, end);
+ WARN_ON_ONCE(!found);
+}
+
int bpf_local_storage_map_alloc_check(union bpf_attr *attr)
{
if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK ||
@@ -863,6 +967,19 @@ 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;
+ /*
+ * Only map create calls map_alloc(). The kernel's copy of an inner map
+ * template holds no slot, but the map given as inner_map_fd holds one
+ * until it is freed. map_free() gives the slot back, also when a later
+ * step of map create fails.
+ */
+ if (attr->map_flags & BPF_F_INLINE_VALUE) {
+ err = bpf_local_storage_inline_get(smap);
+ if (err)
+ goto free_smap;
+ }
+
smap->cache_idx = bpf_local_storage_cache_idx_get(cache);
return &smap->map;
@@ -882,6 +999,8 @@ void bpf_local_storage_map_free(struct bpf_map *map,
smap = (struct bpf_local_storage_map *)map;
bpf_local_storage_cache_idx_free(cache, smap->cache_idx);
+ if (smap->inline_off)
+ bpf_local_storage_inline_free(smap);
/* Note that this map might be concurrently cloned from
* bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH bpf-next v2 05/13] bpf: Size new local storage objects for the inline area
2026-11-23 15:15 [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Mykyta Yatsenko
` (3 preceding siblings ...)
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 ` 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
` (8 subsequent siblings)
13 siblings, 0 replies; 16+ messages in thread
From: Mykyta Yatsenko @ 2026-11-23 15:15 UTC (permalink / raw)
To: bpf, ast, andrii, daniel, kernel-team, eddyz87; +Cc: Mykyta Yatsenko
From: Mykyta Yatsenko <yatsenko@meta.com>
A storage object must have room for the slots of the inline maps of its
owner type. Make a new object as big as the end of the last used slot.
The size of an object does not change after that, so the slots that an
object holds stay at fixed places.
No map can have a slot yet, so all objects keep the size of the struct.
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
kernel/bpf/bpf_local_storage.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index eed6f18bf52a..072807a1245f 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -525,6 +525,17 @@ static int check_flags(const struct bpf_local_storage_data *old_sdata,
return 0;
}
+/*
+ * Once an inline map exists, a new object gets an inline area up to the end
+ * of the last slot, also for other maps. Its size does not change.
+ */
+static u32 bpf_local_storage_obj_size(struct bpf_local_storage_map *smap)
+{
+ u32 end = max(READ_ONCE(smap->cache->inline_end), smap->inline_end);
+
+ return end ? kmalloc_size_roundup(end) : sizeof(struct bpf_local_storage);
+}
+
int bpf_local_storage_alloc(void *owner,
struct bpf_local_storage_map *smap,
struct bpf_local_storage_elem *first_selem)
@@ -532,7 +543,7 @@ int bpf_local_storage_alloc(void *owner,
struct bpf_local_storage *prev_storage, *storage;
struct bpf_local_storage **owner_storage_ptr;
struct bpf_local_storage_map_bucket *b;
- u32 size = sizeof(*storage);
+ u32 size = bpf_local_storage_obj_size(smap);
unsigned long flags;
int err;
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH bpf-next v2 06/13] bpf: Keep local storage objects with an inline area until owner death
2026-11-23 15:15 [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Mykyta Yatsenko
` (4 preceding siblings ...)
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 ` 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
` (7 subsequent siblings)
13 siblings, 0 replies; 16+ messages in thread
From: Mykyta Yatsenko @ 2026-11-23 15:15 UTC (permalink / raw)
To: bpf, ast, andrii, daniel, kernel-team, eddyz87; +Cc: Mykyta Yatsenko
From: Mykyta Yatsenko <yatsenko@meta.com>
A later patch lets programs get pointers to values in the inline area.
Such a value has no selem, so an empty list does not mean that nobody
uses the object. Free an object with an inline area only after destroy(),
and always after an RCU tasks trace grace period, so that sleepable
programs cannot see freed memory.
Updaters can no longer use an empty list to see that the object goes
away, so destroy() marks it as dying under the storage lock. If it
cannot take that lock, it tries again after a grace period, so the mark
adds no leak. A failed unlink of an selem leaks as before.
While an inline map exists, new objects of its owner type are bigger and
live until owner death, also when the owner uses only normal maps.
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
include/linux/bpf_local_storage.h | 11 +++++
kernel/bpf/bpf_local_storage.c | 96 +++++++++++++++++++++++++++++++++++----
2 files changed, 99 insertions(+), 8 deletions(-)
diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h
index f8e4e47b47c6..7a5f024622e6 100644
--- a/include/linux/bpf_local_storage.h
+++ b/include/linux/bpf_local_storage.h
@@ -111,8 +111,19 @@ struct bpf_local_storage {
u32 obj_size; /* Size of this object. It does not change. */
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 */
+ bool dying; /* destroy() has run. Only with an inline area */
};
+/*
+ * A program can hold a pointer into the inline area without an selem. So an
+ * object with an inline area lives until its owner goes away.
+ */
+static inline bool
+bpf_local_storage_has_inline(const struct bpf_local_storage *local_storage)
+{
+ return local_storage->obj_size > sizeof(*local_storage);
+}
+
/*
* 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
diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index 072807a1245f..b737b2e67c8a 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -114,7 +114,12 @@ static void bpf_local_storage_free(struct bpf_local_storage *local_storage,
if (!local_storage)
return;
- if (reuse_now) {
+ /*
+ * A sleepable program can still use an inline value: wait for it. A
+ * tasks trace grace period also covers plain RCU readers: RCU Tasks
+ * Trace is SRCU-fast, and its grace period calls synchronize_rcu().
+ */
+ if (reuse_now && !bpf_local_storage_has_inline(local_storage)) {
kfree_rcu(local_storage, rcu);
return;
}
@@ -184,6 +189,31 @@ static void bpf_selem_free_list(struct hlist_head *list, bool reuse_now)
bpf_selem_free(selem, reuse_now);
}
+/*
+ * Free local_storage when selem, its last selem, goes away. An object with an
+ * inline area can hold values with no selem, so free it only after destroy()
+ * marked it.
+ */
+static bool bpf_local_storage_free_last(struct bpf_local_storage *local_storage,
+ struct bpf_local_storage_elem *selem)
+{
+ if (!hlist_is_singular_node(&selem->snode, &local_storage->list))
+ return false;
+ return !bpf_local_storage_has_inline(local_storage) ||
+ READ_ONCE(local_storage->dying);
+}
+
+/*
+ * local_storage goes away: the owner needs a new one for a new value. An
+ * object with an inline area can be empty, so destroy() marks it instead.
+ */
+static bool bpf_local_storage_gone(const struct bpf_local_storage *local_storage)
+{
+ if (bpf_local_storage_has_inline(local_storage))
+ return READ_ONCE(local_storage->dying);
+ return hlist_empty(&local_storage->list);
+}
+
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,
@@ -227,8 +257,7 @@ static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_stor
smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
- free_local_storage = hlist_is_singular_node(&selem->snode,
- &local_storage->list);
+ free_local_storage = bpf_local_storage_free_last(local_storage, selem);
bpf_selem_unlink_storage_nolock_misc(selem, smap, local_storage,
free_local_storage, false);
@@ -440,8 +469,8 @@ static void bpf_selem_unlink_nofail(struct bpf_local_storage_elem *selem,
err = raw_res_spin_lock_irqsave(&local_storage->lock, flags);
if (!err) {
if (likely(selem_linked_to_storage(selem))) {
- free_storage = hlist_is_singular_node(&selem->snode,
- &local_storage->list);
+ free_storage = bpf_local_storage_free_last(local_storage,
+ selem);
/*
* Okay to skip clearing owner_storage and storage->owner in
* destroy() since the owner is going away. No user or bpf
@@ -634,7 +663,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
local_storage = rcu_dereference_check(*owner_storage(smap, owner),
bpf_rcu_lock_held());
- if (!local_storage || hlist_empty(&local_storage->list)) {
+ if (!local_storage || bpf_local_storage_gone(local_storage)) {
/* Very first elem for the owner */
err = check_flags(NULL, map_flags);
if (err)
@@ -683,7 +712,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
goto free_selem;
/* Recheck local_storage->list under local_storage->lock */
- if (unlikely(hlist_empty(&local_storage->list))) {
+ if (unlikely(bpf_local_storage_gone(local_storage))) {
/* A parallel del is happening and local_storage is going
* away. It has just been checked before, so very
* unlikely. Return instead of retry to keep things
@@ -901,6 +930,48 @@ int bpf_local_storage_map_check_btf(struct bpf_map *map,
return 0;
}
+/*
+ * A storage is freed when its last selem goes away. An object with an inline
+ * area can hold values with an empty list, so mark it as dying under the
+ * lock: from now on updaters return -EAGAIN, and the last selem frees the
+ * object. Free an empty object now.
+ *
+ * If destroy() could not take the lock to unlink an selem, the selem stays
+ * linked. A racing map free that read its storage pointer before destroy()
+ * cleared it unlinks it and frees the object. Else the object leaks, as the
+ * selem does: map free skips the storage, and bpf_selem_unlink_nofail()
+ * warns.
+ *
+ * Only a timeout can fail the lock here: destroy() holds no other lock.
+ * Then try again after a grace period. Nothing frees the object while dying
+ * is false, so its rcu head is free. destroy() never runs in NMI.
+ */
+static void bpf_local_storage_mark_dying_rcu(struct rcu_head *rcu);
+
+static void bpf_local_storage_mark_dying(struct bpf_local_storage *local_storage)
+{
+ unsigned long flags;
+ bool empty;
+
+ if (raw_res_spin_lock_irqsave(&local_storage->lock, flags)) {
+ call_rcu_tasks_trace(&local_storage->rcu,
+ bpf_local_storage_mark_dying_rcu);
+ return;
+ }
+ WRITE_ONCE(local_storage->dying, true);
+ empty = hlist_empty(&local_storage->list);
+ raw_res_spin_unlock_irqrestore(&local_storage->lock, flags);
+
+ if (empty)
+ bpf_local_storage_free(local_storage, false);
+}
+
+static void bpf_local_storage_mark_dying_rcu(struct rcu_head *rcu)
+{
+ bpf_local_storage_mark_dying(container_of(rcu, struct bpf_local_storage,
+ rcu));
+}
+
/*
* Destroy local storage when the owner is going away. Caller must uncharge memory
* if memory charging is used.
@@ -908,6 +979,7 @@ int bpf_local_storage_map_check_btf(struct bpf_map *map,
u32 bpf_local_storage_destroy(struct bpf_local_storage *local_storage)
{
struct bpf_local_storage_elem *selem;
+ u32 mem;
/* Neither the bpf_prog nor the bpf_map's syscall
* could be modifying the local_storage->list now.
@@ -931,7 +1003,15 @@ u32 bpf_local_storage_destroy(struct bpf_local_storage *local_storage)
smp_mb();
}
- return local_storage->mem_charge;
+ /*
+ * The loop must not free an object with an inline area: only the mark
+ * lets the last selem free it, so mark it after the loop. The mark can
+ * free the object, so read mem_charge first.
+ */
+ mem = local_storage->mem_charge;
+ if (bpf_local_storage_has_inline(local_storage))
+ bpf_local_storage_mark_dying(local_storage);
+ return mem;
}
u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map)
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH bpf-next v2 07/13] bpf: Keep values of BPF_F_INLINE_VALUE maps in the storage object
2026-11-23 15:15 [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Mykyta Yatsenko
` (5 preceding siblings ...)
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 ` 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
` (6 subsequent siblings)
13 siblings, 0 replies; 16+ messages in thread
From: Mykyta Yatsenko @ 2026-11-23 15:15 UTC (permalink / raw)
To: bpf, ast, andrii, daniel, kernel-team, eddyz87; +Cc: Mykyta Yatsenko
From: Mykyta Yatsenko <yatsenko@meta.com>
A lookup of a normal map reads the storage object, a cache slot and
then the selem that holds the value. When the owner is cold, each read
can miss. A map with a slot finds its value at a fixed offset in the
object, with one tag compare, so a lookup has one dependent load less.
The tag is 8 bytes and comes from the claim number of the map, so old
value bytes rarely match it. It is written with a release store after
the value and read with an acquire load. 32-bit has no 8 byte acquire
and release, so slots are 64-bit only.
An update copies the value in place under the storage lock. Lockless
readers (programs, the syscall lookup, sk diag) can see a torn value, as
in array maps. KCSAN can report this race; it is intended.
A normal map pays one more test of a map field. There is no hint on
that test, so that inline maps do not pay for normal maps.
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
include/linux/bpf_local_storage.h | 63 ++++++++++++++++++-
kernel/bpf/bpf_local_storage.c | 129 ++++++++++++++++++++++++++++++++++----
net/core/bpf_sk_storage.c | 9 ++-
3 files changed, 180 insertions(+), 21 deletions(-)
diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h
index 7a5f024622e6..d482ff6a1448 100644
--- a/include/linux/bpf_local_storage.h
+++ b/include/linux/bpf_local_storage.h
@@ -70,8 +70,16 @@ struct bpf_local_storage_data {
*
* Put it in the same cacheline as the data to minimize
* the number of cachelines accessed during the cache hit case.
+ *
+ * An inline slot has the same layout, with a tag in place of smap:
+ * the tag of the map whose value is in the slot. Any other tag (0
+ * after a delete) means no value. A slot is not in an selem: never
+ * apply SELEM() to it.
*/
- struct bpf_local_storage_map __rcu *smap;
+ union {
+ struct bpf_local_storage_map __rcu *smap;
+ u64 inline_tag;
+ };
u8 data[] __aligned(8);
};
@@ -195,6 +203,45 @@ bpf_local_storage_map_alloc(union bpf_attr *attr,
void __bpf_local_storage_insert_cache(struct bpf_local_storage *local_storage,
struct bpf_local_storage_map *smap,
struct bpf_local_storage_elem *selem);
+
+/*
+ * smap has an inline slot. The tag needs an 8 byte acquire and release, so
+ * slots are 64-bit only: on 32-bit, false lets the compiler drop the slot
+ * code. There is no hint on the inline_off test, so that inline maps do not
+ * pay for normal maps.
+ */
+static __always_inline bool
+bpf_local_storage_map_inline(const struct bpf_local_storage_map *smap)
+{
+ return IS_ENABLED(CONFIG_64BIT) && smap->inline_off;
+}
+
+/* The inline slot of smap, or NULL if local_storage does not hold it */
+static __always_inline struct bpf_local_storage_data *
+bpf_local_storage_inline(struct bpf_local_storage *local_storage,
+ struct bpf_local_storage_map *smap)
+{
+ if (!bpf_local_storage_map_inline(smap) ||
+ smap->inline_end > local_storage->obj_size)
+ return NULL;
+ return (void *)local_storage + smap->inline_off;
+}
+
+static inline u64 bpf_local_storage_inline_tag(const struct bpf_local_storage_map *smap)
+{
+ return (u64)~smap->inline_claim << 32 | smap->inline_claim;
+}
+
+/* The slot holds a value of smap */
+static __always_inline bool
+bpf_local_storage_inline_valid(struct bpf_local_storage_data *sdata,
+ const struct bpf_local_storage_map *smap)
+{
+ /* Pairs with the tag release in an update: see the value */
+ return smp_load_acquire(&sdata->inline_tag) ==
+ bpf_local_storage_inline_tag(smap);
+}
+
/* If cacheit_lockit is false, this lookup function is lockless */
static inline struct bpf_local_storage_data *
bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
@@ -204,6 +251,15 @@ bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
struct bpf_local_storage_data *sdata;
struct bpf_local_storage_elem *selem;
+ /*
+ * An object that holds the slot of smap never has an selem of smap:
+ * the object size does not change, and a create puts the value in
+ * the slot.
+ */
+ sdata = bpf_local_storage_inline(local_storage, smap);
+ if (sdata)
+ return bpf_local_storage_inline_valid(sdata, smap) ? sdata : NULL;
+
/* Fast path (cache hit) */
sdata = rcu_dereference_check(local_storage->cache[smap->cache_idx],
bpf_rcu_lock_held());
@@ -251,10 +307,11 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,
void bpf_selem_free(struct bpf_local_storage_elem *selem,
bool reuse_now);
-int
+struct bpf_local_storage *
bpf_local_storage_alloc(void *owner,
struct bpf_local_storage_map *smap,
- struct bpf_local_storage_elem *first_selem);
+ struct bpf_local_storage_elem *first_selem,
+ void *value);
struct bpf_local_storage_data *
bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index b737b2e67c8a..31f62a2be62b 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -390,6 +390,8 @@ int bpf_local_storage_delete(void *owner, struct bpf_local_storage_map *smap)
struct bpf_local_storage __rcu **owner_ptr = owner_storage(smap, owner);
struct bpf_local_storage *local_storage;
struct bpf_local_storage_data *sdata;
+ unsigned long flags;
+ int err;
/* An inode can have no LSM blob, so no storage pointer */
local_storage = owner_ptr ? rcu_dereference_check(*owner_ptr, bpf_rcu_lock_held()) : NULL;
@@ -397,7 +399,24 @@ int bpf_local_storage_delete(void *owner, struct bpf_local_storage_map *smap)
if (!sdata)
return -ENOENT;
- return bpf_selem_unlink(SELEM(sdata));
+ if (sdata != bpf_local_storage_inline(local_storage, smap))
+ return bpf_selem_unlink(SELEM(sdata));
+
+ /* As bpf_selem_unlink(): the result must not depend on the place */
+ if (in_nmi())
+ return -EOPNOTSUPP;
+
+ /* The value bytes stay: a program can still hold a pointer to them */
+ err = raw_res_spin_lock_irqsave(&local_storage->lock, flags);
+ if (err)
+ return err;
+ /* A racing delete can clear the tag before this one takes the lock */
+ if (sdata->inline_tag == bpf_local_storage_inline_tag(smap))
+ WRITE_ONCE(sdata->inline_tag, 0);
+ else
+ err = -ENOENT;
+ raw_res_spin_unlock_irqrestore(&local_storage->lock, flags);
+ return err;
}
/*
@@ -565,20 +584,30 @@ static u32 bpf_local_storage_obj_size(struct bpf_local_storage_map *smap)
return end ? kmalloc_size_roundup(end) : sizeof(struct bpf_local_storage);
}
-int bpf_local_storage_alloc(void *owner,
- struct bpf_local_storage_map *smap,
- struct bpf_local_storage_elem *first_selem)
+/*
+ * The first value for owner goes into first_selem or, if first_selem is NULL,
+ * into the inline slot of smap (value, or zeros if value is NULL). Return the
+ * new storage, so that the caller does not read the owner pointer again.
+ */
+struct bpf_local_storage *
+bpf_local_storage_alloc(void *owner, struct bpf_local_storage_map *smap,
+ struct bpf_local_storage_elem *first_selem, void *value)
{
+ struct bpf_local_storage **owner_storage_ptr =
+ (struct bpf_local_storage **)owner_storage(smap, owner);
struct bpf_local_storage *prev_storage, *storage;
- struct bpf_local_storage **owner_storage_ptr;
struct bpf_local_storage_map_bucket *b;
u32 size = bpf_local_storage_obj_size(smap);
+ struct bpf_local_storage_data *sdata;
unsigned long flags;
int err;
+ /* The new object holds the slot of an inline map: no selem for it */
+ WARN_ON_ONCE(first_selem && bpf_local_storage_map_inline(smap));
+
err = mem_charge(smap, owner, size);
if (err)
- return err;
+ return ERR_PTR(err);
storage = bpf_map_kmalloc_nolock(&smap->map, size, __GFP_ZERO,
NUMA_NO_NODE);
@@ -594,6 +623,24 @@ int bpf_local_storage_alloc(void *owner,
storage->mem_charge = size;
refcount_set(&storage->owner_refcnt, 1);
+ if (!first_selem) {
+ /*
+ * Nobody can see storage before it is published, so fill the
+ * slot with no lock. With no selem, nobody can find storage
+ * through the map, so no bucket lock either. cmpxchg() orders
+ * the slot before the pointer. sdata is not NULL:
+ * bpf_local_storage_obj_size() covers the slot of smap.
+ */
+ sdata = bpf_local_storage_inline(storage, smap);
+ if (value)
+ copy_map_value(&smap->map, sdata->data, value);
+ sdata->inline_tag = bpf_local_storage_inline_tag(smap);
+ if (likely(!cmpxchg(owner_storage_ptr, NULL, storage)))
+ return storage;
+ err = -EAGAIN;
+ goto uncharge;
+ }
+
bpf_selem_link_storage_nolock(storage, first_selem);
b = select_bucket(smap, storage);
@@ -601,8 +648,6 @@ int bpf_local_storage_alloc(void *owner,
if (err)
goto uncharge;
- owner_storage_ptr =
- (struct bpf_local_storage **)owner_storage(smap, owner);
/* Publish storage to the owner.
* Instead of using any lock of the kernel object (i.e. owner),
* cmpxchg will work with any kernel object regardless what
@@ -628,13 +673,53 @@ int bpf_local_storage_alloc(void *owner,
bpf_selem_link_map_nolock(b, first_selem);
raw_res_spin_unlock_irqrestore(&b->lock, flags);
- return 0;
+ return storage;
uncharge:
/* storage was never published: no reader can see it */
kfree_nolock(storage);
mem_uncharge(smap, owner, size);
- return err;
+ return ERR_PTR(err);
+}
+
+/* Write the value of smap into its slot, in place */
+static struct bpf_local_storage_data *
+bpf_local_storage_update_inline(struct bpf_local_storage *local_storage,
+ struct bpf_local_storage_map *smap,
+ void *value, u64 map_flags)
+{
+ u64 tag = bpf_local_storage_inline_tag(smap);
+ struct bpf_local_storage_data *sdata;
+ unsigned long flags;
+ int err;
+
+ sdata = bpf_local_storage_inline(local_storage, smap);
+ err = raw_res_spin_lock_irqsave(&local_storage->lock, flags);
+ if (err)
+ return ERR_PTR(err);
+
+ /* As an empty list in the selem path: destroy() has started */
+ if (unlikely(bpf_local_storage_gone(local_storage)))
+ err = -EAGAIN;
+ else
+ err = check_flags(sdata->inline_tag == tag ? sdata : NULL, map_flags);
+ if (err)
+ goto unlock;
+
+ /*
+ * Lockless readers (programs, the syscall lookup, sk diag) can see a
+ * torn value. This race is intended, as in array maps. The init value
+ * can be the old value in the slot: keep it.
+ */
+ if (!value)
+ memset(sdata->data, 0, smap->map.value_size);
+ else if (value != sdata->data)
+ copy_map_value(&smap->map, sdata->data, value);
+ /* A reader that sees the tag must see the value */
+ smp_store_release(&sdata->inline_tag, tag);
+unlock:
+ raw_res_spin_unlock_irqrestore(&local_storage->lock, flags);
+ return err ? ERR_PTR(err) : sdata;
}
/* sk cannot be going away because it is linking new elem
@@ -663,26 +748,44 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
local_storage = rcu_dereference_check(*owner_storage(smap, owner),
bpf_rcu_lock_held());
+ /* One gone() read decides slot or selem: a second read can differ */
if (!local_storage || bpf_local_storage_gone(local_storage)) {
/* Very first elem for the owner */
err = check_flags(NULL, map_flags);
if (err)
return ERR_PTR(err);
+ /* destroy() has started: no new object can be published */
+ if (local_storage && READ_ONCE(local_storage->dying))
+ return ERR_PTR(-EAGAIN);
+
+ if (bpf_local_storage_map_inline(smap)) {
+ /* Only destroy() frees the new object with the slot */
+ local_storage = bpf_local_storage_alloc(owner, smap, NULL,
+ value);
+ return IS_ERR(local_storage) ? ERR_CAST(local_storage) :
+ bpf_local_storage_inline(local_storage, smap);
+ }
+
selem = bpf_selem_alloc(smap, owner, value, swap_uptrs);
if (!selem)
return ERR_PTR(-ENOMEM);
- err = bpf_local_storage_alloc(owner, smap, selem);
- if (err) {
+ local_storage = bpf_local_storage_alloc(owner, smap, selem, NULL);
+ if (IS_ERR(local_storage)) {
bpf_selem_free_unpublished(selem, smap);
mem_uncharge(smap, owner, smap->elem_size);
- return ERR_PTR(err);
+ return ERR_CAST(local_storage);
}
return SDATA(selem);
}
+ /* An object without room for the slot keeps the value in an selem */
+ if (bpf_local_storage_inline(local_storage, smap))
+ return bpf_local_storage_update_inline(local_storage, smap,
+ value, map_flags);
+
if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) {
/* Hoping to find an old_sdata to do inline update
* such that it can avoid taking the local_storage->lock
diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
index a838be80ebbd..409213755feb 100644
--- a/net/core/bpf_sk_storage.c
+++ b/net/core/bpf_sk_storage.c
@@ -201,17 +201,16 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
}
bpf_selem_link_storage_nolock(new_sk_storage, copy_selem);
} else {
- ret = bpf_local_storage_alloc(newsk, smap, copy_selem);
- if (ret) {
+ new_sk_storage = bpf_local_storage_alloc(newsk, smap,
+ copy_selem, NULL);
+ if (IS_ERR(new_sk_storage)) {
+ ret = PTR_ERR(new_sk_storage);
bpf_selem_free(copy_selem, true);
atomic_sub(smap->elem_size,
&newsk->sk_omem_alloc);
bpf_map_put(map);
goto out;
}
-
- new_sk_storage =
- rcu_dereference(copy_selem->local_storage);
}
bpf_map_put(map);
}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH bpf-next v2 08/13] bpf: Refuse the sk storage map iterator for BPF_F_INLINE_VALUE maps
2026-11-23 15:15 [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Mykyta Yatsenko
` (6 preceding siblings ...)
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 ` Mykyta Yatsenko
2026-11-23 15:15 ` [PATCH bpf-next v2 09/13] bpf: Show inline values in sk diag Mykyta Yatsenko
` (5 subsequent siblings)
13 siblings, 0 replies; 16+ messages in thread
From: Mykyta Yatsenko @ 2026-11-23 15:15 UTC (permalink / raw)
To: bpf, ast, andrii, daniel, kernel-team, eddyz87; +Cc: Mykyta Yatsenko
From: Mykyta Yatsenko <yatsenko@meta.com>
Inline values are not linked to the map, so the iterator would miss
them.
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
net/core/bpf_sk_storage.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
index 409213755feb..d0eaf4de8090 100644
--- a/net/core/bpf_sk_storage.c
+++ b/net/core/bpf_sk_storage.c
@@ -938,6 +938,12 @@ static int bpf_iter_attach_map(struct bpf_prog *prog,
if (map->map_type != BPF_MAP_TYPE_SK_STORAGE)
goto put_map;
+ /* The iterator walks the selems of the map: it cannot see inline values */
+ if (map->map_flags & BPF_F_INLINE_VALUE) {
+ err = -EOPNOTSUPP;
+ goto put_map;
+ }
+
aux->map = map;
err = bpf_iter_map_check_prog(prog, aux);
if (err)
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH bpf-next v2 09/13] bpf: Show inline values in sk diag
2026-11-23 15:15 [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Mykyta Yatsenko
` (7 preceding siblings ...)
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 ` 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
` (4 subsequent siblings)
13 siblings, 0 replies; 16+ messages in thread
From: Mykyta Yatsenko @ 2026-11-23 15:15 UTC (permalink / raw)
To: bpf, ast, andrii, daniel, kernel-team, eddyz87; +Cc: Mykyta Yatsenko
From: Mykyta Yatsenko <yatsenko@meta.com>
Inline values have no selem, and the list of an object can be empty when
all its values are inline. A request that names maps finds them with the
lookup, so it must not stop at an empty list. A dump of all maps walks
the list only: let it also walk the table of live inline maps. Both walks
skip a map that goes away (id 0): user space cannot name it.
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
net/core/bpf_sk_storage.c | 48 ++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 41 insertions(+), 7 deletions(-)
diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
index d0eaf4de8090..cae3606609ae 100644
--- a/net/core/bpf_sk_storage.c
+++ b/net/core/bpf_sk_storage.c
@@ -529,7 +529,17 @@ bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs)
}
EXPORT_SYMBOL_GPL(bpf_sk_storage_diag_alloc);
-static int diag_get(struct bpf_local_storage_map *smap,
+/*
+ * Map free can set the map id to 0 at any time, so read it once. User space
+ * cannot name a map with id 0: skip it.
+ */
+static u32 diag_map_id(struct bpf_local_storage_map *smap)
+{
+ return smap ? READ_ONCE(smap->map.id) : 0;
+}
+
+/* id is the map id that the caller read once */
+static int diag_get(struct bpf_local_storage_map *smap, u32 id,
struct bpf_local_storage_data *sdata, struct sk_buff *skb)
{
struct nlattr *nla_stg, *nla_value;
@@ -541,7 +551,7 @@ static int diag_get(struct bpf_local_storage_map *smap,
if (!nla_stg)
return -EMSGSIZE;
- if (nla_put_u32(skb, SK_DIAG_BPF_STORAGE_MAP_ID, smap->map.id))
+ if (nla_put_u32(skb, SK_DIAG_BPF_STORAGE_MAP_ID, id))
goto errout;
nla_value = nla_reserve_64bit(skb, SK_DIAG_BPF_STORAGE_MAP_VALUE,
@@ -572,16 +582,19 @@ static int bpf_sk_storage_diag_put_all(struct sock *sk, struct sk_buff *skb,
/* stg_array_type (e.g. INET_DIAG_BPF_SK_STORAGES) */
unsigned int diag_size = nla_total_size(0);
struct bpf_local_storage *sk_storage;
+ struct bpf_local_storage_data *sdata;
struct bpf_local_storage_elem *selem;
struct bpf_local_storage_map *smap;
struct nlattr *nla_stgs;
unsigned int saved_len;
+ u32 i, id, nr;
int err = 0;
rcu_read_lock();
sk_storage = rcu_dereference(sk->sk_bpf_storage);
- if (!sk_storage || hlist_empty(&sk_storage->list)) {
+ /* The list can be empty when the values are inline */
+ if (!sk_storage) {
rcu_read_unlock();
return 0;
}
@@ -594,15 +607,34 @@ static int bpf_sk_storage_diag_put_all(struct sock *sk, struct sk_buff *skb,
saved_len = skb->len;
hlist_for_each_entry_rcu(selem, &sk_storage->list, snode) {
smap = rcu_dereference(SDATA(selem)->smap);
- if (!smap)
+ id = diag_map_id(smap);
+ if (!id)
continue;
diag_size += nla_value_size(smap->map.value_size);
- if (nla_stgs && diag_get(smap, SDATA(selem), skb))
+ if (nla_stgs && diag_get(smap, id, SDATA(selem), skb))
/* Continue to learn diag_size */
err = -EMSGSIZE;
}
+ /*
+ * Inline values have no selem: find them through the inline maps. A
+ * plain object holds no slot.
+ */
+ nr = bpf_local_storage_has_inline(sk_storage) ?
+ BPF_LOCAL_STORAGE_INLINE_MAPS : 0;
+ for (i = 0; i < nr; i++) {
+ smap = rcu_dereference(sk_cache.inline_maps[i]);
+ id = diag_map_id(smap);
+ sdata = id ? bpf_local_storage_inline(sk_storage, smap) : NULL;
+ if (!sdata || !bpf_local_storage_inline_valid(sdata, smap))
+ continue;
+ diag_size += nla_value_size(smap->map.value_size);
+
+ if (nla_stgs && diag_get(smap, id, sdata, skb))
+ err = -EMSGSIZE;
+ }
+
rcu_read_unlock();
if (nla_stgs) {
@@ -644,7 +676,8 @@ int bpf_sk_storage_diag_put(struct bpf_sk_storage_diag *diag,
rcu_read_lock();
sk_storage = rcu_dereference(sk->sk_bpf_storage);
- if (!sk_storage || hlist_empty(&sk_storage->list)) {
+ /* The list can be empty when the values are inline */
+ if (!sk_storage) {
rcu_read_unlock();
return 0;
}
@@ -665,7 +698,8 @@ int bpf_sk_storage_diag_put(struct bpf_sk_storage_diag *diag,
diag_size += nla_value_size(diag->maps[i]->value_size);
- if (nla_stgs && diag_get((struct bpf_local_storage_map *)diag->maps[i], sdata, skb))
+ if (nla_stgs && diag_get((struct bpf_local_storage_map *)diag->maps[i],
+ diag->maps[i]->id, sdata, skb))
/* Continue to learn diag_size */
err = -EMSGSIZE;
}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH bpf-next v2 10/13] bpf: Allow BPF_F_INLINE_VALUE for local storage maps
2026-11-23 15:15 [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Mykyta Yatsenko
` (8 preceding siblings ...)
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 ` 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
` (3 subsequent siblings)
13 siblings, 0 replies; 16+ messages in thread
From: Mykyta Yatsenko @ 2026-11-23 15:15 UTC (permalink / raw)
To: bpf, ast, andrii, daniel, kernel-team, eddyz87; +Cc: Mykyta Yatsenko
From: Mykyta Yatsenko <yatsenko@meta.com>
A lookup of a local storage map reads the selem that holds the value.
When the owner is cold, this read can miss on each packet. Let sk, task,
inode and cgroup storage maps keep their values in the storage object
instead, so that a lookup has one dependent load less.
Map create fails with -E2BIG if the value never fits in the inline area,
with -ENOSPC if the area has no room now or the owner type has 16 inline
maps, with -EINVAL with BPF_F_CLONE or special fields, and with
-EOPNOTSUPP on 32-bit. Documentation/bpf/map_sk_storage.rst gives the
rules and costs.
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
include/linux/bpf_local_storage.h | 3 ---
include/uapi/linux/bpf.h | 7 +++++++
kernel/bpf/bpf_local_storage.c | 19 ++++++++++++++++++-
tools/include/uapi/linux/bpf.h | 7 +++++++
4 files changed, 32 insertions(+), 4 deletions(-)
diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h
index d482ff6a1448..4d9ae3a11b94 100644
--- a/include/linux/bpf_local_storage.h
+++ b/include/linux/bpf_local_storage.h
@@ -172,9 +172,6 @@ bpf_local_storage_unpin_owner(struct bpf_local_storage *local_storage)
*/
#define BPF_LOCAL_STORAGE_INLINE_MAPS 16
-/* A later patch adds this flag to the UAPI and allows it */
-#define BPF_F_INLINE_VALUE BIT(20)
-
struct bpf_local_storage_cache {
spinlock_t idx_lock;
u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE];
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 62de0919db72..9eac0930af95 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1513,6 +1513,13 @@ enum {
/* Enable BPF ringbuf overwrite mode */
BPF_F_RB_OVERWRITE = (1U << 19),
+
+/*
+ * Local storage maps: keep the value in the storage object of the owner.
+ * Readers take no lock, so they can see torn values. 64-bit only. The rules
+ * and errors are in Documentation/bpf/map_sk_storage.rst.
+ */
+ 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 31f62a2be62b..aaeaafe60e6d 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -15,7 +15,8 @@
#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 struct bpf_local_storage_map_bucket *
select_bucket(struct bpf_local_storage_map *smap,
@@ -1010,15 +1011,27 @@ int bpf_local_storage_map_alloc_check(union bpf_attr *attr)
{
if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK ||
!(attr->map_flags & BPF_F_NO_PREALLOC) ||
+ ((attr->map_flags & BPF_F_CLONE) &&
+ (attr->map_flags & BPF_F_INLINE_VALUE)) ||
attr->max_entries ||
attr->key_size != sizeof(int) || !attr->value_size ||
/* Enforce BTF for userspace sk dumping */
!attr->btf_key_type_id || !attr->btf_value_type_id)
return -EINVAL;
+ /* The slot tag needs an 8 byte acquire and release */
+ if (!IS_ENABLED(CONFIG_64BIT) && (attr->map_flags & BPF_F_INLINE_VALUE))
+ return -EOPNOTSUPP;
+
if (attr->value_size > BPF_LOCAL_STORAGE_MAX_VALUE_SIZE)
return -E2BIG;
+ /* Never fits, also in an empty area: -ENOSPC is only for no room now */
+ if ((attr->map_flags & BPF_F_INLINE_VALUE) &&
+ bpf_local_storage_slot_size(attr->value_size) >
+ BPF_LOCAL_STORAGE_INLINE_MAX - BPF_LOCAL_STORAGE_INLINE_START)
+ return -E2BIG;
+
return 0;
}
@@ -1030,6 +1043,10 @@ int bpf_local_storage_map_check_btf(struct bpf_map *map,
if (!btf_type_is_i32(key_type))
return -EINVAL;
+ /* An inline value is copied in place and is not freed with a delete */
+ if ((map->map_flags & BPF_F_INLINE_VALUE) && !IS_ERR_OR_NULL(map->record))
+ return -EINVAL;
+
return 0;
}
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 62de0919db72..9eac0930af95 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1513,6 +1513,13 @@ enum {
/* Enable BPF ringbuf overwrite mode */
BPF_F_RB_OVERWRITE = (1U << 19),
+
+/*
+ * Local storage maps: keep the value in the storage object of the owner.
+ * Readers take no lock, so they can see torn values. 64-bit only. The rules
+ * and errors are in Documentation/bpf/map_sk_storage.rst.
+ */
+ BPF_F_INLINE_VALUE = (1U << 20),
};
/* Flags for BPF_PROG_QUERY. */
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH bpf-next v2 11/13] libbpf: Do not retry local storage map create without BTF
2026-11-23 15:15 [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Mykyta Yatsenko
` (9 preceding siblings ...)
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 ` 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
` (2 subsequent siblings)
13 siblings, 0 replies; 16+ messages in thread
From: Mykyta Yatsenko @ 2026-11-23 15:15 UTC (permalink / raw)
To: bpf, ast, andrii, daniel, kernel-team, eddyz87; +Cc: Mykyta Yatsenko
From: Mykyta Yatsenko <yatsenko@meta.com>
Local storage maps need BTF, so the retry without BTF always fails with
-EINVAL. That hides the real error, for example -ENOSPC, -E2BIG or
-EOPNOTSUPP for a BPF_F_INLINE_VALUE map, and the application cannot see
when to create the map without the flag. Return the first error.
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
tools/lib/bpf/libbpf.c | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 1efc0dd37b2e..01185fa917c9 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -5531,6 +5531,20 @@ bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
static void bpf_map__destroy(struct bpf_map *map);
+/* The kernel refuses these map types without BTF */
+static bool map_type_needs_btf(enum bpf_map_type type)
+{
+ switch (type) {
+ case BPF_MAP_TYPE_SK_STORAGE:
+ case BPF_MAP_TYPE_INODE_STORAGE:
+ case BPF_MAP_TYPE_TASK_STORAGE:
+ case BPF_MAP_TYPE_CGRP_STORAGE:
+ return true;
+ default:
+ return false;
+ }
+}
+
static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner)
{
LIBBPF_OPTS(bpf_map_create_opts, create_attr);
@@ -5631,7 +5645,8 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b
def->key_size, def->value_size,
def->max_entries, &create_attr);
}
- if (map_fd < 0 && (create_attr.btf_key_type_id || create_attr.btf_value_type_id)) {
+ if (map_fd < 0 && (create_attr.btf_key_type_id || create_attr.btf_value_type_id) &&
+ !map_type_needs_btf(def->type)) {
err = -errno;
pr_warn("Error in bpf_create_map_xattr(%s): %s. Retrying without BTF.\n",
map->name, errstr(err));
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH bpf-next v2 12/13] selftests/bpf: Test BPF_F_INLINE_VALUE local storage maps
2026-11-23 15:15 [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Mykyta Yatsenko
` (10 preceding siblings ...)
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 ` 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
13 siblings, 0 replies; 16+ messages in thread
From: Mykyta Yatsenko @ 2026-11-23 15:15 UTC (permalink / raw)
To: bpf, ast, andrii, daniel, kernel-team, eddyz87; +Cc: Mykyta Yatsenko
From: Mykyta Yatsenko <yatsenko@meta.com>
Inline maps keep values in a slot of the storage object and have new
limits. Make sure that they behave as normal maps and that the limits
hold.
Test that values in the slot are the same through the syscall and a
program for sk storage, and through a program for task storage, also
after a delete in a program. The program records where each value is, so
the test proves that the slot path runs. Test the map limits and errors:
-E2BIG for a value that never fits, also through libbpf, -ENOSPC for a
full area and after 16 maps, and the undo of a slot claim. Test the
fallback for an object with no room for the slot. Test that a new map at
the offset of a freed map sees no old value.
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
.../bpf/prog_tests/local_storage_inline.c | 583 +++++++++++++++++++++
.../selftests/bpf/progs/local_storage_inline.c | 123 +++++
2 files changed, 706 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/local_storage_inline.c b/tools/testing/selftests/bpf/prog_tests/local_storage_inline.c
new file mode 100644
index 000000000000..5c062609c695
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/local_storage_inline.c
@@ -0,0 +1,583 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <netinet/in.h>
+#include <pthread.h>
+#include <sys/socket.h>
+#include <test_progs.h>
+#include <bpf/btf.h>
+#include "task_local_storage_helpers.h"
+
+/* The same as in progs/local_storage_inline.c */
+#define LS_VAL_WORDS 4
+#define NR_MAPS 4
+/* The sk maps come first. The syscall needs a key: the test has a socket fd */
+#define NR_SK_MAPS 2
+
+struct ls_val {
+ __u64 w[LS_VAL_WORDS];
+};
+
+#include "local_storage_inline.skel.h"
+
+#define INLINE_FLAGS (BPF_F_NO_PREALLOC | BPF_F_INLINE_VALUE)
+/* All inline maps of one owner type share an area that ends at 1 KB */
+#define AREA_END 1024
+#define MAX_INLINE_MAPS 16
+#define NR_SOCKS 2
+/*
+ * Map free is deferred: the slot of a closed map can stay taken for a time.
+ * All waits share one time budget, so that the test fails fast after the
+ * first timeout.
+ */
+#define WAIT_BUDGET_NS (30ULL * 1000 * 1000 * 1000)
+#define WAIT_US 10000
+
+static __u64 wait_end;
+
+/* Sleep a little and return true, or return false if the budget is spent */
+static bool wait_more(void)
+{
+ if (get_time_ns() >= wait_end)
+ return false;
+ usleep(WAIT_US);
+ return true;
+}
+
+/* A create that must fail with err. Close the map if it worked */
+static void assert_err(int fd, int err, const char *name)
+{
+ if (!ASSERT_EQ(fd, err, name) && fd >= 0)
+ close(fd);
+}
+
+/* A create that must work. The test needs only the result */
+static void assert_fd_close(int fd, const char *name)
+{
+ if (ASSERT_OK_FD(fd, name))
+ close(fd);
+}
+
+static void close_fds(const int *fds, int n)
+{
+ int i;
+
+ for (i = 0; i < n; i++)
+ if (fds[i] >= 0)
+ close(fds[i]);
+}
+
+struct socks {
+ int fd[NR_SOCKS];
+ bool ok; /* All sockets are open */
+};
+
+static struct socks open_socks(void)
+{
+ struct socks s = { .ok = true };
+ int i;
+
+ for (i = 0; i < NR_SOCKS; i++) {
+ s.fd[i] = socket(AF_INET, SOCK_STREAM, 0);
+ if (!ASSERT_OK_FD(s.fd[i], "socket"))
+ s.ok = false;
+ }
+ return s;
+}
+
+/* BTF with an int key (id 1) and, as the last type, a value of value_size bytes */
+static struct btf *load_btf(__u32 value_size)
+{
+ struct btf *btf;
+
+ btf = btf__new_empty();
+ if (!btf)
+ return NULL;
+ btf__add_int(btf, "int", 4, BTF_INT_SIGNED);
+ btf__add_int(btf, "char", 1, 0);
+ btf__add_array(btf, 1, 2, value_size);
+ if (btf__load_into_kernel(btf)) {
+ btf__free(btf);
+ return NULL;
+ }
+ return btf;
+}
+
+static int map_create_btf(struct btf *btf, enum bpf_map_type type, __u32 flags,
+ __u32 value_size)
+{
+ LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = flags,
+ .btf_fd = btf__fd(btf), .btf_key_type_id = 1,
+ .btf_value_type_id = btf__type_cnt(btf) - 1);
+
+ return bpf_map_create(type, "ls_inline", sizeof(int), value_size, 0, &opts);
+}
+
+/*
+ * Make a local storage map with an int key. With retry, wait while the slots
+ * of closed maps are not free. A create that must fail does not retry.
+ */
+static int create_map(enum bpf_map_type type, __u32 flags, __u32 value_size,
+ bool retry)
+{
+ struct btf *btf = load_btf(value_size);
+ int fd;
+
+ if (!ASSERT_OK_PTR(btf, "load_btf"))
+ return -ENOMEM;
+
+ do {
+ fd = map_create_btf(btf, type, flags, value_size);
+ } while (retry && fd == -ENOSPC && wait_more());
+ btf__free(btf);
+ return fd;
+}
+
+/*
+ * The largest inline value. The area starts after struct bpf_local_storage,
+ * aligned to 8 B. A slot is an 8 B tag and the value, aligned to 8 B.
+ */
+static int read_value_max(void)
+{
+ const struct btf_type *t;
+ struct btf *btf;
+ int id, max = -1;
+
+ btf = btf__load_vmlinux_btf();
+ if (!ASSERT_OK_PTR(btf, "btf__load_vmlinux_btf"))
+ return -1;
+ id = btf__find_by_name_kind(btf, "bpf_local_storage", BTF_KIND_STRUCT);
+ if (id > 0) {
+ t = btf__type_by_id(btf, id);
+ max = AREA_END - (t->size + 7) / 8 * 8 - 8;
+ }
+ btf__free(btf);
+ return max;
+}
+
+static void fill_val(struct ls_val *v, __u64 first)
+{
+ int i;
+
+ for (i = 0; i < LS_VAL_WORDS; i++)
+ v->w[i] = first + i;
+}
+
+/* Run the program on sk and on the current task. Return the number of NULL gets */
+static int run_prog(struct local_storage_inline *skel, int sk, __u64 get_flags)
+{
+ socklen_t len = sizeof(int);
+ int ttl;
+
+ skel->bss->get_flags = get_flags;
+ skel->bss->nr_null = 0;
+ ASSERT_OK(getsockopt(sk, SOL_IP, IP_TTL, &ttl, &len), "getsockopt");
+ return skel->bss->nr_null;
+}
+
+/* The value that get i saw is in the storage object of its owner: a slot */
+static bool in_obj(struct local_storage_inline *skel, int i)
+{
+ __u64 off = skel->bss->val_addr[i] - skel->bss->obj_addr[i];
+
+ return skel->bss->obj_addr[i] && off < skel->bss->obj_size[i];
+}
+
+/* The program and the syscall must see the same value in the sk maps */
+static void check_values(struct local_storage_inline *skel, const int *fds, int sk,
+ const struct ls_val *v)
+{
+ struct ls_val out;
+ int i;
+
+ for (i = 0; i < NR_SK_MAPS; i++) {
+ ASSERT_MEMEQ(&skel->bss->seen_val[i], v, sizeof(*v), "seen");
+ ASSERT_OK(bpf_map_lookup_elem(fds[i], &sk, &out), "lookup");
+ ASSERT_MEMEQ(&out, v, sizeof(*v), "lookup value");
+ }
+}
+
+/* The program must see v in the task maps */
+static void check_task_values(struct local_storage_inline *skel, const struct ls_val *v)
+{
+ int i;
+
+ for (i = NR_SK_MAPS; i < NR_MAPS; i++)
+ ASSERT_MEMEQ(&skel->bss->seen_val[i], v, sizeof(*v), "task seen");
+}
+
+enum { OP_UPDATE, OP_DELETE, OP_LOOKUP };
+
+/* All maps get the same ops: an inline map must give the codes of a normal map */
+static void check_ops(int fd, int key)
+{
+ static const struct {
+ int op;
+ __u64 flags;
+ int ret;
+ } ops[] = {
+ { OP_DELETE, 0, 0 },
+ { OP_DELETE, 0, -ENOENT },
+ { OP_LOOKUP, 0, -ENOENT },
+ { OP_UPDATE, BPF_EXIST, -ENOENT },
+ { OP_UPDATE, BPF_NOEXIST, 0 },
+ { OP_UPDATE, BPF_NOEXIST, -EEXIST },
+ { OP_UPDATE, BPF_F_LOCK, -EINVAL },
+ { OP_UPDATE, BPF_ANY, 0 },
+ { OP_DELETE, 0, 0 },
+ { OP_UPDATE, BPF_ANY, 0 },
+ { OP_UPDATE, BPF_EXIST, 0 },
+ { OP_LOOKUP, 0, 0 },
+ };
+ struct ls_val v, out;
+ char name[16];
+ int i, ret;
+
+ for (i = 0; i < ARRAY_SIZE(ops); i++) {
+ fill_val(&v, 0x4000 + i * 0x10);
+ if (ops[i].op == OP_UPDATE)
+ ret = bpf_map_update_elem(fd, &key, &v, ops[i].flags);
+ else if (ops[i].op == OP_DELETE)
+ ret = bpf_map_delete_elem(fd, &key);
+ else
+ ret = bpf_map_lookup_elem(fd, &key, &out);
+ snprintf(name, sizeof(name), "op %d", i);
+ ASSERT_EQ(ret, ops[i].ret, name);
+ }
+ /* The last op reads the value of the update before it */
+ fill_val(&v, 0x4000 + (i - 2) * 0x10);
+ ASSERT_MEMEQ(&out, &v, sizeof(v), "last value");
+}
+
+/* Runs in a new thread: the task is the owner of the task values */
+static void test_values(int max)
+{
+ struct local_storage_inline *skel;
+ int fds[NR_SK_MAPS], i, j, sk = -1;
+ struct ls_val v, zero = {};
+
+ skel = local_storage_inline__open();
+ if (!ASSERT_OK_PTR(skel, "skel open"))
+ return;
+ if (!ASSERT_OK(bpf_map__set_map_flags(skel->maps.sk_inline, INLINE_FLAGS), "flags") ||
+ !ASSERT_OK(bpf_map__set_map_flags(skel->maps.task_inline, INLINE_FLAGS), "flags") ||
+ !ASSERT_OK(local_storage_inline__load(skel), "skel load") ||
+ !ASSERT_OK(local_storage_inline__attach(skel), "attach"))
+ goto out;
+ sk = socket(AF_INET, SOCK_STREAM, 0);
+ if (!ASSERT_OK_FD(sk, "socket"))
+ goto out;
+
+ /* The order of the maps in the program */
+ fds[0] = bpf_map__fd(skel->maps.sk_inline);
+ fds[1] = bpf_map__fd(skel->maps.sk_normal);
+ skel->bss->target_tid = sys_gettid();
+
+ fill_val(&skel->bss->init_val, 0x1000);
+ skel->bss->use_init = true;
+ ASSERT_EQ(run_prog(skel, sk, BPF_LOCAL_STORAGE_GET_F_CREATE), 0,
+ "create with init");
+ check_values(skel, fds, sk, &skel->bss->init_val);
+ check_task_values(skel, &skel->bss->init_val);
+ /* The program finds inline values in a slot, normal values elsewhere */
+ for (i = 0; i < NR_MAPS; i++)
+ ASSERT_EQ(in_obj(skel, i), !(i & 1), "value in object");
+
+ /* The program sees a syscall update, and the syscall a program write */
+ fill_val(&v, 0x2000);
+ for (i = 0; i < NR_SK_MAPS; i++)
+ ASSERT_OK(bpf_map_update_elem(fds[i], &sk, &v, BPF_EXIST), "update");
+ skel->bss->val_add = 0x100;
+ ASSERT_EQ(run_prog(skel, sk, 0), 0, "get");
+ fill_val(&v, 0x2100);
+ check_values(skel, fds, sk, &v);
+ fill_val(&v, 0x1100);
+ check_task_values(skel, &v);
+
+ /*
+ * A delete in the program must give the codes of a normal map. A slot
+ * keeps its bytes after a delete, but they must not show.
+ */
+ skel->bss->do_delete = true;
+ for (i = 0; i < 2; i++) {
+ ASSERT_EQ(run_prog(skel, sk, 0), i ? NR_MAPS : 0, "get before delete");
+ for (j = 0; j < NR_MAPS; j++)
+ ASSERT_EQ(skel->bss->del_ret[j], i ? -ENOENT : 0, "prog delete");
+ }
+ skel->bss->do_delete = false;
+ skel->bss->use_init = false;
+ skel->bss->val_add = 0;
+ ASSERT_EQ(run_prog(skel, sk, BPF_LOCAL_STORAGE_GET_F_CREATE), 0, "create");
+ check_values(skel, fds, sk, &zero);
+ check_task_values(skel, &zero);
+
+ skel->bss->target_tid = 0;
+ for (i = 0; i < NR_SK_MAPS; i++)
+ check_ops(fds[i], sk);
+out:
+ if (sk >= 0)
+ close(sk);
+ local_storage_inline__destroy(skel);
+}
+
+/*
+ * A value that never fits gets -E2BIG at once, with no retry. The biggest
+ * value takes the full area: retry until the maps of earlier subtests are
+ * freed.
+ */
+static void check_biggest(int max)
+{
+ int big;
+
+ assert_err(create_map(BPF_MAP_TYPE_TASK_STORAGE, INLINE_FLAGS, max + 1, false),
+ -E2BIG, "value too big");
+ big = create_map(BPF_MAP_TYPE_TASK_STORAGE, INLINE_FLAGS, max, true);
+ if (!ASSERT_OK_FD(big, "biggest value"))
+ return;
+ /* No room now: the biggest value takes the full area */
+ assert_err(create_map(BPF_MAP_TYPE_TASK_STORAGE, INLINE_FLAGS, 8, false),
+ -ENOSPC, "no room now");
+ close(big);
+ assert_fd_close(create_map(BPF_MAP_TYPE_TASK_STORAGE, INLINE_FLAGS, 8, true),
+ "room after free");
+}
+
+static const char excl_hash[32];
+
+/* A create that fails after the slot claim gives the slot back */
+static void check_claim_undo(void)
+{
+ LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = INLINE_FLAGS,
+ .btf_key_type_id = 1, .excl_prog_hash = excl_hash,
+ .excl_prog_hash_size = 1);
+ struct btf *btf = load_btf(8);
+ int i, fd = -EINVAL;
+
+ if (!ASSERT_OK_PTR(btf, "load_btf"))
+ return;
+ opts.btf_fd = btf__fd(btf);
+ opts.btf_value_type_id = btf__type_cnt(btf) - 1;
+ /* More failures than table entries: each must give its entry back */
+ for (i = 0; i <= MAX_INLINE_MAPS && fd == -EINVAL; i++)
+ fd = bpf_map_create(BPF_MAP_TYPE_TASK_STORAGE, "ls_inline",
+ sizeof(int), 8, 0, &opts);
+ assert_err(fd, -EINVAL, "bad excl_prog_hash_size");
+ btf__free(btf);
+}
+
+/* libbpf gives the error of the kernel, not -EINVAL of a retry without BTF */
+static void check_load_err(int max)
+{
+ struct local_storage_inline *skel;
+
+ skel = local_storage_inline__open();
+ if (!ASSERT_OK_PTR(skel, "skel open"))
+ return;
+ if (ASSERT_OK(bpf_map__set_map_flags(skel->maps.task_inline, INLINE_FLAGS), "flags") &&
+ ASSERT_OK(bpf_map__set_value_size(skel->maps.task_inline, max + 1), "value size"))
+ ASSERT_EQ(local_storage_inline__load(skel), -E2BIG, "skeleton load");
+ local_storage_inline__destroy(skel);
+}
+
+static void test_limits(int max)
+{
+ int fds[MAX_INLINE_MAPS], i;
+
+ check_biggest(max);
+ check_load_err(max);
+
+ for (i = 0; i < MAX_INLINE_MAPS; i++) {
+ fds[i] = create_map(BPF_MAP_TYPE_TASK_STORAGE, INLINE_FLAGS, 8, true);
+ if (!ASSERT_OK_FD(fds[i], "inline map"))
+ break;
+ /* The first map proves room: the failed creates need no retry */
+ if (!i)
+ check_claim_undo();
+ }
+ if (i == MAX_INLINE_MAPS) {
+ /* The area has room, but the owner type has 16 inline maps */
+ assert_err(create_map(BPF_MAP_TYPE_TASK_STORAGE, INLINE_FLAGS, 8, false),
+ -ENOSPC, "too many maps");
+ /* Each owner type has its own limit */
+ assert_fd_close(create_map(BPF_MAP_TYPE_SK_STORAGE, INLINE_FLAGS, 8, true),
+ "other owner type");
+ }
+ close_fds(fds, i);
+}
+
+/* Load the program with map_fd in place of sk_reuse, and attach it */
+static struct local_storage_inline *load_reuse(int map_fd)
+{
+ struct local_storage_inline *skel;
+
+ skel = local_storage_inline__open();
+ if (!ASSERT_OK_PTR(skel, "skel open"))
+ return NULL;
+ if (!ASSERT_OK(bpf_map__reuse_fd(skel->maps.sk_reuse, map_fd), "reuse_fd") ||
+ !ASSERT_OK(local_storage_inline__load(skel), "skel load") ||
+ !ASSERT_OK(local_storage_inline__attach(skel), "attach")) {
+ local_storage_inline__destroy(skel);
+ return NULL;
+ }
+ skel->bss->target_tid = sys_gettid();
+ skel->bss->reuse_test = true;
+ return skel;
+}
+
+static void test_fallback(int max)
+{
+ struct socks s = open_socks();
+ struct local_storage_inline *skel;
+ int normal, map = -1, i;
+ char val[AREA_END], out[AREA_END];
+
+ normal = create_map(BPF_MAP_TYPE_SK_STORAGE, BPF_F_NO_PREALLOC, 4, false);
+ if (!s.ok || !ASSERT_OK_FD(normal, "normal map"))
+ goto out;
+
+ /* sk 0 gets its storage before the inline map exists, sk 1 after */
+ ASSERT_OK(bpf_map_update_elem(normal, &s.fd[0], &s.fd[0], 0), "normal update");
+ /* The slot takes the full area, so the storage of sk 0 has no room */
+ map = create_map(BPF_MAP_TYPE_SK_STORAGE, INLINE_FLAGS, max, true);
+ if (!ASSERT_OK_FD(map, "inline map"))
+ goto out;
+
+ for (i = 0; i < 2; i++) {
+ memset(val, 0x5a + i, max);
+ ASSERT_OK(bpf_map_update_elem(map, &s.fd[i], val, BPF_NOEXIST), "update");
+ }
+ /* sk 0 keeps the value in a normal element, sk 1 in the slot */
+ skel = load_reuse(map);
+ for (i = 0; skel && i < 2; i++) {
+ ASSERT_EQ(run_prog(skel, s.fd[i], 0), 0, "prog get");
+ ASSERT_EQ(in_obj(skel, 0), i, "value in object");
+ }
+ local_storage_inline__destroy(skel);
+ for (i = 0; i < 2; i++) {
+ memset(val, 0x5a + i, max);
+ ASSERT_OK(bpf_map_lookup_elem(map, &s.fd[i], out), "lookup");
+ ASSERT_MEMEQ(out, val, max, "lookup value");
+ ASSERT_OK(bpf_map_delete_elem(map, &s.fd[i]), "delete");
+ }
+ ASSERT_OK(bpf_map_lookup_elem(normal, &s.fd[0], out), "normal lookup");
+ ASSERT_EQ(*(int *)out, s.fd[0], "normal value");
+out:
+ close_fds(s.fd, NR_SOCKS);
+ if (normal >= 0)
+ close(normal);
+ if (map >= 0)
+ close(map);
+}
+
+/*
+ * Map B gets the slot of the freed map A in a live object. The old tag and
+ * value bytes of A must not show as a value of B.
+ */
+static void test_reuse(int max)
+{
+ struct local_storage_inline *skel = NULL;
+ struct socks s = open_socks();
+ int a, b = -1, i;
+ char val[AREA_END], out[AREA_END];
+ struct ls_val zero = {};
+
+ if (!s.ok)
+ goto out;
+ /* The biggest value: the slot of A and of B starts at the area start */
+ a = create_map(BPF_MAP_TYPE_SK_STORAGE, INLINE_FLAGS, max, true);
+ if (!ASSERT_OK_FD(a, "map A"))
+ goto out;
+ memset(val, 0x5a, max);
+ for (i = 0; i < 2; i++)
+ ASSERT_OK(bpf_map_update_elem(a, &s.fd[i], val, BPF_NOEXIST), "update A");
+ close(a);
+
+ /* B gets room only after the deferred free of A, with a new tag */
+ b = create_map(BPF_MAP_TYPE_SK_STORAGE, INLINE_FLAGS, max, true);
+ if (!ASSERT_OK_FD(b, "map B"))
+ goto out;
+ for (i = 0; i < 2; i++)
+ ASSERT_EQ(bpf_map_lookup_elem(b, &s.fd[i], out), -ENOENT, "lookup B");
+
+ memset(val, 0xa5, max);
+ ASSERT_OK(bpf_map_update_elem(b, &s.fd[0], val, BPF_NOEXIST), "syscall create");
+ ASSERT_OK(bpf_map_lookup_elem(b, &s.fd[0], out), "lookup syscall value");
+ ASSERT_MEMEQ(out, val, max, "syscall value");
+
+ skel = load_reuse(b);
+ if (!skel)
+ goto out;
+ ASSERT_EQ(run_prog(skel, s.fd[1], BPF_LOCAL_STORAGE_GET_F_CREATE), 0,
+ "prog create");
+ ASSERT_MEMEQ(&skel->bss->seen_val[0], &zero, sizeof(zero), "prog value");
+ memset(val, 0, max);
+ ASSERT_OK(bpf_map_lookup_elem(b, &s.fd[1], out), "lookup prog value");
+ ASSERT_MEMEQ(out, val, max, "prog value zero");
+out:
+ local_storage_inline__destroy(skel);
+ close_fds(s.fd, NR_SOCKS);
+ if (b >= 0)
+ close(b);
+}
+
+struct subtest {
+ const char *name;
+ void (*fn)(int max);
+ bool thread; /* Task storage: a new task has a new storage object */
+};
+
+struct thread_arg {
+ const struct subtest *t;
+ int max;
+};
+
+static void *subtest_thread(void *arg)
+{
+ struct thread_arg *a = arg;
+
+ a->t->fn(a->max);
+ return NULL;
+}
+
+static void run_subtest(const struct subtest *t, int max)
+{
+ struct thread_arg a = { t, max };
+ pthread_t thread;
+
+ if (!t->thread) {
+ t->fn(max);
+ return;
+ }
+ if (ASSERT_OK(pthread_create(&thread, NULL, subtest_thread, &a), "pthread_create"))
+ pthread_join(thread, NULL);
+}
+
+/* Serial: all inline maps of an owner type share one area and one limit */
+void serial_test_local_storage_inline(void)
+{
+ static const struct subtest tests[] = {
+ { "values", test_values, true },
+ { "limits", test_limits },
+ { "fallback", test_fallback },
+ { "reuse", test_reuse },
+ };
+ int fd, i, max;
+
+ wait_end = get_time_ns() + WAIT_BUDGET_NS;
+ /* 32-bit kernels refuse the flag */
+ fd = create_map(BPF_MAP_TYPE_SK_STORAGE, INLINE_FLAGS, 8, true);
+ if (fd == -EOPNOTSUPP) {
+ test__skip();
+ return;
+ }
+ assert_fd_close(fd, "inline map");
+ max = read_value_max();
+ if (!ASSERT_GT(max, 0, "inline value max"))
+ return;
+
+ for (i = 0; i < ARRAY_SIZE(tests); i++) {
+ if (!test__start_subtest(tests[i].name))
+ continue;
+ run_subtest(&tests[i], max);
+ }
+}
diff --git a/tools/testing/selftests/bpf/progs/local_storage_inline.c b/tools/testing/selftests/bpf/progs/local_storage_inline.c
new file mode 100644
index 000000000000..aef16b6947ec
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/local_storage_inline.c
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+#define LS_VAL_WORDS 4
+/* Maps that getsockopt_enter gets values from */
+#define NR_MAPS 4
+
+struct ls_val {
+ __u64 w[LS_VAL_WORDS];
+};
+
+/* User space sets BPF_F_INLINE_VALUE on the *_inline maps before load */
+#define STORAGE_MAP(name, map_type) \
+ struct { \
+ __uint(type, map_type); \
+ __uint(map_flags, BPF_F_NO_PREALLOC); \
+ __type(key, int); \
+ __type(value, struct ls_val); \
+ } name SEC(".maps")
+
+STORAGE_MAP(sk_inline, BPF_MAP_TYPE_SK_STORAGE);
+STORAGE_MAP(sk_normal, BPF_MAP_TYPE_SK_STORAGE);
+STORAGE_MAP(task_inline, BPF_MAP_TYPE_TASK_STORAGE);
+STORAGE_MAP(task_normal, BPF_MAP_TYPE_TASK_STORAGE);
+/* User space can replace it with a map of a bigger value */
+STORAGE_MAP(sk_reuse, BPF_MAP_TYPE_SK_STORAGE);
+
+int target_tid;
+__u64 get_flags;
+bool use_init;
+__u64 val_add;
+struct ls_val init_val;
+/* The value that each get saw, in the order of the maps above */
+struct ls_val seen_val[NR_MAPS];
+int nr_null;
+bool reuse_test;
+bool do_delete;
+/* The return code of each delete, in the order of the maps above */
+int del_ret[NR_MAPS];
+/*
+ * Where each value is: its address, and the address and size of the
+ * storage object of its owner. User space checks if the value is in it.
+ */
+__u64 val_addr[NR_MAPS];
+__u64 obj_addr[NR_MAPS];
+__u32 obj_size[NR_MAPS];
+
+static __always_inline void record(struct ls_val *v, int i,
+ struct bpf_local_storage *obj)
+{
+ int j;
+
+ if (!v) {
+ nr_null++;
+ return;
+ }
+ val_addr[i] = (__u64)v;
+ obj_addr[i] = (__u64)obj;
+ obj_size[i] = obj ? obj->obj_size : 0;
+ /* Write, so that user space sees that the program found the value */
+ for (j = 0; j < LS_VAL_WORDS; j++)
+ v->w[j] += val_add;
+ seen_val[i] = *v;
+}
+
+/*
+ * Get first, then read the storage pointer of the owner: the get can make
+ * the storage object.
+ */
+static __always_inline void record_sk(void *map, struct sock *sk, void *v, int i)
+{
+ v = bpf_sk_storage_get(map, sk, v, get_flags);
+ record(v, i, sk->sk_bpf_storage);
+}
+
+static __always_inline void record_task(void *map, struct task_struct *task,
+ void *v, int i)
+{
+ v = bpf_task_storage_get(map, task, v, get_flags);
+ record(v, i, task->bpf_storage);
+}
+
+static __always_inline bool is_target(void)
+{
+ return (__u32)bpf_get_current_pid_tgid() == target_tid;
+}
+
+/* getsockopt(SOL_IP) on a TCP socket gives the socket and the task */
+SEC("fentry/sock_common_getsockopt")
+int BPF_PROG(getsockopt_enter, struct socket *sock)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+ struct sock *sk = sock->sk;
+ void *v;
+
+ if (!is_target() || !sk)
+ return 0;
+
+ if (reuse_test) {
+ /* The value of sk_reuse can be bigger than init_val: no init */
+ record_sk(&sk_reuse, sk, NULL, 0);
+ return 0;
+ }
+
+ v = use_init ? &init_val : NULL;
+ record_sk(&sk_inline, sk, v, 0);
+ record_sk(&sk_normal, sk, v, 1);
+ record_task(&task_inline, task, v, 2);
+ record_task(&task_normal, task, v, 3);
+ if (do_delete) {
+ del_ret[0] = bpf_sk_storage_delete(&sk_inline, sk);
+ del_ret[1] = bpf_sk_storage_delete(&sk_normal, sk);
+ del_ret[2] = bpf_task_storage_delete(&task_inline, task);
+ del_ret[3] = bpf_task_storage_delete(&task_normal, task);
+ }
+ return 0;
+}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH bpf-next v2 13/13] bpf, docs: Document BPF_F_INLINE_VALUE for local storage maps
2026-11-23 15:15 [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Mykyta Yatsenko
` (11 preceding siblings ...)
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 ` Mykyta Yatsenko
2026-11-30 0:57 ` [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Alexei Starovoitov
13 siblings, 0 replies; 16+ messages in thread
From: Mykyta Yatsenko @ 2026-11-23 15:15 UTC (permalink / raw)
To: bpf, ast, andrii, daniel, kernel-team, eddyz87; +Cc: Mykyta Yatsenko
From: Mykyta Yatsenko <yatsenko@meta.com>
Users need the rules of the new flag, its costs and the -ENOSPC
fallback. Task and inode storage have no docs, so the sk storage doc
gives the rules for all owner types.
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
Documentation/bpf/map_cgrp_storage.rst | 3 +
Documentation/bpf/map_sk_storage.rst | 103 ++++++++++++++++++++++++++++++++-
2 files changed, 105 insertions(+), 1 deletion(-)
diff --git a/Documentation/bpf/map_cgrp_storage.rst b/Documentation/bpf/map_cgrp_storage.rst
index 5d3f603efffa..a636f2635cd7 100644
--- a/Documentation/bpf/map_cgrp_storage.rst
+++ b/Documentation/bpf/map_cgrp_storage.rst
@@ -31,6 +31,9 @@ The local storage can be removed with ``bpf_cgrp_storage_delete``::
The map is available to all program types.
+A map created with ``BPF_F_INLINE_VALUE`` keeps its value in the storage object
+of the cgroup. Documentation/bpf/map_sk_storage.rst gives the rules.
+
Examples
========
diff --git a/Documentation/bpf/map_sk_storage.rst b/Documentation/bpf/map_sk_storage.rst
index 4e9d23ab9ecd..a1e06f987a5c 100644
--- a/Documentation/bpf/map_sk_storage.rst
+++ b/Documentation/bpf/map_sk_storage.rst
@@ -14,7 +14,9 @@ to be provided and acts as the handle for accessing the socket-local
storage. The values for maps of type ``BPF_MAP_TYPE_SK_STORAGE`` are stored
locally with each socket instead of with the map. The kernel is responsible for
allocating storage for a socket when requested and for freeing the storage when
-either the map or the socket is deleted.
+either the map or the socket is deleted. Values of a ``BPF_F_INLINE_VALUE`` map
+that are in the inline area are freed only with the socket. A value in a normal
+element is freed as for other maps (see `Inline values`_).
.. note::
- The key type must be ``int`` and ``max_entries`` must be set to ``0``.
@@ -108,6 +110,105 @@ Socket-local storage for map ``map_fd`` can be deleted from a socket using the
socket identified by a `socket` ``fd`` stored in the pointer ``key``. Returns
``0`` on success, or negative error in case of failure.
+Inline values
+=============
+
+A map created with ``BPF_F_INLINE_VALUE`` keeps its value in the storage object
+of the socket, not in a separate element, so a lookup has one dependent load
+less. The flag also works for ``BPF_MAP_TYPE_TASK_STORAGE``,
+``BPF_MAP_TYPE_INODE_STORAGE`` and ``BPF_MAP_TYPE_CGRP_STORAGE``. Inline maps
+work only on 64-bit kernels.
+
+All inline maps of one owner type share an inline area at the end of each
+storage object. A storage object with an inline area is at most 1 KB, including
+``struct bpf_local_storage``. On x86-64, the inline area is 840 bytes and the
+largest inline value is 832 bytes, because each value has an 8 byte tag. An
+owner type can have at most 16 inline maps. These limits can change between
+kernel versions.
+
+- Map create fails with ``-EOPNOTSUPP`` on a 32-bit kernel.
+- Map create fails with ``-E2BIG`` if the value does not fit even in an empty
+ area. It fails with ``-ENOSPC`` only if the area has no room for the value
+ now, or if the owner type has 16 inline maps. The application can then
+ create the map without the flag.
+- The kernel takes the slot early in map create. So ``-ENOSPC`` can come
+ before ``-EINVAL`` for special fields or bad BTF, and before the LSM check.
+ A create that fails after that holds a slot for a short time.
+- Map create fails with ``-EINVAL`` with ``BPF_F_CLONE``, or if the value has
+ a spin lock, a kptr, a refcount or an uptr (task storage). Other special
+ fields are refused for all local storage maps. An update with ``BPF_F_LOCK``
+ fails with ``-EINVAL``.
+- An inline map needs ``CAP_BPF``, as other local storage maps do. A BPF token
+ can give it inside a user namespace. The area and the limit of 16 maps are
+ shared by the whole host, also with users of a BPF token. One such user can
+ take the full area or all 16 maps, and make new storage objects of the owner
+ type bigger for all users.
+- A syscall update copies the value in place, under the storage lock. A reader
+ that takes no lock can see a torn value, as in array maps. Without the flag,
+ an update makes a new element, so readers see the whole old or new value.
+ ``BPF_F_LOCK`` is not available (see above).
+- A delete keeps the value bytes. A value pointer that a program keeps after an
+ update or a delete stays valid and points to the same slot. It sees, and can
+ change, the next value of the same map. Without the flag, it sees the old
+ value.
+- After map free, old objects keep the tag and value bytes. Each new inline map
+ gets a new 8 byte tag, so an accidental match with old bytes is unlikely.
+- The tag is predictable. A user that can create inline maps can make another
+ map see bytes that it never wrote, in objects made before that map. Tags
+ come back after 2^32 inline maps of the owner type: a new map can then see
+ old values of a freed map. Both cases are memory safe.
+- Only the size of an object decides if it holds the slot of a map. The size is
+ set when the object is made. An object without room keeps the value in a
+ normal element. Programs must not depend on which one applies.
+- The slot of a map comes back after the map is freed, which can be later than
+ ``close()``. For a short time, a new map create can then fail with
+ ``-ENOSPC``.
+- In map-in-map, inline and normal inner maps are not interchangeable: the map
+ flags must match. The kernel's copy of the inner map template holds no
+ slot, but the map given as ``inner_map_fd`` holds one until it is freed.
+ This is also true for the template map that libbpf makes.
+- The sk storage map iterator fails with ``-EOPNOTSUPP`` for an inline map.
+ sk diag shows inline values, also in a dump of all maps.
+- As for all local storage maps, a delete in NMI context fails with
+ ``-EOPNOTSUPP``. In tracing programs, the sk storage helpers refuse hard IRQ
+ and NMI context: ``bpf_sk_storage_get()`` returns NULL and
+ ``bpf_sk_storage_delete()`` returns ``-EPERM``.
+
+Inline maps also change things for users of normal maps:
+
+- A lookup of a normal map costs a little more, also when no inline map
+ exists: it tests if the map has an inline slot.
+- While an inline map exists, every new storage object of that owner type gets
+ the inline area, also for owners that use only normal maps. The memcg of the
+ map that makes an object pays for all of it. For sockets, the object counts
+ in ``sk_omem_alloc``, so ``optmem_max`` is reached sooner.
+- An object with an inline area is freed only when its owner goes away (the
+ socket is closed and freed, the task struct is freed, the cgroup is freed or
+ the inode is evicted), after an RCU tasks trace grace period. Until then it
+ stays charged, also after all its values are deleted and all inline maps are
+ freed.
+
+A new version of an inline map that is created while the old one exists gets a
+different slot. It can be after the old one, and then new objects become bigger.
+To keep one slot across restarts of an application, pin the map and reuse it
+with ``bpf_map__reuse_fd()``.
+
+A libbpf skeleton cannot run ``bpf_object__load()`` again after a failure. To
+fall back:
+
+1. Open the skeleton, add ``BPF_F_INLINE_VALUE`` to the map flags, and load
+ it. ``bpf_map__set_map_flags()`` replaces all flags, so keep the others::
+
+ bpf_map__set_map_flags(map, bpf_map__map_flags(map) | BPF_F_INLINE_VALUE);
+
+2. If the load fails with ``-ENOSPC``, ``-E2BIG``, ``-EOPNOTSUPP``, or
+ ``-EINVAL`` (a kernel without the flag), destroy the skeleton, open it
+ again, and load it without the flag. A kernel with the flag also gives
+ ``-EINVAL`` for the cases above that inline maps do not allow.
+
+A map with ``LIBBPF_PIN_BY_NAME`` does not mix with this fallback: libbpf
+reuses a pinned map only if its ``map_flags`` match the map definition.
+
Examples
========
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object
2026-11-23 15:15 [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object Mykyta Yatsenko
` (12 preceding siblings ...)
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
2026-11-30 4:58 ` Alexei Starovoitov
13 siblings, 1 reply; 16+ messages in thread
From: Alexei Starovoitov @ 2026-11-30 0:57 UTC (permalink / raw)
To: Mykyta Yatsenko, bpf, andrii, daniel, kernel-team, eddyz87
Cc: Mykyta Yatsenko
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
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object
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
0 siblings, 0 replies; 16+ messages in thread
From: Alexei Starovoitov @ 2026-11-30 4:58 UTC (permalink / raw)
To: Mykyta Yatsenko, bpf, andrii, daniel, kernel-team, eddyz87
Cc: Mykyta Yatsenko, edumazet, ncardwell, kuniyu, martin.lau, netdev
On Sun, Nov 29, 2026 at 04:57 PM Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:
> 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]).
Numbers are back...
1 gives nothing.
2 and 3 together are on par with this series.
It's better to do 3 early too. The diff below hides both misses:
tcp_sendmsg(), __tcp_transmit_skb() prefetch storage->cache[]
right before ->queue_xmit() prefetch cache[i] of hot maps
tc, cgroup, fentry, ... lookup hits
A map is hot when it was looked up for the socket that has just sent a
packet. It's learned per cpu and ages out after 256 packets, so it
doesn't depend on the prog type or the hook.
No uapi, no new limits, no change to the layout.
A copy of your workload: 131k loopback connections, 9 maps per socket,
2 progs, Zen 5 KVM guest. ns per request against bpf-next, +- 60:
this series selem in the tail diff below
-------------------- ----------- ----------------- ----------
tcx egress -540 -550 -840
tcx egress, quickack -570 -380 -860
cgroup egress -300 - -700
fentry/ip_output -340 - -530
cold, no progs +100 - +60
hot, 8 connections -20 - +15
The two lookups in tcx go from 190 and 85 ns to 10 and 1 ns.
Prefetch of all entries of cache[] is simpler, but it's ~200 ns worse
with 9 maps.
The static key is on while an sk storage map exists. Then the hooks cost
~10 ns per packet.
Only TCP transmit and sk storage are covered. IPv6 is not measured.
Could you pls run it on your setup?
diff --git a/include/net/bpf_sk_storage.h b/include/net/bpf_sk_storage.h
index 2926f1f00d65..b7e78c4739cc 100644
--- a/include/net/bpf_sk_storage.h
+++ b/include/net/bpf_sk_storage.h
@@ -13,6 +13,8 @@
#include <uapi/linux/sock_diag.h>
#include <uapi/linux/btf.h>
#include <linux/bpf_local_storage.h>
+#include <linux/jump_label_ratelimit.h>
+#include <linux/prefetch.h>
struct sock;
@@ -29,6 +31,27 @@ struct sk_buff;
struct nlattr;
#ifdef CONFIG_BPF_SYSCALL
+extern struct static_key_false_deferred bpf_sk_storage_needed;
+void __bpf_sk_storage_prefetch_values(const struct sock *sk);
+
+static inline void bpf_sk_storage_prefetch_cache(const struct sock *sk)
+{
+ if (static_branch_unlikely(&bpf_sk_storage_needed.key)) {
+ void *storage = rcu_access_pointer(sk->sk_bpf_storage);
+
+ if (storage) {
+ prefetch(storage);
+ prefetch(storage + SMP_CACHE_BYTES);
+ }
+ }
+}
+
+static inline void bpf_sk_storage_prefetch_values(const struct sock *sk)
+{
+ if (static_branch_unlikely(&bpf_sk_storage_needed.key))
+ __bpf_sk_storage_prefetch_values(sk);
+}
+
int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk);
struct bpf_sk_storage_diag *
bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs);
@@ -38,6 +61,8 @@ int bpf_sk_storage_diag_put(struct bpf_sk_storage_diag *diag,
int stg_array_type,
unsigned int *res_diag_size);
#else
+static inline void bpf_sk_storage_prefetch_cache(const struct sock *sk) {}
+static inline void bpf_sk_storage_prefetch_values(const struct sock *sk) {}
static inline int bpf_sk_storage_clone(const struct sock *sk,
struct sock *newsk)
{
diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
index 67288177492a..b1fedc161bde 100644
--- a/net/core/bpf_sk_storage.c
+++ b/net/core/bpf_sk_storage.c
@@ -17,6 +17,46 @@
DEFINE_BPF_STORAGE_CACHE(sk_cache);
+DEFINE_STATIC_KEY_DEFERRED_FALSE(bpf_sk_storage_needed, HZ);
+
+/* A hint. A race costs a cache miss. */
+struct sk_storage_xmit {
+ const struct sock *sk; /* The last socket that has sent a packet */
+ u16 seen; /* Entries of cache[] looked up for such sockets */
+ u16 mask; /* "seen" of the previous round */
+ u8 round;
+};
+
+static DEFINE_PER_CPU(struct sk_storage_xmit, sk_storage_xmit);
+
+void __bpf_sk_storage_prefetch_values(const struct sock *sk)
+{
+ struct sk_storage_xmit *xmit = raw_cpu_ptr(&sk_storage_xmit);
+ struct bpf_local_storage *storage;
+ unsigned long mask;
+ void *sdata;
+ int i;
+
+ xmit->sk = sk;
+ if (!++xmit->round) {
+ xmit->mask = xmit->seen;
+ xmit->seen = 0;
+ }
+ mask = xmit->mask;
+ if (!mask)
+ return;
+
+ guard(rcu)();
+ storage = rcu_dereference(sk->sk_bpf_storage);
+ if (!storage)
+ return;
+ for_each_set_bit(i, &mask, BPF_LOCAL_STORAGE_CACHE_SIZE) {
+ sdata = rcu_access_pointer(storage->cache[i]);
+ if (sdata)
+ prefetch(sdata);
+ }
+}
+
static struct bpf_local_storage_data *
bpf_sk_storage_lookup(struct sock *sk, struct bpf_map *map, bool cacheit_lockit)
{
@@ -29,6 +69,8 @@ bpf_sk_storage_lookup(struct sock *sk, struct bpf_map *map, bool cacheit_lockit)
return NULL;
smap = (struct bpf_local_storage_map *)map;
+ if (raw_cpu_read(sk_storage_xmit.sk) == sk)
+ raw_cpu_or(sk_storage_xmit.seen, BIT(smap->cache_idx));
return bpf_local_storage_lookup(sk_storage, smap, cacheit_lockit);
}
@@ -64,11 +106,16 @@ void bpf_sk_storage_free(struct sock *sk)
static void bpf_sk_storage_map_free(struct bpf_map *map)
{
bpf_local_storage_map_free(map, &sk_cache);
+ static_branch_slow_dec_deferred(&bpf_sk_storage_needed);
}
static struct bpf_map *bpf_sk_storage_map_alloc(union bpf_attr *attr)
{
- return bpf_local_storage_map_alloc(attr, &sk_cache);
+ struct bpf_map *map = bpf_local_storage_map_alloc(attr, &sk_cache);
+
+ if (!IS_ERR(map))
+ static_branch_deferred_inc(&bpf_sk_storage_needed);
+ return map;
}
static int notsupp_get_next_key(struct bpf_map *map, void *key,
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 80de376a9d2e..f8c03b6d4fb0 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -273,6 +273,7 @@
#include <net/inet_common.h>
#include <net/inet_ecn.h>
#include <net/tcp.h>
+#include <net/bpf_sk_storage.h>
#include <net/tcp_ecn.h>
#include <net/mptcp.h>
#include <net/proto_memory.h>
@@ -1463,6 +1464,7 @@ int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
{
int ret;
+ bpf_sk_storage_prefetch_cache(sk);
lock_sock(sk);
ret = tcp_sendmsg_locked(sk, msg, size);
release_sock(sk);
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index cf3ddc055d48..53dcbe563568 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -38,6 +38,7 @@
#define pr_fmt(fmt) "TCP: " fmt
#include <net/tcp.h>
+#include <net/bpf_sk_storage.h>
#include <net/tcp_ecn.h>
#include <net/mptcp.h>
#include <net/smc.h>
@@ -1605,6 +1606,7 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
int err;
BUG_ON(!skb || !tcp_skb_pcount(skb));
+ bpf_sk_storage_prefetch_cache(sk);
tp = tcp_sk(sk);
prior_wstamp = tp->tcp_wstamp_ns;
tp->tcp_wstamp_ns = max(tp->tcp_wstamp_ns, tp->tcp_clock_cache);
@@ -1769,6 +1771,7 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
tcp_add_tx_delay(skb, tp);
+ bpf_sk_storage_prefetch_values(sk);
err = INDIRECT_CALL_INET(icsk->icsk_af_ops->queue_xmit,
inet6_csk_xmit, ip_queue_xmit,
sk, skb, &inet->cork.fl);
^ permalink raw reply related [flat|nested] 16+ messages in thread