Re: [PATCH bpf v2 2/2] bpftool: Fix sparse CPU IDs in prog profile

From: Andrii Nakryiko

Date: Mon Sep 21 2026 - 18:00:12 EST


On Fri, Sep 18, 2026 at 3:21 AM Hui Su <sh_def@xxxxxxx> wrote:
>
> bpftool prog profile currently treats the number of possible CPUs as
> both the logical CPU ID range and the stride of the perf event array.
> That misses valid logical CPUs when the possible CPU mask is sparse, such
> as 0,2-3, and can use incorrect PERF_EVENT_ARRAY keys.
>
> Keep the compact possible CPU count for per-CPU result buffers, while
> using the actual logical CPU IDs for perf events and the maximum logical
> CPU ID plus one as the metric stride in the PERF_EVENT_ARRAY. Keep the
> userspace perf file descriptor index separate from the BPF map key. Do
> not advance profile_perf_event_cnt for CPUs that return ENODEV, since it
> tracks opened userspace perf file descriptors rather than event-array
> slots.
>
> Tested:
> - Built tools/bpf/bpftool successfully on the host.
> - Booted an arm64 QEMU guest with a patched virt device tree reporting
> possible=0,2-3, present=0,2-3, and online=0,2-3.
> - Ran both pre-fix and fixed bpftool with cycles and instructions against
> a BTF-enabled fentry target. The pre-fix binary faulted in
> perf_event_alloc(), while the fixed binary reached perf-event setup and
> reported failure to create the instructions event on CPU 0. QEMU did
> not provide a usable hardware PMU runtime result, so no profile counts
> are claimed.
>
> Fixes: 47c09d6a9f67 ("bpftool: Introduce "prog profile" command")
> Link: https://lore.kernel.org/bpf/20260813160858.1042834-3-sh_def@xxxxxxx/
> Signed-off-by: Hui Su <sh_def@xxxxxxx>
> ---
> tools/bpf/bpftool/prog.c | 51 +++++++++++++++--------
> tools/bpf/bpftool/skeleton/profiler.bpf.c | 8 ++--
> 2 files changed, 38 insertions(+), 21 deletions(-)
>

I don't think these fixes should go into bpf tree, please rebase them
onto bpf-next/master and resend. As is they don't apply cleanly.

pw-bot: cr


[...]