[PATCH 6.1.y 1/2] libperf cpumap: Add for_each_cpu() that skips the "any CPU" case
Sasha Levin
sashal at kernel.org
Thu Dec 3 20:21:10 PST 2026
From: Ian Rogers <irogers at google.com>
[ Upstream commit 5805c82513c444333efb086017be8d666336858a ]
When iterating CPUs in a CPU map it is often desirable to skip the "any
CPU" (aka dummy) case. Add a helper for this and use in builtin-record.
Reviewed-by: James Clark <james.clark at arm.com>
Signed-off-by: Ian Rogers <irogers at google.com>
Cc: Adrian Hunter <adrian.hunter at intel.com>
Cc: Alexander Shishkin <alexander.shishkin at linux.intel.com>
Cc: Alexandre Ghiti <alexghiti at rivosinc.com>
Cc: Andrew Jones <ajones at ventanamicro.com>
Cc: André Almeida <andrealmeid at igalia.com>
Cc: Athira Jajeev <atrajeev at linux.vnet.ibm.com>
Cc: Atish Patra <atishp at rivosinc.com>
Cc: Changbin Du <changbin.du at huawei.com>
Cc: Darren Hart <dvhart at infradead.org>
Cc: Davidlohr Bueso <dave at stgolabs.net>
Cc: Huacai Chen <chenhuacai at kernel.org>
Cc: Ingo Molnar <mingo at redhat.com>
Cc: Jiri Olsa <jolsa at kernel.org>
Cc: John Garry <john.g.garry at oracle.com>
Cc: K Prateek Nayak <kprateek.nayak at amd.com>
Cc: Kajol Jain <kjain at linux.ibm.com>
Cc: Kan Liang <kan.liang at linux.intel.com>
Cc: Leo Yan <leo.yan at linaro.org>
Cc: Mark Rutland <mark.rutland at arm.com>
Cc: Mike Leach <mike.leach at linaro.org>
Cc: Namhyung Kim <namhyung at kernel.org>
Cc: Nick Desaulniers <ndesaulniers at google.com>
Cc: Paolo Bonzini <pbonzini at redhat.com>
Cc: Paran Lee <p4ranlee at gmail.com>
Cc: Peter Zijlstra <peterz at infradead.org>
Cc: Ravi Bangoria <ravi.bangoria at amd.com>
Cc: Sandipan Das <sandipan.das at amd.com>
Cc: Sean Christopherson <seanjc at google.com>
Cc: Steinar H. Gunderson <sesse at google.com>
Cc: Suzuki Poulouse <suzuki.poulose at arm.com>
Cc: Thomas Gleixner <tglx at linutronix.de>
Cc: Will Deacon <will at kernel.org>
Cc: Yang Jihong <yangjihong1 at huawei.com>
Cc: Yang Li <yang.lee at linux.alibaba.com>
Cc: Yanteng Si <siyanteng at loongson.cn>
Cc: bpf at vger.kernel.org
Cc: coresight at lists.linaro.org
Cc: linux-arm-kernel at lists.infradead.org
Link: https://lore.kernel.org/r/20231129060211.1890454-6-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme at redhat.com>
Stable-dep-of: 82b4a28e7588 ("perf record: Reject CPU indices equal to the mask size")
Signed-off-by: Sasha Levin <sashal at kernel.org>
---
tools/lib/perf/include/perf/cpumap.h | 6 ++++++
tools/perf/builtin-record.c | 4 +---
2 files changed, 7 insertions(+), 3 deletions(-)
diff --git a/tools/lib/perf/include/perf/cpumap.h b/tools/lib/perf/include/perf/cpumap.h
index 03aceb72a783c..60c4e05470102 100644
--- a/tools/lib/perf/include/perf/cpumap.h
+++ b/tools/lib/perf/include/perf/cpumap.h
@@ -31,6 +31,12 @@ LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_c
(idx) < perf_cpu_map__nr(cpus); \
(idx)++, (cpu) = perf_cpu_map__cpu(cpus, idx))
+#define perf_cpu_map__for_each_cpu_skip_any(_cpu, idx, cpus) \
+ for ((idx) = 0, (_cpu) = perf_cpu_map__cpu(cpus, idx); \
+ (idx) < perf_cpu_map__nr(cpus); \
+ (idx)++, (_cpu) = perf_cpu_map__cpu(cpus, idx)) \
+ if ((_cpu).cpu != -1)
+
#define perf_cpu_map__for_each_idx(idx, cpus) \
for ((idx) = 0; (idx) < perf_cpu_map__nr(cpus); (idx)++)
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index fa2a066005ad0..eac672d02e94b 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -3545,9 +3545,7 @@ static int record__mmap_cpu_mask_init(struct mmap_cpu_mask *mask, struct perf_cp
if (cpu_map__is_dummy(cpus))
return 0;
- perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
- if (cpu.cpu == -1)
- continue;
+ perf_cpu_map__for_each_cpu_skip_any(cpu, idx, cpus) {
/* Return ENODEV is input cpu is greater than max cpu */
if ((unsigned long)cpu.cpu > mask->nbits)
return -ENODEV;
--
2.53.0
More information about the linux-arm-kernel
mailing list