[PATCH v2 2/2] cpupower: do not count incomplete topology entries as physical cores
Ali Ahmet Memis <[email protected]> Mon, 3 Aug 2026 17:52:08 +0000
| Newsgroups | org.kernel.vger.linux-pm,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <[email protected]> |
The physical core count is derived by sorting core_info by core_cpu_list
and counting how many distinct lists there are. The loop seeds the count
with entry 0 unconditionally:
last_cpu_list = cpu_top->core_info[0].core_cpu_list;
cpu_top->cores = 1;
An entry whose topology could not be read is still a member of the array,
carrying pkg and core of -1 and either an empty core_cpu_list or the
literal "-1". After the sort such an entry can land at index 0, where it is
counted as a physical core even though the pkg check inside the loop was
meant to exclude it. The check is also applied only to the entries that
follow, so the seed is never validated.
Skip entries without complete topology data and count from zero, so only
CPUs with a package, a core and a core list contribute.
Fixes: f89cb9cba7a2 ("cpupower: Implement CPU physical core querying")
Signed-off-by: Ali Ahmet Memis <[email protected]>
---
tools/power/cpupower/lib/cpupower.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/tools/power/cpupower/lib/cpupower.c b/tools/power/cpupower/lib/cpupower.c
index 559b04f4387e..a8ee304bdcc0 100644
--- a/tools/power/cpupower/lib/cpupower.c
+++ b/tools/power/cpupower/lib/cpupower.c
@@ -214,11 +214,17 @@ int get_cpu_topology(struct cpupower_topology *cpu_top)
qsort(cpu_top->core_info, cpus, sizeof(struct cpuid_core_info),
__compare_core_cpu_list);
- last_cpu_list = cpu_top->core_info[0].core_cpu_list;
- cpu_top->cores = 1;
- for (cpu = 1; cpu < cpus; cpu++) {
- if (strcmp(cpu_top->core_info[cpu].core_cpu_list, last_cpu_list) != 0 &&
- cpu_top->core_info[cpu].pkg != -1) {
+ last_cpu_list = NULL;
+ cpu_top->cores = 0;
+ for (cpu = 0; cpu < cpus; cpu++) {
+ if (cpu_top->core_info[cpu].pkg == -1 ||
+ cpu_top->core_info[cpu].core == -1 ||
+ cpu_top->core_info[cpu].core_cpu_list[0] == '\0')
+ continue;
+
+ if (!last_cpu_list ||
+ strcmp(cpu_top->core_info[cpu].core_cpu_list,
+ last_cpu_list) != 0) {
last_cpu_list = cpu_top->core_info[cpu].core_cpu_list;
cpu_top->cores++;
}
--
2.55.0