All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] hwmon: (coretemp) Fix missing sensors with uneven core counts
@ 2026-12-04 14:46 Aveline Noir
  2026-12-04 15:04 ` sashiko-bot
  2026-12-04 15:53 ` Asaf Nuri Bertizlioğlu
  0 siblings, 2 replies; 9+ messages in thread
From: Aveline Noir @ 2026-12-04 14:46 UTC (permalink / raw)
  To: linux; +Cc: bakshansky.lists, linux-hwmon, sashiko-reviews, sashiko-bot

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")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/linux-hwmon/20260516121622.1E9DAC19425@smtp.kernel.org/
Signed-off-by: Aveline Noir <jm5905938@gmail.com>
---
Only tested in QEMU, with 2+4 cores.
---
 drivers/hwmon/coretemp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/hwmon/coretemp.c b/drivers/hwmon/coretemp.c
index 96d57fe12de6..2353d2d096e7 100644
--- a/drivers/hwmon/coretemp.c
+++ b/drivers/hwmon/coretemp.c
@@ -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


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] hwmon: (coretemp) Fix missing sensors with uneven core counts
  2026-12-04 14:46 [PATCH] hwmon: (coretemp) Fix missing sensors with uneven core counts Aveline Noir
@ 2026-12-04 15:04 ` sashiko-bot
  2026-12-04 15:13   ` Aveline Noir
  2026-12-04 15:53 ` Asaf Nuri Bertizlioğlu
  1 sibling, 1 reply; 9+ messages in thread
From: sashiko-bot @ 2026-12-04 15:04 UTC (permalink / raw)
  To: Aveline Noir; +Cc: linux-hwmon

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 <jm5905938@gmail.com>

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.

> diff --git a/drivers/hwmon/coretemp.c b/drivers/hwmon/coretemp.c
> index 96d57fe12de6d..2353d2d096e7c 100644
> --- a/drivers/hwmon/coretemp.c
> +++ b/drivers/hwmon/coretemp.c
> @@ -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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205-coretemp-core-limit-v1-1-a7020e0e4208@gmail.com?part=1

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] hwmon: (coretemp) Fix missing sensors with uneven core counts
  2026-12-04 15:04 ` sashiko-bot
@ 2026-12-04 15:13   ` Aveline Noir
  2026-12-04 20:34     ` Guenter Roeck
  0 siblings, 1 reply; 9+ messages in thread
From: Aveline Noir @ 2026-12-04 15:13 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: bakshansky.lists, linux-hwmon, linux, sashiko-bot

Hi Sashiko,

On Fri, 04 Dec 2026 15:04:36 +0000 sashiko-bot@kernel.org 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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] hwmon: (coretemp) Fix missing sensors with uneven core counts
  2026-12-04 14:46 [PATCH] hwmon: (coretemp) Fix missing sensors with uneven core counts Aveline Noir
  2026-12-04 15:04 ` sashiko-bot
@ 2026-12-04 15:53 ` Asaf Nuri Bertizlioğlu
  2026-12-04 16:04   ` Aveline Noir
  1 sibling, 1 reply; 9+ messages in thread
From: Asaf Nuri Bertizlioğlu @ 2026-12-04 15:53 UTC (permalink / raw)
  To: jm5905938; +Cc: linux, linux-hwmon, bakshansky.lists, sashiko-reviews

On Sat, Dec 05, 2026 at 01:46:08AM +1100, Aveline Noir wrote:
> 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.
[...]
> 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/8a756afa-20d1-49a3-8cb1-7f8c62d5700e@roeck-us.net/

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] hwmon: (coretemp) Fix missing sensors with uneven core counts
  2026-12-04 15:53 ` Asaf Nuri Bertizlioğlu
@ 2026-12-04 16:04   ` Aveline Noir
  2026-12-04 16:30     ` Asaf Nuri Bertizlioğlu
  0 siblings, 1 reply; 9+ messages in thread
