tick-sched.c 24 KB

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