tick-sched.c 28 KB

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