[PATCH RFC] timekeeping: don't use seqcount loop in ktime_mono_to_any on 64-bit arch
From: Jeff Layton
Date: Tue Sep 10 2024 - 07:17:44 EST
ktime_mono_to_any only fetches the offset inside the loop. This is a
single word on 64-bit arch, and seqcount_read_begin implies a full SMP
barrier. While we do want to use the latest offset value available, a
full seqcount loop is overkill on 64-bit, where there is no possibility
of torn reads. Just do a READ_ONCE for that and don't bother with the
seqcount.
Cc: Vadim Fedorenko <vadim.fedorenko@xxxxxxxxx>
Signed-off-by: Jeff Layton <jlayton@xxxxxxxxxx>
---
kernel/time/timekeeping.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 5391e4167d60..0693f44e064e 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -924,6 +924,15 @@ EXPORT_SYMBOL_GPL(ktime_get_coarse_with_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];
+
+ return ktime_add(tmono, READ_ONCE(*offset));
+}
+EXPORT_SYMBOL_GPL(ktime_mono_to_any);
+#else /* BITS_PER_LONG == 64 */
ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
{
ktime_t *offset = offsets[offs];
@@ -938,6 +947,7 @@ ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
return tconv;
}
EXPORT_SYMBOL_GPL(ktime_mono_to_any);
+#endif /* BITS_PER_LONG == 64 */
/**
* ktime_get_raw - Returns the raw monotonic time in ktime_t format
---
base-commit: 79d4f9eb4a3c4c68736d500025423e96e212b13d
change-id: 20240910-mgtime-731eace7cca5
Best regards,
--
Jeff Layton <jlayton@xxxxxxxxxx>