Skip to content

Commit d01363d

Browse files
jwrdegoedesre
authored andcommitted
power: supply: bq25890: Fix race causing oops at boot
Before this commit the driver was registering its interrupt handler before it registered the power_supply, causing bq->charger to potentially be NULL when the interrupt handler runs, triggering a NULL pointer exception in the interrupt handler: [ 21.213531] BUG: kernel NULL pointer dereference, address: 0000000000000680 ... [ 21.213573] Hardware name: Xiaomi Inc Mipad2/Mipad, BIOS MIPad-P4.X64.0043.R03.1603071414 03/07/2016 [ 21.213576] RIP: 0010:__lock_acquire+0x5c5/0x1de0 ... [ 21.213629] Call Trace: [ 21.213636] ? disable_irq_nosync+0x10/0x10 [ 21.213644] ? __mutex_unlock_slowpath+0x35/0x260 [ 21.213655] lock_acquire+0xb5/0x2b0 [ 21.213661] ? power_supply_changed+0x23/0x90 [ 21.213670] ? disable_irq_nosync+0x10/0x10 [ 21.213676] _raw_spin_lock_irqsave+0x48/0x60 [ 21.213682] ? power_supply_changed+0x23/0x90 [ 21.213687] power_supply_changed+0x23/0x90 [ 21.213697] __bq25890_handle_irq+0x5e/0xe0 [bq25890_charger] [ 21.213709] bq25890_irq_handler_thread+0x26/0x40 [bq25890_charger] [ 21.213718] irq_thread_fn+0x20/0x60 ... Fix this by moving the power_supply_register() call to above the request_threaded_irq() call. Note this fix includes making the following 2 (necessary) changes: 1. Switch to the devm version of power_supply_register() to avoid the need to make the error-handling in probe() more complicated. 2. Rename the "irq_fail" label to "err_unregister_usb_notifier" since the old name no longer makes sense after this fix. Signed-off-by: Hans de Goede <hdegoede@redhat.com> Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
1 parent cdf10ff commit d01363d

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

drivers/power/supply/bq25890_charger.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,9 @@ static int bq25890_power_supply_init(struct bq25890_device *bq)
734734
psy_cfg.supplied_to = bq25890_charger_supplied_to;
735735
psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
736736

737-
bq->charger = power_supply_register(bq->dev, &bq25890_power_supply_desc,
738-
&psy_cfg);
737+
bq->charger = devm_power_supply_register(bq->dev,
738+
&bq25890_power_supply_desc,
739+
&psy_cfg);
739740

740741
return PTR_ERR_OR_ZERO(bq->charger);
741742
}
@@ -983,22 +984,22 @@ static int bq25890_probe(struct i2c_client *client,
983984
usb_register_notifier(bq->usb_phy, &bq->usb_nb);
984985
}
985986

987+
ret = bq25890_power_supply_init(bq);
988+
if (ret < 0) {
989+
dev_err(dev, "Failed to register power supply\n");
990+
goto err_unregister_usb_notifier;
991+
}
992+
986993
ret = devm_request_threaded_irq(dev, client->irq, NULL,
987994
bq25890_irq_handler_thread,
988995
IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
989996
BQ25890_IRQ_PIN, bq);
990997
if (ret)
991-
goto irq_fail;
992-
993-
ret = bq25890_power_supply_init(bq);
994-
if (ret < 0) {
995-
dev_err_probe(dev, ret, "Failed to register power supply.\n");
996-
goto irq_fail;
997-
}
998+
goto err_unregister_usb_notifier;
998999

9991000
return 0;
10001001

1001-
irq_fail:
1002+
err_unregister_usb_notifier:
10021003
if (!IS_ERR_OR_NULL(bq->usb_phy))
10031004
usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
10041005

@@ -1009,8 +1010,6 @@ static int bq25890_remove(struct i2c_client *client)
10091010
{
10101011
struct bq25890_device *bq = i2c_get_clientdata(client);
10111012

1012-
power_supply_unregister(bq->charger);
1013-
10141013
if (!IS_ERR_OR_NULL(bq->usb_phy))
10151014
usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
10161015

0 commit comments

Comments
 (0)