Skip to content

Commit ec5806b

Browse files
KAGA-KOKObwhacks
authored andcommitted
timekeeping: Provide hrtimer update function
This is a backport of f6c06ab 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> 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> [John Stultz: Backported to 3.2] Cc: Prarit Bhargava <prarit@redhat.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Linux Kernel <linux-kernel@vger.kernel.org> Signed-off-by: John Stultz <johnstul@us.ibm.com> Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
1 parent d6a2a04 commit ec5806b

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
@@ -1240,6 +1240,40 @@ void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
12401240
} while (read_seqretry(&xtime_lock, seq));
12411241
}
12421242

1243+
#ifdef CONFIG_HIGH_RES_TIMERS
1244+
/**
1245+
* ktime_get_update_offsets - hrtimer helper
1246+
* @real: pointer to storage for monotonic -> realtime offset
1247+
* @_boot: pointer to storage for monotonic -> boottime offset
1248+
*
1249+
* Returns current monotonic time and updates the offsets
1250+
* Called from hrtimer_interupt() or retrigger_next_event()
1251+
*/
1252+
ktime_t ktime_get_update_offsets(ktime_t *real, ktime_t *boot)
1253+
{
1254+
ktime_t now;
1255+
unsigned int seq;
1256+
u64 secs, nsecs;
1257+
1258+
do {
1259+
seq = read_seqbegin(&xtime_lock);
1260+
1261+
secs = xtime.tv_sec;
1262+
nsecs = xtime.tv_nsec;
1263+
nsecs += timekeeping_get_ns();
1264+
/* If arch requires, add in gettimeoffset() */
1265+
nsecs += arch_gettimeoffset();
1266+
1267+
*real = offs_real;
1268+
*boot = offs_boot;
1269+
} while (read_seqretry(&xtime_lock, seq));
1270+
1271+
now = ktime_add_ns(ktime_set(secs, 0), nsecs);
1272+
now = ktime_sub(now, *real);
1273+
return now;
1274+
}
1275+
#endif
1276+
12431277
/**
12441278
* ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
12451279
*/

0 commit comments

Comments
 (0)