Skip to contentSkip to conversation

power: supply: bq27xxx: Fix kernel crash on IRQ handler register error

PATCH In review Hans de Goede 3 participants 6 messages Oct 31, 2021 – Nov 2, 2021

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 power: supply: bq27xxx: Fix kernel crash on IRQ handler register error

When registering the IRQ handler fails, do not just return the error code, this will free the devm_kzalloc()-ed data struct while leaving the queued work queued and the registered power_supply registered with both of them now pointing to free-ed memory, resulting in various kernel crashes soon afterwards.

Instead properly tear-down things on IRQ handler register errors.

Fixes 703df6c09795 ("power: bq27xxx_battery: Reorganize I2C into a module")
Cc Andrew F. Davis <[email protected]>

---
Changes in v2:
- Fix devm_kzalloc()-ed type in the commit message

---
 drivers/power/supply/bq27xxx_battery_i2c.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/power/supply/bq27xxx_battery_i2c.c +2 -1
index 46f078350fd3..cf38cbfe13e9 100644
@@ -187,7 +187,8 @@ static int bq27xxx_battery_i2c_probe(struct i2c_client *client,
 			dev_err(&client->dev,
 				"Unable to register IRQ %d error %d\n",
 				client->irq, ret);
-			return ret;
+			bq27xxx_battery_teardown(di);
+			goto err_failed;
 		}
 	}
 
Signature · 1 line
2.31.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · Hans de Goede

When registering the IRQ handler fails, do not just return the error code, this will free the devm_kzalloc()-ed data struct while leaving the queued work queued and the registered power_supply registered with both of them now pointing to free-ed memory, resulting in various kernel crashes soon afterwards.

Instead properly tear-down things on IRQ handler register errors.

FWIW,

Quoted 27 lines
Fixes 703df6c09795 ("power: bq27xxx_battery: Reorganize I2C into a module")
Cc Andrew F. Davis <[email protected]>

---
Changes in v2:
- Fix devm_kzalloc()-ed type in the commit message

---
 drivers/power/supply/bq27xxx_battery_i2c.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/power/supply/bq27xxx_battery_i2c.c +2 -2
index 46f078350fd3..cf38cbfe13e9 100644
@@ -187,7 +187,8 @@ static int bq27xxx_battery_i2c_probe(struct i2c_client *client,
                        dev_err(&client->dev,
                                "Unable to register IRQ %d error %d\n",
                                client->irq, ret);
-                       return ret;
+                       bq27xxx_battery_teardown(di);
+                       goto err_failed;
                }
        }
 
--

2.31.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw

Hi,

Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Hans de Goede

When registering the IRQ handler fails, do not just return the error code, this will free the devm_kzalloc()-ed data struct while leaving the queued work queued and the registered power_supply registered with both of them now pointing to free-ed memory, resulting in various kernel crashes soon afterwards.

Quoted 4 lines
Quoted 1 line

Instead properly tear-down things on IRQ handler register errors.

FWIW,

Thanks, queued.

-- Sebastian

Quoted 32 lines
Quoted 27 lines
Fixes 703df6c09795 ("power: bq27xxx_battery: Reorganize I2C into a module")
Cc Andrew F. Davis <[email protected]>

---
Changes in v2:
- Fix devm_kzalloc()-ed type in the commit message

---
 drivers/power/supply/bq27xxx_battery_i2c.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/power/supply/bq27xxx_battery_i2c.c +2 -2
index 46f078350fd3..cf38cbfe13e9 100644
@@ -187,7 +187,8 @@ static int bq27xxx_battery_i2c_probe(struct i2c_client *client,
                        dev_err(&client->dev,
                                "Unable to register IRQ %d error %d\n",
                                client->irq, ret);
-                       return ret;
+                       bq27xxx_battery_teardown(di);
+                       goto err_failed;
                }
        }
 
--

2.31.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Hi Sebastian,

Quoted 11 lines · Sebastian Reichel

Hi,

Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Hans de Goede

When registering the IRQ handler fails, do not just return the error code, this will free the devm_kzalloc()-ed data struct while leaving the queued work queued and the registered power_supply registered with both of them now pointing to free-ed memory, resulting in various kernel crashes soon afterwards.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Instead properly tear-down things on IRQ handler register errors.

