[PATCH 1/3] power: supply: ltc2941: drain poll work before the power supply is unregistered
From: hyunjungg
Date: Fri Nov 06 2026 - 11:05:07 EST
From: Hyunjung Ko <hj351016@xxxxxxxxx>
ltc294x_i2c_probe() registers the self-rearming poll work before the
power supply, so device-managed teardown drains it too late:
devm_delayed_work_autocancel(&client->dev, &info->work, ltc294x_work);
...
info->supply = devm_power_supply_register(&client->dev, ...);
...
schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
devres unwinds in reverse registration order, so on unbind/unload the
power supply is torn down first and the work is cancelled last:
power_supply_unregister(info->supply); /* frees info->supply */
cancel_delayed_work_sync(&info->work); /* too late */
power_supply_unregister() ends in device_unregister(&psy->dev), whose
release (power_supply_dev_release()) kfree()s the power_supply once the
last reference drops. The driver holds info->supply as a bare pointer
and takes no extra reference, so it is left dangling.
ltc294x_work() re-queues itself unconditionally every LTC294X_WORK_DELAY
seconds, so a poll instance is essentially always pending or running at
unbind. Its ltc294x_update() calls power_supply_changed(info->supply),
which unconditionally dereferences the supply (spin_lock on
psy->changed_lock, pm_stay_awake(&psy->dev), schedule_work on
psy->changed_work) with no use_cnt/removing guard on that path. If the
poll instance runs in the window after power_supply_unregister() has
freed the supply but before cancel_delayed_work_sync() has drained it,
it dereferences freed memory -> use-after-free.
Register the work cancellation after devm_power_supply_register(), so
devres (LIFO) cancels the work first -- while info->supply is still
valid -- and only then unregisters the power supply. Nothing queues the
work before schedule_delayed_work() below (external_power_changed is
NULL and ltc294x_reset() does not touch the work), so moving the
registration is safe; devm_delayed_work_autocancel() still performs the
INIT_DELAYED_WORK() before the work is first scheduled.
Found by static analysis / code review; compile-tested only, not
reproduced on hardware (requires an LTC2941/LTC2943 gas-gauge chip).
[USER: add a Fixes: tag. SHA not verifiable in this graft-at-7.3
worktree. The bug entered when ltc294x_i2c_probe() was converted
to devm_delayed_work_autocancel() ahead of
devm_power_supply_register()
(drivers/power/supply/ltc2941-battery-gauge.c). On a full-history
tree, blame those two devm calls; confirm the 12-char SHA +
subject of the devm-autocancel conversion before send.]
Fixes: 6d0c5de2fd84 ("power: supply: Clean-up few drivers by using managed work init")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hyunjung Ko <hj351016@xxxxxxxxx>
---
drivers/power/supply/ltc2941-battery-gauge.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/drivers/power/supply/ltc2941-battery-gauge.c b/drivers/power/supply/ltc2941-battery-gauge.c
index 6ebb555..8f8a8f6 100644
--- a/drivers/power/supply/ltc2941-battery-gauge.c
+++ b/drivers/power/supply/ltc2941-battery-gauge.c
@@ -531,11 +531,6 @@ static int ltc294x_i2c_probe(struct i2c_client *client)
psy_cfg.drv_data = info;
- ret = devm_delayed_work_autocancel(&client->dev, &info->work,
- ltc294x_work);
- if (ret)
- return ret;
-
ret = ltc294x_reset(info, prescaler_exp);
if (ret < 0)
return dev_err_probe(&client->dev, ret,
@@ -547,6 +542,11 @@ static int ltc294x_i2c_probe(struct i2c_client *client)
return dev_err_probe(&client->dev, PTR_ERR(info->supply),
"failed to register ltc2941\n");
+ ret = devm_delayed_work_autocancel(&client->dev, &info->work,
+ ltc294x_work);
+ if (ret)
+ return ret;
+
schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
return 0;
--
2.43.0