From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Sebastian Reichel <sre@kernel.org>,
Anish Kumar <yesanishhere@gmail.com>,
Anton Vorontsov <cbouatmailru@gmail.com>,
Jonathan Cameron <jic23@kernel.org>,
linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 02/10] power: supply: generic-adc-battery: Initialise bat_work before registering supply
Date: Thu, 3 Dec 2026 16:12:23 +0000 [thread overview]
Message-ID: <20261203161316.319090-3-lee@kernel.org> (raw)
In-Reply-To: <20261203161316.319090-1-lee@kernel.org>
gab_probe() registers the power supply via devm_power_supply_register()
before initialising adc_bat->bat_work via devm_delayed_work_autocancel().
A concurrent supplier notification can invoke gab_ext_power_changed()
and schedule the uninitialised delayed work before initialisation runs.
Acquire the optional charged GPIO and initialise bat_work with
INIT_DELAYED_WORK() before registering the power supply, guard against a
NULL adc_bat->psy in gab_work() during the registration window, and
register a devm cleanup action calling disable_delayed_work_sync()
immediately after registration so that devres LIFO teardown disables the
work before unregistering the power supply and releasing the GPIO.
Fixes: e60fea794e6e ("power: battery: Generic battery driver using IIO")
Closes: https://sashiko.dev/#/bug/linux-189bc20e-3b41-4fbd-bae8-c44aafba891a
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
drivers/power/supply/generic-adc-battery.c | 40 ++++++++++++++++------
1 file changed, 29 insertions(+), 11 deletions(-)
diff --git a/drivers/power/supply/generic-adc-battery.c b/drivers/power/supply/generic-adc-battery.c
index 7a1f8188bd27..0e4d1a3baf6c 100644
--- a/drivers/power/supply/generic-adc-battery.c
+++ b/drivers/power/supply/generic-adc-battery.c
@@ -18,7 +18,7 @@
#include <linux/iio/consumer.h>
#include <linux/iio/types.h>
#include <linux/of.h>
-#include <linux/devm-helpers.h>
+#include <linux/workqueue.h>
#define JITTER_DEFAULT 10 /* hope 10ms is enough */
@@ -129,13 +129,18 @@ static void gab_work(struct work_struct *work)
{
struct gab *adc_bat;
struct delayed_work *delayed_work;
+ struct power_supply *psy;
int status;
delayed_work = to_delayed_work(work);
adc_bat = container_of(delayed_work, struct gab, bat_work);
+ psy = READ_ONCE(adc_bat->psy);
+ if (!psy)
+ return;
+
status = adc_bat->status;
- if (power_supply_am_i_supplied(adc_bat->psy) <= 0)
+ if (power_supply_am_i_supplied(psy) <= 0)
adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
else if (gab_charge_finished(adc_bat))
adc_bat->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
@@ -143,7 +148,14 @@ static void gab_work(struct work_struct *work)
adc_bat->status = POWER_SUPPLY_STATUS_CHARGING;
if (status != adc_bat->status)
- power_supply_changed(adc_bat->psy);
+ power_supply_changed(psy);
+}
+
+static void gab_disable_work(void *data)
+{
+ struct gab *adc_bat = data;
+
+ disable_delayed_work_sync(&adc_bat->bat_work);
}
static irqreturn_t gab_charged(int irq, void *dev_id)
@@ -159,6 +171,7 @@ static irqreturn_t gab_charged(int irq, void *dev_id)
static int gab_probe(struct platform_device *pdev)
{
struct gab *adc_bat;
+ struct power_supply *psy;
struct power_supply_desc *psy_desc;
struct power_supply_config psy_cfg = {};
enum power_supply_property *properties;
@@ -234,18 +247,23 @@ static int gab_probe(struct platform_device *pdev)
psy_desc->properties = properties;
psy_desc->num_properties = index;
- adc_bat->psy = devm_power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
- if (IS_ERR(adc_bat->psy))
- return dev_err_probe(&pdev->dev, PTR_ERR(adc_bat->psy), "Failed to register power-supply device\n");
-
- ret = devm_delayed_work_autocancel(&pdev->dev, &adc_bat->bat_work, gab_work);
- if (ret)
- return dev_err_probe(&pdev->dev, ret, "Failed to register delayed work\n");
-
adc_bat->charge_finished = devm_gpiod_get_optional(&pdev->dev, "charged", GPIOD_IN);
if (IS_ERR(adc_bat->charge_finished))
return dev_err_probe(&pdev->dev, PTR_ERR(adc_bat->charge_finished),
"failed to get charged GPIO\n");
+
+ INIT_DELAYED_WORK(&adc_bat->bat_work, gab_work);
+
+ psy = devm_power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
+ if (IS_ERR(psy))
+ return dev_err_probe(&pdev->dev, PTR_ERR(psy),
+ "Failed to register power-supply device\n");
+ WRITE_ONCE(adc_bat->psy, psy);
+
+ ret = devm_add_action_or_reset(&pdev->dev, gab_disable_work, adc_bat);
+ if (ret)
+ return dev_err_probe(&pdev->dev, ret, "Failed to register delayed work cleanup\n");
+
if (adc_bat->charge_finished) {
int irq;
--
2.56.1.911.g883d8bcb20-goog
next prev parent reply other threads:[~2026-12-03 16:13 UTC|newest]
Thread overview: 11+ 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 16:12 ` Lee Jones [this message]
2026-12-03 16:12 ` [PATCH 03/10] power: supply: bq24190: Manage teardown order with devm to prevent UAF Lee Jones
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 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-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-3-lee@kernel.org \
--to=lee@kernel.org \
--cc=cbouatmailru@gmail.com \
--cc=jic23@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=sre@kernel.org \
--cc=stable@vger.kernel.org \
--cc=yesanishhere@gmail.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox