From: Tianyi Chen Use BPF_MAP_LOOKUP_BATCH when dumping hash maps to reduce the number of BPF syscalls while preserving plain, JSON and BTF formatting. For a 100,000-entry hash map in an x86-64 KVM guest, BPF syscall counts fell from 200,004 to 395. The median of five untraced runs fell from 0.774013 s to 0.733844 s, about 5.2% in this measurement. Syscall counts were collected separately with strace. Hash batch lookup must fit an entire bucket. Restrict eligibility to maps whose maximum key/value storage fits in 4 MiB, so even a worst-case bucket can fit without restarting a partially printed dump. Start with up to 256 entries and grow on ENOSPC using the same input cursor. Fall back to individual lookups only when the initial batch operation is unsupported. Restarting after output has begun would duplicate entries. Process the final partial batch on ENOENT, but do not trust count or output buffers after other errors. Keep fatal diagnostics on stderr so JSON element arrays contain only map entries. Link: https://github.com/libbpf/bpftool/issues/63 Assisted-by: LLM Signed-off-by: Tianyi Chen --- tools/bpf/bpftool/map.c | 117 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 109 insertions(+), 8 deletions(-) diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 684a8fb7241..971da173600 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -740,15 +740,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) +static void print_map_elem(void *key, void *value, + struct bpf_map_info *map_info, struct btf *btf, + json_writer_t *btf_wtr) { - 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); } else if (btf) { @@ -762,10 +757,112 @@ static int dump_map_elem(int fd, void *key, void *value, } else { print_entry_plain(map_info, key, value); } +} + +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) +{ + 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); return 0; } +#define MAP_DUMP_BATCH_FALLBACK 1 +#define MAP_DUMP_BATCH_SIZE 256U +#define MAP_DUMP_BATCH_MAX_BYTES (4 * 1024 * 1024) + +/* Return MAP_DUMP_BATCH_FALLBACK only before batch traversal starts. */ +static int dump_map_batch(int fd, void *key, void *value, + struct bpf_map_info *info, struct btf *btf, + json_writer_t *wtr, unsigned int *num_elems) +{ + __u32 capacity, count, batch = 0, next_batch = 0, i; + void *keys = NULL, *values = NULL, *buf; + bool first = true, can_fallback = true; + int err; + + /* + * Hash lookup batches must accommodate a whole bucket. Restrict the + * optimization to maps whose worst-case bucket fits the memory budget, + * so a later ENOSPC never forces a restart after printing some entries. + * Division also bounds the allocation multiplications on 32-bit hosts. + */ + if (info->type != BPF_MAP_TYPE_HASH || !info->max_entries || + (__u64)info->key_size + info->value_size > + MAP_DUMP_BATCH_MAX_BYTES / info->max_entries) + return MAP_DUMP_BATCH_FALLBACK; + + capacity = min(info->max_entries, MAP_DUMP_BATCH_SIZE); +resize: + buf = realloc(keys, (size_t)capacity * info->key_size); + if (!buf) { + err = ENOMEM; + goto error; + } + keys = buf; + buf = realloc(values, (size_t)capacity * info->value_size); + if (!buf) { + err = ENOMEM; + goto error; + } + values = buf; + + while (true) { + count = capacity; + err = bpf_map_lookup_batch(fd, first ? NULL : &batch, + &next_batch, keys, values, &count, NULL); + err = err ? errno : 0; + /* + * Older kernels reject the command before updating count. Do not + * inspect the buffers on these errors, or fall back after progress. + */ + if (can_fallback && (err == EINVAL || err == EOPNOTSUPP || + err == 524 /* ENOTSUPP */)) { + err = MAP_DUMP_BATCH_FALLBACK; + goto out; + } + can_fallback = false; + if (err == ENOSPC) { + if (capacity == info->max_entries) + goto error; + capacity += min(capacity, info->max_entries - capacity); + /* Preserve the input cursor: the oversized bucket was not read. */ + goto resize; + } + /* In particular, EFAULT can leave count and the buffers invalid. */ + if (err && err != ENOENT) + goto error; + for (i = 0; i < count; i++) { + /* + * Keep the alignment provided by individual lookups, including + * for BTF types whose map key/value size is not aligned. + */ + memcpy(key, keys + (size_t)i * info->key_size, info->key_size); + memcpy(value, values + (size_t)i * info->value_size, info->value_size); + print_map_elem(key, value, info, btf, wtr); + (*num_elems)++; + } + if (err == ENOENT) { + err = 0; + goto out; + } + first = false; + batch = next_batch; + } +error: + fprintf(stderr, "Error: can't lookup map batch: %s\n", strerror(err)); + err = -1; +out: + free(keys); + free(values); + return err; +} + static int maps_have_btf(int *fds, int nb_fds) { struct bpf_map_info info = {}; @@ -869,6 +966,9 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr, p_info("Warning: cannot read values from %s map with value_size != 8", map_type_str); } + err = dump_map_batch(fd, key, value, info, btf, wtr, &num_elems); + if (err != MAP_DUMP_BATCH_FALLBACK) + goto end_dump; while (true) { err = bpf_map_get_next_key(fd, prev_key, key); if (err) { @@ -881,6 +981,7 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr, prev_key = key; } +end_dump: if (wtr) { jsonw_end_array(wtr); /* elements */ if (show_header) -- 2.55.0