FAILED: Patch "hwmon: (max16065) Use READ/WRITE_ONCE to avoid compiler optimization induced race" failed to apply to 5.10-stable tree
Subsystems (2)
Sasha Levin
maintainerThe patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From 007be4327e443d79c9dd9e56dc16c36f6395d208 Mon Sep 17 00:00:00 2001
From: Gui-Dong Han <[email protected]>
Date: Tue, 3 Feb 2026 20:14:43 +0800
Subject: [PATCH] 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.
--- drivers/hwmon/max16065.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)
drivers/hwmon/max16065.c +13 -13
@@ -151,27 +151,27 @@ static struct max16065_data *max16065_update_device(struct device *dev)int i;for (i =0 ; i < data->num_adc; i++) - data->adc[i] - =max16065_read_adc (client,MAX16065_ADC (i)); +WRITE_ONCE (data->adc[i], +max16065_read_adc (client,MAX16065_ADC (i)));if (data->have_current) { - data->adc[MAX16065_NUM_ADC ] - =max16065_read_adc (client,MAX16065_CSP_ADC ); - data->curr_sense - =i2c_smbus_read_byte_data (client, -MAX16065_CURR_SENSE ); +WRITE_ONCE (data->adc[MAX16065_NUM_ADC ], +max16065_read_adc (client,MAX16065_CSP_ADC )); +WRITE_ONCE (data->curr_sense, +i2c_smbus_read_byte_data (client,MAX16065_CURR_SENSE )); }for (i =0 ; i <2 ; i++) - data->fault[i] - =i2c_smbus_read_byte_data (client,MAX16065_FAULT (i)); +WRITE_ONCE (data->fault[i], +i2c_smbus_read_byte_data (client,MAX16065_FAULT (i)));/* * MAX16067 and MAX16068 have separate undervoltage and * overvoltage alarm bits. Squash them together. */ if (data->chip == max16067 || data->chip == max16068) - data->fault[0 ] |= data->fault[1 ]; +WRITE_ONCE (data->fault[0 ], + data->fault[0 ] | data->fault[1 ]); data->last_updated = jiffies; data->valid = true;
@@ -185,7 +185,7 @@ static ssize_t max16065_alarm_show(struct device *dev, {struct sensor_device_attribute_2 *attr2 =to_sensor_dev_attr_2 (da);struct max16065_data *data =max16065_update_device (dev); -int val = data->fault[attr2->nr]; +int val =READ_ONCE (data->fault[attr2->nr]);if (val <0 )return val;
@@ -203,7 +203,7 @@ static ssize_t max16065_input_show(struct device *dev, {struct sensor_device_attribute *attr =to_sensor_dev_attr (da);struct max16065_data *data =max16065_update_device (dev); -int adc = data->adc[attr->index]; +int adc =READ_ONCE (data->adc[attr->index]);if (unlikely (adc <0 ))return adc;
@@ -216,7 +216,7 @@ static ssize_t max16065_current_show(struct device *dev, struct device_attribute *da, char *buf) { struct max16065_data *data = max16065_update_device(dev); -int curr_sense = data->curr_sense; +int curr_sense =READ_ONCE (data->curr_sense);if (unlikely (curr_sense <0 ))return curr_sense;
Signature · 1 line
2.51.0