From: Quentin Monnet <qmo@kernel.org>
To: Tianyi Chen <hi@tychen.cc>, bpf@vger.kernel.org
Cc: andrii@kernel.org, eddyz87@gmail.com, ihor.solodrai@linux.dev,
linux-kselftest@vger.kernel.org, shuah@kernel.org,
daniel@iogearbox.net, ast@kernel.org,
alexei.starovoitov@gmail.com
Subject: Re: [PATCH bpf-next v9 1/2] bpftool: Use batch lookups for map dumps
Date: Tue, 10 Nov 2026 16:46:48 +0000 [thread overview]
Message-ID: <3901a136-d2e1-43eb-8284-31dae956391e@kernel.org> (raw)
In-Reply-To: <7a728136ad18840c94836ec1ce988f8bc2fcfb35.1793821981.git.hi@tychen.cc>
On 04/11/2026 19:54, Tianyi Chen wrote:
> Dumping a map takes two BPF syscalls per element: one to get the next
> key and one to look it up. Use BPF_MAP_LOOKUP_BATCH instead, with keys
> and values bounded to 1 MiB and per-CPU value strides accounted for.
> The output is unchanged.
>
> For a hash map with 100,000 4-byte keys and values, a plain dump takes 5
> BPF syscalls instead of 200,005. With output to /dev/null, the median of
> 15 runs in an x86-64 KVM guest on Linux 7.3 dropped from 86 ms to 52 ms
> (JSON: 78 ms to 42 ms). Printing to a terminal dominates either way.
>
> If a batch lookup fails, discard its output and continue with individual
> lookups after the last printed key. bpf_map_get_next_key() walks the map
> in the same order. This covers maps without batch support, and hash
> maps whose bucket lock cannot be taken, which individual lookups do not
> need.
>
> Link: https://github.com/libbpf/bpftool/issues/63
> Suggested-by: Alexei Starovoitov <ast@kernel.org>
> Assisted-by: LLM
> Signed-off-by: Tianyi Chen <hi@tychen.cc>
Looks good to me, thanks!
Acked-by: Quentin Monnet <qmo@kernel.org>
> ---
> tools/bpf/bpftool/map.c | 97 ++++++++++++++++++++++++++++++++++-------
> 1 file changed, 82 insertions(+), 15 deletions(-)
>
> diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
> index 4b65edf74859b..c0bd9bf846492 100644
> --- a/tools/bpf/bpftool/map.c
> +++ b/tools/bpf/bpftool/map.c
> @@ -59,13 +59,16 @@ static int map_type_from_str(const char *type)
> return -1;
> }
>
> -static void *alloc_value(struct bpf_map_info *info)
> +static size_t map_value_size(struct bpf_map_info *info)
> {
> if (map_is_per_cpu(info->type))
> - return malloc(round_up(info->value_size, 8) *
> - get_possible_cpus());
> - else
> - return malloc(info->value_size);
> + return (size_t)round_up(info->value_size, 8) * get_possible_cpus();
> + return info->value_size;
> +}
> +
> +static void *alloc_value(struct bpf_map_info *info)
> +{
> + return malloc(map_value_size(info));
> }
>
> static int do_dump_btf(const struct btf_dumper *d,
> @@ -741,15 +744,10 @@ static int do_show(int argc, char **argv)
> return errno == ENOENT ? 0 : -1;
> }
>
> -static int dump_map_elem(int fd, void *key, void *value,
> - struct bpf_map_info *map_info, struct btf *btf,
> - json_writer_t *btf_wtr, const int *cpu_ids, int cpu_cnt)
> +static void print_map_elem(void *key, void *value,
> + struct bpf_map_info *map_info, struct btf *btf,
> + json_writer_t *btf_wtr, const int *cpu_ids, int cpu_cnt)
> {
> - if (bpf_map_lookup_elem(fd, key, value)) {
> - print_entry_error(map_info, key, errno);
> - return -1;
> - }
> -
> if (json_output) {
> print_entry_json(map_info, key, value, btf, cpu_ids, cpu_cnt);
> } else if (btf) {
> @@ -763,10 +761,74 @@ static int dump_map_elem(int fd, void *key, void *value,
> } else {
> print_entry_plain(map_info, key, value, cpu_ids, cpu_cnt);
> }
> +}
>
> +static int dump_map_elem(int fd, void *key, void *value,
> + struct bpf_map_info *map_info, struct btf *btf,
> + json_writer_t *btf_wtr, const int *cpu_ids, int cpu_cnt)
> +{
> + if (bpf_map_lookup_elem(fd, key, value)) {
> + print_entry_error(map_info, key, errno);
> + return -1;
> + }
> +
> + print_map_elem(key, value, map_info, btf, btf_wtr, cpu_ids, cpu_cnt);
> return 0;
> }
>
> +#define MAP_DUMP_BATCH_BYTES (1U << 20)
> +
> +/*
> + * Print the elements returned by batched lookups, leaving the last one in
> + * @key. Return true at the end of the map. After any error, the caller
> + * continues with bpf_map_get_next_key(), which walks the map in the same order.
> + */
> +static bool dump_map_batch(int fd, void *key, void *value,
> + struct bpf_map_info *info, struct btf *btf,
> + json_writer_t *wtr, const int *cpu_ids, int cpu_cnt,
> + unsigned int *num_elems)
> +{
> + size_t value_size = map_value_size(info);
> + void *keys, *values, *batch, *in_batch = NULL;
> + __u32 capacity, count, i;
> + bool done = false;
> + int err;
> +
> + capacity = min(MAP_DUMP_BATCH_BYTES / (info->key_size + value_size),
> + (size_t)info->max_entries);
Nit: If you respin, dump_map_batch() might be worth a comment to tell it
assumes key_size + value_size to be non-zero. Right now we're good, we
exit early from map_dump() (before calling dump_map_batch()) if we have
no key.
next prev parent reply other threads:[~2026-11-10 16:46 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-04 19:54 [PATCH bpf-next v9 0/2] bpftool: Use batch lookups for map dumps Tianyi Chen
2026-11-04 19:54 ` [PATCH bpf-next v9 1/2] " Tianyi Chen
2026-11-04 20:31 ` bot+bpf-ci
2026-11-07 13:12 ` Tianyi Chen
2026-11-10 16:46 ` Quentin Monnet [this message]
2026-11-04 19:54 ` [PATCH bpf-next v9 2/2] selftests/bpf: Check bpftool batch " Tianyi Chen
2026-11-10 16:46 ` Quentin Monnet
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=3901a136-d2e1-43eb-8284-31dae956391e@kernel.org \
--to=qmo@kernel.org \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=hi@tychen.cc \
--cc=ihor.solodrai@linux.dev \
--cc=linux-kselftest@vger.kernel.org \
--cc=shuah@kernel.org \
/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