[PATCH v3] timekeeping: don't use seqcount loop in ktime_mono_to_any() on 64-bit systems

From: Jeff Layton
Date: Tue Sep 10 2024 - 13:45:13 EST


ktime_mono_to_any() only fetches the offset inside the loop. This is a
single word on 64-bit hosts, and seqcount_read_begin() implies a full
SMP barrier.

When BITS_PER_LONG == 64, use READ_ONCE to fetch the offset instead of
doing a seqcount loop. This means that we also need to use WRITE_ONCE to
update the offsets in tk_set_wall_to_mono() and tk_update_sleep_time().

Cc: Vadim Fedorenko <vadim.fedorenko@xxxxxxxxx>
Signed-off-by: Jeff Layton <jlayton@xxxxxxxxxx>
---
This one puts back the READ_ONCE and adds WRITE_ONCEs (as Thomas
suggested).
---
Changes in v3:
- add back the READ_ONCE, and use WRITE_ONCE to update the offsets
- Link to v2: https://lore.kernel.org/r/20240910-mgtime-v2-1-e96826ac56f0@xxxxxxxxxx

Changes in v2:
- drop the READ_ONCE
- clean up changelog
- Link to v1: https://lore.kernel.org/r/20240910-mgtime-v1-1-35fb64bd0af5@xxxxxxxxxx
---
kernel/time/timekeeping.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 99381e04a871..ec999a39d9cb 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -161,13 +161,15 @@ static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
WARN_ON_ONCE(tk->offs_real != timespec64_to_ktime(tmp));
tk->wall_to_monotonic = wtm;
set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
- tk->offs_real = timespec64_to_ktime(tmp);
- tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
+ /* paired with READ_ONCE in ktime_mono_to_any */
+ WRITE_ONCE(tk->offs_real, timespec64_to_ktime(tmp));
+ WRITE_ONCE(tk->offs_tai, ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0)));
}

static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
{
- tk->offs_boot = ktime_add(tk->offs_boot, delta);
+ /* paired with READ_ONCE in ktime_mono_to_any */
+ WRITE_ONCE(tk->offs_boot, ktime_add(tk->offs_boot, delta));
/*
* Timespec representation for VDSO update to avoid 64bit division
* on every update.
@@ -954,6 +956,15 @@ EXPORT_SYMBOL_GPL(ktime_get_coarse_with_floor_and_offset);
* @tmono: time to convert.
* @offs: which offset to use
*/
+#if BITS_PER_LONG == 64
+ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
+{
+ ktime_t *offset = offsets[offs];
+
+ /* paired with WRITE_ONCEs in tk_set_wall_to_mono and tk_update_sleep_time */
+ return ktime_add(tmono, READ_ONCE(*offset));
+}
+#else /* BITS_PER_LONG == 64 */
ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
{
ktime_t *offset = offsets[offs];
@@ -967,6 +978,7 @@ ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)

return tconv;
}
+#endif /* BITS_PER_LONG == 64 */
EXPORT_SYMBOL_GPL(ktime_mono_to_any);

/**

---
base-commit: a833754dba0fcc8984e8e86042a8877be70187d9
change-id: 20240910-mgtime-731eace7cca5

Best regards,
--
Jeff Layton <jlayton@xxxxxxxxxx>