[PATCH bpf v2 0/2] bpftool: Fix sparse CPU IDs

Hui Su posted 2 patches 6 days, 13 hours ago
There is a newer version of this series
tools/bpf/bpftool/common.c                | 40 ++++++++++++
tools/bpf/bpftool/main.h                  |  1 +
tools/bpf/bpftool/map.c                   | 76 ++++++++++++++++-------
tools/bpf/bpftool/prog.c                  | 51 ++++++++++-----
tools/bpf/bpftool/skeleton/profiler.bpf.c |  8 +--
5 files changed, 131 insertions(+), 45 deletions(-)
[PATCH bpf v2 0/2] bpftool: Fix sparse CPU IDs
Posted by Hui Su 6 days, 13 hours ago
bpftool currently assumes that possible CPU IDs are dense.  This is not true
when the possible CPU mask itself is sparse, such as 0,2-3.  In that case,
per-CPU map output labels and prog profile event-array keys can refer to the
wrong logical CPUs.

The first patch keeps dense per-CPU buffer slots separate from logical CPU
IDs when printing map values, and propagates errors from the shared output
path to its callers.

The second patch applies the same distinction to prog profile: userspace
keeps a compact CPU count for result buffers, while BPF event-array keys use
the logical CPU ID and the logical ID span.

Changes in v2:
- Add explicit bpftool errors for possible-CPU mask parsing and allocation
  failures.
- Rename the helper result variable to reflect that successful returns are
  CPU counts, and remove the duplicate CPU-count consistency check.
- Use a dedicated `cpu_cnt` in `map_dump()` instead of overloading `err`.
- Propagate BTF/output errors from `print_key_value()` through both callers.
- Remove the unused BPF-side `num_cpu` rodata and use the userspace
  `profile_cpu_cnt` for compact per-CPU buffers.
- Reset all profile CPU state on cleanup paths.

Testing:
- Built the final bpftool tree successfully.
- Booted an ARM64 QEMU guest from a DTS-built virt DTB with possible CPUs
  0,2-3.
- Verified plain/BTF and JSON per-CPU map output reports CPUs 0,2,3 and
  omits CPU 1.
- Ran `prog profile` with cycles and instructions; the profiler skeleton
  reached perf-event setup. QEMU did not provide a usable instructions PMU
  event, so no hardware profile counts are claimed.
- Confirmed the final v2 tree is code-identical to the runtime-tested tree;
  only commit metadata changed afterward.

Hui Su (2):
  bpftool: Fix CPU IDs in per-CPU map output
  bpftool: Fix sparse CPU IDs in prog profile

 tools/bpf/bpftool/common.c                | 40 ++++++++++++
 tools/bpf/bpftool/main.h                  |  1 +
 tools/bpf/bpftool/map.c                   | 76 ++++++++++++++++-------
 tools/bpf/bpftool/prog.c                  | 51 ++++++++++-----
 tools/bpf/bpftool/skeleton/profiler.bpf.c |  8 +--
 5 files changed, 131 insertions(+), 45 deletions(-)


base-commit: 238650ef6c7c7cca08e032527329424c9fbd70e5
-- 
2.55.0
Re: [PATCH bpf v2 0/2] bpftool: Fix sparse CPU IDs
Posted by Quentin Monnet 6 days, 12 hours ago
2026-09-18 19:20 UTC+0900 ~ Hui Su <sh_def@163.com>
> bpftool currently assumes that possible CPU IDs are dense.  This is not true
> when the possible CPU mask itself is sparse, such as 0,2-3.  In that case,
> per-CPU map output labels and prog profile event-array keys can refer to the
> wrong logical CPUs.
> 
> The first patch keeps dense per-CPU buffer slots separate from logical CPU
> IDs when printing map values, and propagates errors from the shared output
> path to its callers.
> 
> The second patch applies the same distinction to prog profile: userspace
> keeps a compact CPU count for result buffers, while BPF event-array keys use
> the logical CPU ID and the logical ID span.

Looks good to me, thank you!

Acked-by: Quentin Monnet <qmo@kernel.org>