Skip to contentSkip to conversation

timekeeping: Janitorial updates

PATCH Applied Thomas Gleixner 3 participants 11 messages Apr 15, 2022 – May 2, 2022 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

cover timekeeping: Janitorial updates

The following series provides a few minor fixlets and consolidation:

- Add a missing data_race() annotation

- Ensure that the clock readout is always inlined

- Reduce the copy & pasta in the NMI safe accessors

Thanks,

tglx

---
 timekeeping.c |   24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
#1 lore raw

PATCH 1/3 timekeeping: Annotate ktime_get_boot_fast_ns() with data_race()

Accessing timekeeper::offset_boot in ktime_get_boot_fast_ns() is an intended data race as the reader side cannot synchronize with a writer and there is no space in struct tk_read_base of the NMI safe timekeeper.

Mark it so.

---
 kernel/time/timekeeping.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timekeeping.c +1 -1
@@ -528,7 +528,7 @@ u64 notrace ktime_get_boot_fast_ns(void)
 {
 	struct timekeeper *tk = &tk_core.timekeeper;
 
-	return (ktime_get_mono_fast_ns() + ktime_to_ns(tk->offs_boot));
+	return (ktime_get_mono_fast_ns() + ktime_to_ns(data_race(tk->offs_boot)));
 }
 EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns);
 
#2 lore raw

Re: [patch 1/3] timekeeping: Annotate ktime_get_boot_fast_ns() with data_race()

Quoted 5 lines · Thomas Gleixner

Accessing timekeeper::offset_boot in ktime_get_boot_fast_ns() is an intended data race as the reader side cannot synchronize with a writer and there is no space in struct tk_read_base of the NMI safe timekeeper.

Mark it so.

If offs_boot actually ever changed?

#3 lore raw

Re: [patch 1/3] timekeeping: Annotate ktime_get_boot_fast_ns() with data_race()

Quoted 8 lines · Peter Zijlstra
Quoted 5 lines · Thomas Gleixner

Accessing timekeeper::offset_boot in ktime_get_boot_fast_ns() is an intended data race as the reader side cannot synchronize with a writer and there is no space in struct tk_read_base of the NMI safe timekeeper.

Mark it so.

If offs_boot actually ever changed?

Yes, during resume to compensate for the time spent in suspend. So, yes the data_race() is more of documentary value.

Thanks,

tglx

#4 lore raw

[tip: timers/core] timekeeping: Annotate ktime_get_boot_fast_ns() with data_race()

In reply to Thomas Gleixner
The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     eff4849f928f2b90402907e06a6de1619cf16b1a
Gitweb:        https://git.kernel.org/tip/eff4849f928f2b90402907e06a6de1619cf16b1a
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Fri, 15 Apr 2022 11:19:35 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Mon, 02 May 2022 14:00:20 +02:00

timekeeping: Annotate ktime_get_boot_fast_ns() with data_race()

Accessing timekeeper::offset_boot in ktime_get_boot_fast_ns() is an intended data race as the reader side cannot synchronize with a writer and there is no space in struct tk_read_base of the NMI safe timekeeper.

Mark it so.

---
 kernel/time/timekeeping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timekeeping.c +1 -1
index 2c22023..3479804 100644
@@ -528,7 +528,7 @@ u64 notrace ktime_get_boot_fast_ns(void)
 {
 	struct timekeeper *tk = &tk_core.timekeeper;
 
-	return (ktime_get_mono_fast_ns() + ktime_to_ns(tk->offs_boot));
+	return (ktime_get_mono_fast_ns() + ktime_to_ns(data_race(tk->offs_boot)));
 }
 EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns);
 
#5 lore raw

PATCH 2/3 timekeeping: Mark tk_clock_read() __always_inline

In reply to Thomas Gleixner

Compilers can uninline this which makes the notrace annotation of the NMI safe accessors moot.

---
 kernel/time/timekeeping.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timekeeping.c +1 -1
