tick-sched.c 26 KB

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