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