[PATCH v3 0/3] power: supply: bq257xx: Fix probe time race against driver teardown and battery parsing
From: Alexey Charkov
Date: Thu Sep 10 2026 - 10:45:23 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).
This was observed on a bq257xx device, which also uses the legacy parsing
of battery information from the probe routine, where it happens after the
power supply registration, adding yet another failure mode to callbacks
firing early (i.e. before the device is fully initialized).
This series therefore does the following:
- Add a new semaphore in the core to prevent the unregistration of the
psy device before its callbacks complete
- Make bq257xx use the passed-in psy during external power change events
instead of the one stored in its private data, as the latter is not
guaranteed to be valid at that point (and was observed to produce a
use after free during teardown)
- Migrate the battery parsing and hw init into the newly available .init
callback, which ensures that the device is fully programmed by the time
external callbacks start firing
Signed-off-by: Alexey Charkov <alchark@xxxxxxxxxxx>
---
Changes in v3:
- Use the new .init callback instead of adding a dev-based battery parsing
helper (Sebastian)
- Link to v2: https://patch.msgid.link/20260909-bq257xx-init-v2-0-deb4076b1f02@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 (3):
power: supply: core: prevent unregistering a power supply while a callback runs
power: supply: bq257xx: Use psy directly instead of driver data
power: supply: bq257xx: Convert parameters setup to the new .init callback
drivers/power/supply/bq257xx_charger.c | 124 +++++++++++++++----------------
drivers/power/supply/power_supply_core.c | 26 ++++++-
include/linux/power_supply.h | 10 +++
3 files changed, 94 insertions(+), 66 deletions(-)
---
base-commit: c68a982815dcce5464e3bf2a31ac94f5146c04ca
change-id: 20260907-bq257xx-init-ff03beb07c36
Best regards,
--
Alexey Charkov <alchark@xxxxxxxxxxx>