diff options
Diffstat (limited to 'kernel/time/timekeeping.c')
| -rw-r--r-- | kernel/time/timekeeping.c | 748 |
1 files changed, 440 insertions, 308 deletions
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 6f46a00a1e8a..e16af197a2bc 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. */ |
| 25 | struct timekeeper { | 25 | struct 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,14 +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 | /* Offset clock monotonic -> clock realtime */ | ||
| 69 | ktime_t offs_real; | ||
| 68 | /* time spent in suspend */ | 70 | /* time spent in suspend */ |
| 69 | struct timespec total_sleep_time; | 71 | struct timespec total_sleep_time; |
| 72 | /* Offset clock monotonic -> clock boottime */ | ||
| 73 | ktime_t offs_boot; | ||
| 70 | /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */ | 74 | /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */ |
| 71 | struct timespec raw_time; | 75 | struct timespec raw_time; |
| 72 | |||
| 73 | /* Seqlock for all timekeeper values */ | 76 | /* Seqlock for all timekeeper values */ |
| 74 | seqlock_t lock; | 77 | seqlock_t lock; |
| 75 | }; | 78 | }; |
| 76 | 79 | ||
| 77 | static struct timekeeper timekeeper; | 80 | static struct timekeeper timekeeper; |
| @@ -82,11 +85,62 @@ static struct timekeeper timekeeper; | |||
| 82 | */ | 85 | */ |
| 83 | __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock); | 86 | __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock); |
| 84 | 87 | ||
| 85 | |||
| 86 | /* flag for if timekeeping is suspended */ | 88 | /* flag for if timekeeping is suspended */ |
| 87 | int __read_mostly timekeeping_suspended; | 89 | int __read_mostly timekeeping_suspended; |
| 88 | 90 | ||
| 91 | static 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 | } | ||
| 98 | |||
| 99 | static 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 | } | ||
| 89 | 107 | ||
| 108 | static void tk_set_xtime(struct timekeeper *tk, const struct timespec *ts) | ||
| 109 | { | ||
| 110 | tk->xtime_sec = ts->tv_sec; | ||
| 111 | tk->xtime_nsec = (u64)ts->tv_nsec << tk->shift; | ||
| 112 | } | ||
| 113 | |||
| 114 | static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts) | ||
| 115 | { | ||
| 116 | tk->xtime_sec += ts->tv_sec; | ||
| 117 | tk->xtime_nsec += (u64)ts->tv_nsec << tk->shift; | ||
| 118 | } | ||
| 119 | |||
| 120 | static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec wtm) | ||
| 121 | { | ||
| 122 | struct timespec tmp; | ||
| 123 | |||
| 124 | /* | ||
| 125 | * Verify consistency of: offset_real = -wall_to_monotonic | ||
| 126 | * before modifying anything | ||
| 127 | */ | ||
| 128 | set_normalized_timespec(&tmp, -tk->wall_to_monotonic.tv_sec, | ||
| 129 | -tk->wall_to_monotonic.tv_nsec); | ||
| 130 | WARN_ON_ONCE(tk->offs_real.tv64 != timespec_to_ktime(tmp).tv64); | ||
| 131 | tk->wall_to_monotonic = wtm; | ||
| 132 | set_normalized_timespec(&tmp, -wtm.tv_sec, -wtm.tv_nsec); | ||
| 133 | tk->offs_real = timespec_to_ktime(tmp); | ||
| 134 | } | ||
| 135 | |||
| 136 | static void tk_set_sleep_time(struct timekeeper *tk, struct timespec t) | ||
| 137 | { | ||
| 138 | /* Verify consistency before modifying */ | ||
| 139 | WARN_ON_ONCE(tk->offs_boot.tv64 != timespec_to_ktime(tk->total_sleep_time).tv64); | ||
| 140 | |||
| 141 | tk->total_sleep_time = t; | ||
| 142 | tk->offs_boot = timespec_to_ktime(t); | ||
| 143 | } | ||
| 90 | 144 | ||
| 91 | /** | 145 | /** |
| 92 | * timekeeper_setup_internals - Set up internals to use clocksource clock. | 146 | * timekeeper_setup_internals - Set up internals to use clocksource clock. |
| @@ -98,12 +152,14 @@ int __read_mostly timekeeping_suspended; | |||
| 98 | * | 152 | * |
| 99 | * Unless you're the timekeeping code, you should not be using this! | 153 | * Unless you're the timekeeping code, you should not be using this! |
| 100 | */ | 154 | */ |
| 101 | static void timekeeper_setup_internals(struct clocksource *clock) | 155 | static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) |
| 102 | { | 156 | { |
| 103 | cycle_t interval; | 157 | cycle_t interval; |
| 104 | u64 tmp, ntpinterval; | 158 | u64 tmp, ntpinterval; |
| 159 | struct clocksource *old_clock; | ||
| 105 | 160 | ||
| 106 | timekeeper.clock = clock; | 161 | old_clock = tk->clock; |
| 162 | tk->clock = clock; | ||
| 107 | clock->cycle_last = clock->read(clock); | 163 | clock->cycle_last = clock->read(clock); |
| 108 | 164 | ||
| 109 | /* Do the ns -> cycle conversion first, using original mult */ | 165 | /* Do the ns -> cycle conversion first, using original mult */ |
| @@ -116,74 +172,89 @@ static void timekeeper_setup_internals(struct clocksource *clock) | |||
| 116 | tmp = 1; | 172 | tmp = 1; |
| 117 | 173 | ||
| 118 | interval = (cycle_t) tmp; | 174 | interval = (cycle_t) tmp; |
| 119 | timekeeper.cycle_interval = interval; | 175 | tk->cycle_interval = interval; |
| 120 | 176 | ||
| 121 | /* Go back from cycles -> shifted ns */ | 177 | /* Go back from cycles -> shifted ns */ |
| 122 | timekeeper.xtime_interval = (u64) interval * clock->mult; | 178 | tk->xtime_interval = (u64) interval * clock->mult; |
| 123 | timekeeper.xtime_remainder = ntpinterval - timekeeper.xtime_interval; | 179 | tk->xtime_remainder = ntpinterval - tk->xtime_interval; |
| 124 | timekeeper.raw_interval = | 180 | tk->raw_interval = |
| 125 | ((u64) interval * clock->mult) >> clock->shift; | 181 | ((u64) interval * clock->mult) >> clock->shift; |
| 126 | 182 | ||
| 127 | timekeeper.xtime_nsec = 0; | 183 | /* if changing clocks, convert xtime_nsec shift units */ |
| 128 | timekeeper.shift = clock->shift; | 184 | if (old_clock) { |
| 185 | int shift_change = clock->shift - old_clock->shift; | ||
| 186 | if (shift_change < 0) | ||
| 187 | tk->xtime_nsec >>= -shift_change; | ||
| 188 | else | ||
| 189 | tk->xtime_nsec <<= shift_change; | ||
| 190 | } | ||
| 191 | tk->shift = clock->shift; | ||
| 129 | 192 | ||
| 130 | timekeeper.ntp_error = 0; | 193 | tk->ntp_error = 0; |
| 131 | timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift; | 194 | tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift; |
| 132 | 195 | ||
| 133 | /* | 196 | /* |
| 134 | * The timekeeper keeps its own mult values for the currently | 197 | * The timekeeper keeps its own mult values for the currently |
| 135 | * active clocksource. These value will be adjusted via NTP | 198 | * active clocksource. These value will be adjusted via NTP |
| 136 | * to counteract clock drifting. | 199 | * to counteract clock drifting. |
| 137 | */ | 200 | */ |
| 138 | timekeeper.mult = clock->mult; | 201 | tk->mult = clock->mult; |
| 139 | } | 202 | } |
| 140 | 203 | ||
| 141 | /* Timekeeper helper functions. */ | 204 | /* Timekeeper helper functions. */ |
| 142 | static inline s64 timekeeping_get_ns(void) | 205 | static inline s64 timekeeping_get_ns(struct timekeeper *tk) |
| 143 | { | 206 | { |
| 144 | cycle_t cycle_now, cycle_delta; | 207 | cycle_t cycle_now, cycle_delta; |
| 145 | struct clocksource *clock; | 208 | struct clocksource *clock; |
| 209 | s64 nsec; | ||
| 146 | 210 | ||
| 147 | /* read clocksource: */ | 211 | /* read clocksource: */ |
| 148 | clock = timekeeper.clock; | 212 | clock = tk->clock; |
| 149 | cycle_now = clock->read(clock); | 213 | cycle_now = clock->read(clock); |
| 150 | 214 | ||
| 151 | /* calculate the delta since the last update_wall_time: */ | 215 | /* calculate the delta since the last update_wall_time: */ |
| 152 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; | 216 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; |
| 153 | 217 | ||
| 154 | /* return delta convert to nanoseconds using ntp adjusted mult. */ | 218 | nsec = cycle_delta * tk->mult + tk->xtime_nsec; |
| 155 | return clocksource_cyc2ns(cycle_delta, timekeeper.mult, | 219 | nsec >>= tk->shift; |
| 156 | timekeeper.shift); | 220 | |
| 221 | /* If arch requires, add in gettimeoffset() */ | ||
| 222 | return nsec + arch_gettimeoffset(); | ||
| 157 | } | 223 | } |
| 158 | 224 | ||
| 159 | static inline s64 timekeeping_get_ns_raw(void) | 225 | static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk) |
| 160 | { | 226 | { |
| 161 | cycle_t cycle_now, cycle_delta; | 227 | cycle_t cycle_now, cycle_delta; |
| 162 | struct clocksource *clock; | 228 | struct clocksource *clock; |
| 229 | s64 nsec; | ||
| 163 | 230 | ||
| 164 | /* read clocksource: */ | 231 | /* read clocksource: */ |
| 165 | clock = timekeeper.clock; | 232 | clock = tk->clock; |
| 166 | cycle_now = clock->read(clock); | 233 | cycle_now = clock->read(clock); |
| 167 | 234 | ||
| 168 | /* calculate the delta since the last update_wall_time: */ | 235 | /* calculate the delta since the last update_wall_time: */ |
| 169 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; | 236 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; |
| 170 | 237 | ||
| 171 | /* return delta convert to nanoseconds. */ | 238 | /* convert delta to nanoseconds. */ |
| 172 | return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift); | 239 | nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift); |
| 240 | |||
| 241 | /* If arch requires, add in gettimeoffset() */ | ||
| 242 | return nsec + arch_gettimeoffset(); | ||
| 173 | } | 243 | } |
| 174 | 244 | ||
| 175 | /* must hold write on timekeeper.lock */ | 245 | /* must hold write on timekeeper.lock */ |
| 176 | static void timekeeping_update(bool clearntp) | 246 | static void timekeeping_update(struct timekeeper *tk, bool clearntp) |
| 177 | { | 247 | { |
| 248 | struct timespec xt; | ||
| 249 | |||
| 178 | if (clearntp) { | 250 | if (clearntp) { |
| 179 | timekeeper.ntp_error = 0; | 251 | tk->ntp_error = 0; |
| 180 | ntp_clear(); | 252 | ntp_clear(); |
| 181 | } | 253 | } |
| 182 | update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic, | 254 | xt = tk_xtime(tk); |
| 183 | timekeeper.clock, timekeeper.mult); | 255 | update_vsyscall(&xt, &tk->wall_to_monotonic, tk->clock, tk->mult); |
| 184 | } | 256 | } |
| 185 | 257 | ||
| 186 | |||
| 187 | /** | 258 | /** |
| 188 | * timekeeping_forward_now - update clock to the current time | 259 | * timekeeping_forward_now - update clock to the current time |
| 189 | * | 260 | * |
| @@ -191,27 +262,26 @@ static void timekeeping_update(bool clearntp) | |||
| 191 | * update_wall_time(). This is useful before significant clock changes, | 262 | * update_wall_time(). This is useful before significant clock changes, |
| 192 | * as it avoids having to deal with this time offset explicitly. | 263 | * as it avoids having to deal with this time offset explicitly. |
| 193 | */ | 264 | */ |
| 194 | static void timekeeping_forward_now(void) | 265 | static void timekeeping_forward_now(struct timekeeper *tk) |
| 195 | { | 266 | { |
| 196 | cycle_t cycle_now, cycle_delta; | 267 | cycle_t cycle_now, cycle_delta; |
| 197 | struct clocksource *clock; | 268 | struct clocksource *clock; |
| 198 | s64 nsec; | 269 | s64 nsec; |
| 199 | 270 | ||
| 200 | clock = timekeeper.clock; | 271 | clock = tk->clock; |
| 201 | cycle_now = clock->read(clock); | 272 | cycle_now = clock->read(clock); |
| 202 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; | 273 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; |
| 203 | clock->cycle_last = cycle_now; | 274 | clock->cycle_last = cycle_now; |
| 204 | 275 | ||
| 205 | nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult, | 276 | tk->xtime_nsec += cycle_delta * tk->mult; |
| 206 | timekeeper.shift); | ||
| 207 | 277 | ||
| 208 | /* If arch requires, add in gettimeoffset() */ | 278 | /* If arch requires, add in gettimeoffset() */ |
| 209 | nsec += arch_gettimeoffset(); | 279 | tk->xtime_nsec += arch_gettimeoffset() << tk->shift; |
| 210 | 280 | ||
| 211 | timespec_add_ns(&timekeeper.xtime, nsec); | 281 | tk_normalize_xtime(tk); |
| 212 | 282 | ||
| 213 | nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift); | 283 | nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift); |
| 214 | timespec_add_ns(&timekeeper.raw_time, nsec); | 284 | timespec_add_ns(&tk->raw_time, nsec); |
| 215 | } | 285 | } |
| 216 | 286 | ||
| 217 | /** | 287 | /** |
| @@ -222,21 +292,19 @@ static void timekeeping_forward_now(void) | |||
| 222 | */ | 292 | */ |
| 223 | void getnstimeofday(struct timespec *ts) | 293 | void getnstimeofday(struct timespec *ts) |
| 224 | { | 294 | { |
| 295 | struct timekeeper *tk = &timekeeper; | ||
| 225 | unsigned long seq; | 296 | unsigned long seq; |
| 226 | s64 nsecs; | 297 | s64 nsecs = 0; |
| 227 | 298 | ||
| 228 | WARN_ON(timekeeping_suspended); | 299 | WARN_ON(timekeeping_suspended); |
| 229 | 300 | ||
| 230 | do { | 301 | do { |
| 231 | seq = read_seqbegin(&timekeeper.lock); | 302 | seq = read_seqbegin(&tk->lock); |
| 232 | |||
| 233 | *ts = timekeeper.xtime; | ||
| 234 | nsecs = timekeeping_get_ns(); | ||
| 235 | 303 | ||
| 236 | /* If arch requires, add in gettimeoffset() */ | 304 | ts->tv_sec = tk->xtime_sec; |
| 237 | nsecs += arch_gettimeoffset(); | 305 | ts->tv_nsec = timekeeping_get_ns(tk); |
| 238 | 306 | ||
| 239 | } while (read_seqretry(&timekeeper.lock, seq)); | 307 | } while (read_seqretry(&tk->lock, seq)); |
| 240 | 308 | ||
| 241 | timespec_add_ns(ts, nsecs); | 309 | timespec_add_ns(ts, nsecs); |
| 242 | } | 310 | } |
| @@ -244,22 +312,18 @@ EXPORT_SYMBOL(getnstimeofday); | |||
| 244 | 312 | ||
| 245 | ktime_t ktime_get(void) | 313 | ktime_t ktime_get(void) |
| 246 | { | 314 | { |
| 315 | struct timekeeper *tk = &timekeeper; | ||
| 247 | unsigned int seq; | 316 | unsigned int seq; |
| 248 | s64 secs, nsecs; | 317 | s64 secs, nsecs; |
| 249 | 318 | ||
| 250 | WARN_ON(timekeeping_suspended); | 319 | WARN_ON(timekeeping_suspended); |
| 251 | 320 | ||
| 252 | do { | 321 | do { |
| 253 | seq = read_seqbegin(&timekeeper.lock); | 322 | seq = read_seqbegin(&tk->lock); |
| 254 | secs = timekeeper.xtime.tv_sec + | 323 | secs = tk->xtime_sec + tk->wall_to_monotonic.tv_sec; |
| 255 | timekeeper.wall_to_monotonic.tv_sec; | 324 | nsecs = timekeeping_get_ns(tk) + tk->wall_to_monotonic.tv_nsec; |
| 256 | nsecs = timekeeper.xtime.tv_nsec + | 325 | |
| 257 | timekeeper.wall_to_monotonic.tv_nsec; | 326 | } while (read_seqretry(&tk->lock, seq)); |
| 258 | nsecs += timekeeping_get_ns(); | ||
| 259 | /* If arch requires, add in gettimeoffset() */ | ||
| 260 | nsecs += arch_gettimeoffset(); | ||
| 261 | |||
| 262 | } while (read_seqretry(&timekeeper.lock, seq)); | ||
| 263 | /* | 327 | /* |
| 264 | * Use ktime_set/ktime_add_ns to create a proper ktime on | 328 | * Use ktime_set/ktime_add_ns to create a proper ktime on |
| 265 | * 32-bit architectures without CONFIG_KTIME_SCALAR. | 329 | * 32-bit architectures without CONFIG_KTIME_SCALAR. |
| @@ -278,24 +342,22 @@ EXPORT_SYMBOL_GPL(ktime_get); | |||
| 278 | */ | 342 | */ |
| 279 | void ktime_get_ts(struct timespec *ts) | 343 | void ktime_get_ts(struct timespec *ts) |
| 280 | { | 344 | { |
| 345 | struct timekeeper *tk = &timekeeper; | ||
| 281 | struct timespec tomono; | 346 | struct timespec tomono; |
| 282 | unsigned int seq; | 347 | unsigned int seq; |
| 283 | s64 nsecs; | ||
| 284 | 348 | ||
| 285 | WARN_ON(timekeeping_suspended); | 349 | WARN_ON(timekeeping_suspended); |
| 286 | 350 | ||
| 287 | do { | 351 | do { |
| 288 | seq = read_seqbegin(&timekeeper.lock); | 352 | seq = read_seqbegin(&tk->lock); |
| 289 | *ts = timekeeper.xtime; | 353 | ts->tv_sec = tk->xtime_sec; |
| 290 | tomono = timekeeper.wall_to_monotonic; | 354 | ts->tv_nsec = timekeeping_get_ns(tk); |
| 291 | nsecs = timekeeping_get_ns(); | 355 | tomono = tk->wall_to_monotonic; |
| 292 | /* If arch requires, add in gettimeoffset() */ | ||
| 293 | nsecs += arch_gettimeoffset(); | ||
| 294 | 356 | ||
| 295 | } while (read_seqretry(&timekeeper.lock, seq)); | 357 | } while (read_seqretry(&tk->lock, seq)); |
| 296 | 358 | ||
| 297 | set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec, | 359 | set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec, |
| 298 | ts->tv_nsec + tomono.tv_nsec + nsecs); | 360 | ts->tv_nsec + tomono.tv_nsec); |
| 299 | } | 361 | } |
| 300 | EXPORT_SYMBOL_GPL(ktime_get_ts); | 362 | EXPORT_SYMBOL_GPL(ktime_get_ts); |
| 301 | 363 | ||
| @@ -312,28 +374,23 @@ EXPORT_SYMBOL_GPL(ktime_get_ts); | |||
| 312 | */ | 374 | */ |
| 313 | void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real) | 375 | void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real) |
| 314 | { | 376 | { |
| 377 | struct timekeeper *tk = &timekeeper; | ||
| 315 | unsigned long seq; | 378 | unsigned long seq; |
| 316 | s64 nsecs_raw, nsecs_real; | 379 | s64 nsecs_raw, nsecs_real; |
| 317 | 380 | ||
| 318 | WARN_ON_ONCE(timekeeping_suspended); | 381 | WARN_ON_ONCE(timekeeping_suspended); |
| 319 | 382 | ||
| 320 | do { | 383 | do { |
| 321 | u32 arch_offset; | 384 | seq = read_seqbegin(&tk->lock); |
| 322 | 385 | ||
| 323 | seq = read_seqbegin(&timekeeper.lock); | 386 | *ts_raw = tk->raw_time; |
| 387 | ts_real->tv_sec = tk->xtime_sec; | ||
| 388 | ts_real->tv_nsec = 0; | ||
| 324 | 389 | ||
| 325 | *ts_raw = timekeeper.raw_time; | 390 | nsecs_raw = timekeeping_get_ns_raw(tk); |
| 326 | *ts_real = timekeeper.xtime; | 391 | nsecs_real = timekeeping_get_ns(tk); |
| 327 | 392 | ||
| 328 | nsecs_raw = timekeeping_get_ns_raw(); | 393 | } while (read_seqretry(&tk->lock, seq)); |
| 329 | nsecs_real = timekeeping_get_ns(); | ||
| 330 | |||
| 331 | /* If arch requires, add in gettimeoffset() */ | ||
| 332 | arch_offset = arch_gettimeoffset(); | ||
| 333 | nsecs_raw += arch_offset; | ||
| 334 | nsecs_real += arch_offset; | ||
| 335 | |||
| 336 | } while (read_seqretry(&timekeeper.lock, seq)); | ||
| 337 | 394 | ||
| 338 | timespec_add_ns(ts_raw, nsecs_raw); | 395 | timespec_add_ns(ts_raw, nsecs_raw); |
| 339 | timespec_add_ns(ts_real, nsecs_real); | 396 | timespec_add_ns(ts_real, nsecs_real); |
| @@ -366,25 +423,28 @@ EXPORT_SYMBOL(do_gettimeofday); | |||
| 366 | */ | 423 | */ |
| 367 | int do_settimeofday(const struct timespec *tv) | 424 | int do_settimeofday(const struct timespec *tv) |
| 368 | { | 425 | { |
| 369 | struct timespec ts_delta; | 426 | struct timekeeper *tk = &timekeeper; |
| 427 | struct timespec ts_delta, xt; | ||
| 370 | unsigned long flags; | 428 | unsigned long flags; |
| 371 | 429 | ||
| 372 | if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) | 430 | if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) |
| 373 | return -EINVAL; | 431 | return -EINVAL; |
| 374 | 432 | ||
| 375 | write_seqlock_irqsave(&timekeeper.lock, flags); | 433 | write_seqlock_irqsave(&tk->lock, flags); |
| 434 | |||
| 435 | timekeeping_forward_now(tk); | ||
| 376 | 436 | ||
| 377 | timekeeping_forward_now(); | 437 | xt = tk_xtime(tk); |
| 438 | ts_delta.tv_sec = tv->tv_sec - xt.tv_sec; | ||
| 439 | ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec; | ||
| 378 | 440 | ||
| 379 | ts_delta.tv_sec = tv->tv_sec - timekeeper.xtime.tv_sec; | 441 | tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, ts_delta)); |
| 380 | ts_delta.tv_nsec = tv->tv_nsec - timekeeper.xtime.tv_nsec; | ||
| 381 | timekeeper.wall_to_monotonic = | ||
| 382 | timespec_sub(timekeeper.wall_to_monotonic, ts_delta); | ||
| 383 | 442 | ||
| 384 | timekeeper.xtime = *tv; | 443 | tk_set_xtime(tk, tv); |
| 385 | timekeeping_update(true); | ||
| 386 | 444 | ||
| 387 | write_sequnlock_irqrestore(&timekeeper.lock, flags); | 445 | timekeeping_update(tk, true); |
| 446 | |||
| 447 | write_sequnlock_irqrestore(&tk->lock, flags); | ||
| 388 | 448 | ||
| 389 | /* signal hrtimers about time change */ | 449 | /* signal hrtimers about time change */ |
| 390 | clock_was_set(); | 450 | clock_was_set(); |
| @@ -393,7 +453,6 @@ int do_settimeofday(const struct timespec *tv) | |||
| 393 | } | 453 | } |
| 394 | EXPORT_SYMBOL(do_settimeofday); | 454 | EXPORT_SYMBOL(do_settimeofday); |
| 395 | 455 | ||
| 396 | |||
| 397 | /** | 456 | /** |
| 398 | * timekeeping_inject_offset - Adds or subtracts from the current time. | 457 | * timekeeping_inject_offset - Adds or subtracts from the current time. |
| 399 | * @tv: pointer to the timespec variable containing the offset | 458 | * @tv: pointer to the timespec variable containing the offset |
| @@ -402,22 +461,23 @@ EXPORT_SYMBOL(do_settimeofday); | |||
| 402 | */ | 461 | */ |
| 403 | int timekeeping_inject_offset(struct timespec *ts) | 462 | int timekeeping_inject_offset(struct timespec *ts) |
| 404 | { | 463 | { |
| 464 | struct timekeeper *tk = &timekeeper; | ||
| 405 | unsigned long flags; | 465 | unsigned long flags; |
| 406 | 466 | ||
| 407 | if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) | 467 | if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) |
| 408 | return -EINVAL; | 468 | return -EINVAL; |
| 409 | 469 | ||
| 410 | write_seqlock_irqsave(&timekeeper.lock, flags); | 470 | write_seqlock_irqsave(&tk->lock, flags); |
| 411 | 471 | ||
| 412 | timekeeping_forward_now(); | 472 | timekeeping_forward_now(tk); |
| 413 | 473 | ||
| 414 | timekeeper.xtime = timespec_add(timekeeper.xtime, *ts); | ||
| 415 | timekeeper.wall_to_monotonic = | ||
| 416 | timespec_sub(timekeeper.wall_to_monotonic, *ts); | ||
| 417 | 474 | ||
| 418 | timekeeping_update(true); | 475 | tk_xtime_add(tk, ts); |
| 476 | tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *ts)); | ||
| 419 | 477 | ||
| 420 | write_sequnlock_irqrestore(&timekeeper.lock, flags); | 478 | timekeeping_update(tk, true); |
| 479 | |||
| 480 | write_sequnlock_irqrestore(&tk->lock, flags); | ||
| 421 | 481 | ||
| 422 | /* signal hrtimers about time change */ | 482 | /* signal hrtimers about time change */ |
| 423 | clock_was_set(); | 483 | clock_was_set(); |
| @@ -433,23 +493,24 @@ EXPORT_SYMBOL(timekeeping_inject_offset); | |||
| 433 | */ | 493 | */ |
| 434 | static int change_clocksource(void *data) | 494 | static int change_clocksource(void *data) |
| 435 | { | 495 | { |
| 496 | struct timekeeper *tk = &timekeeper; | ||
| 436 | struct clocksource *new, *old; | 497 | struct clocksource *new, *old; |
| 437 | unsigned long flags; | 498 | unsigned long flags; |
| 438 | 499 | ||
| 439 | new = (struct clocksource *) data; | 500 | new = (struct clocksource *) data; |
| 440 | 501 | ||
| 441 | write_seqlock_irqsave(&timekeeper.lock, flags); | 502 | write_seqlock_irqsave(&tk->lock, flags); |
| 442 | 503 | ||
| 443 | timekeeping_forward_now(); | 504 | timekeeping_forward_now(tk); |
| 444 | if (!new->enable || new->enable(new) == 0) { | 505 | if (!new->enable || new->enable(new) == 0) { |
| 445 | old = timekeeper.clock; | 506 | old = tk->clock; |
| 446 | timekeeper_setup_internals(new); | 507 | tk_setup_internals(tk, new); |
| 447 | if (old->disable) | 508 | if (old->disable) |
| 448 | old->disable(old); | 509 | old->disable(old); |
| 449 | } | 510 | } |
| 450 | timekeeping_update(true); | 511 | timekeeping_update(tk, true); |
| 451 | 512 | ||
| 452 | write_sequnlock_irqrestore(&timekeeper.lock, flags); | 513 | write_sequnlock_irqrestore(&tk->lock, flags); |
| 453 | 514 | ||
| 454 | return 0; | 515 | return 0; |
| 455 | } | 516 | } |
| @@ -463,7 +524,9 @@ static int change_clocksource(void *data) | |||
| 463 | */ | 524 | */ |
| 464 | void timekeeping_notify(struct clocksource *clock) | 525 | void timekeeping_notify(struct clocksource *clock) |
| 465 | { | 526 | { |
| 466 | if (timekeeper.clock == clock) | 527 | struct timekeeper *tk = &timekeeper; |
| 528 | |||
| 529 | if (tk->clock == clock) | ||
| 467 | return; | 530 | return; |
| 468 | stop_machine(change_clocksource, clock, NULL); | 531 | stop_machine(change_clocksource, clock, NULL); |
| 469 | tick_clock_notify(); | 532 | tick_clock_notify(); |
| @@ -492,35 +555,36 @@ EXPORT_SYMBOL_GPL(ktime_get_real); | |||
| 492 | */ | 555 | */ |
| 493 | void getrawmonotonic(struct timespec *ts) | 556 | void getrawmonotonic(struct timespec *ts) |
| 494 | { | 557 | { |
| 558 | struct timekeeper *tk = &timekeeper; | ||
| 495 | unsigned long seq; | 559 | unsigned long seq; |
| 496 | s64 nsecs; | 560 | s64 nsecs; |
| 497 | 561 | ||
| 498 | do { | 562 | do { |
| 499 | seq = read_seqbegin(&timekeeper.lock); | 563 | seq = read_seqbegin(&tk->lock); |
| 500 | nsecs = timekeeping_get_ns_raw(); | 564 | nsecs = timekeeping_get_ns_raw(tk); |
| 501 | *ts = timekeeper.raw_time; | 565 | *ts = tk->raw_time; |
| 502 | 566 | ||
| 503 | } while (read_seqretry(&timekeeper.lock, seq)); | 567 | } while (read_seqretry(&tk->lock, seq)); |
| 504 | 568 | ||
| 505 | timespec_add_ns(ts, nsecs); | 569 | timespec_add_ns(ts, nsecs); |
| 506 | } | 570 | } |
| 507 | EXPORT_SYMBOL(getrawmonotonic); | 571 | EXPORT_SYMBOL(getrawmonotonic); |
| 508 | 572 | ||
| 509 | |||
| 510 | /** | 573 | /** |
| 511 | * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres | 574 | * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres |
| 512 | */ | 575 | */ |
| 513 | int timekeeping_valid_for_hres(void) | 576 | int timekeeping_valid_for_hres(void) |
| 514 | { | 577 | { |
| 578 | struct timekeeper *tk = &timekeeper; | ||
| 515 | unsigned long seq; | 579 | unsigned long seq; |
| 516 | int ret; | 580 | int ret; |
| 517 | 581 | ||
| 518 | do { | 582 | do { |
| 519 | seq = read_seqbegin(&timekeeper.lock); | 583 | seq = read_seqbegin(&tk->lock); |
| 520 | 584 | ||
| 521 | ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; | 585 | ret = tk->clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; |
| 522 | 586 | ||
| 523 | } while (read_seqretry(&timekeeper.lock, seq)); | 587 | } while (read_seqretry(&tk->lock, seq)); |
| 524 | 588 | ||
| 525 | return ret; | 589 | return ret; |
| 526 | } | 590 | } |
| @@ -530,14 +594,16 @@ int timekeeping_valid_for_hres(void) | |||
| 530 | */ | 594 | */ |
| 531 | u64 timekeeping_max_deferment(void) | 595 | u64 timekeeping_max_deferment(void) |
| 532 | { | 596 | { |
| 597 | struct timekeeper *tk = &timekeeper; | ||
| 533 | unsigned long seq; | 598 | unsigned long seq; |
| 534 | u64 ret; | 599 | u64 ret; |
| 600 | |||
| 535 | do { | 601 | do { |
| 536 | seq = read_seqbegin(&timekeeper.lock); | 602 | seq = read_seqbegin(&tk->lock); |
| 537 | 603 | ||
| 538 | ret = timekeeper.clock->max_idle_ns; | 604 | ret = tk->clock->max_idle_ns; |
| 539 | 605 | ||
| 540 | } while (read_seqretry(&timekeeper.lock, seq)); | 606 | } while (read_seqretry(&tk->lock, seq)); |
| 541 | 607 | ||
| 542 | return ret; | 608 | return ret; |
| 543 | } | 609 | } |
| @@ -577,36 +643,38 @@ void __attribute__((weak)) read_boot_clock(struct timespec *ts) | |||
| 577 | */ | 643 | */ |
| 578 | void __init timekeeping_init(void) | 644 | void __init timekeeping_init(void) |
| 579 | { | 645 | { |
| 646 | struct timekeeper *tk = &timekeeper; | ||
| 580 | struct clocksource *clock; | 647 | struct clocksource *clock; |
| 581 | unsigned long flags; | 648 | unsigned long flags; |
| 582 | struct timespec now, boot; | 649 | struct timespec now, boot, tmp; |
| 583 | 650 | ||
| 584 | read_persistent_clock(&now); | 651 | read_persistent_clock(&now); |
| 585 | read_boot_clock(&boot); | 652 | read_boot_clock(&boot); |
| 586 | 653 | ||
| 587 | seqlock_init(&timekeeper.lock); | 654 | seqlock_init(&tk->lock); |
| 588 | 655 | ||
| 589 | ntp_init(); | 656 | ntp_init(); |
| 590 | 657 | ||
| 591 | write_seqlock_irqsave(&timekeeper.lock, flags); | 658 | write_seqlock_irqsave(&tk->lock, flags); |
| 592 | clock = clocksource_default_clock(); | 659 | clock = clocksource_default_clock(); |
| 593 | if (clock->enable) | 660 | if (clock->enable) |
| 594 | clock->enable(clock); | 661 | clock->enable(clock); |
| 595 | timekeeper_setup_internals(clock); | 662 | tk_setup_internals(tk, clock); |
| 596 | 663 | ||
| 597 | timekeeper.xtime.tv_sec = now.tv_sec; | 664 | tk_set_xtime(tk, &now); |
| 598 | timekeeper.xtime.tv_nsec = now.tv_nsec; | 665 | tk->raw_time.tv_sec = 0; |
| 599 | timekeeper.raw_time.tv_sec = 0; | 666 | tk->raw_time.tv_nsec = 0; |
| 600 | timekeeper.raw_time.tv_nsec = 0; | 667 | if (boot.tv_sec == 0 && boot.tv_nsec == 0) |
| 601 | if (boot.tv_sec == 0 && boot.tv_nsec == 0) { | 668 | boot = tk_xtime(tk); |
| 602 | boot.tv_sec = timekeeper.xtime.tv_sec; | 669 | |
| 603 | boot.tv_nsec = timekeeper.xtime.tv_nsec; | 670 | set_normalized_timespec(&tmp, -boot.tv_sec, -boot.tv_nsec); |
| 604 | } | 671 | tk_set_wall_to_mono(tk, tmp); |
| 605 | set_normalized_timespec(&timekeeper.wall_to_monotonic, | 672 | |
| 606 | -boot.tv_sec, -boot.tv_nsec); | 673 | tmp.tv_sec = 0; |
| 607 | timekeeper.total_sleep_time.tv_sec = 0; | 674 | tmp.tv_nsec = 0; |
| 608 | timekeeper.total_sleep_time.tv_nsec = 0; | 675 | tk_set_sleep_time(tk, tmp); |
| 609 | write_sequnlock_irqrestore(&timekeeper.lock, flags); | 676 | |
| 677 | write_sequnlock_irqrestore(&tk->lock, flags); | ||
| 610 | } | 678 | } |
| 611 | 679 | ||
| 612 | /* time in seconds when suspend began */ | 680 | /* time in seconds when suspend began */ |
| @@ -619,22 +687,19 @@ static struct timespec timekeeping_suspend_time; | |||
| 619 | * Takes a timespec offset measuring a suspend interval and properly | 687 | * Takes a timespec offset measuring a suspend interval and properly |
| 620 | * adds the sleep offset to the timekeeping variables. | 688 | * adds the sleep offset to the timekeeping variables. |
| 621 | */ | 689 | */ |
| 622 | static void __timekeeping_inject_sleeptime(struct timespec *delta) | 690 | static void __timekeeping_inject_sleeptime(struct timekeeper *tk, |
| 691 | struct timespec *delta) | ||
| 623 | { | 692 | { |
| 624 | if (!timespec_valid(delta)) { | 693 | if (!timespec_valid(delta)) { |
| 625 | printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid " | 694 | printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid " |
| 626 | "sleep delta value!\n"); | 695 | "sleep delta value!\n"); |
| 627 | return; | 696 | return; |
| 628 | } | 697 | } |
| 629 | 698 | tk_xtime_add(tk, delta); | |
| 630 | timekeeper.xtime = timespec_add(timekeeper.xtime, *delta); | 699 | tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *delta)); |
| 631 | timekeeper.wall_to_monotonic = | 700 | tk_set_sleep_time(tk, timespec_add(tk->total_sleep_time, *delta)); |
| 632 | timespec_sub(timekeeper.wall_to_monotonic, *delta); | ||
| 633 | timekeeper.total_sleep_time = timespec_add( | ||
| 634 | timekeeper.total_sleep_time, *delta); | ||
| 635 | } | 701 | } |
| 636 | 702 | ||
| 637 | |||
| 638 | /** | 703 | /** |
| 639 | * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values | 704 | * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values |
| 640 | * @delta: pointer to a timespec delta value | 705 | * @delta: pointer to a timespec delta value |
| @@ -647,6 +712,7 @@ static void __timekeeping_inject_sleeptime(struct timespec *delta) | |||
| 647 | */ | 712 | */ |
| 648 | void timekeeping_inject_sleeptime(struct timespec *delta) | 713 | void timekeeping_inject_sleeptime(struct timespec *delta) |
| 649 | { | 714 | { |
| 715 | struct timekeeper *tk = &timekeeper; | ||
| 650 | unsigned long flags; | 716 | unsigned long flags; |
| 651 | struct timespec ts; | 717 | struct timespec ts; |
| 652 | 718 | ||
| @@ -655,21 +721,20 @@ void timekeeping_inject_sleeptime(struct timespec *delta) | |||
| 655 | if (!(ts.tv_sec == 0 && ts.tv_nsec == 0)) | 721 | if (!(ts.tv_sec == 0 && ts.tv_nsec == 0)) |
| 656 | return; | 722 | return; |
| 657 | 723 | ||
| 658 | write_seqlock_irqsave(&timekeeper.lock, flags); | 724 | write_seqlock_irqsave(&tk->lock, flags); |
| 659 | 725 | ||
| 660 | timekeeping_forward_now(); | 726 | timekeeping_forward_now(tk); |
| 661 | 727 | ||
| 662 | __timekeeping_inject_sleeptime(delta); | 728 | __timekeeping_inject_sleeptime(tk, delta); |
| 663 | 729 | ||
| 664 | timekeeping_update(true); | 730 | timekeeping_update(tk, true); |
| 665 | 731 | ||
| 666 | write_sequnlock_irqrestore(&timekeeper.lock, flags); | 732 | write_sequnlock_irqrestore(&tk->lock, flags); |
| 667 | 733 | ||
| 668 | /* signal hrtimers about time change */ | 734 | /* signal hrtimers about time change */ |
| 669 | clock_was_set(); | 735 | clock_was_set(); |
| 670 | } | 736 | } |
| 671 | 737 | ||
| 672 | |||
| 673 | /** | 738 | /** |
| 674 | * timekeeping_resume - Resumes the generic timekeeping subsystem. | 739 | * timekeeping_resume - Resumes the generic timekeeping subsystem. |
| 675 | * | 740 | * |
| @@ -679,6 +744,7 @@ void timekeeping_inject_sleeptime(struct timespec *delta) | |||
| 679 | */ | 744 | */ |
| 680 | static void timekeeping_resume(void) | 745 | static void timekeeping_resume(void) |
| 681 | { | 746 | { |
| 747 | struct timekeeper *tk = &timekeeper; | ||
| 682 | unsigned long flags; | 748 | unsigned long flags; |
| 683 | struct timespec ts; | 749 | struct timespec ts; |
| 684 | 750 | ||
| @@ -686,17 +752,18 @@ static void timekeeping_resume(void) | |||
| 686 | 752 | ||
| 687 | clocksource_resume(); | 753 | clocksource_resume(); |
| 688 | 754 | ||
| 689 | write_seqlock_irqsave(&timekeeper.lock, flags); | 755 | write_seqlock_irqsave(&tk->lock, flags); |
| 690 | 756 | ||
| 691 | if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) { | 757 | if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) { |
| 692 | ts = timespec_sub(ts, timekeeping_suspend_time); | 758 | ts = timespec_sub(ts, timekeeping_suspend_time); |
| 693 | __timekeeping_inject_sleeptime(&ts); | 759 | __timekeeping_inject_sleeptime(tk, &ts); |
| 694 | } | 760 | } |
| 695 | /* re-base the last cycle value */ | 761 | /* re-base the last cycle value */ |
| 696 | timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock); | 762 | tk->clock->cycle_last = tk->clock->read(tk->clock); |
| 697 | timekeeper.ntp_error = 0; | 763 | tk->ntp_error = 0; |
| 698 | timekeeping_suspended = 0; | 764 | timekeeping_suspended = 0; |
| 699 | write_sequnlock_irqrestore(&timekeeper.lock, flags); | 765 | timekeeping_update(tk, false); |
| 766 | write_sequnlock_irqrestore(&tk->lock, flags); | ||
| 700 | 767 | ||
| 701 | touch_softlockup_watchdog(); | 768 | touch_softlockup_watchdog(); |
| 702 | 769 | ||
| @@ -708,14 +775,15 @@ static void timekeeping_resume(void) | |||
| 708 | 775 | ||
| 709 | static int timekeeping_suspend(void) | 776 | static int timekeeping_suspend(void) |
| 710 | { | 777 | { |
| 778 | struct timekeeper *tk = &timekeeper; | ||
| 711 | unsigned long flags; | 779 | unsigned long flags; |
| 712 | struct timespec delta, delta_delta; | 780 | struct timespec delta, delta_delta; |
| 713 | static struct timespec old_delta; | 781 | static struct timespec old_delta; |
| 714 | 782 | ||
| 715 | read_persistent_clock(&timekeeping_suspend_time); | 783 | read_persistent_clock(&timekeeping_suspend_time); |
| 716 | 784 | ||
| 717 | write_seqlock_irqsave(&timekeeper.lock, flags); | 785 | write_seqlock_irqsave(&tk->lock, flags); |
| 718 | timekeeping_forward_now(); | 786 | timekeeping_forward_now(tk); |
| 719 | timekeeping_suspended = 1; | 787 | timekeeping_suspended = 1; |
| 720 | 788 | ||
| 721 | /* | 789 | /* |
| @@ -724,7 +792,7 @@ static int timekeeping_suspend(void) | |||
| 724 | * try to compensate so the difference in system time | 792 | * try to compensate so the difference in system time |
| 725 | * and persistent_clock time stays close to constant. | 793 | * and persistent_clock time stays close to constant. |
| 726 | */ | 794 | */ |
| 727 | delta = timespec_sub(timekeeper.xtime, timekeeping_suspend_time); | 795 | delta = timespec_sub(tk_xtime(tk), timekeeping_suspend_time); |
| 728 | delta_delta = timespec_sub(delta, old_delta); | 796 | delta_delta = timespec_sub(delta, old_delta); |
| 729 | if (abs(delta_delta.tv_sec) >= 2) { | 797 | if (abs(delta_delta.tv_sec) >= 2) { |
| 730 | /* | 798 | /* |
| @@ -737,7 +805,7 @@ static int timekeeping_suspend(void) | |||
| 737 | timekeeping_suspend_time = | 805 | timekeeping_suspend_time = |
| 738 | timespec_add(timekeeping_suspend_time, delta_delta); | 806 | timespec_add(timekeeping_suspend_time, delta_delta); |
| 739 | } | 807 | } |
| 740 | write_sequnlock_irqrestore(&timekeeper.lock, flags); | 808 | write_sequnlock_irqrestore(&tk->lock, flags); |
| 741 | 809 | ||
| 742 | clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL); | 810 | clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL); |
| 743 | clocksource_suspend(); | 811 | clocksource_suspend(); |
| @@ -763,7 +831,8 @@ device_initcall(timekeeping_init_ops); | |||
| 763 | * If the error is already larger, we look ahead even further | 831 | * If the error is already larger, we look ahead even further |
| 764 | * to compensate for late or lost adjustments. | 832 | * to compensate for late or lost adjustments. |
| 765 | */ | 833 | */ |
| 766 | static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval, | 834 | static __always_inline int timekeeping_bigadjust(struct timekeeper *tk, |
| 835 | s64 error, s64 *interval, | ||
| 767 | s64 *offset) | 836 | s64 *offset) |
| 768 | { | 837 | { |
| 769 | s64 tick_error, i; | 838 | s64 tick_error, i; |
| @@ -779,7 +848,7 @@ static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval, | |||
| 779 | * here. This is tuned so that an error of about 1 msec is adjusted | 848 | * here. This is tuned so that an error of about 1 msec is adjusted |
| 780 | * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks). | 849 | * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks). |
| 781 | */ | 850 | */ |
| 782 | error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ); | 851 | error2 = tk->ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ); |
| 783 | error2 = abs(error2); | 852 | error2 = abs(error2); |
| 784 | for (look_ahead = 0; error2 > 0; look_ahead++) | 853 | for (look_ahead = 0; error2 > 0; look_ahead++) |
| 785 | error2 >>= 2; | 854 | error2 >>= 2; |
| @@ -788,8 +857,8 @@ static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval, | |||
| 788 | * Now calculate the error in (1 << look_ahead) ticks, but first | 857 | * Now calculate the error in (1 << look_ahead) ticks, but first |
| 789 | * remove the single look ahead already included in the error. | 858 | * remove the single look ahead already included in the error. |
| 790 | */ | 859 | */ |
| 791 | tick_error = ntp_tick_length() >> (timekeeper.ntp_error_shift + 1); | 860 | tick_error = ntp_tick_length() >> (tk->ntp_error_shift + 1); |
| 792 | tick_error -= timekeeper.xtime_interval >> 1; | 861 | tick_error -= tk->xtime_interval >> 1; |
| 793 | error = ((error - tick_error) >> look_ahead) + tick_error; | 862 | error = ((error - tick_error) >> look_ahead) + tick_error; |
| 794 | 863 | ||
| 795 | /* Finally calculate the adjustment shift value. */ | 864 | /* Finally calculate the adjustment shift value. */ |
| @@ -814,9 +883,9 @@ static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval, | |||
| 814 | * this is optimized for the most common adjustments of -1,0,1, | 883 | * this is optimized for the most common adjustments of -1,0,1, |
| 815 | * for other values we can do a bit more work. | 884 | * for other values we can do a bit more work. |
| 816 | */ | 885 | */ |
| 817 | static void timekeeping_adjust(s64 offset) | 886 | static void timekeeping_adjust(struct timekeeper *tk, s64 offset) |
| 818 | { | 887 | { |
| 819 | s64 error, interval = timekeeper.cycle_interval; | 888 | s64 error, interval = tk->cycle_interval; |
| 820 | int adj; | 889 | int adj; |
| 821 | 890 | ||
| 822 | /* | 891 | /* |
| @@ -832,7 +901,7 @@ static void timekeeping_adjust(s64 offset) | |||
| 832 | * | 901 | * |
| 833 | * Note: It does not "save" on aggravation when reading the code. | 902 | * Note: It does not "save" on aggravation when reading the code. |
| 834 | */ | 903 | */ |
| 835 | error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1); | 904 | error = tk->ntp_error >> (tk->ntp_error_shift - 1); |
| 836 | if (error > interval) { | 905 | if (error > interval) { |
| 837 | /* | 906 | /* |
| 838 | * We now divide error by 4(via shift), which checks if | 907 | * We now divide error by 4(via shift), which checks if |
| @@ -847,34 +916,36 @@ static void timekeeping_adjust(s64 offset) | |||
| 847 | * the error. This causes the likely below to be unlikely. | 916 | * the error. This causes the likely below to be unlikely. |
| 848 | * | 917 | * |
| 849 | * The proper fix is to avoid rounding up by using | 918 | * The proper fix is to avoid rounding up by using |
| 850 | * the high precision timekeeper.xtime_nsec instead of | 919 | * the high precision tk->xtime_nsec instead of |
| 851 | * xtime.tv_nsec everywhere. Fixing this will take some | 920 | * xtime.tv_nsec everywhere. Fixing this will take some |
| 852 | * time. | 921 | * time. |
| 853 | */ | 922 | */ |
| 854 | if (likely(error <= interval)) | 923 | if (likely(error <= interval)) |
| 855 | adj = 1; | 924 | adj = 1; |
| 856 | else | 925 | else |
| 857 | adj = timekeeping_bigadjust(error, &interval, &offset); | 926 | adj = timekeeping_bigadjust(tk, error, &interval, &offset); |
| 858 | } else if (error < -interval) { | 927 | } else { |
| 859 | /* See comment above, this is just switched for the negative */ | 928 | if (error < -interval) { |
| 860 | error >>= 2; | 929 | /* See comment above, this is just switched for the negative */ |
| 861 | if (likely(error >= -interval)) { | 930 | error >>= 2; |
| 862 | adj = -1; | 931 | if (likely(error >= -interval)) { |
| 863 | interval = -interval; | 932 | adj = -1; |
| 864 | offset = -offset; | 933 | interval = -interval; |
| 865 | } else | 934 | offset = -offset; |
| 866 | adj = timekeeping_bigadjust(error, &interval, &offset); | 935 | } else { |
| 867 | } else /* No adjustment needed */ | 936 | adj = timekeeping_bigadjust(tk, error, &interval, &offset); |
| 868 | return; | 937 | } |
| 938 | } else { | ||
| 939 | goto out_adjust; | ||
| 940 | } | ||
| 941 | } | ||
| 869 | 942 | ||
| 870 | if (unlikely(timekeeper.clock->maxadj && | 943 | if (unlikely(tk->clock->maxadj && |
| 871 | (timekeeper.mult + adj > | 944 | (tk->mult + adj > tk->clock->mult + tk->clock->maxadj))) { |
| 872 | timekeeper.clock->mult + timekeeper.clock->maxadj))) { | ||
| 873 | printk_once(KERN_WARNING | 945 | printk_once(KERN_WARNING |
| 874 | "Adjusting %s more than 11%% (%ld vs %ld)\n", | 946 | "Adjusting %s more than 11%% (%ld vs %ld)\n", |
| 875 | timekeeper.clock->name, (long)timekeeper.mult + adj, | 947 | tk->clock->name, (long)tk->mult + adj, |
| 876 | (long)timekeeper.clock->mult + | 948 | (long)tk->clock->mult + tk->clock->maxadj); |
| 877 | timekeeper.clock->maxadj); | ||
| 878 | } | 949 | } |
| 879 | /* | 950 | /* |
| 880 | * So the following can be confusing. | 951 | * So the following can be confusing. |
| @@ -925,13 +996,68 @@ static void timekeeping_adjust(s64 offset) | |||
| 925 | * | 996 | * |
| 926 | * XXX - TODO: Doc ntp_error calculation. | 997 | * XXX - TODO: Doc ntp_error calculation. |
| 927 | */ | 998 | */ |
| 928 | timekeeper.mult += adj; | 999 | tk->mult += adj; |
| 929 | timekeeper.xtime_interval += interval; | 1000 | tk->xtime_interval += interval; |
| 930 | timekeeper.xtime_nsec -= offset; | 1001 | tk->xtime_nsec -= offset; |
| 931 | timekeeper.ntp_error -= (interval - offset) << | 1002 | tk->ntp_error -= (interval - offset) << tk->ntp_error_shift; |
| 932 | timekeeper.ntp_error_shift; | 1003 | |
| 1004 | out_adjust: | ||
| 1005 | /* | ||
| 1006 | * It may be possible that when we entered this function, xtime_nsec | ||
| 1007 | * was very small. Further, if we're slightly speeding the clocksource | ||
| 1008 | * in the code above, its possible the required corrective factor to | ||
| 1009 | * xtime_nsec could cause it to underflow. | ||
| 1010 | * | ||
| 1011 | * Now, since we already accumulated the second, cannot simply roll | ||
| 1012 | * the accumulated second back, since the NTP subsystem has been | ||
| 1013 | * notified via second_overflow. So instead we push xtime_nsec forward | ||
| 1014 | * by the amount we underflowed, and add that amount into the error. | ||
| 1015 | * | ||
| 1016 | * We'll correct this error next time through this function, when | ||
| 1017 | * xtime_nsec is not as small. | ||
| 1018 | */ | ||
| 1019 | if (unlikely((s64)tk->xtime_nsec < 0)) { | ||
| 1020 | s64 neg = -(s64)tk->xtime_nsec; | ||
| 1021 | tk->xtime_nsec = 0; | ||
| 1022 | tk->ntp_error += neg << tk->ntp_error_shift; | ||
| 1023 | } | ||
| 1024 | |||
| 933 | } | 1025 | } |
| 934 | 1026 | ||
| 1027 | /** | ||
| 1028 | * accumulate_nsecs_to_secs - Accumulates nsecs into secs | ||
| 1029 | * | ||
| 1030 | * Helper function that accumulates a the nsecs greater then a second | ||
| 1031 | * from the xtime_nsec field to the xtime_secs field. | ||
| 1032 | * It also calls into the NTP code to handle leapsecond processing. | ||
| 1033 | * | ||
| 1034 | */ | ||
| 1035 | static inline void accumulate_nsecs_to_secs(struct timekeeper *tk) | ||
| 1036 | { | ||
| 1037 | u64 nsecps = (u64)NSEC_PER_SEC << tk->shift; | ||
| 1038 | |||
| 1039 | while (tk->xtime_nsec >= nsecps) { | ||
| 1040 | int leap; | ||
| 1041 | |||
| 1042 | tk->xtime_nsec -= nsecps; | ||
| 1043 | tk->xtime_sec++; | ||
| 1044 | |||
| 1045 | /* Figure out if its a leap sec and apply if needed */ | ||
| 1046 | leap = second_overflow(tk->xtime_sec); | ||
| 1047 | if (unlikely(leap)) { | ||
| 1048 | struct timespec ts; | ||
| 1049 | |||
| 1050 | tk->xtime_sec += leap; | ||
| 1051 | |||
| 1052 | ts.tv_sec = leap; | ||
| 1053 | ts.tv_nsec = 0; | ||
| 1054 | tk_set_wall_to_mono(tk, | ||
| 1055 | timespec_sub(tk->wall_to_monotonic, ts)); | ||
| 1056 | |||
| 1057 | clock_was_set_delayed(); | ||
| 1058 | } | ||
| 1059 | } | ||
| 1060 | } | ||
| 935 | 1061 | ||
| 936 | /** | 1062 | /** |
| 937 | * logarithmic_accumulation - shifted accumulation of cycles | 1063 | * logarithmic_accumulation - shifted accumulation of cycles |
| @@ -942,49 +1068,40 @@ static void timekeeping_adjust(s64 offset) | |||
| 942 | * | 1068 | * |
| 943 | * Returns the unconsumed cycles. | 1069 | * Returns the unconsumed cycles. |
| 944 | */ | 1070 | */ |
| 945 | static cycle_t logarithmic_accumulation(cycle_t offset, int shift) | 1071 | static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset, |
| 1072 | u32 shift) | ||
| 946 | { | 1073 | { |
| 947 | u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift; | ||
| 948 | u64 raw_nsecs; | 1074 | u64 raw_nsecs; |
| 949 | 1075 | ||
| 950 | /* If the offset is smaller than a shifted interval, do nothing */ | 1076 | /* If the offset is smaller then a shifted interval, do nothing */ |
| 951 | if (offset < timekeeper.cycle_interval<<shift) | 1077 | if (offset < tk->cycle_interval<<shift) |
| 952 | return offset; | 1078 | return offset; |
| 953 | 1079 | ||
| 954 | /* Accumulate one shifted interval */ | 1080 | /* Accumulate one shifted interval */ |
| 955 | offset -= timekeeper.cycle_interval << shift; | 1081 | offset -= tk->cycle_interval << shift; |
| 956 | timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift; | 1082 | tk->clock->cycle_last += tk->cycle_interval << shift; |
| 957 | 1083 | ||
| 958 | timekeeper.xtime_nsec += timekeeper.xtime_interval << shift; | 1084 | tk->xtime_nsec += tk->xtime_interval << shift; |
| 959 | while (timekeeper.xtime_nsec >= nsecps) { | 1085 | accumulate_nsecs_to_secs(tk); |
| 960 | int leap; | ||
| 961 | timekeeper.xtime_nsec -= nsecps; | ||
| 962 | timekeeper.xtime.tv_sec++; | ||
| 963 | leap = second_overflow(timekeeper.xtime.tv_sec); | ||
| 964 | timekeeper.xtime.tv_sec += leap; | ||
| 965 | timekeeper.wall_to_monotonic.tv_sec -= leap; | ||
| 966 | } | ||
| 967 | 1086 | ||
| 968 | /* Accumulate raw time */ | 1087 | /* Accumulate raw time */ |
| 969 | raw_nsecs = timekeeper.raw_interval << shift; | 1088 | raw_nsecs = tk->raw_interval << shift; |
| 970 | raw_nsecs += timekeeper.raw_time.tv_nsec; | 1089 | raw_nsecs += tk->raw_time.tv_nsec; |
| 971 | if (raw_nsecs >= NSEC_PER_SEC) { | 1090 | if (raw_nsecs >= NSEC_PER_SEC) { |
| 972 | u64 raw_secs = raw_nsecs; | 1091 | u64 raw_secs = raw_nsecs; |
| 973 | raw_nsecs = do_div(raw_secs, NSEC_PER_SEC); | 1092 | raw_nsecs = do_div(raw_secs, NSEC_PER_SEC); |
| 974 | timekeeper.raw_time.tv_sec += raw_secs; | 1093 | tk->raw_time.tv_sec += raw_secs; |
| 975 | } | 1094 | } |
| 976 | timekeeper.raw_time.tv_nsec = raw_nsecs; | 1095 | tk->raw_time.tv_nsec = raw_nsecs; |
| 977 | 1096 | ||
| 978 | /* Accumulate error between NTP and clock interval */ | 1097 | /* Accumulate error between NTP and clock interval */ |
| 979 | timekeeper.ntp_error += ntp_tick_length() << shift; | 1098 | tk->ntp_error += ntp_tick_length() << shift; |
| 980 | timekeeper.ntp_error -= | 1099 | tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) << |
| 981 | (timekeeper.xtime_interval + timekeeper.xtime_remainder) << | 1100 | (tk->ntp_error_shift + shift); |
| 982 | (timekeeper.ntp_error_shift + shift); | ||
| 983 | 1101 | ||
| 984 | return offset; | 1102 | return offset; |
| 985 | } | 1103 | } |
| 986 | 1104 | ||
| 987 | |||
| 988 | /** | 1105 | /** |
| 989 | * update_wall_time - Uses the current clocksource to increment the wall time | 1106 | * update_wall_time - Uses the current clocksource to increment the wall time |
| 990 | * | 1107 | * |
| @@ -992,25 +1109,25 @@ static cycle_t logarithmic_accumulation(cycle_t offset, int shift) | |||
| 992 | static void update_wall_time(void) | 1109 | static void update_wall_time(void) |
| 993 | { | 1110 | { |
| 994 | struct clocksource *clock; | 1111 | struct clocksource *clock; |
| 1112 | struct timekeeper *tk = &timekeeper; | ||
| 995 | cycle_t offset; | 1113 | cycle_t offset; |
| 996 | int shift = 0, maxshift; | 1114 | int shift = 0, maxshift; |
| 997 | unsigned long flags; | 1115 | unsigned long flags; |
| 1116 | s64 remainder; | ||
| 998 | 1117 | ||
| 999 | write_seqlock_irqsave(&timekeeper.lock, flags); | 1118 | write_seqlock_irqsave(&tk->lock, flags); |
| 1000 | 1119 | ||
| 1001 | /* Make sure we're fully resumed: */ | 1120 | /* Make sure we're fully resumed: */ |
| 1002 | if (unlikely(timekeeping_suspended)) | 1121 | if (unlikely(timekeeping_suspended)) |
| 1003 | goto out; | 1122 | goto out; |
| 1004 | 1123 | ||
| 1005 | clock = timekeeper.clock; | 1124 | clock = tk->clock; |
| 1006 | 1125 | ||
| 1007 | #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET | 1126 | #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET |
| 1008 | offset = timekeeper.cycle_interval; | 1127 | offset = tk->cycle_interval; |
| 1009 | #else | 1128 | #else |
| 1010 | offset = (clock->read(clock) - clock->cycle_last) & clock->mask; | 1129 | offset = (clock->read(clock) - clock->cycle_last) & clock->mask; |
| 1011 | #endif | 1130 | #endif |
| 1012 | timekeeper.xtime_nsec = (s64)timekeeper.xtime.tv_nsec << | ||
| 1013 | timekeeper.shift; | ||
| 1014 | 1131 | ||
| 1015 | /* | 1132 | /* |
| 1016 | * With NO_HZ we may have to accumulate many cycle_intervals | 1133 | * With NO_HZ we may have to accumulate many cycle_intervals |
| @@ -1020,71 +1137,45 @@ static void update_wall_time(void) | |||
| 1020 | * chunk in one go, and then try to consume the next smaller | 1137 | * chunk in one go, and then try to consume the next smaller |
| 1021 | * doubled multiple. | 1138 | * doubled multiple. |
| 1022 | */ | 1139 | */ |
| 1023 | shift = ilog2(offset) - ilog2(timekeeper.cycle_interval); | 1140 | shift = ilog2(offset) - ilog2(tk->cycle_interval); |
| 1024 | shift = max(0, shift); | 1141 | shift = max(0, shift); |
| 1025 | /* Bound shift to one less than what overflows tick_length */ | 1142 | /* Bound shift to one less than what overflows tick_length */ |
| 1026 | maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1; | 1143 | maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1; |
| 1027 | shift = min(shift, maxshift); | 1144 | shift = min(shift, maxshift); |
| 1028 | while (offset >= timekeeper.cycle_interval) { | 1145 | while (offset >= tk->cycle_interval) { |
| 1029 | offset = logarithmic_accumulation(offset, shift); | 1146 | offset = logarithmic_accumulation(tk, offset, shift); |
| 1030 | if(offset < timekeeper.cycle_interval<<shift) | 1147 | if (offset < tk->cycle_interval<<shift) |
| 1031 | shift--; | 1148 | shift--; |
| 1032 | } | 1149 | } |
| 1033 | 1150 | ||
| 1034 | /* correct the clock when NTP error is too big */ | 1151 | /* correct the clock when NTP error is too big */ |
| 1035 | timekeeping_adjust(offset); | 1152 | timekeeping_adjust(tk, offset); |
| 1036 | |||
| 1037 | /* | ||
| 1038 | * Since in the loop above, we accumulate any amount of time | ||
| 1039 | * in xtime_nsec over a second into xtime.tv_sec, its possible for | ||
| 1040 | * xtime_nsec to be fairly small after the loop. Further, if we're | ||
| 1041 | * slightly speeding the clocksource up in timekeeping_adjust(), | ||
| 1042 | * its possible the required corrective factor to xtime_nsec could | ||
| 1043 | * cause it to underflow. | ||
| 1044 | * | ||
| 1045 | * Now, we cannot simply roll the accumulated second back, since | ||
| 1046 | * the NTP subsystem has been notified via second_overflow. So | ||
| 1047 | * instead we push xtime_nsec forward by the amount we underflowed, | ||
| 1048 | * and add that amount into the error. | ||
| 1049 | * | ||
| 1050 | * We'll correct this error next time through this function, when | ||
| 1051 | * xtime_nsec is not as small. | ||
| 1052 | */ | ||
| 1053 | if (unlikely((s64)timekeeper.xtime_nsec < 0)) { | ||
| 1054 | s64 neg = -(s64)timekeeper.xtime_nsec; | ||
| 1055 | timekeeper.xtime_nsec = 0; | ||
| 1056 | timekeeper.ntp_error += neg << timekeeper.ntp_error_shift; | ||
| 1057 | } | ||
| 1058 | 1153 | ||
| 1059 | 1154 | ||
| 1060 | /* | 1155 | /* |
| 1061 | * Store full nanoseconds into xtime after rounding it up and | 1156 | * Store only full nanoseconds into xtime_nsec after rounding |
| 1062 | * add the remainder to the error difference. | 1157 | * it up and add the remainder to the error difference. |
| 1063 | */ | 1158 | * XXX - This is necessary to avoid small 1ns inconsistnecies caused |
| 1064 | timekeeper.xtime.tv_nsec = ((s64)timekeeper.xtime_nsec >> | 1159 | * by truncating the remainder in vsyscalls. However, it causes |
| 1065 | timekeeper.shift) + 1; | 1160 | * additional work to be done in timekeeping_adjust(). Once |
| 1066 | timekeeper.xtime_nsec -= (s64)timekeeper.xtime.tv_nsec << | 1161 | * the vsyscall implementations are converted to use xtime_nsec |
| 1067 | timekeeper.shift; | 1162 | * (shifted nanoseconds), this can be killed. |
| 1068 | timekeeper.ntp_error += timekeeper.xtime_nsec << | 1163 | */ |
| 1069 | timekeeper.ntp_error_shift; | 1164 | remainder = tk->xtime_nsec & ((1 << tk->shift) - 1); |
| 1165 | tk->xtime_nsec -= remainder; | ||
| 1166 | tk->xtime_nsec += 1 << tk->shift; | ||
| 1167 | tk->ntp_error += remainder << tk->ntp_error_shift; | ||
| 1070 | 1168 | ||
| 1071 | /* | 1169 | /* |
| 1072 | * Finally, make sure that after the rounding | 1170 | * Finally, make sure that after the rounding |
| 1073 | * xtime.tv_nsec isn't larger than NSEC_PER_SEC | 1171 | * xtime_nsec isn't larger than NSEC_PER_SEC |
| 1074 | */ | 1172 | */ |
| 1075 | if (unlikely(timekeeper.xtime.tv_nsec >= NSEC_PER_SEC)) { | 1173 | accumulate_nsecs_to_secs(tk); |
| 1076 | int leap; | ||
| 1077 | timekeeper.xtime.tv_nsec -= NSEC_PER_SEC; | ||
| 1078 | timekeeper.xtime.tv_sec++; | ||
| 1079 | leap = second_overflow(timekeeper.xtime.tv_sec); | ||
| 1080 | timekeeper.xtime.tv_sec += leap; | ||
| 1081 | timekeeper.wall_to_monotonic.tv_sec -= leap; | ||
| 1082 | } | ||
| 1083 | 1174 | ||
| 1084 | timekeeping_update(false); | 1175 | timekeeping_update(tk, false); |
| 1085 | 1176 | ||
| 1086 | out: | 1177 | out: |
| 1087 | write_sequnlock_irqrestore(&timekeeper.lock, flags); | 1178 | write_sequnlock_irqrestore(&tk->lock, flags); |
| 1088 | 1179 | ||
| 1089 | } | 1180 | } |
| 1090 | 1181 | ||
| @@ -1101,18 +1192,18 @@ out: | |||
| 1101 | */ | 1192 | */ |
| 1102 | void getboottime(struct timespec *ts) | 1193 | void getboottime(struct timespec *ts) |
| 1103 | { | 1194 | { |
| 1195 | struct timekeeper *tk = &timekeeper; | ||
| 1104 | struct timespec boottime = { | 1196 | struct timespec boottime = { |
| 1105 | .tv_sec = timekeeper.wall_to_monotonic.tv_sec + | 1197 | .tv_sec = tk->wall_to_monotonic.tv_sec + |
| 1106 | timekeeper.total_sleep_time.tv_sec, | 1198 | tk->total_sleep_time.tv_sec, |
| 1107 | .tv_nsec = timekeeper.wall_to_monotonic.tv_nsec + | 1199 | .tv_nsec = tk->wall_to_monotonic.tv_nsec + |
| 1108 | timekeeper.total_sleep_time.tv_nsec | 1200 | tk->total_sleep_time.tv_nsec |
| 1109 | }; | 1201 | }; |
| 1110 | 1202 | ||
| 1111 | set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec); | 1203 | set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec); |
| 1112 | } | 1204 | } |
| 1113 | EXPORT_SYMBOL_GPL(getboottime); | 1205 | EXPORT_SYMBOL_GPL(getboottime); |
| 1114 | 1206 | ||
| 1115 | |||
| 1116 | /** | 1207 | /** |
| 1117 | * get_monotonic_boottime - Returns monotonic time since boot | 1208 | * get_monotonic_boottime - Returns monotonic time since boot |
| 1118 | * @ts: pointer to the timespec to be set | 1209 | * @ts: pointer to the timespec to be set |
| @@ -1124,23 +1215,23 @@ EXPORT_SYMBOL_GPL(getboottime); | |||
| 1124 | */ | 1215 | */ |
| 1125 | void get_monotonic_boottime(struct timespec *ts) | 1216 | void get_monotonic_boottime(struct timespec *ts) |
| 1126 | { | 1217 | { |
| 1218 | struct timekeeper *tk = &timekeeper; | ||
| 1127 | struct timespec tomono, sleep; | 1219 | struct timespec tomono, sleep; |
| 1128 | unsigned int seq; | 1220 | unsigned int seq; |
| 1129 | s64 nsecs; | ||
| 1130 | 1221 | ||
| 1131 | WARN_ON(timekeeping_suspended); | 1222 | WARN_ON(timekeeping_suspended); |
| 1132 | 1223 | ||
| 1133 | do { | 1224 | do { |
| 1134 | seq = read_seqbegin(&timekeeper.lock); | 1225 | seq = read_seqbegin(&tk->lock); |
| 1135 | *ts = timekeeper.xtime; | 1226 | ts->tv_sec = tk->xtime_sec; |
| 1136 | tomono = timekeeper.wall_to_monotonic; | 1227 | ts->tv_nsec = timekeeping_get_ns(tk); |
| 1137 | sleep = timekeeper.total_sleep_time; | 1228 | tomono = tk->wall_to_monotonic; |
| 1138 | nsecs = timekeeping_get_ns(); | 1229 | sleep = tk->total_sleep_time; |
| 1139 | 1230 | ||
| 1140 | } while (read_seqretry(&timekeeper.lock, seq)); | 1231 | } while (read_seqretry(&tk->lock, seq)); |
| 1141 | 1232 | ||
| 1142 | set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec, | 1233 | set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec, |
| 1143 | ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec + nsecs); | 1234 | ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec); |
| 1144 | } | 1235 | } |
| 1145 | EXPORT_SYMBOL_GPL(get_monotonic_boottime); | 1236 | EXPORT_SYMBOL_GPL(get_monotonic_boottime); |
| 1146 | 1237 | ||
| @@ -1167,31 +1258,38 @@ EXPORT_SYMBOL_GPL(ktime_get_boottime); | |||
| 1167 | */ | 1258 | */ |
| 1168 | void monotonic_to_bootbased(struct timespec *ts) | 1259 | void monotonic_to_bootbased(struct timespec *ts) |
| 1169 | { | 1260 | { |
| 1170 | *ts = timespec_add(*ts, timekeeper.total_sleep_time); | 1261 | struct timekeeper *tk = &timekeeper; |
| 1262 | |||
| 1263 | *ts = timespec_add(*ts, tk->total_sleep_time); | ||
| 1171 | } | 1264 | } |
| 1172 | EXPORT_SYMBOL_GPL(monotonic_to_bootbased); | 1265 | EXPORT_SYMBOL_GPL(monotonic_to_bootbased); |
| 1173 | 1266 | ||
| 1174 | unsigned long get_seconds(void) | 1267 | unsigned long get_seconds(void) |
| 1175 | { | 1268 | { |
| 1176 | return timekeeper.xtime.tv_sec; | 1269 | struct timekeeper *tk = &timekeeper; |
| 1270 | |||
| 1271 | return tk->xtime_sec; | ||
| 1177 | } | 1272 | } |
| 1178 | EXPORT_SYMBOL(get_seconds); | 1273 | EXPORT_SYMBOL(get_seconds); |
| 1179 | 1274 | ||
| 1180 | struct timespec __current_kernel_time(void) | 1275 | struct timespec __current_kernel_time(void) |
| 1181 | { | 1276 | { |
| 1182 | return timekeeper.xtime; | 1277 | struct timekeeper *tk = &timekeeper; |
| 1278 | |||
| 1279 | return tk_xtime(tk); | ||
| 1183 | } | 1280 | } |
| 1184 | 1281 | ||
| 1185 | struct timespec current_kernel_time(void) | 1282 | struct timespec current_kernel_time(void) |
| 1186 | { | 1283 | { |
| 1284 | struct timekeeper *tk = &timekeeper; | ||
| 1187 | struct timespec now; | 1285 | struct timespec now; |
| 1188 | unsigned long seq; | 1286 | unsigned long seq; |
| 1189 | 1287 | ||
| 1190 | do { | 1288 | do { |
| 1191 | seq = read_seqbegin(&timekeeper.lock); | 1289 | seq = read_seqbegin(&tk->lock); |
| 1192 | 1290 | ||
| 1193 | now = timekeeper.xtime; | 1291 | now = tk_xtime(tk); |
| 1194 | } while (read_seqretry(&timekeeper.lock, seq)); | 1292 | } while (read_seqretry(&tk->lock, seq)); |
| 1195 | 1293 | ||
| 1196 | return now; | 1294 | return now; |
| 1197 | } | 1295 | } |
| @@ -1199,15 +1297,16 @@ EXPORT_SYMBOL(current_kernel_time); | |||
| 1199 | 1297 | ||
| 1200 | struct timespec get_monotonic_coarse(void) | 1298 | struct timespec get_monotonic_coarse(void) |
| 1201 | { | 1299 | { |
| 1300 | struct timekeeper *tk = &timekeeper; | ||
| 1202 | struct timespec now, mono; | 1301 | struct timespec now, mono; |
| 1203 | unsigned long seq; | 1302 | unsigned long seq; |
| 1204 | 1303 | ||
| 1205 | do { | 1304 | do { |
| 1206 | seq = read_seqbegin(&timekeeper.lock); | 1305 | seq = read_seqbegin(&tk->lock); |
| 1207 | 1306 | ||
| 1208 | now = timekeeper.xtime; | 1307 | now = tk_xtime(tk); |
| 1209 | mono = timekeeper.wall_to_monotonic; | 1308 | mono = tk->wall_to_monotonic; |
| 1210 | } while (read_seqretry(&timekeeper.lock, seq)); | 1309 | } while (read_seqretry(&tk->lock, seq)); |
| 1211 | 1310 | ||
| 1212 | set_normalized_timespec(&now, now.tv_sec + mono.tv_sec, | 1311 | set_normalized_timespec(&now, now.tv_sec + mono.tv_sec, |
| 1213 | now.tv_nsec + mono.tv_nsec); | 1312 | now.tv_nsec + mono.tv_nsec); |
| @@ -1236,34 +1335,67 @@ void do_timer(unsigned long ticks) | |||
| 1236 | void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim, | 1335 | void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim, |
| 1237 | struct timespec *wtom, struct timespec *sleep) | 1336 | struct timespec *wtom, struct timespec *sleep) |
| 1238 | { | 1337 | { |
| 1338 | struct timekeeper *tk = &timekeeper; | ||
| 1239 | unsigned long seq; | 1339 | unsigned long seq; |
| 1240 | 1340 | ||
| 1241 | do { | 1341 | do { |
| 1242 | seq = read_seqbegin(&timekeeper.lock); | 1342 | seq = read_seqbegin(&tk->lock); |
| 1243 | *xtim = timekeeper.xtime; | 1343 | *xtim = tk_xtime(tk); |
| 1244 | *wtom = timekeeper.wall_to_monotonic; | 1344 | *wtom = tk->wall_to_monotonic; |
| 1245 | *sleep = timekeeper.total_sleep_time; | 1345 | *sleep = tk->total_sleep_time; |
| 1246 | } while (read_seqretry(&timekeeper.lock, seq)); | 1346 | } while (read_seqretry(&tk->lock, seq)); |
| 1247 | } | 1347 | } |
| 1248 | 1348 | ||
| 1349 | #ifdef CONFIG_HIGH_RES_TIMERS | ||
| 1350 | /** | ||
| 1351 | * ktime_get_update_offsets - hrtimer helper | ||
| 1352 | * @offs_real: pointer to storage for monotonic -> realtime offset | ||
| 1353 | * @offs_boot: pointer to storage for monotonic -> boottime offset | ||
| 1354 | * | ||
| 1355 | * Returns current monotonic time and updates the offsets | ||
| 1356 | * Called from hrtimer_interupt() or retrigger_next_event() | ||
| 1357 | */ | ||
| 1358 | ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot) | ||
| 1359 | { | ||
| 1360 | struct timekeeper *tk = &timekeeper; | ||
| 1361 | ktime_t now; | ||
| 1362 | unsigned int seq; | ||
| 1363 | u64 secs, nsecs; | ||
| 1364 | |||
| 1365 | do { | ||
| 1366 | seq = read_seqbegin(&tk->lock); | ||
| 1367 | |||
| 1368 | secs = tk->xtime_sec; | ||
| 1369 | nsecs = timekeeping_get_ns(tk); | ||
| 1370 | |||
| 1371 | *offs_real = tk->offs_real; | ||
| 1372 | *offs_boot = tk->offs_boot; | ||
| 1373 | } while (read_seqretry(&tk->lock, seq)); | ||
| 1374 | |||
| 1375 | now = ktime_add_ns(ktime_set(secs, 0), nsecs); | ||
| 1376 | now = ktime_sub(now, *offs_real); | ||
| 1377 | return now; | ||
| 1378 | } | ||
| 1379 | #endif | ||
| 1380 | |||
| 1249 | /** | 1381 | /** |
| 1250 | * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format | 1382 | * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format |
| 1251 | */ | 1383 | */ |
| 1252 | ktime_t ktime_get_monotonic_offset(void) | 1384 | ktime_t ktime_get_monotonic_offset(void) |
| 1253 | { | 1385 | { |
| 1386 | struct timekeeper *tk = &timekeeper; | ||
| 1254 | unsigned long seq; | 1387 | unsigned long seq; |
| 1255 | struct timespec wtom; | 1388 | struct timespec wtom; |
| 1256 | 1389 | ||
| 1257 | do { | 1390 | do { |
| 1258 | seq = read_seqbegin(&timekeeper.lock); | 1391 | seq = read_seqbegin(&tk->lock); |
| 1259 | wtom = timekeeper.wall_to_monotonic; | 1392 | wtom = tk->wall_to_monotonic; |
| 1260 | } while (read_seqretry(&timekeeper.lock, seq)); | 1393 | } while (read_seqretry(&tk->lock, seq)); |
| 1261 | 1394 | ||
| 1262 | return timespec_to_ktime(wtom); | 1395 | return timespec_to_ktime(wtom); |
| 1263 | } | 1396 | } |
| 1264 | EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset); | 1397 | EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset); |
| 1265 | 1398 | ||
| 1266 | |||
| 1267 | /** | 1399 | /** |
| 1268 | * xtime_update() - advances the timekeeping infrastructure | 1400 | * xtime_update() - advances the timekeeping infrastructure |
| 1269 | * @ticks: number of ticks, that have elapsed since the last call. | 1401 | * @ticks: number of ticks, that have elapsed since the last call. |
