Skip to content

Commit 8c111f1

Browse files
jtlaytonKAGA-KOKO
authored andcommitted
timekeeping: Don't use seqcount loop in ktime_mono_to_any() on 64-bit systems
ktime_mono_to_any() only fetches the offset inside the loop. This is a single word on 64-bit CPUs, and seqcount_read_begin() implies a full SMP barrier. Use READ_ONCE() to fetch the offset instead of doing a seqcount loop on 64-bit and add the matching WRITE_ONCE()'s to update the offsets in tk_set_wall_to_mono() and tk_update_sleep_time(). [ tglx: Get rid of the #ifdeffery ] Signed-off-by: Jeff Layton <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/all/[email protected]
1 parent b98b276 commit 8c111f1

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

kernel/time/timekeeping.c

+13-3
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,15 @@ static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
161161
WARN_ON_ONCE(tk->offs_real != timespec64_to_ktime(tmp));
162162
tk->wall_to_monotonic = wtm;
163163
set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
164-
tk->offs_real = timespec64_to_ktime(tmp);
165-
tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
164+
/* Paired with READ_ONCE() in ktime_mono_to_any() */
165+
WRITE_ONCE(tk->offs_real, timespec64_to_ktime(tmp));
166+
WRITE_ONCE(tk->offs_tai, ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0)));
166167
}
167168

168169
static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
169170
{
170-
tk->offs_boot = ktime_add(tk->offs_boot, delta);
171+
/* Paired with READ_ONCE() in ktime_mono_to_any() */
172+
WRITE_ONCE(tk->offs_boot, ktime_add(tk->offs_boot, delta));
171173
/*
172174
* Timespec representation for VDSO update to avoid 64bit division
173175
* on every update.
@@ -930,6 +932,14 @@ ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
930932
unsigned int seq;
931933
ktime_t tconv;
932934

935+
if (IS_ENABLED(CONFIG_64BIT)) {
936+
/*
937+
* Paired with WRITE_ONCE()s in tk_set_wall_to_mono() and
938+
* tk_update_sleep_time().
939+
*/
940+
return ktime_add(tmono, READ_ONCE(*offset));
941+
}
942+
933943
do {
934944
seq = read_seqcount_begin(&tk_core.seq);
935945
tconv = ktime_add(tmono, *offset);

0 commit comments

Comments
 (0)