FWIW,

Thanks, queued.

Thank you, note these 2 patches (for the bq25980 driver) are also pure bug-fixes, I posted them as part of a larger series, but in hindsight I should have probably posted them separately:

https://lore.kernel.org/platform-driver-x86/[email protected]/
https://lore.kernel.org/platform-driver-x86/[email protected]/

It would be good if you can pick these 2 up too (I'll respin the rest of the series to address various review comments without them then).

Let me know if you want me to resend these 2 as a stand alone series.

Regards,

Hans

Quoted 32 lines
Quoted 32 lines
Quoted 27 lines
Fixes 703df6c09795 ("power: bq27xxx_battery: Reorganize I2C into a module")
Cc Andrew F. Davis <[email protected]>

---
Changes in v2:
- Fix devm_kzalloc()-ed type in the commit message

---
 drivers/power/supply/bq27xxx_battery_i2c.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/power/supply/bq27xxx_battery_i2c.c +2 -2
index 46f078350fd3..cf38cbfe13e9 100644
@@ -187,7 +187,8 @@ static int bq27xxx_battery_i2c_probe(struct i2c_client *client,
                        dev_err(&client->dev,
                                "Unable to register IRQ %d error %d\n",
                                client->irq, ret);
-                       return ret;
+                       bq27xxx_battery_teardown(di);
+                       goto err_failed;
                }
        }
 
--

2.31.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Hi Hans,

Quoted 25 lines · Hans de Goede

Hi Sebastian,

Quoted 11 lines · Sebastian Reichel

Hi,

Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Hans de Goede

When registering the IRQ handler fails, do not just return the error code, this will free the devm_kzalloc()-ed data struct while leaving the queued work queued and the registered power_supply registered with both of them now pointing to free-ed memory, resulting in various kernel crashes soon afterwards.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Instead properly tear-down things on IRQ handler register errors.

FWIW,

Thanks, queued.

Thank you, note these 2 patches (for the bq25980 driver) are also pure bug-fixes, I posted them as part of a larger series, but in hindsight I should have probably posted them separately:

Quoted 8 lines

https://lore.kernel.org/platform-driver-x86/[email protected]/
https://lore.kernel.org/platform-driver-x86/[email protected]/

It would be good if you can pick these 2 up too (I'll respin the rest of the series to address various review comments without them then).
Let me know if you want me to resend these 2 as a stand alone series.

Thanks for the pointer, I queued both of them. I had to slightly rebase, since your base did not include 172d0ccea55c. I have not yet reviewed the remaining patchset, but considering there are more patches to bq25890 a rebase would be ncie.

Greetings,

-- Sebastian

#5 lore raw

Hi,

Quoted 37 lines · Sebastian Reichel

Hi Hans,

Quoted 25 lines · Hans de Goede

Hi Sebastian,

Quoted 11 lines · Sebastian Reichel

Hi,

Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Hans de Goede

When registering the IRQ handler fails, do not just return the error code, this will free the devm_kzalloc()-ed data struct while leaving the queued work queued and the registered power_supply registered with both of them now pointing to free-ed memory, resulting in various kernel crashes soon afterwards.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Instead properly tear-down things on IRQ handler register errors.

FWIW,

Thanks, queued.

Thank you, note these 2 patches (for the bq25980 driver) are also pure bug-fixes, I posted them as part of a larger series, but in hindsight I should have probably posted them separately:

Quoted 8 lines

https://lore.kernel.org/platform-driver-x86/[email protected]/
https://lore.kernel.org/platform-driver-x86/[email protected]/

It would be good if you can pick these 2 up too (I'll respin the rest of the series to address various review comments without them then).
Let me know if you want me to resend these 2 as a stand alone series.

Quoted 4 lines

Thanks for the pointer, I queued both of them. I had to slightly rebase, since your base did not include 172d0ccea55c. I have not yet reviewed the remaining patchset, but considering there are more patches to bq25890 a rebase would be ncie.

Ack, I'll rebase for the next version of that series.

Regards,

Hans