Skip to content

rtc: macsmc: return 0 from a successful set_time (NTP sync retry storm) - #2616

Open
satchlj wants to merge 2 commits into
integration/aurorafrom
satchlj/macsmc-rtc-set-time-ret-20261120
Open

satchlj wants to merge 2 commits into
integration/aurorafrom
satchlj/macsmc-rtc-set-time-ret-20261120

Conversation

@satchlj

@satchlj satchlj commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Claude Opus 5.5 on @satchlj's behalf.

The RTC NTP-sync retry storm on macsmc-rtc comes from a return value. macsmc_rtc_set_time() ends with return nvmem_cell_write(...), which returns the bytes written (6) on success. sync_hw_clock()/update_rtc() treat any non-zero result as a failure and retry. With CONFIG_RTC_SYSTOHC=y, every successful write is retried at once, and the PMU's RTC offset cell is rewritten in a loop instead of once per 11 minutes.

Affected set, by property:

  • Hardware: every DT that describes apple,smc-rtc, which is 13 files in this tree. They cover t8103, t600x, t8112, t602x, t8122, t6030/t6031, t604x, t8132, t8140, t8142 (j704/j813 power dtsi) and t6050.
  • Kernel: a kernel with CONFIG_RTC_SYSTOHC=y, which is the Kconfig default (default y). arch/arm64/configs/defconfig and asahi.config don't turn it off.
  • Also needed: userspace reports NTP sync, and RTC_SYSTOHC_DEVICE is the macsmc RTC (rtc0, the only RTC on these machines).
  • Not affected: kernels that disable SYSTOHC, such as alarm-aurora's linux-aurora package config and lobata's own default.
  • Booted: only on J775c. The other SoCs are in the set by the shared code path, not by test.

Base: integration/aurora db868f050a8f, rebased from b4cd7b3e15f4 on 2026-11-21. The rebase is clean, the commits are unchanged (git range-diff: =), and the four merges in between don't touch either file. The boot results above were taken on cand-10, which carries the identical commit 1.

Commits.

  1. rtc: macsmc: return 0 from a successful set_time. The offset_in_seconds path and the alarm writes already did.
  2. power: reset: macsmc-reboot: the same byte-count return in nvmem_cell_set_u8(). Its callers test < 0, except ac_power_mode_store(), which therefore never logged its "PM setting … =>" line. Only cosmetic, but it's the same bug. Compile-tested only (W=1, clean), not booted.

Measured on J775c (lobata).

  • Setup: one-shot trials of cand-10 (integration/aurora df62a8c4d38d + 84) with RTC_SYSTOHC=y, each with a trace-only patch. The patch logs every sync_hw_clock() decision to an isolated ntp_sync ftrace instance: path, update_rtc() result, expiry vs now, and rtc_set_time() return and time.
  • Window: a 60 s baseline, then a capture until three successful writes (at most 35 min).
  • RTC−REALTIME: my own method, not the row sheet's prequalified second-edge tool. I read /sys/class/rtc/rtc0/since_epoch transitions against CLOCK_REALTIME at the RTC's tick edge, three samples per boot.
  • Repeats: n=1 boot per build, except "commit 1 alone", which has n=2. Its second boot was measured after a write made with timesyncd's offset at 192 µs (1.5 ms at the time of measurement).
Kernel Re-arms in first 60 s Writes counted as successful What happens RTC−REALTIME
Unchanged 3774 0 in 35 min (62,337 retries) rtc_set_time returns 6, retried every ~0.8 ms inside the window. Outside it, the retry expiry lands in the past. —
#2197 head 33958ff9 42 0 (2,143 retries) One real write per second, indefinitely, still returning 6 —
#2197 + commit 1 2 3, 661 s apart Fixed cadence −0.501 s
Commit 1 alone 2 3, 658 s apart, 2 retries in all Fixed cadence +0.004 s (boot 1), +0.004 s (boot 2, after NTP settled)

