hwmon: (it87) Lock the device while reading the temperature sensor type
Subsystems (3)
PATCH v2 1/1 hwmon: (it87) Lock the device while reading the temperature sensor type
show_temp_type() calls it87_update_device() and then get_temp_type(). it87_update_device() drops data->update_lock before it returns. Since commit 6593eac89920 ("hwmon: (it87) Add support to detect sensor type AMDTSI"), get_temp_type() no longer uses the cached register values but reads IT87_REG_IFSEL, IT87_REG_TEMP_ENABLE and IT87_REG_TEMP_EXTRA from the chip. These reads therefore run without update_lock and with SMBus access enabled, while the comment above it87_read_value() requires both.
it87_read_value() and it87_write_value() go through one address/data port pair. If a read of tempN_type races with a sysfs store that holds the lock, for example set_temp_type(), the address write of one side can land between the address and data accesses of the other. The store can then write its value to a different register, or the read returns the wrong register.
Take the lock with it87_lock() around get_temp_type(), as set_temp_type() does. get_temp_type() uses none of the cached values, so also replace it87_update_device() with dev_get_drvdata(), again as set_temp_type() does. This avoids taking update_lock twice and an unused refresh of the cached registers.
The missing lock was found by a static check that turns the locking rule in a function's comment into a Clang context-analysis requirement on its callers.
---
Notes:
Changes in v2:
- Use dev_get_drvdata() instead of it87_update_device(). get_temp_type() reads the registers itself, so the refresh only took update_lock a second time (Sashiko review, Guenter Roeck agreed).
This was found by a compiler-based check, part of research at UCSB SecLab. The locking rule stated in the kernel-doc comment of the called function was turned into a Clang context-analysis requirement (__must_hold) on that function, and Clang 23 with CONFIG_WARN_CONTEXT_ANALYSIS then checked every caller. An LLM was used only to turn the comment into a machine-readable rule; the report itself comes from the compiler. The patch and changelog were drafted with Claude Opus 5.5. I reviewed the patch and take responsibility for it.
Testing: builds without new warnings with W=1 (x86_64, clang 23, allmodconfig). Not runtime-tested; I do not have this hardware, and I have not observed the race myself.
drivers/hwmon/it87.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/hwmon/it87.c +8 -4
@@ -1397,12 +1397,16 @@ static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr, char *buf) {struct sensor_device_attribute *sensor_attr =to_sensor_dev_attr (attr); -struct it87_data *data =it87_update_device (dev); +struct it87_data *data =dev_get_drvdata (dev); +int type, err; -if (IS_ERR (data)) -return PTR_ERR (data); + err =it87_lock (data); +if (err) +return err; + type =get_temp_type (data, sensor_attr->index); +it87_unlock (data); -return sprintf (buf,"%d\n" ,get_temp_type (data, sensor_attr->index)); +return sprintf (buf,"%d\n" , type); } static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
Signature · 1 line
2.25.1
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 21 lines
show_temp_type()callsit87_update_device()and thenget_temp_type().it87_update_device()dropsdata->update_lockbefore it returns. Since commit6593eac89920("hwmon: (it87) Add support to detect sensor type AMDTSI"),get_temp_type()no longer uses the cached register values but readsIT87_REG_IFSEL,IT87_REG_TEMP_ENABLEandIT87_REG_TEMP_EXTRAfrom the chip. These reads therefore run withoutupdate_lockand with SMBus access enabled, while the comment aboveit87_read_value()requires both.
it87_read_value()andit87_write_value()go through one address/data port pair. If a read oftempN_typeraces with a sysfs store that holds the lock, for exampleset_temp_type(), the address write of one side can land between the address and data accesses of the other. The store can then write its value to a different register, or the read returns the wrong register.Take the lock with
it87_lock()aroundget_temp_type(), asset_temp_type()does.get_temp_type()uses none of the cached values, so also replaceit87_update_device()withdev_get_drvdata(), again asset_temp_type()does. This avoids takingupdate_locktwice and an unused refresh of the cached registers.
Quoted 7 lines
The missing lock was found by a static check that turns the locking rule in a function's comment into a Clang context-analysis requirement on its callers.
Fixes 6593eac89920 ("hwmon: (it87) Add support to detect sensor type AMDTSI")Signed-off-by Praneeth Balasubramanian <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1