]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/time/tick-sched.c
spi doc update: describe clock mode bits
[net-next-2.6.git] / kernel / time / tick-sched.c
CommitLineData
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 *
12 * For licencing details see kernel-base/COPYING
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 */
31static 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 */
36static ktime_t last_jiffies_update;
37
289f480a
IM
38struct 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 */
46static 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 */
78static 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 */
98static int tick_nohz_enabled __read_mostly = 1;
99
100/*
101 * Enable / Disable tickless mode
102 */
103static 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 */
126void 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
136 cpu_clear(cpu, nohz_cpu_mask);
137 now = ktime_get();
138
139 local_irq_save(flags);
140 tick_do_update_jiffies64(now);
141 local_irq_restore(flags);
142}
143
144/**
145 * tick_nohz_stop_sched_tick - stop the idle tick from the idle task
146 *
147 * When the next event is more than a tick into the future, stop the idle tick
148 * Called either from the idle loop or from irq_exit() when an idle period was
149 * just interrupted by an interrupt which did not cause a reschedule.
150 */
151void tick_nohz_stop_sched_tick(void)
152{
153 unsigned long seq, last_jiffies, next_jiffies, delta_jiffies, flags;
154 struct tick_sched *ts;
155 ktime_t last_update, expires, now, delta;
156 int cpu;
157
158 local_irq_save(flags);
159
160 cpu = smp_processor_id();
161 ts = &per_cpu(tick_cpu_sched, cpu);
162
163 if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
164 goto end;
165
166 if (need_resched())
167 goto end;
168
169 cpu = smp_processor_id();
bc5393a6
TG
170 if (unlikely(local_softirq_pending()))
171 printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
172 local_softirq_pending());
79bf2bb3
TG
173
174 now = ktime_get();
175 /*
176 * When called from irq_exit we need to account the idle sleep time
177 * correctly.
178 */
179 if (ts->tick_stopped) {
180 delta = ktime_sub(now, ts->idle_entrytime);
181 ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
182 }
183
184 ts->idle_entrytime = now;
185 ts->idle_calls++;
186
187 /* Read jiffies and the time when jiffies were updated last */
188 do {
189 seq = read_seqbegin(&xtime_lock);
190 last_update = last_jiffies_update;
191 last_jiffies = jiffies;
192 } while (read_seqretry(&xtime_lock, seq));
193
194 /* Get the next timer wheel timer */
195 next_jiffies = get_next_timer_interrupt(last_jiffies);
196 delta_jiffies = next_jiffies - last_jiffies;
197
6ba9b346
IM
198 if (rcu_needs_cpu(cpu))
199 delta_jiffies = 1;
79bf2bb3
TG
200 /*
201 * Do not stop the tick, if we are only one off
202 * or if the cpu is required for rcu
203 */
6ba9b346 204 if (!ts->tick_stopped && delta_jiffies == 1)
79bf2bb3
TG
205 goto out;
206
207 /* Schedule the tick, if we are at least one jiffie off */
208 if ((long)delta_jiffies >= 1) {
209
6ba9b346 210 if (delta_jiffies > 1)
79bf2bb3
TG
211 cpu_set(cpu, nohz_cpu_mask);
212 /*
213 * nohz_stop_sched_tick can be called several times before
214 * the nohz_restart_sched_tick is called. This happens when
215 * interrupts arrive which do not cause a reschedule. In the
216 * first call we save the current tick time, so we can restart
217 * the scheduler tick in nohz_restart_sched_tick.
218 */
219 if (!ts->tick_stopped) {
46cb4b7c
SS
220 if (select_nohz_load_balancer(1)) {
221 /*
222 * sched tick not stopped!
223 */
224 cpu_clear(cpu, nohz_cpu_mask);
225 goto out;
226 }
227
79bf2bb3
TG
228 ts->idle_tick = ts->sched_timer.expires;
229 ts->tick_stopped = 1;
230 ts->idle_jiffies = last_jiffies;
231 }
d3ed7824
TG
232
233 /*
234 * If this cpu is the one which updates jiffies, then
235 * give up the assignment and let it be taken by the
236 * cpu which runs the tick timer next, which might be
237 * this cpu as well. If we don't drop this here the
238 * jiffies might be stale and do_timer() never
239 * invoked.
240 */
241 if (cpu == tick_do_timer_cpu)
242 tick_do_timer_cpu = -1;
243
79bf2bb3
TG
244 /*
245 * calculate the expiry time for the next timer wheel
246 * timer
247 */
248 expires = ktime_add_ns(last_update, tick_period.tv64 *
249 delta_jiffies);
250 ts->idle_expires = expires;
251 ts->idle_sleeps++;
252
253 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
254 hrtimer_start(&ts->sched_timer, expires,
255 HRTIMER_MODE_ABS);
256 /* Check, if the timer was already in the past */
257 if (hrtimer_active(&ts->sched_timer))
258 goto out;
259 } else if(!tick_program_event(expires, 0))
260 goto out;
261 /*
262 * We are past the event already. So we crossed a
263 * jiffie boundary. Update jiffies and raise the
264 * softirq.
265 */
266 tick_do_update_jiffies64(ktime_get());
267 cpu_clear(cpu, nohz_cpu_mask);
268 }
269 raise_softirq_irqoff(TIMER_SOFTIRQ);
270out:
271 ts->next_jiffies = next_jiffies;
272 ts->last_jiffies = last_jiffies;
273end:
274 local_irq_restore(flags);
275}
276
277/**
278 * nohz_restart_sched_tick - restart the idle tick from the idle task
279 *
280 * Restart the idle tick when the CPU is woken up from idle
281 */
282void tick_nohz_restart_sched_tick(void)
283{
284 int cpu = smp_processor_id();
285 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
286 unsigned long ticks;
287 ktime_t now, delta;
288
289 if (!ts->tick_stopped)
290 return;
291
292 /* Update jiffies first */
293 now = ktime_get();
294
295 local_irq_disable();
46cb4b7c 296 select_nohz_load_balancer(0);
79bf2bb3
TG
297 tick_do_update_jiffies64(now);
298 cpu_clear(cpu, nohz_cpu_mask);
299
300 /* Account the idle time */
301 delta = ktime_sub(now, ts->idle_entrytime);
302 ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
303
304 /*
305 * We stopped the tick in idle. Update process times would miss the
306 * time we slept as update_process_times does only a 1 tick
307 * accounting. Enforce that this is accounted to idle !
308 */
309 ticks = jiffies - ts->idle_jiffies;
310 /*
311 * We might be one off. Do not randomly account a huge number of ticks!
312 */
313 if (ticks && ticks < LONG_MAX) {
314 add_preempt_count(HARDIRQ_OFFSET);
315 account_system_time(current, HARDIRQ_OFFSET,
316 jiffies_to_cputime(ticks));
317 sub_preempt_count(HARDIRQ_OFFSET);
318 }
319
320 /*
321 * Cancel the scheduled timer and restore the tick
322 */
323 ts->tick_stopped = 0;
324 hrtimer_cancel(&ts->sched_timer);
325 ts->sched_timer.expires = ts->idle_tick;
326
327 while (1) {
328 /* Forward the time to expire in the future */
329 hrtimer_forward(&ts->sched_timer, now, tick_period);
330
331 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
332 hrtimer_start(&ts->sched_timer,
333 ts->sched_timer.expires,
334 HRTIMER_MODE_ABS);
335 /* Check, if the timer was already in the past */
336 if (hrtimer_active(&ts->sched_timer))
337 break;
338 } else {
339 if (!tick_program_event(ts->sched_timer.expires, 0))
340 break;
341 }
342 /* Update jiffies and reread time */
343 tick_do_update_jiffies64(now);
344 now = ktime_get();
345 }
346 local_irq_enable();
347}
348
349static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
350{
351 hrtimer_forward(&ts->sched_timer, now, tick_period);
352 return tick_program_event(ts->sched_timer.expires, 0);
353}
354
355/*
356 * The nohz low res interrupt handler
357 */
358static void tick_nohz_handler(struct clock_event_device *dev)
359{
360 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
361 struct pt_regs *regs = get_irq_regs();
d3ed7824 362 int cpu = smp_processor_id();
79bf2bb3
TG
363 ktime_t now = ktime_get();
364
365 dev->next_event.tv64 = KTIME_MAX;
366
d3ed7824
TG
367 /*
368 * Check if the do_timer duty was dropped. We don't care about
369 * concurrency: This happens only when the cpu in charge went
370 * into a long sleep. If two cpus happen to assign themself to
371 * this duty, then the jiffies update is still serialized by
372 * xtime_lock.
373 */
374 if (unlikely(tick_do_timer_cpu == -1))
375 tick_do_timer_cpu = cpu;
376
79bf2bb3 377 /* Check, if the jiffies need an update */
d3ed7824
TG
378 if (tick_do_timer_cpu == cpu)
379 tick_do_update_jiffies64(now);
79bf2bb3
TG
380
381 /*
382 * When we are idle and the tick is stopped, we have to touch
383 * the watchdog as we might not schedule for a really long
384 * time. This happens on complete idle SMP systems while
385 * waiting on the login prompt. We also increment the "start
386 * of idle" jiffy stamp so the idle accounting adjustment we
387 * do when we go busy again does not account too much ticks.
388 */
389 if (ts->tick_stopped) {
390 touch_softlockup_watchdog();
391 ts->idle_jiffies++;
392 }
393
394 update_process_times(user_mode(regs));
395 profile_tick(CPU_PROFILING);
396
397 /* Do not restart, when we are in the idle loop */
398 if (ts->tick_stopped)
399 return;
400
401 while (tick_nohz_reprogram(ts, now)) {
402 now = ktime_get();
403 tick_do_update_jiffies64(now);
404 }
405}
406
407/**
408 * tick_nohz_switch_to_nohz - switch to nohz mode
409 */
410static void tick_nohz_switch_to_nohz(void)
411{
412 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
413 ktime_t next;
414
415 if (!tick_nohz_enabled)
416 return;
417
418 local_irq_disable();
419 if (tick_switch_to_oneshot(tick_nohz_handler)) {
420 local_irq_enable();
421 return;
422 }
423
424 ts->nohz_mode = NOHZ_MODE_LOWRES;
425
426 /*
427 * Recycle the hrtimer in ts, so we can share the
428 * hrtimer_forward with the highres code.
429 */
430 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
431 /* Get the next period */
432 next = tick_init_jiffy_update();
433
434 for (;;) {
435 ts->sched_timer.expires = next;
436 if (!tick_program_event(next, 0))
437 break;
438 next = ktime_add(next, tick_period);
439 }
440 local_irq_enable();
441
442 printk(KERN_INFO "Switched to NOHz mode on CPU #%d\n",
443 smp_processor_id());
444}
445
446#else
447
448static inline void tick_nohz_switch_to_nohz(void) { }
449
450#endif /* NO_HZ */
451
452/*
453 * High resolution timer specific code
454 */
455#ifdef CONFIG_HIGH_RES_TIMERS
456/*
457 * We rearm the timer until we get disabled by the idle code
458 * Called with interrupts disabled and timer->base->cpu_base->lock held.
459 */
460static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
461{
462 struct tick_sched *ts =
463 container_of(timer, struct tick_sched, sched_timer);
464 struct hrtimer_cpu_base *base = timer->base->cpu_base;
465 struct pt_regs *regs = get_irq_regs();
466 ktime_t now = ktime_get();
d3ed7824
TG
467 int cpu = smp_processor_id();
468
469#ifdef CONFIG_NO_HZ
470 /*
471 * Check if the do_timer duty was dropped. We don't care about
472 * concurrency: This happens only when the cpu in charge went
473 * into a long sleep. If two cpus happen to assign themself to
474 * this duty, then the jiffies update is still serialized by
475 * xtime_lock.
476 */
477 if (unlikely(tick_do_timer_cpu == -1))
478 tick_do_timer_cpu = cpu;
479#endif
79bf2bb3
TG
480
481 /* Check, if the jiffies need an update */
d3ed7824
TG
482 if (tick_do_timer_cpu == cpu)
483 tick_do_update_jiffies64(now);
79bf2bb3
TG
484
485 /*
486 * Do not call, when we are not in irq context and have
487 * no valid regs pointer
488 */
489 if (regs) {
490 /*
491 * When we are idle and the tick is stopped, we have to touch
492 * the watchdog as we might not schedule for a really long
493 * time. This happens on complete idle SMP systems while
494 * waiting on the login prompt. We also increment the "start of
495 * idle" jiffy stamp so the idle accounting adjustment we do
496 * when we go busy again does not account too much ticks.
497 */
498 if (ts->tick_stopped) {
499 touch_softlockup_watchdog();
500 ts->idle_jiffies++;
501 }
502 /*
503 * update_process_times() might take tasklist_lock, hence
504 * drop the base lock. sched-tick hrtimers are per-CPU and
505 * never accessible by userspace APIs, so this is safe to do.
506 */
507 spin_unlock(&base->lock);
508 update_process_times(user_mode(regs));
509 profile_tick(CPU_PROFILING);
510 spin_lock(&base->lock);
511 }
512
513 /* Do not restart, when we are in the idle loop */
514 if (ts->tick_stopped)
515 return HRTIMER_NORESTART;
516
517 hrtimer_forward(timer, now, tick_period);
518
519 return HRTIMER_RESTART;
520}
521
522/**
523 * tick_setup_sched_timer - setup the tick emulation timer
524 */
525void tick_setup_sched_timer(void)
526{
527 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
528 ktime_t now = ktime_get();
529
530 /*
531 * Emulate tick processing via per-CPU hrtimers:
532 */
533 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
534 ts->sched_timer.function = tick_sched_timer;
535 ts->sched_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
536
537 /* Get the next period */
538 ts->sched_timer.expires = tick_init_jiffy_update();
539
540 for (;;) {
541 hrtimer_forward(&ts->sched_timer, now, tick_period);
542 hrtimer_start(&ts->sched_timer, ts->sched_timer.expires,
543 HRTIMER_MODE_ABS);
544 /* Check, if the timer was already in the past */
545 if (hrtimer_active(&ts->sched_timer))
546 break;
547 now = ktime_get();
548 }
549
550#ifdef CONFIG_NO_HZ
551 if (tick_nohz_enabled)
552 ts->nohz_mode = NOHZ_MODE_HIGHRES;
553#endif
554}
555
556void tick_cancel_sched_timer(int cpu)
557{
558 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
559
560 if (ts->sched_timer.base)
561 hrtimer_cancel(&ts->sched_timer);
562 ts->tick_stopped = 0;
563 ts->nohz_mode = NOHZ_MODE_INACTIVE;
564}
565#endif /* HIGH_RES_TIMERS */
566
567/**
568 * Async notification about clocksource changes
569 */
570void tick_clock_notify(void)
571{
572 int cpu;
573
574 for_each_possible_cpu(cpu)
575 set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
576}
577
578/*
579 * Async notification about clock event changes
580 */
581void tick_oneshot_notify(void)
582{
583 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
584
585 set_bit(0, &ts->check_clocks);
586}
587
588/**
589 * Check, if a change happened, which makes oneshot possible.
590 *
591 * Called cyclic from the hrtimer softirq (driven by the timer
592 * softirq) allow_nohz signals, that we can switch into low-res nohz
593 * mode, because high resolution timers are disabled (either compile
594 * or runtime).
595 */
596int tick_check_oneshot_change(int allow_nohz)
597{
598 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
599
600 if (!test_and_clear_bit(0, &ts->check_clocks))
601 return 0;
602
603 if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
604 return 0;
605
606 if (!timekeeping_is_continuous() || !tick_is_oneshot_available())
607 return 0;
608
609 if (!allow_nohz)
610 return 1;
611
612 tick_nohz_switch_to_nohz();
613 return 0;
614}