From: xiaopeitux@foxmail.com
To: cryolitia@uniontech.com, linux@roeck-us.net,
linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Pei Xiao <xiaopei01@kylinos.cn>
Subject: [PATCH 4/4] hwmon: (gpd-fan): fix race condition between device removal and sysfs access
Date: Wed, 27 May 2026 18:10:21 +0800 [thread overview]
Message-ID: <tencent_395A116FDCB95475901B5E5C27237380AE08@qq.com> (raw)
In-Reply-To: <cover.1779876412.git.xiaopei01@kylinos.cn>
From: Pei Xiao <xiaopei01@kylinos.cn>
Replace the manual gpd_fan_remove() callback with a devres-managed
action using devm_add_action_or_reset(). The original remove hook
resets the fan to AUTOMATIC mode, but the hwmon sysfs interface
(registered with devm_hwmon_device_register_with_info()) remains
active until after the remove callback completes. This creates a
race window where a concurrent userspace sysfs access can interleave
with the EC I/O sequence, potentially corrupting EC registers.
Using devm_add_action_or_reset() registers the reset function as a
devres action. Due to the LIFO release order of devres, the hwmon
device is unregistered (sysfs removed) before the reset action
executes, eliminating the race condition.
Fixes: 0ab88e239439 ("hwmon: add GPD devices sensor driver")
Signed-off-by: Pei Xiao <xiaopei01@kylinos.cn>
---
drivers/hwmon/gpd-fan.c | 27 ++++++++++++++++-----------
1 file changed, 16 insertions(+), 11 deletions(-)
diff --git a/drivers/hwmon/gpd-fan.c b/drivers/hwmon/gpd-fan.c
index 1b57a5add934..d1993cd645cb 100644
--- a/drivers/hwmon/gpd-fan.c
+++ b/drivers/hwmon/gpd-fan.c
@@ -609,6 +609,16 @@ static void gpd_init_ec(struct gpd_fan_data *data)
gpd_win4_init_ec(data);
}
+static void gpd_fan_reset_hardware(void *pdata)
+{
+ struct gpd_fan_data *data = pdata;
+
+ if (data) {
+ data->pwm_enable = AUTOMATIC;
+ gpd_set_pwm_enable(data, AUTOMATIC);
+ }
+}
+
static int gpd_fan_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
@@ -617,6 +627,7 @@ static int gpd_fan_probe(struct platform_device *pdev)
struct device *hwdev;
struct gpd_fan_data *data;
const struct gpd_fan_drvdata *match;
+ int ret;
res = platform_get_resource(pdev, IORESOURCE_IO, 0);
if (!res)
@@ -644,6 +655,11 @@ static int gpd_fan_probe(struct platform_device *pdev)
dev_set_drvdata(dev, data);
gpd_init_ec(data);
+
+ ret = devm_add_action_or_reset(dev, gpd_fan_reset_hardware, data);
+ if (ret)
+ return ret;
+
hwdev = devm_hwmon_device_register_with_info(dev,
DRIVER_NAME,
data,
@@ -655,19 +671,8 @@ static int gpd_fan_probe(struct platform_device *pdev)
return 0;
}
-static void gpd_fan_remove(struct platform_device *pdev)
-{
- struct gpd_fan_data *data = dev_get_drvdata(&pdev->dev);
-
- if (data) {
- data->pwm_enable = AUTOMATIC;
- gpd_set_pwm_enable(data, AUTOMATIC);
- }
-}
-
static struct platform_driver gpd_fan_driver = {
.probe = gpd_fan_probe,
- .remove = gpd_fan_remove,
.driver = {
.name = KBUILD_MODNAME,
},
--
2.25.1
prev parent reply other threads:[~2026-05-27 10:10 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <cover.1779876412.git.xiaopei01@kylinos.cn>
2026-05-27 10:10 ` [PATCH v2 1/4] hwmon: (gpd-fan): drop global driver data and use per-device allocation xiaopeitux
2026-05-27 10:10 ` [PATCH 2/4] hwmon: (gpd-fan): Initialize EC before registering hwmon device xiaopeitux
2026-05-27 10:10 ` [PATCH v2 3/4] hwmon: (gpd-fan): upgrade log level from warn to err for platform device creation failure xiaopeitux
2026-05-27 10:10 ` xiaopeitux [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tencent_395A116FDCB95475901B5E5C27237380AE08@qq.com \
--to=xiaopeitux@foxmail.com \
--cc=cryolitia@uniontech.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=xiaopei01@kylinos.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®