Skip to contentSkip to conversation

hwmon: (coretemp) Report unreliable temperature readings

PATCH Applied Ricardo Neri 3 participants 14 messages Sep 25 – Sep 28

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

cover hwmon: (coretemp) Report unreliable temperature readings

Hi,

Intel CPUs indicate in IA32_[PACKAGE]_THERM_STATUS whether the digital thermal readout they expose is valid. coretemp deliberately ignores that indication, for the reason given in commit bf6ea084ebb5 ("hwmon:
(coretemp) Do not return -EAGAIN for low temperatures"): some CPUs clear it while the temperature is too low to be measured, and the value reported in that state is more useful to userspace than an error would be.

The consequence is that userspace cannot distinguish a genuinely low temperature from one the CPU could not measure. This series exposes the indication through the standard hwmon temp%d_fault attribute, leaving temp%d_input exactly as it is.

One user-visible effect is worth mentioning: sensors(1) prints FAULT in place of the temperature when temp%d_fault reads 1. On a CPU that clears the valid bit at low temperature, that core stops showing a number in the default output, although sensors -u and -j still report it, as does anything that reads temp%d_input from sysfs directly. A driver-custom attribute name would avoid this, but would be invisible to generic tools. Reporting the condition through the documented attribute looks like a better option, but please say if you prefer otherwise.

Patch 1 is pre-work: it fixes an existing bug where, on 32-bit kernels, the first temperature read is skipped and temp%d_input reports zero for the first 301 seconds of uptime. Both the bug and the fix were verified with a 32-bit kernel. It carries a Fixes: tag and does not depend on the rest of the series.

Patch 2 stops reading TjMax on a cache hit, where the value is discarded. It is separate so that the behavioral change is not buried in the restructuring patch 3 does.

Patch 3 adds temp%d_fault.

Thanks and BR,
Ricardo

---
Ricardo Neri (3):
hwmon: (coretemp) Refresh the temperature on the first read
hwmon: (coretemp) Read TjMax only when refreshing the temperature
hwmon: (coretemp) Add temp%d_fault sysfs attribute

 Documentation/hwmon/coretemp.rst |  4 +++
 drivers/hwmon/coretemp.c         | 71 +++++++++++++++++++++++++++++-----------
 2 files changed, 55 insertions(+), 20 deletions(-)

---
base-commit: 93f51579e7df248780214094418f205253383cc5
change-id: 20260831-coretemp-temp-fault-a44c447936ec

Best regards,

Signature · 1 line
Ricardo Neri <[email protected]>
#1 lore raw

PATCH 1/3 hwmon: (coretemp) Refresh the temperature on the first read

show_temp() reads the status MSR only when the cached temperature is older than one second. Commit 5c0e64dde80f ("hwmon: (coretemp) Remove obsolete temp_data->valid") dropped the tdata->valid check that used to force the very first read, leaving that comparison as the only trigger.

A never-updated temp_data carries a zero timestamp, which does not look stale on 32-bit kernels: jiffies starts 300 seconds short of wrapping, so the comparison stays false until jiffies wraps and passes HZ. For the first 301 seconds of uptime temp%d_input reports the zero left by the allocator rather than the CPU temperature. 64-bit kernels are
unaffected: jiffies starts at a positive value there and does not wrap.

Backdate the timestamp when the temperature data is allocated. One jiffy older than the caching interval is stale under either word size, and the first refresh overwrites it.

Fixes 5c0e64dde80f ("hwmon: (coretemp) Remove obsolete temp_data->valid")
---
 drivers/hwmon/coretemp.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/hwmon/coretemp.c +5 -0
index 5095eb057680..ace51e08e72d 100644
@@ -511,6 +511,11 @@ init_temp_data(struct platform_data *pdata, unsigned int cpu, int pkg_flag)
 	tdata->cpu = cpu;
 	tdata->cpu_core_id = topology_core_id(cpu);
 	tdata->attr_size = MAX_CORE_ATTRS;
+	/*
+	 * A zero timestamp does not look stale on 32-bit, where jiffies
+	 * starts just short of wrapping. Backdate it instead.
+	 */
+	tdata->last_updated = jiffies - HZ - 1;
 	mutex_init(&tdata->update_lock);
 	return tdata;
 }
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/3] hwmon: (coretemp) Refresh the temperature on the first read

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 12 lines

