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