tick-sched.c 32 KB

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