Skip to contentSkip to conversation

timekeeping: don't use seqcount loop in ktime_mono_to_any on 64-bit systems

PATCH Superseded Jeff Layton 2 participants 2 messages Sep 10, 2024 Timekeeping, clocksource core, NTP, alarmtimer

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 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 hosts, and seqcount_read_begin implies a full SMP barrier.

When BITS_PER_LONG == 64, just do a simple ktime_add instead as there is no possibility of getting a torn offset value.

Cc Vadim Fedorenko <[email protected]>

---
Thanks for the review so far, Thomas! Hopefully this looks better. Disregard my earlier question about making this a static inline too. That would require making offsets[] a global symbol, which I don't think we want to do.
---
Changes in v2:
- drop the READ_ONCE
- clean up changelog
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 kernel/time/timekeeping.c | 9 +++++++++
 1 file changed, 9 insertions(+)
kernel/time/timekeeping.c +9 -0
index 3f524d43d685..a3872f087fbc 100644
@@ -946,6 +946,14 @@ EXPORT_SYMBOL_GPL(ktime_get_coarse_with_offset_and_floor);
  * @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, *offset);
+}
+#else /* BITS_PER_LONG == 64 */
 ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
 {
 	ktime_t *offset = offsets[offs];
@@ -959,6 +967,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: dc8f9bbf6ceb160c7fc5e1a03c5a9318cb61bbd7
change-id: 20240910-mgtime-731eace7cca5

Best regards,

Signature · 1 line
Jeff Layton <[email protected]>
#1 lore raw
Quoted 7 lines · Jeff Layton

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, just do a simple ktime_add instead as there is no possibility of getting a torn offset value.

Quoted 7 lines
Cc Vadim Fedorenko <[email protected]>

---
Thanks for the review so far, Thomas! Hopefully this looks better. Disregard my earlier question about making this a static inline too. That would require making offsets[] a global symbol, which I don't think we want to do.

This still fails to address these:

Quoted 6 lines

Please describe functions with foo() and not foo.

Which will make KCSAN complain ...

So yes, READ_ONCE() is the correct thing todo, but then we want to have the counterpart at the write sides.

KCSAN requires this to be annotated and it's also a good documentation that this is a intentional unprotected access.

Thanks,

tglx