Re: [PATCH] timekeeping: Use READ_ONCE/WRITE_ONCE() for ktime_sec to prevent tearing
From: John Stultz
Date: Thu Sep 03 2026 - 16:52:05 EST
On Thu, Sep 3, 2026 at 12:11 AM Thomas Weißschuh
<thomas.weissschuh@xxxxxxxxxxxxx> wrote:
>
> The timekeeper update path uses a bulk memcpy() to synchronize the
> timekeeper structure, which is not guaranteed to be atomic. This allows for
> torn reads in ktime_get_seconds() which bypasses the sequence counter
> protection for performance.
>
> To prevent reading a torn ktime_sec value, enforce atomic-like
> access by using WRITE_ONCE() for the critical field before the bulk
> memcpy() in timekeeping_update_from_shadow(). Correspondingly, use
> READ_ONCE() in ktime_get_seconds() to ensure a fresh, consistent load
> from memory.
>
> The same was done for xtime_sec and ktime_get_real_seconds() in commit
> d7fc133bf91f ("timekeeping: Use READ_ONCE/WRITE_ONCE() for xtime_sec to
> prevent tearing").
>
> Signed-off-by: Thomas Weißschuh (Schneider Electric) <thomas.weissschuh@xxxxxxxxxxxxx>
> ---
> Signed-off-by: Thomas Weißschuh <thomas.weissschuh@xxxxxxxxxxxxx>
Acked-by: John Stultz <jstultz@xxxxxxxxxx>