mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] power: supply: bq25890: cancel pump work before supply removal
@ 2026-11-04  9:47 Yaohan Li
  2026-11-06  0:37 ` Sebastian Reichel
  0 siblings, 1 reply; 2+ messages in thread
From: Yaohan Li @ 2026-11-04  9:47 UTC (permalink / raw)
  To: sre; +Cc: hansg, marex, linux-pm, linux-kernel

The existing devm cleanup for pump_express_work runs after the
managed power supply has been unregistered. A delayed or running pump
work item can therefore call power_supply_changed() with a freed
power_supply pointer.

Register an additional devm cleanup action after power-supply
registration, so devres reverse ordering drains the work while the
supply is still alive. Keep the original late cleanup as a backstop
for work that might be queued again during supplier teardown.

A KASAN test using a simulated I2C device and the real bq25890
driver reproduced the use-after-free before the change and not under
the corresponding teardown sequence afterward.

Fixes: a7aaa80098d5b7608 ("power: supply: bq25890: Ensure pump_express_work is cancelled on remove")
Assisted-by: ZCode:GLM-4
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
 drivers/power/supply/bq25890_charger.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/drivers/power/supply/bq25890_charger.c b/drivers/power/supply/bq25890_charger.c
index f7510ada6b734..a3e35e036b97b 100644
--- a/drivers/power/supply/bq25890_charger.c
+++ b/drivers/power/supply/bq25890_charger.c
@@ -1459,6 +1459,23 @@ static void bq25890_non_devm_cleanup(void *data)
 	}
 }
 
+static void bq25890_cancel_pump_express(void *data)
+{
+	struct bq25890_device *bq = data;
+
+	/*
+	 * Cancel the delayed work while the power supply is still alive:
+	 * bq25890_pump_express_work() calls power_supply_changed() at its
+	 * end and must not run against an already released supply.
+	 *
+	 * bq25890_non_devm_cleanup() stays in place as well: it runs
+	 * after the devres-managed power supply has been unregistered,
+	 * catching work queued again from the supplier-change path
+	 * between this cancel and the unregister.
+	 */
+	cancel_delayed_work_sync(&bq->pump_express_work);
+}
+
 static int bq25890_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
@@ -1531,6 +1548,10 @@ static int bq25890_probe(struct i2c_client *client)
 	if (ret < 0)
 		return dev_err_probe(dev, ret, "registering power supply\n");
 
+	ret = devm_add_action_or_reset(dev, bq25890_cancel_pump_express, bq);
+	if (ret)
+		return ret;
+
 	ret = devm_request_threaded_irq(dev, client->irq, NULL,
 					bq25890_irq_handler_thread,
 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
-- 
2.34.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] power: supply: bq25890: cancel pump work before supply removal
  2026-11-04  9:47 [PATCH] power: supply: bq25890: cancel pump work before supply removal Yaohan Li
@ 2026-11-06  0:37 ` Sebastian Reichel
  0 siblings, 0 replies; 2+ messages in thread
From: Sebastian Reichel @ 2026-11-06  0:37 UTC (permalink / raw)
  To: Yaohan Li; +Cc: hansg, marex, linux-pm, linux-kernel

Hi,

On Wed, Nov 04, 2026 at 05:47:31PM +0800, Yaohan Li wrote:
> The existing devm cleanup for pump_express_work runs after the
> managed power supply has been unregistered. A delayed or running pump
> work item can therefore call power_supply_changed() with a freed
> power_supply pointer.
> 
> Register an additional devm cleanup action after power-supply
> registration, so devres reverse ordering drains the work while the
> supply is still alive. Keep the original late cleanup as a backstop
> for work that might be queued again during supplier teardown.
> 
> A KASAN test using a simulated I2C device and the real bq25890
> driver reproduced the use-after-free before the change and not under
> the corresponding teardown sequence afterward.
> 
> Fixes: a7aaa80098d5b7608 ("power: supply: bq25890: Ensure pump_express_work is cancelled on remove")
> Assisted-by: ZCode:GLM-4
> Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
> ---
>  drivers/power/supply/bq25890_charger.c | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
> 
> diff --git a/drivers/power/supply/bq25890_charger.c b/drivers/power/supply/bq25890_charger.c
> index f7510ada6b734..a3e35e036b97b 100644
> --- a/drivers/power/supply/bq25890_charger.c
> +++ b/drivers/power/supply/bq25890_charger.c
> @@ -1459,6 +1459,23 @@ static void bq25890_non_devm_cleanup(void *data)
>  	}
>  }
>  
> +static void bq25890_cancel_pump_express(void *data)
> +{
> +	struct bq25890_device *bq = data;
> +
> +	/*
> +	 * Cancel the delayed work while the power supply is still alive:
> +	 * bq25890_pump_express_work() calls power_supply_changed() at its
> +	 * end and must not run against an already released supply.
> +	 *
> +	 * bq25890_non_devm_cleanup() stays in place as well: it runs
> +	 * after the devres-managed power supply has been unregistered,
> +	 * catching work queued again from the supplier-change path
> +	 * between this cancel and the unregister.
> +	 */
> +	cancel_delayed_work_sync(&bq->pump_express_work);
> +}
> +
>  static int bq25890_probe(struct i2c_client *client)
>  {
>  	struct device *dev = &client->dev;
> @@ -1531,6 +1548,10 @@ static int bq25890_probe(struct i2c_client *client)
>  	if (ret < 0)
>  		return dev_err_probe(dev, ret, "registering power supply\n");
>  
> +	ret = devm_add_action_or_reset(dev, bq25890_cancel_pump_express, bq);
> +	if (ret)
> +		return ret;

Use devm_delayed_work_autocancel() (and drop the INIT_DELAYED_WORK).

-- Sebastian

> +
>  	ret = devm_request_threaded_irq(dev, client->irq, NULL,
>  					bq25890_irq_handler_thread,
>  					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
> -- 
> 2.34.1
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-06  0:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-04  9:47 [PATCH] power: supply: bq25890: cancel pump work before supply removal Yaohan Li
2026-11-06  0:37 ` Sebastian Reichel

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®