The −0.5 s row is #2197's set_offset_nsec = NSEC_PER_SEC / 2. This driver makes the set second begin at the write, so the class default (1 s + 5 ms) is the right offset, and with it the RTC lands within 4 ms. I'll note this on #2197. This PR doesn't depend on #2197 and doesn't touch set_offset_nsec.

Build: commit 1 was booted as above. Commit 2 is compile-tested only: W=1, clean, CONFIG_POWER_RESET_MACSMC=y.

Assisted-by: LLM (Anthropic:Claude Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor

Looks right to me. nvmem_cell_write() returns the byte count, and rtc_set_time() hands the driver's value straight to update_rtc(), which treats anything non-zero as a failure, so the tick-offset branch at drivers/rtc/rtc-macsmc.c:152 was retried on every success. The fix matches the seconds-offset branch (:146) and macsmc_rtc_write_u8(). It also stops RTC_SET_TIME on /dev/rtc from returning 6 to userspace.

The macsmc-reboot change checks out: every nvmem_cell_set_u8() caller tests < 0 except ac_power_mode_store() (macsmc-reboot.c:279), which tests !ret and now logs as intended.

Optional, and it can be left alone: macsmc_rtc_get_time() still returns the SMC read's byte count (rtc-macsmc.c:117). The core only checks < 0 there, so it does no harm, but return 0 would match set_time.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor

A before row from a second SoC, on a fleet kernel with CONFIG_RTC_SYSTOHC=y and NTP synchronized: M1 Pro (T6000), 7.3.0, about 3 minutes after boot. A private ftrace instance recorded the rtc:rtc_set_time tracepoint.

Window rtc_set_time calls Return values
60 s 473 6 on every line sampled
30 s 234 6 on all 234

That is about 8 writes a second, every one reported as failed, the same pattern as your "Unchanged" row on the J775c. So the bug isn't specific to T6050. Configs with SYSTOHC off, such as the Fedora kernel-aurora package, don't hit it.

Assisted-by: LLM (Claude:Opus 5.5)

@satchlj

satchlj commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Claude Opus 5.5 on @satchlj's behalf.

@RyanTheTide: thanks for the review and for the M1 Pro (T6000) before row. With your row, the bug is reproduced on two SoCs, and it matches the affected set in the description (any apple,smc-rtc machine with RTC_SYSTOHC=y).

What we can supply from lobata (J775c) is already in the table above, on the same kernel with only this change toggled:

  • Before: 3774 re-arms in the first 60 s; rtc_set_time returning 6, retried every ~0.8 ms inside the write window.
  • After: one write per ~658 s, 2 retries in all; RTC at +4 ms from CLOCK_REALTIME on two boots.

If an after row on the M1 Pro fits your lane, it would close the second SoC. I can't run it here.

On macsmc_rtc_get_time() returning the read's byte count (:117): agreed, it's harmless because the core only checks < 0. I'd rather not add an untested change to an accepted PR. If you'd like it here, say so, and I'll add it as a third commit and boot it on lobata first.

@RyanTheTide

Copy link
Copy Markdown
Contributor

get_time is fine as it is. No need to add an untested commit to this PR.

I'll add the M1 Pro after-row with the same capture once a kernel carrying this change runs there.

Assisted-by: LLM (Claude:Opus 5.5)

macsmc_rtc_set_time() ends with "return nvmem_cell_write(...)", which
returns the number of bytes written (6) on success. rtc_set_time()
passes that on, and the NTP sync path (sync_hw_clock() via update_rtc())
treats any non-zero result as a failure and retries: with
CONFIG_RTC_SYSTOHC=y every successful write is retried at once, so the
PMU's RTC offset cell is rewritten in a loop instead of once per 11
minutes. The offset_in_seconds path and the alarm writes already
return 0 on success; do the same here.

On J775c (T6050), traced at the sync_hw_clock() decisions:
  without:  3774 timer re-arms in 60 s, rtc_set_time returning 6,
            retried every ~0.8 ms inside the write window;
  with:     one write per 658 s (3 in 35 min), 2 retries in all, and
            the RTC's second at +4 ms from CLOCK_REALTIME at its tick.

Signed-off-by: Satya Benson <satya@benson.earth>
…t_u8()

nvmem_cell_set_u8() returns nvmem_cell_write()'s byte count (1) on
success. Its callers test "< 0", except ac_power_mode_store(), whose
"if (!ret && new != val)" never logs the "PM setting %02x => %02x"
line after a successful change. Return 0 on success, as the rtc-macsmc
helpers do.

Signed-off-by: Satya Benson <satya@benson.earth>
@satchlj
satchlj force-pushed the satchlj/macsmc-rtc-set-time-ret-20261120 branch from c1687f2 to f6a55de Compare November 21, 2026 00:42
@funkzy

funkzy commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

@satchlj, a J775D (Mac Studio, M5 Ultra) row for this, and one more effect of the bug. We found it separately while tracing why an RTC-woken s2idle never woke on an integration kernel, and carried the same two changes locally until we saw this PR.

RTC-woken s2idle. With CONFIG_RTC_SYSTOHC=y, every retried set_time makes the RTC core re-check the armed alarm. The kernel's clock runs about 70 ms ahead of the PMU's alarm comparator, so the core expires the alarm in software and disarms it just before the PMU would fire. No alarm interrupt is raised, and the Mac sleeps until something else wakes it. With the sync stopped by hand on the same boot, the alarm fired.

J775D, integration/aurora, default command line RTC-woken s2idle
without these changes 0 of 5 woke
with them 5 of 5 (9.75 to 10.46 s asleep), then 14 of 14 across a 3 h soak

Fixes: tags, if you want them:

  • rtc: macsmc: Fixes: 49a51df427db ("rtc: Add new rtc-macsmc driver for Apple Silicon Macs")
  • power: reset: macsmc-reboot: Fixes: 4d11bfdc0ef5 ("power: reset: macsmc-reboot: Add ac_power_mode, with power-on at connect"). That one is ours: ac_power_mode_store() is the only caller that reads the helper's result as an error.

We'll switch our series to your two commits.

@satchlj
satchlj force-pushed the satchlj/macsmc-rtc-set-time-ret-20261120 branch from f6a55de to 078f441 Compare November 21, 2026 21:12
@satchlj

satchlj commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Claude Opus 5.5 on @satchlj's behalf: trailer-only rewrite, removed Co-Authored-By from 2 commit(s); same trees, Signed-off-by kept (f6a55de -> 078f441, --force-with-lease).

@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.

Pre-PR checklist review at 078f441ec922 (§1–§6).

The code changes are clean, minimal, and verified against surrounding source:

  • drivers/rtc/rtc-macsmc.c:152-153: returning ret < 0 ? ret : 0 fixes the positive byte-count return (6) that caused update_rtc() / sync_hw_clock() to treat every successful write as a failure and retry in a loop (and matches the offset_in_seconds branch at :146-147 and macsmc_rtc_write_u8() at :179). Confirmed __rtc_read_time() overwrites positive returns via rtc_valid_tm(), so leaving macsmc_rtc_get_time() untouched is safe.
  • drivers/power/reset/macsmc-reboot.c:99-104: all callers of nvmem_cell_set_u8() check < 0 except ac_power_mode_store() (:279-282), which checks !ret and now logs "PM setting %02x => %02x" as intended.

Checklist summary (§1–§6)

  • §1 No model gating: Pass — applies across all apple,smc-rtc / apple,smc-reboot machines.
  • §2 Production-ready: Pass — root-cause fix on default command line.
  • §3 Safety: Pass.
  • §4 Clean-room & attribution: Pass — human commits and Signed-off-by only (cleaned up in 078f441ec922), Assisted-by: at end of PR body.
  • §5 Evidence (holding here only):
    • Commit 1 has strong hardware proof on J775c (NTP sync cadence +4 ms) and J775D (@funkzy's 5/5 + 14/14 RTC-woken s2idle soak on default cmdline), with an M1 Pro before-row from @RyanTheTide.
    • Missing before approve: (1) commit 2 (macsmc-reboot.c) is still listed as compile-tested only — please boot and exercise ac_power_mode_store() to confirm the "PM setting %02x => %02x" log line on hardware; (2) a §5 regression run (0 SELinux denials, 0 failed units, 0 new kernel errors) on the live head 078f441ec922.
    • Optional: consider adding the two Fixes: tags @funkzy suggested if you respin.
  • §6 Naming: Pass.
  • CI: Green (host-tests).

Holding approval for the §5 hardware/regression rows only; no code or policy blockers.

Assisted-by: LLM (Gemini 4 Neon)

@RyanTheTide

Copy link
Copy Markdown
Contributor

Confirmed that 078f441 is a trailer-only rewrite: same tree as f6a55de (e7778e6e). Nothing new to review. The M1 Pro after-row I offered is still to come.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor

The macsmc-rtc KUnit cases in #2902 expect set_time to return 0, so they depend on this PR. With this change applied, macsmc-rtc is 16/16 and macsmc-reboot is 13/13 (arm64 QEMU). On the unfixed code, tick_set and tick_negative fail on the returned byte count (6).

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide RyanTheTide 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.

Independent source review at 078f441ec922. The two wrappers now preserve negative nvmem errors and normalize a successful write to the RTC/reset callback convention. The measured T6050 retry cadence supports the RTC change. Please add a T8112 row with SYSTOHC active and a check that the reset driver's setting round-trips on the same build, since that second change has only been compiled.

@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 adversarial §1–§6 pre-PR re-review at live head 078f441ec922e0c45de1ae3cefade2244390638a (checked against live integration/aurora 70a00e583b03: not superseded).

Code, Policy & CI Audit (§1–§4, §6)

  • Correctness / locking / lifetime / ABI: Clean.
    • drivers/rtc/rtc-macsmc.c:152-153 (ce650ca27bec): returning ret < 0 ? ret : 0 fixes the positive byte-count return (6) that caused update_rtc() / sync_hw_clock() retry storms and RTC-woken s2idle failures under CONFIG_RTC_SYSTOHC=y, matching the offset_in_seconds branch and macsmc_rtc_write_u8().
    • drivers/power/reset/macsmc-reboot.c:99-104 (078f441ec922): verified all callers of nvmem_cell_set_u8() check < 0 except ac_power_mode_store(), which checks !ret and now logs "PM setting %02x => %02x" as intended.
  • §1 No model gating: Pass.
  • §2 Production-ready: Pass — root-cause fix on default command line.
  • §3 Safety: Pass.
  • §4 Clean-room & attribution: Pass — both commits carry human author/committer and human Signed-off-by: only (zero AI trailers); PR body carries Assisted-by:.
  • §6 Naming & live CI: Pass (host-tests: success; MERGEABLE/CLEAN).

§5 Blockers (Required Before Approve)

  1. Commit 2 (drivers/power/reset/macsmc-reboot.c) is compile-tested only: it has not been booted or exercised on hardware. Please boot a kernel carrying 078f441ec922 and exercise ac_power_mode read/write round-trips, confirming the "PM setting %02x => %02x" kernel log line on hardware.
  2. Missing §5 regression gate at live head 078f441ec922 (and second-SoC after-row): none of the reported hardware runs (J775c cand-10 commit-1 trace run, J775D s2idle soak) include a §5 regression run (0 SELinux AVC denials, 0 failed systemd units, 0 new kernel errors) on the live head 078f441ec922; please also add a second-SoC CONFIG_RTC_SYSTOHC=y after-row (e.g. M1 Pro or T8112) alongside the J775c/J775D evidence.

Assisted-by: LLM (Gemini 4 Neon)

@satchlj

satchlj commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Gemini 4 Neon on @satchlj's behalf.

Hardware verification on J775c (M5 Max Mac Studio, lobata) at live head 078f441ec922 (verified byte-for-byte identical blobs: drivers/rtc/rtc-macsmc.c 94852fac217d, drivers/power/reset/macsmc-reboot.c 9ad57b9d89d7; kernel 7.3.0-lobata-prverify+ #1 SMP PREEMPT Mon Nov 23 20:11:51 EST 2026, built cleanly with tools/aurora-bindgen, default cmdline rdinit=/init nr_cpus=18 maxcpus=18 root=UUID=... arm64.nomte):

Commit 2 (macsmc-reboot.c nvmem_cell_set_u8) — ac_power_mode round-trips

  • ac-power-mode.service on boot: status=0/SUCCESS (no longer fails on boot).
  • Live sysfs round-trip on /sys/bus/platform/devices/28c600000.smc/macsmc-reboot/ac_power_mode (restore -> off -> always -> restore):
    • Readback transitions: off [restore] always → [off] restore always → off restore [always] → off [restore] always
    • Kernel log (dmesg):
      [  113.328172] macsmc-reboot macsmc-reboot: PM setting 22 => 23 (ac_power_mode off)
      [  113.340383] macsmc-reboot macsmc-reboot: PM setting 23 => 03 (ac_power_mode always)
      [  113.348495] macsmc-reboot macsmc-reboot: PM setting 03 => 22 (ac_power_mode restore)
      

Commit 1 (rtc-macsmc.c) & §5 Regression Gate

  • macsmc-rtc: registered as rtc0, hwclock -r clean, and rtcwake -m freeze -s 5 armed and woke cleanly (suspend_stats/success = 1, fail = 0).
  • SELinux AVC denials: 0
  • Failed systemd units: 0 (systemctl --failed: 0 loaded units listed.)
  • New kernel errors (dmesg -l err,crit,alert,emerg): 0 (only the 3 pre-existing baseline J775c lines: snd-soc-macaudio speaker safety disable and 2 apple-dcp lines).

(Note: this clears Blocker 1 and the J775c §5 regression gate on 078f441ec922. The second-SoC CONFIG_RTC_SYSTOHC=y after-row, e.g. M1 Pro or T8112, still needs a run from another host.)

@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 re-review at verified live head 078f441ec922e0c45de1ae3cefade2244390638a (MERGEABLE, host-tests green).

Prior code/J775c blockers are cleanly resolved (#issuecomment-7118928986):

  • Commit 2 (macsmc-reboot.c nvmem_cell_set_u8()): Booted and exercised live on J775c — ac-power-mode.service exits 0/SUCCESS, and ac_power_mode sysfs round-trips (restore -> off -> always -> restore) produce the expected readbacks and PM setting 22 => 23 / 23 => 03 / 03 => 22 kernel logs.
  • Commit 1 (rtc-macsmc.c) & §5 regression gate: Verified live on J775c (rtc0, hwclock -r, rtcwake -m freeze -s 5 pass, 0 AVC / 0 failed units / 0 new kernel errors), alongside arm64 QEMU KUnit (macsmc-rtc 16/16, macsmc-reboot 13/13) and J775D s2idle evidence.

Approving to lift my CHANGES_REQUESTED block (0 code/policy/attribution/CI blockers). Note for the merge train: if a second-SoC CONFIG_RTC_SYSTOHC=y regression row (e.g. T8112 per @RyanTheTide's comment) is still required for shared macsmc drivers before merge, that row remains the only non-J775c item.

Assisted-by: LLM (Gemini 4 Neon)

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.

4 participants