Skip to contentSkip to conversation

hwmon: (coretemp) Fix missing sensors with uneven core counts

PATCH In review Aveline Noir 4 participants 8 messages Dec 4

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwmon: (coretemp) Fix missing sensors with uneven core counts

topology_num_cores_per_package() only counts the cores of one package. If another package has more cores, e.g. because the BIOS disabled cores on only one CPU, the extra cores get no temperature sensors.

Use num_possible_cpus() as the limit instead.

Fixes b89e1255c92b ("hwmon: (coretemp) replace hardcoded core count with dynamic value")

---
Only tested in QEMU, with 2+4 cores.

---
 drivers/hwmon/coretemp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hwmon/coretemp.c +2 -1
index 96d57fe12de6..2353d2d096e7 100644
@@ -491,7 +491,8 @@ init_temp_data(struct platform_data *pdata, unsigned int cpu, int pkg_flag)
 	struct temp_data *tdata;
 
 	if (!pdata->core_data) {
-		int nr_cores = topology_num_cores_per_package();
+		/* Packages can have different numbers of cores */
+		int nr_cores = num_possible_cpus();
 
 		pdata->core_data = kzalloc_objs(struct temp_data *, nr_cores);
 		if (!pdata->core_data)

---
base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
change-id: 20261205-coretemp-core-limit-4e403bcd7b15

#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] hwmon: (coretemp) oversized contiguous core_data array in init_temp_data() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] hwmon: (coretemp) oversized contiguous core_data array in init_temp_data()
--

commit b4264371dd35960dd3d06c662f4625da35ac4376
Author: Aveline Noir <[email protected]>

hwmon: (coretemp) Fix missing sensors with uneven core counts

This commit changes init_temp_data() to size the per-zone pdata->core_data array using num_possible_cpus() instead of topology_num_cores_per_package(), so packages with more cores than the first package do not run out of sensor slots.

Quoted 5 lines
drivers/hwmon/coretemp.c +0 -0
index 96d57fe12de6d..2353d2d096e7c 100644

drivers/hwmon/coretemp.c init_temp_data(struct platform_data *pdata, unsigned int cpu, int pkg_flag) · line 498

Quoted 9 lines
drivers/hwmon/coretemp.c +2 -1
@@ -491,7 +491,8 @@ init_temp_data(struct platform_data *pdata, unsigned int cpu, int pkg_flag)
 	struct temp_data *tdata;
 
 	if (!pdata->core_data) {
-		int nr_cores = topology_num_cores_per_package();
+		/* Packages can have different numbers of cores */
+		int nr_cores = num_possible_cpus();
 
 		pdata->core_data = kzalloc_objs(struct temp_data *, nr_cores);
 		if (!pdata->core_data)

[Severity: Medium]
Can sizing pdata->core_data to num_possible_cpus() cause high-order allocation failures and excessive per-zone memory use?

On x86_64 systems with more than 4096 possible CPUs (up to 8192 under CONFIG_CPUMASK_OFFSTACK), kzalloc_objs(struct temp_data *, nr_cores) requests an order-4 contiguous allocation (up to 64 KiB), exceeding PAGE_ALLOC_COSTLY_ORDER.

Because destroy_temp_data() frees pdata->core_data when all housekeeping CPUs in a zone go offline, re-onlining a CPU or loading coretemp on fragmented memory can stall compaction or fail with -ENOMEM, leaving cores without temperature sensors.

coretemp_init() creates one zone per package and die, so every zone now allocates slots for all packages, dies, and SMT siblings, scaling total memory as O(max_zones * num_possible_cpus()).

get_temp_data() also linearly scans all pdata->nr_cores entries on every coretemp_cpu_offline() call, so misses on isolated CPUs skipped by coretemp_cpu_online() scan all num_possible_cpus() mostly-NULL slots.

Since pdata->core_data is only used internally for lookup in get_temp_data(), replacing the fixed array and pdata->ida with a list_head or xarray (or at minimum using kvzalloc_objs()/kvfree()) would avoid the costly-order allocation and per-zone over-sizing.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

Hi Sashiko,

Quoted 2 lines · On Fri, 04 Dec 2026 15:04:36 +0000 [email protected] wrote:

Can sizing pdata->core_data to num_possible_cpus() cause high-order allocation failures and excessive per-zone memory use?

The order-4 allocation needs more than 4096 possible CPUs. With up to 512, it's at most 4 KiB per die, no more than the fixed array before b89e1255c92b.

Guenter, I can switch to kvzalloc_objs() in v2 if you prefer.

Thanks,
Aveline Noir

maintainer
#3 lore raw
Quoted 11 lines · Aveline Noir

Hi Sashiko,

Quoted 2 lines · On Fri, 04 Dec 2026 15:04:36 +0000 [email protected] wrote:

Can sizing pdata->core_data to num_possible_cpus() cause high-order allocation failures and excessive per-zone memory use?

The order-4 allocation needs more than 4096 possible CPUs. With up to 512, it's at most 4 KiB per die, no more than the fixed array before b89e1255c92b.

Please do not use the "old code was bad, this isn't worse" pseudo-argument.

Quoted 2 lines

Guenter, I can switch to kvzalloc_objs() in v2 if you prefer.

Please do. Ultimately, the driver should switch to dynamic allocation, but that should be a separate effort.

Guenter

#4 lore raw
In reply to Aveline Noir
Quoted 3 lines · Aveline Noir

topology_num_cores_per_package() only counts the cores of one package. If another package has more cores, e.g. because the BIOS disabled cores on only one CPU, the extra cores get no temperature sensors.

[...]

Quoted 1 line

Only tested in QEMU, with 2+4 cores.

Could you share the QEMU command line you used? Here coretemp does not load in a guest at all. With QEMU 11.1.4, KVM and -cpu host on an i7-14700F, the guest has no dtherm flag (KVM only exposes ARAT in CPUID leaf 6) and insmod coretemp.ko fails with -ENODEV. QEMU does not offer a dtherm CPU property either.

I also could not get an uneven count from QEMU. With
-smp cpus=6,sockets=2,cores=4,maxcpus=8 the guest prints "Num. cores per package: 4", because topo_register_apic() also puts the not-present APICs in the domain maps. The same happens with 2 cores present in package 0 and 4 in package 1. CPU hot-add is covered as well, since topology_hotplug_apic() rejects APIC IDs that were not registered at boot. So the count can only be too small when the package with the lowest APIC ID has fewer registered cores than another package.

Have you seen that happen, with "Adding Core N failed" from the unpatched driver, on real hardware or in a VM? Sashiko raised the same concern on b89e1255c92b, and at the time it was assumed not to be valid:
https://lore.kernel.org/linux-hwmon/[email protected]/

The change itself looks safe to me: num_possible_cpus() is a valid upper bound and costs no more than the old 512-entry array on most systems. It would help to state the exact condition in the commit message.

Thanks,
Asaf

#5 lore raw

Hi Asaf,

Quoted 1 line · On Fri, 04 Dec 2026 18:53:42 +0300 Asaf Nuri Bertizlioğlu wrote:

Could you share the QEMU command line you used?

I used TCG instead of KVM, and setcpuid= to force the dtherm flag. Something like this:

  qemu-system-x86_64 -accel tcg -m 512 -nographic -cpu Haswell \
    -smp 1,sockets=2,cores=2,maxcpus=4 \
    -device Haswell-x86_64-cpu,socket-id=1,core-id=0,thread-id=0 \
    -device Haswell-x86_64-cpu,socket-id=1,core-id=1,thread-id=0 \
    -kernel bzImage -initrd initramfs.cpio \
    -append "console=ttyS0 setcpuid=dtherm,pts"
Quoted 1 line

I also could not get an uneven count from QEMU.

Yeah, KVM hides it. The kernel also counts the not-present CPUs when it detects a hypervisor, see acpi_is_processor_usable(). It doesn't detect TCG as one, so they're skipped like on bare metal.

Quoted 2 lines

Have you seen that happen, with "Adding Core N failed" from the unpatched driver, on real hardware or in a VM?

Only in QEMU so far. But some dual-socket boards can disable cores on each CPU separately in the BIOS, so it should be possible on real hardware too.

Quoted 1 line

It would help to state the exact condition in the commit message.

Makes sense to me. I'll do that in v2.

Thanks,
Aveline Noir

Tested-by Reviewed-by
#6 lore raw
Quoted 3 lines · Aveline Noir

Yeah, KVM hides it. The kernel also counts the not-present CPUs when it detects a hypervisor, see acpi_is_processor_usable(). It doesn't detect TCG as one, so they're skipped like on bare metal.

You are right, I missed that. My KVM guest counted the disabled MADT entries only because it detected a hypervisor.

With your command line and a 7.3.7 guest kernel, the unpatched driver logs "platform coretemp.1: Adding Core 2 failed" and package 1 only has Core 0. With this patch applied, package 1 has Core 0 and Core 1, and nothing is logged. (TCG has no real sensor, so the reads return -ENODATA; I only checked which sensors get created.)

On an Intel Core i7-14700F (one package, 8 P-cores and 12 E-cores) the patched driver shows all 21 temperature inputs. 30 rounds of taking an SMT pair (cpu2/cpu3) and an E-core (cpu16) offline and back, and 20 rmmod/insmod rounds, with a loop reading all coretemp sysfs files in the background, gave no warnings in dmesg.

I used an LLM to help set up the QEMU test and draft this reply; I ran the tests, checked the results and reviewed the change myself.

If v2 only changes the commit message, feel free to keep these:

Thanks,
Asaf