Skip to content

Commit 007be43

Browse files
2045castorgroeck
authored andcommitted
hwmon: (max16065) Use READ/WRITE_ONCE to avoid compiler optimization induced race
Simply copying shared data to a local variable cannot prevent data races. The compiler is allowed to optimize away the local copy and re-read the shared memory, causing a Time-of-Check Time-of-Use (TOCTOU) issue if the data changes between the check and the usage. To enforce the use of the local variable, use READ_ONCE() when reading the shared data and WRITE_ONCE() when updating it. Apply these macros to the three identified locations (curr_sense, adc, and fault) where local variables are used for error validation, ensuring the value remains consistent. Reported-by: Ben Hutchings <ben@decadent.org.uk> Closes: https://lore.kernel.org/all/6fe17868327207e8b850cf9f88b7dc58b2021f73.camel@decadent.org.uk/ Fixes: f5bae26 ("hwmon: Driver for MAX16065 System Manager and compatibles") Fixes: b8d5acd ("hwmon: (max16065) Use local variable to avoid TOCTOU") Cc: stable@vger.kernel.org Signed-off-by: Gui-Dong Han <hanguidong02@gmail.com> Link: https://lore.kernel.org/r/20260203121443.5482-1-hanguidong02@gmail.com Signed-off-by: Guenter Roeck <linux@roeck-us.net>
1 parent ddb2325 commit 007be43

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

drivers/hwmon/max16065.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,27 +151,27 @@ static struct max16065_data *max16065_update_device(struct device *dev)
151151
int i;
152152

153153
for (i = 0; i < data->num_adc; i++)
154-
data->adc[i]
155-
= max16065_read_adc(client, MAX16065_ADC(i));
154+
WRITE_ONCE(data->adc[i],
155+
max16065_read_adc(client, MAX16065_ADC(i)));
156156

157157
if (data->have_current) {
158-
data->adc[MAX16065_NUM_ADC]
159-
= max16065_read_adc(client, MAX16065_CSP_ADC);
160-
data->curr_sense
161-
= i2c_smbus_read_byte_data(client,
162-
MAX16065_CURR_SENSE);
158+
WRITE_ONCE(data->adc[MAX16065_NUM_ADC],
159+
max16065_read_adc(client, MAX16065_CSP_ADC));
160+
WRITE_ONCE(data->curr_sense,
161+
i2c_smbus_read_byte_data(client, MAX16065_CURR_SENSE));
163162
}
164163

165164
for (i = 0; i < 2; i++)
166-
data->fault[i]
167-
= i2c_smbus_read_byte_data(client, MAX16065_FAULT(i));
165+
WRITE_ONCE(data->fault[i],
166+
i2c_smbus_read_byte_data(client, MAX16065_FAULT(i)));
168167

169168
/*
170169
* MAX16067 and MAX16068 have separate undervoltage and
171170
* overvoltage alarm bits. Squash them together.
172171
*/
173172
if (data->chip == max16067 || data->chip == max16068)
174-
data->fault[0] |= data->fault[1];
173+
WRITE_ONCE(data->fault[0],
174+
data->fault[0] | data->fault[1]);
175175

176176
data->last_updated = jiffies;
177177
data->valid = true;
@@ -185,7 +185,7 @@ static ssize_t max16065_alarm_show(struct device *dev,
185185
{
186186
struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da);
187187
struct max16065_data *data = max16065_update_device(dev);
188-
int val = data->fault[attr2->nr];
188+
int val = READ_ONCE(data->fault[attr2->nr]);
189189

190190
if (val < 0)
191191
return val;
@@ -203,7 +203,7 @@ static ssize_t max16065_input_show(struct device *dev,
203203
{
204204
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
205205
struct max16065_data *data = max16065_update_device(dev);
206-
int adc = data->adc[attr->index];
206+
int adc = READ_ONCE(data->adc[attr->index]);
207207

208208
if (unlikely(adc < 0))
209209
return adc;
@@ -216,7 +216,7 @@ static ssize_t max16065_current_show(struct device *dev,
216216
struct device_attribute *da, char *buf)
217217
{
218218
struct max16065_data *data = max16065_update_device(dev);
219-
int curr_sense = data->curr_sense;
219+
int curr_sense = READ_ONCE(data->curr_sense);
220220

221221
if (unlikely(curr_sense < 0))
222222
return curr_sense;

0 commit comments

Comments
 (0)