tick-sched.c 22 KB

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