tick-sched.c 31 KB

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