tick-sched.c 32 KB

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