From: Emil Tsalapatis <emil@etsalapatis.com>
To: bpf@vger.kernel.org
Cc: ast@kernel.org, andrii@kernel.org, eddyz87@gmail.com,
memxor@gmail.com, daniel@iogearbox.net,
Emil Tsalapatis <emil@etsalapatis.com>
Subject: [PATCH bpf-next v1 2/4] bpf: Plumb local storage context
Date: Tue, 24 Nov 2026 06:03:55 +0000 [thread overview]
Message-ID: <20261124060357.11700-3-emil@etsalapatis.com> (raw)
In-Reply-To: <20261124060357.11700-1-emil@etsalapatis.com>
Local-storage allocation lacks context about the calling program.
Pass the program through the hidden helper argument and share the
allocation path for new elements and storage containers.
Non-functional change, will be used to decide whether context
allocation should be sleepable.
Signed-off-by: Emil Tsalapatis <emil@etsalapatis.com>
---
include/linux/bpf_local_storage.h | 3 +-
kernel/bpf/bpf_cgrp_storage.c | 9 +++---
kernel/bpf/bpf_inode_storage.c | 9 +++---
kernel/bpf/bpf_local_storage.c | 47 +++++++++++++++++++++++--------
kernel/bpf/bpf_task_storage.c | 9 +++---
net/core/bpf_sk_storage.c | 17 ++++++-----
6 files changed, 62 insertions(+), 32 deletions(-)
diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h
index 6c643ac510799..28f6ffbd75741 100644
--- a/include/linux/bpf_local_storage.h
+++ b/include/linux/bpf_local_storage.h
@@ -213,7 +213,8 @@ bpf_local_storage_alloc(void *owner,
struct bpf_local_storage_data *
bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
- void *value, u64 map_flags, bool swap_uptrs);
+ void *value, u64 map_flags, bool swap_uptrs,
+ const struct bpf_prog_aux *aux);
u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map);
diff --git a/kernel/bpf/bpf_cgrp_storage.c b/kernel/bpf/bpf_cgrp_storage.c
index c76e9b0fabba8..8a6b38b51ea2f 100644
--- a/kernel/bpf/bpf_cgrp_storage.c
+++ b/kernel/bpf/bpf_cgrp_storage.c
@@ -76,7 +76,7 @@ static long bpf_cgrp_storage_update_elem(struct bpf_map *map, void *key,
return PTR_ERR(cgroup);
sdata = bpf_local_storage_update(cgroup, (struct bpf_local_storage_map *)map,
- value, map_flags, false);
+ value, map_flags, false, NULL);
cgroup_put(cgroup);
return PTR_ERR_OR_ZERO(sdata);
}
@@ -122,8 +122,8 @@ static void cgroup_storage_map_free(struct bpf_map *map)
bpf_local_storage_map_free(map, &cgroup_cache);
}
-BPF_CALL_4(bpf_cgrp_storage_get, struct bpf_map *, map, struct cgroup *, cgroup,
- void *, value, u64, flags)
+BPF_CALL_5(bpf_cgrp_storage_get, struct bpf_map *, map, struct cgroup *, cgroup,
+ void *, value, u64, flags, struct bpf_prog_aux *, aux)
{
struct bpf_local_storage_data *sdata;
@@ -142,7 +142,7 @@ BPF_CALL_4(bpf_cgrp_storage_get, struct bpf_map *, map, struct cgroup *, cgroup,
if (!percpu_ref_is_dying(&cgroup->self.refcnt) &&
(flags & BPF_LOCAL_STORAGE_GET_F_CREATE))
sdata = bpf_local_storage_update(cgroup, (struct bpf_local_storage_map *)map,
- value, BPF_NOEXIST, false);
+ value, BPF_NOEXIST, false, aux);
out:
return IS_ERR_OR_NULL(sdata) ? (unsigned long)NULL : (unsigned long)sdata->data;
@@ -181,6 +181,7 @@ const struct bpf_func_proto bpf_cgrp_storage_get_proto = {
.arg2_btf_id = &bpf_cgroup_btf_id[0],
.arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
.arg4_type = ARG_ANYTHING,
+ .arg5_type = ARG_PTR_TO_PROG_AUX,
};
const struct bpf_func_proto bpf_cgrp_storage_delete_proto = {
diff --git a/kernel/bpf/bpf_inode_storage.c b/kernel/bpf/bpf_inode_storage.c
index f9e81060c1f45..c979e841ec130 100644
--- a/kernel/bpf/bpf_inode_storage.c
+++ b/kernel/bpf/bpf_inode_storage.c
@@ -98,7 +98,7 @@ static long bpf_fd_inode_storage_update_elem(struct bpf_map *map, void *key,
sdata = bpf_local_storage_update(file_inode(fd_file(f)),
(struct bpf_local_storage_map *)map,
- value, map_flags, false);
+ value, map_flags, false, NULL);
return PTR_ERR_OR_ZERO(sdata);
}
@@ -122,8 +122,8 @@ static long bpf_fd_inode_storage_delete_elem(struct bpf_map *map, void *key)
return inode_storage_delete(file_inode(fd_file(f)), map);
}
-BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,
- void *, value, u64, flags)
+BPF_CALL_5(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,
+ void *, value, u64, flags, struct bpf_prog_aux *, aux)
{
struct bpf_local_storage_data *sdata;
@@ -149,7 +149,7 @@ BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,
if (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) {
sdata = bpf_local_storage_update(
inode, (struct bpf_local_storage_map *)map, value,
- BPF_NOEXIST, false);
+ BPF_NOEXIST, false, aux);
return IS_ERR(sdata) ? (unsigned long)NULL :
(unsigned long)sdata->data;
}
@@ -221,6 +221,7 @@ const struct bpf_func_proto bpf_inode_storage_get_proto = {
.arg2_btf_id = &bpf_inode_storage_btf_ids[0],
.arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
.arg4_type = ARG_ANYTHING,
+ .arg5_type = ARG_PTR_TO_PROG_AUX,
};
const struct bpf_func_proto bpf_inode_storage_delete_proto = {
diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index bd79754a1e2fd..28eaee5c66ce8 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -66,16 +66,22 @@ static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem)
return !hlist_unhashed(&selem->map_node);
}
-struct bpf_local_storage_elem *
-bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
- void *value, bool swap_uptrs)
+static void *bpf_local_storage_alloc_mem(struct bpf_local_storage_map *smap,
+ size_t size, bool sleepable)
+{
+ return bpf_map_kmalloc_maylock(&smap->map, size, __GFP_ZERO);
+}
+
+static struct bpf_local_storage_elem *
+__bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
+ void *value, bool swap_uptrs, bool sleepable)
{
struct bpf_local_storage_elem *selem;
if (mem_charge(smap, owner, smap->elem_size))
return NULL;
- selem = bpf_map_kmalloc_maylock(&smap->map, smap->elem_size, __GFP_ZERO);
+ selem = bpf_local_storage_alloc_mem(smap, smap->elem_size, sleepable);
if (selem) {
RCU_INIT_POINTER(SDATA(selem)->smap, smap);
@@ -95,6 +101,13 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
return NULL;
}
+struct bpf_local_storage_elem *
+bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
+ void *value, bool swap_uptrs)
+{
+ return __bpf_selem_alloc(smap, owner, value, swap_uptrs, false);
+}
+
static void bpf_local_storage_free_trace_rcu(struct rcu_head *rcu)
{
struct bpf_local_storage *local_storage;
@@ -497,9 +510,10 @@ static int check_flags(const struct bpf_local_storage_data *old_sdata,
return 0;
}
-int bpf_local_storage_alloc(void *owner,
- struct bpf_local_storage_map *smap,
- struct bpf_local_storage_elem *first_selem)
+static int __bpf_local_storage_alloc(void *owner,
+ struct bpf_local_storage_map *smap,
+ struct bpf_local_storage_elem *first_selem,
+ bool sleepable)
{
struct bpf_local_storage *prev_storage, *storage;
struct bpf_local_storage **owner_storage_ptr;
@@ -511,7 +525,7 @@ int bpf_local_storage_alloc(void *owner,
if (err)
return err;
- storage = bpf_map_kmalloc_maylock(&smap->map, sizeof(*storage), __GFP_ZERO);
+ storage = bpf_local_storage_alloc_mem(smap, sizeof(*storage), sleepable);
if (!storage) {
err = -ENOMEM;
goto uncharge;
@@ -565,6 +579,13 @@ int bpf_local_storage_alloc(void *owner,
return err;
}
+int bpf_local_storage_alloc(void *owner,
+ struct bpf_local_storage_map *smap,
+ struct bpf_local_storage_elem *first_selem)
+{
+ return __bpf_local_storage_alloc(owner, smap, first_selem, false);
+}
+
/* sk cannot be going away because it is linking new elem
* to sk->sk_bpf_storage. (i.e. sk->sk_refcnt cannot be 0).
* Otherwise, it will become a leak (and other memory issues
@@ -572,10 +593,12 @@ int bpf_local_storage_alloc(void *owner,
*/
struct bpf_local_storage_data *
bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
- void *value, u64 map_flags, bool swap_uptrs)
+ void *value, u64 map_flags, bool swap_uptrs,
+ const struct bpf_prog_aux *aux)
{
struct bpf_local_storage_data *old_sdata = NULL;
struct bpf_local_storage_elem *alloc_selem, *selem = NULL;
+ bool sleepable = false;
struct bpf_local_storage *local_storage;
struct bpf_local_storage_map_bucket *b;
HLIST_HEAD(old_selem_free_list);
@@ -597,11 +620,11 @@ 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, value, swap_uptrs, sleepable);
if (!selem)
return ERR_PTR(-ENOMEM);
- err = bpf_local_storage_alloc(owner, smap, selem);
+ err = __bpf_local_storage_alloc(owner, smap, selem, sleepable);
if (err) {
bpf_selem_free(selem, true);
mem_uncharge(smap, owner, smap->elem_size);
@@ -631,7 +654,7 @@ 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, value, swap_uptrs, sleepable);
if (!alloc_selem)
return ERR_PTR(-ENOMEM);
diff --git a/kernel/bpf/bpf_task_storage.c b/kernel/bpf/bpf_task_storage.c
index 4b342be29eac7..b081e8799eef1 100644
--- a/kernel/bpf/bpf_task_storage.c
+++ b/kernel/bpf/bpf_task_storage.c
@@ -118,7 +118,7 @@ static long bpf_pid_task_storage_update_elem(struct bpf_map *map, void *key,
sdata = bpf_local_storage_update(
task, (struct bpf_local_storage_map *)map, value, map_flags,
- true);
+ true, NULL);
err = PTR_ERR_OR_ZERO(sdata);
out:
@@ -165,8 +165,8 @@ static long bpf_pid_task_storage_delete_elem(struct bpf_map *map, void *key)
return err;
}
-BPF_CALL_4(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *,
- task, void *, value, u64, flags)
+BPF_CALL_5(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *, task,
+ void *, value, u64, flags, struct bpf_prog_aux *, aux)
{
struct bpf_local_storage_data *sdata;
@@ -183,7 +183,7 @@ BPF_CALL_4(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *,
(flags & BPF_LOCAL_STORAGE_GET_F_CREATE)) {
sdata = bpf_local_storage_update(
task, (struct bpf_local_storage_map *)map, value,
- BPF_NOEXIST, false);
+ BPF_NOEXIST, false, aux);
return IS_ERR(sdata) ? (unsigned long)NULL : (unsigned long)sdata->data;
}
@@ -244,6 +244,7 @@ const struct bpf_func_proto bpf_task_storage_get_proto = {
.arg2_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
.arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
.arg4_type = ARG_ANYTHING,
+ .arg5_type = ARG_PTR_TO_PROG_AUX,
};
const struct bpf_func_proto bpf_task_storage_delete_proto = {
diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
index 1c59300af05c7..5a013b81d161a 100644
--- a/net/core/bpf_sk_storage.c
+++ b/net/core/bpf_sk_storage.c
@@ -106,7 +106,7 @@ static long bpf_fd_sk_storage_update_elem(struct bpf_map *map, void *key,
if (sock) {
sdata = bpf_local_storage_update(
sock->sk, (struct bpf_local_storage_map *)map, value,
- map_flags, false);
+ map_flags, false, NULL);
sockfd_put(sock);
return PTR_ERR_OR_ZERO(sdata);
}
@@ -232,8 +232,8 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
return ret;
}
-BPF_CALL_4(bpf_sk_storage_get, struct bpf_map *, map, struct sock *, sk,
- void *, value, u64, flags)
+BPF_CALL_5(bpf_sk_storage_get, struct bpf_map *, map, struct sock *, sk,
+ void *, value, u64, flags, struct bpf_prog_aux *, aux)
{
struct bpf_local_storage_data *sdata;
@@ -254,7 +254,7 @@ BPF_CALL_4(bpf_sk_storage_get, struct bpf_map *, map, struct sock *, sk,
refcount_inc_not_zero(&sk->sk_refcnt)) {
sdata = bpf_local_storage_update(
sk, (struct bpf_local_storage_map *)map, value,
- BPF_NOEXIST, false);
+ BPF_NOEXIST, false, aux);
/* sk must be a fullsock (guaranteed by verifier),
* so sock_gen_put() is unnecessary.
*/
@@ -341,6 +341,7 @@ const struct bpf_func_proto bpf_sk_storage_get_proto = {
.arg2_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
.arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
.arg4_type = ARG_ANYTHING,
+ .arg5_type = ARG_PTR_TO_PROG_AUX,
};
const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto = {
@@ -351,6 +352,7 @@ const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto = {
.arg2_type = ARG_PTR_TO_CTX, /* context is 'struct sock' */
.arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
.arg4_type = ARG_ANYTHING,
+ .arg5_type = ARG_PTR_TO_PROG_AUX,
};
const struct bpf_func_proto bpf_sk_storage_delete_proto = {
@@ -387,14 +389,14 @@ static bool bpf_sk_storage_tracing_allowed(const struct bpf_prog *prog)
return false;
}
-BPF_CALL_4(bpf_sk_storage_get_tracing, struct bpf_map *, map, struct sock *, sk,
- void *, value, u64, flags)
+BPF_CALL_5(bpf_sk_storage_get_tracing, struct bpf_map *, map, struct sock *, sk,
+ void *, value, u64, flags, struct bpf_prog_aux *, aux)
{
WARN_ON_ONCE(!bpf_rcu_lock_held());
if (in_hardirq() || in_nmi())
return (unsigned long)NULL;
- return (unsigned long)____bpf_sk_storage_get(map, sk, value, flags);
+ return ____bpf_sk_storage_get(map, sk, value, flags, aux);
}
BPF_CALL_2(bpf_sk_storage_delete_tracing, struct bpf_map *, map,
@@ -416,6 +418,7 @@ const struct bpf_func_proto bpf_sk_storage_get_tracing_proto = {
.arg2_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON],
.arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
.arg4_type = ARG_ANYTHING,
+ .arg5_type = ARG_PTR_TO_PROG_AUX,
.allowed = bpf_sk_storage_tracing_allowed,
};
--
2.52.1
next prev parent reply other threads:[~2026-11-24 6:04 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 6:03 [PATCH bpf-next v1 0/4] bpf: Use kmalloc for local storage if possible Emil Tsalapatis
2026-11-24 6:03 ` [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time Emil Tsalapatis
2026-11-24 6:21 ` sashiko-bot
2026-11-24 6:03 ` Emil Tsalapatis [this message]
2026-11-24 6:17 ` [PATCH bpf-next v1 2/4] bpf: Plumb local storage context sashiko-bot
2026-11-24 6:03 ` [PATCH bpf-next v1 3/4] bpf: Add sleepable local storage Emil Tsalapatis
2026-11-24 6:03 ` [PATCH bpf-next v1 4/4] selftests/bpf: Test kmalloc variant selection for task storage Emil Tsalapatis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124060357.11700-3-emil@etsalapatis.com \
--to=emil@etsalapatis.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=memxor@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox