Skip to content

Commit f6c06ab

Browse files
committed
timekeeping: Provide hrtimer update function
To finally fix the infamous leap second issue and other race windows caused by functions which change the offsets between the various time bases (CLOCK_MONOTONIC, CLOCK_REALTIME and CLOCK_BOOTTIME) we need a function which atomically gets the current monotonic time and updates the offsets of CLOCK_REALTIME and CLOCK_BOOTTIME with minimalistic overhead. The previous patch which provides ktime_t offsets allows us to make this function almost as cheap as ktime_get() which is going to be replaced in hrtimer_interrupt(). Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Ingo Molnar <mingo@kernel.org> Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Acked-by: Prarit Bhargava <prarit@redhat.com> Cc: stable@vger.kernel.org Signed-off-by: John Stultz <johnstul@us.ibm.com> Link: http://lkml.kernel.org/r/1341960205-56738-7-git-send-email-johnstul@us.ibm.com Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 196951e commit f6c06ab

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎include/linux/hrtimer.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ extern ktime_t ktime_get(void);
327327
extern ktime_t ktime_get_real(void);
328328
extern ktime_t ktime_get_boottime(void);
329329
extern ktime_t ktime_get_monotonic_offset(void);
330+
extern ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot);
330331

331332
DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
332333

‎kernel/time/timekeeping.c‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,40 @@ void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
12711271
} while (read_seqretry(&timekeeper.lock, seq));
12721272
}
12731273

1274+
#ifdef CONFIG_HIGH_RES_TIMERS
1275+
/**
1276+
* ktime_get_update_offsets - hrtimer helper
1277+
* @offs_real: pointer to storage for monotonic -> realtime offset
1278+
* @offs_boot: pointer to storage for monotonic -> boottime offset
1279+
*
1280+
* Returns current monotonic time and updates the offsets
1281+
* Called from hrtimer_interupt() or retrigger_next_event()
1282+
*/
1283+
ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot)
1284+
{
1285+
ktime_t now;
1286+
unsigned int seq;
1287+
u64 secs, nsecs;
1288+
1289+
do {
1290+
seq = read_seqbegin(&timekeeper.lock);
1291+
1292+
secs = timekeeper.xtime.tv_sec;
1293+
nsecs = timekeeper.xtime.tv_nsec;
1294+
nsecs += timekeeping_get_ns();
1295+
/* If arch requires, add in gettimeoffset() */
1296+
nsecs += arch_gettimeoffset();
1297+
1298+
*offs_real = timekeeper.offs_real;
1299+
*offs_boot = timekeeper.offs_boot;
1300+
} while (read_seqretry(&timekeeper.lock, seq));
1301+
1302+
now = ktime_add_ns(ktime_set(secs, 0), nsecs);
1303+
now = ktime_sub(now, *offs_real);
1304+
return now;
1305+
}
1306+
#endif
1307+
12741308
/**
12751309
* ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
12761310
*/

0 commit comments

Comments
 (0)