tick-sched.c 26 KB

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