From: Zhang Rui <rui.zhang@intel.com>
To: rafael.j.wysocki@intel.com, lenb@kernel.org
Cc: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org
Subject: [PATCH 1/2] tools/power turbostat: Fix out-of-bounds memory access in thread_data allocation
Date: Tue, 10 Mar 2026 14:02:00 +0800 [thread overview]
Message-ID: <20260310060201.660773-1-rui.zhang@intel.com> (raw)
Turbostat suffers from a critical memory allocation bug that can cause
segmentation faults or silent data corruption.
This is because turbostat uses
1. num_cores * threads_per_core to allocate memory for per thread data
(threads_per_core is calculated based on online CPU thread siblings,
resulting in a value of 1 when SMT is inactive)
2. cpu_id to reference the thread data for a given cpu
And then, when SMT is inactive, it is possible that,
num_cores * threads_per_core < cpu_id of certain online cpus
and this causes out-of-bounds memory access when referencing the thread
data for these CPUs.
For example, on a 4-core system,
1. the core's are numbered 0,1,2,3
2. the CPU's are numbered 0,1,2,3 and the HT siblings are 4, 5,6,7,
When only CPU0 and CPU7 are online, turbostat allocates thread_data
memory for 4 threads (4 cores * 1 thread_per_core) but references
thread_data[7] for CPU7.
Fix the problem by using topo.max_cpu_num + 1 to represent the total
thread count instead. The topo.max_cpu_num value is derived from the
width of /sys/devices/system/cpu/cpuX/topology/thread_siblings, which
represents all possible CPUs regardless of their online/offline status.
This ensures adequate memory allocation for all potential CPU accesses.
Validated on multiple Intel platforms (ICX/SPR/SRF/EMR/GNR/CWF) with
various CPU online/offline configurations and SMT enabled/disabled
scenarios. No regression or memory access violation observed.
Fixes: a2b4d0f8bf07 ("tools/power turbostat: Favor cpu# over core#")
Signed-off-by: Zhang Rui <rui.zhang@intel.com>
---
tools/power/x86/turbostat/turbostat.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
index 1a2671c28209..ae827485950d 100644
--- a/tools/power/x86/turbostat/turbostat.c
+++ b/tools/power/x86/turbostat/turbostat.c
@@ -9702,13 +9702,12 @@ void allocate_counters(struct counters *counters)
{
int i;
int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages;
- int num_threads = topo.threads_per_core * num_cores;
- counters->threads = calloc(num_threads, sizeof(struct thread_data));
+ counters->threads = calloc(topo.max_cpu_num + 1, sizeof(struct thread_data));
if (counters->threads == NULL)
goto error;
- for (i = 0; i < num_threads; i++)
+ for (i = 0; i < topo.max_cpu_num + 1; i++)
(counters->threads)[i].cpu_id = -1;
counters->cores = calloc(num_cores, sizeof(struct core_data));
--
2.43.0
next reply other threads:[~2026-03-10 6:04 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-10 6:02 Zhang Rui [this message]
2026-03-10 6:02 ` [PATCH 2/2] tools/power turbostat: Optimize core count calculation and fix naming inconsistency Zhang Rui
2026-03-10 19:04 ` Len Brown
2026-03-10 19:03 ` [PATCH 1/2] tools/power turbostat: Fix out-of-bounds memory access in thread_data allocation Len Brown
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260310060201.660773-1-rui.zhang@intel.com \
--to=rui.zhang@intel.com \
--cc=lenb@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=rafael.j.wysocki@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox