Skip to content

watchdog: apple: report no time left once the counter passed the bite, and add KUnit tests - #2919

Merged
RyanTheTide merged 5 commits into
mainfrom
kunit/apple-wdt
Nov 28, 2026
Merged

RyanTheTide merged 5 commits into
mainfrom
kunit/apple-wdt

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem. apple_wdt_get_timeleft() subtracts the current count from the bite time in u32. The counter keeps running while the reset is not armed (the watchdog is stopped or was never started), so once it has passed the bite time, the time left wraps to up to the whole counter range (about 178 s at 24 MHz) instead of 0. The driver also had no test.

Change.

  1. watchdog: apple: report no time left once the counter passed the bite: return 0 when the counter is at or past the bite time.
  2. watchdog: apple: add KUnit tests: a new apple_wdt suite (included from the driver) on a register block in kernel memory. It checks that start, stop, ping and restart write exactly their registers and no other, set_timeout() for a range of timeouts including the clamp to the 32-bit counter, the time left (normal, after a ping, past the bite), and suspend/resume of an idle, an active and a hardware-running watchdog. Plus a standalone drivers/watchdog/apple_wdt.kunitconfig.

Testing.

  • kunit.py run --arch=arm64 (QEMU, gcc): 20/20 with the fix, standalone kunitconfig 20/20, and again with all six of this series' branches merged on integration/aurora 322eadc.
  • Without the fix (tip driver code, tests kept): apple_wdt_test_timeleft_past_bite fails (19/20).
  • Negative controls: bite time written from the unclamped timeout (6 failures), start no longer resetting the count (4), restart writing watchdog 0's registers instead of watchdog 1's (2).
  • checkpatch --strict: 0 errors; the warnings are MAINTAINERS and the kunitconfig comment.

Still to run. A hardware row is not needed for the fix (it only changes the value reported once the counter has passed the bite). Reading /sys/class/watchdog/watchdog0/timeleft on a Mac with the watchdog stopped would show it.

apple_wdt_get_timeleft() subtracts the current count from the bite
time in u32. The counter keeps running while the reset is not armed
(the watchdog is stopped, or was never started), so once it has passed
the bite time the difference wraps and the time left reads as up to
the whole counter range (178 s at 24 MHz) instead of 0.

Return 0 when the counter is at or past the bite time.

Fixes: 4ed224a ("watchdog: Add Apple SoC watchdog driver")
Signed-off-by: Ryan Murray <[email protected]>
Add a suite for the Apple SoC watchdog driver that runs its operations
on a register block in kernel memory with a fixed clock rate:

- start, stop, ping and restart write exactly the registers they
  should, and no other;
- set_timeout() for a range of timeouts, including the clamp to what
  the 32-bit counter can hold;
- the time left before the reset, after a ping, and once the counter
  has passed the bite time;
- suspend and resume of an idle, an active and a hardware-running
  watchdog.

Tests only, no functional change.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Review at 23a46a1.

The units are right: ticks of the DT clock (clkref, 24 MHz fixed-clock, read with clk_get_rate() at probe) divided by clk_rate gives whole seconds, rounded down. The change only reads registers, so it cannot make the watchdog fire, or stop it firing. Arming, pings and the bite value are unchanged.

  1. drivers/watchdog/apple_wdt.c:124: the fix only covers the first counter period. While the reset is disarmed, WD1_CUR_TIME is a free-running 32-bit counter. About 179 s (at 24 MHz) after it passes the bite time it wraps to 0, and from then on cur_time < reset_time. A stopped watchdog therefore again reports anything between 0 and the full timeout, once per counter period. Read WD1_CTRL and return 0 when APPLE_WDT_CTRL_RESET_EN is clear. Keep cur_time >= reset_time for the armed case: there the bite is clamped below U32_MAX, so the counter cannot wrap before the reset.
  2. apple_wdt-test.c:254-272: timeleft_past_bite sets CTRL=0 but only tests counts above the bite. Add a disarmed case with a count that has wrapped, e.g. CUR_TIME=5 and BITE=30 s, which should give 0. It fails at this head. Add an armed case with CTRL=RESET_EN too, so the 30/29/20/1 values in apple_wdt_test_timeleft() do not depend on the fill pattern happening to set bit 2 (0x1c).
  3. The idea that the counter keeps running while the reset is disarmed is the premise of the fix, and no Mac has confirmed it at this head. One row on any Mac would: stop watchdog0, then read /sys/class/watchdog/watchdog0/timeleft over a few minutes, before and after the change.
  4. Nit, apple_wdt-test.c:174: 4 GHz is not "one tick short of a 32-bit rate" (U32_MAX is 4294967295). The case is right; the comment is wrong.

Otherwise the tests are good. The fill-pattern check catches stray writes to WD0 and WD2, and the set_timeout table covers the clamp to the 32-bit counter, including UINT_MAX/1000.

@DjDeveloperr DjDeveloperr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Formal §1–§6 pre-PR checklist review at 23a46a1d08b47f8e461037377c05139b58d04b71.

Checklist Summary

  • §1 No model gating: Pass.
  • §2 Production-ready, no hacks: Needs change — see blocker 1 below.
  • §3 Safety: Pass (apple_wdt_get_timeleft() is read-only and does not alter arming, ping, or bite register writes).
  • §4 Clean-room & attribution: Pass (both commits carry human author/committer and Signed-off-by: Ryan Murray <[email protected]> only, zero AI trailers in commits; PR body disclosure present).
  • §5 Evidence: Owed — see blocker 2 below.
  • §6 Naming: Pass.

Blockers

  1. Incomplete fix for 32-bit counter wrap while disarmed (drivers/watchdog/apple_wdt.c:116-127): On a free-running 32-bit counter at 24 MHz, WD1_CUR_TIME wraps back to 0 every ~179 s while APPLE_WDT_CTRL_RESET_EN is clear. Once it wraps below reset_time, cur_time >= reset_time is false again and a stopped watchdog resumes reporting bogus non-zero timeleft (timeout..1 s) every counter period. Read APPLE_WDT_WD1_CTRL and return 0 whenever !(ctrl & APPLE_WDT_CTRL_RESET_EN) (keeping cur_time >= reset_time for the armed case). Update apple_wdt-test.c to set APPLE_WDT_WD1_CTRL = APPLE_WDT_CTRL_RESET_EN explicitly in armed timeleft tests (rather than relying on APPLE_WDT_TEST_FILL(0x1c) having bit 2 set) and add a disarmed wrapped-counter test case (CUR_TIME < BITE_TIME with CTRL = 0 -> 0).
  2. §5 hardware evidence owed: This PR changes driver behavior (apple_wdt.c) based on the unverified premise that WD1_CUR_TIME keeps running while disarmed, and every Apple Silicon Mac carries apple_wdt. Owed: a hardware row on any Apple Silicon Mac reading /sys/class/watchdog/watchdog0/timeleft with the watchdog stopped across >180 s (verifying both past-bite and post-wrap behavior), plus regression check (0 SELinux denials, 0 failed units, 0 new kernel errors).

Non-blocking notes

  • drivers/watchdog/apple_wdt-test.c:174: comment says 4 GHz is "one tick short of a 32-bit rate" (U32_MAX is 4294967295, not 4000000001).
  • KUnit integration keeps all functions static under CONFIG_KUNIT=n and has clean teardown.
  • CI checks are still queued/in-progress at review time.

@github-actions github-actions Bot added area:platform Area from CODEOWNERS (bot) changes-requested A reviewer requested changes (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merge readiness at 3500a179: not ready yet

Check Detail
CI at head ok 25 checks passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes no requested by DjDeveloperr
Hardware row at head ok comment at 3500a179
Conflicts ok none
PR text adopt when you next edit add a 'Tested:' line; a 'Risk:' line (door and Macs) (guide)

Areas: area:gpu, area:platform

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: FINISH, then merge at 23a46a1d08b4

Blockers:

  1. drivers/watchdog/apple_wdt.c:116-127: WD1_CUR_TIME is a free-running 32-bit counter at 24 MHz, so with the watchdog disarmed it wraps to 0 about every 179 s. Once it wraps below reset_time, cur_time >= reset_time is false again, and a stopped watchdog reports a bogus time left. Fix: report 0 whenever RESET_EN is clear, or track the bite across wraps, with a KUnit case for the wrap.

Checked: no Apple material, no gates. The KUnit tests cover the armed path.

@DjDeveloperr
DjDeveloperr dismissed their stale review November 24, 2026 23:51

Superseded by the review at 23a46a1

WD1_CUR_TIME is a free-running 32-bit counter. With RESET_EN clear it
keeps counting and wraps to zero every 2^32 ticks (about 179 s at
24 MHz). Once it wraps back below the bite time, the cur >= bite check
no longer catches it and a stopped watchdog reports up to the whole
timeout as time left, although no reset is coming.

Read the control register first and report 0 whenever RESET_EN is
clear. The cur >= bite check stays for the armed counter that reads
past the bite while the reset is on its way.

The KUnit tests arm the reset where they expect time left, and a new
case covers the disarmed counter past the bite, wrapped back below it,
with only IRQ bits set, and a start/stop pair. With the driver change
reverted, the new case fails 4 expectations.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide
RyanTheTide requested review from a team as code owners November 27, 2026 05:23
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Fixed in d790d27 (one normal push, no force).

Blocker 1 (free-running counter wraps below the bite time): apple_wdt_get_timeleft() now reads WD1_CTRL first and reports 0 whenever RESET_EN is clear, so a stopped or never-started watchdog has no time left at any counter value. The cur >= bite check stays for the armed counter that reads past the bite while the reset is on its way.

Testing: kunit.py run --arch=arm64 --kunitconfig=drivers/watchdog/apple_wdt.kunitconfig: 21/21 pass. The new apple_wdt_test_timeleft_disarmed covers the disarmed counter past the bite, at U32_MAX, wrapped back to 0 and to 10 s below the bite, with only the IRQ bits set, and start, then stop. With the driver change reverted it fails 4 expectations (lines 297/300/305/311). The existing time-left cases now arm RESET_EN first. checkpatch --strict is clean.

Still to run: a lab Mac row at this head (timeleft reads 0 when stopped, including after more than 179 s, and counts down when opened). I'll post it here.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Also forward-merged main at 5b1cdc4 (clean merge) so the head matches what our combined test kernel carries. Builds in 7.3.0-aurora-fixa2.

@RyanTheTide

RyanTheTide commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Row at 5b1cdc4d1ee6 (this PR's files byte-identical in the combined test kernel 7.3.0-aurora-fixa2): M1 Pro (J316s, T6000/G13S): PASS, M2 Air (J413, T8112/G14G): PASS.

The armed path reads correctly on hardware. The disarmed and wrapped cases are covered by the KUnit cases, since systemd keeps the watchdog armed on these Macs.

Detail

Both rows ran on the default cmdline with the kernel booted once as a non-default entry, then back to fleet89, which was verified afterwards (saved entry, empty next_entry, kernel removed, 0 failed, Enforcing). Per Mac: SELinux Enforcing, 0 failed units, 0 AVC, 0 err-level kernel lines, 0 oops; Wi-Fi ping OK; s2idle (rtc1, 15 s) rc=0 with ping and a 64 MiB direct NVMe read after the wake; Apple SoC watchdog armed with time left counting (44 s and 58 s of systemd's 60 s); dEQP-VK api.smoke 8/8 pass. J413 also: glmark2 off-screen (FBO) score 7672, 2 audio sinks. On the M1 Pro, glmark2-wayland never starts rendering: its internal panel is dead and there is no Wayland output, the same on fleet89 (traced), so no GL FBO number there. Silent, no playback. Logs: traces/kernel/20261127-fixa2-rows/ (store).
Kernel kit: kits/test/20261127-fixa2 = main 0a4f97d + forward-merged heads of #2919 #2886 #2302 #2475 #2393 #2384 #2895.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Row at 5b1cdc4d1ee6 (byte-identical in 7.3.0-aurora-fixa2): Neo (J700, T8140/G17P): PASS.

Third family: the armed path reads correctly on the Neo too.

Detail

Neo (J700, T8140/G17P), fixa2 booted once as a non-default BLS entry, then back to fleet89, verified afterwards (saved entry, empty next_entry, kernel removed, 0 failed, Enforcing). SELinux Enforcing, 0 failed units, 0 AVC, 0 oops, 0 err-level kernel lines; Wi-Fi up; G17P Vulkan device; dEQP-VK api.smoke 8/8 and draw.renderpass.simple_draw 4/4; watchdog armed (57 s left of 60); s2idle on rtc0 rc=0 with a 64 MiB direct NVMe read after the wake. Two lines are the same on fleet89 (baseline traced): the boot-time "G17: crash logs from the firmware data segment" note, and Wi-Fi needing ~9 s after the wake before ping answers. Logs: traces/kernel/20261127-fixa2-rows/neo/ (store).

@github-actions github-actions Bot added ci-failing A check failed at the PR head (bot) and removed needs-hw-row No hardware row at the PR head (bot) labels Nov 27, 2026
…mode

The merge of main brought the WD2 simple-reconfig path, where WD1
RESET_EN is never set and the WD2 bark IRQ_EN is what resets the SoC.
The disarmed check still read WD1_CTRL, so in reconfig mode
get_timeleft() always reported 0 (apple-wdt-tests step 4 failed).

Read the control register of the watchdog that resets: WD2 IRQ_EN with
reconfig, WD1 RESET_EN without. Drop the duplicated cur >= reset check
the merge left behind.

The host test now also checks a stopped WD2 with its counter below the
bark; with the WD2 check removed it fails.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

CI fix at 3500a17: the main merge (5b1cdc4) brought the WD2 simple-reconfig path, where WD1 RESET_EN is never set and the WD2 bark IRQ_EN is what resets. The disarmed check still read WD1_CTRL, so in reconfig mode get_timeleft() always returned 0 and apple-wdt-tests step 4 failed (get_timeleft == 10).

  • get_timeleft() now reads the control register of the watchdog that resets: WD2 IRQ_EN with reconfig, WD1 RESET_EN without. The duplicated cur >= reset check the merge left behind is gone.
  • apple-wdt-tests.py also checks a stopped WD2 with its counter below the bark (expects 0). With the WD2 check removed, that assertion fails.

Tested: run-host-tests.py --only apple-wdt 10/10 (the plain run, --old-ref and the 8 mutations); KUnit apple_wdt 21/21 (arm64 qemu, 16K pages); checkpatch clean.
Risk: the WD1 behaviour is unchanged since the fixa2 rows. In reconfig mode, time left now follows the WD2 bark (it was always 0 at the previous head). The fixa2 rows (M1 Pro, J413, Neo) ran the WD1 path. A reconfig-mode Mac row at this head is still owed.

@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) needs-hw-row No hardware row at the PR head (bot) and removed ci-failing A check failed at the PR head (bot) labels Nov 27, 2026
@RyanTheTide

RyanTheTide commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Hardware row at this head (3500a17): A18 Pro (T8140, J700), legacy WD1 mode (no reconfig reg in the DT). The kernel was the combined fixa5 test kernel, linux main fca33ed plus the heads of #2887 #2959 #3384 #2919 #2393 #2895. All five files this PR changes are byte-identical to this head there. One-shot boot, default command line.

The watchdog was stopped by setting systemd's RuntimeWatchdogUSec to 0 for 100 s, longer than the 60 s timeout, then re-armed to 60 s. I read /sys/class/watchdog/watchdog0/{state,timeleft}:

armed stopped t=0 / 20 / 40 s stopped t=59 / 61 / 100 s (past the bite) re-armed
fleet89 (main 645f7cd, no fix) 56 55 54 53 / 33 / 13 173 / 171 / 132 (wrapped) 57
fixa5 (this head) 52 51 50 0 / 0 / 0 0 / 0 / 0 57
  • fleet89 shows the bug: after the bite time the stopped counter wraps to about 173 s left.
  • With this head a stopped watchdog reads 0 throughout, armed values count down normally, and re-arming works.
  • Same boot: 0 failed units, Enforcing, 0 AVC denials, 0 kernel errors from apple_wdt.

Same row on the M4 Pro (T6040, J616s), legacy WD1 mode, same fixa5 kernel:

  • fleet89, stopped: 48 at t=0, then 167 at t=61 s (wrapped).
  • fixa5, stopped: 0 at t=0 / 40 / 61 / 100 s. Re-armed reads 57.
  • Evidence: traces/kernel/20261128-fixa5-rows/j616s/{A,B}-wdt.txt.

A reconfig-mode (WD2) Mac was not run. Only T6050 has the reconfig register in our DTs, so that row is still owed.

Evidence (lab store): traces/kernel/20261128-fixa5-rows/neo/{A1,B1}-wdt.txt, script kits/test/20261128-fixa5/wdt.sh.

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: MERGE at 3500a179

Blockers: none (26/26 CI green and 3-family exact-head hardware rows PASS; ready to merge).

Checked:

  • drivers/watchdog/apple_wdt.c: d790d275 and 3500a179 cleanly resolve both the free-running 32-bit counter wraparound blocker and WD2 reconfig-mode compatibility by checking APPLE_WDT_WD2_CTRL & APPLE_WDT_CTRL_IRQ_EN when wdt->reconfig is set and APPLE_WDT_WD1_CTRL & APPLE_WDT_CTRL_RESET_EN otherwise before computing remaining ticks, returning 0 when disarmed or past bite time.
  • KUnit (drivers/watchdog/apple_wdt-test.c) and host tests (tools/asahi/apple-wdt-tests.py) cover stopped WD1/WD2 and wraparound edge cases; 26/26 CI checks green (SUCCESS), MERGEABLE on main.
  • Exact-head hardware rows PASS across J700 (A18 Pro), J316s (M1 Pro), and J413 (M2 Air); checked for Apple material, tool attribution, private serials, and gating: clean.
Evidence
  • Head: 3500a179b5d6 (base: main, mergeable: MERGEABLE, +463/-1 across 5 files).

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

code-owner ping <@422957901716652033> / @DjDeveloperr: merge train at 3500a179. The review App approved this head (NEON-PR-REVIEW2's adversarial pass). Rows: 3-family. This merges under the 6-point rule 1 h from now, once hosted CI is green, unless you object.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Merging under the 6-point rule (Ryan 09:29, permanent), at 3500a179. (1) The review App approved at this head. (2) Full CI is green. (3/4) Rows: 3-family. (5) There's no blocking review. (6) Dj was pinged here 118 min ago with no objection. Revert: git revert -m 1 <merge>.

@RyanTheTide
RyanTheTide merged commit 9c851e6 into main Nov 28, 2026
27 checks passed
@RyanTheTide
RyanTheTide deleted the kunit/apple-wdt branch November 28, 2026 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:gpu Area from CODEOWNERS (bot) area:platform Area from CODEOWNERS (bot) changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants