[PATCH v2 0/4] power: supply: Fix probe time race against driver teardown and battery parsing

From: Alexey Charkov

Date: Wed Sep 09 2026 - 13:51:24 EST


If a bus holding the battery-equipped power supply goes down right after
it registered, it leads to massive fireworks as the driver starts to tear
down its resources while callers from other contexts are already eager to
process events using this driver's callbacks (and their private data).

In current power supply core it's further aggravated by the fact that the
battery parsing code expects a psy struct from the caller, which is only
available once the device has been registered, so the device gets
registered with its hardware constraints (e.g. charging current) not yet
fully filled in.

Fix the former by preventing the unregistration of the psy device before
its callbacks complete, and the latter by providing a battery parsing
helper which doesn't require a psy struct from the caller (as it doesn't
strictly need one).

While here, fix up these issues in bq257xx where all of it was discovered.

Signed-off-by: Alexey Charkov <alchark@xxxxxxxxxxx>
---
Changes in v2:
- Use psy->removing as the predicate for detecting unregistration instead
of use_cnt, because a leaking caller can leave use_cnt non-zero even
after the device is being removed, and ->removing already exists and is
used by the removal function anyway (Sashiko)
- Don't rearrange the existing short-circuit check for use_cnt and the
availability of the callback in the psy struct to avoid breakage (Sashiko)
- Drop the patch making the bq257xx_external_power_changed() return early
upon I2C read failures, otherwise a transient I2C failure in updating
cached status fields could lead to missed updates to charging current or
input current, which is worse (Sashiko). Those status fields are re-read
upon every property update anyway
- Amend patch description of patch 3 (formerly 4) to explicitly state the
potential use of unallocated memory, and add a Fixes: tag accordingly
- Link to v1: https://patch.msgid.link/20260907-bq257xx-init-v1-0-1a08b29e1a68@xxxxxxxxxxx

To: Sebastian Reichel <sre@xxxxxxxxxx>
To: Pavel Machek <pavel@xxxxxx>
To: Bartlomiej Zolnierkiewicz <b.zolnierkie@xxxxxxxxxxx>
To: Krzysztof Kozlowski <krzk@xxxxxxxxxx>
To: Lee Jones <lee@xxxxxxxxxx>
To: Chris Morgan <macromorgan@xxxxxxxxxxx>
Cc: linux-pm@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Cc: Sebastian Reichel <sebastian.reichel@xxxxxxxxxxxxx>

---
Alexey Charkov (4):
power: supply: core: prevent unregistering a power supply while a callback runs
power: supply: core: Allow getting battery info before psy is registered
power: supply: bq257xx: Use psy directly instead of driver data
power: supply: bq257xx: Parse battery info before registering power supply

drivers/power/supply/bq257xx_charger.c | 52 +++++++------
drivers/power/supply/power_supply_core.c | 128 +++++++++++++++++++++++--------
include/linux/power_supply.h | 14 ++++
3 files changed, 142 insertions(+), 52 deletions(-)
---
base-commit: c68a982815dcce5464e3bf2a31ac94f5146c04ca
change-id: 20260907-bq257xx-init-ff03beb07c36

Best regards,
--
Alexey Charkov <alchark@xxxxxxxxxxx>