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 < tick_period)
  54. return;
  55. /* Reevaluate with jiffies_lock held */
  56. write_seqlock(&jiffies_lock);
  57. delta = ktime_sub(now, last_jiffies_update);
  58. if (delta >= tick_period) {
  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 >= tick_period)) {
  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 == 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(unsigned int cpu)
  328. {
  329. /*
  330. * The boot CPU handles housekeeping duty (unbound timers,
  331. * workqueues, timekeeping, ...) on behalf of full dynticks
  332. * CPUs. It must remain online when nohz full is enabled.
  333. */
  334. if (tick_nohz_full_running && tick_do_timer_cpu == cpu)
  335. return -EBUSY;
  336. return 0;
  337. }
  338. static int tick_nohz_init_all(void)
  339. {
  340. int err = -1;
  341. #ifdef CONFIG_NO_HZ_FULL_ALL
  342. if (!alloc_cpumask_var(&tick_nohz_full_mask, GFP_KERNEL)) {
  343. WARN(1, "NO_HZ: Can't allocate full dynticks cpumask\n");
  344. return err;
  345. }
  346. err = 0;
  347. cpumask_setall(tick_nohz_full_mask);
  348. tick_nohz_full_running = true;
  349. #endif
  350. return err;
  351. }
  352. void __init tick_nohz_init(void)
  353. {
  354. int cpu, ret;
  355. if (!tick_nohz_full_running) {
  356. if (tick_nohz_init_all() < 0)
  357. return;
  358. }
  359. if (!alloc_cpumask_var(&housekeeping_mask, GFP_KERNEL)) {
  360. WARN(1, "NO_HZ: Can't allocate not-full dynticks cpumask\n");
  361. cpumask_clear(tick_nohz_full_mask);
  362. tick_nohz_full_running = false;
  363. return;
  364. }
  365. /*
  366. * Full dynticks uses irq work to drive the tick rescheduling on safe
  367. * locking contexts. But then we need irq work to raise its own
  368. * interrupts to avoid circular dependency on the tick
  369. */
  370. if (!arch_irq_work_has_interrupt()) {
  371. pr_warn("NO_HZ: Can't run full dynticks because arch doesn't support irq work self-IPIs\n");
  372. cpumask_clear(tick_nohz_full_mask);
  373. cpumask_copy(housekeeping_mask, cpu_possible_mask);
  374. tick_nohz_full_running = false;
  375. return;
  376. }
  377. cpu = smp_processor_id();
  378. if (cpumask_test_cpu(cpu, tick_nohz_full_mask)) {
  379. pr_warn("NO_HZ: Clearing %d from nohz_full range for timekeeping\n",
  380. cpu);
  381. cpumask_clear_cpu(cpu, tick_nohz_full_mask);
  382. }
  383. cpumask_andnot(housekeeping_mask,
  384. cpu_possible_mask, tick_nohz_full_mask);
  385. for_each_cpu(cpu, tick_nohz_full_mask)
  386. context_tracking_cpu_set(cpu);
  387. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  388. "kernel/nohz:predown", NULL,
  389. tick_nohz_cpu_down);
  390. WARN_ON(ret < 0);
  391. pr_info("NO_HZ: Full dynticks CPUs: %*pbl.\n",
  392. cpumask_pr_args(tick_nohz_full_mask));
  393. /*
  394. * We need at least one CPU to handle housekeeping work such
  395. * as timekeeping, unbound timers, workqueues, ...
  396. */
  397. WARN_ON_ONCE(cpumask_empty(housekeeping_mask));
  398. }
  399. #endif
  400. /*
  401. * NOHZ - aka dynamic tick functionality
  402. */
  403. #ifdef CONFIG_NO_HZ_COMMON
  404. /*
  405. * NO HZ enabled ?
  406. */
  407. bool tick_nohz_enabled __read_mostly = true;
  408. unsigned long tick_nohz_active __read_mostly;
  409. /*
  410. * Enable / Disable tickless mode
  411. */
  412. static int __init setup_tick_nohz(char *str)
  413. {
  414. return (kstrtobool(str, &tick_nohz_enabled) == 0);
  415. }
  416. __setup("nohz=", setup_tick_nohz);
  417. int tick_nohz_tick_stopped(void)
  418. {
  419. return __this_cpu_read(tick_cpu_sched.tick_stopped);
  420. }
  421. /**
  422. * tick_nohz_update_jiffies - update jiffies when idle was interrupted
  423. *
  424. * Called from interrupt entry when the CPU was idle
  425. *
  426. * In case the sched_tick was stopped on this CPU, we have to check if jiffies
  427. * must be updated. Otherwise an interrupt handler could use a stale jiffy
  428. * value. We do this unconditionally on any CPU, as we don't know whether the
  429. * CPU, which has the update task assigned is in a long sleep.
  430. */
  431. static void tick_nohz_update_jiffies(ktime_t now)
  432. {
  433. unsigned long flags;
  434. __this_cpu_write(tick_cpu_sched.idle_waketime, now);
  435. local_irq_save(flags);
  436. tick_do_update_jiffies64(now);
  437. local_irq_restore(flags);
  438. touch_softlockup_watchdog_sched();
  439. }
  440. /*
  441. * Updates the per-CPU time idle statistics counters
  442. */
  443. static void
  444. update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
  445. {
  446. ktime_t delta;
  447. if (ts->idle_active) {
  448. delta = ktime_sub(now, ts->idle_entrytime);
  449. if (nr_iowait_cpu(cpu) > 0)
  450. ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
  451. else
  452. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  453. ts->idle_entrytime = now;
  454. }
  455. if (last_update_time)
  456. *last_update_time = ktime_to_us(now);
  457. }
  458. static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now)
  459. {
  460. update_ts_time_stats(smp_processor_id(), ts, now, NULL);
  461. ts->idle_active = 0;
  462. sched_clock_idle_wakeup_event(0);
  463. }
  464. static ktime_t tick_nohz_start_idle(struct tick_sched *ts)
  465. {
  466. ktime_t now = ktime_get();
  467. ts->idle_entrytime = now;
  468. ts->idle_active = 1;
  469. sched_clock_idle_sleep_event();
  470. return now;
  471. }
  472. /**
  473. * get_cpu_idle_time_us - get the total idle time of a CPU
  474. * @cpu: CPU number to query
  475. * @last_update_time: variable to store update time in. Do not update
  476. * counters if NULL.
  477. *
  478. * Return the cumulative idle time (since boot) for a given
  479. * CPU, in microseconds.
  480. *
  481. * This time is measured via accounting rather than sampling,
  482. * and is as accurate as ktime_get() is.
  483. *
  484. * This function returns -1 if NOHZ is not enabled.
  485. */
  486. u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
  487. {
  488. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  489. ktime_t now, idle;
  490. if (!tick_nohz_active)
  491. return -1;
  492. now = ktime_get();
  493. if (last_update_time) {
  494. update_ts_time_stats(cpu, ts, now, last_update_time);
  495. idle = ts->idle_sleeptime;
  496. } else {
  497. if (ts->idle_active && !nr_iowait_cpu(cpu)) {
  498. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  499. idle = ktime_add(ts->idle_sleeptime, delta);
  500. } else {
  501. idle = ts->idle_sleeptime;
  502. }
  503. }
  504. return ktime_to_us(idle);
  505. }
  506. EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
  507. /**
  508. * get_cpu_iowait_time_us - get the total iowait time of a CPU
  509. * @cpu: CPU number to query
  510. * @last_update_time: variable to store update time in. Do not update
  511. * counters if NULL.
  512. *
  513. * Return the cumulative iowait time (since boot) for a given
  514. * CPU, in microseconds.
  515. *
  516. * This time is measured via accounting rather than sampling,
  517. * and is as accurate as ktime_get() is.
  518. *
  519. * This function returns -1 if NOHZ is not enabled.
  520. */
  521. u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
  522. {
  523. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  524. ktime_t now, iowait;
  525. if (!tick_nohz_active)
  526. return -1;
  527. now = ktime_get();
  528. if (last_update_time) {
  529. update_ts_time_stats(cpu, ts, now, last_update_time);
  530. iowait = ts->iowait_sleeptime;
  531. } else {
  532. if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
  533. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  534. iowait = ktime_add(ts->iowait_sleeptime, delta);
  535. } else {
  536. iowait = ts->iowait_sleeptime;
  537. }
  538. }
  539. return ktime_to_us(iowait);
  540. }
  541. EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
  542. static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
  543. {
  544. hrtimer_cancel(&ts->sched_timer);
  545. hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
  546. /* Forward the time to expire in the future */
  547. hrtimer_forward(&ts->sched_timer, now, tick_period);
  548. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  549. hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
  550. else
  551. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  552. }
  553. static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
  554. ktime_t now, int cpu)
  555. {
  556. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  557. u64 basemono, next_tick, next_tmr, next_rcu, delta, expires;
  558. unsigned long seq, basejiff;
  559. ktime_t tick;
  560. /* Read jiffies and the time when jiffies were updated last */
  561. do {
  562. seq = read_seqbegin(&jiffies_lock);
  563. basemono = last_jiffies_update;
  564. basejiff = jiffies;
  565. } while (read_seqretry(&jiffies_lock, seq));
  566. ts->last_jiffies = basejiff;
  567. if (rcu_needs_cpu(basemono, &next_rcu) ||
  568. arch_needs_cpu() || irq_work_needs_cpu()) {
  569. next_tick = basemono + TICK_NSEC;
  570. } else {
  571. /*
  572. * Get the next pending timer. If high resolution
  573. * timers are enabled this only takes the timer wheel
  574. * timers into account. If high resolution timers are
  575. * disabled this also looks at the next expiring
  576. * hrtimer.
  577. */
  578. next_tmr = get_next_timer_interrupt(basejiff, basemono);
  579. ts->next_timer = next_tmr;
  580. /* Take the next rcu event into account */
  581. next_tick = next_rcu < next_tmr ? next_rcu : next_tmr;
  582. }
  583. /*
  584. * If the tick is due in the next period, keep it ticking or
  585. * force prod the timer.
  586. */
  587. delta = next_tick - basemono;
  588. if (delta <= (u64)TICK_NSEC) {
  589. tick = 0;
  590. /*
  591. * Tell the timer code that the base is not idle, i.e. undo
  592. * the effect of get_next_timer_interrupt():
  593. */
  594. timer_clear_idle();
  595. /*
  596. * We've not stopped the tick yet, and there's a timer in the
  597. * next period, so no point in stopping it either, bail.
  598. */
  599. if (!ts->tick_stopped)
  600. goto out;
  601. /*
  602. * If, OTOH, we did stop it, but there's a pending (expired)
  603. * timer reprogram the timer hardware to fire now.
  604. *
  605. * We will not restart the tick proper, just prod the timer
  606. * hardware into firing an interrupt to process the pending
  607. * timers. Just like tick_irq_exit() will not restart the tick
  608. * for 'normal' interrupts.
  609. *
  610. * Only once we exit the idle loop will we re-enable the tick,
  611. * see tick_nohz_idle_exit().
  612. */
  613. if (delta == 0) {
  614. tick_nohz_restart(ts, now);
  615. goto out;
  616. }
  617. }
  618. /*
  619. * If this CPU is the one which updates jiffies, then give up
  620. * the assignment and let it be taken by the CPU which runs
  621. * the tick timer next, which might be this CPU as well. If we
  622. * don't drop this here the jiffies might be stale and
  623. * do_timer() never invoked. Keep track of the fact that it
  624. * was the one which had the do_timer() duty last. If this CPU
  625. * is the one which had the do_timer() duty last, we limit the
  626. * sleep time to the timekeeping max_deferment value.
  627. * Otherwise we can sleep as long as we want.
  628. */
  629. delta = timekeeping_max_deferment();
  630. if (cpu == tick_do_timer_cpu) {
  631. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  632. ts->do_timer_last = 1;
  633. } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
  634. delta = KTIME_MAX;
  635. ts->do_timer_last = 0;
  636. } else if (!ts->do_timer_last) {
  637. delta = KTIME_MAX;
  638. }
  639. #ifdef CONFIG_NO_HZ_FULL
  640. /* Limit the tick delta to the maximum scheduler deferment */
  641. if (!ts->inidle)
  642. delta = min(delta, scheduler_tick_max_deferment());
  643. #endif
  644. /* Calculate the next expiry time */
  645. if (delta < (KTIME_MAX - basemono))
  646. expires = basemono + delta;
  647. else
  648. expires = KTIME_MAX;
  649. expires = min_t(u64, expires, next_tick);
  650. tick = expires;
  651. /* Skip reprogram of event if its not changed */
  652. if (ts->tick_stopped && (expires == ts->next_tick))
  653. goto out;
  654. /*
  655. * nohz_stop_sched_tick can be called several times before
  656. * the nohz_restart_sched_tick is called. This happens when
  657. * interrupts arrive which do not cause a reschedule. In the
  658. * first call we save the current tick time, so we can restart
  659. * the scheduler tick in nohz_restart_sched_tick.
  660. */
  661. if (!ts->tick_stopped) {
  662. nohz_balance_enter_idle(cpu);
  663. calc_load_enter_idle();
  664. cpu_load_update_nohz_start();
  665. ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
  666. ts->tick_stopped = 1;
  667. trace_tick_stop(1, TICK_DEP_MASK_NONE);
  668. }
  669. ts->next_tick = tick;
  670. /*
  671. * If the expiration time == KTIME_MAX, then we simply stop
  672. * the tick timer.
  673. */
  674. if (unlikely(expires == KTIME_MAX)) {
  675. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  676. hrtimer_cancel(&ts->sched_timer);
  677. goto out;
  678. }
  679. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  680. hrtimer_start(&ts->sched_timer, tick, HRTIMER_MODE_ABS_PINNED);
  681. else
  682. tick_program_event(tick, 1);
  683. out:
  684. /*
  685. * Update the estimated sleep length until the next timer
  686. * (not only the tick).
  687. */
  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 = 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 > 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 = 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. }