Commit e3ff9c36 authored by Thomas Gleixner's avatar Thomas Gleixner

timekeeping: Repair ktime_get_coarse*() granularity

Jason reported that the coarse ktime based time getters advance only once
per second and not once per tick as advertised.

The code reads only the monotonic base time, which advances once per
second. The nanoseconds are accumulated on every tick in xtime_nsec up to
a second and the regular time getters take this nanoseconds offset into
account, but the ktime_get_coarse*() implementation fails to do so.

Add the accumulated xtime_nsec value to the monotonic base time to get the
proper per tick advancing coarse tinme.

Fixes: b9ff604c ("timekeeping: Add ktime_get_coarse_with_offset")
Reported-by: default avatarJason A. Donenfeld <>
Signed-off-by: default avatarThomas Gleixner <>
Tested-by: default avatarJason A. Donenfeld <>
Cc: Arnd Bergmann <>
Cc: Peter Zijlstra <>
Cc: Clemens Ladisch <>
Cc: Sultan Alsawaf <>
Cc: Waiman Long <>
parent 6cb3dd75
......@@ -808,17 +808,18 @@ ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs)
struct timekeeper *tk = &tk_core.timekeeper;
unsigned int seq;
ktime_t base, *offset = offsets[offs];
u64 nsecs;
do {
seq = read_seqcount_begin(&tk_core.seq);
base = ktime_add(tk->tkr_mono.base, *offset);
nsecs = tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
} while (read_seqcount_retry(&tk_core.seq, seq));
return base;
return base + nsecs;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment