tick-sched.c 31 KB

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