* [PATCH bpf-next v2 0/3] bpf: account for local storage map elements memory
@ 2026-11-30 20:14 Mahe Tardy
2026-11-30 20:14 ` [PATCH bpf-next v2 1/3] bpf: reuse local storage map link and unlink helpers Mahe Tardy
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Mahe Tardy @ 2026-11-30 20:14 UTC (permalink / raw)
To: bpf
Cc: Andrii Nakryiko, Eduard Zingerman, Ihor Solodrai,
Alexei Starovoitov, Daniel Borkmann, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, linux-kernel, John Fastabend, Mahe Tardy
We started using task local storage within Tetragon and realized memlock
omitted the memory used by map entries[^1] which is significant in our
use case, this aims at making the memory usage more accurate.
v2 changes:
- use map->elem_count instead of walking the elements (alexei)
Link to v1: https://lore.kernel.org/bpf/20261126131318.470837-1-mahe.tardy@gmail.com/
[^1]: https://github.com/cilium/tetragon/issues/6139
Mahe Tardy (3):
bpf: reuse local storage map link and unlink helpers
bpf: account for local storage map elements memory
selftests/bpf: test task storage memory accounting
kernel/bpf/bpf_local_storage.c | 41 ++++++++-----
.../bpf/prog_tests/task_local_storage.c | 61 +++++++++++++++++++
2 files changed, 87 insertions(+), 15 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH bpf-next v2 1/3] bpf: reuse local storage map link and unlink helpers
2026-11-30 20:14 [PATCH bpf-next v2 0/3] bpf: account for local storage map elements memory Mahe Tardy
@ 2026-11-30 20:14 ` Mahe Tardy
2026-11-30 20:14 ` [PATCH bpf-next v2 2/3] bpf: account for local storage map elements memory Mahe Tardy
2026-11-30 20:14 ` [PATCH bpf-next v2 3/3] selftests/bpf: test task storage memory accounting Mahe Tardy
2 siblings, 0 replies; 4+ messages in thread
From: Mahe Tardy @ 2026-11-30 20:14 UTC (permalink / raw)
To: bpf
Cc: Andrii Nakryiko, Eduard Zingerman, Ihor Solodrai,
Alexei Starovoitov, Daniel Borkmann, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, linux-kernel, John Fastabend, Mahe Tardy
Route map-side insertions and removals through the existing _nolock
helpers. The following commit will use the bpf map elem counters helpers
from the _nolock functions to estimate the memory usage more accurately.
Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
---
kernel/bpf/bpf_local_storage.c | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index b12fcec47ffa..e470447d6218 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -240,6 +240,11 @@ void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
hlist_add_head_rcu(&selem->snode, &local_storage->list);
}
+static void bpf_selem_unlink_map_nolock(struct bpf_local_storage_elem *selem)
+{
+ hlist_del_init_rcu(&selem->map_node);
+}
+
/* Must be called with the owning local_storage->lock held. */
static int bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
{
@@ -258,15 +263,16 @@ static int bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
if (err)
return err;
- hlist_del_init_rcu(&selem->map_node);
+ bpf_selem_unlink_map_nolock(selem);
raw_res_spin_unlock(&b->lock);
return 0;
}
-static void bpf_selem_unlink_map_nolock(struct bpf_local_storage_elem *selem)
+static void bpf_selem_link_map_nolock(struct bpf_local_storage_map_bucket *b,
+ struct bpf_local_storage_elem *selem)
{
- hlist_del_init_rcu(&selem->map_node);
+ hlist_add_head_rcu(&selem->map_node, &b->list);
}
int bpf_selem_link_map(struct bpf_local_storage_map *smap,
@@ -283,18 +289,12 @@ int bpf_selem_link_map(struct bpf_local_storage_map *smap,
if (err)
return err;
- hlist_add_head_rcu(&selem->map_node, &b->list);
+ bpf_selem_link_map_nolock(b, selem);
raw_res_spin_unlock_irqrestore(&b->lock, flags);
return 0;
}
-static void bpf_selem_link_map_nolock(struct bpf_local_storage_map_bucket *b,
- struct bpf_local_storage_elem *selem)
-{
- hlist_add_head_rcu(&selem->map_node, &b->list);
-}
-
/*
* Unlink an selem from map and local storage with lock held.
* This is the common path used by local storages to delete an selem.
@@ -377,7 +377,7 @@ static void bpf_selem_unlink_nofail(struct bpf_local_storage_elem *selem,
* is sleepable: see below.
*/
if (likely(selem_linked_to_map(selem))) {
- hlist_del_init_rcu(&selem->map_node);
+ bpf_selem_unlink_map_nolock(selem);
if (sleepable_users)
keep_fields = true;
else
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH bpf-next v2 2/3] bpf: account for local storage map elements memory
2026-11-30 20:14 [PATCH bpf-next v2 0/3] bpf: account for local storage map elements memory Mahe Tardy
2026-11-30 20:14 ` [PATCH bpf-next v2 1/3] bpf: reuse local storage map link and unlink helpers Mahe Tardy
@ 2026-11-30 20:14 ` Mahe Tardy
2026-11-30 20:14 ` [PATCH bpf-next v2 3/3] selftests/bpf: test task storage memory accounting Mahe Tardy
2 siblings, 0 replies; 4+ messages in thread
From: Mahe Tardy @ 2026-11-30 20:14 UTC (permalink / raw)
To: bpf
Cc: Andrii Nakryiko, Eduard Zingerman, Ihor Solodrai,
Alexei Starovoitov, Daniel Borkmann, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, linux-kernel, John Fastabend, Mahe Tardy
Commit 7490b7f1c02e ("bpf, net: bpf_local_storage memory usage")
introduced bpf_local_storage_map_mem_usage() to compute the memory usage
of local storage related maps but left the accounting of the allocated
element aside. For users of these maps, the elements can quickly make up
the majority of the memory usage and make the reported memory usage
quite wrong.
Maintain the existing per-CPU map element counters[^1][^2] when linking
and unlinking local storage elements. Sum those counters to report
memory. Note that the existing maps.debug iterator also uses the
counters to report cur_entries.
[^1]: https://lore.kernel.org/all/20230706133932.45883-1-aspsk@isovalent.com/
[^2]: https://lore.kernel.org/all/20230719092952.41202-1-aspsk@isovalent.com/
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
---
kernel/bpf/bpf_local_storage.c | 29 ++++++++++++++++++++---------
1 file changed, 20 insertions(+), 9 deletions(-)
diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index e470447d6218..ed849546afd1 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -240,9 +240,11 @@ void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
hlist_add_head_rcu(&selem->snode, &local_storage->list);
}
-static void bpf_selem_unlink_map_nolock(struct bpf_local_storage_elem *selem)
+static void bpf_selem_unlink_map_nolock(struct bpf_local_storage_map *smap,
+ struct bpf_local_storage_elem *selem)
{
hlist_del_init_rcu(&selem->map_node);
+ bpf_map_dec_elem_count(&smap->map);
}
/* Must be called with the owning local_storage->lock held. */
@@ -263,18 +265,21 @@ static int bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
if (err)
return err;
- bpf_selem_unlink_map_nolock(selem);
+ bpf_selem_unlink_map_nolock(smap, selem);
raw_res_spin_unlock(&b->lock);
return 0;
}
-static void bpf_selem_link_map_nolock(struct bpf_local_storage_map_bucket *b,
+static void bpf_selem_link_map_nolock(struct bpf_local_storage_map *smap,
+ struct bpf_local_storage_map_bucket *b,
struct bpf_local_storage_elem *selem)
{
hlist_add_head_rcu(&selem->map_node, &b->list);
+ bpf_map_inc_elem_count(&smap->map);
}
+
int bpf_selem_link_map(struct bpf_local_storage_map *smap,
struct bpf_local_storage *local_storage,
struct bpf_local_storage_elem *selem)
@@ -289,7 +294,7 @@ int bpf_selem_link_map(struct bpf_local_storage_map *smap,
if (err)
return err;
- bpf_selem_link_map_nolock(b, selem);
+ bpf_selem_link_map_nolock(smap, b, selem);
raw_res_spin_unlock_irqrestore(&b->lock, flags);
return 0;
@@ -377,7 +382,7 @@ static void bpf_selem_unlink_nofail(struct bpf_local_storage_elem *selem,
* is sleepable: see below.
*/
if (likely(selem_linked_to_map(selem))) {
- bpf_selem_unlink_map_nolock(selem);
+ bpf_selem_unlink_map_nolock(smap, selem);
if (sleepable_users)
keep_fields = true;
else
@@ -556,7 +561,7 @@ int bpf_local_storage_alloc(void *owner,
* such a storage through the map. b->lock holds off those who find
* first_selem through the owner and want to unlink it.
*/
- bpf_selem_link_map_nolock(b, first_selem);
+ bpf_selem_link_map_nolock(smap, b, first_selem);
raw_res_spin_unlock_irqrestore(&b->lock, flags);
return 0;
@@ -673,14 +678,14 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
alloc_selem = NULL;
/* First, link the new selem to the map */
- bpf_selem_link_map_nolock(b, selem);
+ bpf_selem_link_map_nolock(smap, b, selem);
/* Second, link (and publish) the new selem to local_storage */
bpf_selem_link_storage_nolock(local_storage, selem);
/* Third, remove old selem, SELEM(old_sdata) */
if (old_sdata) {
- bpf_selem_unlink_map_nolock(SELEM(old_sdata));
+ bpf_selem_unlink_map_nolock(smap, SELEM(old_sdata));
bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata),
&old_selem_free_list);
}
@@ -794,8 +799,9 @@ u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map)
struct bpf_local_storage_map *smap = (struct bpf_local_storage_map *)map;
u64 usage = sizeof(*smap);
- /* The dynamically callocated selems are not counted currently. */
+ /* The maps of an owner share its bpf_local_storage. It's not counted. */
usage += sizeof(*smap->buckets) * (1ULL << smap->bucket_log);
+ usage += (u64)smap->elem_size * max(bpf_map_sum_elem_count(map), 0);
return usage;
}
@@ -830,6 +836,10 @@ bpf_local_storage_map_alloc(union bpf_attr *attr,
raw_res_spin_lock_init(&smap->buckets[i].lock);
}
+ err = bpf_map_init_elem_count(&smap->map);
+ if (err)
+ goto free_smap;
+
smap->elem_size = offsetof(struct bpf_local_storage_elem,
sdata.data[attr->value_size]);
@@ -902,6 +912,7 @@ void bpf_local_storage_map_free(struct bpf_map *map,
/* smap remains in use regardless of kmalloc_nolock, so wait unconditionally. */
rcu_barrier_tasks_trace();
rcu_barrier();
+ bpf_map_free_elem_count(map);
kvfree(smap->buckets);
bpf_map_area_free(smap);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH bpf-next v2 3/3] selftests/bpf: test task storage memory accounting
2026-11-30 20:14 [PATCH bpf-next v2 0/3] bpf: account for local storage map elements memory Mahe Tardy
2026-11-30 20:14 ` [PATCH bpf-next v2 1/3] bpf: reuse local storage map link and unlink helpers Mahe Tardy
2026-11-30 20:14 ` [PATCH bpf-next v2 2/3] bpf: account for local storage map elements memory Mahe Tardy
@ 2026-11-30 20:14 ` Mahe Tardy
2 siblings, 0 replies; 4+ messages in thread
From: Mahe Tardy @ 2026-11-30 20:14 UTC (permalink / raw)
To: bpf
Cc: Andrii Nakryiko, Eduard Zingerman, Ihor Solodrai,
Alexei Starovoitov, Daniel Borkmann, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, linux-kernel, John Fastabend, Mahe Tardy
This tests that the memory increase as we add elements to a
task_storage. Copy the map_memlock() helper from arena_mem_usage.c.
Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
---
.../bpf/prog_tests/task_local_storage.c | 61 +++++++++++++++++++
1 file changed, 61 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/task_local_storage.c b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c
index 5b2b56cc3a4f..1415782ad72f 100644
--- a/tools/testing/selftests/bpf/prog_tests/task_local_storage.c
+++ b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c
@@ -495,6 +495,65 @@ static void test_uptr_map_failure(const char *map_name, int expected_errno)
uptr_map_failure__destroy(skel);
}
+static long map_memlock(int map_fd)
+{
+ char path[64], line[128];
+ long memlock = -1;
+ FILE *f;
+
+ snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", map_fd);
+ f = fopen(path, "r");
+ if (!ASSERT_OK_PTR(f, "open_fdinfo"))
+ return -1;
+ while (fgets(line, sizeof(line), f)) {
+ if (sscanf(line, "memlock:\t%ld", &memlock) == 1)
+ break;
+ }
+ fclose(f);
+ ASSERT_NEQ(memlock, -1, "parse_memlock");
+ return memlock;
+}
+
+static void test_memory_usage(void)
+{
+ struct task_local_storage_exit_creds *skel;
+ struct bpf_program *prog;
+ int map_fd, task_fd = -1;
+ long baseline;
+ u64 value = 12;
+
+ skel = task_local_storage_exit_creds__open();
+ if (!ASSERT_OK_PTR(skel, "skel_open"))
+ return;
+
+ /* Just load the map */
+ bpf_object__for_each_program(prog, skel->obj) {
+ if (!ASSERT_OK(bpf_program__set_autoload(prog, false), "disable_autoload"))
+ goto out;
+ }
+
+ if (!ASSERT_OK(task_local_storage_exit_creds__load(skel), "skel_load"))
+ goto out;
+
+ map_fd = bpf_map__fd(skel->maps.task_storage);
+ baseline = map_memlock(map_fd);
+ if (!ASSERT_GE(baseline, 0, "baseline_greater_zero"))
+ goto out;
+
+ task_fd = sys_pidfd_open(getpid(), 0);
+ if (!ASSERT_OK_FD(task_fd, "task_fd"))
+ goto out;
+
+ if (!ASSERT_OK(bpf_map_update_elem(map_fd, &task_fd, &value, BPF_NOEXIST), "update_map"))
+ goto out;
+
+ ASSERT_GT(map_memlock(map_fd), baseline, "memory_increase");
+out:
+ if (task_fd >= 0)
+ close(task_fd);
+ task_local_storage_exit_creds__destroy(skel);
+}
+
void test_task_local_storage(void)
{
if (test__start_subtest("sys_enter_exit"))
@@ -521,5 +580,7 @@ void test_task_local_storage(void)
test_uptr_map_failure("empty_uptr_map", EINVAL);
if (test__start_subtest("uptr_map_failure_kstruct"))
test_uptr_map_failure("kstruct_uptr_map", EINVAL);
+ if (test__start_subtest("memory_usage"))
+ test_memory_usage();
RUN_TESTS(uptr_failure);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-30 20:14 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 20:14 [PATCH bpf-next v2 0/3] bpf: account for local storage map elements memory Mahe Tardy
2026-11-30 20:14 ` [PATCH bpf-next v2 1/3] bpf: reuse local storage map link and unlink helpers Mahe Tardy
2026-11-30 20:14 ` [PATCH bpf-next v2 2/3] bpf: account for local storage map elements memory Mahe Tardy
2026-11-30 20:14 ` [PATCH bpf-next v2 3/3] selftests/bpf: test task storage memory accounting Mahe Tardy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox