All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Sebastian Reichel <sre@kernel.org>,
	"Mark A. Greer" <mgreer@animalcreek.com>,
	Anton Vorontsov <anton@enomsg.org>,
	linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 03/10] power: supply: bq24190: Manage teardown order with devm to prevent UAF
Date: Thu,  3 Dec 2026 16:12:24 +0000	[thread overview]
Message-ID: <20261203161316.319090-4-lee@kernel.org> (raw)
In-Reply-To: <20261203161316.319090-1-lee@kernel.org>

bq24190_probe() registers input_current_limit_work, the threaded IRQ,
and the VBUS regulator using devm helpers while manually unregistering
bdi->charger and bdi->battery in bq24190_remove() and on probe error.
During unbind, the supplies are unregistered and runtime PM is disabled
before devres frees the IRQ and cancels delayed work, allowing
bq24190_irq_handler_thread() or bq24190_input_current_limit_work() to
call power_supply_changed() on freed supplies.

Unify all teardown under devres in strict LIFO order, drop
bq24190_remove(), register bdi->battery before bdi->charger followed
immediately by the work-disable action, and clear bdi->initialized and
drain runtime PM via pm_runtime_barrier() while disabling
input_current_limit_work before unregistering the supplies and resetting
the hardware.

Fixes: d7bf353fd0aa ("bq24190_charger: Add support for TI BQ24190 Battery Charger")
Closes: https://sashiko.dev/#/bug/linux-a308a73a-596a-4aaf-99a5-d3b422b19742
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 drivers/power/supply/bq24190_charger.c | 122 +++++++++++++------------
 1 file changed, 64 insertions(+), 58 deletions(-)

diff --git a/drivers/power/supply/bq24190_charger.c b/drivers/power/supply/bq24190_charger.c
index 7a5867dd74c2..4d27d142f1c1 100644
--- a/drivers/power/supply/bq24190_charger.c
+++ b/drivers/power/supply/bq24190_charger.c
@@ -8,7 +8,6 @@
 #include <linux/module.h>
 #include <linux/interrupt.h>
 #include <linux/delay.h>
-#include <linux/devm-helpers.h>
 #include <linux/pm_runtime.h>
 #include <linux/pm_wakeirq.h>
 #include <linux/power_supply.h>
@@ -230,6 +229,7 @@ struct bq24190_dev_info {
 	struct delayed_work		input_current_limit_work;
 	char				model_name[I2C_NAME_SIZE];
 	bool				initialized;
+	bool				hw_initialized;
 	bool				irq_event;
 	bool				otg_vbus_enabled;
 	int				charge_type;
@@ -2114,6 +2114,32 @@ static const struct bq24190_chip_info bq24190_chip_info_tbl[] = {
 	},
 };
 
