History

timekeeping: Use READ_ONCE/WRITE_ONCE() for xtime_sec to prevent tearing

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_real_seconds() on 64-bit systems, where the sequence counter protection is bypassed for performance. To prevent reading a torn 64-bit xtime_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_real_seconds() to ensure a fresh, consistent load from memory. [ tglx: Format changelog and add comment ]

Dennis Moshegov committed 3 months ago

Commit d7fc1331 parent ecc330e1 file · +7 −3Browse files

Showing the changes to kernel (1 of 1 file). Show all

Unified · Split

kernel/time/timekeeping.c+7−3
@@ -858,7 +858,11 @@ static void timekeeping_update_from_shadow(struct tk_data *tkd, unsigned int act · 857 unmodified lines
858858 * the downside that the reader side does not longer benefit from
859859 * the cacheline optimized data layout of the timekeeper and requires
860860 * another indirection.
861+ *
862+ * Write xtime_sec first so that even if the memcpy() tears the store
863+ * data integrity is provided for ktime_get_real_seconds().
861864 */
865+ WRITE_ONCE(tkd->timekeeper.xtime_sec, tk->xtime_sec);
862866 memcpy(&tkd->timekeeper, tk, sizeof(*tk));
863867 write_seqcount_end(&tkd->seq);
864868 }
@@ -1186,11 +1190,11 @@ time64_t ktime_get_real_seconds(void) · 321 unmodified lines
11861190 unsigned int seq;
11871191
11881192 if (IS_ENABLED(CONFIG_64BIT))
1189− return tk->xtime_sec;
1193+ return READ_ONCE(tk->xtime_sec);
11901194
11911195 do {
11921196 seq = read_seqcount_begin(&tk_core.seq);
1193− seconds = tk->xtime_sec;
1197+ seconds = READ_ONCE(tk->xtime_sec);
11941198
11951199 } while (read_seqcount_retry(&tk_core.seq, seq));
11961200
@@ -1212,7 +1216,7 @@ noinstr time64_t __ktime_get_real_seconds(void) · 15 unmodified lines
12121216 {
12131217 struct timekeeper *tk = &tk_core.timekeeper;
12141218
1215− return tk->xtime_sec;
1219+ return READ_ONCE(tk->xtime_sec);
12161220 }
12171221
12181222 static inline u64 tk_clock_read_snapshot(const struct tk_read_base *tkr,