tick-sched.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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. * Distribute under GPLv2.
  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/module.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. /*
  45. * Do a quick check without holding xtime_lock:
  46. */
  47. delta = ktime_sub(now, last_jiffies_update);
  48. if (delta.tv64 < tick_period.tv64)
  49. return;
  50. /* Reevalute with xtime_lock held */
  51. write_seqlock(&xtime_lock);
  52. delta = ktime_sub(now, last_jiffies_update);
  53. if (delta.tv64 >= tick_period.tv64) {
  54. delta = ktime_sub(delta, tick_period);
  55. last_jiffies_update = ktime_add(last_jiffies_update,
  56. tick_period);
  57. /* Slow path for long timeouts */
  58. if (unlikely(delta.tv64 >= tick_period.tv64)) {
  59. s64 incr = ktime_to_ns(tick_period);
  60. ticks = ktime_divns(delta, incr);
  61. last_jiffies_update = ktime_add_ns(last_jiffies_update,
  62. incr * ticks);
  63. }
  64. do_timer(++ticks);
  65. /* Keep the tick_next_period variable up to date */
  66. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  67. }
  68. write_sequnlock(&xtime_lock);
  69. }
  70. /*
  71. * Initialize and return retrieve the jiffies update.
  72. */
  73. static ktime_t tick_init_jiffy_update(void)
  74. {
  75. ktime_t period;
  76. write_seqlock(&xtime_lock);
  77. /* Did we start the jiffies update yet ? */
  78. if (last_jiffies_update.tv64 == 0)
  79. last_jiffies_update = tick_next_period;
  80. period = last_jiffies_update;
  81. write_sequnlock(&xtime_lock);
  82. return period;
  83. }
  84. /*
  85. * NOHZ - aka dynamic tick functionality
  86. */
  87. #ifdef CONFIG_NO_HZ
  88. /*
  89. * NO HZ enabled ?
  90. */
  91. static int tick_nohz_enabled __read_mostly = 1;
  92. /*
  93. * Enable / Disable tickless mode
  94. */
  95. static int __init setup_tick_nohz(char *str)
  96. {
  97. if (!strcmp(str, "off"))
  98. tick_nohz_enabled = 0;
  99. else if (!strcmp(str, "on"))
  100. tick_nohz_enabled = 1;
  101. else
  102. return 0;
  103. return 1;
  104. }
  105. __setup("nohz=", setup_tick_nohz);
  106. /**
  107. * tick_nohz_update_jiffies - update jiffies when idle was interrupted
  108. *
  109. * Called from interrupt entry when the CPU was idle
  110. *
  111. * In case the sched_tick was stopped on this CPU, we have to check if jiffies
  112. * must be updated. Otherwise an interrupt handler could use a stale jiffy
  113. * value. We do this unconditionally on any cpu, as we don't know whether the
  114. * cpu, which has the update task assigned is in a long sleep.
  115. */
  116. static void tick_nohz_update_jiffies(ktime_t now)
  117. {
  118. int cpu = smp_processor_id();
  119. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  120. unsigned long flags;
  121. cpumask_clear_cpu(cpu, nohz_cpu_mask);
  122. ts->idle_waketime = now;
  123. local_irq_save(flags);
  124. tick_do_update_jiffies64(now);
  125. local_irq_restore(flags);
  126. touch_softlockup_watchdog();
  127. }
  128. /*
  129. * Updates the per cpu time idle statistics counters
  130. */
  131. static void
  132. update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
  133. {
  134. ktime_t delta;
  135. if (ts->idle_active) {
  136. delta = ktime_sub(now, ts->idle_entrytime);
  137. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  138. if (nr_iowait_cpu(cpu) > 0)
  139. ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
  140. ts->idle_entrytime = now;
  141. }
  142. if (last_update_time)
  143. *last_update_time = ktime_to_us(now);
  144. }
  145. static void tick_nohz_stop_idle(int cpu, ktime_t now)
  146. {
  147. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  148. update_ts_time_stats(cpu, ts, now, NULL);
  149. ts->idle_active = 0;
  150. sched_clock_idle_wakeup_event(0);
  151. }
  152. static ktime_t tick_nohz_start_idle(int cpu, struct tick_sched *ts)
  153. {
  154. ktime_t now;
  155. now = ktime_get();
  156. update_ts_time_stats(cpu, ts, now, NULL);
  157. ts->idle_entrytime = now;
  158. ts->idle_active = 1;
  159. sched_clock_idle_sleep_event();
  160. return now;
  161. }
  162. /**
  163. * get_cpu_idle_time_us - get the total idle time of a cpu
  164. * @cpu: CPU number to query
  165. * @last_update_time: variable to store update time in
  166. *
  167. * Return the cummulative idle time (since boot) for a given
  168. * CPU, in microseconds. The idle time returned includes
  169. * the iowait time (unlike what "top" and co report).
  170. *
  171. * This time is measured via accounting rather than sampling,
  172. * and is as accurate as ktime_get() is.
  173. *
  174. * This function returns -1 if NOHZ is not enabled.
  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. if (!tick_nohz_enabled)
  180. return -1;
  181. update_ts_time_stats(cpu, ts, ktime_get(), last_update_time);
  182. return ktime_to_us(ts->idle_sleeptime);
  183. }
  184. EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
  185. /*
  186. * get_cpu_iowait_time_us - get the total iowait time of a cpu
  187. * @cpu: CPU number to query
  188. * @last_update_time: variable to store update time in
  189. *
  190. * Return the cummulative iowait time (since boot) for a given
  191. * CPU, in microseconds.
  192. *
  193. * This time is measured via accounting rather than sampling,
  194. * and is as accurate as ktime_get() is.
  195. *
  196. * This function returns -1 if NOHZ is not enabled.
  197. */
  198. u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
  199. {
  200. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  201. if (!tick_nohz_enabled)
  202. return -1;
  203. update_ts_time_stats(cpu, ts, ktime_get(), last_update_time);
  204. return ktime_to_us(ts->iowait_sleeptime);
  205. }
  206. EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
  207. /**
  208. * tick_nohz_stop_sched_tick - stop the idle tick from the idle task
  209. *
  210. * When the next event is more than a tick into the future, stop the idle tick
  211. * Called either from the idle loop or from irq_exit() when an idle period was
  212. * just interrupted by an interrupt which did not cause a reschedule.
  213. */
  214. void tick_nohz_stop_sched_tick(int inidle)
  215. {
  216. unsigned long seq, last_jiffies, next_jiffies, delta_jiffies, flags;
  217. struct tick_sched *ts;
  218. ktime_t last_update, expires, now;
  219. struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
  220. u64 time_delta;
  221. int cpu;
  222. local_irq_save(flags);
  223. cpu = smp_processor_id();
  224. ts = &per_cpu(tick_cpu_sched, cpu);
  225. /*
  226. * Call to tick_nohz_start_idle stops the last_update_time from being
  227. * updated. Thus, it must not be called in the event we are called from
  228. * irq_exit() with the prior state different than idle.
  229. */
  230. if (!inidle && !ts->inidle)
  231. goto end;
  232. /*
  233. * Set ts->inidle unconditionally. Even if the system did not
  234. * switch to NOHZ mode the cpu frequency governers rely on the
  235. * update of the idle time accounting in tick_nohz_start_idle().
  236. */
  237. ts->inidle = 1;
  238. now = tick_nohz_start_idle(cpu, ts);
  239. /*
  240. * If this cpu is offline and it is the one which updates
  241. * jiffies, then give up the assignment and let it be taken by
  242. * the cpu which runs the tick timer next. If we don't drop
  243. * this here the jiffies might be stale and do_timer() never
  244. * invoked.
  245. */
  246. if (unlikely(!cpu_online(cpu))) {
  247. if (cpu == tick_do_timer_cpu)
  248. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  249. }
  250. if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
  251. goto end;
  252. if (need_resched())
  253. goto end;
  254. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  255. static int ratelimit;
  256. if (ratelimit < 10) {
  257. printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  258. (unsigned int) local_softirq_pending());
  259. ratelimit++;
  260. }
  261. goto end;
  262. }
  263. ts->idle_calls++;
  264. /* Read jiffies and the time when jiffies were updated last */
  265. do {
  266. seq = read_seqbegin(&xtime_lock);
  267. last_update = last_jiffies_update;
  268. last_jiffies = jiffies;
  269. time_delta = timekeeping_max_deferment();
  270. } while (read_seqretry(&xtime_lock, seq));
  271. if (rcu_needs_cpu(cpu) || printk_needs_cpu(cpu) ||
  272. arch_needs_cpu(cpu)) {
  273. next_jiffies = last_jiffies + 1;
  274. delta_jiffies = 1;
  275. } else {
  276. /* Get the next timer wheel timer */
  277. next_jiffies = get_next_timer_interrupt(last_jiffies);
  278. delta_jiffies = next_jiffies - last_jiffies;
  279. }
  280. /*
  281. * Do not stop the tick, if we are only one off
  282. * or if the cpu is required for rcu
  283. */
  284. if (!ts->tick_stopped && delta_jiffies == 1)
  285. goto out;
  286. /* Schedule the tick, if we are at least one jiffie off */
  287. if ((long)delta_jiffies >= 1) {
  288. /*
  289. * If this cpu is the one which updates jiffies, then
  290. * give up the assignment and let it be taken by the
  291. * cpu which runs the tick timer next, which might be
  292. * this cpu as well. If we don't drop this here the
  293. * jiffies might be stale and do_timer() never
  294. * invoked. Keep track of the fact that it was the one
  295. * which had the do_timer() duty last. If this cpu is
  296. * the one which had the do_timer() duty last, we
  297. * limit the sleep time to the timekeeping
  298. * max_deferement value which we retrieved
  299. * above. Otherwise we can sleep as long as we want.
  300. */
  301. if (cpu == tick_do_timer_cpu) {
  302. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  303. ts->do_timer_last = 1;
  304. } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
  305. time_delta = KTIME_MAX;
  306. ts->do_timer_last = 0;
  307. } else if (!ts->do_timer_last) {
  308. time_delta = KTIME_MAX;
  309. }
  310. /*
  311. * calculate the expiry time for the next timer wheel
  312. * timer. delta_jiffies >= NEXT_TIMER_MAX_DELTA signals
  313. * that there is no timer pending or at least extremely
  314. * far into the future (12 days for HZ=1000). In this
  315. * case we set the expiry to the end of time.
  316. */
  317. if (likely(delta_jiffies < NEXT_TIMER_MAX_DELTA)) {
  318. /*
  319. * Calculate the time delta for the next timer event.
  320. * If the time delta exceeds the maximum time delta
  321. * permitted by the current clocksource then adjust
  322. * the time delta accordingly to ensure the
  323. * clocksource does not wrap.
  324. */
  325. time_delta = min_t(u64, time_delta,
  326. tick_period.tv64 * delta_jiffies);
  327. }
  328. if (time_delta < KTIME_MAX)
  329. expires = ktime_add_ns(last_update, time_delta);
  330. else
  331. expires.tv64 = KTIME_MAX;
  332. if (delta_jiffies > 1)
  333. cpumask_set_cpu(cpu, nohz_cpu_mask);
  334. /* Skip reprogram of event if its not changed */
  335. if (ts->tick_stopped && ktime_equal(expires, dev->next_event))
  336. goto out;
  337. /*
  338. * nohz_stop_sched_tick can be called several times before
  339. * the nohz_restart_sched_tick is called. This happens when
  340. * interrupts arrive which do not cause a reschedule. In the
  341. * first call we save the current tick time, so we can restart
  342. * the scheduler tick in nohz_restart_sched_tick.
  343. */
  344. if (!ts->tick_stopped) {
  345. select_nohz_load_balancer(1);
  346. ts->idle_tick = hrtimer_get_expires(&ts->sched_timer);
  347. ts->tick_stopped = 1;
  348. ts->idle_jiffies = last_jiffies;
  349. rcu_enter_nohz();
  350. }
  351. ts->idle_sleeps++;
  352. /* Mark expires */
  353. ts->idle_expires = expires;
  354. /*
  355. * If the expiration time == KTIME_MAX, then
  356. * in this case we simply stop the tick timer.
  357. */
  358. if (unlikely(expires.tv64 == KTIME_MAX)) {
  359. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  360. hrtimer_cancel(&ts->sched_timer);
  361. goto out;
  362. }
  363. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  364. hrtimer_start(&ts->sched_timer, expires,
  365. HRTIMER_MODE_ABS_PINNED);
  366. /* Check, if the timer was already in the past */
  367. if (hrtimer_active(&ts->sched_timer))
  368. goto out;
  369. } else if (!tick_program_event(expires, 0))
  370. goto out;
  371. /*
  372. * We are past the event already. So we crossed a
  373. * jiffie boundary. Update jiffies and raise the
  374. * softirq.
  375. */
  376. tick_do_update_jiffies64(ktime_get());
  377. cpumask_clear_cpu(cpu, nohz_cpu_mask);
  378. }
  379. raise_softirq_irqoff(TIMER_SOFTIRQ);
  380. out:
  381. ts->next_jiffies = next_jiffies;
  382. ts->last_jiffies = last_jiffies;
  383. ts->sleep_length = ktime_sub(dev->next_event, now);
  384. end:
  385. local_irq_restore(flags);
  386. }
  387. /**
  388. * tick_nohz_get_sleep_length - return the length of the current sleep
  389. *
  390. * Called from power state control code with interrupts disabled
  391. */
  392. ktime_t tick_nohz_get_sleep_length(void)
  393. {
  394. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  395. return ts->sleep_length;
  396. }
  397. static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
  398. {
  399. hrtimer_cancel(&ts->sched_timer);
  400. hrtimer_set_expires(&ts->sched_timer, ts->idle_tick);
  401. while (1) {
  402. /* Forward the time to expire in the future */
  403. hrtimer_forward(&ts->sched_timer, now, tick_period);
  404. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  405. hrtimer_start_expires(&ts->sched_timer,
  406. HRTIMER_MODE_ABS_PINNED);
  407. /* Check, if the timer was already in the past */
  408. if (hrtimer_active(&ts->sched_timer))
  409. break;
  410. } else {
  411. if (!tick_program_event(
  412. hrtimer_get_expires(&ts->sched_timer), 0))
  413. break;
  414. }
  415. /* Reread time and update jiffies */
  416. now = ktime_get();
  417. tick_do_update_jiffies64(now);
  418. }
  419. }
  420. /**
  421. * tick_nohz_restart_sched_tick - restart the idle tick from the idle task
  422. *
  423. * Restart the idle tick when the CPU is woken up from idle
  424. */
  425. void tick_nohz_restart_sched_tick(void)
  426. {
  427. int cpu = smp_processor_id();
  428. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  429. #ifndef CONFIG_VIRT_CPU_ACCOUNTING
  430. unsigned long ticks;
  431. #endif
  432. ktime_t now;
  433. local_irq_disable();
  434. if (ts->idle_active || (ts->inidle && ts->tick_stopped))
  435. now = ktime_get();
  436. if (ts->idle_active)
  437. tick_nohz_stop_idle(cpu, now);
  438. if (!ts->inidle || !ts->tick_stopped) {
  439. ts->inidle = 0;
  440. local_irq_enable();
  441. return;
  442. }
  443. ts->inidle = 0;
  444. rcu_exit_nohz();
  445. /* Update jiffies first */
  446. select_nohz_load_balancer(0);
  447. tick_do_update_jiffies64(now);
  448. cpumask_clear_cpu(cpu, nohz_cpu_mask);
  449. #ifndef CONFIG_VIRT_CPU_ACCOUNTING
  450. /*
  451. * We stopped the tick in idle. Update process times would miss the
  452. * time we slept as update_process_times does only a 1 tick
  453. * accounting. Enforce that this is accounted to idle !
  454. */
  455. ticks = jiffies - ts->idle_jiffies;
  456. /*
  457. * We might be one off. Do not randomly account a huge number of ticks!
  458. */
  459. if (ticks && ticks < LONG_MAX)
  460. account_idle_ticks(ticks);
  461. #endif
  462. touch_softlockup_watchdog();
  463. /*
  464. * Cancel the scheduled timer and restore the tick
  465. */
  466. ts->tick_stopped = 0;
  467. ts->idle_exittime = now;
  468. tick_nohz_restart(ts, now);
  469. local_irq_enable();
  470. }
  471. static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
  472. {
  473. hrtimer_forward(&ts->sched_timer, now, tick_period);
  474. return tick_program_event(hrtimer_get_expires(&ts->sched_timer), 0);
  475. }
  476. /*
  477. * The nohz low res interrupt handler
  478. */
  479. static void tick_nohz_handler(struct clock_event_device *dev)
  480. {
  481. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  482. struct pt_regs *regs = get_irq_regs();
  483. int cpu = smp_processor_id();
  484. ktime_t now = ktime_get();
  485. dev->next_event.tv64 = KTIME_MAX;
  486. /*
  487. * Check if the do_timer duty was dropped. We don't care about
  488. * concurrency: This happens only when the cpu in charge went
  489. * into a long sleep. If two cpus happen to assign themself to
  490. * this duty, then the jiffies update is still serialized by
  491. * xtime_lock.
  492. */
  493. if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
  494. tick_do_timer_cpu = cpu;
  495. /* Check, if the jiffies need an update */
  496. if (tick_do_timer_cpu == cpu)
  497. tick_do_update_jiffies64(now);
  498. /*
  499. * When we are idle and the tick is stopped, we have to touch
  500. * the watchdog as we might not schedule for a really long
  501. * time. This happens on complete idle SMP systems while
  502. * waiting on the login prompt. We also increment the "start
  503. * of idle" jiffy stamp so the idle accounting adjustment we
  504. * do when we go busy again does not account too much ticks.
  505. */
  506. if (ts->tick_stopped) {
  507. touch_softlockup_watchdog();
  508. ts->idle_jiffies++;
  509. }
  510. update_process_times(user_mode(regs));
  511. profile_tick(CPU_PROFILING);
  512. while (tick_nohz_reprogram(ts, now)) {
  513. now = ktime_get();
  514. tick_do_update_jiffies64(now);
  515. }
  516. }
  517. /**
  518. * tick_nohz_switch_to_nohz - switch to nohz mode
  519. */
  520. static void tick_nohz_switch_to_nohz(void)
  521. {
  522. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  523. ktime_t next;
  524. if (!tick_nohz_enabled)
  525. return;
  526. local_irq_disable();
  527. if (tick_switch_to_oneshot(tick_nohz_handler)) {
  528. local_irq_enable();
  529. return;
  530. }
  531. ts->nohz_mode = NOHZ_MODE_LOWRES;
  532. /*
  533. * Recycle the hrtimer in ts, so we can share the
  534. * hrtimer_forward with the highres code.
  535. */
  536. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  537. /* Get the next period */
  538. next = tick_init_jiffy_update();
  539. for (;;) {
  540. hrtimer_set_expires(&ts->sched_timer, next);
  541. if (!tick_program_event(next, 0))
  542. break;
  543. next = ktime_add(next, tick_period);
  544. }
  545. local_irq_enable();
  546. printk(KERN_INFO "Switched to NOHz mode on CPU #%d\n", smp_processor_id());
  547. }
  548. /*
  549. * When NOHZ is enabled and the tick is stopped, we need to kick the
  550. * tick timer from irq_enter() so that the jiffies update is kept
  551. * alive during long running softirqs. That's ugly as hell, but
  552. * correctness is key even if we need to fix the offending softirq in
  553. * the first place.
  554. *
  555. * Note, this is different to tick_nohz_restart. We just kick the
  556. * timer and do not touch the other magic bits which need to be done
  557. * when idle is left.
  558. */
  559. static void tick_nohz_kick_tick(int cpu, ktime_t now)
  560. {
  561. #if 0
  562. /* Switch back to 2.6.27 behaviour */
  563. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  564. ktime_t delta;
  565. /*
  566. * Do not touch the tick device, when the next expiry is either
  567. * already reached or less/equal than the tick period.
  568. */
  569. delta = ktime_sub(hrtimer_get_expires(&ts->sched_timer), now);
  570. if (delta.tv64 <= tick_period.tv64)
  571. return;
  572. tick_nohz_restart(ts, now);
  573. #endif
  574. }
  575. static inline void tick_check_nohz(int cpu)
  576. {
  577. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  578. ktime_t now;
  579. if (!ts->idle_active && !ts->tick_stopped)
  580. return;
  581. now = ktime_get();
  582. if (ts->idle_active)
  583. tick_nohz_stop_idle(cpu, now);
  584. if (ts->tick_stopped) {
  585. tick_nohz_update_jiffies(now);
  586. tick_nohz_kick_tick(cpu, now);
  587. }
  588. }
  589. #else
  590. static inline void tick_nohz_switch_to_nohz(void) { }
  591. static inline void tick_check_nohz(int cpu) { }
  592. #endif /* NO_HZ */
  593. /*
  594. * Called from irq_enter to notify about the possible interruption of idle()
  595. */
  596. void tick_check_idle(int cpu)
  597. {
  598. tick_check_oneshot_broadcast(cpu);
  599. tick_check_nohz(cpu);
  600. }
  601. /*
  602. * High resolution timer specific code
  603. */
  604. #ifdef CONFIG_HIGH_RES_TIMERS
  605. /*
  606. * We rearm the timer until we get disabled by the idle code.
  607. * Called with interrupts disabled and timer->base->cpu_base->lock held.
  608. */
  609. static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
  610. {
  611. struct tick_sched *ts =
  612. container_of(timer, struct tick_sched, sched_timer);
  613. struct pt_regs *regs = get_irq_regs();
  614. ktime_t now = ktime_get();
  615. int cpu = smp_processor_id();
  616. #ifdef CONFIG_NO_HZ
  617. /*
  618. * Check if the do_timer duty was dropped. We don't care about
  619. * concurrency: This happens only when the cpu in charge went
  620. * into a long sleep. If two cpus happen to assign themself to
  621. * this duty, then the jiffies update is still serialized by
  622. * xtime_lock.
  623. */
  624. if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
  625. tick_do_timer_cpu = cpu;
  626. #endif
  627. /* Check, if the jiffies need an update */
  628. if (tick_do_timer_cpu == cpu)
  629. tick_do_update_jiffies64(now);
  630. /*
  631. * Do not call, when we are not in irq context and have
  632. * no valid regs pointer
  633. */
  634. if (regs) {
  635. /*
  636. * When we are idle and the tick is stopped, we have to touch
  637. * the watchdog as we might not schedule for a really long
  638. * time. This happens on complete idle SMP systems while
  639. * waiting on the login prompt. We also increment the "start of
  640. * idle" jiffy stamp so the idle accounting adjustment we do
  641. * when we go busy again does not account too much ticks.
  642. */
  643. if (ts->tick_stopped) {
  644. touch_softlockup_watchdog();
  645. ts->idle_jiffies++;
  646. }
  647. update_process_times(user_mode(regs));
  648. profile_tick(CPU_PROFILING);
  649. }
  650. hrtimer_forward(timer, now, tick_period);
  651. return HRTIMER_RESTART;
  652. }
  653. /**
  654. * tick_setup_sched_timer - setup the tick emulation timer
  655. */
  656. void tick_setup_sched_timer(void)
  657. {
  658. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  659. ktime_t now = ktime_get();
  660. /*
  661. * Emulate tick processing via per-CPU hrtimers:
  662. */
  663. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  664. ts->sched_timer.function = tick_sched_timer;
  665. /* Get the next period (per cpu) */
  666. hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
  667. for (;;) {
  668. hrtimer_forward(&ts->sched_timer, now, tick_period);
  669. hrtimer_start_expires(&ts->sched_timer,
  670. HRTIMER_MODE_ABS_PINNED);
  671. /* Check, if the timer was already in the past */
  672. if (hrtimer_active(&ts->sched_timer))
  673. break;
  674. now = ktime_get();
  675. }
  676. #ifdef CONFIG_NO_HZ
  677. if (tick_nohz_enabled) {
  678. ts->nohz_mode = NOHZ_MODE_HIGHRES;
  679. printk(KERN_INFO "Switched to NOHz mode on CPU #%d\n", smp_processor_id());
  680. }
  681. #endif
  682. }
  683. #endif /* HIGH_RES_TIMERS */
  684. #if defined CONFIG_NO_HZ || defined CONFIG_HIGH_RES_TIMERS
  685. void tick_cancel_sched_timer(int cpu)
  686. {
  687. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  688. # ifdef CONFIG_HIGH_RES_TIMERS
  689. if (ts->sched_timer.base)
  690. hrtimer_cancel(&ts->sched_timer);
  691. # endif
  692. ts->nohz_mode = NOHZ_MODE_INACTIVE;
  693. }
  694. #endif
  695. /**
  696. * Async notification about clocksource changes
  697. */
  698. void tick_clock_notify(void)
  699. {
  700. int cpu;
  701. for_each_possible_cpu(cpu)
  702. set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
  703. }
  704. /*
  705. * Async notification about clock event changes
  706. */
  707. void tick_oneshot_notify(void)
  708. {
  709. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  710. set_bit(0, &ts->check_clocks);
  711. }
  712. /**
  713. * Check, if a change happened, which makes oneshot possible.
  714. *
  715. * Called cyclic from the hrtimer softirq (driven by the timer
  716. * softirq) allow_nohz signals, that we can switch into low-res nohz
  717. * mode, because high resolution timers are disabled (either compile
  718. * or runtime).
  719. */
  720. int tick_check_oneshot_change(int allow_nohz)
  721. {
  722. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  723. if (!test_and_clear_bit(0, &ts->check_clocks))
  724. return 0;
  725. if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
  726. return 0;
  727. if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
  728. return 0;
  729. if (!allow_nohz)
  730. return 1;
  731. tick_nohz_switch_to_nohz();
  732. return 0;
  733. }