Commit | Line | Data |
---|---|---|
79bf2bb3 TG |
1 | /* |
2 | * linux/kernel/time/tick-sched.c | |
3 | * | |
4 | * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de> | |
5 | * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar | |
6 | * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner | |
7 | * | |
8 | * No idle tick implementation for low and high resolution timers | |
9 | * | |
10 | * Started by: Thomas Gleixner and Ingo Molnar | |
11 | * | |
b10db7f0 | 12 | * Distribute under GPLv2. |
79bf2bb3 TG |
13 | */ |
14 | #include <linux/cpu.h> | |
15 | #include <linux/err.h> | |
16 | #include <linux/hrtimer.h> | |
17 | #include <linux/interrupt.h> | |
18 | #include <linux/kernel_stat.h> | |
19 | #include <linux/percpu.h> | |
20 | #include <linux/profile.h> | |
21 | #include <linux/sched.h> | |
22 | #include <linux/tick.h> | |
23 | ||
9e203bcc DM |
24 | #include <asm/irq_regs.h> |
25 | ||
79bf2bb3 TG |
26 | #include "tick-internal.h" |
27 | ||
28 | /* | |
29 | * Per cpu nohz control structure | |
30 | */ | |
31 | static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched); | |
32 | ||
33 | /* | |
34 | * The time, when the last jiffy update happened. Protected by xtime_lock. | |
35 | */ | |
36 | static ktime_t last_jiffies_update; | |
37 | ||
289f480a IM |
38 | struct tick_sched *tick_get_tick_sched(int cpu) |
39 | { | |
40 | return &per_cpu(tick_cpu_sched, cpu); | |
41 | } | |
42 | ||
79bf2bb3 TG |
43 | /* |
44 | * Must be called with interrupts disabled ! | |
45 | */ | |
46 | static void tick_do_update_jiffies64(ktime_t now) | |
47 | { | |
48 | unsigned long ticks = 0; | |
49 | ktime_t delta; | |
50 | ||
51 | /* Reevalute with xtime_lock held */ | |
52 | write_seqlock(&xtime_lock); | |
53 | ||
54 | delta = ktime_sub(now, last_jiffies_update); | |
55 | if (delta.tv64 >= tick_period.tv64) { | |
56 | ||
57 | delta = ktime_sub(delta, tick_period); | |
58 | last_jiffies_update = ktime_add(last_jiffies_update, | |
59 | tick_period); | |
60 | ||
61 | /* Slow path for long timeouts */ | |
62 | if (unlikely(delta.tv64 >= tick_period.tv64)) { | |
63 | s64 incr = ktime_to_ns(tick_period); | |
64 | ||
65 | ticks = ktime_divns(delta, incr); | |
66 | ||
67 | last_jiffies_update = ktime_add_ns(last_jiffies_update, | |
68 | incr * ticks); | |
69 | } | |
70 | do_timer(++ticks); | |
71 | } | |
72 | write_sequnlock(&xtime_lock); | |
73 | } | |
74 | ||
75 | /* | |
76 | * Initialize and return retrieve the jiffies update. | |
77 | */ | |
78 | static ktime_t tick_init_jiffy_update(void) | |
79 | { | |
80 | ktime_t period; | |
81 | ||
82 | write_seqlock(&xtime_lock); | |
83 | /* Did we start the jiffies update yet ? */ | |
84 | if (last_jiffies_update.tv64 == 0) | |
85 | last_jiffies_update = tick_next_period; | |
86 | period = last_jiffies_update; | |
87 | write_sequnlock(&xtime_lock); | |
88 | return period; | |
89 | } | |
90 | ||
91 | /* | |
92 | * NOHZ - aka dynamic tick functionality | |
93 | */ | |
94 | #ifdef CONFIG_NO_HZ | |
95 | /* | |
96 | * NO HZ enabled ? | |
97 | */ | |
98 | static int tick_nohz_enabled __read_mostly = 1; | |
99 | ||
100 | /* | |
101 | * Enable / Disable tickless mode | |
102 | */ | |
103 | static int __init setup_tick_nohz(char *str) | |
104 | { | |
105 | if (!strcmp(str, "off")) | |
106 | tick_nohz_enabled = 0; | |
107 | else if (!strcmp(str, "on")) | |
108 | tick_nohz_enabled = 1; | |
109 | else | |
110 | return 0; | |
111 | return 1; | |
112 | } | |
113 | ||
114 | __setup("nohz=", setup_tick_nohz); | |
115 | ||
116 | /** | |
117 | * tick_nohz_update_jiffies - update jiffies when idle was interrupted | |
118 | * | |
119 | * Called from interrupt entry when the CPU was idle | |
120 | * | |
121 | * In case the sched_tick was stopped on this CPU, we have to check if jiffies | |
122 | * must be updated. Otherwise an interrupt handler could use a stale jiffy | |
123 | * value. We do this unconditionally on any cpu, as we don't know whether the | |
124 | * cpu, which has the update task assigned is in a long sleep. | |
125 | */ | |
126 | void tick_nohz_update_jiffies(void) | |
127 | { | |
128 | int cpu = smp_processor_id(); | |
129 | struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu); | |
130 | unsigned long flags; | |
131 | ktime_t now; | |
132 | ||
133 | if (!ts->tick_stopped) | |
134 | return; | |
135 | ||
d3938204 TG |
136 | touch_softlockup_watchdog(); |
137 | ||
79bf2bb3 TG |
138 | cpu_clear(cpu, nohz_cpu_mask); |
139 | now = ktime_get(); | |
140 | ||
141 | local_irq_save(flags); | |
142 | tick_do_update_jiffies64(now); | |
143 | local_irq_restore(flags); | |
144 | } | |
145 | ||
6378ddb5 VP |
146 | void tick_nohz_stop_idle(int cpu) |
147 | { | |
148 | struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu); | |
149 | ||
150 | if (ts->idle_active) { | |
151 | ktime_t now, delta; | |
152 | now = ktime_get(); | |
153 | delta = ktime_sub(now, ts->idle_entrytime); | |
154 | ts->idle_lastupdate = now; | |
155 | ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta); | |
156 | ts->idle_active = 0; | |
157 | } | |
158 | } | |
159 | ||
160 | static ktime_t tick_nohz_start_idle(int cpu) | |
161 | { | |
162 | struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu); | |
163 | ktime_t now, delta; | |
164 | ||
165 | now = ktime_get(); | |
166 | if (ts->idle_active) { | |
167 | delta = ktime_sub(now, ts->idle_entrytime); | |
168 | ts->idle_lastupdate = now; | |
169 | ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta); | |
170 | } | |
171 | ts->idle_entrytime = now; | |
172 | ts->idle_active = 1; | |
173 | return now; | |
174 | } | |
175 | ||
176 | u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time) | |
177 | { | |
178 | struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu); | |
179 | ||
180 | *last_update_time = ktime_to_us(ts->idle_lastupdate); | |
181 | return ktime_to_us(ts->idle_sleeptime); | |
182 | } | |
183 | ||
79bf2bb3 TG |
184 | /** |
185 | * tick_nohz_stop_sched_tick - stop the idle tick from the idle task | |
186 | * | |
187 | * When the next event is more than a tick into the future, stop the idle tick | |
188 | * Called either from the idle loop or from irq_exit() when an idle period was | |
189 | * just interrupted by an interrupt which did not cause a reschedule. | |
190 | */ | |
191 | void tick_nohz_stop_sched_tick(void) | |
192 | { | |
193 | unsigned long seq, last_jiffies, next_jiffies, delta_jiffies, flags; | |
48d5e258 | 194 | unsigned long rt_jiffies; |
79bf2bb3 | 195 | struct tick_sched *ts; |
6378ddb5 | 196 | ktime_t last_update, expires, now; |
4f86d3a8 | 197 | struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev; |
79bf2bb3 TG |
198 | int cpu; |
199 | ||
200 | local_irq_save(flags); | |
201 | ||
202 | cpu = smp_processor_id(); | |
6378ddb5 | 203 | now = tick_nohz_start_idle(cpu); |
79bf2bb3 TG |
204 | ts = &per_cpu(tick_cpu_sched, cpu); |
205 | ||
5e41d0d6 TG |
206 | /* |
207 | * If this cpu is offline and it is the one which updates | |
208 | * jiffies, then give up the assignment and let it be taken by | |
209 | * the cpu which runs the tick timer next. If we don't drop | |
210 | * this here the jiffies might be stale and do_timer() never | |
211 | * invoked. | |
212 | */ | |
213 | if (unlikely(!cpu_online(cpu))) { | |
214 | if (cpu == tick_do_timer_cpu) | |
215 | tick_do_timer_cpu = -1; | |
216 | } | |
217 | ||
79bf2bb3 TG |
218 | if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE)) |
219 | goto end; | |
220 | ||
221 | if (need_resched()) | |
222 | goto end; | |
223 | ||
224 | cpu = smp_processor_id(); | |
35282316 TG |
225 | if (unlikely(local_softirq_pending())) { |
226 | static int ratelimit; | |
227 | ||
228 | if (ratelimit < 10) { | |
229 | printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n", | |
230 | local_softirq_pending()); | |
231 | ratelimit++; | |
232 | } | |
233 | } | |
79bf2bb3 | 234 | |
79bf2bb3 | 235 | ts->idle_calls++; |
79bf2bb3 TG |
236 | /* Read jiffies and the time when jiffies were updated last */ |
237 | do { | |
238 | seq = read_seqbegin(&xtime_lock); | |
239 | last_update = last_jiffies_update; | |
240 | last_jiffies = jiffies; | |
241 | } while (read_seqretry(&xtime_lock, seq)); | |
242 | ||
243 | /* Get the next timer wheel timer */ | |
244 | next_jiffies = get_next_timer_interrupt(last_jiffies); | |
245 | delta_jiffies = next_jiffies - last_jiffies; | |
246 | ||
48d5e258 PZ |
247 | rt_jiffies = rt_needs_cpu(cpu); |
248 | if (rt_jiffies && rt_jiffies < delta_jiffies) | |
249 | delta_jiffies = rt_jiffies; | |
250 | ||
6ba9b346 IM |
251 | if (rcu_needs_cpu(cpu)) |
252 | delta_jiffies = 1; | |
79bf2bb3 TG |
253 | /* |
254 | * Do not stop the tick, if we are only one off | |
255 | * or if the cpu is required for rcu | |
256 | */ | |
6ba9b346 | 257 | if (!ts->tick_stopped && delta_jiffies == 1) |
79bf2bb3 TG |
258 | goto out; |
259 | ||
260 | /* Schedule the tick, if we are at least one jiffie off */ | |
261 | if ((long)delta_jiffies >= 1) { | |
262 | ||
6ba9b346 | 263 | if (delta_jiffies > 1) |
79bf2bb3 TG |
264 | cpu_set(cpu, nohz_cpu_mask); |
265 | /* | |
266 | * nohz_stop_sched_tick can be called several times before | |
267 | * the nohz_restart_sched_tick is called. This happens when | |
268 | * interrupts arrive which do not cause a reschedule. In the | |
269 | * first call we save the current tick time, so we can restart | |
270 | * the scheduler tick in nohz_restart_sched_tick. | |
271 | */ | |
272 | if (!ts->tick_stopped) { | |
46cb4b7c SS |
273 | if (select_nohz_load_balancer(1)) { |
274 | /* | |
275 | * sched tick not stopped! | |
276 | */ | |
277 | cpu_clear(cpu, nohz_cpu_mask); | |
278 | goto out; | |
279 | } | |
280 | ||
79bf2bb3 TG |
281 | ts->idle_tick = ts->sched_timer.expires; |
282 | ts->tick_stopped = 1; | |
283 | ts->idle_jiffies = last_jiffies; | |
284 | } | |
d3ed7824 TG |
285 | |
286 | /* | |
287 | * If this cpu is the one which updates jiffies, then | |
288 | * give up the assignment and let it be taken by the | |
289 | * cpu which runs the tick timer next, which might be | |
290 | * this cpu as well. If we don't drop this here the | |
291 | * jiffies might be stale and do_timer() never | |
292 | * invoked. | |
293 | */ | |
294 | if (cpu == tick_do_timer_cpu) | |
295 | tick_do_timer_cpu = -1; | |
296 | ||
eaad084b TG |
297 | ts->idle_sleeps++; |
298 | ||
299 | /* | |
300 | * delta_jiffies >= NEXT_TIMER_MAX_DELTA signals that | |
301 | * there is no timer pending or at least extremly far | |
302 | * into the future (12 days for HZ=1000). In this case | |
303 | * we simply stop the tick timer: | |
304 | */ | |
305 | if (unlikely(delta_jiffies >= NEXT_TIMER_MAX_DELTA)) { | |
306 | ts->idle_expires.tv64 = KTIME_MAX; | |
307 | if (ts->nohz_mode == NOHZ_MODE_HIGHRES) | |
308 | hrtimer_cancel(&ts->sched_timer); | |
309 | goto out; | |
310 | } | |
311 | ||
79bf2bb3 TG |
312 | /* |
313 | * calculate the expiry time for the next timer wheel | |
314 | * timer | |
315 | */ | |
316 | expires = ktime_add_ns(last_update, tick_period.tv64 * | |
317 | delta_jiffies); | |
318 | ts->idle_expires = expires; | |
79bf2bb3 TG |
319 | |
320 | if (ts->nohz_mode == NOHZ_MODE_HIGHRES) { | |
321 | hrtimer_start(&ts->sched_timer, expires, | |
322 | HRTIMER_MODE_ABS); | |
323 | /* Check, if the timer was already in the past */ | |
324 | if (hrtimer_active(&ts->sched_timer)) | |
325 | goto out; | |
4c9dc641 | 326 | } else if (!tick_program_event(expires, 0)) |
79bf2bb3 TG |
327 | goto out; |
328 | /* | |
329 | * We are past the event already. So we crossed a | |
330 | * jiffie boundary. Update jiffies and raise the | |
331 | * softirq. | |
332 | */ | |
333 | tick_do_update_jiffies64(ktime_get()); | |
334 | cpu_clear(cpu, nohz_cpu_mask); | |
335 | } | |
336 | raise_softirq_irqoff(TIMER_SOFTIRQ); | |
337 | out: | |
338 | ts->next_jiffies = next_jiffies; | |
339 | ts->last_jiffies = last_jiffies; | |
4f86d3a8 | 340 | ts->sleep_length = ktime_sub(dev->next_event, now); |
79bf2bb3 TG |
341 | end: |
342 | local_irq_restore(flags); | |
343 | } | |
344 | ||
4f86d3a8 LB |
345 | /** |
346 | * tick_nohz_get_sleep_length - return the length of the current sleep | |
347 | * | |
348 | * Called from power state control code with interrupts disabled | |
349 | */ | |
350 | ktime_t tick_nohz_get_sleep_length(void) | |
351 | { | |
352 | struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched); | |
353 | ||
354 | return ts->sleep_length; | |
355 | } | |
356 | ||
79bf2bb3 | 357 | /** |
8dce39c2 | 358 | * tick_nohz_restart_sched_tick - restart the idle tick from the idle task |
79bf2bb3 TG |
359 | * |
360 | * Restart the idle tick when the CPU is woken up from idle | |
361 | */ | |
362 | void tick_nohz_restart_sched_tick(void) | |
363 | { | |
364 | int cpu = smp_processor_id(); | |
365 | struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu); | |
366 | unsigned long ticks; | |
6378ddb5 | 367 | ktime_t now; |
79bf2bb3 | 368 | |
6378ddb5 VP |
369 | local_irq_disable(); |
370 | tick_nohz_stop_idle(cpu); | |
371 | ||
372 | if (!ts->tick_stopped) { | |
373 | local_irq_enable(); | |
79bf2bb3 | 374 | return; |
6378ddb5 | 375 | } |
79bf2bb3 TG |
376 | |
377 | /* Update jiffies first */ | |
46cb4b7c | 378 | select_nohz_load_balancer(0); |
6378ddb5 | 379 | now = ktime_get(); |
79bf2bb3 TG |
380 | tick_do_update_jiffies64(now); |
381 | cpu_clear(cpu, nohz_cpu_mask); | |
382 | ||
79bf2bb3 TG |
383 | /* |
384 | * We stopped the tick in idle. Update process times would miss the | |
385 | * time we slept as update_process_times does only a 1 tick | |
386 | * accounting. Enforce that this is accounted to idle ! | |
387 | */ | |
388 | ticks = jiffies - ts->idle_jiffies; | |
389 | /* | |
390 | * We might be one off. Do not randomly account a huge number of ticks! | |
391 | */ | |
392 | if (ticks && ticks < LONG_MAX) { | |
393 | add_preempt_count(HARDIRQ_OFFSET); | |
394 | account_system_time(current, HARDIRQ_OFFSET, | |
395 | jiffies_to_cputime(ticks)); | |
396 | sub_preempt_count(HARDIRQ_OFFSET); | |
397 | } | |
398 | ||
399 | /* | |
400 | * Cancel the scheduled timer and restore the tick | |
401 | */ | |
402 | ts->tick_stopped = 0; | |
403 | hrtimer_cancel(&ts->sched_timer); | |
404 | ts->sched_timer.expires = ts->idle_tick; | |
405 | ||
406 | while (1) { | |
407 | /* Forward the time to expire in the future */ | |
408 | hrtimer_forward(&ts->sched_timer, now, tick_period); | |
409 | ||
410 | if (ts->nohz_mode == NOHZ_MODE_HIGHRES) { | |
411 | hrtimer_start(&ts->sched_timer, | |
412 | ts->sched_timer.expires, | |
413 | HRTIMER_MODE_ABS); | |
414 | /* Check, if the timer was already in the past */ | |
415 | if (hrtimer_active(&ts->sched_timer)) | |
416 | break; | |
417 | } else { | |
418 | if (!tick_program_event(ts->sched_timer.expires, 0)) | |
419 | break; | |
420 | } | |
421 | /* Update jiffies and reread time */ | |
422 | tick_do_update_jiffies64(now); | |
423 | now = ktime_get(); | |
424 | } | |
425 | local_irq_enable(); | |
426 | } | |
427 | ||
428 | static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now) | |
429 | { | |
430 | hrtimer_forward(&ts->sched_timer, now, tick_period); | |
431 | return tick_program_event(ts->sched_timer.expires, 0); | |
432 | } | |
433 | ||
434 | /* | |
435 | * The nohz low res interrupt handler | |
436 | */ | |
437 | static void tick_nohz_handler(struct clock_event_device *dev) | |
438 | { | |
439 | struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched); | |
440 | struct pt_regs *regs = get_irq_regs(); | |
d3ed7824 | 441 | int cpu = smp_processor_id(); |
79bf2bb3 TG |
442 | ktime_t now = ktime_get(); |
443 | ||
444 | dev->next_event.tv64 = KTIME_MAX; | |
445 | ||
d3ed7824 TG |
446 | /* |
447 | * Check if the do_timer duty was dropped. We don't care about | |
448 | * concurrency: This happens only when the cpu in charge went | |
449 | * into a long sleep. If two cpus happen to assign themself to | |
450 | * this duty, then the jiffies update is still serialized by | |
451 | * xtime_lock. | |
452 | */ | |
453 | if (unlikely(tick_do_timer_cpu == -1)) | |
454 | tick_do_timer_cpu = cpu; | |
455 | ||
79bf2bb3 | 456 | /* Check, if the jiffies need an update */ |
d3ed7824 TG |
457 | if (tick_do_timer_cpu == cpu) |
458 | tick_do_update_jiffies64(now); | |
79bf2bb3 TG |
459 | |
460 | /* | |
461 | * When we are idle and the tick is stopped, we have to touch | |
462 | * the watchdog as we might not schedule for a really long | |
463 | * time. This happens on complete idle SMP systems while | |
464 | * waiting on the login prompt. We also increment the "start | |
465 | * of idle" jiffy stamp so the idle accounting adjustment we | |
466 | * do when we go busy again does not account too much ticks. | |
467 | */ | |
468 | if (ts->tick_stopped) { | |
469 | touch_softlockup_watchdog(); | |
470 | ts->idle_jiffies++; | |
471 | } | |
472 | ||
473 | update_process_times(user_mode(regs)); | |
474 | profile_tick(CPU_PROFILING); | |
475 | ||
476 | /* Do not restart, when we are in the idle loop */ | |
477 | if (ts->tick_stopped) | |
478 | return; | |
479 | ||
480 | while (tick_nohz_reprogram(ts, now)) { | |
481 | now = ktime_get(); | |
482 | tick_do_update_jiffies64(now); | |
483 | } | |
484 | } | |
485 | ||
486 | /** | |
487 | * tick_nohz_switch_to_nohz - switch to nohz mode | |
488 | */ | |
489 | static void tick_nohz_switch_to_nohz(void) | |
490 | { | |
491 | struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched); | |
492 | ktime_t next; | |
493 | ||
494 | if (!tick_nohz_enabled) | |
495 | return; | |
496 | ||
497 | local_irq_disable(); | |
498 | if (tick_switch_to_oneshot(tick_nohz_handler)) { | |
499 | local_irq_enable(); | |
500 | return; | |
501 | } | |
502 | ||
503 | ts->nohz_mode = NOHZ_MODE_LOWRES; | |
504 | ||
505 | /* | |
506 | * Recycle the hrtimer in ts, so we can share the | |
507 | * hrtimer_forward with the highres code. | |
508 | */ | |
509 | hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); | |
510 | /* Get the next period */ | |
511 | next = tick_init_jiffy_update(); | |
512 | ||
513 | for (;;) { | |
514 | ts->sched_timer.expires = next; | |
515 | if (!tick_program_event(next, 0)) | |
516 | break; | |
517 | next = ktime_add(next, tick_period); | |
518 | } | |
519 | local_irq_enable(); | |
520 | ||
521 | printk(KERN_INFO "Switched to NOHz mode on CPU #%d\n", | |
522 | smp_processor_id()); | |
523 | } | |
524 | ||
525 | #else | |
526 | ||
527 | static inline void tick_nohz_switch_to_nohz(void) { } | |
528 | ||
529 | #endif /* NO_HZ */ | |
530 | ||
531 | /* | |
532 | * High resolution timer specific code | |
533 | */ | |
534 | #ifdef CONFIG_HIGH_RES_TIMERS | |
535 | /* | |
4c9dc641 | 536 | * We rearm the timer until we get disabled by the idle code. |
79bf2bb3 TG |
537 | * Called with interrupts disabled and timer->base->cpu_base->lock held. |
538 | */ | |
539 | static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer) | |
540 | { | |
541 | struct tick_sched *ts = | |
542 | container_of(timer, struct tick_sched, sched_timer); | |
79bf2bb3 TG |
543 | struct pt_regs *regs = get_irq_regs(); |
544 | ktime_t now = ktime_get(); | |
d3ed7824 TG |
545 | int cpu = smp_processor_id(); |
546 | ||
547 | #ifdef CONFIG_NO_HZ | |
548 | /* | |
549 | * Check if the do_timer duty was dropped. We don't care about | |
550 | * concurrency: This happens only when the cpu in charge went | |
551 | * into a long sleep. If two cpus happen to assign themself to | |
552 | * this duty, then the jiffies update is still serialized by | |
553 | * xtime_lock. | |
554 | */ | |
555 | if (unlikely(tick_do_timer_cpu == -1)) | |
556 | tick_do_timer_cpu = cpu; | |
557 | #endif | |
79bf2bb3 TG |
558 | |
559 | /* Check, if the jiffies need an update */ | |
d3ed7824 TG |
560 | if (tick_do_timer_cpu == cpu) |
561 | tick_do_update_jiffies64(now); | |
79bf2bb3 TG |
562 | |
563 | /* | |
564 | * Do not call, when we are not in irq context and have | |
565 | * no valid regs pointer | |
566 | */ | |
567 | if (regs) { | |
568 | /* | |
569 | * When we are idle and the tick is stopped, we have to touch | |
570 | * the watchdog as we might not schedule for a really long | |
571 | * time. This happens on complete idle SMP systems while | |
572 | * waiting on the login prompt. We also increment the "start of | |
573 | * idle" jiffy stamp so the idle accounting adjustment we do | |
574 | * when we go busy again does not account too much ticks. | |
575 | */ | |
576 | if (ts->tick_stopped) { | |
577 | touch_softlockup_watchdog(); | |
578 | ts->idle_jiffies++; | |
579 | } | |
79bf2bb3 TG |
580 | update_process_times(user_mode(regs)); |
581 | profile_tick(CPU_PROFILING); | |
79bf2bb3 TG |
582 | } |
583 | ||
584 | /* Do not restart, when we are in the idle loop */ | |
585 | if (ts->tick_stopped) | |
586 | return HRTIMER_NORESTART; | |
587 | ||
588 | hrtimer_forward(timer, now, tick_period); | |
589 | ||
590 | return HRTIMER_RESTART; | |
591 | } | |
592 | ||
593 | /** | |
594 | * tick_setup_sched_timer - setup the tick emulation timer | |
595 | */ | |
596 | void tick_setup_sched_timer(void) | |
597 | { | |
598 | struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched); | |
599 | ktime_t now = ktime_get(); | |
3704540b | 600 | u64 offset; |
79bf2bb3 TG |
601 | |
602 | /* | |
603 | * Emulate tick processing via per-CPU hrtimers: | |
604 | */ | |
605 | hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); | |
606 | ts->sched_timer.function = tick_sched_timer; | |
607 | ts->sched_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ; | |
608 | ||
3704540b | 609 | /* Get the next period (per cpu) */ |
79bf2bb3 | 610 | ts->sched_timer.expires = tick_init_jiffy_update(); |
3704540b | 611 | offset = ktime_to_ns(tick_period) >> 1; |
b2d9323d | 612 | do_div(offset, num_possible_cpus()); |
3704540b | 613 | offset *= smp_processor_id(); |
614 | ts->sched_timer.expires = ktime_add_ns(ts->sched_timer.expires, offset); | |
79bf2bb3 TG |
615 | |
616 | for (;;) { | |
617 | hrtimer_forward(&ts->sched_timer, now, tick_period); | |
618 | hrtimer_start(&ts->sched_timer, ts->sched_timer.expires, | |
619 | HRTIMER_MODE_ABS); | |
620 | /* Check, if the timer was already in the past */ | |
621 | if (hrtimer_active(&ts->sched_timer)) | |
622 | break; | |
623 | now = ktime_get(); | |
624 | } | |
625 | ||
626 | #ifdef CONFIG_NO_HZ | |
627 | if (tick_nohz_enabled) | |
628 | ts->nohz_mode = NOHZ_MODE_HIGHRES; | |
629 | #endif | |
630 | } | |
631 | ||
632 | void tick_cancel_sched_timer(int cpu) | |
633 | { | |
634 | struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu); | |
635 | ||
636 | if (ts->sched_timer.base) | |
637 | hrtimer_cancel(&ts->sched_timer); | |
638 | ts->tick_stopped = 0; | |
639 | ts->nohz_mode = NOHZ_MODE_INACTIVE; | |
640 | } | |
641 | #endif /* HIGH_RES_TIMERS */ | |
642 | ||
643 | /** | |
644 | * Async notification about clocksource changes | |
645 | */ | |
646 | void tick_clock_notify(void) | |
647 | { | |
648 | int cpu; | |
649 | ||
650 | for_each_possible_cpu(cpu) | |
651 | set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks); | |
652 | } | |
653 | ||
654 | /* | |
655 | * Async notification about clock event changes | |
656 | */ | |
657 | void tick_oneshot_notify(void) | |
658 | { | |
659 | struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched); | |
660 | ||
661 | set_bit(0, &ts->check_clocks); | |
662 | } | |
663 | ||
664 | /** | |
665 | * Check, if a change happened, which makes oneshot possible. | |
666 | * | |
667 | * Called cyclic from the hrtimer softirq (driven by the timer | |
668 | * softirq) allow_nohz signals, that we can switch into low-res nohz | |
669 | * mode, because high resolution timers are disabled (either compile | |
670 | * or runtime). | |
671 | */ | |
672 | int tick_check_oneshot_change(int allow_nohz) | |
673 | { | |
674 | struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched); | |
675 | ||
676 | if (!test_and_clear_bit(0, &ts->check_clocks)) | |
677 | return 0; | |
678 | ||
679 | if (ts->nohz_mode != NOHZ_MODE_INACTIVE) | |
680 | return 0; | |
681 | ||
682 | if (!timekeeping_is_continuous() || !tick_is_oneshot_available()) | |
683 | return 0; | |
684 | ||
685 | if (!allow_nohz) | |
686 | return 1; | |
687 | ||
688 | tick_nohz_switch_to_nohz(); | |
689 | return 0; | |
690 | } |