From: Aveline Noir @ 2026-12-04 16:04 UTC (permalink / raw)
  To: Asaf Nuri Bertizlioğlu
  Cc: bakshansky.lists, linux-hwmon, linux, sashiko-bot,
	sashiko-reviews

Hi Asaf,

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"

> 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.

> 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.

> 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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] hwmon: (coretemp) Fix missing sensors with uneven core counts
  2026-12-04 16:04   ` Aveline Noir
@ 2026-12-04 16:30     ` Asaf Nuri Bertizlioğlu
  2026-12-04 16:43       ` Aveline Noir
  0 siblings, 1 reply; 9+ messages in thread
From: Asaf Nuri Bertizlioğlu @ 2026-12-04 16:30 UTC (permalink / raw)
  To: jm5905938
  Cc: linux, linux-hwmon, bakshansky.lists, sashiko-reviews,
	sashiko-bot

On Sat, Dec 05, 2026 at 03:04:42AM +1100, Aveline Noir wrote:
> 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:

Tested-by: Asaf Nuri Bertizlioğlu <asafbertizlioglu@gmail.com>
Reviewed-by: Asaf Nuri Bertizlioğlu <asafbertizlioglu@gmail.com>

Thanks,
Asaf

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] hwmon: (coretemp) Fix missing sensors with uneven core counts
  2026-12-04 16:30     ` Asaf Nuri Bertizlioğlu
@ 2026-12-04 16:43       ` Aveline Noir
  0 siblings, 0 replies; 9+ messages in thread
From: Aveline Noir @ 2026-12-04 16:43 UTC (permalink / raw)
  To: Asaf Nuri Bertizlioğlu
  Cc: linux, linux-hwmon, bakshansky.lists, sashiko-reviews,
	sashiko-bot

Thanks Asaf :)

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] hwmon: (coretemp) Fix missing sensors with uneven core counts
  2026-12-04 15:13   ` Aveline Noir
@ 2026-12-04 20:34     ` Guenter Roeck
  2026-12-05  6:11       ` Aveline Noir
  0 siblings, 1 reply; 9+ messages in thread
From: Guenter Roeck @ 2026-12-04 20:34 UTC (permalink / raw)
  To: Aveline Noir, sashiko-reviews; +Cc: bakshansky.lists, linux-hwmon, sashiko-bot

On 12/4/26 07:13, Aveline Noir wrote:
> Hi Sashiko,
> 
> On Fri, 04 Dec 2026 15:04:36 +0000 sashiko-bot@kernel.org 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.

> 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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] hwmon: (coretemp) Fix missing sensors with uneven core counts
  2026-12-04 20:34     ` Guenter Roeck
@ 2026-12-05  6:11       ` Aveline Noir
  0 siblings, 0 replies; 9+ messages in thread
From: Aveline Noir @ 2026-12-05  6:11 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Asaf Nuri Bertizlioğlu, bakshansky.lists, linux-hwmon,
	sashiko-bot, sashiko-reviews

Hi Guenter,

On Fri, 04 Dec 2026 12:34:41 -0800 Guenter Roeck wrote:

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

Ah, got it. I'll avoid that.

> > 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.

Will do in v2. I can look into the dynamic allocation separately
afterwards.

Thanks,
Aveline Noir

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-12-05  6:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 14:46 [PATCH] hwmon: (coretemp) Fix missing sensors with uneven core counts Aveline Noir
2026-12-04 15:04 ` sashiko-bot
2026-12-04 15:13   ` Aveline Noir
2026-12-04 20:34     ` Guenter Roeck
2026-12-05  6:11       ` Aveline Noir
2026-12-04 15:53 ` Asaf Nuri Bertizlioğlu
2026-12-04 16:04   ` Aveline Noir
2026-12-04 16:30     ` Asaf Nuri Bertizlioğlu
2026-12-04 16:43       ` Aveline Noir

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.