@@ -186,7 +186,7 @@ static inline void tk_update_sleep_time(
  * a read of the fast-timekeeper tkrs (which is protected by its own locking
  * and update logic).
  */
-static inline u64 tk_clock_read(const struct tk_read_base *tkr)
+static __always_inline u64 tk_clock_read(const struct tk_read_base *tkr)
 {
 	struct clocksource *clock = READ_ONCE(tkr->clock);
 
#6 lore raw

Re: [patch 2/3] timekeeping: Mark tk_clock_read() __always_inline

Quoted 2 lines · Thomas Gleixner

Compilers can uninline this which makes the notrace annotation of the NMI safe accessors moot.

inline already implies notrace.

No objection to making it __always_inline, but this reason doesn't really work.

Quoted 18 lines
---
 kernel/time/timekeeping.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timekeeping.c +1 -1
@@ -186,7 +186,7 @@ static inline void tk_update_sleep_time(
  * a read of the fast-timekeeper tkrs (which is protected by its own locking
  * and update logic).
  */
-static inline u64 tk_clock_read(const struct tk_read_base *tkr)
+static __always_inline u64 tk_clock_read(const struct tk_read_base *tkr)
 {
 	struct clocksource *clock = READ_ONCE(tkr->clock);
 
#7 lore raw

Re: [patch 2/3] timekeeping: Mark tk_clock_read() __always_inline

Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Thomas Gleixner

Compilers can uninline this which makes the notrace annotation of the NMI safe accessors moot.

inline already implies notrace.

Bah. Confused myself vs. noinstr. We have too many constraints...

Quoted 2 lines

No objection to making it __always_inline, but this reason doesn't really work.

Let me come up with a more coherent argument.

Thanks,

tglx

#8 lore raw

PATCH 3/3 timekeeping: Consolidate fast timekeeper

In reply to Thomas Gleixner

Provide a inline function which replaces the copy & pasta.

---
 kernel/time/timekeeping.c |   20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
kernel/time/timekeeping.c +10 -10
@@ -429,6 +429,14 @@ static void update_fast_timekeeper(const
 	memcpy(base + 1, base, sizeof(*base));
 }
 
+static __always_inline u64 fast_tk_get_delta_ns(struct tk_read_base *tkr)
+{
+	u64 delta, cycles = tk_clock_read(tkr);
+
+	delta = clocksource_delta(cycles, tkr->cycle_last, tkr->mask);
+	return timekeeping_delta_to_ns(tkr, delta);
+}
+
 static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf)
 {
 	struct tk_read_base *tkr;
@@ -439,12 +447,7 @@ static __always_inline u64 __ktime_get_f
 		seq = raw_read_seqcount_latch(&tkf->seq);
 		tkr = tkf->base + (seq & 0x01);
 		now = ktime_to_ns(tkr->base);
-
-		now += timekeeping_delta_to_ns(tkr,
-				clocksource_delta(
-					tk_clock_read(tkr),
-					tkr->cycle_last,
-					tkr->mask));
+		now += fast_tk_get_delta_ns(tkr);
 	} while (read_seqcount_latch_retry(&tkf->seq, seq));
 
 	return now;
@@ -560,10 +563,7 @@ static __always_inline u64 __ktime_get_r
 		tkr = tkf->base + (seq & 0x01);
 		basem = ktime_to_ns(tkr->base);
 		baser = ktime_to_ns(tkr->base_real);
-
-		delta = timekeeping_delta_to_ns(tkr,
-				clocksource_delta(tk_clock_read(tkr),
-				tkr->cycle_last, tkr->mask));
+		delta = fast_tk_get_delta_ns(tkr);
 	} while (read_seqcount_latch_retry(&tkf->seq, seq));
 
 	if (mono)
#10 lore raw

[tip: timers/core] timekeeping: Consolidate fast timekeeper

In reply to Thomas Gleixner
The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     90be8d6c1f91e1e5121c219726524c91b52bfc20
Gitweb:        https://git.kernel.org/tip/90be8d6c1f91e1e5121c219726524c91b52bfc20
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Fri, 15 Apr 2022 11:19:38 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Mon, 02 May 2022 14:00:20 +02:00

timekeeping: Consolidate fast timekeeper

Provide a inline function which replaces the copy & pasta.

---
 kernel/time/timekeeping.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
kernel/time/timekeeping.c +10 -10
index 3479804..8895ff2 100644
@@ -429,6 +429,14 @@ static void update_fast_timekeeper(const struct tk_read_base *tkr,
 	memcpy(base + 1, base, sizeof(*base));
 }
 
+static __always_inline u64 fast_tk_get_delta_ns(struct tk_read_base *tkr)
+{
+	u64 delta, cycles = tk_clock_read(tkr);
+
+	delta = clocksource_delta(cycles, tkr->cycle_last, tkr->mask);
+	return timekeeping_delta_to_ns(tkr, delta);
+}
+
 static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf)
 {
 	struct tk_read_base *tkr;
@@ -439,12 +447,7 @@ static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf)
 		seq = raw_read_seqcount_latch(&tkf->seq);
 		tkr = tkf->base + (seq & 0x01);
 		now = ktime_to_ns(tkr->base);
-
-		now += timekeeping_delta_to_ns(tkr,
-				clocksource_delta(
-					tk_clock_read(tkr),
-					tkr->cycle_last,
-					tkr->mask));
+		now += fast_tk_get_delta_ns(tkr);
 	} while (read_seqcount_latch_retry(&tkf->seq, seq));
 
 	return now;
@@ -560,10 +563,7 @@ static __always_inline u64 __ktime_get_real_fast(struct tk_fast *tkf, u64 *mono)
 		tkr = tkf->base + (seq & 0x01);
 		basem = ktime_to_ns(tkr->base);
 		baser = ktime_to_ns(tkr->base_real);
-
-		delta = timekeeping_delta_to_ns(tkr,
-				clocksource_delta(tk_clock_read(tkr),
-				tkr->cycle_last, tkr->mask));
+		delta = fast_tk_get_delta_ns(tkr);
 	} while (read_seqcount_latch_retry(&tkf->seq, seq));
 
 	if (mono)