+static void bq24190_pm_runtime_cleanup(void *data)
+{
+	struct bq24190_dev_info *bdi = data;
+
+	if (bdi->hw_initialized) {
+		int error = pm_runtime_resume_and_get(bdi->dev);
+
+		if (error < 0)
+			dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error);
+		bq24190_register_reset(bdi);
+		if (error >= 0)
+			pm_runtime_put_sync(bdi->dev);
+	}
+	pm_runtime_dont_use_autosuspend(bdi->dev);
+	pm_runtime_disable(bdi->dev);
+}
+
+static void bq24190_disable_work(void *data)
+{
+	struct bq24190_dev_info *bdi = data;
+
+	WRITE_ONCE(bdi->initialized, false);
+	pm_runtime_barrier(bdi->dev);
+	disable_delayed_work_sync(&bdi->input_current_limit_work);
+}
+
 static int bq24190_probe(struct i2c_client *client)
 {
 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
@@ -2143,10 +2169,8 @@ static int bq24190_probe(struct i2c_client *client)
 	bdi->f_reg = 0;
 	bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */
 
-	ret = devm_delayed_work_autocancel(dev, &bdi->input_current_limit_work,
-					   bq24190_input_current_limit_work);
-	if (ret)
-		return ret;
+	INIT_DELAYED_WORK(&bdi->input_current_limit_work,
+			  bq24190_input_current_limit_work);
 
 	i2c_set_clientdata(client, bdi);
 
@@ -2166,10 +2190,27 @@ static int bq24190_probe(struct i2c_client *client)
 	pm_runtime_enable(dev);
 	pm_runtime_use_autosuspend(dev);
 	pm_runtime_set_autosuspend_delay(dev, 600);
-	ret = pm_runtime_get_sync(dev);
+	ret = devm_add_action_or_reset(dev, bq24190_pm_runtime_cleanup, bdi);
+	if (ret)
+		return ret;
+
+	ret = pm_runtime_resume_and_get(dev);
 	if (ret < 0) {
 		dev_err(dev, "pm_runtime_get failed: %i\n", ret);
-		goto out_pmrt;
+		return ret;
+	}
+
+	/* the battery class is deprecated and will be removed. */
+	/* in the interim, this property hides it.              */
+	if (!device_property_read_bool(dev, "omit-battery-class")) {
+		battery_cfg.drv_data = bdi;
+		bdi->battery = devm_power_supply_register(dev, &bq24190_battery_desc,
+							  &battery_cfg);
+		if (IS_ERR(bdi->battery)) {
+			dev_err(dev, "Can't register battery\n");
+			ret = PTR_ERR(bdi->battery);
+			goto out_pmrt;
+		}
 	}
 
 #ifdef CONFIG_SYSFS
@@ -2181,99 +2222,65 @@ static int bq24190_probe(struct i2c_client *client)
 	charger_cfg.fwnode = dev_fwnode(dev);
 	charger_cfg.supplied_to = bq24190_charger_supplied_to;
 	charger_cfg.num_supplicants = ARRAY_SIZE(bq24190_charger_supplied_to);
-	bdi->charger = power_supply_register(dev, &bq24190_charger_desc,
-						&charger_cfg);
+	bdi->charger = devm_power_supply_register(dev, &bq24190_charger_desc,
+						  &charger_cfg);
 	if (IS_ERR(bdi->charger)) {
 		dev_err(dev, "Can't register charger\n");
 		ret = PTR_ERR(bdi->charger);
 		goto out_pmrt;
 	}
 
-	/* the battery class is deprecated and will be removed. */
-	/* in the interim, this property hides it.              */
-	if (!device_property_read_bool(dev, "omit-battery-class")) {
-		battery_cfg.drv_data = bdi;
-		bdi->battery = power_supply_register(dev, &bq24190_battery_desc,
-						     &battery_cfg);
-		if (IS_ERR(bdi->battery)) {
-			dev_err(dev, "Can't register battery\n");
-			ret = PTR_ERR(bdi->battery);
-			goto out_charger;
-		}
-	}
+	ret = devm_add_action_or_reset(dev, bq24190_disable_work, bdi);
+	if (ret)
+		goto out_pmrt;
 
 	ret = bq24190_get_config(bdi);
 	if (ret < 0) {
 		dev_err(dev, "Can't get devicetree config\n");
-		goto out_charger;
+		goto out_pmrt;
 	}
 
 	ret = bq24190_hw_init(bdi);
 	if (ret < 0) {
 		dev_err(dev, "Hardware init failed\n");
-		goto out_charger;
+		goto out_pmrt;
 	}
+	bdi->hw_initialized = true;
 
 	ret = bq24190_configure_usb_otg(bdi, bdi->ss_reg);
 	if (ret < 0)
-		goto out_charger;
+		goto out_pmrt;
 
-	bdi->initialized = true;
+	WRITE_ONCE(bdi->initialized, true);
 
 	ret = devm_request_threaded_irq(dev, client->irq, NULL,
 			bq24190_irq_handler_thread,
 			IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
 			"bq24190-charger", bdi);
 	if (ret < 0)
-		goto out_charger;
+		goto out_pmrt;
 
 	ret = bq24190_register_vbus_regulator(bdi);
 	if (ret < 0)
-		goto out_charger;
+		goto out_pmrt;
 
 	ret = devm_device_init_wakeup(dev);
 	if (ret < 0)
-		goto out_charger;
+		goto out_pmrt;
 
 	ret = devm_pm_set_wake_irq(dev, client->irq);
 	if (ret < 0)
-		goto out_charger;
+		goto out_pmrt;
 
 	pm_runtime_put_autosuspend(dev);
 
 	return 0;
 
-out_charger:
-	if (!IS_ERR_OR_NULL(bdi->battery))
-		power_supply_unregister(bdi->battery);
-	power_supply_unregister(bdi->charger);
-
 out_pmrt:
 	pm_runtime_put_sync(dev);
-	pm_runtime_dont_use_autosuspend(dev);
-	pm_runtime_disable(dev);
 	return ret;
 }
 
