FAILED: patch "[PATCH] hwmon: (gpio-fan) Fix use-after-free in alarm work" failed to apply to 5.10-stable tree
Subsystems (2)
[email protected]
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]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.10.y
git checkout FETCH_HEAD
git cherry-pick -x a2471ed17b0e6ff7bfb6b2ea8e6e5b04c309d293
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026091651-verdict-tactless-1748@gregkh' --subject-prefix 'PATCH 5.10.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From a2471ed17b0e6ff7bfb6b2ea8e6e5b04c309d293 Mon Sep 17 00:00:00 2001
From: Fan Wu <[email protected]>
Date: Wed, 19 Aug 2026 03:33:17 +0000
Subject: [PATCH] hwmon: (gpio-fan) Fix use-after-free in alarm work
fan_alarm_irq_handler() queues fan_data->alarm_work, but nothing cancels it. fan_alarm_notify() dereferences fan_data and its hwmon device. On unbind, devres frees the interrupt, which only waits for the handler itself, and then releases the hwmon device and fan_data, so a pending fan_alarm_notify() can run after those frees.
Replace INIT_WORK() with devm_work_autocancel(), registered before devm_request_irq(). The devres cleanup then frees the interrupt first, so no new work can be queued, and cancels the work while fan_data and the hwmon device are still alive.
This issue was found by an in-house static analysis tool.
drivers/hwmon/gpio-fan.c +7 -1
@@ -12,6 +12,7 @@#include <linux/slab.h> #include <linux/interrupt.h> #include <linux/irq.h> +#include <linux/devm-helpers.h> #include <linux/platform_device.h> #include <linux/err.h> #include <linux/kstrtox.h>
@@ -84,6 +85,7 @@ static DEVICE_ATTR_RO(fan1_alarm);static int fan_alarm_init (struct gpio_fan_data *fan_data) {int alarm_irq; +int err;struct device *dev = fan_data->dev;/*
@@ -94,7 +96,11 @@ static int fan_alarm_init(struct gpio_fan_data *fan_data)if (alarm_irq <=0 )return 0 ; -INIT_WORK (&fan_data->alarm_work, fan_alarm_notify); + err =devm_work_autocancel (dev, &fan_data->alarm_work, + fan_alarm_notify); +if (err) +return err; +irq_set_irq_type (alarm_irq,IRQ_TYPE_EDGE_BOTH );return devm_request_irq (dev, alarm_irq, fan_alarm_irq_handler,IRQF_SHARED ,"GPIO fan alarm" , fan_data);
Sasha Levin
maintainerPATCH 1/3 workqueue: Add resource managed version of delayed work init
From: Matti Vaittinen <[email protected]>
[ Upstream commit 0341ce5443949588e93581b49b934cdde2befbf8 ]
A few drivers which need a delayed work-queue must cancel work at driver detach. Some of those implement remove() solely for this purpose. Help drivers to avoid unnecessary remove and error-branch implementation by adding managed verision of delayed work initialization. This will also help drivers to avoid mixing manual and devm based unwinding when other resources are handled by devm.
Stable-dep-of: a2471ed17b0e ("hwmon: (gpio-fan) Fix use-after-free in alarm work")
--- include/linux/devm-helpers.h | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 include/linux/devm-helpers.h
include/linux/devm-helpers.h new file +53 -0
@@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __LINUX_DEVM_HELPERS_H +#define __LINUX_DEVM_HELPERS_H + +/* +* Functions which do automatically cancel operations or release resources upon +* driver detach. +* +* These should be helpful to avoid mixing the manual and devm-based resource +* management which can be source of annoying, rarely occurring, +* hard-to-reproduce bugs. +* +* Please take into account that devm based cancellation may be performed some +* time after the remove() is ran. +* +* Thus mixing devm and manual resource management can easily cause problems +* when unwinding operations with dependencies. IRQ scheduling a work in a queue +* is typical example where IRQs are often devm-managed and WQs are manually +* cleaned at remove(). If IRQs are not manually freed at remove() (and this is +* often the case when we use devm for IRQs) we have a period of time after +* remove() - and before devm managed IRQs are freed - where new IRQ may fire +* and schedule a work item which won't be cancelled because remove() was +* already ran. +*/ + +#include <linux/device.h> +#include <linux/workqueue.h> + +static inline void devm_delayed_work_drop (void *res) +{ +cancel_delayed_work_sync (res); +} + +/** +* devm_delayed_work_autocancel - Resource-managed work allocation +* @dev: Device which lifetime work is bound to +* @pdata: work to be cancelled when driver is detached +* +* Initialize work which is automatically cancelled when driver is detached. +* A few drivers need delayed work which must be cancelled before driver +* is detached to avoid accessing removed resources. +* devm_delayed_work_autocancel() can be used to omit the explicit +* cancelleation when driver is detached. +*/ +static inline int devm_delayed_work_autocancel (struct device *dev, +struct delayed_work *w, +work_func_t worker) +{ +INIT_DELAYED_WORK (w, worker); +return devm_add_action (dev, devm_delayed_work_drop, w); +} + +#endif
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/3 devm-helpers: Add resource managed version of work init
From: Matti Vaittinen <[email protected]>
[ Upstream commit 7a2c4cc537fa9f05fe90812e7d789b9faf7eb869 ]
A few drivers which need a work-queue must cancel work at driver detach. Some of those implement remove() solely for this purpose. Help drivers to avoid unnecessary remove and error-branch implementation by adding managed verision of work initialization. This will also help drivers to avoid mixing manual and devm based unwinding when other resources are handled by devm.
Stable-dep-of: a2471ed17b0e ("hwmon: (gpio-fan) Fix use-after-free in alarm work")
--- include/linux/devm-helpers.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
include/linux/devm-helpers.h +25 -0
@@ -50,4 +50,29 @@ static inline int devm_delayed_work_autocancel(struct device *dev, return devm_add_action(dev, devm_delayed_work_drop, w); } +static inline void devm_work_drop (void *res) +{ +cancel_work_sync (res); +} + +/** +* devm_work_autocancel - Resource-managed work allocation +* @dev: Device which lifetime work is bound to +* @w: Work to be added (and automatically cancelled) +* @worker: Worker function +* +* Initialize work which is automatically cancelled when driver is detached. +* A few drivers need to queue work which must be cancelled before driver +* is detached to avoid accessing removed resources. +* devm_work_autocancel() can be used to omit the explicit +* cancelleation when driver is detached. +*/ +static inline int devm_work_autocancel (struct device *dev, +struct work_struct *w, +work_func_t worker) +{ +INIT_WORK (w, worker); +return devm_add_action (dev, devm_work_drop, w); +} +#endif
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/3 hwmon: (gpio-fan) Fix use-after-free in alarm work
In reply to Sasha LevinFrom: Fan Wu <[email protected]>
[ Upstream commit a2471ed17b0e6ff7bfb6b2ea8e6e5b04c309d293 ]
fan_alarm_irq_handler() queues fan_data->alarm_work, but nothing cancels it. fan_alarm_notify() dereferences fan_data and its hwmon device. On unbind, devres frees the interrupt, which only waits for the handler itself, and then releases the hwmon device and fan_data, so a pending fan_alarm_notify() can run after those frees.
Replace INIT_WORK() with devm_work_autocancel(), registered before devm_request_irq(). The devres cleanup then frees the interrupt first, so no new work can be queued, and cancels the work while fan_data and the hwmon device are still alive.
This issue was found by an in-house static analysis tool.
--- drivers/hwmon/gpio-fan.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/hwmon/gpio-fan.c +7 -1
@@ -12,6 +12,7 @@#include <linux/slab.h> #include <linux/interrupt.h> #include <linux/irq.h> +#include <linux/devm-helpers.h> #include <linux/platform_device.h> #include <linux/err.h> #include <linux/mutex.h>
@@ -81,6 +82,7 @@ static DEVICE_ATTR_RO(fan1_alarm);static int fan_alarm_init (struct gpio_fan_data *fan_data) {int alarm_irq; +int err;struct device *dev = fan_data->dev;/*
@@ -91,7 +93,11 @@ static int fan_alarm_init(struct gpio_fan_data *fan_data)if (alarm_irq <=0 )return 0 ; -INIT_WORK (&fan_data->alarm_work, fan_alarm_notify); + err =devm_work_autocancel (dev, &fan_data->alarm_work, + fan_alarm_notify); +if (err) +return err; +irq_set_irq_type (alarm_irq,IRQ_TYPE_EDGE_BOTH );return devm_request_irq (dev, alarm_irq, fan_alarm_irq_handler,IRQF_SHARED ,"GPIO fan alarm" , fan_data);
Signature · 1 line
2.53.0