aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/time/timekeeping.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/time/timekeeping.c')
-rw-r--r--kernel/time/timekeeping.c487
1 files changed, 261 insertions, 226 deletions
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 3447cfaf11e7..f045cc50832d 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -24,32 +24,32 @@
24/* Structure holding internal timekeeping values. */ 24/* Structure holding internal timekeeping values. */
25struct timekeeper { 25struct timekeeper {
26 /* Current clocksource used for timekeeping. */ 26 /* Current clocksource used for timekeeping. */
27 struct clocksource *clock; 27 struct clocksource *clock;
28 /* NTP adjusted clock multiplier */ 28 /* NTP adjusted clock multiplier */
29 u32 mult; 29 u32 mult;
30 /* The shift value of the current clocksource. */ 30 /* The shift value of the current clocksource. */
31 int shift; 31 u32 shift;
32
33 /* Number of clock cycles in one NTP interval. */ 32 /* Number of clock cycles in one NTP interval. */
34 cycle_t cycle_interval; 33 cycle_t cycle_interval;
35 /* Number of clock shifted nano seconds in one NTP interval. */ 34 /* Number of clock shifted nano seconds in one NTP interval. */
36 u64 xtime_interval; 35 u64 xtime_interval;
37 /* shifted nano seconds left over when rounding cycle_interval */ 36 /* shifted nano seconds left over when rounding cycle_interval */
38 s64 xtime_remainder; 37 s64 xtime_remainder;
39 /* Raw nano seconds accumulated per NTP interval. */ 38 /* Raw nano seconds accumulated per NTP interval. */
40 u32 raw_interval; 39 u32 raw_interval;
40
41 /* Current CLOCK_REALTIME time in seconds */
42 u64 xtime_sec;
43 /* Clock shifted nano seconds */
44 u64 xtime_nsec;
41 45
42 /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
43 u64 xtime_nsec;
44 /* Difference between accumulated time and NTP time in ntp 46 /* Difference between accumulated time and NTP time in ntp
45 * shifted nano seconds. */ 47 * shifted nano seconds. */
46 s64 ntp_error; 48 s64 ntp_error;
47 /* Shift conversion between clock shifted nano seconds and 49 /* Shift conversion between clock shifted nano seconds and
48 * ntp shifted nano seconds. */ 50 * ntp shifted nano seconds. */
49 int ntp_error_shift; 51 u32 ntp_error_shift;
50 52
51 /* The current time */
52 struct timespec xtime;
53 /* 53 /*
54 * wall_to_monotonic is what we need to add to xtime (or xtime corrected 54 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
55 * for sub jiffie times) to get to monotonic time. Monotonic is pegged 55 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
@@ -64,20 +64,17 @@ struct timekeeper {
64 * - wall_to_monotonic is no longer the boot time, getboottime must be 64 * - wall_to_monotonic is no longer the boot time, getboottime must be
65 * used instead. 65 * used instead.
66 */ 66 */
67 struct timespec wall_to_monotonic; 67 struct timespec wall_to_monotonic;
68 /* time spent in suspend */ 68 /* time spent in suspend */
69 struct timespec total_sleep_time; 69 struct timespec total_sleep_time;
70 /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */ 70 /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */
71 struct timespec raw_time; 71 struct timespec raw_time;
72
73 /* Offset clock monotonic -> clock realtime */ 72 /* Offset clock monotonic -> clock realtime */
74 ktime_t offs_real; 73 ktime_t offs_real;
75
76 /* Offset clock monotonic -> clock boottime */ 74 /* Offset clock monotonic -> clock boottime */
77 ktime_t offs_boot; 75 ktime_t offs_boot;
78
79 /* Seqlock for all timekeeper values */ 76 /* Seqlock for all timekeeper values */
80 seqlock_t lock; 77 seqlock_t lock;
81}; 78};
82 79
83static struct timekeeper timekeeper; 80static struct timekeeper timekeeper;
@@ -88,11 +85,37 @@ static struct timekeeper timekeeper;
88 */ 85 */
89__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock); 86__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
90 87
91
92/* flag for if timekeeping is suspended */ 88/* flag for if timekeeping is suspended */
93int __read_mostly timekeeping_suspended; 89int __read_mostly timekeeping_suspended;
94 90
91static inline void tk_normalize_xtime(struct timekeeper *tk)
92{
93 while (tk->xtime_nsec >= ((u64)NSEC_PER_SEC << tk->shift)) {
94 tk->xtime_nsec -= (u64)NSEC_PER_SEC << tk->shift;
95 tk->xtime_sec++;
96 }
97}
95 98
99static struct timespec tk_xtime(struct timekeeper *tk)
100{
101 struct timespec ts;
102
103 ts.tv_sec = tk->xtime_sec;
104 ts.tv_nsec = (long)(tk->xtime_nsec >> tk->shift);
105 return ts;
106}
107
108static void tk_set_xtime(struct timekeeper *tk, const struct timespec *ts)
109{
110 tk->xtime_sec = ts->tv_sec;
111 tk->xtime_nsec = ts->tv_nsec << tk->shift;
112}
113
114static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts)
115{
116 tk->xtime_sec += ts->tv_sec;
117 tk->xtime_nsec += ts->tv_nsec << tk->shift;
118}
96 119
97/** 120/**
98 * timekeeper_setup_internals - Set up internals to use clocksource clock. 121 * timekeeper_setup_internals - Set up internals to use clocksource clock.
@@ -104,12 +127,14 @@ int __read_mostly timekeeping_suspended;
104 * 127 *
105 * Unless you're the timekeeping code, you should not be using this! 128 * Unless you're the timekeeping code, you should not be using this!
106 */ 129 */
107static void timekeeper_setup_internals(struct clocksource *clock) 130static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
108{ 131{
109 cycle_t interval; 132 cycle_t interval;
110 u64 tmp, ntpinterval; 133 u64 tmp, ntpinterval;
134 struct clocksource *old_clock;
111 135
112 timekeeper.clock = clock; 136 old_clock = tk->clock;
137 tk->clock = clock;
113 clock->cycle_last = clock->read(clock); 138 clock->cycle_last = clock->read(clock);
114 139
115 /* Do the ns -> cycle conversion first, using original mult */ 140 /* Do the ns -> cycle conversion first, using original mult */
@@ -122,80 +147,96 @@ static void timekeeper_setup_internals(struct clocksource *clock)
122 tmp = 1; 147 tmp = 1;
123 148
124 interval = (cycle_t) tmp; 149 interval = (cycle_t) tmp;
125 timekeeper.cycle_interval = interval; 150 tk->cycle_interval = interval;
126 151
127 /* Go back from cycles -> shifted ns */ 152 /* Go back from cycles -> shifted ns */
128 timekeeper.xtime_interval = (u64) interval * clock->mult; 153 tk->xtime_interval = (u64) interval * clock->mult;
129 timekeeper.xtime_remainder = ntpinterval - timekeeper.xtime_interval; 154 tk->xtime_remainder = ntpinterval - tk->xtime_interval;
130 timekeeper.raw_interval = 155 tk->raw_interval =
131 ((u64) interval * clock->mult) >> clock->shift; 156 ((u64) interval * clock->mult) >> clock->shift;
132 157
133 timekeeper.xtime_nsec = 0; 158 /* if changing clocks, convert xtime_nsec shift units */
134 timekeeper.shift = clock->shift; 159 if (old_clock) {
160 int shift_change = clock->shift - old_clock->shift;
161 if (shift_change < 0)
162 tk->xtime_nsec >>= -shift_change;
163 else
164 tk->xtime_nsec <<= shift_change;
165 }
166 tk->shift = clock->shift;
135 167
136 timekeeper.ntp_error = 0; 168 tk->ntp_error = 0;
137 timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift; 169 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
138 170
139 /* 171 /*
140 * The timekeeper keeps its own mult values for the currently 172 * The timekeeper keeps its own mult values for the currently
141 * active clocksource. These value will be adjusted via NTP 173 * active clocksource. These value will be adjusted via NTP
142 * to counteract clock drifting. 174 * to counteract clock drifting.
143 */ 175 */
144 timekeeper.mult = clock->mult; 176 tk->mult = clock->mult;
145} 177}
146 178
147/* Timekeeper helper functions. */ 179/* Timekeeper helper functions. */
148static inline s64 timekeeping_get_ns(void) 180static inline s64 timekeeping_get_ns(struct timekeeper *tk)
149{ 181{
150 cycle_t cycle_now, cycle_delta; 182 cycle_t cycle_now, cycle_delta;
151 struct clocksource *clock; 183 struct clocksource *clock;
184 s64 nsec;
152 185
153 /* read clocksource: */ 186 /* read clocksource: */
154 clock = timekeeper.clock; 187 clock = tk->clock;
155 cycle_now = clock->read(clock); 188 cycle_now = clock->read(clock);
156 189
157 /* calculate the delta since the last update_wall_time: */ 190 /* calculate the delta since the last update_wall_time: */
158 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; 191 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
159 192
160 /* return delta convert to nanoseconds using ntp adjusted mult. */ 193 nsec = cycle_delta * tk->mult + tk->xtime_nsec;
161 return clocksource_cyc2ns(cycle_delta, timekeeper.mult, 194 nsec >>= tk->shift;
162 timekeeper.shift); 195
196 /* If arch requires, add in gettimeoffset() */
197 return nsec + arch_gettimeoffset();
163} 198}
164 199
165static inline s64 timekeeping_get_ns_raw(void) 200static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
166{ 201{
167 cycle_t cycle_now, cycle_delta; 202 cycle_t cycle_now, cycle_delta;
168 struct clocksource *clock; 203 struct clocksource *clock;
204 s64 nsec;
169 205
170 /* read clocksource: */ 206 /* read clocksource: */
171 clock = timekeeper.clock; 207 clock = tk->clock;
172 cycle_now = clock->read(clock); 208 cycle_now = clock->read(clock);
173 209
174 /* calculate the delta since the last update_wall_time: */ 210 /* calculate the delta since the last update_wall_time: */
175 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; 211 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
176 212
177 /* return delta convert to nanoseconds. */ 213 /* convert delta to nanoseconds. */
178 return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift); 214 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
215
216 /* If arch requires, add in gettimeoffset() */
217 return nsec + arch_gettimeoffset();
179} 218}
180 219
181static void update_rt_offset(void) 220static void update_rt_offset(struct timekeeper *tk)
182{ 221{
183 struct timespec tmp, *wtm = &timekeeper.wall_to_monotonic; 222 struct timespec tmp, *wtm = &tk->wall_to_monotonic;
184 223
185 set_normalized_timespec(&tmp, -wtm->tv_sec, -wtm->tv_nsec); 224 set_normalized_timespec(&tmp, -wtm->tv_sec, -wtm->tv_nsec);
186 timekeeper.offs_real = timespec_to_ktime(tmp); 225 tk->offs_real = timespec_to_ktime(tmp);
187} 226}
188 227
189/* must hold write on timekeeper.lock */ 228/* must hold write on timekeeper.lock */
190static void timekeeping_update(bool clearntp) 229static void timekeeping_update(struct timekeeper *tk, bool clearntp)
191{ 230{
231 struct timespec xt;
232
192 if (clearntp) { 233 if (clearntp) {
193 timekeeper.ntp_error = 0; 234 tk->ntp_error = 0;
194 ntp_clear(); 235 ntp_clear();
195 } 236 }
196 update_rt_offset(); 237 update_rt_offset(tk);
197 update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic, 238 xt = tk_xtime(tk);
198 timekeeper.clock, timekeeper.mult); 239 update_vsyscall(&xt, &tk->wall_to_monotonic, tk->clock, tk->mult);
199} 240}
200 241
201 242
@@ -206,27 +247,26 @@ static void timekeeping_update(bool clearntp)
206 * update_wall_time(). This is useful before significant clock changes, 247 * update_wall_time(). This is useful before significant clock changes,
207 * as it avoids having to deal with this time offset explicitly. 248 * as it avoids having to deal with this time offset explicitly.
208 */ 249 */
209static void timekeeping_forward_now(void) 250static void timekeeping_forward_now(struct timekeeper *tk)
210{ 251{
211 cycle_t cycle_now, cycle_delta; 252 cycle_t cycle_now, cycle_delta;
212 struct clocksource *clock; 253 struct clocksource *clock;
213 s64 nsec; 254 s64 nsec;
214 255
215 clock = timekeeper.clock; 256 clock = tk->clock;
216 cycle_now = clock->read(clock); 257 cycle_now = clock->read(clock);
217 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; 258 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
218 clock->cycle_last = cycle_now; 259 clock->cycle_last = cycle_now;
219 260
220 nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult, 261 tk->xtime_nsec += cycle_delta * tk->mult;
221 timekeeper.shift);
222 262
223 /* If arch requires, add in gettimeoffset() */ 263 /* If arch requires, add in gettimeoffset() */
224 nsec += arch_gettimeoffset(); 264 tk->xtime_nsec += arch_gettimeoffset() << tk->shift;
225 265
226 timespec_add_ns(&timekeeper.xtime, nsec); 266 tk_normalize_xtime(tk);
227 267
228 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift); 268 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
229 timespec_add_ns(&timekeeper.raw_time, nsec); 269 timespec_add_ns(&tk->raw_time, nsec);
230} 270}
231 271
232/** 272/**
@@ -238,18 +278,15 @@ static void timekeeping_forward_now(void)
238void getnstimeofday(struct timespec *ts) 278void getnstimeofday(struct timespec *ts)
239{ 279{
240 unsigned long seq; 280 unsigned long seq;
241 s64 nsecs; 281 s64 nsecs = 0;
242 282
243 WARN_ON(timekeeping_suspended); 283 WARN_ON(timekeeping_suspended);
244 284
245 do { 285 do {
246 seq = read_seqbegin(&timekeeper.lock); 286 seq = read_seqbegin(&timekeeper.lock);
247 287
248 *ts = timekeeper.xtime; 288 ts->tv_sec = timekeeper.xtime_sec;
249 nsecs = timekeeping_get_ns(); 289 ts->tv_nsec = timekeeping_get_ns(&timekeeper);
250
251 /* If arch requires, add in gettimeoffset() */
252 nsecs += arch_gettimeoffset();
253 290
254 } while (read_seqretry(&timekeeper.lock, seq)); 291 } while (read_seqretry(&timekeeper.lock, seq));
255 292
@@ -266,13 +303,10 @@ ktime_t ktime_get(void)
266 303
267 do { 304 do {
268 seq = read_seqbegin(&timekeeper.lock); 305 seq = read_seqbegin(&timekeeper.lock);
269 secs = timekeeper.xtime.tv_sec + 306 secs = timekeeper.xtime_sec +
270 timekeeper.wall_to_monotonic.tv_sec; 307 timekeeper.wall_to_monotonic.tv_sec;
271 nsecs = timekeeper.xtime.tv_nsec + 308 nsecs = timekeeping_get_ns(&timekeeper) +
272 timekeeper.wall_to_monotonic.tv_nsec; 309 timekeeper.wall_to_monotonic.tv_nsec;
273 nsecs += timekeeping_get_ns();
274 /* If arch requires, add in gettimeoffset() */
275 nsecs += arch_gettimeoffset();
276 310
277 } while (read_seqretry(&timekeeper.lock, seq)); 311 } while (read_seqretry(&timekeeper.lock, seq));
278 /* 312 /*
@@ -295,22 +329,19 @@ void ktime_get_ts(struct timespec *ts)
295{ 329{
296 struct timespec tomono; 330 struct timespec tomono;
297 unsigned int seq; 331 unsigned int seq;
298 s64 nsecs;
299 332
300 WARN_ON(timekeeping_suspended); 333 WARN_ON(timekeeping_suspended);
301 334
302 do { 335 do {
303 seq = read_seqbegin(&timekeeper.lock); 336 seq = read_seqbegin(&timekeeper.lock);
304 *ts = timekeeper.xtime; 337 ts->tv_sec = timekeeper.xtime_sec;
338 ts->tv_nsec = timekeeping_get_ns(&timekeeper);
305 tomono = timekeeper.wall_to_monotonic; 339 tomono = timekeeper.wall_to_monotonic;
306 nsecs = timekeeping_get_ns();
307 /* If arch requires, add in gettimeoffset() */
308 nsecs += arch_gettimeoffset();
309 340
310 } while (read_seqretry(&timekeeper.lock, seq)); 341 } while (read_seqretry(&timekeeper.lock, seq));
311 342
312 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec, 343 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
313 ts->tv_nsec + tomono.tv_nsec + nsecs); 344 ts->tv_nsec + tomono.tv_nsec);
314} 345}
315EXPORT_SYMBOL_GPL(ktime_get_ts); 346EXPORT_SYMBOL_GPL(ktime_get_ts);
316 347
@@ -333,20 +364,14 @@ void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
333 WARN_ON_ONCE(timekeeping_suspended); 364 WARN_ON_ONCE(timekeeping_suspended);
334 365
335 do { 366 do {
336 u32 arch_offset;
337
338 seq = read_seqbegin(&timekeeper.lock); 367 seq = read_seqbegin(&timekeeper.lock);
339 368
340 *ts_raw = timekeeper.raw_time; 369 *ts_raw = timekeeper.raw_time;
341 *ts_real = timekeeper.xtime; 370 ts_real->tv_sec = timekeeper.xtime_sec;
342 371 ts_real->tv_nsec = 0;
343 nsecs_raw = timekeeping_get_ns_raw();
344 nsecs_real = timekeeping_get_ns();
345 372
346 /* If arch requires, add in gettimeoffset() */ 373 nsecs_raw = timekeeping_get_ns_raw(&timekeeper);
347 arch_offset = arch_gettimeoffset(); 374 nsecs_real = timekeeping_get_ns(&timekeeper);
348 nsecs_raw += arch_offset;
349 nsecs_real += arch_offset;
350 375
351 } while (read_seqretry(&timekeeper.lock, seq)); 376 } while (read_seqretry(&timekeeper.lock, seq));
352 377
@@ -381,7 +406,7 @@ EXPORT_SYMBOL(do_gettimeofday);
381 */ 406 */
382int do_settimeofday(const struct timespec *tv) 407int do_settimeofday(const struct timespec *tv)
383{ 408{
384 struct timespec ts_delta; 409 struct timespec ts_delta, xt;
385 unsigned long flags; 410 unsigned long flags;
386 411
387 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) 412 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
@@ -389,15 +414,18 @@ int do_settimeofday(const struct timespec *tv)
389 414
390 write_seqlock_irqsave(&timekeeper.lock, flags); 415 write_seqlock_irqsave(&timekeeper.lock, flags);
391 416
392 timekeeping_forward_now(); 417 timekeeping_forward_now(&timekeeper);
418
419 xt = tk_xtime(&timekeeper);
420 ts_delta.tv_sec = tv->tv_sec - xt.tv_sec;
421 ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec;
393 422
394 ts_delta.tv_sec = tv->tv_sec - timekeeper.xtime.tv_sec;
395 ts_delta.tv_nsec = tv->tv_nsec - timekeeper.xtime.tv_nsec;
396 timekeeper.wall_to_monotonic = 423 timekeeper.wall_to_monotonic =
397 timespec_sub(timekeeper.wall_to_monotonic, ts_delta); 424 timespec_sub(timekeeper.wall_to_monotonic, ts_delta);
398 425
399 timekeeper.xtime = *tv; 426 tk_set_xtime(&timekeeper, tv);
400 timekeeping_update(true); 427
428 timekeeping_update(&timekeeper, true);
401 429
402 write_sequnlock_irqrestore(&timekeeper.lock, flags); 430 write_sequnlock_irqrestore(&timekeeper.lock, flags);
403 431
@@ -424,13 +452,14 @@ int timekeeping_inject_offset(struct timespec *ts)
424 452
425 write_seqlock_irqsave(&timekeeper.lock, flags); 453 write_seqlock_irqsave(&timekeeper.lock, flags);
426 454
427 timekeeping_forward_now(); 455 timekeeping_forward_now(&timekeeper);
456
428 457
429 timekeeper.xtime = timespec_add(timekeeper.xtime, *ts); 458 tk_xtime_add(&timekeeper, ts);
430 timekeeper.wall_to_monotonic = 459 timekeeper.wall_to_monotonic =
431 timespec_sub(timekeeper.wall_to_monotonic, *ts); 460 timespec_sub(timekeeper.wall_to_monotonic, *ts);
432 461
433 timekeeping_update(true); 462 timekeeping_update(&timekeeper, true);
434 463
435 write_sequnlock_irqrestore(&timekeeper.lock, flags); 464 write_sequnlock_irqrestore(&timekeeper.lock, flags);
436 465
@@ -455,14 +484,14 @@ static int change_clocksource(void *data)
455 484
456 write_seqlock_irqsave(&timekeeper.lock, flags); 485 write_seqlock_irqsave(&timekeeper.lock, flags);
457 486
458 timekeeping_forward_now(); 487 timekeeping_forward_now(&timekeeper);
459 if (!new->enable || new->enable(new) == 0) { 488 if (!new->enable || new->enable(new) == 0) {
460 old = timekeeper.clock; 489 old = timekeeper.clock;
461 timekeeper_setup_internals(new); 490 tk_setup_internals(&timekeeper, new);
462 if (old->disable) 491 if (old->disable)
463 old->disable(old); 492 old->disable(old);
464 } 493 }
465 timekeeping_update(true); 494 timekeeping_update(&timekeeper, true);
466 495
467 write_sequnlock_irqrestore(&timekeeper.lock, flags); 496 write_sequnlock_irqrestore(&timekeeper.lock, flags);
468 497
@@ -512,7 +541,7 @@ void getrawmonotonic(struct timespec *ts)
512 541
513 do { 542 do {
514 seq = read_seqbegin(&timekeeper.lock); 543 seq = read_seqbegin(&timekeeper.lock);
515 nsecs = timekeeping_get_ns_raw(); 544 nsecs = timekeeping_get_ns_raw(&timekeeper);
516 *ts = timekeeper.raw_time; 545 *ts = timekeeper.raw_time;
517 546
518 } while (read_seqretry(&timekeeper.lock, seq)); 547 } while (read_seqretry(&timekeeper.lock, seq));
@@ -547,6 +576,7 @@ u64 timekeeping_max_deferment(void)
547{ 576{
548 unsigned long seq; 577 unsigned long seq;
549 u64 ret; 578 u64 ret;
579
550 do { 580 do {
551 seq = read_seqbegin(&timekeeper.lock); 581 seq = read_seqbegin(&timekeeper.lock);
552 582
@@ -607,19 +637,17 @@ void __init timekeeping_init(void)
607 clock = clocksource_default_clock(); 637 clock = clocksource_default_clock();
608 if (clock->enable) 638 if (clock->enable)
609 clock->enable(clock); 639 clock->enable(clock);
610 timekeeper_setup_internals(clock); 640 tk_setup_internals(&timekeeper, clock);
611 641
612 timekeeper.xtime.tv_sec = now.tv_sec; 642 tk_set_xtime(&timekeeper, &now);
613 timekeeper.xtime.tv_nsec = now.tv_nsec;
614 timekeeper.raw_time.tv_sec = 0; 643 timekeeper.raw_time.tv_sec = 0;
615 timekeeper.raw_time.tv_nsec = 0; 644 timekeeper.raw_time.tv_nsec = 0;
616 if (boot.tv_sec == 0 && boot.tv_nsec == 0) { 645 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
617 boot.tv_sec = timekeeper.xtime.tv_sec; 646 boot = tk_xtime(&timekeeper);
618 boot.tv_nsec = timekeeper.xtime.tv_nsec; 647
619 }
620 set_normalized_timespec(&timekeeper.wall_to_monotonic, 648 set_normalized_timespec(&timekeeper.wall_to_monotonic,
621 -boot.tv_sec, -boot.tv_nsec); 649 -boot.tv_sec, -boot.tv_nsec);
622 update_rt_offset(); 650 update_rt_offset(&timekeeper);
623 timekeeper.total_sleep_time.tv_sec = 0; 651 timekeeper.total_sleep_time.tv_sec = 0;
624 timekeeper.total_sleep_time.tv_nsec = 0; 652 timekeeper.total_sleep_time.tv_nsec = 0;
625 write_sequnlock_irqrestore(&timekeeper.lock, flags); 653 write_sequnlock_irqrestore(&timekeeper.lock, flags);
@@ -641,7 +669,8 @@ static void update_sleep_time(struct timespec t)
641 * Takes a timespec offset measuring a suspend interval and properly 669 * Takes a timespec offset measuring a suspend interval and properly
642 * adds the sleep offset to the timekeeping variables. 670 * adds the sleep offset to the timekeeping variables.
643 */ 671 */
644static void __timekeeping_inject_sleeptime(struct timespec *delta) 672static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
673 struct timespec *delta)
645{ 674{
646 if (!timespec_valid(delta)) { 675 if (!timespec_valid(delta)) {
647 printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid " 676 printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid "
@@ -649,10 +678,9 @@ static void __timekeeping_inject_sleeptime(struct timespec *delta)
649 return; 678 return;
650 } 679 }
651 680
652 timekeeper.xtime = timespec_add(timekeeper.xtime, *delta); 681 tk_xtime_add(tk, delta);
653 timekeeper.wall_to_monotonic = 682 tk->wall_to_monotonic = timespec_sub(tk->wall_to_monotonic, *delta);
654 timespec_sub(timekeeper.wall_to_monotonic, *delta); 683 update_sleep_time(timespec_add(tk->total_sleep_time, *delta));
655 update_sleep_time(timespec_add(timekeeper.total_sleep_time, *delta));
656} 684}
657 685
658 686
@@ -678,11 +706,11 @@ void timekeeping_inject_sleeptime(struct timespec *delta)
678 706
679 write_seqlock_irqsave(&timekeeper.lock, flags); 707 write_seqlock_irqsave(&timekeeper.lock, flags);
680 708
681 timekeeping_forward_now(); 709 timekeeping_forward_now(&timekeeper);
682 710
683 __timekeeping_inject_sleeptime(delta); 711 __timekeeping_inject_sleeptime(&timekeeper, delta);
684 712
685 timekeeping_update(true); 713 timekeeping_update(&timekeeper, true);
686 714
687 write_sequnlock_irqrestore(&timekeeper.lock, flags); 715 write_sequnlock_irqrestore(&timekeeper.lock, flags);
688 716
@@ -711,13 +739,13 @@ static void timekeeping_resume(void)
711 739
712 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) { 740 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
713 ts = timespec_sub(ts, timekeeping_suspend_time); 741 ts = timespec_sub(ts, timekeeping_suspend_time);
714 __timekeeping_inject_sleeptime(&ts); 742 __timekeeping_inject_sleeptime(&timekeeper, &ts);
715 } 743 }
716 /* re-base the last cycle value */ 744 /* re-base the last cycle value */
717 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock); 745 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
718 timekeeper.ntp_error = 0; 746 timekeeper.ntp_error = 0;
719 timekeeping_suspended = 0; 747 timekeeping_suspended = 0;
720 timekeeping_update(false); 748 timekeeping_update(&timekeeper, false);
721 write_sequnlock_irqrestore(&timekeeper.lock, flags); 749 write_sequnlock_irqrestore(&timekeeper.lock, flags);
722 750
723 touch_softlockup_watchdog(); 751 touch_softlockup_watchdog();
@@ -737,7 +765,7 @@ static int timekeeping_suspend(void)
737 read_persistent_clock(&timekeeping_suspend_time); 765 read_persistent_clock(&timekeeping_suspend_time);
738 766
739 write_seqlock_irqsave(&timekeeper.lock, flags); 767 write_seqlock_irqsave(&timekeeper.lock, flags);
740 timekeeping_forward_now(); 768 timekeeping_forward_now(&timekeeper);
741 timekeeping_suspended = 1; 769 timekeeping_suspended = 1;
742 770
743 /* 771 /*
@@ -746,7 +774,7 @@ static int timekeeping_suspend(void)
746 * try to compensate so the difference in system time 774 * try to compensate so the difference in system time
747 * and persistent_clock time stays close to constant. 775 * and persistent_clock time stays close to constant.
748 */ 776 */
749 delta = timespec_sub(timekeeper.xtime, timekeeping_suspend_time); 777 delta = timespec_sub(tk_xtime(&timekeeper), timekeeping_suspend_time);
750 delta_delta = timespec_sub(delta, old_delta); 778 delta_delta = timespec_sub(delta, old_delta);
751 if (abs(delta_delta.tv_sec) >= 2) { 779 if (abs(delta_delta.tv_sec) >= 2) {
752 /* 780 /*
@@ -785,7 +813,8 @@ device_initcall(timekeeping_init_ops);
785 * If the error is already larger, we look ahead even further 813 * If the error is already larger, we look ahead even further
786 * to compensate for late or lost adjustments. 814 * to compensate for late or lost adjustments.
787 */ 815 */
788static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval, 816static __always_inline int timekeeping_bigadjust(struct timekeeper *tk,
817 s64 error, s64 *interval,
789 s64 *offset) 818 s64 *offset)
790{ 819{
791 s64 tick_error, i; 820 s64 tick_error, i;
@@ -801,7 +830,7 @@ static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
801 * here. This is tuned so that an error of about 1 msec is adjusted 830 * here. This is tuned so that an error of about 1 msec is adjusted
802 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks). 831 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
803 */ 832 */
804 error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ); 833 error2 = tk->ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
805 error2 = abs(error2); 834 error2 = abs(error2);
806 for (look_ahead = 0; error2 > 0; look_ahead++) 835 for (look_ahead = 0; error2 > 0; look_ahead++)
807 error2 >>= 2; 836 error2 >>= 2;
@@ -810,8 +839,8 @@ static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
810 * Now calculate the error in (1 << look_ahead) ticks, but first 839 * Now calculate the error in (1 << look_ahead) ticks, but first
811 * remove the single look ahead already included in the error. 840 * remove the single look ahead already included in the error.
812 */ 841 */
813 tick_error = ntp_tick_length() >> (timekeeper.ntp_error_shift + 1); 842 tick_error = ntp_tick_length() >> (tk->ntp_error_shift + 1);
814 tick_error -= timekeeper.xtime_interval >> 1; 843 tick_error -= tk->xtime_interval >> 1;
815 error = ((error - tick_error) >> look_ahead) + tick_error; 844 error = ((error - tick_error) >> look_ahead) + tick_error;
816 845
817 /* Finally calculate the adjustment shift value. */ 846 /* Finally calculate the adjustment shift value. */
@@ -836,9 +865,9 @@ static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
836 * this is optimized for the most common adjustments of -1,0,1, 865 * this is optimized for the most common adjustments of -1,0,1,
837 * for other values we can do a bit more work. 866 * for other values we can do a bit more work.
838 */ 867 */
839static void timekeeping_adjust(s64 offset) 868static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
840{ 869{
841 s64 error, interval = timekeeper.cycle_interval; 870 s64 error, interval = tk->cycle_interval;
842 int adj; 871 int adj;
843 872
844 /* 873 /*
@@ -854,7 +883,7 @@ static void timekeeping_adjust(s64 offset)
854 * 883 *
855 * Note: It does not "save" on aggravation when reading the code. 884 * Note: It does not "save" on aggravation when reading the code.
856 */ 885 */
857 error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1); 886 error = tk->ntp_error >> (tk->ntp_error_shift - 1);
858 if (error > interval) { 887 if (error > interval) {
859 /* 888 /*
860 * We now divide error by 4(via shift), which checks if 889 * We now divide error by 4(via shift), which checks if
@@ -876,7 +905,8 @@ static void timekeeping_adjust(s64 offset)
876 if (likely(error <= interval)) 905 if (likely(error <= interval))
877 adj = 1; 906 adj = 1;
878 else 907 else
879 adj = timekeeping_bigadjust(error, &interval, &offset); 908 adj = timekeeping_bigadjust(tk, error, &interval,
909 &offset);
880 } else if (error < -interval) { 910 } else if (error < -interval) {
881 /* See comment above, this is just switched for the negative */ 911 /* See comment above, this is just switched for the negative */
882 error >>= 2; 912 error >>= 2;
@@ -885,18 +915,17 @@ static void timekeeping_adjust(s64 offset)
885 interval = -interval; 915 interval = -interval;
886 offset = -offset; 916 offset = -offset;
887 } else 917 } else
888 adj = timekeeping_bigadjust(error, &interval, &offset); 918 adj = timekeeping_bigadjust(tk, error, &interval,
889 } else /* No adjustment needed */ 919 &offset);
920 } else
890 return; 921 return;
891 922
892 if (unlikely(timekeeper.clock->maxadj && 923 if (unlikely(tk->clock->maxadj &&
893 (timekeeper.mult + adj > 924 (tk->mult + adj > tk->clock->mult + tk->clock->maxadj))) {
894 timekeeper.clock->mult + timekeeper.clock->maxadj))) {
895 printk_once(KERN_WARNING 925 printk_once(KERN_WARNING
896 "Adjusting %s more than 11%% (%ld vs %ld)\n", 926 "Adjusting %s more than 11%% (%ld vs %ld)\n",
897 timekeeper.clock->name, (long)timekeeper.mult + adj, 927 tk->clock->name, (long)tk->mult + adj,
898 (long)timekeeper.clock->mult + 928 (long)tk->clock->mult + tk->clock->maxadj);
899 timekeeper.clock->maxadj);
900 } 929 }
901 /* 930 /*
902 * So the following can be confusing. 931 * So the following can be confusing.
@@ -947,11 +976,60 @@ static void timekeeping_adjust(s64 offset)
947 * 976 *
948 * XXX - TODO: Doc ntp_error calculation. 977 * XXX - TODO: Doc ntp_error calculation.
949 */ 978 */
950 timekeeper.mult += adj; 979 tk->mult += adj;
951 timekeeper.xtime_interval += interval; 980 tk->xtime_interval += interval;
952 timekeeper.xtime_nsec -= offset; 981 tk->xtime_nsec -= offset;
953 timekeeper.ntp_error -= (interval - offset) << 982 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
954 timekeeper.ntp_error_shift; 983
984 /*
985 * It may be possible that when we entered this function, xtime_nsec
986 * was very small. Further, if we're slightly speeding the clocksource
987 * in the code above, its possible the required corrective factor to
988 * xtime_nsec could cause it to underflow.
989 *
990 * Now, since we already accumulated the second, cannot simply roll
991 * the accumulated second back, since the NTP subsystem has been
992 * notified via second_overflow. So instead we push xtime_nsec forward
993 * by the amount we underflowed, and add that amount into the error.
994 *
995 * We'll correct this error next time through this function, when
996 * xtime_nsec is not as small.
997 */
998 if (unlikely((s64)tk->xtime_nsec < 0)) {
999 s64 neg = -(s64)tk->xtime_nsec;
1000 tk->xtime_nsec = 0;
1001 tk->ntp_error += neg << tk->ntp_error_shift;
1002 }
1003
1004}
1005
1006
1007/**
1008 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
1009 *
1010 * Helper function that accumulates a the nsecs greater then a second
1011 * from the xtime_nsec field to the xtime_secs field.
1012 * It also calls into the NTP code to handle leapsecond processing.
1013 *
1014 */
1015static inline void accumulate_nsecs_to_secs(struct timekeeper *tk)
1016{
1017 u64 nsecps = (u64)NSEC_PER_SEC << tk->shift;
1018
1019 while (tk->xtime_nsec >= nsecps) {
1020 int leap;
1021
1022 tk->xtime_nsec -= nsecps;
1023 tk->xtime_sec++;
1024
1025 /* Figure out if its a leap sec and apply if needed */
1026 leap = second_overflow(tk->xtime_sec);
1027 tk->xtime_sec += leap;
1028 tk->wall_to_monotonic.tv_sec -= leap;
1029 if (leap)
1030 clock_was_set_delayed();
1031
1032 }
955} 1033}
956 1034
957 1035
@@ -964,46 +1042,36 @@ static void timekeeping_adjust(s64 offset)
964 * 1042 *
965 * Returns the unconsumed cycles. 1043 * Returns the unconsumed cycles.
966 */ 1044 */
967static cycle_t logarithmic_accumulation(cycle_t offset, int shift) 1045static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
1046 u32 shift)
968{ 1047{
969 u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
970 u64 raw_nsecs; 1048 u64 raw_nsecs;
971 1049
972 /* If the offset is smaller than a shifted interval, do nothing */ 1050 /* If the offset is smaller then a shifted interval, do nothing */
973 if (offset < timekeeper.cycle_interval<<shift) 1051 if (offset < tk->cycle_interval<<shift)
974 return offset; 1052 return offset;
975 1053
976 /* Accumulate one shifted interval */ 1054 /* Accumulate one shifted interval */
977 offset -= timekeeper.cycle_interval << shift; 1055 offset -= tk->cycle_interval << shift;
978 timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift; 1056 tk->clock->cycle_last += tk->cycle_interval << shift;
979 1057
980 timekeeper.xtime_nsec += timekeeper.xtime_interval << shift; 1058 tk->xtime_nsec += tk->xtime_interval << shift;
981 while (timekeeper.xtime_nsec >= nsecps) { 1059 accumulate_nsecs_to_secs(tk);
982 int leap;
983 timekeeper.xtime_nsec -= nsecps;
984 timekeeper.xtime.tv_sec++;
985 leap = second_overflow(timekeeper.xtime.tv_sec);
986 timekeeper.xtime.tv_sec += leap;
987 timekeeper.wall_to_monotonic.tv_sec -= leap;
988 if (leap)
989 clock_was_set_delayed();
990 }
991 1060
992 /* Accumulate raw time */ 1061 /* Accumulate raw time */
993 raw_nsecs = timekeeper.raw_interval << shift; 1062 raw_nsecs = tk->raw_interval << shift;
994 raw_nsecs += timekeeper.raw_time.tv_nsec; 1063 raw_nsecs += tk->raw_time.tv_nsec;
995 if (raw_nsecs >= NSEC_PER_SEC) { 1064 if (raw_nsecs >= NSEC_PER_SEC) {
996 u64 raw_secs = raw_nsecs; 1065 u64 raw_secs = raw_nsecs;
997 raw_nsecs = do_div(raw_secs, NSEC_PER_SEC); 1066 raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
998 timekeeper.raw_time.tv_sec += raw_secs; 1067 tk->raw_time.tv_sec += raw_secs;
999 } 1068 }
1000 timekeeper.raw_time.tv_nsec = raw_nsecs; 1069 tk->raw_time.tv_nsec = raw_nsecs;
1001 1070
1002 /* Accumulate error between NTP and clock interval */ 1071 /* Accumulate error between NTP and clock interval */
1003 timekeeper.ntp_error += ntp_tick_length() << shift; 1072 tk->ntp_error += ntp_tick_length() << shift;
1004 timekeeper.ntp_error -= 1073 tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
1005 (timekeeper.xtime_interval + timekeeper.xtime_remainder) << 1074 (tk->ntp_error_shift + shift);
1006 (timekeeper.ntp_error_shift + shift);
1007 1075
1008 return offset; 1076 return offset;
1009} 1077}
@@ -1019,6 +1087,7 @@ static void update_wall_time(void)
1019 cycle_t offset; 1087 cycle_t offset;
1020 int shift = 0, maxshift; 1088 int shift = 0, maxshift;
1021 unsigned long flags; 1089 unsigned long flags;
1090 s64 remainder;
1022 1091
1023 write_seqlock_irqsave(&timekeeper.lock, flags); 1092 write_seqlock_irqsave(&timekeeper.lock, flags);
1024 1093
@@ -1033,8 +1102,6 @@ static void update_wall_time(void)
1033#else 1102#else
1034 offset = (clock->read(clock) - clock->cycle_last) & clock->mask; 1103 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
1035#endif 1104#endif
1036 timekeeper.xtime_nsec = (s64)timekeeper.xtime.tv_nsec <<
1037 timekeeper.shift;
1038 1105
1039 /* 1106 /*
1040 * With NO_HZ we may have to accumulate many cycle_intervals 1107 * With NO_HZ we may have to accumulate many cycle_intervals
@@ -1050,64 +1117,36 @@ static void update_wall_time(void)
1050 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1; 1117 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
1051 shift = min(shift, maxshift); 1118 shift = min(shift, maxshift);
1052 while (offset >= timekeeper.cycle_interval) { 1119 while (offset >= timekeeper.cycle_interval) {
1053 offset = logarithmic_accumulation(offset, shift); 1120 offset = logarithmic_accumulation(&timekeeper, offset, shift);
1054 if(offset < timekeeper.cycle_interval<<shift) 1121 if(offset < timekeeper.cycle_interval<<shift)
1055 shift--; 1122 shift--;
1056 } 1123 }
1057 1124
1058 /* correct the clock when NTP error is too big */ 1125 /* correct the clock when NTP error is too big */
1059 timekeeping_adjust(offset); 1126 timekeeping_adjust(&timekeeper, offset);
1060
1061 /*
1062 * Since in the loop above, we accumulate any amount of time
1063 * in xtime_nsec over a second into xtime.tv_sec, its possible for
1064 * xtime_nsec to be fairly small after the loop. Further, if we're
1065 * slightly speeding the clocksource up in timekeeping_adjust(),
1066 * its possible the required corrective factor to xtime_nsec could
1067 * cause it to underflow.
1068 *
1069 * Now, we cannot simply roll the accumulated second back, since
1070 * the NTP subsystem has been notified via second_overflow. So
1071 * instead we push xtime_nsec forward by the amount we underflowed,
1072 * and add that amount into the error.
1073 *
1074 * We'll correct this error next time through this function, when
1075 * xtime_nsec is not as small.
1076 */
1077 if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
1078 s64 neg = -(s64)timekeeper.xtime_nsec;
1079 timekeeper.xtime_nsec = 0;
1080 timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
1081 }
1082 1127
1083 1128
1084 /* 1129 /*
1085 * Store full nanoseconds into xtime after rounding it up and 1130 * Store only full nanoseconds into xtime_nsec after rounding
1086 * add the remainder to the error difference. 1131 * it up and add the remainder to the error difference.
1087 */ 1132 * XXX - This is necessary to avoid small 1ns inconsistnecies caused
1088 timekeeper.xtime.tv_nsec = ((s64)timekeeper.xtime_nsec >> 1133 * by truncating the remainder in vsyscalls. However, it causes
1089 timekeeper.shift) + 1; 1134 * additional work to be done in timekeeping_adjust(). Once
1090 timekeeper.xtime_nsec -= (s64)timekeeper.xtime.tv_nsec << 1135 * the vsyscall implementations are converted to use xtime_nsec
1091 timekeeper.shift; 1136 * (shifted nanoseconds), this can be killed.
1092 timekeeper.ntp_error += timekeeper.xtime_nsec << 1137 */
1093 timekeeper.ntp_error_shift; 1138 remainder = timekeeper.xtime_nsec & ((1 << timekeeper.shift) - 1);
1139 timekeeper.xtime_nsec -= remainder;
1140 timekeeper.xtime_nsec += 1 << timekeeper.shift;
1141 timekeeper.ntp_error += remainder << timekeeper.ntp_error_shift;
1094 1142
1095 /* 1143 /*
1096 * Finally, make sure that after the rounding 1144 * Finally, make sure that after the rounding
1097 * xtime.tv_nsec isn't larger than NSEC_PER_SEC 1145 * xtime_nsec isn't larger than NSEC_PER_SEC
1098 */ 1146 */
1099 if (unlikely(timekeeper.xtime.tv_nsec >= NSEC_PER_SEC)) { 1147 accumulate_nsecs_to_secs(&timekeeper);
1100 int leap;
1101 timekeeper.xtime.tv_nsec -= NSEC_PER_SEC;
1102 timekeeper.xtime.tv_sec++;
1103 leap = second_overflow(timekeeper.xtime.tv_sec);
1104 timekeeper.xtime.tv_sec += leap;
1105 timekeeper.wall_to_monotonic.tv_sec -= leap;
1106 if (leap)
1107 clock_was_set_delayed();
1108 }
1109 1148
1110 timekeeping_update(false); 1149 timekeeping_update(&timekeeper, false);
1111 1150
1112out: 1151out:
1113 write_sequnlock_irqrestore(&timekeeper.lock, flags); 1152 write_sequnlock_irqrestore(&timekeeper.lock, flags);
@@ -1152,21 +1191,20 @@ void get_monotonic_boottime(struct timespec *ts)
1152{ 1191{
1153 struct timespec tomono, sleep; 1192 struct timespec tomono, sleep;
1154 unsigned int seq; 1193 unsigned int seq;
1155 s64 nsecs;
1156 1194
1157 WARN_ON(timekeeping_suspended); 1195 WARN_ON(timekeeping_suspended);
1158 1196
1159 do { 1197 do {
1160 seq = read_seqbegin(&timekeeper.lock); 1198 seq = read_seqbegin(&timekeeper.lock);
1161 *ts = timekeeper.xtime; 1199 ts->tv_sec = timekeeper.xtime_sec;
1200 ts->tv_nsec = timekeeping_get_ns(&timekeeper);
1162 tomono = timekeeper.wall_to_monotonic; 1201 tomono = timekeeper.wall_to_monotonic;
1163 sleep = timekeeper.total_sleep_time; 1202 sleep = timekeeper.total_sleep_time;
1164 nsecs = timekeeping_get_ns();
1165 1203
1166 } while (read_seqretry(&timekeeper.lock, seq)); 1204 } while (read_seqretry(&timekeeper.lock, seq));
1167 1205
1168 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec, 1206 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec,
1169 ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec + nsecs); 1207 ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec);
1170} 1208}
1171EXPORT_SYMBOL_GPL(get_monotonic_boottime); 1209EXPORT_SYMBOL_GPL(get_monotonic_boottime);
1172 1210
@@ -1199,13 +1237,13 @@ EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
1199 1237
1200unsigned long get_seconds(void) 1238unsigned long get_seconds(void)
1201{ 1239{
1202 return timekeeper.xtime.tv_sec; 1240 return timekeeper.xtime_sec;
1203} 1241}
1204EXPORT_SYMBOL(get_seconds); 1242EXPORT_SYMBOL(get_seconds);
1205 1243
1206struct timespec __current_kernel_time(void) 1244struct timespec __current_kernel_time(void)
1207{ 1245{
1208 return timekeeper.xtime; 1246 return tk_xtime(&timekeeper);
1209} 1247}
1210 1248
1211struct timespec current_kernel_time(void) 1249struct timespec current_kernel_time(void)
@@ -1216,7 +1254,7 @@ struct timespec current_kernel_time(void)
1216 do { 1254 do {
1217 seq = read_seqbegin(&timekeeper.lock); 1255 seq = read_seqbegin(&timekeeper.lock);
1218 1256
1219 now = timekeeper.xtime; 1257 now = tk_xtime(&timekeeper);
1220 } while (read_seqretry(&timekeeper.lock, seq)); 1258 } while (read_seqretry(&timekeeper.lock, seq));
1221 1259
1222 return now; 1260 return now;
@@ -1231,7 +1269,7 @@ struct timespec get_monotonic_coarse(void)
1231 do { 1269 do {
1232 seq = read_seqbegin(&timekeeper.lock); 1270 seq = read_seqbegin(&timekeeper.lock);
1233 1271
1234 now = timekeeper.xtime; 1272 now = tk_xtime(&timekeeper);
1235 mono = timekeeper.wall_to_monotonic; 1273 mono = timekeeper.wall_to_monotonic;
1236 } while (read_seqretry(&timekeeper.lock, seq)); 1274 } while (read_seqretry(&timekeeper.lock, seq));
1237 1275
@@ -1266,7 +1304,7 @@ void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
1266 1304
1267 do { 1305 do {
1268 seq = read_seqbegin(&timekeeper.lock); 1306 seq = read_seqbegin(&timekeeper.lock);
1269 *xtim = timekeeper.xtime; 1307 *xtim = tk_xtime(&timekeeper);
1270 *wtom = timekeeper.wall_to_monotonic; 1308 *wtom = timekeeper.wall_to_monotonic;
1271 *sleep = timekeeper.total_sleep_time; 1309 *sleep = timekeeper.total_sleep_time;
1272 } while (read_seqretry(&timekeeper.lock, seq)); 1310 } while (read_seqretry(&timekeeper.lock, seq));
@@ -1290,11 +1328,8 @@ ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot)
1290 do { 1328 do {
1291 seq = read_seqbegin(&timekeeper.lock); 1329 seq = read_seqbegin(&timekeeper.lock);
1292 1330
1293 secs = timekeeper.xtime.tv_sec; 1331 secs = timekeeper.xtime_sec;
1294 nsecs = timekeeper.xtime.tv_nsec; 1332 nsecs = timekeeping_get_ns(&timekeeper);
1295 nsecs += timekeeping_get_ns();
1296 /* If arch requires, add in gettimeoffset() */
1297 nsecs += arch_gettimeoffset();
1298 1333
1299 *offs_real = timekeeper.offs_real; 1334 *offs_real = timekeeper.offs_real;
1300 *offs_boot = timekeeper.offs_boot; 1335 *offs_boot = timekeeper.offs_boot;