[PATCH 02/10] power: supply: generic-adc-battery: Initialise bat_work before registering supply

From: Lee Jones

Date: Thu Dec 03 2026 - 11:13:44 EST


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@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
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