tick-sched.c 31 KB

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