-static void bq24190_remove(struct i2c_client *client)
-{
-	struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
-	int error;
-
-	error = pm_runtime_resume_and_get(bdi->dev);
-	if (error < 0)
-		dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error);
-
-	bq24190_register_reset(bdi);
-	if (bdi->battery)
-		power_supply_unregister(bdi->battery);
-	power_supply_unregister(bdi->charger);
-	if (error >= 0)
-		pm_runtime_put_sync(bdi->dev);
-	pm_runtime_dont_use_autosuspend(bdi->dev);
-	pm_runtime_disable(bdi->dev);
-}
-
 static void bq24190_shutdown(struct i2c_client *client)
 {
 	struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
@@ -2287,7 +2294,7 @@ static __maybe_unused int bq24190_runtime_suspend(struct device *dev)
 	struct i2c_client *client = to_i2c_client(dev);
 	struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
 
-	if (!bdi->initialized)
+	if (!READ_ONCE(bdi->initialized))
 		return 0;
 
 	dev_dbg(bdi->dev, "%s\n", __func__);
@@ -2300,7 +2307,7 @@ static __maybe_unused int bq24190_runtime_resume(struct device *dev)
 	struct i2c_client *client = to_i2c_client(dev);
 	struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
 
-	if (!bdi->initialized)
+	if (!READ_ONCE(bdi->initialized))
 		return 0;
 
 	if (!bdi->irq_event) {
@@ -2379,7 +2386,6 @@ MODULE_DEVICE_TABLE(of, bq24190_of_match);
 
 static struct i2c_driver bq24190_driver = {
 	.probe		= bq24190_probe,
-	.remove		= bq24190_remove,
 	.shutdown	= bq24190_shutdown,
 	.id_table	= bq24190_i2c_ids,
 	.driver = {
-- 
2.56.1.911.g883d8bcb20-goog


  parent reply	other threads:[~2026-12-03 16:13 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 16:12 [PATCH 00/10] power: supply: Fix Critical and High issues reported by Sashiko Lee Jones
2026-12-03 16:12 ` [PATCH 01/10] power: supply: bq27xxx: Check for short i2c_transfer() in bq27xxx_battery_i2c_read() Lee Jones
2026-12-03 19:14   ` Andrew Davis
2026-12-03 16:12 ` [PATCH 02/10] power: supply: generic-adc-battery: Initialise bat_work before registering supply Lee Jones
2026-12-03 16:12 ` Lee Jones [this message]
2026-12-03 16:12 ` [PATCH 04/10] power: supply: gpio-charger: Avoid printing uninitialised chargetype Lee Jones
2026-12-03 16:12 ` [PATCH 05/10] power: supply: ab8500_btemp: Remove unused global device list Lee Jones
2026-12-03 23:55   ` Linus Walleij
2026-12-03 16:12 ` [PATCH 06/10] power: supply: core: Pack supplied_from densely and bound writes Lee Jones
2026-12-03 16:12 ` [PATCH 07/10] power: supply: core: Reject empty ocv-capacity-table properties Lee Jones
2026-12-03 16:12 ` [PATCH 08/10] power: supply: cpcap-battery: Release battery info in cpcap_battery_update_battery_data() Lee Jones
2026-12-04  6:01   ` Svyatoslav Ryhel
2026-12-03 16:12 ` [PATCH 09/10] power: supply: qcom_battmgr: Hold battmgr->lock in qcom_battmgr_enable_worker() Lee Jones
2026-12-03 16:12 ` [PATCH 10/10] power: supply: sbs-battery: Always cancel delayed work on suspend Lee Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203161316.319090-4-lee@kernel.org \
    --to=lee@kernel.org \
    --cc=anton@enomsg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=mgreer@animalcreek.com \
    --cc=sre@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.