tick-sched.c 31 KB

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