tick-sched.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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. bool tick_nohz_tick_stopped(void)
  408. {
  409. return __this_cpu_read(tick_cpu_sched.tick_stopped);
  410. }
  411. bool tick_nohz_tick_stopped_cpu(int cpu)
  412. {
  413. struct tick_sched *ts = per_cpu_ptr(&tick_cpu_sched, cpu);
  414. return ts->tick_stopped;
  415. }
  416. /**
  417. * tick_nohz_update_jiffies - update jiffies when idle was interrupted
  418. *
  419. * Called from interrupt entry when the CPU was idle
  420. *
  421. * In case the sched_tick was stopped on this CPU, we have to check if jiffies
  422. * must be updated. Otherwise an interrupt handler could use a stale jiffy
  423. * value. We do this unconditionally on any CPU, as we don't know whether the
  424. * CPU, which has the update task assigned is in a long sleep.
  425. */
  426. static void tick_nohz_update_jiffies(ktime_t now)
  427. {
  428. unsigned long flags;
  429. __this_cpu_write(tick_cpu_sched.idle_waketime, now);
  430. local_irq_save(flags);
  431. tick_do_update_jiffies64(now);
  432. local_irq_restore(flags);
  433. touch_softlockup_watchdog_sched();
  434. }
  435. /*
  436. * Updates the per-CPU time idle statistics counters
  437. */
  438. static void
  439. update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
  440. {
  441. ktime_t delta;
  442. if (ts->idle_active) {
  443. delta = ktime_sub(now, ts->idle_entrytime);
  444. if (nr_iowait_cpu(cpu) > 0)
  445. ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
  446. else
  447. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  448. ts->idle_entrytime = now;
  449. }
  450. if (last_update_time)
  451. *last_update_time = ktime_to_us(now);
  452. }
  453. static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now)
  454. {
  455. update_ts_time_stats(smp_processor_id(), ts, now, NULL);
  456. ts->idle_active = 0;
  457. sched_clock_idle_wakeup_event();
  458. }
  459. static ktime_t tick_nohz_start_idle(struct tick_sched *ts)
  460. {
  461. ktime_t now = ktime_get();
  462. ts->idle_entrytime = now;
  463. ts->idle_active = 1;
  464. sched_clock_idle_sleep_event();
  465. return now;
  466. }
  467. /**
  468. * get_cpu_idle_time_us - get the total idle time of a CPU
  469. * @cpu: CPU number to query
  470. * @last_update_time: variable to store update time in. Do not update
  471. * counters if NULL.
  472. *
  473. * Return the cumulative idle time (since boot) for a given
  474. * CPU, in microseconds.
  475. *
  476. * This time is measured via accounting rather than sampling,
  477. * and is as accurate as ktime_get() is.
  478. *
  479. * This function returns -1 if NOHZ is not enabled.
  480. */
  481. u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
  482. {
  483. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  484. ktime_t now, idle;
  485. if (!tick_nohz_active)
  486. return -1;
  487. now = ktime_get();
  488. if (last_update_time) {
  489. update_ts_time_stats(cpu, ts, now, last_update_time);
  490. idle = ts->idle_sleeptime;
  491. } else {
  492. if (ts->idle_active && !nr_iowait_cpu(cpu)) {
  493. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  494. idle = ktime_add(ts->idle_sleeptime, delta);
  495. } else {
  496. idle = ts->idle_sleeptime;
  497. }
  498. }
  499. return ktime_to_us(idle);
  500. }
  501. EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
  502. /**
  503. * get_cpu_iowait_time_us - get the total iowait time of a CPU
  504. * @cpu: CPU number to query
  505. * @last_update_time: variable to store update time in. Do not update
  506. * counters if NULL.
  507. *
  508. * Return the cumulative iowait time (since boot) for a given
  509. * CPU, in microseconds.
  510. *
  511. * This time is measured via accounting rather than sampling,
  512. * and is as accurate as ktime_get() is.
  513. *
  514. * This function returns -1 if NOHZ is not enabled.
  515. */
  516. u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
  517. {
  518. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  519. ktime_t now, iowait;
  520. if (!tick_nohz_active)
  521. return -1;
  522. now = ktime_get();
  523. if (last_update_time) {
  524. update_ts_time_stats(cpu, ts, now, last_update_time);
  525. iowait = ts->iowait_sleeptime;
  526. } else {
  527. if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
  528. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  529. iowait = ktime_add(ts->iowait_sleeptime, delta);
  530. } else {
  531. iowait = ts->iowait_sleeptime;
  532. }
  533. }
  534. return ktime_to_us(iowait);
  535. }
  536. EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
  537. static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
  538. {
  539. hrtimer_cancel(&ts->sched_timer);
  540. hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
  541. /* Forward the time to expire in the future */
  542. hrtimer_forward(&ts->sched_timer, now, tick_period);
  543. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  544. hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
  545. else
  546. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  547. /*
  548. * Reset to make sure next tick stop doesn't get fooled by past
  549. * cached clock deadline.
  550. */
  551. ts->next_tick = 0;
  552. }
  553. static inline bool local_timer_softirq_pending(void)
  554. {
  555. return local_softirq_pending() & TIMER_SOFTIRQ;
  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. /*
  572. * Keep the periodic tick, when RCU, architecture or irq_work
  573. * requests it.
  574. * Aside of that check whether the local timer softirq is
  575. * pending. If so its a bad idea to call get_next_timer_interrupt()
  576. * because there is an already expired timer, so it will request
  577. * immeditate expiry, which rearms the hardware timer with a
  578. * minimal delta which brings us back to this place
  579. * immediately. Lather, rinse and repeat...
  580. */
  581. if (rcu_needs_cpu(basemono, &next_rcu) || arch_needs_cpu() ||
  582. irq_work_needs_cpu() || local_timer_softirq_pending()) {
  583. next_tick = basemono + TICK_NSEC;
  584. } else {
  585. /*
  586. * Get the next pending timer. If high resolution
  587. * timers are enabled this only takes the timer wheel
  588. * timers into account. If high resolution timers are
  589. * disabled this also looks at the next expiring
  590. * hrtimer.
  591. */
  592. next_tmr = get_next_timer_interrupt(basejiff, basemono);
  593. ts->next_timer = next_tmr;
  594. /* Take the next rcu event into account */
  595. next_tick = next_rcu < next_tmr ? next_rcu : next_tmr;
  596. }
  597. /*
  598. * If the tick is due in the next period, keep it ticking or
  599. * force prod the timer.
  600. */
  601. delta = next_tick - basemono;
  602. if (delta <= (u64)TICK_NSEC) {
  603. /*
  604. * Tell the timer code that the base is not idle, i.e. undo
  605. * the effect of get_next_timer_interrupt():
  606. */
  607. timer_clear_idle();
  608. /*
  609. * We've not stopped the tick yet, and there's a timer in the
  610. * next period, so no point in stopping it either, bail.
  611. */
  612. if (!ts->tick_stopped) {
  613. tick = 0;
  614. goto out;
  615. }
  616. }
  617. /*
  618. * If this CPU is the one which updates jiffies, then give up
  619. * the assignment and let it be taken by the CPU which runs
  620. * the tick timer next, which might be this CPU as well. If we
  621. * don't drop this here the jiffies might be stale and
  622. * do_timer() never invoked. Keep track of the fact that it
  623. * was the one which had the do_timer() duty last. If this CPU
  624. * is the one which had the do_timer() duty last, we limit the
  625. * sleep time to the timekeeping max_deferment value.
  626. * Otherwise we can sleep as long as we want.
  627. */
  628. delta = timekeeping_max_deferment();
  629. if (cpu == tick_do_timer_cpu) {
  630. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  631. ts->do_timer_last = 1;
  632. } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
  633. delta = KTIME_MAX;
  634. ts->do_timer_last = 0;
  635. } else if (!ts->do_timer_last) {
  636. delta = KTIME_MAX;
  637. }
  638. /* Calculate the next expiry time */
  639. if (delta < (KTIME_MAX - basemono))
  640. expires = basemono + delta;
  641. else
  642. expires = KTIME_MAX;
  643. expires = min_t(u64, expires, next_tick);
  644. tick = expires;
  645. /* Skip reprogram of event if its not changed */
  646. if (ts->tick_stopped && (expires == ts->next_tick)) {
  647. /* Sanity check: make sure clockevent is actually programmed */
  648. if (tick == KTIME_MAX || ts->next_tick == hrtimer_get_expires(&ts->sched_timer))
  649. goto out;
  650. WARN_ON_ONCE(1);
  651. printk_once("basemono: %llu ts->next_tick: %llu dev->next_event: %llu timer->active: %d timer->expires: %llu\n",
  652. basemono, ts->next_tick, dev->next_event,
  653. hrtimer_active(&ts->sched_timer), hrtimer_get_expires(&ts->sched_timer));
  654. }
  655. /*
  656. * nohz_stop_sched_tick can be called several times before
  657. * the nohz_restart_sched_tick is called. This happens when
  658. * interrupts arrive which do not cause a reschedule. In the
  659. * first call we save the current tick time, so we can restart
  660. * the scheduler tick in nohz_restart_sched_tick.
  661. */
  662. if (!ts->tick_stopped) {
  663. calc_load_nohz_start();
  664. cpu_load_update_nohz_start();
  665. quiet_vmstat();
  666. ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
  667. ts->tick_stopped = 1;
  668. trace_tick_stop(1, TICK_DEP_MASK_NONE);
  669. }
  670. ts->next_tick = tick;
  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. hrtimer_set_expires(&ts->sched_timer, tick);
  681. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  682. hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
  683. else
  684. tick_program_event(tick, 1);
  685. out:
  686. /*
  687. * Update the estimated sleep length until the next timer
  688. * (not only the tick).
  689. */
  690. ts->sleep_length = ktime_sub(dev->next_event, now);
  691. return tick;
  692. }
  693. static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
  694. {
  695. /* Update jiffies first */
  696. tick_do_update_jiffies64(now);
  697. cpu_load_update_nohz_stop();
  698. /*
  699. * Clear the timer idle flag, so we avoid IPIs on remote queueing and
  700. * the clock forward checks in the enqueue path:
  701. */
  702. timer_clear_idle();
  703. calc_load_nohz_stop();
  704. touch_softlockup_watchdog_sched();
  705. /*
  706. * Cancel the scheduled timer and restore the tick
  707. */
  708. ts->tick_stopped = 0;
  709. ts->idle_exittime = now;
  710. tick_nohz_restart(ts, now);
  711. }
  712. static void tick_nohz_full_update_tick(struct tick_sched *ts)
  713. {
  714. #ifdef CONFIG_NO_HZ_FULL
  715. int cpu = smp_processor_id();
  716. if (!tick_nohz_full_cpu(cpu))
  717. return;
  718. if (!ts->tick_stopped && ts->nohz_mode == NOHZ_MODE_INACTIVE)
  719. return;
  720. if (can_stop_full_tick(cpu, ts))
  721. tick_nohz_stop_sched_tick(ts, ktime_get(), cpu);
  722. else if (ts->tick_stopped)
  723. tick_nohz_restart_sched_tick(ts, ktime_get());
  724. #endif
  725. }
  726. static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
  727. {
  728. /*
  729. * If this CPU is offline and it is the one which updates
  730. * jiffies, then give up the assignment and let it be taken by
  731. * the CPU which runs the tick timer next. If we don't drop
  732. * this here the jiffies might be stale and do_timer() never
  733. * invoked.
  734. */
  735. if (unlikely(!cpu_online(cpu))) {
  736. if (cpu == tick_do_timer_cpu)
  737. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  738. /*
  739. * Make sure the CPU doesn't get fooled by obsolete tick
  740. * deadline if it comes back online later.
  741. */
  742. ts->next_tick = 0;
  743. return false;
  744. }
  745. if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE)) {
  746. ts->sleep_length = NSEC_PER_SEC / HZ;
  747. return false;
  748. }
  749. if (need_resched())
  750. return false;
  751. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  752. static int ratelimit;
  753. if (ratelimit < 10 &&
  754. (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  755. pr_warn("NOHZ: local_softirq_pending %02x\n",
  756. (unsigned int) local_softirq_pending());
  757. ratelimit++;
  758. }
  759. return false;
  760. }
  761. if (tick_nohz_full_enabled()) {
  762. /*
  763. * Keep the tick alive to guarantee timekeeping progression
  764. * if there are full dynticks CPUs around
  765. */
  766. if (tick_do_timer_cpu == cpu)
  767. return false;
  768. /*
  769. * Boot safety: make sure the timekeeping duty has been
  770. * assigned before entering dyntick-idle mode,
  771. */
  772. if (tick_do_timer_cpu == TICK_DO_TIMER_NONE)
  773. return false;
  774. }
  775. return true;
  776. }
  777. static void __tick_nohz_idle_enter(struct tick_sched *ts)
  778. {
  779. ktime_t now, expires;
  780. int cpu = smp_processor_id();
  781. now = tick_nohz_start_idle(ts);
  782. if (can_stop_idle_tick(cpu, ts)) {
  783. int was_stopped = ts->tick_stopped;
  784. ts->idle_calls++;
  785. expires = tick_nohz_stop_sched_tick(ts, now, cpu);
  786. if (expires > 0LL) {
  787. ts->idle_sleeps++;
  788. ts->idle_expires = expires;
  789. }
  790. if (!was_stopped && ts->tick_stopped) {
  791. ts->idle_jiffies = ts->last_jiffies;
  792. nohz_balance_enter_idle(cpu);
  793. }
  794. }
  795. }
  796. /**
  797. * tick_nohz_idle_enter - stop the idle tick from the idle task
  798. *
  799. * When the next event is more than a tick into the future, stop the idle tick
  800. * Called when we start the idle loop.
  801. *
  802. * The arch is responsible of calling:
  803. *
  804. * - rcu_idle_enter() after its last use of RCU before the CPU is put
  805. * to sleep.
  806. * - rcu_idle_exit() before the first use of RCU after the CPU is woken up.
  807. */
  808. void tick_nohz_idle_enter(void)
  809. {
  810. struct tick_sched *ts;
  811. lockdep_assert_irqs_enabled();
  812. /*
  813. * Update the idle state in the scheduler domain hierarchy
  814. * when tick_nohz_stop_sched_tick() is called from the idle loop.
  815. * State will be updated to busy during the first busy tick after
  816. * exiting idle.
  817. */
  818. set_cpu_sd_state_idle();
  819. local_irq_disable();
  820. ts = this_cpu_ptr(&tick_cpu_sched);
  821. ts->inidle = 1;
  822. __tick_nohz_idle_enter(ts);
  823. local_irq_enable();
  824. }
  825. /**
  826. * tick_nohz_irq_exit - update next tick event from interrupt exit
  827. *
  828. * When an interrupt fires while we are idle and it doesn't cause
  829. * a reschedule, it may still add, modify or delete a timer, enqueue
  830. * an RCU callback, etc...
  831. * So we need to re-calculate and reprogram the next tick event.
  832. */
  833. void tick_nohz_irq_exit(void)
  834. {
  835. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  836. if (ts->inidle)
  837. __tick_nohz_idle_enter(ts);
  838. else
  839. tick_nohz_full_update_tick(ts);
  840. }
  841. /**
  842. * tick_nohz_get_sleep_length - return the length of the current sleep
  843. *
  844. * Called from power state control code with interrupts disabled
  845. */
  846. ktime_t tick_nohz_get_sleep_length(void)
  847. {
  848. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  849. return ts->sleep_length;
  850. }
  851. /**
  852. * tick_nohz_get_idle_calls_cpu - return the current idle calls counter value
  853. * for a particular CPU.
  854. *
  855. * Called from the schedutil frequency scaling governor in scheduler context.
  856. */
  857. unsigned long tick_nohz_get_idle_calls_cpu(int cpu)
  858. {
  859. struct tick_sched *ts = tick_get_tick_sched(cpu);
  860. return ts->idle_calls;
  861. }
  862. /**
  863. * tick_nohz_get_idle_calls - return the current idle calls counter value
  864. *
  865. * Called from the schedutil frequency scaling governor in scheduler context.
  866. */
  867. unsigned long tick_nohz_get_idle_calls(void)
  868. {
  869. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  870. return ts->idle_calls;
  871. }
  872. static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
  873. {
  874. #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  875. unsigned long ticks;
  876. if (vtime_accounting_cpu_enabled())
  877. return;
  878. /*
  879. * We stopped the tick in idle. Update process times would miss the
  880. * time we slept as update_process_times does only a 1 tick
  881. * accounting. Enforce that this is accounted to idle !
  882. */
  883. ticks = jiffies - ts->idle_jiffies;
  884. /*
  885. * We might be one off. Do not randomly account a huge number of ticks!
  886. */
  887. if (ticks && ticks < LONG_MAX)
  888. account_idle_ticks(ticks);
  889. #endif
  890. }
  891. /**
  892. * tick_nohz_idle_exit - restart the idle tick from the idle task
  893. *
  894. * Restart the idle tick when the CPU is woken up from idle
  895. * This also exit the RCU extended quiescent state. The CPU
  896. * can use RCU again after this function is called.
  897. */
  898. void tick_nohz_idle_exit(void)
  899. {
  900. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  901. ktime_t now;
  902. local_irq_disable();
  903. WARN_ON_ONCE(!ts->inidle);
  904. ts->inidle = 0;
  905. if (ts->idle_active || ts->tick_stopped)
  906. now = ktime_get();
  907. if (ts->idle_active)
  908. tick_nohz_stop_idle(ts, now);
  909. if (ts->tick_stopped) {
  910. tick_nohz_restart_sched_tick(ts, now);
  911. tick_nohz_account_idle_ticks(ts);
  912. }
  913. local_irq_enable();
  914. }
  915. /*
  916. * The nohz low res interrupt handler
  917. */
  918. static void tick_nohz_handler(struct clock_event_device *dev)
  919. {
  920. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  921. struct pt_regs *regs = get_irq_regs();
  922. ktime_t now = ktime_get();
  923. dev->next_event = KTIME_MAX;
  924. tick_sched_do_timer(now);
  925. tick_sched_handle(ts, regs);
  926. /* No need to reprogram if we are running tickless */
  927. if (unlikely(ts->tick_stopped))
  928. return;
  929. hrtimer_forward(&ts->sched_timer, now, tick_period);
  930. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  931. }
  932. static inline void tick_nohz_activate(struct tick_sched *ts, int mode)
  933. {
  934. if (!tick_nohz_enabled)
  935. return;
  936. ts->nohz_mode = mode;
  937. /* One update is enough */
  938. if (!test_and_set_bit(0, &tick_nohz_active))
  939. timers_update_nohz();
  940. }
  941. /**
  942. * tick_nohz_switch_to_nohz - switch to nohz mode
  943. */
  944. static void tick_nohz_switch_to_nohz(void)
  945. {
  946. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  947. ktime_t next;
  948. if (!tick_nohz_enabled)
  949. return;
  950. if (tick_switch_to_oneshot(tick_nohz_handler))
  951. return;
  952. /*
  953. * Recycle the hrtimer in ts, so we can share the
  954. * hrtimer_forward with the highres code.
  955. */
  956. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  957. /* Get the next period */
  958. next = tick_init_jiffy_update();
  959. hrtimer_set_expires(&ts->sched_timer, next);
  960. hrtimer_forward_now(&ts->sched_timer, tick_period);
  961. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  962. tick_nohz_activate(ts, NOHZ_MODE_LOWRES);
  963. }
  964. static inline void tick_nohz_irq_enter(void)
  965. {
  966. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  967. ktime_t now;
  968. if (!ts->idle_active && !ts->tick_stopped)
  969. return;
  970. now = ktime_get();
  971. if (ts->idle_active)
  972. tick_nohz_stop_idle(ts, now);
  973. if (ts->tick_stopped)
  974. tick_nohz_update_jiffies(now);
  975. }
  976. #else
  977. static inline void tick_nohz_switch_to_nohz(void) { }
  978. static inline void tick_nohz_irq_enter(void) { }
  979. static inline void tick_nohz_activate(struct tick_sched *ts, int mode) { }
  980. #endif /* CONFIG_NO_HZ_COMMON */
  981. /*
  982. * Called from irq_enter to notify about the possible interruption of idle()
  983. */
  984. void tick_irq_enter(void)
  985. {
  986. tick_check_oneshot_broadcast_this_cpu();
  987. tick_nohz_irq_enter();
  988. }
  989. /*
  990. * High resolution timer specific code
  991. */
  992. #ifdef CONFIG_HIGH_RES_TIMERS
  993. /*
  994. * We rearm the timer until we get disabled by the idle code.
  995. * Called with interrupts disabled.
  996. */
  997. static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
  998. {
  999. struct tick_sched *ts =
  1000. container_of(timer, struct tick_sched, sched_timer);
  1001. struct pt_regs *regs = get_irq_regs();
  1002. ktime_t now = ktime_get();
  1003. tick_sched_do_timer(now);
  1004. /*
  1005. * Do not call, when we are not in irq context and have
  1006. * no valid regs pointer
  1007. */
  1008. if (regs)
  1009. tick_sched_handle(ts, regs);
  1010. else
  1011. ts->next_tick = 0;
  1012. /* No need to reprogram if we are in idle or full dynticks mode */
  1013. if (unlikely(ts->tick_stopped))
  1014. return HRTIMER_NORESTART;
  1015. hrtimer_forward(timer, now, tick_period);
  1016. return HRTIMER_RESTART;
  1017. }
  1018. static int sched_skew_tick;
  1019. static int __init skew_tick(char *str)
  1020. {
  1021. get_option(&str, &sched_skew_tick);
  1022. return 0;
  1023. }
  1024. early_param("skew_tick", skew_tick);
  1025. /**
  1026. * tick_setup_sched_timer - setup the tick emulation timer
  1027. */
  1028. void tick_setup_sched_timer(void)
  1029. {
  1030. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1031. ktime_t now = ktime_get();
  1032. /*
  1033. * Emulate tick processing via per-CPU hrtimers:
  1034. */
  1035. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1036. ts->sched_timer.function = tick_sched_timer;
  1037. /* Get the next period (per-CPU) */
  1038. hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
  1039. /* Offset the tick to avert jiffies_lock contention. */
  1040. if (sched_skew_tick) {
  1041. u64 offset = ktime_to_ns(tick_period) >> 1;
  1042. do_div(offset, num_possible_cpus());
  1043. offset *= smp_processor_id();
  1044. hrtimer_add_expires_ns(&ts->sched_timer, offset);
  1045. }
  1046. hrtimer_forward(&ts->sched_timer, now, tick_period);
  1047. hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
  1048. tick_nohz_activate(ts, NOHZ_MODE_HIGHRES);
  1049. }
  1050. #endif /* HIGH_RES_TIMERS */
  1051. #if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
  1052. void tick_cancel_sched_timer(int cpu)
  1053. {
  1054. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  1055. # ifdef CONFIG_HIGH_RES_TIMERS
  1056. if (ts->sched_timer.base)
  1057. hrtimer_cancel(&ts->sched_timer);
  1058. # endif
  1059. memset(ts, 0, sizeof(*ts));
  1060. }
  1061. #endif
  1062. /**
  1063. * Async notification about clocksource changes
  1064. */
  1065. void tick_clock_notify(void)
  1066. {
  1067. int cpu;
  1068. for_each_possible_cpu(cpu)
  1069. set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
  1070. }
  1071. /*
  1072. * Async notification about clock event changes
  1073. */
  1074. void tick_oneshot_notify(void)
  1075. {
  1076. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1077. set_bit(0, &ts->check_clocks);
  1078. }
  1079. /**
  1080. * Check, if a change happened, which makes oneshot possible.
  1081. *
  1082. * Called cyclic from the hrtimer softirq (driven by the timer
  1083. * softirq) allow_nohz signals, that we can switch into low-res nohz
  1084. * mode, because high resolution timers are disabled (either compile
  1085. * or runtime). Called with interrupts disabled.
  1086. */
  1087. int tick_check_oneshot_change(int allow_nohz)
  1088. {
  1089. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1090. if (!test_and_clear_bit(0, &ts->check_clocks))
  1091. return 0;
  1092. if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
  1093. return 0;
  1094. if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
  1095. return 0;
  1096. if (!allow_nohz)
  1097. return 1;
  1098. tick_nohz_switch_to_nohz();
  1099. return 0;
  1100. }