show_temp() reads the status MSR only when the cached temperature is older than one second. Commit 5c0e64dde80f ("hwmon: (coretemp) Remove obsolete temp_data->valid") dropped the tdata->valid check that used to force the very first read, leaving that comparison as the only trigger.

A never-updated temp_data carries a zero timestamp, which does not look stale on 32-bit kernels: jiffies starts 300 seconds short of wrapping, so the comparison stays false until jiffies wraps and passes HZ. For the first 301 seconds of uptime temp%d_input reports the zero left by the allocator rather than the CPU temperature. 64-bit kernels are
unaffected: jiffies starts at a positive value there and does not wrap.

Quoted 6 lines

Backdate the timestamp when the temperature data is allocated. One jiffy older than the caching interval is stale under either word size, and the first refresh overwrites it.

Fixes 5c0e64dde80f ("hwmon: (coretemp) Remove obsolete temp_data->valid")

Sashiko has reviewed this patch and found no issues. It looks great!

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

Re: [PATCH 1/3] hwmon: (coretemp) Refresh the temperature on the first read

In reply to Ricardo Neri
Quoted 12 lines · Ricardo Neri

show_temp() reads the status MSR only when the cached temperature is older than one second. Commit 5c0e64dde80f ("hwmon: (coretemp) Remove obsolete temp_data->valid") dropped the tdata->valid check that used to force the very first read, leaving that comparison as the only trigger.

A never-updated temp_data carries a zero timestamp, which does not look stale on 32-bit kernels: jiffies starts 300 seconds short of wrapping, so the comparison stays false until jiffies wraps and passes HZ. For the first 301 seconds of uptime temp%d_input reports the zero left by the allocator rather than the CPU temperature. 64-bit kernels are
unaffected: jiffies starts at a positive value there and does not wrap.

Quoted 6 lines

Backdate the timestamp when the temperature data is allocated. One jiffy older than the caching interval is stale under either word size, and the first refresh overwrites it.

Fixes 5c0e64dde80f ("hwmon: (coretemp) Remove obsolete temp_data->valid")

Applied.

Thanks,
Guenter

#4 lore raw

PATCH 2/3 hwmon: (coretemp) Read TjMax only when refreshing the temperature

In reply to Ricardo Neri

show_temp() reads TjMax before checking whether the cached temperature is still fresh, but consumes it only when recomputing that temperature. On a cache hit the value is discarded.

Discarding it is not free. On CPUs that report TjMax in MSR_IA32_TEMPERATURE_TARGET, get_tjmax() returns the value it just read without storing it in tdata->tjmax, so every call reads the MSR again. That is a cross-CPU call whenever the CPU owning the sensor is not the current one, and it is made while holding tdata->update_lock.

Read TjMax where it is used. The value cannot go stale in the meantime:
bits 23:16 of MSR_IA32_TEMPERATURE_TARGET are read-only, and the register is package-scoped, so every CPU of the package reads the same value.

The temperature reported to userspace does not change.

---
 drivers/hwmon/coretemp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/coretemp.c +1 -1
