Skip to content

rtc: macsmc: don't set the clock to 1970 from an unset offset; store NTP time in the RTC - #482

Open
ilyasakin wants to merge 3 commits into
omacom:integration/aurorafrom
ilyasakin:rtc-1970
Open

ilyasakin wants to merge 3 commits into
omacom:integration/aurorafrom
ilyasakin:rtc-1970

Conversation

@ilyasakin

@ilyasakin ilyasakin commented Nov 15, 2026 •

Copy link
Copy Markdown

What this changes and why

After the PMU on my J514S (MacBook Pro 14", M3 Pro) lost its RTC offset, every boot set the clock to 1970. TLS then failed in the window before NTP, and tailscaled, which starts in that window, stayed logged out until it was restarted.

What happened:

systemd[1]: System time advanced to timestamp on /var/lib/systemd/timesync/clock: Sun 2026-11-15 13:04:19 +03
macsmc-rtc macsmc-rtc: setting system clock to 1970-01-04T09:09:21 UTC (292161)
tailscaled: Received error: fetch control key: Get "https://controlplane.tailscale.com/key?v=148": x509: certificate has expired or is not yet valid: current time 1970-01-04T11:09:28+02

The T6030 offset cell (rtc-offset@2100, 6 bytes of 32768 Hz ticks) read all zero, while the counter read about three days. Two things combined, plus a bug found in review:

  1. Patch 1 (rtc-macsmc), found in review: a successful tick-offset write returned the byte count (6) instead of 0, so with RTC_SYSTOHC sync_hw_clock() retried at once: 270 writes in 30 s on a J514S. It now returns 0, as the seconds format does. With the fix, a 25-minute rtc:rtc_set_time trace shows two writes, both returning 0, 658 s apart.
  2. Patch 2 (rtc-macsmc): an offset of zero in the tick format was taken for a valid time: the bare counter, i.e. 1970.
    • macsmc-rtc probes late, after systemd has set the clock from its saved timestamp, so RTC_HCTOSYS put the clock back to 1970.
    • The offset-in-seconds format (t8140) already returns -EINVAL for a zero offset. This adds the same check to the tick format.
  3. Patch 3 (asahi.config): nothing ever wrote the RTC back.
    • The Omarchy config disables RTC_SYSTOHC, and systemd-timesyncd does not write the RTC itself.
    • So the RTC stayed at 1970 on every boot until hwclock --systohc.
    • This enables RTC_SYSTOHC, which is upstream's default and is on in Omarchy's other kernels (linux-omarchy, linux-omarchy-aarch64).
    • The kernel then stores NTP time in rtc0 about every 11 minutes.

What was tested

  • Mac model and chip: J514S, MacBook Pro 14" M3 Pro (T6030), 11-core.
  • Was a kernel built from THIS commit booted?
    • Yes: 47e98d0b7 was built with the omarchy-pkgs linux-aurora recipe, pointed at the commit with tools/aurora-ci/prepare-recipe.py, as 7.2.11.aurora16-1.2. It was installed and booted.
    • The package's config has CONFIG_RTC_SYSTOHC=y.
  • What I ran and what I saw:
    • I recreated the failure by writing six zero bytes to the PMU's rtc-offset@2100 through the spmi_nvmem nvmem file. Before the reboot, rtc0 read 1970-01-04.

    • Then I rebooted into the patched kernel:

      [57.9] systemd[1]: System time advanced to timestamp on /var/lib/systemd/timesync/clock: Sun 2026-11-15 15:00:13 +03
      [58.4] macsmc-rtc macsmc-rtc: registered as rtc0
      [58.4] macsmc-rtc macsmc-rtc: hctosys: unable to read the hardware clock
      [65.8] tailscaled: Switching ipn state Starting -> Running (WantRunning=true, nm=true)
      [65.8] tailscaled: magicsock: derp-26 connected; connGen=1
      [88.8] systemd-timesyncd: Contacted time server ...
      
    • The clock was not set back.

    • tailscaled logged in on the first try, with no x509 errors.

    • Four minutes after boot, rtc0 read the correct UTC time again (2026-11-15 12:04:33), written by the kernel after NTP synced.

  • Before these patches, on the same machine and kernel base:
    • the clock was set to 1970 on every boot;
    • tailscaled needed a restart;
    • timedatectl showed RTCTimeUSec=Sun 1970-01-04 even while NTP-synchronized.
  • Build: make W=1 drivers/rtc/rtc-macsmc.o (clang) is clean. checkpatch is clean apart from the trailers and the quoted log line.

What was NOT tested

  • What reset the PMU's offset in the first place. It was not done by Linux on purpose. The counter also restarted about three days before, which suggests the PMU lost its RTC state, for example through a full drain or an SMC reset.
  • Other Macs. Both patches apply to every Mac that uses this driver's tick-format offset and this config fragment. I tested only the J514S.
  • Interaction with macOS on the same machine. It was not booted after the RTC was rewritten by Linux. It reads and writes the same offset, and hwclock --systohc has always written it there.

Risk

  • Patch 2: this only changes behaviour when the stored offset is exactly zero. A real offset is the time since 1970 minus the counter, so it is never zero. In that case hwclock and RTC_HCTOSYS now report an invalid time instead of 1970.
  • Patch 3: the kernel writes the RTC every ~11 minutes while NTP reports sync. That is the upstream default.

AI assistance

  • I used an AI tool or agent to write or draft part of this change: Claude (claude-opus-5-5), via Claude Code. It found the cause, wrote the patches and this text, with me running the machine and reviewing.
  • A human has read the whole diff and takes responsibility for it.

🤖 Generated with Claude Code

@scottjones scottjones left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @ilyasakin, this is a really clear write-up. The zero-offset check is right and matches what the t8140 path already does, and the J514S repro and testing make it easy to follow.

One blocking thing, in a line this PR doesn't touch but that patch 2 now exercises. In the tick format, macsmc_rtc_set_time returns nvmem_cell_write()'s result directly (rtc-macsmc.c:155), and on success that's the byte count, 6, not 0. rtc_set_time passes it through, so with RTC_SYSTOHC=y, sync_hw_clock sees a nonzero result and re-arms every ~2 s instead of every 11 minutes, on every M1-M3 Mac. Nothing is logged, so your J514S run wouldn't have shown it.

The fix is to do what the seconds-format path at :149-150 already does:

	ret = nvmem_cell_write(rtc->rtc_offset, &off, RTC_BYTES);
	return ret < 0 ? ret : 0;

Could you add that, and confirm with the rtc:rtc_set_time tracepoint that writes land about every 11 minutes?

The same line is upstream too (torvalds and AsahiLinux rtc-macsmc.c), and Fedora Asahi ships SYSTOHC=y, so it's probably worth reporting there as well.

A couple of small notes, not blockers:

  • M4 (t8132), t8142-j813 and t8152-j873g have no RTC node, so SYSTOHC just gets -ENODEV there. That's harmless.
  • macOS dual boot on M3 hasn't been tested after Linux rewrites the RTC. Worth one boot if you can.

Happy to approve with the fix.

ilyasakin and others added 3 commits November 15, 2026 22:45
macsmc_rtc_set_time() returns nvmem_cell_write()'s result in the
tick-offset format, which on success is the number of bytes written,
6. rtc_set_time() passes that on, so callers take a successful write
for a failure. With RTC_SYSTOHC, sync_hw_clock() retries at once: on a
J514S (MacBook Pro 14", M3 Pro), the rtc:rtc_set_time tracepoint showed
270 writes in 30 seconds, each returning 6, instead of one every 11
minutes.

Return 0 on success, as the offset-in-seconds format already does.
Scott Jones found this in the review of the RTC_SYSTOHC change.

Link: omacom#482
Assisted-by: Claude:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Ilyas Akin <[email protected]>
With the offset kept in RTC ticks, an offset of zero is read as a valid
time: the bare counter, the time since the PMU powered up. After the PMU
lost its RTC state on a J514S (MacBook Pro 14", M3 Pro), the offset read
as zero and the counter as about three days, so the RTC reported
1970-01-04. macsmc-rtc probes late, after systemd has set the clock from
its last saved timestamp, and RTC_HCTOSYS then set the system clock back
to 1970:

  macsmc-rtc macsmc-rtc: setting system clock to 1970-01-04T09:09:21 UTC (292161)

Until NTP corrected it about 30 seconds later, TLS failed, and
tailscaled, which had started in that window, stayed logged out
("x509: certificate has expired or is not yet valid: current time
1970-01-04") until it was restarted.

Return -EINVAL for a zero offset, as the offset-in-seconds format
already does, so that the RTC core does not take it for the time.

Assisted-by: Claude:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Ilyas Akin <[email protected]>
Without RTC_SYSTOHC nothing writes the RTC back once NTP has set the
clock: systemd-timesyncd does not, and the Omarchy kernel configuration
disables it. An RTC that lost its time therefore stays wrong across
every boot, until macOS or hwclock sets it. On a J514S whose PMU had
lost its RTC offset, every boot started at 1970 until 'hwclock
--systohc' stored it again.

Enable it, as upstream's default and Omarchy's other kernels do, so the
kernel stores the NTP time in rtc0 about every 11 minutes.

Assisted-by: Claude:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Ilyas Akin <[email protected]>
@ilyasakin

Copy link
Copy Markdown
Author

Thanks, good catch. It's worse than every ~2 s on this machine. With RTC_SYSTOHC=y and the unfixed driver, the rtc:rtc_set_time tracepoint showed 270 writes in 30 s, each returning 6.

I added the fix as a separate first commit (rtc: macsmc: Return 0 from a successful tick-offset write), before the commit that enables SYSTOHC, so no point in the series has the retry loop. It's the line you suggested.

Verified on the J514S with the fixed module and NTP synchronized, tracing rtc:rtc_set_time for 25 minutes:

743.156411: rtc_set_time: UTC (1794772866) (0)
1401.154591: rtc_set_time: UTC (1794773524) (0)

That's two writes, both returning 0, 658 s apart.

I'll report the return value upstream as well. macOS dual boot after Linux rewrites the RTC is still untested; I'll do one boot when I can and report back.

@h43nz

h43nz commented Nov 16, 2026

Copy link
Copy Markdown

Same issue on a MacBook Pro 14" M1 Pro (J314s / T6000), linux-aurora 7.2.11-16-1-ARCH, so it isn't limited to T6030. Here the offset cell is rtc-offset@1411; before the fix, /sys/class/rtc/rtc0 read 1970-01-02.

Boot log (monotonic):

[    0.313745] systemd[1]: System time advanced to built-in epoch: Tue 2026-11-10 14:39:46 UTC
[   11.245033] platform macsmc-rtc: deferred probe pending: platform: wait for supplier /soc/spmi@2920a1300/pmic@f/nvmem-layout/rtc-offset@1411
[   72.135171] systemd[1]: System time advanced to timestamp on /var/lib/systemd/timesync/clock: Sun 2026-11-15 00:04:24 CET
[   72.693852] macsmc-rtc macsmc-rtc: setting system clock to 1970-01-02T10:28:22 UTC (124102)
[  103.235921] systemd-timesyncd[641]: Initial clock synchronization to Mon 2026-11-16 08:59:48.359858 CET.

Side effects in the ~30 s window before NTP: Chromium web apps started in that window (Teams, Outlook) obtained Microsoft 365 tokens/cookies with expiry times based on the wrong clock and required a fresh sign-in after the jump, and files/sockets created then carry 1970 timestamps.

Workaround that helped here: after NTP sync, a one-time hwclock --systohc --utc wrote the offset; /sys/class/rtc/rtc0 then matched system time.

Update after a reboot (warm reboot, no macOS boot in between): the offset persisted. No more 1970:

[   ...] macsmc-rtc macsmc-rtc: registered as rtc0
[   ...] macsmc-rtc macsmc-rtc: setting system clock to 2026-11-16T10:24:52 UTC (1794824692)

/sys/class/rtc/rtc0 matches system time, and timesyncd's offset after sync is about 7 ms. I haven't tested a full power-off cold boot or a macOS boot in between yet; I'll add a note if either changes anything.

@h43nz

h43nz commented Nov 16, 2026

Copy link
Copy Markdown

Same issue on a MacBook Pro 14" M1 Pro (J314s / T6000), linux-aurora 7.2.11-16-1-ARCH, so it isn't limited to T6030. Here the offset cell is rtc-offset@1411; before the fix, /sys/class/rtc/rtc0 read 1970-01-02.

Boot log (monotonic):

[    0.313745] systemd[1]: System time advanced to built-in epoch: Tue 2026-11-10 14:39:46 UTC
[   11.245033] platform macsmc-rtc: deferred probe pending: platform: wait for supplier /soc/spmi@2920a1300/pmic@f/nvmem-layout/rtc-offset@1411
[   72.135171] systemd[1]: System time advanced to timestamp on /var/lib/systemd/timesync/clock: Sun 2026-11-15 00:04:24 CET
[   72.693852] macsmc-rtc macsmc-rtc: setting system clock to 1970-01-02T10:28:22 UTC (124102)
[  103.235921] systemd-timesyncd[641]: Initial clock synchronization to Mon 2026-11-16 08:59:48.359858 CET.

Side effects in the ~30 s window before NTP: Chromium web apps started in that window (Teams, Outlook) obtained Microsoft 365 tokens/cookies with expiry times based on the wrong clock and required a fresh sign-in after the jump, and files/sockets created then carry 1970 timestamps.

Workaround that helped here: after NTP sync, a one-time hwclock --systohc --utc wrote the offset; /sys/class/rtc/rtc0 then matched system time. It survived a reboot (Linux to Linux, macOS not booted in between):

macsmc-rtc macsmc-rtc: setting system clock to 2026-11-16T10:24:52 UTC (1794824692)

So on this machine the offset cell is writable and persists. Without RTC_SYSTOHC (patch 3) nothing writes it on its own, so a fresh install or a lost offset will hit this again.

@DerAlbertCom

Copy link
Copy Markdown

One more board, plus a power-cycle result for the "survives the next reboot after the manual write"
item. MacBook Air 13" M2 (apple,j413 / Mac14,2), linux-aurora 7.2.11.aurora14-1, LUKS + btrfs,
rtc-offset@f900 under pmic@e.

After hwclock --systohc --utc (since_epoch 19621 → 1795014842), full power-off for a minute, then:

[   15.606136] macsmc-rtc macsmc-rtc: registered as rtc0
[   15.606237] macsmc-rtc macsmc-rtc: setting system clock to 2026-11-18T15:23:09 UTC (1795015389)

Offset survived, clock correct at bind, 0 journal lines stamped 1970 that boot.

Caveat: a minute of shutdown leaves the PMU battery-backed, so this does not cover a full battery
drain or SMC reset — the case that actually loses the offset. Still open: macOS boot after the write.

Side note for #782: the gap between PID 1's restore and the rtc0 bind was 448 ms with a 114 s initrd
and 457 ms with a 15 s one, so it does not depend on initrd duration.

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