tick-sched.c 30 KB

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