power: supply: bq24190: stop IRQ and work before supply removal
PATCH 1/1 power: supply: bq24190: stop IRQ and work before supply removal
bq24190_remove() unregisters its power supplies while a managed IRQ handler and input_current_limit_work may still access them. The managed IRQ is only released after remove() returns, so stopping the supplies first leaves a window for a callback to use freed memory.
Release and synchronize the IRQ before unregistering the supplies, and cancel the delayed current-limit work. Cancel the work once more after unregistering to drain any instance requeued by an in-flight power-supply callback.
A KASAN test with a simulated I2C device and the in-tree charger driver reproduced the race during removal. The same test completed without the use-after-free after the change.
--- drivers/power/supply/bq24190_charger.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/power/supply/bq24190_charger.c +15 -0
@@ -2265,9 +2265,24 @@ static void bq24190_remove(struct i2c_client *client)dev_warn (bdi->dev,"pm_runtime_get failed: %i\n" , error);bq24190_register_reset (bdi); + +/* +* The IRQ handler and the input current limit work both access the +* power supplies. Stop them before the supplies are unregistered +* and freed below: the devm cleanup only runs after .remove() has +* returned. The second cancel catches work re-queued by an +* external_power_changed() callback which was still running while +* the supplies were unregistered (the work has a 300 ms delay). +*/ +devm_free_irq (bdi->dev, client->irq, bdi); +cancel_delayed_work_sync (&bdi->input_current_limit_work); +if (bdi->battery)power_supply_unregister (bdi->battery);power_supply_unregister (bdi->charger); + +cancel_delayed_work_sync (&bdi->input_current_limit_work); +if (error >=0 )pm_runtime_put_sync (bdi->dev);pm_runtime_dont_use_autosuspend (bdi->dev);
Signature · 1 line
2.34.1
Hi,
Quoted 10 lines · Yaohan Li
bq24190_remove()unregisters its power supplies while a managed IRQ handler andinput_current_limit_workmay still access them. The managed IRQ is only released afterremove()returns, so stopping the supplies first leaves a window for a callback to use freed memory.Release and synchronize the IRQ before unregistering the supplies, and cancel the delayed current-limit work. Cancel the work once more after unregistering to drain any instance requeued by an in-flight power-supply callback.
Quoted 8 lines
A KASAN test with a simulated I2C device and the in-tree charger driver reproduced the race during removal. The same test completed without the use-after-free after the change.
Fixes 4f73a52df7d28278 ("power: supply: bq24190: Avoid rescheduling after cancelling work")Assisted-by ZCode:GLM-4Signed-off-by Yaohan Li <[email protected]>---
The mistake is registering runtime PM and power_supply without device managed resources. Instead of manually unrolling the device amanged resources in the reomve function, fix the probe side instead resulting in a nice cleanup as a side effect.
Greetings,
-- Sebastian
Quoted 35 lines
drivers/power/supply/bq24190_charger.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)drivers/power/supply/bq24190_charger.c +15
-0@@ -2265,9 +2265,24 @@ static void bq24190_remove(struct i2c_client *client)dev_warn (bdi->dev,"pm_runtime_get failed: %i\n" , error);bq24190_register_reset (bdi); + +/* +* The IRQ handler and the input current limit work both access the +* power supplies. Stop them before the supplies are unregistered +* and freed below: the devm cleanup only runs after .remove() has +* returned. The second cancel catches work re-queued by an +* external_power_changed() callback which was still running while +* the supplies were unregistered (the work has a 300 ms delay). +*/ +devm_free_irq (bdi->dev, client->irq, bdi); +cancel_delayed_work_sync (&bdi->input_current_limit_work); +if (bdi->battery)power_supply_unregister (bdi->battery);power_supply_unregister (bdi->charger); + +cancel_delayed_work_sync (&bdi->input_current_limit_work); +if (error >=0 )pm_runtime_put_sync (bdi->dev);pm_runtime_dont_use_autosuspend (bdi->dev);Signature · 1 line
2.34.1
Hi Sebastian,
Thanks for the suggestion. I see what you mean about fixing the resource lifetime from the probe side rather than manually freeing the managed IRQ in remove.
I'll rework the power supply registration and runtime PM cleanup so the resources are released in the right order. I'll also check the delayed work path and the probe error cases before posting v2.
Thanks,
Yaohan