tick-sched.c 22 KB

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