Skip to content

omamobile-device-husky: apply the charge limit once per boot, not again on every charger event - #1068

Draft
Somnius wants to merge 3 commits into
derekross:mainfrom
Somnius:husky-charge-limit-add-only-1125
Draft

Somnius wants to merge 3 commits into
derekross:mainfrom
Somnius:husky-charge-limit-add-only-1125

Conversation

@Somnius

@Somnius Somnius commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Draft, not run on the phone yet. Tests pass in a container only.

The charge limit rule wanted omamobile-charge-limit-apply.service on every power_supply event from max77759-charger. On one boot the charger's CHGIN input kept bouncing near the end of a charge from a desktop port (26 061 CHGIN irqs). The unit started 6 968 times that boot, peaking around 10 a second. The SoC went from 53-54 C to 57-59 C over the same window. That's a timing match only, I didn't measure CPU per process.

This keeps TAG+="systemd" on every event and sets SYSTEMD_WANTS on add only. On 11-16 the unit didn't start at boot with ACTION=="add" on the whole rule. I think losing the tag on change did it, but that's not proven, so the tag stays on every event. It also drops StartLimitIntervalSec=0 from the unit, so systemd's default start limit (5 in 10 s) caps any future storm. The flip side: if the charger were added more than 5 times in 10 s (a rebind loop), the unit would stay failed. I don't see a path that does that. test/all checks both rule lines and the unit, and fails against main's rule. Version goes to 0.1.22. #997 and #1001 also claim 0.1.22, so whichever merges later gets renumbered.

Should work from reading systemd (wants come from each event's properties, and boot replays add), but I haven't checked it. The check on the phone: after a boot journalctl -b -u omamobile-charge-limit-apply shows the run succeeded (one start, or two if the coldplug add replays after the first has finished), and a plug/unplug doesn't start it again. With this there's no retry on change anymore, so if the first run at boot fails, it stays failed until the next boot.

The kernel side, CHGIN reported at most once a second, is a separate draft on linux-omamobile: derekross/linux-omamobile#298

The charge limit rule wanted omamobile-charge-limit-apply.service on every
power_supply event of max77759-charger. On 2026-11-25 the charger's CHGIN
input bounced near the end of a charge from a desktop port and the unit
started 6968 times in one boot, up to about 10 a second; the SoC went from
53-54 C to 57-59 C over the same 11 minutes (a timing match, not measured
per process).

The systemd tag stays on every event (with the whole rule on add only the
unit did not start at boot on 2026-11-16, most likely from the lost tag),
and SYSTEMD_WANTS is now set on add only. The
unit drops StartLimitIntervalSec=0, so systemd's default start limit caps
any future storm. test/all checks both lines of the rule and the unit.

Not run on the phone yet: the check is one boot (the unit runs once) and a
plug/unplug (it does not run).
@Somnius

Somnius commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Tested this on my 8 Pro tonight together with derekross/linux-omamobile#298, and it has a bug: with the add-only rule the unit never ran at boot. The saved limit is 90 % (/etc/omamobile/charge-limit), but after the reboot charge_control_end_threshold read 100 and the journal has no start of omamobile-charge-limit-apply for that boot. The device unit's Wants= is empty. The charger probed at 3.94 s, after systemd-udev-trigger (3.15 s). Under the old rule it applied 90 % fine on a replug at 20:49.

So the storm is gone (0 starts instead of 6 968), but only because the unit doesn't start at all. I haven't found why the add event doesn't pull it in yet. Keeping this a draft until I have a fix that applies once at boot and on replug, and that's tested on the phone.

Wanting the unit on add only left it unstarted at boot on 2026-11-25:
systemd drops a device's udev Wants= and rebuilds them from SYSTEMD_WANTS
on every uevent (src/core/device.c, device_setup_unit and
device_add_udev_wants), so after the charger's first change event Wants=
was empty. The same code starts every wanted unit again on each event
unless it is already active, which is what turned the old every-event rule
into 6968 starts of a oneshot that went inactive after each run.

So the rule is back to one line with SYSTEMD_WANTS on every event, and the
unit gets RemainAfterExit=yes (stays active, change events skip it) and
StopWhenUnneeded=yes (stops when the charger device goes away, so a real
add runs it again). The default start limit stays on. test/all checks the
rule and both settings; omamobile-settings' charge-backend test now checks
RemainAfterExit instead of the dropped StartLimitIntervalSec=0.

Not run on the phone yet: check one start after boot, the saved limit in
charge_control_end_threshold, and no new start on plug/unplug.

Signed-off-by: Lef <me@lefteros.com>
@Somnius

Somnius commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in d9eaee2 + 2f4da3c. The add-only filter was the wrong idea: systemd rebuilds the device's Wants= from each uevent's SYSTEMD_WANTS, so the first change event after add left it empty. Now the rule wants the unit on every event again (no ACTION), and the unit has RemainAfterExit=yes, so it stays active after one run and the change events don't start it again. StopWhenUnneeded=yes makes it run again only when the charger device goes away (unbind or module reload). A cable replug doesn't need it: I checked the driver keeps the limit across a replug.

Tested on my 8 Pro without a reboot: one change event started it (limit 90 applied), then 10 more udevadm trigger --action=change on the charger didn't start it again (same start time). Package tests pass in the container, and tests/grep-q-pipes too. The boot path still needs a reboot on the phone; I'll add that result here tomorrow.

I also changed one line in packages/omamobile-settings/test/charge-backend.sh: it still checked for StartLimitIntervalSec=0 in this unit, which fe6aea8 had already removed. It checks RemainAfterExit=yes now.

@derekross derekross left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Lef, and thanks for finding your own bug so fast. The head now leaves the rule as main has it and makes the unit stay active after one run, which is a cleaner fix than filtering the events, and I checked that the Settings page applies a changed limit through its helper directly, so RemainAfterExit can't hide a user's change. The package's test and the settings test pass on today's main.

Before it leaves draft: the boot run you mention, one start with the saved limit applied and nothing more on a replug or a change trigger; and please retitle it, since "on add only" is now the design you dropped. One question for that run: systemd's own manual says it acts on a device's Wants= only when the device first becomes active, while your phone started the unit on change events (the 6,968, and the trigger that applied 90 %). If the boot run's journal shows which event came before each start, we'll know which it is, and the unit's comment can say what a failed first run does next: with starts on change events it is retried, up to systemd's default limit of five in ten seconds, rather than staying failed until the next boot.

@Somnius Somnius changed the title omamobile-device-husky 0.1.22: start the charge limit unit on add only omamobile-device-husky: apply the charge limit once per boot, not again on every charger event Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants