[PATCH v3 0/3] cpupower: fix topology array handling
From: Ali Ahmet Memis
Date: Thu Aug 06 2026 - 13:54:23 EST
First, a correction. The v2 cover letter said I had no machine where a
topology attribute actually disappears during enumeration. That was wrong,
and it is the answer to your question about a real scenario: an offline CPU
is enough. The topology attribute group is added and removed by a CPU
hotplug callback in drivers/base/topology.c, so while a CPU is offline it
has no topology directory at all and both reads fail. chcpu -d, a write to
cpuN/online, or turning SMT off all get there.
Measured on a 4 CPU machine against its real sysfs, no fake tree this time,
calling get_cpu_topology() and printing what it decided:
all four CPUs online
unpatched cores=4
v3 series cores=4
cpu2 and cpu3 offlined
unpatched cores=3
with both continues removed cores=3
v3 series cores=2
Two online CPUs, one core each, so 3 is the wrong answer and 4 is
unaffected by the series.
> However, did you consider simplifying the logic in these conditionals?
> -- Is this continue necessary here?
No, they are not necessary, and removing them is the right thing. That is
patch 2. Without them the core == -1 check runs for the entries it was
written for and gives them a defined core_cpu_list of "-1", which is what
you meant by the branch being in the wrong place rather than dead.
It does not change the count on its own though, which is the third row
above. The count is seeded before anything is checked:
last_cpu_list = cpu_top->core_info[0].core_cpu_list;
cpu_top->cores = 1;
and "-1" sorts ahead of a real cpu list, so entry 0 after the qsort is a
placeholder and the count starts by counting it. Patch 3 is about that
seed, so the two changes are complementary rather than alternatives.
Patch 1 is unchanged from v2. Patch 2 makes the demonstrable uninitialized
read go away by itself, but calloc() is still what covers the remaining
path, a core_cpus_list read that fails and only warns, and it is the
smaller change for stable.
v2: https://lore.kernel.org/all/20260803175215.117518-1-ali@xxxxxxxxxxxxxx/
Ali Ahmet Memis (3):
cpupower: zero the topology array to avoid uninitialized reads
cpupower: let the core == -1 check handle failed topology reads
cpupower: do not count incomplete topology entries as physical cores
tools/power/cpupower/lib/cpupower.c | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)
base-commit: 0d839570765118029aa8bf4a95444c6a11aacf85
--
2.55.0