index ace51e08e72d..0ab6bbff5637 100644
@@ -402,9 +402,9 @@ static ssize_t show_temp(struct device *dev,
 
 	mutex_lock(&tdata->update_lock);
 
-	tjmax = get_tjmax(tdata, dev);
 	/* Check whether the time interval has elapsed */
 	if (time_after(jiffies, tdata->last_updated + HZ)) {
+		tjmax = get_tjmax(tdata, dev);
 		rdmsrq_on_cpu(tdata->cpu, tdata->status_reg, &val.q);
 		/*
 		 * Ignore the valid bit. In all observed cases the register
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 2/3] hwmon: (coretemp) Read TjMax only when refreshing the temperature

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

show_temp() reads TjMax before checking whether the cached temperature is still fresh, but consumes it only when recomputing that temperature. On a cache hit the value is discarded.

Discarding it is not free. On CPUs that report TjMax in MSR_IA32_TEMPERATURE_TARGET, get_tjmax() returns the value it just read without storing it in tdata->tjmax, so every call reads the MSR again. That is a cross-CPU call whenever the CPU owning the sensor is not the current one, and it is made while holding tdata->update_lock.

Quoted 7 lines

Read TjMax where it is used. The value cannot go stale in the meantime:
bits 23:16 of MSR_IA32_TEMPERATURE_TARGET are read-only, and the register is package-scoped, so every CPU of the package reads the same value.

The temperature reported to userspace does not change.

Sashiko has reviewed this patch and found no issues. It looks great!

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

Re: [PATCH 2/3] hwmon: (coretemp) Read TjMax only when refreshing the temperature

In reply to Ricardo Neri
Quoted 10 lines · Ricardo Neri

show_temp() reads TjMax before checking whether the cached temperature is still fresh, but consumes it only when recomputing that temperature. On a cache hit the value is discarded.

Discarding it is not free. On CPUs that report TjMax in MSR_IA32_TEMPERATURE_TARGET, get_tjmax() returns the value it just read without storing it in tdata->tjmax, so every call reads the MSR again. That is a cross-CPU call whenever the CPU owning the sensor is not the current one, and it is made while holding tdata->update_lock.

Quoted 7 lines

Read TjMax where it is used. The value cannot go stale in the meantime:
bits 23:16 of MSR_IA32_TEMPERATURE_TARGET are read-only, and the register is package-scoped, so every CPU of the package reads the same value.

The temperature reported to userspace does not change.

Applied. No need to resend this or the previous patch.

Thanks,
Guenter

#7 lore raw

PATCH 3/3 hwmon: (coretemp) Add temp%d_fault sysfs attribute

In reply to Ricardo Neri

The temp%d_input sysfs attribute reports the CPU core temperature regardless of the valid bit (bit 31) in IA32_[PACKAGE]_THERM_STATUS. Commit bf6ea084ebb5 ("hwmon: (coretemp) Do not return -EAGAIN for low temperatures") established this behavior after observing that some Intel CPUs do not set the valid bit while the temperature is too low to be measured. The value reported is of more use than an error. Changing this is not an option: the interface is long-established and userspace relies on it never failing.

Userspace, however, cannot distinguish a genuinely low temperature from one the CPU could not measure. Until that commit it could: reading temp%d_input returned -EAGAIN. That signal was removed and nothing replaced it. Report the inverse of the valid bit through the standard hwmon temp%d_fault attribute. Its documented meaning is that the measurement for that channel should not be trusted. Tools built on libsensors consume the standard attribute.

temp%d_input continues to ignore the valid bit. A new update_temp() helper refreshes the cached temperature and its fault state together, so both attributes are served from the same reading of the status register.

---
 Documentation/hwmon/coretemp.rst |  4 +++
 drivers/hwmon/coretemp.c         | 68 +++++++++++++++++++++++++++-------------
 2 files changed, 51 insertions(+), 21 deletions(-)
Documentation/hwmon/coretemp.rst +4 -0
index 349301683381..e3e3205b5155 100644
@@ -50,6 +50,10 @@ All sysfs entries are named with their core_id (represented here by 'X').
 
 ================= ========================================================
 tempX_input	  Core temperature (in millidegrees Celsius).
+tempX_fault	  Set when the CPU reports the reading in tempX_input as
+		  invalid. Some CPUs do this while the temperature is too
+		  low to be measured. tempX_input still reports a value in
+		  this state; it is low or zero.
 tempX_max	  Maximum recommended operating temperature (Tcontrol).
 		  All cooling devices should be turned on.
 tempX_crit	  Maximum junction temperature (in millidegrees Celsius).
drivers/hwmon/coretemp.c +47 -21

106 lines not on this page · raw message

Signature · 1 line
2.43.0
#8 lore raw

Re: [PATCH 3/3] hwmon: (coretemp) Add temp%d_fault sysfs attribute

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines

The temp%d_input sysfs attribute reports the CPU core temperature regardless of the valid bit (bit 31) in IA32_[PACKAGE]_THERM_STATUS. Commit bf6ea084ebb5 ("hwmon: (coretemp) Do not return -EAGAIN for low temperatures") established this behavior after observing that some Intel CPUs do not set the valid bit while the temperature is too low to be measured. The value reported is of more use than an error. Changing this is not an option: the interface is long-established and userspace relies on it never failing.

Userspace, however, cannot distinguish a genuinely low temperature from one the CPU could not measure. Until that commit it could: reading temp%d_input returned -EAGAIN. That signal was removed and nothing replaced it. Report the inverse of the valid bit through the standard hwmon temp%d_fault attribute. Its documented meaning is that the measurement for that channel should not be trusted. Tools built on libsensors consume the standard attribute.

Quoted 5 lines

temp%d_input continues to ignore the valid bit. A new update_temp() helper refreshes the cached temperature and its fault state together, so both attributes are served from the same reading of the status register.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer
#9 lore raw
In reply to Ricardo Neri
Quoted 14 lines · Ricardo Neri

Hi,

Intel CPUs indicate in IA32_[PACKAGE]_THERM_STATUS whether the digital thermal readout they expose is valid. coretemp deliberately ignores that indication, for the reason given in commit bf6ea084ebb5 ("hwmon:
(coretemp) Do not return -EAGAIN for low temperatures"): some CPUs clear it while the temperature is too low to be measured, and the value reported in that state is more useful to userspace than an error would be.

The consequence is that userspace cannot distinguish a genuinely low temperature from one the CPU could not measure. This series exposes the indication through the standard hwmon temp%d_fault attribute, leaving temp%d_input exactly as it is.

Quoted 8 lines

One user-visible effect is worth mentioning: sensors(1) prints FAULT in place of the temperature when temp%d_fault reads 1. On a CPU that clears the valid bit at low temperature, that core stops showing a number in the default output, although sensors -u and -j still report it, as does anything that reads temp%d_input from sysfs directly. A driver-custom attribute name would avoid this, but would be invisible to generic tools. Reporting the condition through the documented attribute looks like a better option, but please say if you prefer otherwise.

I think it would be _much_ better to return -ENODATA for invalid readings. This isn't really a fault, after all. The sensor is not defective, it just can not provide valid data.

With -ENODATA the sensors command reports N/A for the temperature measurement, which I also think would be better than reporting FAULT.

Thanks,
Guenter

#10 lore raw
Quoted 24 lines · Guenter Roeck
Quoted 14 lines · Ricardo Neri

Hi,

Intel CPUs indicate in IA32_[PACKAGE]_THERM_STATUS whether the digital thermal readout they expose is valid. coretemp deliberately ignores that indication, for the reason given in commit bf6ea084ebb5 ("hwmon:
(coretemp) Do not return -EAGAIN for low temperatures"): some CPUs clear it while the temperature is too low to be measured, and the value reported in that state is more useful to userspace than an error would be.

The consequence is that userspace cannot distinguish a genuinely low temperature from one the CPU could not measure. This series exposes the indication through the standard hwmon temp%d_fault attribute, leaving temp%d_input exactly as it is.

Quoted 8 lines

One user-visible effect is worth mentioning: sensors(1) prints FAULT in place of the temperature when temp%d_fault reads 1. On a CPU that clears the valid bit at low temperature, that core stops showing a number in the default output, although sensors -u and -j still report it, as does anything that reads temp%d_input from sysfs directly. A driver-custom attribute name would avoid this, but would be invisible to generic tools. Reporting the condition through the documented attribute looks like a better option, but please say if you prefer otherwise.

Quoted 6 lines

I think it would be _much_ better to return -ENODATA for invalid readings. This isn't really a fault, after all. The sensor is not defective, it just can not provide valid data.

With -ENODATA the sensors command reports N/A for the temperature measurement, which I also think would be better than reporting FAULT.

Thank you for your feedback and for applying the other two patches!

Thank you for your feedback anf for applying the first two patches!

Userspace has seen a number in temp%d_input for over 12 years, and with this change it would get an error on CPUs that clear the valid bit. I can certainly implement returning -ENODATA; I just want to confirm you don't see this as an issue for userspace.

Best,
Ricardo

maintainer
#11 lore raw
Quoted 32 lines · Ricardo Neri
Quoted 24 lines · Guenter Roeck
Quoted 14 lines · Ricardo Neri

Hi,

Intel CPUs indicate in IA32_[PACKAGE]_THERM_STATUS whether the digital thermal readout they expose is valid. coretemp deliberately ignores that indication, for the reason given in commit bf6ea084ebb5 ("hwmon:
(coretemp) Do not return -EAGAIN for low temperatures"): some CPUs clear it while the temperature is too low to be measured, and the value reported in that state is more useful to userspace than an error would be.

The consequence is that userspace cannot distinguish a genuinely low temperature from one the CPU could not measure. This series exposes the indication through the standard hwmon temp%d_fault attribute, leaving temp%d_input exactly as it is.

Quoted 8 lines

One user-visible effect is worth mentioning: sensors(1) prints FAULT in place of the temperature when temp%d_fault reads 1. On a CPU that clears the valid bit at low temperature, that core stops showing a number in the default output, although sensors -u and -j still report it, as does anything that reads temp%d_input from sysfs directly. A driver-custom attribute name would avoid this, but would be invisible to generic tools. Reporting the condition through the documented attribute looks like a better option, but please say if you prefer otherwise.

Quoted 6 lines

I think it would be _much_ better to return -ENODATA for invalid readings. This isn't really a fault, after all. The sensor is not defective, it just can not provide valid data.

With -ENODATA the sensors command reports N/A for the temperature measurement, which I also think would be better than reporting FAULT.

Quoted 9 lines

Thank you for your feedback and for applying the other two patches!

Thank you for your feedback anf for applying the first two patches!

Userspace has seen a number in temp%d_input for over 12 years, and with this change it would get an error on CPUs that clear the valid bit. I can certainly implement returning -ENODATA; I just want to confirm you don't see this as an issue for userspace.

Userspace should be able to handle error returns. That is not an ABI change. Even if the fault attribute was implemented, trying to read the temperature should still return an error.

On the other side, claiming that the sensor is faulty is, in my opinion, just wrong. It is not faulty, it just does not return valid data.

Guenter

#12 lore raw
Quoted 43 lines · Guenter Roeck
Quoted 32 lines · Ricardo Neri
Quoted 24 lines · Guenter Roeck
Quoted 14 lines · Ricardo Neri

Hi,

Intel CPUs indicate in IA32_[PACKAGE]_THERM_STATUS whether the digital thermal readout they expose is valid. coretemp deliberately ignores that indication, for the reason given in commit bf6ea084ebb5 ("hwmon:
(coretemp) Do not return -EAGAIN for low temperatures"): some CPUs clear it while the temperature is too low to be measured, and the value reported in that state is more useful to userspace than an error would be.

The consequence is that userspace cannot distinguish a genuinely low temperature from one the CPU could not measure. This series exposes the indication through the standard hwmon temp%d_fault attribute, leaving temp%d_input exactly as it is.

Quoted 8 lines

One user-visible effect is worth mentioning: sensors(1) prints FAULT in place of the temperature when temp%d_fault reads 1. On a CPU that clears the valid bit at low temperature, that core stops showing a number in the default output, although sensors -u and -j still report it, as does anything that reads temp%d_input from sysfs directly. A driver-custom attribute name would avoid this, but would be invisible to generic tools. Reporting the condition through the documented attribute looks like a better option, but please say if you prefer otherwise.

Quoted 6 lines

I think it would be _much_ better to return -ENODATA for invalid readings. This isn't really a fault, after all. The sensor is not defective, it just can not provide valid data.

With -ENODATA the sensors command reports N/A for the temperature measurement, which I also think would be better than reporting FAULT.

Quoted 9 lines

Thank you for your feedback and for applying the other two patches!

Thank you for your feedback anf for applying the first two patches!

Userspace has seen a number in temp%d_input for over 12 years, and with this change it would get an error on CPUs that clear the valid bit. I can certainly implement returning -ENODATA; I just want to confirm you don't see this as an issue for userspace.

Quoted 3 lines

Userspace should be able to handle error returns. That is not an ABI change. Even if the fault attribute was implemented, trying to read the temperature should still return an error.

Then it seems that the fault attribute is redundant.

Quoted 3 lines

On the other side, claiming that the sensor is faulty is, in my opinion, just wrong. It is not faulty, it just does not return valid data.

I see. I will implement returning -ENODATA and drop the temp%d_fault.

Best,
Ricardo

maintainer
#13 lore raw
Quoted 44 lines · Ricardo Neri
Quoted 43 lines · Guenter Roeck
Quoted 41 lines · Ricardo Neri
Quoted 24 lines · Guenter Roeck
Quoted 14 lines · Ricardo Neri

Hi,

Intel CPUs indicate in IA32_[PACKAGE]_THERM_STATUS whether the digital thermal readout they expose is valid. coretemp deliberately ignores that indication, for the reason given in commit bf6ea084ebb5 ("hwmon:
(coretemp) Do not return -EAGAIN for low temperatures"): some CPUs clear it while the temperature is too low to be measured, and the value reported in that state is more useful to userspace than an error would be.

The consequence is that userspace cannot distinguish a genuinely low temperature from one the CPU could not measure. This series exposes the indication through the standard hwmon temp%d_fault attribute, leaving temp%d_input exactly as it is.

Quoted 8 lines

One user-visible effect is worth mentioning: sensors(1) prints FAULT in place of the temperature when temp%d_fault reads 1. On a CPU that clears the valid bit at low temperature, that core stops showing a number in the default output, although sensors -u and -j still report it, as does anything that reads temp%d_input from sysfs directly. A driver-custom attribute name would avoid this, but would be invisible to generic tools. Reporting the condition through the documented attribute looks like a better option, but please say if you prefer otherwise.

Quoted 6 lines

I think it would be _much_ better to return -ENODATA for invalid readings. This isn't really a fault, after all. The sensor is not defective, it just can not provide valid data.

With -ENODATA the sensors command reports N/A for the temperature measurement, which I also think would be better than reporting FAULT.

Thank you for your feedback and for applying the other two patches!

Thank you for your feedback anf for applying the first two patches!

Userspace has seen a number in temp%d_input for over 12 years, and with this change it would get an error on CPUs that clear the valid bit. I can certainly implement returning -ENODATA; I just want to confirm you don't see this as an issue for userspace.

Quoted 6 lines
Quoted 3 lines

Userspace should be able to handle error returns. That is not an ABI change. Even if the fault attribute was implemented, trying to read the temperature should still return an error.

Then it seems that the fault attribute is redundant.

It is not just redundant, it is wrong. unless the chip somehow reports a real fault (defect).

Quoted 6 lines
Quoted 3 lines

On the other side, claiming that the sensor is faulty is, in my opinion, just wrong. It is not faulty, it just does not return valid data.

I see. I will implement returning -ENODATA and drop the temp%d_fault.

Sounds good.

Thanks,
Guenter