From: Mahe Tardy <mahe.tardy@gmail.com>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Shuah Khan <shuah@kernel.org>,
Christian Brauner <brauner@kernel.org>,
Amery Hung <ameryhung@gmail.com>,
Mahe Tardy <mahe.tardy@gmail.com>,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH bpf-next 1/2] bpf: account for local storage map elements memory
Date: Thu, 26 Nov 2026 13:13:17 +0000 [thread overview]
Message-ID: <20261126131318.470837-1-mahe.tardy@gmail.com> (raw)
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.
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.
Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
---
kernel/bpf/bpf_local_storage.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index b12fcec47ffa..6fb95503441e 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -792,11 +792,19 @@ u32 bpf_local_storage_destroy(struct bpf_local_storage *local_storage)
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);
+ struct bpf_local_storage_elem *selem;
+ u64 nbuckets = 1ULL << smap->bucket_log;
+ u64 nr_elem = 0;
+ u64 i;
+
+ for (i = 0; i < nbuckets; i++) {
+ rcu_read_lock();
+ hlist_for_each_entry_rcu(selem, &smap->buckets[i].list, map_node)
+ nr_elem++;
+ rcu_read_unlock();
+ }
- /* The dynamically callocated selems are not counted currently. */
- usage += sizeof(*smap->buckets) * (1ULL << smap->bucket_log);
- return usage;
+ return sizeof(*smap) + nbuckets * sizeof(*smap->buckets) + nr_elem * smap->elem_size;
}
struct bpf_map *
--
2.43.0
next reply other threads:[~2026-11-26 13:13 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 13:13 Mahe Tardy [this message]
2026-11-26 13:13 ` [PATCH bpf-next 2/2] selftests/bpf: test task storage memory accounting Mahe Tardy
2026-11-26 21:11 ` [PATCH bpf-next 1/2] bpf: account for local storage map elements memory Alexei Starovoitov
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=20261126131318.470837-1-mahe.tardy@gmail.com \
--to=mahe.tardy@gmail.com \
--cc=ameryhung@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brauner@kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/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
all inboxes | Powered by JetHome®