power: supply: surface_battery: Defer AC-triggered update across suspend
PATCH 1/1 power: supply: surface_battery: Defer AC-triggered update across suspend
When the AC adapter is plugged in or removed, spwr_external_power_changed() schedules a battery state update after five seconds, on system_wq. That workqueue is not frozen for system suspend. If the system starts to suspend within those five seconds, for example when the charger is unplugged and the lid closed straight away, the work runs after the Surface Aggregator controller has been suspended. The request then trips the WARN_ON() in ssam_request_do_sync_with_buffer() and fails:
WARNING: drivers/platform/surface/aggregator/controller.c:1703 at ssam_request_do_sync_with_buffer+0x15a/0x180 [surface_aggregator] Workqueue: events spwr_battery_update_bst_workfn [surface_battery] surface_battery 01:02:02:01:00: failed to update battery state: -19
Queue the work on system_freezable_wq instead. Freezable workqueues are frozen before devices are suspended and thawed after they have resumed, so a pending update now runs only once the controller is reachable again. Nothing is lost by the delay: surface_battery_resume() already rechecks the full battery state.
The problem was found, and this change written and tested, with the help of an AI coding assistant (Claude Opus 5.5 in Claude Code). It was found by reading the kernel log after suspend tests on a Surface Book 3, then confirmed in the source. Reproduced and tested on a Surface Book 3 with Linux 7.3.4: unplug the charger and close the lid within five seconds. Before the change the warning above appeared; after it, the same test gave no warning and correct battery state after resume.
---
Not done, so reviewers know: the change was built and tested against Linux 7.3.4 only. Mainline's surface_battery.c differs from 7.3.4 only in an unrelated devm_kzalloc_obj() line, and the patch applies to mainline, but it was not built or run there.
--- drivers/power/supply/surface_battery.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/power/supply/surface_battery.c +6 -1
@@ -417,9 +417,14 @@ static void spwr_external_power_changed(struct power_supply *psy)* event for the state (charging/discharging) change. Furthermore it * may take some time until the state is updated on the battery. * Schedule an update to solve this. +* +* Use the freezable workqueue so that the update does not run while +* the system is suspending, when the EC can no longer be reached. +* surface_battery_resume() rechecks the full state anyway. */ -schedule_delayed_work (&bat->update_work,SPWR_AC_BAT_UPDATE_DELAY ); +queue_delayed_work (system_freezable_wq, &bat->update_work, +SPWR_AC_BAT_UPDATE_DELAY ); }
---
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
change-id: 20261116-surface-battery-suspend-b5adbbfe80af
Best regards,
Signature · 1 line
Ricky Sorrell <[email protected]>
Sebastian Reichel
AppliedQuoted 9 lines · Ricky Sorrell
When the AC adapter is plugged in or removed,
spwr_external_power_changed()schedules a battery state update after five seconds, onsystem_wq. That workqueue is not frozen for system suspend. If the system starts to suspend within those five seconds, for example when the charger is unplugged and the lid closed straight away, the work runs after the Surface Aggregator controller has been suspended. The request then trips theWARN_ON()inssam_request_do_sync_with_buffer()and fails:[...]
Applied, thanks!
[1/1] power: supply: surface_battery: Defer AC-triggered update across suspend
commit: 7b4221256a4ed1d0b90de3d1ea00ade25c29670b
Best regards,
Signature · 1 line
Sebastian Reichel <[email protected]>