1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55#include <linux/spinlock.h>
56#include <linux/hardirq.h>
57#include <linux/export.h>
58#include <linux/percpu.h>
59#include <linux/ktime.h>
60#include <linux/sched.h>
61#include <linux/static_key.h>
62#include <linux/workqueue.h>
63#include <linux/compiler.h>
64
65
66
67
68
69
70unsigned long long __weak sched_clock(void)
71{
72 return (unsigned long long)(jiffies - INITIAL_JIFFIES)
73 * (NSEC_PER_SEC / HZ);
74}
75EXPORT_SYMBOL_GPL(sched_clock);
76
77__read_mostly int sched_clock_running;
78
79#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
80static struct static_key __sched_clock_stable = STATIC_KEY_INIT;
81static int __sched_clock_stable_early;
82
83int sched_clock_stable(void)
84{
85 return static_key_false(&__sched_clock_stable);
86}
87
88static void __set_sched_clock_stable(void)
89{
90 if (!sched_clock_stable())
91 static_key_slow_inc(&__sched_clock_stable);
92}
93
94void set_sched_clock_stable(void)
95{
96 __sched_clock_stable_early = 1;
97
98 smp_mb();
99
100 if (!sched_clock_running)
101 return;
102
103 __set_sched_clock_stable();
104}
105
106static void __clear_sched_clock_stable(struct work_struct *work)
107{
108
109 if (sched_clock_stable())
110 static_key_slow_dec(&__sched_clock_stable);
111}
112
113static DECLARE_WORK(sched_clock_work, __clear_sched_clock_stable);
114
115void clear_sched_clock_stable(void)
116{
117 __sched_clock_stable_early = 0;
118
119 smp_mb();
120
121 if (!sched_clock_running)
122 return;
123
124 schedule_work(&sched_clock_work);
125}
126
127struct sched_clock_data {
128 u64 tick_raw;
129 u64 tick_gtod;
130 u64 clock;
131};
132
133static DEFINE_PER_CPU_SHARED_ALIGNED(struct sched_clock_data, sched_clock_data);
134
135static inline struct sched_clock_data *this_scd(void)
136{
137 return this_cpu_ptr(&sched_clock_data);
138}
139
140static inline struct sched_clock_data *cpu_sdc(int cpu)
141{
142 return &per_cpu(sched_clock_data, cpu);
143}
144
145void sched_clock_init(void)
146{
147 u64 ktime_now = ktime_to_ns(ktime_get());
148 int cpu;
149
150 for_each_possible_cpu(cpu) {
151 struct sched_clock_data *scd = cpu_sdc(cpu);
152
153 scd->tick_raw = 0;
154 scd->tick_gtod = ktime_now;
155 scd->clock = ktime_now;
156 }
157
158 sched_clock_running = 1;
159
160
161
162
163
164
165
166
167 smp_mb();
168
169 if (__sched_clock_stable_early)
170 __set_sched_clock_stable();
171 else
172 __clear_sched_clock_stable(NULL);
173}
174
175
176
177
178
179static inline u64 wrap_min(u64 x, u64 y)
180{
181 return (s64)(x - y) < 0 ? x : y;
182}
183
184static inline u64 wrap_max(u64 x, u64 y)
185{
186 return (s64)(x - y) > 0 ? x : y;
187}
188
189
190
191
192
193
194
195static u64 sched_clock_local(struct sched_clock_data *scd)
196{
197 u64 now, clock, old_clock, min_clock, max_clock;
198 s64 delta;
199
200again:
201 now = sched_clock();
202 delta = now - scd->tick_raw;
203 if (unlikely(delta < 0))
204 delta = 0;
205
206 old_clock = scd->clock;
207
208
209
210
211
212
213
214 clock = scd->tick_gtod + delta;
215 min_clock = wrap_max(scd->tick_gtod, old_clock);
216 max_clock = wrap_max(old_clock, scd->tick_gtod + TICK_NSEC);
217
218 clock = wrap_max(clock, min_clock);
219 clock = wrap_min(clock, max_clock);
220
221 if (cmpxchg64(&scd->clock, old_clock, clock) != old_clock)
222 goto again;
223
224 return clock;
225}
226
227static u64 sched_clock_remote(struct sched_clock_data *scd)
228{
229 struct sched_clock_data *my_scd = this_scd();
230 u64 this_clock, remote_clock;
231 u64 *ptr, old_val, val;
232
233#if BITS_PER_LONG != 64
234again:
235
236
237
238
239
240
241
242
243
244
245
246 this_clock = sched_clock_local(my_scd);
247
248
249
250
251
252 remote_clock = cmpxchg64(&scd->clock, 0, 0);
253#else
254
255
256
257
258 sched_clock_local(my_scd);
259again:
260 this_clock = my_scd->clock;
261 remote_clock = scd->clock;
262#endif
263
264
265
266
267
268
269
270 if (likely((s64)(remote_clock - this_clock) < 0)) {
271 ptr = &scd->clock;
272 old_val = remote_clock;
273 val = this_clock;
274 } else {
275
276
277
278 ptr = &my_scd->clock;
279 old_val = this_clock;
280 val = remote_clock;
281 }
282
283 if (cmpxchg64(ptr, old_val, val) != old_val)
284 goto again;
285
286 return val;
287}
288
289
290
291
292
293
294u64 sched_clock_cpu(int cpu)
295{
296 struct sched_clock_data *scd;
297 u64 clock;
298
299 if (sched_clock_stable())
300 return sched_clock();
301
302 if (unlikely(!sched_clock_running))
303 return 0ull;
304
305 preempt_disable_notrace();
306 scd = cpu_sdc(cpu);
307
308 if (cpu != smp_processor_id())
309 clock = sched_clock_remote(scd);
310 else
311 clock = sched_clock_local(scd);
312 preempt_enable_notrace();
313
314 return clock;
315}
316
317void sched_clock_tick(void)
318{
319 struct sched_clock_data *scd;
320 u64 now, now_gtod;
321
322 if (sched_clock_stable())
323 return;
324
325 if (unlikely(!sched_clock_running))
326 return;
327
328 WARN_ON_ONCE(!irqs_disabled());
329
330 scd = this_scd();
331 now_gtod = ktime_to_ns(ktime_get());
332 now = sched_clock();
333
334 scd->tick_raw = now;
335 scd->tick_gtod = now_gtod;
336 sched_clock_local(scd);
337}
338
339
340
341
342void sched_clock_idle_sleep_event(void)
343{
344 sched_clock_cpu(smp_processor_id());
345}
346EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
347
348
349
350
351void sched_clock_idle_wakeup_event(u64 delta_ns)
352{
353 if (timekeeping_suspended)
354 return;
355
356 sched_clock_tick();
357 touch_softlockup_watchdog();
358}
359EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
360
361
362
363
364
365
366
367
368
369
370
371u64 cpu_clock(int cpu)
372{
373 if (!sched_clock_stable())
374 return sched_clock_cpu(cpu);
375
376 return sched_clock();
377}
378
379
380
381
382
383
384
385
386u64 local_clock(void)
387{
388 if (!sched_clock_stable())
389 return sched_clock_cpu(raw_smp_processor_id());
390
391 return sched_clock();
392}
393
394#else
395
396void sched_clock_init(void)
397{
398 sched_clock_running = 1;
399}
400
401u64 sched_clock_cpu(int cpu)
402{
403 if (unlikely(!sched_clock_running))
404 return 0;
405
406 return sched_clock();
407}
408
409u64 cpu_clock(int cpu)
410{
411 return sched_clock();
412}
413
414u64 local_clock(void)
415{
416 return sched_clock();
417}
418
419#endif
420
421EXPORT_SYMBOL_GPL(cpu_clock);
422EXPORT_SYMBOL_GPL(local_clock);
423