tick-sched.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright(C) 2005-2006, Thomas Gleixner <[email protected]>
  4. * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
  5. * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
  6. *
  7. * No idle tick implementation for low and high resolution timers
  8. *
  9. * Started by: Thomas Gleixner and Ingo Molnar
  10. */
  11. #include <linux/cpu.h>
  12. #include <linux/err.h>
  13. #include <linux/hrtimer.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel_stat.h>
  16. #include <linux/percpu.h>
  17. #include <linux/nmi.h>
  18. #include <linux/profile.h>
  19. #include <linux/sched/signal.h>
  20. #include <linux/sched/clock.h>
  21. #include <linux/sched/stat.h>
  22. #include <linux/sched/nohz.h>
  23. #include <linux/sched/loadavg.h>
  24. #include <linux/module.h>
  25. #include <linux/irq_work.h>
  26. #include <linux/posix-timers.h>
  27. #include <linux/context_tracking.h>
  28. #include <linux/mm.h>
  29. #include <trace/hooks/sched.h>
  30. #include <asm/irq_regs.h>
  31. #include "tick-internal.h"
  32. #include <trace/events/timer.h>
  33. /*
  34. * Per-CPU nohz control structure
  35. */
  36. static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
  37. struct tick_sched *tick_get_tick_sched(int cpu)
  38. {
  39. return &per_cpu(tick_cpu_sched, cpu);
  40. }
  41. #if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
  42. /*
  43. * The time, when the last jiffy update happened. Write access must hold
  44. * jiffies_lock and jiffies_seq. tick_nohz_next_event() needs to get a
  45. * consistent view of jiffies and last_jiffies_update.
  46. */
  47. static ktime_t last_jiffies_update;
  48. /*
  49. * Must be called with interrupts disabled !
  50. */
  51. static void tick_do_update_jiffies64(ktime_t now)
  52. {
  53. unsigned long ticks = 1;
  54. ktime_t delta, nextp;
  55. /*
  56. * 64bit can do a quick check without holding jiffies lock and
  57. * without looking at the sequence count. The smp_load_acquire()
  58. * pairs with the update done later in this function.
  59. *
  60. * 32bit cannot do that because the store of tick_next_period
  61. * consists of two 32bit stores and the first store could move it
  62. * to a random point in the future.
  63. */
  64. if (IS_ENABLED(CONFIG_64BIT)) {
  65. if (ktime_before(now, smp_load_acquire(&tick_next_period)))
  66. return;
  67. } else {
  68. unsigned int seq;
  69. /*
  70. * Avoid contention on jiffies_lock and protect the quick
  71. * check with the sequence count.
  72. */
  73. do {
  74. seq = read_seqcount_begin(&jiffies_seq);
  75. nextp = tick_next_period;
  76. } while (read_seqcount_retry(&jiffies_seq, seq));
  77. if (ktime_before(now, nextp))
  78. return;
  79. }
  80. /* Quick check failed, i.e. update is required. */
  81. raw_spin_lock(&jiffies_lock);
  82. /*
  83. * Reevaluate with the lock held. Another CPU might have done the
  84. * update already.
  85. */
  86. if (ktime_before(now, tick_next_period)) {
  87. raw_spin_unlock(&jiffies_lock);
  88. return;
  89. }
  90. write_seqcount_begin(&jiffies_seq);
  91. delta = ktime_sub(now, tick_next_period);
  92. if (unlikely(delta >= TICK_NSEC)) {
  93. /* Slow path for long idle sleep times */
  94. s64 incr = TICK_NSEC;
  95. ticks += ktime_divns(delta, incr);
  96. last_jiffies_update = ktime_add_ns(last_jiffies_update,
  97. incr * ticks);
  98. } else {
  99. last_jiffies_update = ktime_add_ns(last_jiffies_update,
  100. TICK_NSEC);
  101. }
  102. /* Advance jiffies to complete the jiffies_seq protected job */
  103. jiffies_64 += ticks;
  104. /*
  105. * Keep the tick_next_period variable up to date.
  106. */
  107. nextp = ktime_add_ns(last_jiffies_update, TICK_NSEC);
  108. if (IS_ENABLED(CONFIG_64BIT)) {
  109. /*
  110. * Pairs with smp_load_acquire() in the lockless quick
  111. * check above and ensures that the update to jiffies_64 is
  112. * not reordered vs. the store to tick_next_period, neither
  113. * by the compiler nor by the CPU.
  114. */
  115. smp_store_release(&tick_next_period, nextp);
  116. } else {
  117. /*
  118. * A plain store is good enough on 32bit as the quick check
  119. * above is protected by the sequence count.
  120. */
  121. tick_next_period = nextp;
  122. }
  123. /*
  124. * Release the sequence count. calc_global_load() below is not
  125. * protected by it, but jiffies_lock needs to be held to prevent
  126. * concurrent invocations.
  127. */
  128. write_seqcount_end(&jiffies_seq);
  129. calc_global_load();
  130. raw_spin_unlock(&jiffies_lock);
  131. update_wall_time();
  132. }
  133. /*
  134. * Initialize and return retrieve the jiffies update.
  135. */
  136. static ktime_t tick_init_jiffy_update(void)
  137. {
  138. ktime_t period;
  139. raw_spin_lock(&jiffies_lock);
  140. write_seqcount_begin(&jiffies_seq);
  141. /* Did we start the jiffies update yet ? */
  142. if (last_jiffies_update == 0) {
  143. u32 rem;
  144. /*
  145. * Ensure that the tick is aligned to a multiple of
  146. * TICK_NSEC.
  147. */
  148. div_u64_rem(tick_next_period, TICK_NSEC, &rem);
  149. if (rem)
  150. tick_next_period += TICK_NSEC - rem;
  151. last_jiffies_update = tick_next_period;
  152. }
  153. period = last_jiffies_update;
  154. write_seqcount_end(&jiffies_seq);
  155. raw_spin_unlock(&jiffies_lock);
  156. return period;
  157. }
  158. #define MAX_STALLED_JIFFIES 5
  159. static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
  160. {
  161. int cpu = smp_processor_id();
  162. #ifdef CONFIG_NO_HZ_COMMON
  163. /*
  164. * Check if the do_timer duty was dropped. We don't care about
  165. * concurrency: This happens only when the CPU in charge went
  166. * into a long sleep. If two CPUs happen to assign themselves to
  167. * this duty, then the jiffies update is still serialized by
  168. * jiffies_lock.
  169. *
  170. * If nohz_full is enabled, this should not happen because the
  171. * tick_do_timer_cpu never relinquishes.
  172. */
  173. if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE)) {
  174. #ifdef CONFIG_NO_HZ_FULL
  175. WARN_ON_ONCE(tick_nohz_full_running);
  176. #endif
  177. tick_do_timer_cpu = cpu;
  178. }
  179. #endif
  180. /* Check, if the jiffies need an update */
  181. if (tick_do_timer_cpu == cpu) {
  182. tick_do_update_jiffies64(now);
  183. trace_android_vh_jiffies_update(NULL);
  184. }
  185. /*
  186. * If jiffies update stalled for too long (timekeeper in stop_machine()
  187. * or VMEXIT'ed for several msecs), force an update.
  188. */
  189. if (ts->last_tick_jiffies != jiffies) {
  190. ts->stalled_jiffies = 0;
  191. ts->last_tick_jiffies = READ_ONCE(jiffies);
  192. } else {
  193. if (++ts->stalled_jiffies == MAX_STALLED_JIFFIES) {
  194. tick_do_update_jiffies64(now);
  195. ts->stalled_jiffies = 0;
  196. ts->last_tick_jiffies = READ_ONCE(jiffies);
  197. }
  198. }
  199. if (ts->inidle)
  200. ts->got_idle_tick = 1;
  201. }
  202. static void tick_sched_handle(struct tick_sched *ts, struct pt_regs *regs)
  203. {
  204. #ifdef CONFIG_NO_HZ_COMMON
  205. /*
  206. * When we are idle and the tick is stopped, we have to touch
  207. * the watchdog as we might not schedule for a really long
  208. * time. This happens on complete idle SMP systems while
  209. * waiting on the login prompt. We also increment the "start of
  210. * idle" jiffy stamp so the idle accounting adjustment we do
  211. * when we go busy again does not account too much ticks.
  212. */
  213. if (ts->tick_stopped) {
  214. touch_softlockup_watchdog_sched();
  215. if (is_idle_task(current))
  216. ts->idle_jiffies++;
  217. /*
  218. * In case the current tick fired too early past its expected
  219. * expiration, make sure we don't bypass the next clock reprogramming
  220. * to the same deadline.
  221. */
  222. ts->next_tick = 0;
  223. }
  224. #endif
  225. update_process_times(user_mode(regs));
  226. profile_tick(CPU_PROFILING);
  227. }
  228. #endif
  229. #ifdef CONFIG_NO_HZ_FULL
  230. cpumask_var_t tick_nohz_full_mask;
  231. EXPORT_SYMBOL_GPL(tick_nohz_full_mask);
  232. bool tick_nohz_full_running;
  233. EXPORT_SYMBOL_GPL(tick_nohz_full_running);
  234. static atomic_t tick_dep_mask;
  235. static bool check_tick_dependency(atomic_t *dep)
  236. {
  237. int val = atomic_read(dep);
  238. if (val & TICK_DEP_MASK_POSIX_TIMER) {
  239. trace_tick_stop(0, TICK_DEP_MASK_POSIX_TIMER);
  240. return true;
  241. }
  242. if (val & TICK_DEP_MASK_PERF_EVENTS) {
  243. trace_tick_stop(0, TICK_DEP_MASK_PERF_EVENTS);
  244. return true;
  245. }
  246. if (val & TICK_DEP_MASK_SCHED) {
  247. trace_tick_stop(0, TICK_DEP_MASK_SCHED);
  248. return true;
  249. }
  250. if (val & TICK_DEP_MASK_CLOCK_UNSTABLE) {
  251. trace_tick_stop(0, TICK_DEP_MASK_CLOCK_UNSTABLE);
  252. return true;
  253. }
  254. if (val & TICK_DEP_MASK_RCU) {
  255. trace_tick_stop(0, TICK_DEP_MASK_RCU);
  256. return true;
  257. }
  258. if (val & TICK_DEP_MASK_RCU_EXP) {
  259. trace_tick_stop(0, TICK_DEP_MASK_RCU_EXP);
  260. return true;
  261. }
  262. return false;
  263. }
  264. static bool can_stop_full_tick(int cpu, struct tick_sched *ts)
  265. {
  266. lockdep_assert_irqs_disabled();
  267. if (unlikely(!cpu_online(cpu)))
  268. return false;
  269. if (check_tick_dependency(&tick_dep_mask))
  270. return false;
  271. if (check_tick_dependency(&ts->tick_dep_mask))
  272. return false;
  273. if (check_tick_dependency(&current->tick_dep_mask))
  274. return false;
  275. if (check_tick_dependency(&current->signal->tick_dep_mask))
  276. return false;
  277. return true;
  278. }
  279. static void nohz_full_kick_func(struct irq_work *work)
  280. {
  281. /* Empty, the tick restart happens on tick_nohz_irq_exit() */
  282. }
  283. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) =
  284. IRQ_WORK_INIT_HARD(nohz_full_kick_func);
  285. /*
  286. * Kick this CPU if it's full dynticks in order to force it to
  287. * re-evaluate its dependency on the tick and restart it if necessary.
  288. * This kick, unlike tick_nohz_full_kick_cpu() and tick_nohz_full_kick_all(),
  289. * is NMI safe.
  290. */
  291. static void tick_nohz_full_kick(void)
  292. {
  293. if (!tick_nohz_full_cpu(smp_processor_id()))
  294. return;
  295. irq_work_queue(this_cpu_ptr(&nohz_full_kick_work));
  296. }
  297. /*
  298. * Kick the CPU if it's full dynticks in order to force it to
  299. * re-evaluate its dependency on the tick and restart it if necessary.
  300. */
  301. void tick_nohz_full_kick_cpu(int cpu)
  302. {
  303. if (!tick_nohz_full_cpu(cpu))
  304. return;
  305. irq_work_queue_on(&per_cpu(nohz_full_kick_work, cpu), cpu);
  306. }
  307. static void tick_nohz_kick_task(struct task_struct *tsk)
  308. {
  309. int cpu;
  310. /*
  311. * If the task is not running, run_posix_cpu_timers()
  312. * has nothing to elapse, IPI can then be spared.
  313. *
  314. * activate_task() STORE p->tick_dep_mask
  315. * STORE p->on_rq
  316. * __schedule() (switch to task 'p') smp_mb() (atomic_fetch_or())
  317. * LOCK rq->lock LOAD p->on_rq
  318. * smp_mb__after_spin_lock()
  319. * tick_nohz_task_switch()
  320. * LOAD p->tick_dep_mask
  321. */
  322. if (!sched_task_on_rq(tsk))
  323. return;
  324. /*
  325. * If the task concurrently migrates to another CPU,
  326. * we guarantee it sees the new tick dependency upon
  327. * schedule.
  328. *
  329. * set_task_cpu(p, cpu);
  330. * STORE p->cpu = @cpu
  331. * __schedule() (switch to task 'p')
  332. * LOCK rq->lock
  333. * smp_mb__after_spin_lock() STORE p->tick_dep_mask
  334. * tick_nohz_task_switch() smp_mb() (atomic_fetch_or())
  335. * LOAD p->tick_dep_mask LOAD p->cpu
  336. */
  337. cpu = task_cpu(tsk);
  338. preempt_disable();
  339. if (cpu_online(cpu))
  340. tick_nohz_full_kick_cpu(cpu);
  341. preempt_enable();
  342. }
  343. /*
  344. * Kick all full dynticks CPUs in order to force these to re-evaluate
  345. * their dependency on the tick and restart it if necessary.
  346. */
  347. static void tick_nohz_full_kick_all(void)
  348. {
  349. int cpu;
  350. if (!tick_nohz_full_running)
  351. return;
  352. preempt_disable();
  353. for_each_cpu_and(cpu, tick_nohz_full_mask, cpu_online_mask)
  354. tick_nohz_full_kick_cpu(cpu);
  355. preempt_enable();
  356. }
  357. static void tick_nohz_dep_set_all(atomic_t *dep,
  358. enum tick_dep_bits bit)
  359. {
  360. int prev;
  361. prev = atomic_fetch_or(BIT(bit), dep);
  362. if (!prev)
  363. tick_nohz_full_kick_all();
  364. }
  365. /*
  366. * Set a global tick dependency. Used by perf events that rely on freq and
  367. * by unstable clock.
  368. */
  369. void tick_nohz_dep_set(enum tick_dep_bits bit)
  370. {
  371. tick_nohz_dep_set_all(&tick_dep_mask, bit);
  372. }
  373. void tick_nohz_dep_clear(enum tick_dep_bits bit)
  374. {
  375. atomic_andnot(BIT(bit), &tick_dep_mask);
  376. }
  377. /*
  378. * Set per-CPU tick dependency. Used by scheduler and perf events in order to
  379. * manage events throttling.
  380. */
  381. void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit)
  382. {
  383. int prev;
  384. struct tick_sched *ts;
  385. ts = per_cpu_ptr(&tick_cpu_sched, cpu);
  386. prev = atomic_fetch_or(BIT(bit), &ts->tick_dep_mask);
  387. if (!prev) {
  388. preempt_disable();
  389. /* Perf needs local kick that is NMI safe */
  390. if (cpu == smp_processor_id()) {
  391. tick_nohz_full_kick();
  392. } else {
  393. /* Remote irq work not NMI-safe */
  394. if (!WARN_ON_ONCE(in_nmi()))
  395. tick_nohz_full_kick_cpu(cpu);
  396. }
  397. preempt_enable();
  398. }
  399. }
  400. EXPORT_SYMBOL_GPL(tick_nohz_dep_set_cpu);
  401. void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit)
  402. {
  403. struct tick_sched *ts = per_cpu_ptr(&tick_cpu_sched, cpu);
  404. atomic_andnot(BIT(bit), &ts->tick_dep_mask);
  405. }
  406. EXPORT_SYMBOL_GPL(tick_nohz_dep_clear_cpu);
  407. /*
  408. * Set a per-task tick dependency. RCU need this. Also posix CPU timers
  409. * in order to elapse per task timers.
  410. */
  411. void tick_nohz_dep_set_task(struct task_struct *tsk, enum tick_dep_bits bit)
  412. {
  413. if (!atomic_fetch_or(BIT(bit), &tsk->tick_dep_mask))
  414. tick_nohz_kick_task(tsk);
  415. }
  416. EXPORT_SYMBOL_GPL(tick_nohz_dep_set_task);
  417. void tick_nohz_dep_clear_task(struct task_struct *tsk, enum tick_dep_bits bit)
  418. {
  419. atomic_andnot(BIT(bit), &tsk->tick_dep_mask);
  420. }
  421. EXPORT_SYMBOL_GPL(tick_nohz_dep_clear_task);
  422. /*
  423. * Set a per-taskgroup tick dependency. Posix CPU timers need this in order to elapse
  424. * per process timers.
  425. */
  426. void tick_nohz_dep_set_signal(struct task_struct *tsk,
  427. enum tick_dep_bits bit)
  428. {
  429. int prev;
  430. struct signal_struct *sig = tsk->signal;
  431. prev = atomic_fetch_or(BIT(bit), &sig->tick_dep_mask);
  432. if (!prev) {
  433. struct task_struct *t;
  434. lockdep_assert_held(&tsk->sighand->siglock);
  435. __for_each_thread(sig, t)
  436. tick_nohz_kick_task(t);
  437. }
  438. }
  439. void tick_nohz_dep_clear_signal(struct signal_struct *sig, enum tick_dep_bits bit)
  440. {
  441. atomic_andnot(BIT(bit), &sig->tick_dep_mask);
  442. }
  443. /*
  444. * Re-evaluate the need for the tick as we switch the current task.
  445. * It might need the tick due to per task/process properties:
  446. * perf events, posix CPU timers, ...
  447. */
  448. void __tick_nohz_task_switch(void)
  449. {
  450. struct tick_sched *ts;
  451. if (!tick_nohz_full_cpu(smp_processor_id()))
  452. return;
  453. ts = this_cpu_ptr(&tick_cpu_sched);
  454. if (ts->tick_stopped) {
  455. if (atomic_read(&current->tick_dep_mask) ||
  456. atomic_read(&current->signal->tick_dep_mask))
  457. tick_nohz_full_kick();
  458. }
  459. }
  460. /* Get the boot-time nohz CPU list from the kernel parameters. */
  461. void __init tick_nohz_full_setup(cpumask_var_t cpumask)
  462. {
  463. alloc_bootmem_cpumask_var(&tick_nohz_full_mask);
  464. cpumask_copy(tick_nohz_full_mask, cpumask);
  465. tick_nohz_full_running = true;
  466. }
  467. bool tick_nohz_cpu_hotpluggable(unsigned int cpu)
  468. {
  469. /*
  470. * The tick_do_timer_cpu CPU handles housekeeping duty (unbound
  471. * timers, workqueues, timekeeping, ...) on behalf of full dynticks
  472. * CPUs. It must remain online when nohz full is enabled.
  473. */
  474. if (tick_nohz_full_running && tick_do_timer_cpu == cpu)
  475. return false;
  476. return true;
  477. }
  478. static int tick_nohz_cpu_down(unsigned int cpu)
  479. {
  480. return tick_nohz_cpu_hotpluggable(cpu) ? 0 : -EBUSY;
  481. }
  482. void __init tick_nohz_init(void)
  483. {
  484. int cpu, ret;
  485. if (!tick_nohz_full_running)
  486. return;
  487. /*
  488. * Full dynticks uses irq work to drive the tick rescheduling on safe
  489. * locking contexts. But then we need irq work to raise its own
  490. * interrupts to avoid circular dependency on the tick
  491. */
  492. if (!arch_irq_work_has_interrupt()) {
  493. pr_warn("NO_HZ: Can't run full dynticks because arch doesn't support irq work self-IPIs\n");
  494. cpumask_clear(tick_nohz_full_mask);
  495. tick_nohz_full_running = false;
  496. return;
  497. }
  498. if (IS_ENABLED(CONFIG_PM_SLEEP_SMP) &&
  499. !IS_ENABLED(CONFIG_PM_SLEEP_SMP_NONZERO_CPU)) {
  500. cpu = smp_processor_id();
  501. if (cpumask_test_cpu(cpu, tick_nohz_full_mask)) {
  502. pr_warn("NO_HZ: Clearing %d from nohz_full range "
  503. "for timekeeping\n", cpu);
  504. cpumask_clear_cpu(cpu, tick_nohz_full_mask);
  505. }
  506. }
  507. for_each_cpu(cpu, tick_nohz_full_mask)
  508. ct_cpu_track_user(cpu);
  509. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  510. "kernel/nohz:predown", NULL,
  511. tick_nohz_cpu_down);
  512. WARN_ON(ret < 0);
  513. pr_info("NO_HZ: Full dynticks CPUs: %*pbl.\n",
  514. cpumask_pr_args(tick_nohz_full_mask));
  515. }
  516. #endif
  517. /*
  518. * NOHZ - aka dynamic tick functionality
  519. */
  520. #ifdef CONFIG_NO_HZ_COMMON
  521. /*
  522. * NO HZ enabled ?
  523. */
  524. bool tick_nohz_enabled __read_mostly = true;
  525. unsigned long tick_nohz_active __read_mostly;
  526. /*
  527. * Enable / Disable tickless mode
  528. */
  529. static int __init setup_tick_nohz(char *str)
  530. {
  531. return (kstrtobool(str, &tick_nohz_enabled) == 0);
  532. }
  533. __setup("nohz=", setup_tick_nohz);
  534. bool tick_nohz_tick_stopped(void)
  535. {
  536. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  537. return ts->tick_stopped;
  538. }
  539. bool tick_nohz_tick_stopped_cpu(int cpu)
  540. {
  541. struct tick_sched *ts = per_cpu_ptr(&tick_cpu_sched, cpu);
  542. return ts->tick_stopped;
  543. }
  544. /**
  545. * tick_nohz_update_jiffies - update jiffies when idle was interrupted
  546. *
  547. * Called from interrupt entry when the CPU was idle
  548. *
  549. * In case the sched_tick was stopped on this CPU, we have to check if jiffies
  550. * must be updated. Otherwise an interrupt handler could use a stale jiffy
  551. * value. We do this unconditionally on any CPU, as we don't know whether the
  552. * CPU, which has the update task assigned is in a long sleep.
  553. */
  554. static void tick_nohz_update_jiffies(ktime_t now)
  555. {
  556. unsigned long flags;
  557. __this_cpu_write(tick_cpu_sched.idle_waketime, now);
  558. local_irq_save(flags);
  559. tick_do_update_jiffies64(now);
  560. local_irq_restore(flags);
  561. touch_softlockup_watchdog_sched();
  562. }
  563. /*
  564. * Updates the per-CPU time idle statistics counters
  565. */
  566. static void
  567. update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
  568. {
  569. ktime_t delta;
  570. if (ts->idle_active) {
  571. delta = ktime_sub(now, ts->idle_entrytime);
  572. if (nr_iowait_cpu(cpu) > 0)
  573. ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
  574. else
  575. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  576. ts->idle_entrytime = now;
  577. }
  578. if (last_update_time)
  579. *last_update_time = ktime_to_us(now);
  580. }
  581. static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now)
  582. {
  583. update_ts_time_stats(smp_processor_id(), ts, now, NULL);
  584. ts->idle_active = 0;
  585. sched_clock_idle_wakeup_event();
  586. }
  587. static void tick_nohz_start_idle(struct tick_sched *ts)
  588. {
  589. ts->idle_entrytime = ktime_get();
  590. ts->idle_active = 1;
  591. sched_clock_idle_sleep_event();
  592. }
  593. /**
  594. * get_cpu_idle_time_us - get the total idle time of a CPU
  595. * @cpu: CPU number to query
  596. * @last_update_time: variable to store update time in. Do not update
  597. * counters if NULL.
  598. *
  599. * Return the cumulative idle time (since boot) for a given
  600. * CPU, in microseconds.
  601. *
  602. * This time is measured via accounting rather than sampling,
  603. * and is as accurate as ktime_get() is.
  604. *
  605. * This function returns -1 if NOHZ is not enabled.
  606. */
  607. u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
  608. {
  609. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  610. ktime_t now, idle;
  611. if (!tick_nohz_active)
  612. return -1;
  613. now = ktime_get();
  614. if (last_update_time) {
  615. update_ts_time_stats(cpu, ts, now, last_update_time);
  616. idle = ts->idle_sleeptime;
  617. } else {
  618. if (ts->idle_active && !nr_iowait_cpu(cpu)) {
  619. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  620. idle = ktime_add(ts->idle_sleeptime, delta);
  621. } else {
  622. idle = ts->idle_sleeptime;
  623. }
  624. }
  625. return ktime_to_us(idle);
  626. }
  627. EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
  628. /**
  629. * get_cpu_iowait_time_us - get the total iowait time of a CPU
  630. * @cpu: CPU number to query
  631. * @last_update_time: variable to store update time in. Do not update
  632. * counters if NULL.
  633. *
  634. * Return the cumulative iowait time (since boot) for a given
  635. * CPU, in microseconds.
  636. *
  637. * This time is measured via accounting rather than sampling,
  638. * and is as accurate as ktime_get() is.
  639. *
  640. * This function returns -1 if NOHZ is not enabled.
  641. */
  642. u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
  643. {
  644. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  645. ktime_t now, iowait;
  646. if (!tick_nohz_active)
  647. return -1;
  648. now = ktime_get();
  649. if (last_update_time) {
  650. update_ts_time_stats(cpu, ts, now, last_update_time);
  651. iowait = ts->iowait_sleeptime;
  652. } else {
  653. if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
  654. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  655. iowait = ktime_add(ts->iowait_sleeptime, delta);
  656. } else {
  657. iowait = ts->iowait_sleeptime;
  658. }
  659. }
  660. return ktime_to_us(iowait);
  661. }
  662. EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
  663. static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
  664. {
  665. hrtimer_cancel(&ts->sched_timer);
  666. hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
  667. /* Forward the time to expire in the future */
  668. hrtimer_forward(&ts->sched_timer, now, TICK_NSEC);
  669. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  670. hrtimer_start_expires(&ts->sched_timer,
  671. HRTIMER_MODE_ABS_PINNED_HARD);
  672. } else {
  673. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  674. }
  675. /*
  676. * Reset to make sure next tick stop doesn't get fooled by past
  677. * cached clock deadline.
  678. */
  679. ts->next_tick = 0;
  680. }
  681. static inline bool local_timer_softirq_pending(void)
  682. {
  683. return local_softirq_pending() & BIT(TIMER_SOFTIRQ);
  684. }
  685. static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
  686. {
  687. u64 basemono, next_tick, delta, expires;
  688. unsigned long basejiff;
  689. unsigned int seq;
  690. /* Read jiffies and the time when jiffies were updated last */
  691. do {
  692. seq = read_seqcount_begin(&jiffies_seq);
  693. basemono = last_jiffies_update;
  694. basejiff = jiffies;
  695. } while (read_seqcount_retry(&jiffies_seq, seq));
  696. ts->last_jiffies = basejiff;
  697. ts->timer_expires_base = basemono;
  698. /*
  699. * Keep the periodic tick, when RCU, architecture or irq_work
  700. * requests it.
  701. * Aside of that check whether the local timer softirq is
  702. * pending. If so its a bad idea to call get_next_timer_interrupt()
  703. * because there is an already expired timer, so it will request
  704. * immediate expiry, which rearms the hardware timer with a
  705. * minimal delta which brings us back to this place
  706. * immediately. Lather, rinse and repeat...
  707. */
  708. if (rcu_needs_cpu() || arch_needs_cpu() ||
  709. irq_work_needs_cpu() || local_timer_softirq_pending()) {
  710. next_tick = basemono + TICK_NSEC;
  711. } else {
  712. /*
  713. * Get the next pending timer. If high resolution
  714. * timers are enabled this only takes the timer wheel
  715. * timers into account. If high resolution timers are
  716. * disabled this also looks at the next expiring
  717. * hrtimer.
  718. */
  719. next_tick = get_next_timer_interrupt(basejiff, basemono);
  720. ts->next_timer = next_tick;
  721. }
  722. /*
  723. * If the tick is due in the next period, keep it ticking or
  724. * force prod the timer.
  725. */
  726. delta = next_tick - basemono;
  727. if (delta <= (u64)TICK_NSEC) {
  728. /*
  729. * Tell the timer code that the base is not idle, i.e. undo
  730. * the effect of get_next_timer_interrupt():
  731. */
  732. timer_clear_idle();
  733. /*
  734. * We've not stopped the tick yet, and there's a timer in the
  735. * next period, so no point in stopping it either, bail.
  736. */
  737. if (!ts->tick_stopped) {
  738. ts->timer_expires = 0;
  739. goto out;
  740. }
  741. }
  742. /*
  743. * If this CPU is the one which had the do_timer() duty last, we limit
  744. * the sleep time to the timekeeping max_deferment value.
  745. * Otherwise we can sleep as long as we want.
  746. */
  747. delta = timekeeping_max_deferment();
  748. if (cpu != tick_do_timer_cpu &&
  749. (tick_do_timer_cpu != TICK_DO_TIMER_NONE || !ts->do_timer_last))
  750. delta = KTIME_MAX;
  751. /* Calculate the next expiry time */
  752. if (delta < (KTIME_MAX - basemono))
  753. expires = basemono + delta;
  754. else
  755. expires = KTIME_MAX;
  756. ts->timer_expires = min_t(u64, expires, next_tick);
  757. out:
  758. return ts->timer_expires;
  759. }
  760. static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu)
  761. {
  762. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  763. u64 basemono = ts->timer_expires_base;
  764. u64 expires = ts->timer_expires;
  765. ktime_t tick = expires;
  766. /* Make sure we won't be trying to stop it twice in a row. */
  767. ts->timer_expires_base = 0;
  768. /*
  769. * If this CPU is the one which updates jiffies, then give up
  770. * the assignment and let it be taken by the CPU which runs
  771. * the tick timer next, which might be this CPU as well. If we
  772. * don't drop this here the jiffies might be stale and
  773. * do_timer() never invoked. Keep track of the fact that it
  774. * was the one which had the do_timer() duty last.
  775. */
  776. if (cpu == tick_do_timer_cpu) {
  777. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  778. ts->do_timer_last = 1;
  779. } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
  780. ts->do_timer_last = 0;
  781. }
  782. /* Skip reprogram of event if its not changed */
  783. if (ts->tick_stopped && (expires == ts->next_tick)) {
  784. /* Sanity check: make sure clockevent is actually programmed */
  785. if (tick == KTIME_MAX || ts->next_tick == hrtimer_get_expires(&ts->sched_timer))
  786. return;
  787. WARN_ON_ONCE(1);
  788. printk_once("basemono: %llu ts->next_tick: %llu dev->next_event: %llu timer->active: %d timer->expires: %llu\n",
  789. basemono, ts->next_tick, dev->next_event,
  790. hrtimer_active(&ts->sched_timer), hrtimer_get_expires(&ts->sched_timer));
  791. }
  792. /*
  793. * nohz_stop_sched_tick can be called several times before
  794. * the nohz_restart_sched_tick is called. This happens when
  795. * interrupts arrive which do not cause a reschedule. In the
  796. * first call we save the current tick time, so we can restart
  797. * the scheduler tick in nohz_restart_sched_tick.
  798. */
  799. if (!ts->tick_stopped) {
  800. calc_load_nohz_start();
  801. quiet_vmstat();
  802. ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
  803. ts->tick_stopped = 1;
  804. trace_tick_stop(1, TICK_DEP_MASK_NONE);
  805. }
  806. ts->next_tick = tick;
  807. /*
  808. * If the expiration time == KTIME_MAX, then we simply stop
  809. * the tick timer.
  810. */
  811. if (unlikely(expires == KTIME_MAX)) {
  812. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  813. hrtimer_cancel(&ts->sched_timer);
  814. else
  815. tick_program_event(KTIME_MAX, 1);
  816. return;
  817. }
  818. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  819. hrtimer_start(&ts->sched_timer, tick,
  820. HRTIMER_MODE_ABS_PINNED_HARD);
  821. } else {
  822. hrtimer_set_expires(&ts->sched_timer, tick);
  823. tick_program_event(tick, 1);
  824. }
  825. }
  826. static void tick_nohz_retain_tick(struct tick_sched *ts)
  827. {
  828. ts->timer_expires_base = 0;
  829. }
  830. #ifdef CONFIG_NO_HZ_FULL
  831. static void tick_nohz_stop_sched_tick(struct tick_sched *ts, int cpu)
  832. {
  833. if (tick_nohz_next_event(ts, cpu))
  834. tick_nohz_stop_tick(ts, cpu);
  835. else
  836. tick_nohz_retain_tick(ts);
  837. }
  838. #endif /* CONFIG_NO_HZ_FULL */
  839. static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
  840. {
  841. /* Update jiffies first */
  842. tick_do_update_jiffies64(now);
  843. /*
  844. * Clear the timer idle flag, so we avoid IPIs on remote queueing and
  845. * the clock forward checks in the enqueue path:
  846. */
  847. timer_clear_idle();
  848. calc_load_nohz_stop();
  849. touch_softlockup_watchdog_sched();
  850. /*
  851. * Cancel the scheduled timer and restore the tick
  852. */
  853. ts->tick_stopped = 0;
  854. tick_nohz_restart(ts, now);
  855. }
  856. static void __tick_nohz_full_update_tick(struct tick_sched *ts,
  857. ktime_t now)
  858. {
  859. #ifdef CONFIG_NO_HZ_FULL
  860. int cpu = smp_processor_id();
  861. if (can_stop_full_tick(cpu, ts))
  862. tick_nohz_stop_sched_tick(ts, cpu);
  863. else if (ts->tick_stopped)
  864. tick_nohz_restart_sched_tick(ts, now);
  865. #endif
  866. }
  867. static void tick_nohz_full_update_tick(struct tick_sched *ts)
  868. {
  869. if (!tick_nohz_full_cpu(smp_processor_id()))
  870. return;
  871. if (!ts->tick_stopped && ts->nohz_mode == NOHZ_MODE_INACTIVE)
  872. return;
  873. __tick_nohz_full_update_tick(ts, ktime_get());
  874. }
  875. /*
  876. * A pending softirq outside an IRQ (or softirq disabled section) context
  877. * should be waiting for ksoftirqd to handle it. Therefore we shouldn't
  878. * reach here due to the need_resched() early check in can_stop_idle_tick().
  879. *
  880. * However if we are between CPUHP_AP_SMPBOOT_THREADS and CPU_TEARDOWN_CPU on the
  881. * cpu_down() process, softirqs can still be raised while ksoftirqd is parked,
  882. * triggering the below since wakep_softirqd() is ignored.
  883. *
  884. */
  885. static bool report_idle_softirq(void)
  886. {
  887. static int ratelimit;
  888. unsigned int pending = local_softirq_pending();
  889. if (likely(!pending))
  890. return false;
  891. /* Some softirqs claim to be safe against hotplug and ksoftirqd parking */
  892. if (!cpu_active(smp_processor_id())) {
  893. pending &= ~SOFTIRQ_HOTPLUG_SAFE_MASK;
  894. if (!pending)
  895. return false;
  896. }
  897. if (ratelimit >= 10)
  898. return false;
  899. /* On RT, softirqs handling may be waiting on some lock */
  900. if (local_bh_blocked())
  901. return false;
  902. pr_warn("NOHZ tick-stop error: local softirq work is pending, handler #%02x!!!\n",
  903. pending);
  904. ratelimit++;
  905. return true;
  906. }
  907. static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
  908. {
  909. /*
  910. * If this CPU is offline and it is the one which updates
  911. * jiffies, then give up the assignment and let it be taken by
  912. * the CPU which runs the tick timer next. If we don't drop
  913. * this here the jiffies might be stale and do_timer() never
  914. * invoked.
  915. */
  916. if (unlikely(!cpu_online(cpu))) {
  917. if (cpu == tick_do_timer_cpu)
  918. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  919. /*
  920. * Make sure the CPU doesn't get fooled by obsolete tick
  921. * deadline if it comes back online later.
  922. */
  923. ts->next_tick = 0;
  924. return false;
  925. }
  926. if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
  927. return false;
  928. if (need_resched())
  929. return false;
  930. if (unlikely(report_idle_softirq()))
  931. return false;
  932. if (tick_nohz_full_enabled()) {
  933. /*
  934. * Keep the tick alive to guarantee timekeeping progression
  935. * if there are full dynticks CPUs around
  936. */
  937. if (tick_do_timer_cpu == cpu)
  938. return false;
  939. /* Should not happen for nohz-full */
  940. if (WARN_ON_ONCE(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
  941. return false;
  942. }
  943. return true;
  944. }
  945. static void __tick_nohz_idle_stop_tick(struct tick_sched *ts)
  946. {
  947. ktime_t expires;
  948. int cpu = smp_processor_id();
  949. /*
  950. * If tick_nohz_get_sleep_length() ran tick_nohz_next_event(), the
  951. * tick timer expiration time is known already.
  952. */
  953. if (ts->timer_expires_base)
  954. expires = ts->timer_expires;
  955. else if (can_stop_idle_tick(cpu, ts))
  956. expires = tick_nohz_next_event(ts, cpu);
  957. else
  958. return;
  959. ts->idle_calls++;
  960. if (expires > 0LL) {
  961. int was_stopped = ts->tick_stopped;
  962. tick_nohz_stop_tick(ts, cpu);
  963. ts->idle_sleeps++;
  964. ts->idle_expires = expires;
  965. if (!was_stopped && ts->tick_stopped) {
  966. ts->idle_jiffies = ts->last_jiffies;
  967. nohz_balance_enter_idle(cpu);
  968. }
  969. } else {
  970. tick_nohz_retain_tick(ts);
  971. }
  972. }
  973. /**
  974. * tick_nohz_idle_stop_tick - stop the idle tick from the idle task
  975. *
  976. * When the next event is more than a tick into the future, stop the idle tick
  977. */
  978. void tick_nohz_idle_stop_tick(void)
  979. {
  980. __tick_nohz_idle_stop_tick(this_cpu_ptr(&tick_cpu_sched));
  981. }
  982. void tick_nohz_idle_retain_tick(void)
  983. {
  984. tick_nohz_retain_tick(this_cpu_ptr(&tick_cpu_sched));
  985. /*
  986. * Undo the effect of get_next_timer_interrupt() called from
  987. * tick_nohz_next_event().
  988. */
  989. timer_clear_idle();
  990. }
  991. /**
  992. * tick_nohz_idle_enter - prepare for entering idle on the current CPU
  993. *
  994. * Called when we start the idle loop.
  995. */
  996. void tick_nohz_idle_enter(void)
  997. {
  998. struct tick_sched *ts;
  999. lockdep_assert_irqs_enabled();
  1000. local_irq_disable();
  1001. ts = this_cpu_ptr(&tick_cpu_sched);
  1002. WARN_ON_ONCE(ts->timer_expires_base);
  1003. ts->inidle = 1;
  1004. tick_nohz_start_idle(ts);
  1005. local_irq_enable();
  1006. }
  1007. /**
  1008. * tick_nohz_irq_exit - update next tick event from interrupt exit
  1009. *
  1010. * When an interrupt fires while we are idle and it doesn't cause
  1011. * a reschedule, it may still add, modify or delete a timer, enqueue
  1012. * an RCU callback, etc...
  1013. * So we need to re-calculate and reprogram the next tick event.
  1014. */
  1015. void tick_nohz_irq_exit(void)
  1016. {
  1017. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1018. if (ts->inidle)
  1019. tick_nohz_start_idle(ts);
  1020. else
  1021. tick_nohz_full_update_tick(ts);
  1022. }
  1023. /**
  1024. * tick_nohz_idle_got_tick - Check whether or not the tick handler has run
  1025. */
  1026. bool tick_nohz_idle_got_tick(void)
  1027. {
  1028. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1029. if (ts->got_idle_tick) {
  1030. ts->got_idle_tick = 0;
  1031. return true;
  1032. }
  1033. return false;
  1034. }
  1035. /**
  1036. * tick_nohz_get_next_hrtimer - return the next expiration time for the hrtimer
  1037. * or the tick, whatever that expires first. Note that, if the tick has been
  1038. * stopped, it returns the next hrtimer.
  1039. *
  1040. * Called from power state control code with interrupts disabled
  1041. */
  1042. ktime_t tick_nohz_get_next_hrtimer(void)
  1043. {
  1044. return __this_cpu_read(tick_cpu_device.evtdev)->next_event;
  1045. }
  1046. /**
  1047. * tick_nohz_get_sleep_length - return the expected length of the current sleep
  1048. * @delta_next: duration until the next event if the tick cannot be stopped
  1049. *
  1050. * Called from power state control code with interrupts disabled.
  1051. *
  1052. * The return value of this function and/or the value returned by it through the
  1053. * @delta_next pointer can be negative which must be taken into account by its
  1054. * callers.
  1055. */
  1056. ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next)
  1057. {
  1058. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  1059. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1060. int cpu = smp_processor_id();
  1061. /*
  1062. * The idle entry time is expected to be a sufficient approximation of
  1063. * the current time at this point.
  1064. */
  1065. ktime_t now = ts->idle_entrytime;
  1066. ktime_t next_event;
  1067. WARN_ON_ONCE(!ts->inidle);
  1068. *delta_next = ktime_sub(dev->next_event, now);
  1069. if (!can_stop_idle_tick(cpu, ts))
  1070. return *delta_next;
  1071. next_event = tick_nohz_next_event(ts, cpu);
  1072. if (!next_event)
  1073. return *delta_next;
  1074. /*
  1075. * If the next highres timer to expire is earlier than next_event, the
  1076. * idle governor needs to know that.
  1077. */
  1078. next_event = min_t(u64, next_event,
  1079. hrtimer_next_event_without(&ts->sched_timer));
  1080. return ktime_sub(next_event, now);
  1081. }
  1082. EXPORT_SYMBOL_GPL(tick_nohz_get_sleep_length);
  1083. /**
  1084. * tick_nohz_get_idle_calls_cpu - return the current idle calls counter value
  1085. * for a particular CPU.
  1086. *
  1087. * Called from the schedutil frequency scaling governor in scheduler context.
  1088. */
  1089. unsigned long tick_nohz_get_idle_calls_cpu(int cpu)
  1090. {
  1091. struct tick_sched *ts = tick_get_tick_sched(cpu);
  1092. return ts->idle_calls;
  1093. }
  1094. EXPORT_SYMBOL_GPL(tick_nohz_get_idle_calls_cpu);
  1095. /**
  1096. * tick_nohz_get_idle_calls - return the current idle calls counter value
  1097. *
  1098. * Called from the schedutil frequency scaling governor in scheduler context.
  1099. */
  1100. unsigned long tick_nohz_get_idle_calls(void)
  1101. {
  1102. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1103. return ts->idle_calls;
  1104. }
  1105. static void tick_nohz_account_idle_time(struct tick_sched *ts,
  1106. ktime_t now)
  1107. {
  1108. unsigned long ticks;
  1109. ts->idle_exittime = now;
  1110. if (vtime_accounting_enabled_this_cpu())
  1111. return;
  1112. /*
  1113. * We stopped the tick in idle. Update process times would miss the
  1114. * time we slept as update_process_times does only a 1 tick
  1115. * accounting. Enforce that this is accounted to idle !
  1116. */
  1117. ticks = jiffies - ts->idle_jiffies;
  1118. /*
  1119. * We might be one off. Do not randomly account a huge number of ticks!
  1120. */
  1121. if (ticks && ticks < LONG_MAX)
  1122. account_idle_ticks(ticks);
  1123. }
  1124. void tick_nohz_idle_restart_tick(void)
  1125. {
  1126. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1127. if (ts->tick_stopped) {
  1128. ktime_t now = ktime_get();
  1129. tick_nohz_restart_sched_tick(ts, now);
  1130. tick_nohz_account_idle_time(ts, now);
  1131. }
  1132. }
  1133. static void tick_nohz_idle_update_tick(struct tick_sched *ts, ktime_t now)
  1134. {
  1135. if (tick_nohz_full_cpu(smp_processor_id()))
  1136. __tick_nohz_full_update_tick(ts, now);
  1137. else
  1138. tick_nohz_restart_sched_tick(ts, now);
  1139. tick_nohz_account_idle_time(ts, now);
  1140. }
  1141. /**
  1142. * tick_nohz_idle_exit - restart the idle tick from the idle task
  1143. *
  1144. * Restart the idle tick when the CPU is woken up from idle
  1145. * This also exit the RCU extended quiescent state. The CPU
  1146. * can use RCU again after this function is called.
  1147. */
  1148. void tick_nohz_idle_exit(void)
  1149. {
  1150. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1151. bool idle_active, tick_stopped;
  1152. ktime_t now;
  1153. local_irq_disable();
  1154. WARN_ON_ONCE(!ts->inidle);
  1155. WARN_ON_ONCE(ts->timer_expires_base);
  1156. ts->inidle = 0;
  1157. idle_active = ts->idle_active;
  1158. tick_stopped = ts->tick_stopped;
  1159. if (idle_active || tick_stopped)
  1160. now = ktime_get();
  1161. if (idle_active)
  1162. tick_nohz_stop_idle(ts, now);
  1163. if (tick_stopped)
  1164. tick_nohz_idle_update_tick(ts, now);
  1165. local_irq_enable();
  1166. }
  1167. /*
  1168. * The nohz low res interrupt handler
  1169. */
  1170. static void tick_nohz_handler(struct clock_event_device *dev)
  1171. {
  1172. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1173. struct pt_regs *regs = get_irq_regs();
  1174. ktime_t now = ktime_get();
  1175. dev->next_event = KTIME_MAX;
  1176. tick_sched_do_timer(ts, now);
  1177. tick_sched_handle(ts, regs);
  1178. if (unlikely(ts->tick_stopped)) {
  1179. /*
  1180. * The clockevent device is not reprogrammed, so change the
  1181. * clock event device to ONESHOT_STOPPED to avoid spurious
  1182. * interrupts on devices which might not be truly one shot.
  1183. */
  1184. tick_program_event(KTIME_MAX, 1);
  1185. return;
  1186. }
  1187. hrtimer_forward(&ts->sched_timer, now, TICK_NSEC);
  1188. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  1189. }
  1190. static inline void tick_nohz_activate(struct tick_sched *ts, int mode)
  1191. {
  1192. if (!tick_nohz_enabled)
  1193. return;
  1194. ts->nohz_mode = mode;
  1195. /* One update is enough */
  1196. if (!test_and_set_bit(0, &tick_nohz_active))
  1197. timers_update_nohz();
  1198. }
  1199. /**
  1200. * tick_nohz_switch_to_nohz - switch to nohz mode
  1201. */
  1202. static void tick_nohz_switch_to_nohz(void)
  1203. {
  1204. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1205. ktime_t next;
  1206. if (!tick_nohz_enabled)
  1207. return;
  1208. if (tick_switch_to_oneshot(tick_nohz_handler))
  1209. return;
  1210. /*
  1211. * Recycle the hrtimer in ts, so we can share the
  1212. * hrtimer_forward with the highres code.
  1213. */
  1214. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
  1215. /* Get the next period */
  1216. next = tick_init_jiffy_update();
  1217. hrtimer_set_expires(&ts->sched_timer, next);
  1218. hrtimer_forward_now(&ts->sched_timer, TICK_NSEC);
  1219. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  1220. tick_nohz_activate(ts, NOHZ_MODE_LOWRES);
  1221. }
  1222. static inline void tick_nohz_irq_enter(void)
  1223. {
  1224. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1225. ktime_t now;
  1226. if (!ts->idle_active && !ts->tick_stopped)
  1227. return;
  1228. now = ktime_get();
  1229. if (ts->idle_active)
  1230. tick_nohz_stop_idle(ts, now);
  1231. /*
  1232. * If all CPUs are idle. We may need to update a stale jiffies value.
  1233. * Note nohz_full is a special case: a timekeeper is guaranteed to stay
  1234. * alive but it might be busy looping with interrupts disabled in some
  1235. * rare case (typically stop machine). So we must make sure we have a
  1236. * last resort.
  1237. */
  1238. if (ts->tick_stopped)
  1239. tick_nohz_update_jiffies(now);
  1240. }
  1241. #else
  1242. static inline void tick_nohz_switch_to_nohz(void) { }
  1243. static inline void tick_nohz_irq_enter(void) { }
  1244. static inline void tick_nohz_activate(struct tick_sched *ts, int mode) { }
  1245. #endif /* CONFIG_NO_HZ_COMMON */
  1246. /*
  1247. * Called from irq_enter to notify about the possible interruption of idle()
  1248. */
  1249. void tick_irq_enter(void)
  1250. {
  1251. tick_check_oneshot_broadcast_this_cpu();
  1252. tick_nohz_irq_enter();
  1253. }
  1254. /*
  1255. * High resolution timer specific code
  1256. */
  1257. #ifdef CONFIG_HIGH_RES_TIMERS
  1258. /*
  1259. * We rearm the timer until we get disabled by the idle code.
  1260. * Called with interrupts disabled.
  1261. */
  1262. static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
  1263. {
  1264. struct tick_sched *ts =
  1265. container_of(timer, struct tick_sched, sched_timer);
  1266. struct pt_regs *regs = get_irq_regs();
  1267. ktime_t now = ktime_get();
  1268. tick_sched_do_timer(ts, now);
  1269. /*
  1270. * Do not call, when we are not in irq context and have
  1271. * no valid regs pointer
  1272. */
  1273. if (regs)
  1274. tick_sched_handle(ts, regs);
  1275. else
  1276. ts->next_tick = 0;
  1277. /* No need to reprogram if we are in idle or full dynticks mode */
  1278. if (unlikely(ts->tick_stopped))
  1279. return HRTIMER_NORESTART;
  1280. hrtimer_forward(timer, now, TICK_NSEC);
  1281. return HRTIMER_RESTART;
  1282. }
  1283. static int sched_skew_tick;
  1284. static int __init skew_tick(char *str)
  1285. {
  1286. get_option(&str, &sched_skew_tick);
  1287. return 0;
  1288. }
  1289. early_param("skew_tick", skew_tick);
  1290. /**
  1291. * tick_setup_sched_timer - setup the tick emulation timer
  1292. */
  1293. void tick_setup_sched_timer(void)
  1294. {
  1295. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1296. ktime_t now = ktime_get();
  1297. /*
  1298. * Emulate tick processing via per-CPU hrtimers:
  1299. */
  1300. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
  1301. ts->sched_timer.function = tick_sched_timer;
  1302. /* Get the next period (per-CPU) */
  1303. hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
  1304. /* Offset the tick to avert jiffies_lock contention. */
  1305. if (sched_skew_tick) {
  1306. u64 offset = TICK_NSEC >> 1;
  1307. do_div(offset, num_possible_cpus());
  1308. offset *= smp_processor_id();
  1309. hrtimer_add_expires_ns(&ts->sched_timer, offset);
  1310. }
  1311. hrtimer_forward(&ts->sched_timer, now, TICK_NSEC);
  1312. hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED_HARD);
  1313. tick_nohz_activate(ts, NOHZ_MODE_HIGHRES);
  1314. }
  1315. #endif /* HIGH_RES_TIMERS */
  1316. #if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
  1317. void tick_cancel_sched_timer(int cpu)
  1318. {
  1319. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  1320. # ifdef CONFIG_HIGH_RES_TIMERS
  1321. if (ts->sched_timer.base)
  1322. hrtimer_cancel(&ts->sched_timer);
  1323. # endif
  1324. memset(ts, 0, sizeof(*ts));
  1325. }
  1326. #endif
  1327. /*
  1328. * Async notification about clocksource changes
  1329. */
  1330. void tick_clock_notify(void)
  1331. {
  1332. int cpu;
  1333. for_each_possible_cpu(cpu)
  1334. set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
  1335. }
  1336. /*
  1337. * Async notification about clock event changes
  1338. */
  1339. void tick_oneshot_notify(void)
  1340. {
  1341. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1342. set_bit(0, &ts->check_clocks);
  1343. }
  1344. /*
  1345. * Check, if a change happened, which makes oneshot possible.
  1346. *
  1347. * Called cyclic from the hrtimer softirq (driven by the timer
  1348. * softirq) allow_nohz signals, that we can switch into low-res nohz
  1349. * mode, because high resolution timers are disabled (either compile
  1350. * or runtime). Called with interrupts disabled.
  1351. */
  1352. int tick_check_oneshot_change(int allow_nohz)
  1353. {
  1354. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1355. if (!test_and_clear_bit(0, &ts->check_clocks))
  1356. return 0;
  1357. if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
  1358. return 0;
  1359. if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
  1360. return 0;
  1361. if (!allow_nohz)
  1362. return 1;
  1363. tick_nohz_switch_to_nohz();
  1364. return 0;
  1365. }