* [PATCH bpf-next 2/2] selftests/bpf: test task storage memory accounting
2026-11-26 13:13 [PATCH bpf-next 1/2] bpf: account for local storage map elements memory Mahe Tardy
@ 2026-11-26 13:13 ` Mahe Tardy
2026-11-26 21:11 ` [PATCH bpf-next 1/2] bpf: account for local storage map elements memory Alexei Starovoitov
1 sibling, 0 replies; 3+ messages in thread
From: Mahe Tardy @ 2026-11-26 13:13 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, Shuah Khan, Christian Brauner, Amery Hung,
Mahe Tardy, linux-kselftest, linux-kernel
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] 3+ messages in thread* Re: [PATCH bpf-next 1/2] bpf: account for local storage map elements memory
2026-11-26 13:13 [PATCH bpf-next 1/2] bpf: account for local storage map elements memory Mahe Tardy
2026-11-26 13:13 ` [PATCH bpf-next 2/2] selftests/bpf: test task storage memory accounting Mahe Tardy
@ 2026-11-26 21:11 ` Alexei Starovoitov
1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2026-11-26 21:11 UTC (permalink / raw)
To: Mahe Tardy, bpf
Cc: Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman,
Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai,
Shuah Khan, Christian Brauner, Amery Hung, linux-kselftest,
linux-kernel
On Thu, Nov 26, 2026 at 05:13 AM Mahe Tardy <mahe.tardy@gmail.com> wrote:
> Walk the bucket's list to count the number of elements in the map, note
> that concurrent insertions and deletions can make the count imprecise.
No other map_mem_usage() walks the elements. All of them read a counter.
'bpftool map show' reads memlock of every map.
An sk_storage map has one selem per socket, so every read of its fdinfo
is one cache miss per socket.
Lets count in map->elem_count, like htab.
It costs one this_cpu_inc/dec under b->lock. Update and delete take
that lock already.
bpf_map_sum_elem_count() and cur_entries in maps.debug start to work
for local storage maps too.
diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index b12fcec..6f7c2e5 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -259,14 +259,17 @@ static int bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
return err;
hlist_del_init_rcu(&selem->map_node);
+ bpf_map_dec_elem_count(&smap->map);
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_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);
}
int bpf_selem_link_map(struct bpf_local_storage_map *smap,
@@ -284,15 +287,18 @@ int bpf_selem_link_map(struct bpf_local_storage_map *smap,
return err;
hlist_add_head_rcu(&selem->map_node, &b->list);
+ bpf_map_inc_elem_count(&smap->map);
raw_res_spin_unlock_irqrestore(&b->lock, flags);
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);
}
/*
@@ -377,7 +383,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(smap, selem);
if (sleepable_users)
keep_fields = true;
else
@@ -556,7 +562,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 +679,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 +800,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 +837,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 +913,7 @@ restart:
/* 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);
}
pw-bot: cr
^ permalink raw reply related [flat|nested] 3+ messages in thread