Re: [PATCH 03/10] power: supply: bq25890: Fix usb-notifier probe and remove races

Marek Vasut
2022-11-27T21:36:12Z

On 11/27/22 19:02, Hans de Goede wrote:
> There are 2 races surrounding the usb-notifier:
>
> 1. The notifier, and thus usb_work, may run before the bq->charger
> power_supply class device is registered. But usb_work may call
> power_supply_changed() which relies on the psy device being registered.
>
> 2. usb_work may be pending/running at remove() time, so it needs to be
> cancelled on remove after unregistering the usb-notifier.
>
> Fix 1. by moving usb-notifier registration to after the power_supply
> registration.
>
> Fix 2. by adding a cancel_work_sync() call directly after the usb-notifier
> unregistration.
>
> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> ---
> drivers/power/supply/bq25890_charger.c | 26 +++++++++++---------------
> 1 file changed, 11 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/power/supply/bq25890_charger.c b/drivers/power/supply/bq25890_charger.c
> index 30d77afab839..032a10a3877b 100644
> --- a/drivers/power/supply/bq25890_charger.c
> +++ b/drivers/power/supply/bq25890_charger.c
> @@ -1387,16 +1387,10 @@ static int bq25890_probe(struct i2c_client *client)
> if (ret)
> return ret;
>
> - if (!IS_ERR_OR_NULL(bq->usb_phy)) {
> - INIT_WORK(&bq->usb_work, bq25890_usb_work);
> - bq->usb_nb.notifier_call = bq25890_usb_notifier;
> - usb_register_notifier(bq->usb_phy, &bq->usb_nb);
> - }
> -
> ret = bq25890_power_supply_init(bq);
> if (ret < 0) {
> dev_err(dev, "Failed to register power supply\n");
> - goto err_unregister_usb_notifier;
> + return ret;

You can even use 'return dev_err_probe()' above ^ to simplify that piece
of code now.

Reviewed-by: Marek Vasut <marex@denx.de>