]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - kernel/sched/clock.c
x86/tsc: Fold set_cyc2ns_scale() into caller
[mirror_ubuntu-hirsute-kernel.git] / kernel / sched / clock.c
CommitLineData
3e51f33f
PZ
1/*
2 * sched_clock for unstable cpu clocks
3 *
90eec103 4 * Copyright (C) 2008 Red Hat, Inc., Peter Zijlstra
3e51f33f 5 *
c300ba25
SR
6 * Updates and enhancements:
7 * Copyright (C) 2008 Red Hat, Inc. Steven Rostedt <srostedt@redhat.com>
8 *
3e51f33f
PZ
9 * Based on code by:
10 * Ingo Molnar <mingo@redhat.com>
11 * Guillaume Chazarain <guichaz@gmail.com>
12 *
c676329a
PZ
13 *
14 * What:
15 *
16 * cpu_clock(i) provides a fast (execution time) high resolution
17 * clock with bounded drift between CPUs. The value of cpu_clock(i)
18 * is monotonic for constant i. The timestamp returned is in nanoseconds.
19 *
20 * ######################### BIG FAT WARNING ##########################
21 * # when comparing cpu_clock(i) to cpu_clock(j) for i != j, time can #
22 * # go backwards !! #
23 * ####################################################################
24 *
25 * There is no strict promise about the base, although it tends to start
26 * at 0 on boot (but people really shouldn't rely on that).
27 *
28 * cpu_clock(i) -- can be used from any context, including NMI.
c676329a
PZ
29 * local_clock() -- is cpu_clock() on the current cpu.
30 *
ef08f0ff
PZ
31 * sched_clock_cpu(i)
32 *
c676329a
PZ
33 * How:
34 *
35 * The implementation either uses sched_clock() when
36 * !CONFIG_HAVE_UNSTABLE_SCHED_CLOCK, which means in that case the
37 * sched_clock() is assumed to provide these properties (mostly it means
38 * the architecture provides a globally synchronized highres time source).
39 *
40 * Otherwise it tries to create a semi stable clock from a mixture of other
41 * clocks, including:
42 *
43 * - GTOD (clock monotomic)
3e51f33f
PZ
44 * - sched_clock()
45 * - explicit idle events
46 *
c676329a
PZ
47 * We use GTOD as base and use sched_clock() deltas to improve resolution. The
48 * deltas are filtered to provide monotonicity and keeping it within an
49 * expected window.
3e51f33f
PZ
50 *
51 * Furthermore, explicit sleep and wakeup hooks allow us to account for time
52 * that is otherwise invisible (TSC gets stopped).
53 *
3e51f33f 54 */
3e51f33f 55#include <linux/spinlock.h>
6409c4da 56#include <linux/hardirq.h>
9984de1a 57#include <linux/export.h>
b342501c
IM
58#include <linux/percpu.h>
59#include <linux/ktime.h>
60#include <linux/sched.h>
38b8d208 61#include <linux/nmi.h>
e6017571 62#include <linux/sched/clock.h>
35af99e6 63#include <linux/static_key.h>
6577e42a 64#include <linux/workqueue.h>
52f5684c 65#include <linux/compiler.h>
4f49b90a 66#include <linux/tick.h>
2e44b7dd 67#include <linux/init.h>
3e51f33f 68
2c3d103b
HD
69/*
70 * Scheduler clock - returns current time in nanosec units.
71 * This is default implementation.
72 * Architectures and sub-architectures can override this.
73 */
52f5684c 74unsigned long long __weak sched_clock(void)
2c3d103b 75{
92d23f70
R
76 return (unsigned long long)(jiffies - INITIAL_JIFFIES)
77 * (NSEC_PER_SEC / HZ);
2c3d103b 78}
b6ac23af 79EXPORT_SYMBOL_GPL(sched_clock);
3e51f33f 80
5bb6b1ea 81__read_mostly int sched_clock_running;
c1955a3d 82
9881b024
PZ
83void sched_clock_init(void)
84{
85 sched_clock_running = 1;
86}
87
3e51f33f 88#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
acb04058
PZ
89/*
90 * We must start with !__sched_clock_stable because the unstable -> stable
91 * transition is accurate, while the stable -> unstable transition is not.
92 *
93 * Similarly we start with __sched_clock_stable_early, thereby assuming we
94 * will become stable, such that there's only a single 1 -> 0 transition.
95 */
555570d7 96static DEFINE_STATIC_KEY_FALSE(__sched_clock_stable);
acb04058 97static int __sched_clock_stable_early = 1;
35af99e6 98
5680d809 99/*
698eff63 100 * We want: ktime_get_ns() + __gtod_offset == sched_clock() + __sched_clock_offset
5680d809 101 */
698eff63
PZ
102__read_mostly u64 __sched_clock_offset;
103static __read_mostly u64 __gtod_offset;
5680d809
PZ
104
105struct sched_clock_data {
106 u64 tick_raw;
107 u64 tick_gtod;
108 u64 clock;
109};
110
111static DEFINE_PER_CPU_SHARED_ALIGNED(struct sched_clock_data, sched_clock_data);
112
113static inline struct sched_clock_data *this_scd(void)
114{
115 return this_cpu_ptr(&sched_clock_data);
116}
117
118static inline struct sched_clock_data *cpu_sdc(int cpu)
119{
120 return &per_cpu(sched_clock_data, cpu);
121}
122
35af99e6
PZ
123int sched_clock_stable(void)
124{
555570d7 125 return static_branch_likely(&__sched_clock_stable);
35af99e6
PZ
126}
127
cf15ca8d
PZ
128static void __scd_stamp(struct sched_clock_data *scd)
129{
130 scd->tick_gtod = ktime_get_ns();
131 scd->tick_raw = sched_clock();
132}
133
d375b4e0 134static void __set_sched_clock_stable(void)
35af99e6 135{
5680d809
PZ
136 struct sched_clock_data *scd = this_scd();
137
138 /*
139 * Attempt to make the (initial) unstable->stable transition continuous.
140 */
698eff63 141 __sched_clock_offset = (scd->tick_gtod + __gtod_offset) - (scd->tick_raw);
5680d809
PZ
142
143 printk(KERN_INFO "sched_clock: Marking stable (%lld, %lld)->(%lld, %lld)\n",
698eff63
PZ
144 scd->tick_gtod, __gtod_offset,
145 scd->tick_raw, __sched_clock_offset);
5680d809 146
555570d7 147 static_branch_enable(&__sched_clock_stable);
4f49b90a 148 tick_dep_clear(TICK_DEP_BIT_CLOCK_UNSTABLE);
d375b4e0
PZ
149}
150
cf15ca8d
PZ
151/*
152 * If we ever get here, we're screwed, because we found out -- typically after
153 * the fact -- that TSC wasn't good. This means all our clocksources (including
154 * ktime) could have reported wrong values.
155 *
156 * What we do here is an attempt to fix up and continue sort of where we left
157 * off in a coherent manner.
158 *
159 * The only way to fully avoid random clock jumps is to boot with:
160 * "tsc=unstable".
161 */
71fdb70e
PZ
162static void __sched_clock_work(struct work_struct *work)
163{
cf15ca8d
PZ
164 struct sched_clock_data *scd;
165 int cpu;
166
167 /* take a current timestamp and set 'now' */
168 preempt_disable();
169 scd = this_scd();
170 __scd_stamp(scd);
171 scd->clock = scd->tick_gtod + __gtod_offset;
172 preempt_enable();
173
174 /* clone to all CPUs */
175 for_each_possible_cpu(cpu)
176 per_cpu(sched_clock_data, cpu) = *scd;
177
7708d5f0 178 printk(KERN_WARNING "TSC found unstable after boot, most likely due to broken BIOS. Use 'tsc=unstable'.\n");
cf15ca8d
PZ
179 printk(KERN_INFO "sched_clock: Marking unstable (%lld, %lld)<-(%lld, %lld)\n",
180 scd->tick_gtod, __gtod_offset,
181 scd->tick_raw, __sched_clock_offset);
182
71fdb70e
PZ
183 static_branch_disable(&__sched_clock_stable);
184}
185
186static DECLARE_WORK(sched_clock_work, __sched_clock_work);
187
188static void __clear_sched_clock_stable(void)
35af99e6 189{
cf15ca8d
PZ
190 if (!sched_clock_stable())
191 return;
5680d809 192
4f49b90a 193 tick_dep_set(TICK_DEP_BIT_CLOCK_UNSTABLE);
cf15ca8d 194 schedule_work(&sched_clock_work);
71fdb70e 195}
6577e42a
PZ
196
197void clear_sched_clock_stable(void)
198{
d375b4e0
PZ
199 __sched_clock_stable_early = 0;
200
9881b024 201 smp_mb(); /* matches sched_clock_init_late() */
d375b4e0 202
9881b024 203 if (sched_clock_running == 2)
71fdb70e 204 __clear_sched_clock_stable();
6577e42a
PZ
205}
206
2e44b7dd
PZ
207/*
208 * We run this as late_initcall() such that it runs after all built-in drivers,
209 * notably: acpi_processor and intel_idle, which can mark the TSC as unstable.
210 */
211static int __init sched_clock_init_late(void)
3e51f33f 212{
9881b024 213 sched_clock_running = 2;
d375b4e0
PZ
214 /*
215 * Ensure that it is impossible to not do a static_key update.
216 *
217 * Either {set,clear}_sched_clock_stable() must see sched_clock_running
218 * and do the update, or we must see their __sched_clock_stable_early
219 * and do the update, or both.
220 */
221 smp_mb(); /* matches {set,clear}_sched_clock_stable() */
222
223 if (__sched_clock_stable_early)
224 __set_sched_clock_stable();
2e44b7dd
PZ
225
226 return 0;
3e51f33f 227}
2e44b7dd 228late_initcall(sched_clock_init_late);
3e51f33f 229
354879bb 230/*
b342501c 231 * min, max except they take wrapping into account
354879bb
PZ
232 */
233
234static inline u64 wrap_min(u64 x, u64 y)
235{
236 return (s64)(x - y) < 0 ? x : y;
237}
238
239static inline u64 wrap_max(u64 x, u64 y)
240{
241 return (s64)(x - y) > 0 ? x : y;
242}
243
3e51f33f
PZ
244/*
245 * update the percpu scd from the raw @now value
246 *
247 * - filter out backward motion
354879bb 248 * - use the GTOD tick value to create a window to filter crazy TSC values
3e51f33f 249 */
def0a9b2 250static u64 sched_clock_local(struct sched_clock_data *scd)
3e51f33f 251{
7b09cc5a 252 u64 now, clock, old_clock, min_clock, max_clock, gtod;
def0a9b2 253 s64 delta;
3e51f33f 254
def0a9b2
PZ
255again:
256 now = sched_clock();
257 delta = now - scd->tick_raw;
354879bb
PZ
258 if (unlikely(delta < 0))
259 delta = 0;
3e51f33f 260
def0a9b2
PZ
261 old_clock = scd->clock;
262
354879bb
PZ
263 /*
264 * scd->clock = clamp(scd->tick_gtod + delta,
b342501c
IM
265 * max(scd->tick_gtod, scd->clock),
266 * scd->tick_gtod + TICK_NSEC);
354879bb 267 */
3e51f33f 268
7b09cc5a
PT
269 gtod = scd->tick_gtod + __gtod_offset;
270 clock = gtod + delta;
271 min_clock = wrap_max(gtod, old_clock);
272 max_clock = wrap_max(old_clock, gtod + TICK_NSEC);
3e51f33f 273
354879bb
PZ
274 clock = wrap_max(clock, min_clock);
275 clock = wrap_min(clock, max_clock);
3e51f33f 276
152f9d07 277 if (cmpxchg64(&scd->clock, old_clock, clock) != old_clock)
def0a9b2 278 goto again;
56b90612 279
def0a9b2 280 return clock;
3e51f33f
PZ
281}
282
def0a9b2 283static u64 sched_clock_remote(struct sched_clock_data *scd)
3e51f33f 284{
def0a9b2
PZ
285 struct sched_clock_data *my_scd = this_scd();
286 u64 this_clock, remote_clock;
287 u64 *ptr, old_val, val;
288
a1cbcaa9
TG
289#if BITS_PER_LONG != 64
290again:
291 /*
292 * Careful here: The local and the remote clock values need to
293 * be read out atomic as we need to compare the values and
294 * then update either the local or the remote side. So the
295 * cmpxchg64 below only protects one readout.
296 *
297 * We must reread via sched_clock_local() in the retry case on
298 * 32bit as an NMI could use sched_clock_local() via the
299 * tracer and hit between the readout of
300 * the low32bit and the high 32bit portion.
301 */
302 this_clock = sched_clock_local(my_scd);
303 /*
304 * We must enforce atomic readout on 32bit, otherwise the
305 * update on the remote cpu can hit inbetween the readout of
306 * the low32bit and the high 32bit portion.
307 */
308 remote_clock = cmpxchg64(&scd->clock, 0, 0);
309#else
310 /*
311 * On 64bit the read of [my]scd->clock is atomic versus the
312 * update, so we can avoid the above 32bit dance.
313 */
def0a9b2
PZ
314 sched_clock_local(my_scd);
315again:
316 this_clock = my_scd->clock;
317 remote_clock = scd->clock;
a1cbcaa9 318#endif
def0a9b2
PZ
319
320 /*
321 * Use the opportunity that we have both locks
322 * taken to couple the two clocks: we take the
323 * larger time as the latest time for both
324 * runqueues. (this creates monotonic movement)
325 */
326 if (likely((s64)(remote_clock - this_clock) < 0)) {
327 ptr = &scd->clock;
328 old_val = remote_clock;
329 val = this_clock;
3e51f33f 330 } else {
def0a9b2
PZ
331 /*
332 * Should be rare, but possible:
333 */
334 ptr = &my_scd->clock;
335 old_val = this_clock;
336 val = remote_clock;
3e51f33f 337 }
def0a9b2 338
152f9d07 339 if (cmpxchg64(ptr, old_val, val) != old_val)
def0a9b2
PZ
340 goto again;
341
342 return val;
3e51f33f
PZ
343}
344
c676329a
PZ
345/*
346 * Similar to cpu_clock(), but requires local IRQs to be disabled.
347 *
348 * See cpu_clock().
349 */
3e51f33f
PZ
350u64 sched_clock_cpu(int cpu)
351{
b342501c 352 struct sched_clock_data *scd;
def0a9b2
PZ
353 u64 clock;
354
35af99e6 355 if (sched_clock_stable())
698eff63 356 return sched_clock() + __sched_clock_offset;
a381759d 357
a381759d
PZ
358 if (unlikely(!sched_clock_running))
359 return 0ull;
360
96b3d28b 361 preempt_disable_notrace();
def0a9b2 362 scd = cpu_sdc(cpu);
3e51f33f 363
def0a9b2
PZ
364 if (cpu != smp_processor_id())
365 clock = sched_clock_remote(scd);
366 else
367 clock = sched_clock_local(scd);
96b3d28b 368 preempt_enable_notrace();
e4e4e534 369
3e51f33f
PZ
370 return clock;
371}
2c923e94 372EXPORT_SYMBOL_GPL(sched_clock_cpu);
3e51f33f
PZ
373
374void sched_clock_tick(void)
375{
8325d9c0 376 struct sched_clock_data *scd;
a381759d 377
b421b22b
PZ
378 if (sched_clock_stable())
379 return;
380
381 if (unlikely(!sched_clock_running))
382 return;
383
3e51f33f
PZ
384 WARN_ON_ONCE(!irqs_disabled());
385
8325d9c0 386 scd = this_scd();
cf15ca8d 387 __scd_stamp(scd);
b421b22b
PZ
388 sched_clock_local(scd);
389}
390
391void sched_clock_tick_stable(void)
392{
393 u64 gtod, clock;
3e51f33f 394
b421b22b
PZ
395 if (!sched_clock_stable())
396 return;
397
398 /*
399 * Called under watchdog_lock.
400 *
401 * The watchdog just found this TSC to (still) be stable, so now is a
402 * good moment to update our __gtod_offset. Because once we find the
403 * TSC to be unstable, any computation will be computing crap.
404 */
405 local_irq_disable();
406 gtod = ktime_get_ns();
407 clock = sched_clock();
408 __gtod_offset = (clock + __sched_clock_offset) - gtod;
409 local_irq_enable();
3e51f33f
PZ
410}
411
412/*
413 * We are going deep-idle (irqs are disabled):
414 */
415void sched_clock_idle_sleep_event(void)
416{
417 sched_clock_cpu(smp_processor_id());
418}
419EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
420
421/*
f9fccdb9 422 * We just idled; resync with ktime.
3e51f33f 423 */
ac1e843f 424void sched_clock_idle_wakeup_event(void)
3e51f33f 425{
f9fccdb9
PZ
426 unsigned long flags;
427
428 if (sched_clock_stable())
429 return;
430
431 if (unlikely(timekeeping_suspended))
1c5745aa
TG
432 return;
433
f9fccdb9 434 local_irq_save(flags);
354879bb 435 sched_clock_tick();
f9fccdb9 436 local_irq_restore(flags);
3e51f33f
PZ
437}
438EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
439
8325d9c0
PZ
440#else /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */
441
8325d9c0
PZ
442u64 sched_clock_cpu(int cpu)
443{
444 if (unlikely(!sched_clock_running))
445 return 0;
446
447 return sched_clock();
448}
9881b024 449
b9f8fcd5 450#endif /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */
76a2a6ee 451
545a2bf7
CB
452/*
453 * Running clock - returns the time that has elapsed while a guest has been
454 * running.
455 * On a guest this value should be local_clock minus the time the guest was
456 * suspended by the hypervisor (for any reason).
457 * On bare metal this function should return the same as local_clock.
458 * Architectures and sub-architectures can override this.
459 */
460u64 __weak running_clock(void)
461{
462 return local_clock();
463}