mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@kernel.org>
To: John Stultz <jstultz@google.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	 Stephen Boyd <sboyd@kernel.org>
Cc: Vadim Fedorenko <vadim.fedorenko@linux.dev>,
	 linux-kernel@vger.kernel.org, Jeff Layton <jlayton@kernel.org>
Subject: [PATCH v3] timekeeping: don't use seqcount loop in ktime_mono_to_any() on 64-bit systems
Date: Tue, 10 Sep 2024 13:43:34 -0400	[thread overview]
Message-ID: <20240910-mgtime-v3-1-84406ed53fad@kernel.org> (raw)

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@linux.dev>
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
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@kernel.org

Changes in v2:
- drop the READ_ONCE
- clean up changelog
- Link to v1: https://lore.kernel.org/r/20240910-mgtime-v1-1-35fb64bd0af5@kernel.org
---
 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@kernel.org>


             reply	other threads:[~2024-09-10 17:43 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-10 17:43 Jeff Layton [this message]
2024-10-02 16:12 ` [tip: timers/core] timekeeping: Don't " tip-bot2 for Jeff Layton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240910-mgtime-v3-1-84406ed53fad@kernel.org \
    --to=jlayton@kernel.org \
    --cc=jstultz@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sboyd@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=vadim.fedorenko@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®