tick-sched.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. #include <asm/irq_regs.h>
  24. #include "tick-internal.h"
  25. /*
  26. * Per cpu nohz control structure
  27. */
  28. static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
  29. /*
  30. * The time, when the last jiffy update happened. Protected by xtime_lock.
  31. */
  32. static ktime_t last_jiffies_update;
  33. struct tick_sched *tick_get_tick_sched(int cpu)
  34. {
  35. return &per_cpu(tick_cpu_sched, cpu);
  36. }
  37. /*
  38. * Must be called with interrupts disabled !
  39. */
  40. static void tick_do_update_jiffies64(ktime_t now)
  41. {
  42. unsigned long ticks = 0;
  43. ktime_t delta;
  44. /* Reevalute with xtime_lock held */
  45. write_seqlock(&xtime_lock);
  46. delta = ktime_sub(now, last_jiffies_update);
  47. if (delta.tv64 >= tick_period.tv64) {
  48. delta = ktime_sub(delta, tick_period);
  49. last_jiffies_update = ktime_add(last_jiffies_update,
  50. tick_period);
  51. /* Slow path for long timeouts */
  52. if (unlikely(delta.tv64 >= tick_period.tv64)) {
  53. s64 incr = ktime_to_ns(tick_period);
  54. ticks = ktime_divns(delta, incr);
  55. last_jiffies_update = ktime_add_ns(last_jiffies_update,
  56. incr * ticks);
  57. }
  58. do_timer(++ticks);
  59. }
  60. write_sequnlock(&xtime_lock);
  61. }
  62. /*
  63. * Initialize and return retrieve the jiffies update.
  64. */
  65. static ktime_t tick_init_jiffy_update(void)
  66. {
  67. ktime_t period;
  68. write_seqlock(&xtime_lock);
  69. /* Did we start the jiffies update yet ? */
  70. if (last_jiffies_update.tv64 == 0)
  71. last_jiffies_update = tick_next_period;
  72. period = last_jiffies_update;
  73. write_sequnlock(&xtime_lock);
  74. return period;
  75. }
  76. /*
  77. * NOHZ - aka dynamic tick functionality
  78. */
  79. #ifdef CONFIG_NO_HZ
  80. /*
  81. * NO HZ enabled ?
  82. */
  83. static int tick_nohz_enabled __read_mostly = 1;
  84. /*
  85. * Enable / Disable tickless mode
  86. */
  87. static int __init setup_tick_nohz(char *str)
  88. {
  89. if (!strcmp(str, "off"))
  90. tick_nohz_enabled = 0;
  91. else if (!strcmp(str, "on"))
  92. tick_nohz_enabled = 1;
  93. else
  94. return 0;
  95. return 1;
  96. }
  97. __setup("nohz=", setup_tick_nohz);
  98. /**
  99. * tick_nohz_update_jiffies - update jiffies when idle was interrupted
  100. *
  101. * Called from interrupt entry when the CPU was idle
  102. *
  103. * In case the sched_tick was stopped on this CPU, we have to check if jiffies
  104. * must be updated. Otherwise an interrupt handler could use a stale jiffy
  105. * value. We do this unconditionally on any cpu, as we don't know whether the
  106. * cpu, which has the update task assigned is in a long sleep.
  107. */
  108. void tick_nohz_update_jiffies(void)
  109. {
  110. int cpu = smp_processor_id();
  111. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  112. unsigned long flags;
  113. ktime_t now;
  114. if (!ts->tick_stopped)
  115. return;
  116. cpu_clear(cpu, nohz_cpu_mask);
  117. now = ktime_get();
  118. local_irq_save(flags);
  119. tick_do_update_jiffies64(now);
  120. local_irq_restore(flags);
  121. }
  122. /**
  123. * tick_nohz_stop_sched_tick - stop the idle tick from the idle task
  124. *
  125. * When the next event is more than a tick into the future, stop the idle tick
  126. * Called either from the idle loop or from irq_exit() when an idle period was
  127. * just interrupted by an interrupt which did not cause a reschedule.
  128. */
  129. void tick_nohz_stop_sched_tick(void)
  130. {
  131. unsigned long seq, last_jiffies, next_jiffies, delta_jiffies, flags;
  132. struct tick_sched *ts;
  133. ktime_t last_update, expires, now, delta;
  134. int cpu;
  135. local_irq_save(flags);
  136. cpu = smp_processor_id();
  137. ts = &per_cpu(tick_cpu_sched, cpu);
  138. if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
  139. goto end;
  140. if (need_resched())
  141. goto end;
  142. cpu = smp_processor_id();
  143. if (unlikely(local_softirq_pending())) {
  144. static int ratelimit;
  145. if (ratelimit < 10) {
  146. printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  147. local_softirq_pending());
  148. ratelimit++;
  149. }
  150. }
  151. now = ktime_get();
  152. /*
  153. * When called from irq_exit we need to account the idle sleep time
  154. * correctly.
  155. */
  156. if (ts->tick_stopped) {
  157. delta = ktime_sub(now, ts->idle_entrytime);
  158. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  159. }
  160. ts->idle_entrytime = now;
  161. ts->idle_calls++;
  162. /* Read jiffies and the time when jiffies were updated last */
  163. do {
  164. seq = read_seqbegin(&xtime_lock);
  165. last_update = last_jiffies_update;
  166. last_jiffies = jiffies;
  167. } while (read_seqretry(&xtime_lock, seq));
  168. /* Get the next timer wheel timer */
  169. next_jiffies = get_next_timer_interrupt(last_jiffies);
  170. delta_jiffies = next_jiffies - last_jiffies;
  171. if (rcu_needs_cpu(cpu))
  172. delta_jiffies = 1;
  173. /*
  174. * Do not stop the tick, if we are only one off
  175. * or if the cpu is required for rcu
  176. */
  177. if (!ts->tick_stopped && delta_jiffies == 1)
  178. goto out;
  179. /* Schedule the tick, if we are at least one jiffie off */
  180. if ((long)delta_jiffies >= 1) {
  181. if (delta_jiffies > 1)
  182. cpu_set(cpu, nohz_cpu_mask);
  183. /*
  184. * nohz_stop_sched_tick can be called several times before
  185. * the nohz_restart_sched_tick is called. This happens when
  186. * interrupts arrive which do not cause a reschedule. In the
  187. * first call we save the current tick time, so we can restart
  188. * the scheduler tick in nohz_restart_sched_tick.
  189. */
  190. if (!ts->tick_stopped) {
  191. if (select_nohz_load_balancer(1)) {
  192. /*
  193. * sched tick not stopped!
  194. */
  195. cpu_clear(cpu, nohz_cpu_mask);
  196. goto out;
  197. }
  198. ts->idle_tick = ts->sched_timer.expires;
  199. ts->tick_stopped = 1;
  200. ts->idle_jiffies = last_jiffies;
  201. }
  202. /*
  203. * If this cpu is the one which updates jiffies, then
  204. * give up the assignment and let it be taken by the
  205. * cpu which runs the tick timer next, which might be
  206. * this cpu as well. If we don't drop this here the
  207. * jiffies might be stale and do_timer() never
  208. * invoked.
  209. */
  210. if (cpu == tick_do_timer_cpu)
  211. tick_do_timer_cpu = -1;
  212. /*
  213. * calculate the expiry time for the next timer wheel
  214. * timer
  215. */
  216. expires = ktime_add_ns(last_update, tick_period.tv64 *
  217. delta_jiffies);
  218. ts->idle_expires = expires;
  219. ts->idle_sleeps++;
  220. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  221. hrtimer_start(&ts->sched_timer, expires,
  222. HRTIMER_MODE_ABS);
  223. /* Check, if the timer was already in the past */
  224. if (hrtimer_active(&ts->sched_timer))
  225. goto out;
  226. } else if(!tick_program_event(expires, 0))
  227. goto out;
  228. /*
  229. * We are past the event already. So we crossed a
  230. * jiffie boundary. Update jiffies and raise the
  231. * softirq.
  232. */
  233. tick_do_update_jiffies64(ktime_get());
  234. cpu_clear(cpu, nohz_cpu_mask);
  235. }
  236. raise_softirq_irqoff(TIMER_SOFTIRQ);
  237. out:
  238. ts->next_jiffies = next_jiffies;
  239. ts->last_jiffies = last_jiffies;
  240. end:
  241. local_irq_restore(flags);
  242. }
  243. /**
  244. * nohz_restart_sched_tick - restart the idle tick from the idle task
  245. *
  246. * Restart the idle tick when the CPU is woken up from idle
  247. */
  248. void tick_nohz_restart_sched_tick(void)
  249. {
  250. int cpu = smp_processor_id();
  251. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  252. unsigned long ticks;
  253. ktime_t now, delta;
  254. if (!ts->tick_stopped)
  255. return;
  256. /* Update jiffies first */
  257. now = ktime_get();
  258. local_irq_disable();
  259. select_nohz_load_balancer(0);
  260. tick_do_update_jiffies64(now);
  261. cpu_clear(cpu, nohz_cpu_mask);
  262. /* Account the idle time */
  263. delta = ktime_sub(now, ts->idle_entrytime);
  264. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  265. /*
  266. * We stopped the tick in idle. Update process times would miss the
  267. * time we slept as update_process_times does only a 1 tick
  268. * accounting. Enforce that this is accounted to idle !
  269. */
  270. ticks = jiffies - ts->idle_jiffies;
  271. /*
  272. * We might be one off. Do not randomly account a huge number of ticks!
  273. */
  274. if (ticks && ticks < LONG_MAX) {
  275. add_preempt_count(HARDIRQ_OFFSET);
  276. account_system_time(current, HARDIRQ_OFFSET,
  277. jiffies_to_cputime(ticks));
  278. sub_preempt_count(HARDIRQ_OFFSET);
  279. }
  280. /*
  281. * Cancel the scheduled timer and restore the tick
  282. */
  283. ts->tick_stopped = 0;
  284. hrtimer_cancel(&ts->sched_timer);
  285. ts->sched_timer.expires = ts->idle_tick;
  286. while (1) {
  287. /* Forward the time to expire in the future */
  288. hrtimer_forward(&ts->sched_timer, now, tick_period);
  289. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  290. hrtimer_start(&ts->sched_timer,
  291. ts->sched_timer.expires,
  292. HRTIMER_MODE_ABS);
  293. /* Check, if the timer was already in the past */
  294. if (hrtimer_active(&ts->sched_timer))
  295. break;
  296. } else {
  297. if (!tick_program_event(ts->sched_timer.expires, 0))
  298. break;
  299. }
  300. /* Update jiffies and reread time */
  301. tick_do_update_jiffies64(now);
  302. now = ktime_get();
  303. }
  304. local_irq_enable();
  305. }
  306. static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
  307. {
  308. hrtimer_forward(&ts->sched_timer, now, tick_period);
  309. return tick_program_event(ts->sched_timer.expires, 0);
  310. }
  311. /*
  312. * The nohz low res interrupt handler
  313. */
  314. static void tick_nohz_handler(struct clock_event_device *dev)
  315. {
  316. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  317. struct pt_regs *regs = get_irq_regs();
  318. int cpu = smp_processor_id();
  319. ktime_t now = ktime_get();
  320. dev->next_event.tv64 = KTIME_MAX;
  321. /*
  322. * Check if the do_timer duty was dropped. We don't care about
  323. * concurrency: This happens only when the cpu in charge went
  324. * into a long sleep. If two cpus happen to assign themself to
  325. * this duty, then the jiffies update is still serialized by
  326. * xtime_lock.
  327. */
  328. if (unlikely(tick_do_timer_cpu == -1))
  329. tick_do_timer_cpu = cpu;
  330. /* Check, if the jiffies need an update */
  331. if (tick_do_timer_cpu == cpu)
  332. tick_do_update_jiffies64(now);
  333. /*
  334. * When we are idle and the tick is stopped, we have to touch
  335. * the watchdog as we might not schedule for a really long
  336. * time. This happens on complete idle SMP systems while
  337. * waiting on the login prompt. We also increment the "start
  338. * of idle" jiffy stamp so the idle accounting adjustment we
  339. * do when we go busy again does not account too much ticks.
  340. */
  341. if (ts->tick_stopped) {
  342. touch_softlockup_watchdog();
  343. ts->idle_jiffies++;
  344. }
  345. update_process_times(user_mode(regs));
  346. profile_tick(CPU_PROFILING);
  347. /* Do not restart, when we are in the idle loop */
  348. if (ts->tick_stopped)
  349. return;
  350. while (tick_nohz_reprogram(ts, now)) {
  351. now = ktime_get();
  352. tick_do_update_jiffies64(now);
  353. }
  354. }
  355. /**
  356. * tick_nohz_switch_to_nohz - switch to nohz mode
  357. */
  358. static void tick_nohz_switch_to_nohz(void)
  359. {
  360. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  361. ktime_t next;
  362. if (!tick_nohz_enabled)
  363. return;
  364. local_irq_disable();
  365. if (tick_switch_to_oneshot(tick_nohz_handler)) {
  366. local_irq_enable();
  367. return;
  368. }
  369. ts->nohz_mode = NOHZ_MODE_LOWRES;
  370. /*
  371. * Recycle the hrtimer in ts, so we can share the
  372. * hrtimer_forward with the highres code.
  373. */
  374. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  375. /* Get the next period */
  376. next = tick_init_jiffy_update();
  377. for (;;) {
  378. ts->sched_timer.expires = next;
  379. if (!tick_program_event(next, 0))
  380. break;
  381. next = ktime_add(next, tick_period);
  382. }
  383. local_irq_enable();
  384. printk(KERN_INFO "Switched to NOHz mode on CPU #%d\n",
  385. smp_processor_id());
  386. }
  387. #else
  388. static inline void tick_nohz_switch_to_nohz(void) { }
  389. #endif /* NO_HZ */
  390. /*
  391. * High resolution timer specific code
  392. */
  393. #ifdef CONFIG_HIGH_RES_TIMERS
  394. /*
  395. * We rearm the timer until we get disabled by the idle code
  396. * Called with interrupts disabled and timer->base->cpu_base->lock held.
  397. */
  398. static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
  399. {
  400. struct tick_sched *ts =
  401. container_of(timer, struct tick_sched, sched_timer);
  402. struct hrtimer_cpu_base *base = timer->base->cpu_base;
  403. struct pt_regs *regs = get_irq_regs();
  404. ktime_t now = ktime_get();
  405. int cpu = smp_processor_id();
  406. #ifdef CONFIG_NO_HZ
  407. /*
  408. * Check if the do_timer duty was dropped. We don't care about
  409. * concurrency: This happens only when the cpu in charge went
  410. * into a long sleep. If two cpus happen to assign themself to
  411. * this duty, then the jiffies update is still serialized by
  412. * xtime_lock.
  413. */
  414. if (unlikely(tick_do_timer_cpu == -1))
  415. tick_do_timer_cpu = cpu;
  416. #endif
  417. /* Check, if the jiffies need an update */
  418. if (tick_do_timer_cpu == cpu)
  419. tick_do_update_jiffies64(now);
  420. /*
  421. * Do not call, when we are not in irq context and have
  422. * no valid regs pointer
  423. */
  424. if (regs) {
  425. /*
  426. * When we are idle and the tick is stopped, we have to touch
  427. * the watchdog as we might not schedule for a really long
  428. * time. This happens on complete idle SMP systems while
  429. * waiting on the login prompt. We also increment the "start of
  430. * idle" jiffy stamp so the idle accounting adjustment we do
  431. * when we go busy again does not account too much ticks.
  432. */
  433. if (ts->tick_stopped) {
  434. touch_softlockup_watchdog();
  435. ts->idle_jiffies++;
  436. }
  437. /*
  438. * update_process_times() might take tasklist_lock, hence
  439. * drop the base lock. sched-tick hrtimers are per-CPU and
  440. * never accessible by userspace APIs, so this is safe to do.
  441. */
  442. spin_unlock(&base->lock);
  443. update_process_times(user_mode(regs));
  444. profile_tick(CPU_PROFILING);
  445. spin_lock(&base->lock);
  446. }
  447. /* Do not restart, when we are in the idle loop */
  448. if (ts->tick_stopped)
  449. return HRTIMER_NORESTART;
  450. hrtimer_forward(timer, now, tick_period);
  451. return HRTIMER_RESTART;
  452. }
  453. /**
  454. * tick_setup_sched_timer - setup the tick emulation timer
  455. */
  456. void tick_setup_sched_timer(void)
  457. {
  458. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  459. ktime_t now = ktime_get();
  460. /*
  461. * Emulate tick processing via per-CPU hrtimers:
  462. */
  463. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  464. ts->sched_timer.function = tick_sched_timer;
  465. ts->sched_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
  466. /* Get the next period */
  467. ts->sched_timer.expires = tick_init_jiffy_update();
  468. for (;;) {
  469. hrtimer_forward(&ts->sched_timer, now, tick_period);
  470. hrtimer_start(&ts->sched_timer, ts->sched_timer.expires,
  471. HRTIMER_MODE_ABS);
  472. /* Check, if the timer was already in the past */
  473. if (hrtimer_active(&ts->sched_timer))
  474. break;
  475. now = ktime_get();
  476. }
  477. #ifdef CONFIG_NO_HZ
  478. if (tick_nohz_enabled)
  479. ts->nohz_mode = NOHZ_MODE_HIGHRES;
  480. #endif
  481. }
  482. void tick_cancel_sched_timer(int cpu)
  483. {
  484. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  485. if (ts->sched_timer.base)
  486. hrtimer_cancel(&ts->sched_timer);
  487. ts->tick_stopped = 0;
  488. ts->nohz_mode = NOHZ_MODE_INACTIVE;
  489. }
  490. #endif /* HIGH_RES_TIMERS */
  491. /**
  492. * Async notification about clocksource changes
  493. */
  494. void tick_clock_notify(void)
  495. {
  496. int cpu;
  497. for_each_possible_cpu(cpu)
  498. set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
  499. }
  500. /*
  501. * Async notification about clock event changes
  502. */
  503. void tick_oneshot_notify(void)
  504. {
  505. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  506. set_bit(0, &ts->check_clocks);
  507. }
  508. /**
  509. * Check, if a change happened, which makes oneshot possible.
  510. *
  511. * Called cyclic from the hrtimer softirq (driven by the timer
  512. * softirq) allow_nohz signals, that we can switch into low-res nohz
  513. * mode, because high resolution timers are disabled (either compile
  514. * or runtime).
  515. */
  516. int tick_check_oneshot_change(int allow_nohz)
  517. {
  518. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  519. if (!test_and_clear_bit(0, &ts->check_clocks))
  520. return 0;
  521. if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
  522. return 0;
  523. if (!timekeeping_is_continuous() || !tick_is_oneshot_available())
  524. return 0;
  525. if (!allow_nohz)
  526. return 1;
  527. tick_nohz_switch_to_nohz();
  528. return 0;
  529. }