Skip to content

Commit 5a13fc3

Browse files
committed
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull timekeeping fix from Thomas Gleixner: "A single fix for a regression caused by the generic VDSO implementation where a math overflow causes CLOCK_BOOTTIME to become a random number generator" * 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: timekeeping/vsyscall: Prevent math overflow in BOOTTIME update
2 parents 8a04c2e + b99328a commit 5a13fc3

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

include/linux/timekeeper_internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ struct tk_read_base {
5757
* @cs_was_changed_seq: The sequence number of clocksource change events
5858
* @next_leap_ktime: CLOCK_MONOTONIC time value of a pending leap-second
5959
* @raw_sec: CLOCK_MONOTONIC_RAW time in seconds
60+
* @monotonic_to_boot: CLOCK_MONOTONIC to CLOCK_BOOTTIME offset
6061
* @cycle_interval: Number of clock cycles in one NTP interval
6162
* @xtime_interval: Number of clock shifted nano seconds in one NTP
6263
* interval.
@@ -84,6 +85,9 @@ struct tk_read_base {
8485
*
8586
* wall_to_monotonic is no longer the boot time, getboottime must be
8687
* used instead.
88+
*
89+
* @monotonic_to_boottime is a timespec64 representation of @offs_boot to
90+
* accelerate the VDSO update for CLOCK_BOOTTIME.
8791
*/
8892
struct timekeeper {
8993
struct tk_read_base tkr_mono;
@@ -99,6 +103,7 @@ struct timekeeper {
99103
u8 cs_was_changed_seq;
100104
ktime_t next_leap_ktime;
101105
u64 raw_sec;
106+
struct timespec64 monotonic_to_boot;
102107

103108
/* The following members are for timekeeping internal use */
104109
u64 cycle_interval;

kernel/time/timekeeping.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
146146
static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
147147
{
148148
tk->offs_boot = ktime_add(tk->offs_boot, delta);
149+
/*
150+
* Timespec representation for VDSO update to avoid 64bit division
151+
* on every update.
152+
*/
153+
tk->monotonic_to_boot = ktime_to_timespec64(tk->offs_boot);
149154
}
150155

151156
/*

kernel/time/vsyscall.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static inline void update_vdso_data(struct vdso_data *vdata,
1717
struct timekeeper *tk)
1818
{
1919
struct vdso_timestamp *vdso_ts;
20-
u64 nsec;
20+
u64 nsec, sec;
2121

2222
vdata[CS_HRES_COARSE].cycle_last = tk->tkr_mono.cycle_last;
2323
vdata[CS_HRES_COARSE].mask = tk->tkr_mono.mask;
@@ -45,23 +45,27 @@ static inline void update_vdso_data(struct vdso_data *vdata,
4545
}
4646
vdso_ts->nsec = nsec;
4747

48-
/* CLOCK_MONOTONIC_RAW */
49-
vdso_ts = &vdata[CS_RAW].basetime[CLOCK_MONOTONIC_RAW];
50-
vdso_ts->sec = tk->raw_sec;
51-
vdso_ts->nsec = tk->tkr_raw.xtime_nsec;
48+
/* Copy MONOTONIC time for BOOTTIME */
49+
sec = vdso_ts->sec;
50+
/* Add the boot offset */
51+
sec += tk->monotonic_to_boot.tv_sec;
52+
nsec += (u64)tk->monotonic_to_boot.tv_nsec << tk->tkr_mono.shift;
5253

5354
/* CLOCK_BOOTTIME */
5455
vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_BOOTTIME];
55-
vdso_ts->sec = tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
56-
nsec = tk->tkr_mono.xtime_nsec;
57-
nsec += ((u64)(tk->wall_to_monotonic.tv_nsec +
58-
ktime_to_ns(tk->offs_boot)) << tk->tkr_mono.shift);
56+
vdso_ts->sec = sec;
57+
5958
while (nsec >= (((u64)NSEC_PER_SEC) << tk->tkr_mono.shift)) {
6059
nsec -= (((u64)NSEC_PER_SEC) << tk->tkr_mono.shift);
6160
vdso_ts->sec++;
6261
}
6362
vdso_ts->nsec = nsec;
6463

64+
/* CLOCK_MONOTONIC_RAW */
65+
vdso_ts = &vdata[CS_RAW].basetime[CLOCK_MONOTONIC_RAW];
66+
vdso_ts->sec = tk->raw_sec;
67+
vdso_ts->nsec = tk->tkr_raw.xtime_nsec;
68+
6569
/* CLOCK_TAI */
6670
vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_TAI];
6771
vdso_ts->sec = tk->xtime_sec + (s64)tk->tai_offset;

0 commit comments

Comments
 (0)