tick-sched.c 23 KB

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