tick-sched.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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 <linux/rq_stats.h>
  24. #include <asm/irq_regs.h>
  25. #include "tick-internal.h"
  26. struct rq_data rq_info;
  27. struct workqueue_struct *rq_wq;
  28. spinlock_t rq_lock;
  29. /*
  30. * Per cpu nohz control structure
  31. */
  32. static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
  33. /*
  34. * The time, when the last jiffy update happened. Protected by xtime_lock.
  35. */
  36. static ktime_t last_jiffies_update;
  37. struct tick_sched *tick_get_tick_sched(int cpu)
  38. {
  39. return &per_cpu(tick_cpu_sched, cpu);
  40. }
  41. /*
  42. * Must be called with interrupts disabled !
  43. */
  44. static void tick_do_update_jiffies64(ktime_t now)
  45. {
  46. unsigned long ticks = 0;
  47. ktime_t delta;
  48. /*
  49. * Do a quick check without holding xtime_lock:
  50. */
  51. delta = ktime_sub(now, last_jiffies_update);
  52. if (delta.tv64 < tick_period.tv64)
  53. return;
  54. /* Reevalute with xtime_lock held */
  55. write_seqlock(&xtime_lock);
  56. delta = ktime_sub(now, last_jiffies_update);
  57. if (delta.tv64 >= tick_period.tv64) {
  58. delta = ktime_sub(delta, tick_period);
  59. last_jiffies_update = ktime_add(last_jiffies_update,
  60. tick_period);
  61. /* Slow path for long timeouts */
  62. if (unlikely(delta.tv64 >= tick_period.tv64)) {
  63. s64 incr = ktime_to_ns(tick_period);
  64. ticks = ktime_divns(delta, incr);
  65. last_jiffies_update = ktime_add_ns(last_jiffies_update,
  66. incr * ticks);
  67. }
  68. do_timer(++ticks);
  69. /* Keep the tick_next_period variable up to date */
  70. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  71. }
  72. write_sequnlock(&xtime_lock);
  73. }
  74. /*
  75. * Initialize and return retrieve the jiffies update.
  76. */
  77. static ktime_t tick_init_jiffy_update(void)
  78. {
  79. ktime_t period;
  80. write_seqlock(&xtime_lock);
  81. /* Did we start the jiffies update yet ? */
  82. if (last_jiffies_update.tv64 == 0)
  83. last_jiffies_update = tick_next_period;
  84. period = last_jiffies_update;
  85. write_sequnlock(&xtime_lock);
  86. return period;
  87. }
  88. /*
  89. * NOHZ - aka dynamic tick functionality
  90. */
  91. #ifdef CONFIG_NO_HZ
  92. /*
  93. * NO HZ enabled ?
  94. */
  95. int tick_nohz_enabled __read_mostly = 1;
  96. /*
  97. * Enable / Disable tickless mode
  98. */
  99. static int __init setup_tick_nohz(char *str)
  100. {
  101. if (!strcmp(str, "off"))
  102. tick_nohz_enabled = 0;
  103. else if (!strcmp(str, "on"))
  104. tick_nohz_enabled = 1;
  105. else
  106. return 0;
  107. return 1;
  108. }
  109. __setup("nohz=", setup_tick_nohz);
  110. /**
  111. * tick_nohz_update_jiffies - update jiffies when idle was interrupted
  112. *
  113. * Called from interrupt entry when the CPU was idle
  114. *
  115. * In case the sched_tick was stopped on this CPU, we have to check if jiffies
  116. * must be updated. Otherwise an interrupt handler could use a stale jiffy
  117. * value. We do this unconditionally on any cpu, as we don't know whether the
  118. * cpu, which has the update task assigned is in a long sleep.
  119. */
  120. static void tick_nohz_update_jiffies(ktime_t now)
  121. {
  122. int cpu = smp_processor_id();
  123. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  124. unsigned long flags;
  125. ts->idle_waketime = now;
  126. local_irq_save(flags);
  127. tick_do_update_jiffies64(now);
  128. local_irq_restore(flags);
  129. touch_softlockup_watchdog();
  130. }
  131. /*
  132. * Updates the per cpu time idle statistics counters
  133. */
  134. static void
  135. update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
  136. {
  137. ktime_t delta;
  138. if (ts->idle_active) {
  139. delta = ktime_sub(now, ts->idle_entrytime);
  140. if (nr_iowait_cpu(cpu) > 0)
  141. ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
  142. else
  143. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  144. ts->idle_entrytime = now;
  145. }
  146. if (last_update_time)
  147. *last_update_time = ktime_to_us(now);
  148. }
  149. static void tick_nohz_stop_idle(int cpu, ktime_t now)
  150. {
  151. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  152. update_ts_time_stats(cpu, ts, now, NULL);
  153. ts->idle_active = 0;
  154. sched_clock_idle_wakeup_event(0);
  155. }
  156. static ktime_t tick_nohz_start_idle(int cpu, struct tick_sched *ts)
  157. {
  158. ktime_t now = ktime_get();
  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. Do not update
  168. * counters if NULL.
  169. *
  170. * Return the cummulative idle time (since boot) for a given
  171. * CPU, in microseconds.
  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. ktime_t now, idle;
  182. if (!tick_nohz_enabled)
  183. return -1;
  184. now = ktime_get();
  185. if (last_update_time) {
  186. update_ts_time_stats(cpu, ts, now, last_update_time);
  187. idle = ts->idle_sleeptime;
  188. } else {
  189. if (ts->idle_active && !nr_iowait_cpu(cpu)) {
  190. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  191. idle = ktime_add(ts->idle_sleeptime, delta);
  192. } else {
  193. idle = ts->idle_sleeptime;
  194. }
  195. }
  196. return ktime_to_us(idle);
  197. }
  198. EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
  199. /**
  200. * get_cpu_iowait_time_us - get the total iowait time of a cpu
  201. * @cpu: CPU number to query
  202. * @last_update_time: variable to store update time in. Do not update
  203. * counters if NULL.
  204. *
  205. * Return the cummulative iowait time (since boot) for a given
  206. * CPU, in microseconds.
  207. *
  208. * This time is measured via accounting rather than sampling,
  209. * and is as accurate as ktime_get() is.
  210. *
  211. * This function returns -1 if NOHZ is not enabled.
  212. */
  213. u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
  214. {
  215. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  216. ktime_t now, iowait;
  217. if (!tick_nohz_enabled)
  218. return -1;
  219. now = ktime_get();
  220. if (last_update_time) {
  221. update_ts_time_stats(cpu, ts, now, last_update_time);
  222. iowait = ts->iowait_sleeptime;
  223. } else {
  224. if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
  225. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  226. iowait = ktime_add(ts->iowait_sleeptime, delta);
  227. } else {
  228. iowait = ts->iowait_sleeptime;
  229. }
  230. }
  231. return ktime_to_us(iowait);
  232. }
  233. EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
  234. static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
  235. ktime_t now, int cpu)
  236. {
  237. unsigned long seq, last_jiffies, next_jiffies, delta_jiffies;
  238. unsigned long rcu_delta_jiffies;
  239. ktime_t last_update, expires, ret = { .tv64 = 0 };
  240. struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
  241. u64 time_delta;
  242. /* Read jiffies and the time when jiffies were updated last */
  243. do {
  244. seq = read_seqbegin(&xtime_lock);
  245. last_update = last_jiffies_update;
  246. last_jiffies = jiffies;
  247. time_delta = timekeeping_max_deferment();
  248. } while (read_seqretry(&xtime_lock, seq));
  249. if (rcu_needs_cpu(cpu, &rcu_delta_jiffies) || printk_needs_cpu(cpu) ||
  250. arch_needs_cpu(cpu)) {
  251. next_jiffies = last_jiffies + 1;
  252. delta_jiffies = 1;
  253. } else {
  254. /* Get the next timer wheel timer */
  255. next_jiffies = get_next_timer_interrupt(last_jiffies);
  256. delta_jiffies = next_jiffies - last_jiffies;
  257. if (rcu_delta_jiffies < delta_jiffies) {
  258. next_jiffies = last_jiffies + rcu_delta_jiffies;
  259. delta_jiffies = rcu_delta_jiffies;
  260. }
  261. }
  262. /*
  263. * Do not stop the tick, if we are only one off (or less)
  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. /* Skip reprogram of event if its not changed */
  315. if (ts->tick_stopped && ktime_equal(expires, dev->next_event))
  316. goto out;
  317. ret = expires;
  318. /*
  319. * nohz_stop_sched_tick can be called several times before
  320. * the nohz_restart_sched_tick is called. This happens when
  321. * interrupts arrive which do not cause a reschedule. In the
  322. * first call we save the current tick time, so we can restart
  323. * the scheduler tick in nohz_restart_sched_tick.
  324. */
  325. if (!ts->tick_stopped) {
  326. nohz_balance_enter_idle(cpu);
  327. calc_load_enter_idle();
  328. ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
  329. ts->tick_stopped = 1;
  330. }
  331. /*
  332. * If the expiration time == KTIME_MAX, then
  333. * in this case we simply stop the tick timer.
  334. */
  335. if (unlikely(expires.tv64 == KTIME_MAX)) {
  336. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  337. hrtimer_cancel(&ts->sched_timer);
  338. goto out;
  339. }
  340. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  341. hrtimer_start(&ts->sched_timer, expires,
  342. HRTIMER_MODE_ABS_PINNED);
  343. /* Check, if the timer was already in the past */
  344. if (hrtimer_active(&ts->sched_timer))
  345. goto out;
  346. } else if (!tick_program_event(expires, 0))
  347. goto out;
  348. /*
  349. * We are past the event already. So we crossed a
  350. * jiffie boundary. Update jiffies and raise the
  351. * softirq.
  352. */
  353. tick_do_update_jiffies64(ktime_get());
  354. }
  355. raise_softirq_irqoff(TIMER_SOFTIRQ);
  356. out:
  357. ts->next_jiffies = next_jiffies;
  358. ts->last_jiffies = last_jiffies;
  359. return ret;
  360. }
  361. static void tick_nohz_full_stop_tick(struct tick_sched *ts)
  362. {
  363. #ifdef CONFIG_NO_HZ_FULL
  364. int cpu = smp_processor_id();
  365. if (!tick_nohz_full_cpu(cpu) || is_idle_task(current))
  366. return;
  367. if (!ts->tick_stopped && ts->nohz_mode == NOHZ_MODE_INACTIVE)
  368. return;
  369. if (!can_stop_full_tick())
  370. return;
  371. tick_nohz_stop_sched_tick(ts, ktime_get(), cpu);
  372. #endif
  373. }
  374. static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
  375. {
  376. /*
  377. * If this cpu is offline and it is the one which updates
  378. * jiffies, then give up the assignment and let it be taken by
  379. * the cpu which runs the tick timer next. If we don't drop
  380. * this here the jiffies might be stale and do_timer() never
  381. * invoked.
  382. */
  383. if (unlikely(!cpu_online(cpu))) {
  384. if (cpu == tick_do_timer_cpu)
  385. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  386. }
  387. if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
  388. return false;
  389. if (need_resched())
  390. return false;
  391. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  392. static int ratelimit;
  393. if (ratelimit < 10) {
  394. printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  395. (unsigned int) local_softirq_pending());
  396. ratelimit++;
  397. }
  398. return false;
  399. }
  400. return true;
  401. }
  402. static void __tick_nohz_idle_enter(struct tick_sched *ts)
  403. {
  404. ktime_t now, expires;
  405. int cpu = smp_processor_id();
  406. now = tick_nohz_start_idle(cpu, ts);
  407. if (can_stop_idle_tick(cpu, ts)) {
  408. int was_stopped = ts->tick_stopped;
  409. ts->idle_calls++;
  410. expires = tick_nohz_stop_sched_tick(ts, now, cpu);
  411. if (expires.tv64 > 0LL) {
  412. ts->idle_sleeps++;
  413. ts->idle_expires = expires;
  414. }
  415. if (!was_stopped && ts->tick_stopped)
  416. ts->idle_jiffies = ts->last_jiffies;
  417. }
  418. }
  419. /**
  420. * tick_nohz_idle_enter - stop the idle tick from the idle task
  421. *
  422. * When the next event is more than a tick into the future, stop the idle tick
  423. * Called when we start the idle loop.
  424. *
  425. * The arch is responsible of calling:
  426. *
  427. * - rcu_idle_enter() after its last use of RCU before the CPU is put
  428. * to sleep.
  429. * - rcu_idle_exit() before the first use of RCU after the CPU is woken up.
  430. */
  431. void tick_nohz_idle_enter(void)
  432. {
  433. struct tick_sched *ts;
  434. WARN_ON_ONCE(irqs_disabled());
  435. /*
  436. * Update the idle state in the scheduler domain hierarchy
  437. * when tick_nohz_stop_sched_tick() is called from the idle loop.
  438. * State will be updated to busy during the first busy tick after
  439. * exiting idle.
  440. */
  441. set_cpu_sd_state_idle();
  442. local_irq_disable();
  443. ts = &__get_cpu_var(tick_cpu_sched);
  444. /*
  445. * set ts->inidle unconditionally. even if the system did not
  446. * switch to nohz mode the cpu frequency governers rely on the
  447. * update of the idle time accounting in tick_nohz_start_idle().
  448. */
  449. ts->inidle = 1;
  450. __tick_nohz_idle_enter(ts);
  451. local_irq_enable();
  452. }
  453. /**
  454. * tick_nohz_irq_exit - update next tick event from interrupt exit
  455. *
  456. * When an interrupt fires while we are idle and it doesn't cause
  457. * a reschedule, it may still add, modify or delete a timer, enqueue
  458. * an RCU callback, etc...
  459. * So we need to re-calculate and reprogram the next tick event.
  460. */
  461. void tick_nohz_irq_exit(void)
  462. {
  463. unsigned long flags;
  464. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  465. if (ts->inidle) {
  466. /* Cancel the timer because CPU already waken up from the C-states*/
  467. menu_hrtimer_cancel();
  468. __tick_nohz_idle_enter(ts);
  469. } else {
  470. tick_nohz_full_stop_tick(ts);
  471. }
  472. }
  473. /**
  474. * tick_nohz_get_sleep_length - return the length of the current sleep
  475. *
  476. * Called from power state control code with interrupts disabled
  477. */
  478. ktime_t tick_nohz_get_sleep_length(void)
  479. {
  480. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  481. struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
  482. return ktime_sub(dev->next_event, ts->idle_entrytime);
  483. }
  484. static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
  485. {
  486. hrtimer_cancel(&ts->sched_timer);
  487. hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
  488. while (1) {
  489. /* Forward the time to expire in the future */
  490. hrtimer_forward(&ts->sched_timer, now, tick_period);
  491. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  492. hrtimer_start_expires(&ts->sched_timer,
  493. HRTIMER_MODE_ABS_PINNED);
  494. /* Check, if the timer was already in the past */
  495. if (hrtimer_active(&ts->sched_timer))
  496. break;
  497. } else {
  498. if (!tick_program_event(
  499. hrtimer_get_expires(&ts->sched_timer), 0))
  500. break;
  501. }
  502. /* Reread time and update jiffies */
  503. now = ktime_get();
  504. tick_do_update_jiffies64(now);
  505. }
  506. }
  507. static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
  508. {
  509. /* Update jiffies first */
  510. tick_do_update_jiffies64(now);
  511. update_cpu_load_nohz();
  512. calc_load_exit_idle();
  513. touch_softlockup_watchdog();
  514. /*
  515. * Cancel the scheduled timer and restore the tick
  516. */
  517. ts->tick_stopped = 0;
  518. ts->idle_exittime = now;
  519. tick_nohz_restart(ts, now);
  520. }
  521. static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
  522. {
  523. #ifndef CONFIG_VIRT_CPU_ACCOUNTING
  524. unsigned long ticks;
  525. /*
  526. * We stopped the tick in idle. Update process times would miss the
  527. * time we slept as update_process_times does only a 1 tick
  528. * accounting. Enforce that this is accounted to idle !
  529. */
  530. ticks = jiffies - ts->idle_jiffies;
  531. /*
  532. * We might be one off. Do not randomly account a huge number of ticks!
  533. */
  534. if (ticks && ticks < LONG_MAX)
  535. account_idle_ticks(ticks);
  536. #endif
  537. }
  538. /**
  539. * tick_nohz_idle_exit - restart the idle tick from the idle task
  540. *
  541. * Restart the idle tick when the CPU is woken up from idle
  542. * This also exit the RCU extended quiescent state. The CPU
  543. * can use RCU again after this function is called.
  544. */
  545. void tick_nohz_idle_exit(void)
  546. {
  547. int cpu = smp_processor_id();
  548. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  549. ktime_t now;
  550. local_irq_disable();
  551. WARN_ON_ONCE(!ts->inidle);
  552. ts->inidle = 0;
  553. /* Cancel the timer because CPU already waken up from the C-states*/
  554. menu_hrtimer_cancel();
  555. if (ts->idle_active || ts->tick_stopped)
  556. now = ktime_get();
  557. if (ts->idle_active)
  558. tick_nohz_stop_idle(cpu, now);
  559. if (ts->tick_stopped) {
  560. tick_nohz_restart_sched_tick(ts, now);
  561. tick_nohz_account_idle_ticks(ts);
  562. }
  563. local_irq_enable();
  564. }
  565. static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
  566. {
  567. hrtimer_forward(&ts->sched_timer, now, tick_period);
  568. return tick_program_event(hrtimer_get_expires(&ts->sched_timer), 0);
  569. }
  570. /*
  571. * The nohz low res interrupt handler
  572. */
  573. static void tick_nohz_handler(struct clock_event_device *dev)
  574. {
  575. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  576. struct pt_regs *regs = get_irq_regs();
  577. int cpu = smp_processor_id();
  578. ktime_t now = ktime_get();
  579. dev->next_event.tv64 = KTIME_MAX;
  580. /*
  581. * Check if the do_timer duty was dropped. We don't care about
  582. * concurrency: This happens only when the cpu in charge went
  583. * into a long sleep. If two cpus happen to assign themself to
  584. * this duty, then the jiffies update is still serialized by
  585. * xtime_lock.
  586. */
  587. if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
  588. tick_do_timer_cpu = cpu;
  589. /* Check, if the jiffies need an update */
  590. if (tick_do_timer_cpu == cpu)
  591. tick_do_update_jiffies64(now);
  592. /*
  593. * When we are idle and the tick is stopped, we have to touch
  594. * the watchdog as we might not schedule for a really long
  595. * time. This happens on complete idle SMP systems while
  596. * waiting on the login prompt. We also increment the "start
  597. * of idle" jiffy stamp so the idle accounting adjustment we
  598. * do when we go busy again does not account too much ticks.
  599. */
  600. if (ts->tick_stopped) {
  601. touch_softlockup_watchdog();
  602. ts->idle_jiffies++;
  603. }
  604. update_process_times(user_mode(regs));
  605. profile_tick(CPU_PROFILING);
  606. while (tick_nohz_reprogram(ts, now)) {
  607. now = ktime_get();
  608. tick_do_update_jiffies64(now);
  609. }
  610. }
  611. /**
  612. * tick_nohz_switch_to_nohz - switch to nohz mode
  613. */
  614. static void tick_nohz_switch_to_nohz(void)
  615. {
  616. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  617. ktime_t next;
  618. if (!tick_nohz_enabled)
  619. return;
  620. local_irq_disable();
  621. if (tick_switch_to_oneshot(tick_nohz_handler)) {
  622. local_irq_enable();
  623. return;
  624. }
  625. ts->nohz_mode = NOHZ_MODE_LOWRES;
  626. /*
  627. * Recycle the hrtimer in ts, so we can share the
  628. * hrtimer_forward with the highres code.
  629. */
  630. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  631. /* Get the next period */
  632. next = tick_init_jiffy_update();
  633. for (;;) {
  634. hrtimer_set_expires(&ts->sched_timer, next);
  635. if (!tick_program_event(next, 0))
  636. break;
  637. next = ktime_add(next, tick_period);
  638. }
  639. local_irq_enable();
  640. }
  641. /*
  642. * When NOHZ is enabled and the tick is stopped, we need to kick the
  643. * tick timer from irq_enter() so that the jiffies update is kept
  644. * alive during long running softirqs. That's ugly as hell, but
  645. * correctness is key even if we need to fix the offending softirq in
  646. * the first place.
  647. *
  648. * Note, this is different to tick_nohz_restart. We just kick the
  649. * timer and do not touch the other magic bits which need to be done
  650. * when idle is left.
  651. */
  652. static void tick_nohz_kick_tick(int cpu, ktime_t now)
  653. {
  654. #if 0
  655. /* Switch back to 2.6.27 behaviour */
  656. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  657. ktime_t delta;
  658. /*
  659. * Do not touch the tick device, when the next expiry is either
  660. * already reached or less/equal than the tick period.
  661. */
  662. delta = ktime_sub(hrtimer_get_expires(&ts->sched_timer), now);
  663. if (delta.tv64 <= tick_period.tv64)
  664. return;
  665. tick_nohz_restart(ts, now);
  666. #endif
  667. }
  668. static inline void tick_check_nohz(int cpu)
  669. {
  670. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  671. ktime_t now;
  672. if (!ts->idle_active && !ts->tick_stopped)
  673. return;
  674. now = ktime_get();
  675. if (ts->idle_active)
  676. tick_nohz_stop_idle(cpu, now);
  677. if (ts->tick_stopped) {
  678. tick_nohz_update_jiffies(now);
  679. tick_nohz_kick_tick(cpu, now);
  680. }
  681. }
  682. #else
  683. static inline void tick_nohz_switch_to_nohz(void) { }
  684. static inline void tick_check_nohz(int cpu) { }
  685. #endif /* NO_HZ */
  686. /*
  687. * Called from irq_enter to notify about the possible interruption of idle()
  688. */
  689. void tick_check_idle(int cpu)
  690. {
  691. tick_check_oneshot_broadcast(cpu);
  692. tick_check_nohz(cpu);
  693. }
  694. /*
  695. * High resolution timer specific code
  696. */
  697. #ifdef CONFIG_HIGH_RES_TIMERS
  698. static void update_rq_stats(void)
  699. {
  700. unsigned long jiffy_gap = 0;
  701. unsigned int rq_avg = 0;
  702. unsigned long flags = 0;
  703. jiffy_gap = jiffies - rq_info.rq_poll_last_jiffy;
  704. if (jiffy_gap >= rq_info.rq_poll_jiffies) {
  705. spin_lock_irqsave(&rq_lock, flags);
  706. if (!rq_info.rq_avg)
  707. rq_info.rq_poll_total_jiffies = 0;
  708. rq_avg = nr_running() * 10;
  709. if (rq_info.rq_poll_total_jiffies) {
  710. rq_avg = (rq_avg * jiffy_gap) +
  711. (rq_info.rq_avg *
  712. rq_info.rq_poll_total_jiffies);
  713. do_div(rq_avg,
  714. rq_info.rq_poll_total_jiffies + jiffy_gap);
  715. }
  716. rq_info.rq_avg = rq_avg;
  717. rq_info.rq_poll_total_jiffies += jiffy_gap;
  718. rq_info.rq_poll_last_jiffy = jiffies;
  719. spin_unlock_irqrestore(&rq_lock, flags);
  720. }
  721. }
  722. static void wakeup_user(void)
  723. {
  724. unsigned long jiffy_gap;
  725. jiffy_gap = jiffies - rq_info.def_timer_last_jiffy;
  726. if (jiffy_gap >= rq_info.def_timer_jiffies) {
  727. rq_info.def_timer_last_jiffy = jiffies;
  728. queue_work(rq_wq, &rq_info.def_timer_work);
  729. }
  730. }
  731. /*
  732. * We rearm the timer until we get disabled by the idle code.
  733. * Called with interrupts disabled and timer->base->cpu_base->lock held.
  734. */
  735. static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
  736. {
  737. struct tick_sched *ts =
  738. container_of(timer, struct tick_sched, sched_timer);
  739. struct pt_regs *regs = get_irq_regs();
  740. ktime_t now = ktime_get();
  741. int cpu = smp_processor_id();
  742. #ifdef CONFIG_NO_HZ
  743. /*
  744. * Check if the do_timer duty was dropped. We don't care about
  745. * concurrency: This happens only when the cpu in charge went
  746. * into a long sleep. If two cpus happen to assign themself to
  747. * this duty, then the jiffies update is still serialized by
  748. * xtime_lock.
  749. */
  750. if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
  751. tick_do_timer_cpu = cpu;
  752. #endif
  753. /* Check, if the jiffies need an update */
  754. if (tick_do_timer_cpu == cpu)
  755. tick_do_update_jiffies64(now);
  756. /*
  757. * Do not call, when we are not in irq context and have
  758. * no valid regs pointer
  759. */
  760. if (regs) {
  761. /*
  762. * When we are idle and the tick is stopped, we have to touch
  763. * the watchdog as we might not schedule for a really long
  764. * time. This happens on complete idle SMP systems while
  765. * waiting on the login prompt. We also increment the "start of
  766. * idle" jiffy stamp so the idle accounting adjustment we do
  767. * when we go busy again does not account too much ticks.
  768. */
  769. if (ts->tick_stopped) {
  770. touch_softlockup_watchdog();
  771. if (is_idle_task(current))
  772. ts->idle_jiffies++;
  773. }
  774. update_process_times(user_mode(regs));
  775. profile_tick(CPU_PROFILING);
  776. if ((rq_info.init == 1) && (tick_do_timer_cpu == cpu)) {
  777. /*
  778. * update run queue statistics
  779. */
  780. update_rq_stats();
  781. /*
  782. * wakeup user if needed
  783. */
  784. wakeup_user();
  785. }
  786. }
  787. hrtimer_forward(timer, now, tick_period);
  788. return HRTIMER_RESTART;
  789. }
  790. static int sched_skew_tick;
  791. static int __init skew_tick(char *str)
  792. {
  793. get_option(&str, &sched_skew_tick);
  794. return 0;
  795. }
  796. early_param("skew_tick", skew_tick);
  797. /**
  798. * tick_setup_sched_timer - setup the tick emulation timer
  799. */
  800. void tick_setup_sched_timer(void)
  801. {
  802. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  803. ktime_t now = ktime_get();
  804. /*
  805. * Emulate tick processing via per-CPU hrtimers:
  806. */
  807. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  808. ts->sched_timer.function = tick_sched_timer;
  809. /* Get the next period (per cpu) */
  810. hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
  811. /* Offset the tick to avert xtime_lock contention. */
  812. if (sched_skew_tick) {
  813. u64 offset = ktime_to_ns(tick_period) >> 1;
  814. do_div(offset, num_possible_cpus());
  815. offset *= smp_processor_id();
  816. hrtimer_add_expires_ns(&ts->sched_timer, offset);
  817. }
  818. for (;;) {
  819. hrtimer_forward(&ts->sched_timer, now, tick_period);
  820. hrtimer_start_expires(&ts->sched_timer,
  821. HRTIMER_MODE_ABS_PINNED);
  822. /* Check, if the timer was already in the past */
  823. if (hrtimer_active(&ts->sched_timer))
  824. break;
  825. now = ktime_get();
  826. }
  827. #ifdef CONFIG_NO_HZ
  828. if (tick_nohz_enabled)
  829. ts->nohz_mode = NOHZ_MODE_HIGHRES;
  830. #endif
  831. }
  832. #endif /* HIGH_RES_TIMERS */
  833. #if defined CONFIG_NO_HZ || defined CONFIG_HIGH_RES_TIMERS
  834. void tick_cancel_sched_timer(int cpu)
  835. {
  836. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  837. # ifdef CONFIG_HIGH_RES_TIMERS
  838. if (ts->sched_timer.base)
  839. hrtimer_cancel(&ts->sched_timer);
  840. # endif
  841. memset(ts, 0, sizeof(*ts));
  842. }
  843. #endif
  844. /**
  845. * Async notification about clocksource changes
  846. */
  847. void tick_clock_notify(void)
  848. {
  849. int cpu;
  850. for_each_possible_cpu(cpu)
  851. set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
  852. }
  853. /*
  854. * Async notification about clock event changes
  855. */
  856. void tick_oneshot_notify(void)
  857. {
  858. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  859. set_bit(0, &ts->check_clocks);
  860. }
  861. /**
  862. * Check, if a change happened, which makes oneshot possible.
  863. *
  864. * Called cyclic from the hrtimer softirq (driven by the timer
  865. * softirq) allow_nohz signals, that we can switch into low-res nohz
  866. * mode, because high resolution timers are disabled (either compile
  867. * or runtime).
  868. */
  869. int tick_check_oneshot_change(int allow_nohz)
  870. {
  871. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  872. if (!test_and_clear_bit(0, &ts->check_clocks))
  873. return 0;
  874. if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
  875. return 0;
  876. if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
  877. return 0;
  878. if (!allow_nohz)
  879. return 1;
  880. tick_nohz_switch_to_nohz();
  881. return 0;
  882. }