[PATCH 3/3] power: supply: mt6360_charger: drain chrdet_work before the power supply is freed
From: hyunjungg
Date: Fri Nov 06 2026 - 11:06:07 EST
From: Hyunjung Ko <hj351016@xxxxxxxxx>
mt6360_charger_probe() registers the chrdet_work cancellation via
devm_work_autocancel() *before* devm_power_supply_register(), so devres
(which unwinds in reverse) frees the power supply device first and only
then cancels the work:
devm_work_autocancel(..., &mci->chrdet_work, ...); /* registered 1st */
...
mci->psy = devm_power_supply_register(...); /* registered 2nd */
...
schedule_work(&mci->chrdet_work);
The work handler mt6360_chrdet_work() runs mt6360_handle_chrdet_ext_evt(),
which calls power_supply_changed(mci->psy). chrdet_work is scheduled at
the end of probe, so on an unbind that races that work, devres tears down
in reverse order: devm_power_supply_register()'s release frees the power
supply device before the devm_work_autocancel action drains chrdet_work,
and the work then dereferences the freed mci->psy -> use-after-free.
Register the work cancellation after devm_power_supply_register() (and
after the other devm resources), so reverse-order devres cleanup drains
chrdet_work while mci->psy is still valid. chrdet_work is only scheduled
later in probe, so moving the devm_work_autocancel() (which also
initialises the work) down is safe.
Found by code review; not runtime-reproduced (requires MT6360 PMIC
hardware and a charger-detect event racing device unbind).
[USER: add a Fixes: tag. SHA not verifiable in this graft-at-7.3 worktree.
Blame the devm_work_autocancel() introduction in mt6360_charger.c on a
full-history tree and confirm the 12-char SHA + subject before send.]
Fixes: 0402e8ebb8b8 ("power: supply: mt6360_charger: add MT6360 charger support")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hyunjung Ko <hj351016@xxxxxxxxx>
---
drivers/power/supply/mt6360_charger.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/drivers/power/supply/mt6360_charger.c b/drivers/power/supply/mt6360_charger.c
index b4d4623..e382db4 100644
--- a/drivers/power/supply/mt6360_charger.c
+++ b/drivers/power/supply/mt6360_charger.c
@@ -790,9 +790,6 @@ static int mt6360_charger_probe(struct platform_device *pdev)
mci->vinovp = 6500000;
mutex_init(&mci->chgdet_lock);
platform_set_drvdata(pdev, mci);
- ret = devm_work_autocancel(&pdev->dev, &mci->chrdet_work, mt6360_chrdet_work);
- if (ret)
- return dev_err_probe(&pdev->dev, ret, "Failed to set delayed work\n");
ret = device_property_read_u32(&pdev->dev, "richtek,vinovp-microvolt", &mci->vinovp);
if (ret)
@@ -828,6 +825,16 @@ static int mt6360_charger_probe(struct platform_device *pdev)
if (IS_ERR(mci->otg_rdev))
return PTR_ERR(mci->otg_rdev);
+ /*
+ * Register the work cancellation only after the power supply is
+ * registered. devres unwinds in reverse, so this guarantees the
+ * work (which calls power_supply_changed(mci->psy)) is drained
+ * before the power supply device is freed on unbind.
+ */
+ ret = devm_work_autocancel(&pdev->dev, &mci->chrdet_work, mt6360_chrdet_work);
+ if (ret)
+ return dev_err_probe(&pdev->dev, ret, "Failed to set delayed work\n");
+
schedule_work(&mci->chrdet_work);
return 0;
--
2.43.0