tick-sched.c 30 KB

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