tick-sched.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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/nmi.h>
  21. #include <linux/profile.h>
  22. #include <linux/sched/signal.h>
  23. #include <linux/sched/clock.h>
  24. #include <linux/sched/stat.h>
  25. #include <linux/sched/nohz.h>
  26. #include <linux/module.h>
  27. #include <linux/irq_work.h>
  28. #include <linux/posix-timers.h>
  29. #include <linux/context_tracking.h>
  30. #include <asm/irq_regs.h>
  31. #include "tick-internal.h"
  32. #include <trace/events/timer.h>
  33. /*
  34. * Per-CPU nohz control structure
  35. */
  36. static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
  37. struct tick_sched *tick_get_tick_sched(int cpu)
  38. {
  39. return &per_cpu(tick_cpu_sched, cpu);
  40. }
  41. #if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
  42. /*
  43. * The time, when the last jiffy update happened. Protected by jiffies_lock.
  44. */
  45. static ktime_t last_jiffies_update;
  46. /*
  47. * Must be called with interrupts disabled !
  48. */
  49. static void tick_do_update_jiffies64(ktime_t now)
  50. {
  51. unsigned long ticks = 0;
  52. ktime_t delta;
  53. /*
  54. * Do a quick check without holding jiffies_lock:
  55. */
  56. delta = ktime_sub(now, last_jiffies_update);
  57. if (delta < tick_period)
  58. return;
  59. /* Reevaluate with jiffies_lock held */
  60. write_seqlock(&jiffies_lock);
  61. delta = ktime_sub(now, last_jiffies_update);
  62. if (delta >= tick_period) {
  63. delta = ktime_sub(delta, tick_period);
  64. last_jiffies_update = ktime_add(last_jiffies_update,
  65. tick_period);
  66. /* Slow path for long timeouts */
  67. if (unlikely(delta >= tick_period)) {
  68. s64 incr = ktime_to_ns(tick_period);
  69. ticks = ktime_divns(delta, incr);
  70. last_jiffies_update = ktime_add_ns(last_jiffies_update,
  71. incr * ticks);
  72. }
  73. do_timer(++ticks);
  74. /* Keep the tick_next_period variable up to date */
  75. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  76. } else {
  77. write_sequnlock(&jiffies_lock);
  78. return;
  79. }
  80. write_sequnlock(&jiffies_lock);
  81. update_wall_time();
  82. }
  83. /*
  84. * Initialize and return retrieve the jiffies update.
  85. */
  86. static ktime_t tick_init_jiffy_update(void)
  87. {
  88. ktime_t period;
  89. write_seqlock(&jiffies_lock);
  90. /* Did we start the jiffies update yet ? */
  91. if (last_jiffies_update == 0)
  92. last_jiffies_update = tick_next_period;
  93. period = last_jiffies_update;
  94. write_sequnlock(&jiffies_lock);
  95. return period;
  96. }
  97. static void tick_sched_do_timer(ktime_t now)
  98. {
  99. int cpu = smp_processor_id();
  100. #ifdef CONFIG_NO_HZ_COMMON
  101. /*
  102. * Check if the do_timer duty was dropped. We don't care about
  103. * concurrency: This happens only when the CPU in charge went
  104. * into a long sleep. If two CPUs happen to assign themselves to
  105. * this duty, then the jiffies update is still serialized by
  106. * jiffies_lock.
  107. */
  108. if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE)
  109. && !tick_nohz_full_cpu(cpu))
  110. tick_do_timer_cpu = cpu;
  111. #endif
  112. /* Check, if the jiffies need an update */
  113. if (tick_do_timer_cpu == cpu)
  114. tick_do_update_jiffies64(now);
  115. }
  116. static void tick_sched_handle(struct tick_sched *ts, struct pt_regs *regs)
  117. {
  118. #ifdef CONFIG_NO_HZ_COMMON
  119. /*
  120. * When we are idle and the tick is stopped, we have to touch
  121. * the watchdog as we might not schedule for a really long
  122. * time. This happens on complete idle SMP systems while
  123. * waiting on the login prompt. We also increment the "start of
  124. * idle" jiffy stamp so the idle accounting adjustment we do
  125. * when we go busy again does not account too much ticks.
  126. */
  127. if (ts->tick_stopped) {
  128. touch_softlockup_watchdog_sched();
  129. if (is_idle_task(current))
  130. ts->idle_jiffies++;
  131. }
  132. #endif
  133. update_process_times(user_mode(regs));
  134. profile_tick(CPU_PROFILING);
  135. }
  136. #endif
  137. #ifdef CONFIG_NO_HZ_FULL
  138. cpumask_var_t tick_nohz_full_mask;
  139. cpumask_var_t housekeeping_mask;
  140. bool tick_nohz_full_running;
  141. static atomic_t tick_dep_mask;
  142. static bool check_tick_dependency(atomic_t *dep)
  143. {
  144. int val = atomic_read(dep);
  145. if (val & TICK_DEP_MASK_POSIX_TIMER) {
  146. trace_tick_stop(0, TICK_DEP_MASK_POSIX_TIMER);
  147. return true;
  148. }
  149. if (val & TICK_DEP_MASK_PERF_EVENTS) {
  150. trace_tick_stop(0, TICK_DEP_MASK_PERF_EVENTS);
  151. return true;
  152. }
  153. if (val & TICK_DEP_MASK_SCHED) {
  154. trace_tick_stop(0, TICK_DEP_MASK_SCHED);
  155. return true;
  156. }
  157. if (val & TICK_DEP_MASK_CLOCK_UNSTABLE) {
  158. trace_tick_stop(0, TICK_DEP_MASK_CLOCK_UNSTABLE);
  159. return true;
  160. }
  161. return false;
  162. }
  163. static bool can_stop_full_tick(int cpu, struct tick_sched *ts)
  164. {
  165. WARN_ON_ONCE(!irqs_disabled());
  166. if (unlikely(!cpu_online(cpu)))
  167. return false;
  168. if (check_tick_dependency(&tick_dep_mask))
  169. return false;
  170. if (check_tick_dependency(&ts->tick_dep_mask))
  171. return false;
  172. if (check_tick_dependency(&current->tick_dep_mask))
  173. return false;
  174. if (check_tick_dependency(&current->signal->tick_dep_mask))
  175. return false;
  176. return true;
  177. }
  178. static void nohz_full_kick_func(struct irq_work *work)
  179. {
  180. /* Empty, the tick restart happens on tick_nohz_irq_exit() */
  181. }
  182. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  183. .func = nohz_full_kick_func,
  184. };
  185. /*
  186. * Kick this CPU if it's full dynticks in order to force it to
  187. * re-evaluate its dependency on the tick and restart it if necessary.
  188. * This kick, unlike tick_nohz_full_kick_cpu() and tick_nohz_full_kick_all(),
  189. * is NMI safe.
  190. */
  191. static void tick_nohz_full_kick(void)
  192. {
  193. if (!tick_nohz_full_cpu(smp_processor_id()))
  194. return;
  195. irq_work_queue(this_cpu_ptr(&nohz_full_kick_work));
  196. }
  197. /*
  198. * Kick the CPU if it's full dynticks in order to force it to
  199. * re-evaluate its dependency on the tick and restart it if necessary.
  200. */
  201. void tick_nohz_full_kick_cpu(int cpu)
  202. {
  203. if (!tick_nohz_full_cpu(cpu))
  204. return;
  205. irq_work_queue_on(&per_cpu(nohz_full_kick_work, cpu), cpu);
  206. }
  207. /*
  208. * Kick all full dynticks CPUs in order to force these to re-evaluate
  209. * their dependency on the tick and restart it if necessary.
  210. */
  211. static void tick_nohz_full_kick_all(void)
  212. {
  213. int cpu;
  214. if (!tick_nohz_full_running)
  215. return;
  216. preempt_disable();
  217. for_each_cpu_and(cpu, tick_nohz_full_mask, cpu_online_mask)
  218. tick_nohz_full_kick_cpu(cpu);
  219. preempt_enable();
  220. }
  221. static void tick_nohz_dep_set_all(atomic_t *dep,
  222. enum tick_dep_bits bit)
  223. {
  224. int prev;
  225. prev = atomic_fetch_or(BIT(bit), dep);
  226. if (!prev)
  227. tick_nohz_full_kick_all();
  228. }
  229. /*
  230. * Set a global tick dependency. Used by perf events that rely on freq and
  231. * by unstable clock.
  232. */
  233. void tick_nohz_dep_set(enum tick_dep_bits bit)
  234. {
  235. tick_nohz_dep_set_all(&tick_dep_mask, bit);
  236. }
  237. void tick_nohz_dep_clear(enum tick_dep_bits bit)
  238. {
  239. atomic_andnot(BIT(bit), &tick_dep_mask);
  240. }
  241. /*
  242. * Set per-CPU tick dependency. Used by scheduler and perf events in order to
  243. * manage events throttling.
  244. */
  245. void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit)
  246. {
  247. int prev;
  248. struct tick_sched *ts;
  249. ts = per_cpu_ptr(&tick_cpu_sched, cpu);
  250. prev = atomic_fetch_or(BIT(bit), &ts->tick_dep_mask);
  251. if (!prev) {
  252. preempt_disable();
  253. /* Perf needs local kick that is NMI safe */
  254. if (cpu == smp_processor_id()) {
  255. tick_nohz_full_kick();
  256. } else {
  257. /* Remote irq work not NMI-safe */
  258. if (!WARN_ON_ONCE(in_nmi()))
  259. tick_nohz_full_kick_cpu(cpu);
  260. }
  261. preempt_enable();
  262. }
  263. }
  264. void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit)
  265. {
  266. struct tick_sched *ts = per_cpu_ptr(&tick_cpu_sched, cpu);
  267. atomic_andnot(BIT(bit), &ts->tick_dep_mask);
  268. }
  269. /*
  270. * Set a per-task tick dependency. Posix CPU timers need this in order to elapse
  271. * per task timers.
  272. */
  273. void tick_nohz_dep_set_task(struct task_struct *tsk, enum tick_dep_bits bit)
  274. {
  275. /*
  276. * We could optimize this with just kicking the target running the task
  277. * if that noise matters for nohz full users.
  278. */
  279. tick_nohz_dep_set_all(&tsk->tick_dep_mask, bit);
  280. }
  281. void tick_nohz_dep_clear_task(struct task_struct *tsk, enum tick_dep_bits bit)
  282. {
  283. atomic_andnot(BIT(bit), &tsk->tick_dep_mask);
  284. }
  285. /*
  286. * Set a per-taskgroup tick dependency. Posix CPU timers need this in order to elapse
  287. * per process timers.
  288. */
  289. void tick_nohz_dep_set_signal(struct signal_struct *sig, enum tick_dep_bits bit)
  290. {
  291. tick_nohz_dep_set_all(&sig->tick_dep_mask, bit);
  292. }
  293. void tick_nohz_dep_clear_signal(struct signal_struct *sig, enum tick_dep_bits bit)
  294. {
  295. atomic_andnot(BIT(bit), &sig->tick_dep_mask);
  296. }
  297. /*
  298. * Re-evaluate the need for the tick as we switch the current task.
  299. * It might need the tick due to per task/process properties:
  300. * perf events, posix CPU timers, ...
  301. */
  302. void __tick_nohz_task_switch(void)
  303. {
  304. unsigned long flags;
  305. struct tick_sched *ts;
  306. local_irq_save(flags);
  307. if (!tick_nohz_full_cpu(smp_processor_id()))
  308. goto out;
  309. ts = this_cpu_ptr(&tick_cpu_sched);
  310. if (ts->tick_stopped) {
  311. if (atomic_read(&current->tick_dep_mask) ||
  312. atomic_read(&current->signal->tick_dep_mask))
  313. tick_nohz_full_kick();
  314. }
  315. out:
  316. local_irq_restore(flags);
  317. }
  318. /* Parse the boot-time nohz CPU list from the kernel parameters. */
  319. static int __init tick_nohz_full_setup(char *str)
  320. {
  321. alloc_bootmem_cpumask_var(&tick_nohz_full_mask);
  322. if (cpulist_parse(str, tick_nohz_full_mask) < 0) {
  323. pr_warn("NO_HZ: Incorrect nohz_full cpumask\n");
  324. free_bootmem_cpumask_var(tick_nohz_full_mask);
  325. return 1;
  326. }
  327. tick_nohz_full_running = true;
  328. return 1;
  329. }
  330. __setup("nohz_full=", tick_nohz_full_setup);
  331. static int tick_nohz_cpu_down(unsigned int cpu)
  332. {
  333. /*
  334. * The boot CPU handles housekeeping duty (unbound timers,
  335. * workqueues, timekeeping, ...) on behalf of full dynticks
  336. * CPUs. It must remain online when nohz full is enabled.
  337. */
  338. if (tick_nohz_full_running && tick_do_timer_cpu == cpu)
  339. return -EBUSY;
  340. return 0;
  341. }
  342. static int tick_nohz_init_all(void)
  343. {
  344. int err = -1;
  345. #ifdef CONFIG_NO_HZ_FULL_ALL
  346. if (!alloc_cpumask_var(&tick_nohz_full_mask, GFP_KERNEL)) {
  347. WARN(1, "NO_HZ: Can't allocate full dynticks cpumask\n");
  348. return err;
  349. }
  350. err = 0;
  351. cpumask_setall(tick_nohz_full_mask);
  352. tick_nohz_full_running = true;
  353. #endif
  354. return err;
  355. }
  356. void __init tick_nohz_init(void)
  357. {
  358. int cpu, ret;
  359. if (!tick_nohz_full_running) {
  360. if (tick_nohz_init_all() < 0)
  361. return;
  362. }
  363. if (!alloc_cpumask_var(&housekeeping_mask, GFP_KERNEL)) {
  364. WARN(1, "NO_HZ: Can't allocate not-full dynticks cpumask\n");
  365. cpumask_clear(tick_nohz_full_mask);
  366. tick_nohz_full_running = false;
  367. return;
  368. }
  369. /*
  370. * Full dynticks uses irq work to drive the tick rescheduling on safe
  371. * locking contexts. But then we need irq work to raise its own
  372. * interrupts to avoid circular dependency on the tick
  373. */
  374. if (!arch_irq_work_has_interrupt()) {
  375. pr_warn("NO_HZ: Can't run full dynticks because arch doesn't support irq work self-IPIs\n");
  376. cpumask_clear(tick_nohz_full_mask);
  377. cpumask_copy(housekeeping_mask, cpu_possible_mask);
  378. tick_nohz_full_running = false;
  379. return;
  380. }
  381. cpu = smp_processor_id();
  382. if (cpumask_test_cpu(cpu, tick_nohz_full_mask)) {
  383. pr_warn("NO_HZ: Clearing %d from nohz_full range for timekeeping\n",
  384. cpu);
  385. cpumask_clear_cpu(cpu, tick_nohz_full_mask);
  386. }
  387. cpumask_andnot(housekeeping_mask,
  388. cpu_possible_mask, tick_nohz_full_mask);
  389. for_each_cpu(cpu, tick_nohz_full_mask)
  390. context_tracking_cpu_set(cpu);
  391. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  392. "kernel/nohz:predown", NULL,
  393. tick_nohz_cpu_down);
  394. WARN_ON(ret < 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;
  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 = 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 = expires;
  655. /* Skip reprogram of event if its not changed */
  656. if (ts->tick_stopped && (expires == dev->next_event))
  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 = 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. }