tick-sched.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  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 <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. Protected by jiffies_lock.
  44. */
  45. static ktime_t last_jiffies_update;
  46. /*
  47. * Must be called with interrupts disabled !
  48. */
  49. static void tick_do_update_jiffies64(ktime_t now)
  50. {
  51. unsigned long ticks = 0;
  52. ktime_t delta;
  53. /*
  54. * Do a quick check without holding jiffies_lock:
  55. * The READ_ONCE() pairs with two updates done later in this function.
  56. */
  57. delta = ktime_sub(now, READ_ONCE(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. /* Pairs with the lockless read in this function. */
  66. WRITE_ONCE(last_jiffies_update,
  67. ktime_add(last_jiffies_update, tick_period));
  68. /* Slow path for long timeouts */
  69. if (unlikely(delta >= tick_period)) {
  70. s64 incr = ktime_to_ns(tick_period);
  71. ticks = ktime_divns(delta, incr);
  72. /* Pairs with the lockless read in this function. */
  73. WRITE_ONCE(last_jiffies_update,
  74. ktime_add_ns(last_jiffies_update,
  75. incr * ticks));
  76. }
  77. do_timer(++ticks);
  78. /* Keep the tick_next_period variable up to date */
  79. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  80. } else {
  81. write_sequnlock(&jiffies_lock);
  82. return;
  83. }
  84. write_sequnlock(&jiffies_lock);
  85. update_wall_time();
  86. }
  87. /*
  88. * Initialize and return retrieve the jiffies update.
  89. */
  90. static ktime_t tick_init_jiffy_update(void)
  91. {
  92. ktime_t period;
  93. write_seqlock(&jiffies_lock);
  94. /* Did we start the jiffies update yet ? */
  95. if (last_jiffies_update == 0)
  96. last_jiffies_update = tick_next_period;
  97. period = last_jiffies_update;
  98. write_sequnlock(&jiffies_lock);
  99. return period;
  100. }
  101. static void tick_sched_do_timer(ktime_t now)
  102. {
  103. int cpu = smp_processor_id();
  104. #ifdef CONFIG_NO_HZ_COMMON
  105. /*
  106. * Check if the do_timer duty was dropped. We don't care about
  107. * concurrency: This happens only when the CPU in charge went
  108. * into a long sleep. If two CPUs happen to assign themselves to
  109. * this duty, then the jiffies update is still serialized by
  110. * jiffies_lock.
  111. */
  112. if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE)
  113. && !tick_nohz_full_cpu(cpu))
  114. tick_do_timer_cpu = cpu;
  115. #endif
  116. /* Check, if the jiffies need an update */
  117. if (tick_do_timer_cpu == cpu)
  118. tick_do_update_jiffies64(now);
  119. }
  120. static void tick_sched_handle(struct tick_sched *ts, struct pt_regs *regs)
  121. {
  122. #ifdef CONFIG_NO_HZ_COMMON
  123. /*
  124. * When we are idle and the tick is stopped, we have to touch
  125. * the watchdog as we might not schedule for a really long
  126. * time. This happens on complete idle SMP systems while
  127. * waiting on the login prompt. We also increment the "start of
  128. * idle" jiffy stamp so the idle accounting adjustment we do
  129. * when we go busy again does not account too much ticks.
  130. */
  131. if (ts->tick_stopped) {
  132. touch_softlockup_watchdog_sched();
  133. if (is_idle_task(current))
  134. ts->idle_jiffies++;
  135. /*
  136. * In case the current tick fired too early past its expected
  137. * expiration, make sure we don't bypass the next clock reprogramming
  138. * to the same deadline.
  139. */
  140. ts->next_tick = 0;
  141. }
  142. #endif
  143. update_process_times(user_mode(regs));
  144. profile_tick(CPU_PROFILING);
  145. }
  146. #endif
  147. #ifdef CONFIG_NO_HZ_FULL
  148. cpumask_var_t tick_nohz_full_mask;
  149. cpumask_var_t housekeeping_mask;
  150. bool tick_nohz_full_running;
  151. static atomic_t tick_dep_mask;
  152. static bool check_tick_dependency(atomic_t *dep)
  153. {
  154. int val = atomic_read(dep);
  155. if (val & TICK_DEP_MASK_POSIX_TIMER) {
  156. trace_tick_stop(0, TICK_DEP_MASK_POSIX_TIMER);
  157. return true;
  158. }
  159. if (val & TICK_DEP_MASK_PERF_EVENTS) {
  160. trace_tick_stop(0, TICK_DEP_MASK_PERF_EVENTS);
  161. return true;
  162. }
  163. if (val & TICK_DEP_MASK_SCHED) {
  164. trace_tick_stop(0, TICK_DEP_MASK_SCHED);
  165. return true;
  166. }
  167. if (val & TICK_DEP_MASK_CLOCK_UNSTABLE) {
  168. trace_tick_stop(0, TICK_DEP_MASK_CLOCK_UNSTABLE);
  169. return true;
  170. }
  171. return false;
  172. }
  173. static bool can_stop_full_tick(int cpu, struct tick_sched *ts)
  174. {
  175. WARN_ON_ONCE(!irqs_disabled());
  176. if (unlikely(!cpu_online(cpu)))
  177. return false;
  178. if (check_tick_dependency(&tick_dep_mask))
  179. return false;
  180. if (check_tick_dependency(&ts->tick_dep_mask))
  181. return false;
  182. if (check_tick_dependency(&current->tick_dep_mask))
  183. return false;
  184. if (check_tick_dependency(&current->signal->tick_dep_mask))
  185. return false;
  186. return true;
  187. }
  188. static void nohz_full_kick_func(struct irq_work *work)
  189. {
  190. /* Empty, the tick restart happens on tick_nohz_irq_exit() */
  191. }
  192. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  193. .func = nohz_full_kick_func,
  194. };
  195. /*
  196. * Kick this CPU if it's full dynticks in order to force it to
  197. * re-evaluate its dependency on the tick and restart it if necessary.
  198. * This kick, unlike tick_nohz_full_kick_cpu() and tick_nohz_full_kick_all(),
  199. * is NMI safe.
  200. */
  201. static void tick_nohz_full_kick(void)
  202. {
  203. if (!tick_nohz_full_cpu(smp_processor_id()))
  204. return;
  205. irq_work_queue(this_cpu_ptr(&nohz_full_kick_work));
  206. }
  207. /*
  208. * Kick the CPU if it's full dynticks in order to force it to
  209. * re-evaluate its dependency on the tick and restart it if necessary.
  210. */
  211. void tick_nohz_full_kick_cpu(int cpu)
  212. {
  213. if (!tick_nohz_full_cpu(cpu))
  214. return;
  215. irq_work_queue_on(&per_cpu(nohz_full_kick_work, cpu), cpu);
  216. }
  217. /*
  218. * Kick all full dynticks CPUs in order to force these to re-evaluate
  219. * their dependency on the tick and restart it if necessary.
  220. */
  221. static void tick_nohz_full_kick_all(void)
  222. {
  223. int cpu;
  224. if (!tick_nohz_full_running)
  225. return;
  226. preempt_disable();
  227. for_each_cpu_and(cpu, tick_nohz_full_mask, cpu_online_mask)
  228. tick_nohz_full_kick_cpu(cpu);
  229. preempt_enable();
  230. }
  231. static void tick_nohz_dep_set_all(atomic_t *dep,
  232. enum tick_dep_bits bit)
  233. {
  234. int prev;
  235. prev = atomic_fetch_or(BIT(bit), dep);
  236. if (!prev)
  237. tick_nohz_full_kick_all();
  238. }
  239. /*
  240. * Set a global tick dependency. Used by perf events that rely on freq and
  241. * by unstable clock.
  242. */
  243. void tick_nohz_dep_set(enum tick_dep_bits bit)
  244. {
  245. tick_nohz_dep_set_all(&tick_dep_mask, bit);
  246. }
  247. void tick_nohz_dep_clear(enum tick_dep_bits bit)
  248. {
  249. atomic_andnot(BIT(bit), &tick_dep_mask);
  250. }
  251. /*
  252. * Set per-CPU tick dependency. Used by scheduler and perf events in order to
  253. * manage events throttling.
  254. */
  255. void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit)
  256. {
  257. int prev;
  258. struct tick_sched *ts;
  259. ts = per_cpu_ptr(&tick_cpu_sched, cpu);
  260. prev = atomic_fetch_or(BIT(bit), &ts->tick_dep_mask);
  261. if (!prev) {
  262. preempt_disable();
  263. /* Perf needs local kick that is NMI safe */
  264. if (cpu == smp_processor_id()) {
  265. tick_nohz_full_kick();
  266. } else {
  267. /* Remote irq work not NMI-safe */
  268. if (!WARN_ON_ONCE(in_nmi()))
  269. tick_nohz_full_kick_cpu(cpu);
  270. }
  271. preempt_enable();
  272. }
  273. }
  274. void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit)
  275. {
  276. struct tick_sched *ts = per_cpu_ptr(&tick_cpu_sched, cpu);
  277. atomic_andnot(BIT(bit), &ts->tick_dep_mask);
  278. }
  279. /*
  280. * Set a per-task tick dependency. Posix CPU timers need this in order to elapse
  281. * per task timers.
  282. */
  283. void tick_nohz_dep_set_task(struct task_struct *tsk, enum tick_dep_bits bit)
  284. {
  285. /*
  286. * We could optimize this with just kicking the target running the task
  287. * if that noise matters for nohz full users.
  288. */
  289. tick_nohz_dep_set_all(&tsk->tick_dep_mask, bit);
  290. }
  291. void tick_nohz_dep_clear_task(struct task_struct *tsk, enum tick_dep_bits bit)
  292. {
  293. atomic_andnot(BIT(bit), &tsk->tick_dep_mask);
  294. }
  295. /*
  296. * Set a per-taskgroup tick dependency. Posix CPU timers need this in order to elapse
  297. * per process timers.
  298. */
  299. void tick_nohz_dep_set_signal(struct signal_struct *sig, enum tick_dep_bits bit)
  300. {
  301. tick_nohz_dep_set_all(&sig->tick_dep_mask, bit);
  302. }
  303. void tick_nohz_dep_clear_signal(struct signal_struct *sig, enum tick_dep_bits bit)
  304. {
  305. atomic_andnot(BIT(bit), &sig->tick_dep_mask);
  306. }
  307. /*
  308. * Re-evaluate the need for the tick as we switch the current task.
  309. * It might need the tick due to per task/process properties:
  310. * perf events, posix CPU timers, ...
  311. */
  312. void __tick_nohz_task_switch(void)
  313. {
  314. unsigned long flags;
  315. struct tick_sched *ts;
  316. local_irq_save(flags);
  317. if (!tick_nohz_full_cpu(smp_processor_id()))
  318. goto out;
  319. ts = this_cpu_ptr(&tick_cpu_sched);
  320. if (ts->tick_stopped) {
  321. if (atomic_read(&current->tick_dep_mask) ||
  322. atomic_read(&current->signal->tick_dep_mask))
  323. tick_nohz_full_kick();
  324. }
  325. out:
  326. local_irq_restore(flags);
  327. }
  328. /* Parse the boot-time nohz CPU list from the kernel parameters. */
  329. static int __init tick_nohz_full_setup(char *str)
  330. {
  331. alloc_bootmem_cpumask_var(&tick_nohz_full_mask);
  332. if (cpulist_parse(str, tick_nohz_full_mask) < 0) {
  333. pr_warn("NO_HZ: Incorrect nohz_full cpumask\n");
  334. free_bootmem_cpumask_var(tick_nohz_full_mask);
  335. return 1;
  336. }
  337. tick_nohz_full_running = true;
  338. return 1;
  339. }
  340. __setup("nohz_full=", tick_nohz_full_setup);
  341. static int tick_nohz_cpu_down(unsigned int cpu)
  342. {
  343. /*
  344. * The boot CPU handles housekeeping duty (unbound timers,
  345. * workqueues, timekeeping, ...) on behalf of full dynticks
  346. * CPUs. It must remain online when nohz full is enabled.
  347. */
  348. if (tick_nohz_full_running && tick_do_timer_cpu == cpu)
  349. return -EBUSY;
  350. return 0;
  351. }
  352. static int tick_nohz_init_all(void)
  353. {
  354. int err = -1;
  355. #ifdef CONFIG_NO_HZ_FULL_ALL
  356. if (!alloc_cpumask_var(&tick_nohz_full_mask, GFP_KERNEL)) {
  357. WARN(1, "NO_HZ: Can't allocate full dynticks cpumask\n");
  358. return err;
  359. }
  360. err = 0;
  361. cpumask_setall(tick_nohz_full_mask);
  362. tick_nohz_full_running = true;
  363. #endif
  364. return err;
  365. }
  366. void __init tick_nohz_init(void)
  367. {
  368. int cpu, ret;
  369. if (!tick_nohz_full_running) {
  370. if (tick_nohz_init_all() < 0)
  371. return;
  372. }
  373. if (!alloc_cpumask_var(&housekeeping_mask, GFP_KERNEL)) {
  374. WARN(1, "NO_HZ: Can't allocate not-full dynticks cpumask\n");
  375. cpumask_clear(tick_nohz_full_mask);
  376. tick_nohz_full_running = false;
  377. return;
  378. }
  379. /*
  380. * Full dynticks uses irq work to drive the tick rescheduling on safe
  381. * locking contexts. But then we need irq work to raise its own
  382. * interrupts to avoid circular dependency on the tick
  383. */
  384. if (!arch_irq_work_has_interrupt()) {
  385. pr_warn("NO_HZ: Can't run full dynticks because arch doesn't support irq work self-IPIs\n");
  386. cpumask_clear(tick_nohz_full_mask);
  387. cpumask_copy(housekeeping_mask, cpu_possible_mask);
  388. tick_nohz_full_running = false;
  389. return;
  390. }
  391. cpu = smp_processor_id();
  392. if (cpumask_test_cpu(cpu, tick_nohz_full_mask)) {
  393. pr_warn("NO_HZ: Clearing %d from nohz_full range for timekeeping\n",
  394. cpu);
  395. cpumask_clear_cpu(cpu, tick_nohz_full_mask);
  396. }
  397. cpumask_andnot(housekeeping_mask,
  398. cpu_possible_mask, tick_nohz_full_mask);
  399. for_each_cpu(cpu, tick_nohz_full_mask)
  400. context_tracking_cpu_set(cpu);
  401. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  402. "kernel/nohz:predown", NULL,
  403. tick_nohz_cpu_down);
  404. WARN_ON(ret < 0);
  405. pr_info("NO_HZ: Full dynticks CPUs: %*pbl.\n",
  406. cpumask_pr_args(tick_nohz_full_mask));
  407. /*
  408. * We need at least one CPU to handle housekeeping work such
  409. * as timekeeping, unbound timers, workqueues, ...
  410. */
  411. WARN_ON_ONCE(cpumask_empty(housekeeping_mask));
  412. }
  413. #endif
  414. /*
  415. * NOHZ - aka dynamic tick functionality
  416. */
  417. #ifdef CONFIG_NO_HZ_COMMON
  418. /*
  419. * NO HZ enabled ?
  420. */
  421. bool tick_nohz_enabled __read_mostly = true;
  422. unsigned long tick_nohz_active __read_mostly;
  423. /*
  424. * Enable / Disable tickless mode
  425. */
  426. static int __init setup_tick_nohz(char *str)
  427. {
  428. return (kstrtobool(str, &tick_nohz_enabled) == 0);
  429. }
  430. __setup("nohz=", setup_tick_nohz);
  431. int tick_nohz_tick_stopped(void)
  432. {
  433. return __this_cpu_read(tick_cpu_sched.tick_stopped);
  434. }
  435. /**
  436. * tick_nohz_update_jiffies - update jiffies when idle was interrupted
  437. *
  438. * Called from interrupt entry when the CPU was idle
  439. *
  440. * In case the sched_tick was stopped on this CPU, we have to check if jiffies
  441. * must be updated. Otherwise an interrupt handler could use a stale jiffy
  442. * value. We do this unconditionally on any CPU, as we don't know whether the
  443. * CPU, which has the update task assigned is in a long sleep.
  444. */
  445. static void tick_nohz_update_jiffies(ktime_t now)
  446. {
  447. unsigned long flags;
  448. __this_cpu_write(tick_cpu_sched.idle_waketime, now);
  449. local_irq_save(flags);
  450. tick_do_update_jiffies64(now);
  451. local_irq_restore(flags);
  452. touch_softlockup_watchdog_sched();
  453. }
  454. /*
  455. * Updates the per-CPU time idle statistics counters
  456. */
  457. static void
  458. update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
  459. {
  460. ktime_t delta;
  461. if (ts->idle_active) {
  462. delta = ktime_sub(now, ts->idle_entrytime);
  463. if (nr_iowait_cpu(cpu) > 0)
  464. ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
  465. else
  466. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  467. ts->idle_entrytime = now;
  468. }
  469. if (last_update_time)
  470. *last_update_time = ktime_to_us(now);
  471. }
  472. static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now)
  473. {
  474. update_ts_time_stats(smp_processor_id(), ts, now, NULL);
  475. ts->idle_active = 0;
  476. sched_clock_idle_wakeup_event();
  477. }
  478. static void tick_nohz_start_idle(struct tick_sched *ts)
  479. {
  480. ts->idle_entrytime = ktime_get();
  481. ts->idle_active = 1;
  482. sched_clock_idle_sleep_event();
  483. }
  484. /**
  485. * get_cpu_idle_time_us - get the total idle 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 idle 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_idle_time_us(int cpu, u64 *last_update_time)
  499. {
  500. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  501. ktime_t now, idle;
  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. idle = ts->idle_sleeptime;
  508. } else {
  509. if (ts->idle_active && !nr_iowait_cpu(cpu)) {
  510. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  511. idle = ktime_add(ts->idle_sleeptime, delta);
  512. } else {
  513. idle = ts->idle_sleeptime;
  514. }
  515. }
  516. return ktime_to_us(idle);
  517. }
  518. EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
  519. /**
  520. * get_cpu_iowait_time_us - get the total iowait time of a CPU
  521. * @cpu: CPU number to query
  522. * @last_update_time: variable to store update time in. Do not update
  523. * counters if NULL.
  524. *
  525. * Return the cumulative iowait time (since boot) for a given
  526. * CPU, in microseconds.
  527. *
  528. * This time is measured via accounting rather than sampling,
  529. * and is as accurate as ktime_get() is.
  530. *
  531. * This function returns -1 if NOHZ is not enabled.
  532. */
  533. u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
  534. {
  535. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  536. ktime_t now, iowait;
  537. if (!tick_nohz_active)
  538. return -1;
  539. now = ktime_get();
  540. if (last_update_time) {
  541. update_ts_time_stats(cpu, ts, now, last_update_time);
  542. iowait = ts->iowait_sleeptime;
  543. } else {
  544. if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
  545. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  546. iowait = ktime_add(ts->iowait_sleeptime, delta);
  547. } else {
  548. iowait = ts->iowait_sleeptime;
  549. }
  550. }
  551. return ktime_to_us(iowait);
  552. }
  553. EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
  554. static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
  555. {
  556. hrtimer_cancel(&ts->sched_timer);
  557. hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
  558. /* Forward the time to expire in the future */
  559. hrtimer_forward(&ts->sched_timer, now, tick_period);
  560. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  561. hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
  562. else
  563. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  564. /*
  565. * Reset to make sure next tick stop doesn't get fooled by past
  566. * cached clock deadline.
  567. */
  568. ts->next_tick = 0;
  569. }
  570. static inline bool local_timer_softirq_pending(void)
  571. {
  572. return local_softirq_pending() & BIT(TIMER_SOFTIRQ);
  573. }
  574. static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
  575. {
  576. u64 basemono, next_tick, next_tmr, next_rcu, delta, expires;
  577. unsigned long seq, basejiff;
  578. /* Read jiffies and the time when jiffies were updated last */
  579. do {
  580. seq = read_seqbegin(&jiffies_lock);
  581. basemono = last_jiffies_update;
  582. basejiff = jiffies;
  583. } while (read_seqretry(&jiffies_lock, seq));
  584. ts->last_jiffies = basejiff;
  585. ts->timer_expires_base = basemono;
  586. /*
  587. * Keep the periodic tick, when RCU, architecture or irq_work
  588. * requests it.
  589. * Aside of that check whether the local timer softirq is
  590. * pending. If so its a bad idea to call get_next_timer_interrupt()
  591. * because there is an already expired timer, so it will request
  592. * immeditate expiry, which rearms the hardware timer with a
  593. * minimal delta which brings us back to this place
  594. * immediately. Lather, rinse and repeat...
  595. */
  596. if (rcu_needs_cpu(basemono, &next_rcu) || arch_needs_cpu() ||
  597. irq_work_needs_cpu() || local_timer_softirq_pending()) {
  598. next_tick = basemono + TICK_NSEC;
  599. } else {
  600. /*
  601. * Get the next pending timer. If high resolution
  602. * timers are enabled this only takes the timer wheel
  603. * timers into account. If high resolution timers are
  604. * disabled this also looks at the next expiring
  605. * hrtimer.
  606. */
  607. next_tmr = get_next_timer_interrupt(basejiff, basemono);
  608. ts->next_timer = next_tmr;
  609. /* Take the next rcu event into account */
  610. next_tick = next_rcu < next_tmr ? next_rcu : next_tmr;
  611. }
  612. /*
  613. * If the tick is due in the next period, keep it ticking or
  614. * force prod the timer.
  615. */
  616. delta = next_tick - basemono;
  617. if (delta <= (u64)TICK_NSEC) {
  618. /*
  619. * Tell the timer code that the base is not idle, i.e. undo
  620. * the effect of get_next_timer_interrupt():
  621. */
  622. timer_clear_idle();
  623. /*
  624. * We've not stopped the tick yet, and there's a timer in the
  625. * next period, so no point in stopping it either, bail.
  626. */
  627. if (!ts->tick_stopped) {
  628. ts->timer_expires = 0;
  629. goto out;
  630. }
  631. }
  632. /*
  633. * If this CPU is the one which had the do_timer() duty last, we limit
  634. * the sleep time to the timekeeping max_deferment value.
  635. * Otherwise we can sleep as long as we want.
  636. */
  637. delta = timekeeping_max_deferment();
  638. if (cpu != tick_do_timer_cpu &&
  639. (tick_do_timer_cpu != TICK_DO_TIMER_NONE || !ts->do_timer_last))
  640. delta = KTIME_MAX;
  641. #ifdef CONFIG_NO_HZ_FULL
  642. /* Limit the tick delta to the maximum scheduler deferment */
  643. if (!ts->inidle)
  644. delta = min(delta, scheduler_tick_max_deferment());
  645. #endif
  646. /* Calculate the next expiry time */
  647. if (delta < (KTIME_MAX - basemono))
  648. expires = basemono + delta;
  649. else
  650. expires = KTIME_MAX;
  651. ts->timer_expires = min_t(u64, expires, next_tick);
  652. out:
  653. return ts->timer_expires;
  654. }
  655. static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu)
  656. {
  657. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  658. u64 basemono = ts->timer_expires_base;
  659. u64 expires = ts->timer_expires;
  660. ktime_t tick = expires;
  661. /* Make sure we won't be trying to stop it twice in a row. */
  662. ts->timer_expires_base = 0;
  663. /*
  664. * If this CPU is the one which updates jiffies, then give up
  665. * the assignment and let it be taken by the CPU which runs
  666. * the tick timer next, which might be this CPU as well. If we
  667. * don't drop this here the jiffies might be stale and
  668. * do_timer() never invoked. Keep track of the fact that it
  669. * was the one which had the do_timer() duty last.
  670. */
  671. if (cpu == tick_do_timer_cpu) {
  672. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  673. ts->do_timer_last = 1;
  674. } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
  675. ts->do_timer_last = 0;
  676. }
  677. /* Skip reprogram of event if its not changed */
  678. if (ts->tick_stopped && (expires == ts->next_tick)) {
  679. /* Sanity check: make sure clockevent is actually programmed */
  680. if (tick == KTIME_MAX || ts->next_tick == hrtimer_get_expires(&ts->sched_timer))
  681. return;
  682. WARN_ON_ONCE(1);
  683. printk_once("basemono: %llu ts->next_tick: %llu dev->next_event: %llu timer->active: %d timer->expires: %llu\n",
  684. basemono, ts->next_tick, dev->next_event,
  685. hrtimer_active(&ts->sched_timer), hrtimer_get_expires(&ts->sched_timer));
  686. }
  687. /*
  688. * nohz_stop_sched_tick can be called several times before
  689. * the nohz_restart_sched_tick is called. This happens when
  690. * interrupts arrive which do not cause a reschedule. In the
  691. * first call we save the current tick time, so we can restart
  692. * the scheduler tick in nohz_restart_sched_tick.
  693. */
  694. if (!ts->tick_stopped) {
  695. calc_load_nohz_start();
  696. cpu_load_update_nohz_start();
  697. ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
  698. ts->tick_stopped = 1;
  699. trace_tick_stop(1, TICK_DEP_MASK_NONE);
  700. }
  701. ts->next_tick = tick;
  702. /*
  703. * If the expiration time == KTIME_MAX, then we simply stop
  704. * the tick timer.
  705. */
  706. if (unlikely(expires == KTIME_MAX)) {
  707. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  708. hrtimer_cancel(&ts->sched_timer);
  709. return;
  710. }
  711. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  712. hrtimer_start(&ts->sched_timer, tick, HRTIMER_MODE_ABS_PINNED);
  713. } else {
  714. hrtimer_set_expires(&ts->sched_timer, tick);
  715. tick_program_event(tick, 1);
  716. }
  717. }
  718. static void tick_nohz_retain_tick(struct tick_sched *ts)
  719. {
  720. ts->timer_expires_base = 0;
  721. }
  722. #ifdef CONFIG_NO_HZ_FULL
  723. static void tick_nohz_stop_sched_tick(struct tick_sched *ts, int cpu)
  724. {
  725. if (tick_nohz_next_event(ts, cpu))
  726. tick_nohz_stop_tick(ts, cpu);
  727. else
  728. tick_nohz_retain_tick(ts);
  729. }
  730. #endif /* CONFIG_NO_HZ_FULL */
  731. static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
  732. {
  733. /* Update jiffies first */
  734. tick_do_update_jiffies64(now);
  735. cpu_load_update_nohz_stop();
  736. /*
  737. * Clear the timer idle flag, so we avoid IPIs on remote queueing and
  738. * the clock forward checks in the enqueue path:
  739. */
  740. timer_clear_idle();
  741. calc_load_nohz_stop();
  742. touch_softlockup_watchdog_sched();
  743. /*
  744. * Cancel the scheduled timer and restore the tick
  745. */
  746. ts->tick_stopped = 0;
  747. ts->idle_exittime = now;
  748. tick_nohz_restart(ts, now);
  749. }
  750. static void tick_nohz_full_update_tick(struct tick_sched *ts)
  751. {
  752. #ifdef CONFIG_NO_HZ_FULL
  753. int cpu = smp_processor_id();
  754. if (!tick_nohz_full_cpu(cpu))
  755. return;
  756. if (!ts->tick_stopped && ts->nohz_mode == NOHZ_MODE_INACTIVE)
  757. return;
  758. if (can_stop_full_tick(cpu, ts))
  759. tick_nohz_stop_sched_tick(ts, cpu);
  760. else if (ts->tick_stopped)
  761. tick_nohz_restart_sched_tick(ts, ktime_get());
  762. #endif
  763. }
  764. static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
  765. {
  766. /*
  767. * If this CPU is offline and it is the one which updates
  768. * jiffies, then give up the assignment and let it be taken by
  769. * the CPU which runs the tick timer next. If we don't drop
  770. * this here the jiffies might be stale and do_timer() never
  771. * invoked.
  772. */
  773. if (unlikely(!cpu_online(cpu))) {
  774. if (cpu == tick_do_timer_cpu)
  775. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  776. /*
  777. * Make sure the CPU doesn't get fooled by obsolete tick
  778. * deadline if it comes back online later.
  779. */
  780. ts->next_tick = 0;
  781. return false;
  782. }
  783. if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
  784. return false;
  785. if (need_resched())
  786. return false;
  787. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  788. static int ratelimit;
  789. if (ratelimit < 10 && !in_softirq() &&
  790. (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  791. pr_warn("NOHZ: local_softirq_pending %02x\n",
  792. (unsigned int) local_softirq_pending());
  793. ratelimit++;
  794. }
  795. return false;
  796. }
  797. if (tick_nohz_full_enabled()) {
  798. /*
  799. * Keep the tick alive to guarantee timekeeping progression
  800. * if there are full dynticks CPUs around
  801. */
  802. if (tick_do_timer_cpu == cpu)
  803. return false;
  804. /*
  805. * Boot safety: make sure the timekeeping duty has been
  806. * assigned before entering dyntick-idle mode,
  807. */
  808. if (tick_do_timer_cpu == TICK_DO_TIMER_NONE)
  809. return false;
  810. }
  811. return true;
  812. }
  813. static void __tick_nohz_idle_stop_tick(struct tick_sched *ts)
  814. {
  815. ktime_t expires;
  816. int cpu = smp_processor_id();
  817. /*
  818. * If tick_nohz_get_sleep_length() ran tick_nohz_next_event(), the
  819. * tick timer expiration time is known already.
  820. */
  821. if (ts->timer_expires_base)
  822. expires = ts->timer_expires;
  823. else if (can_stop_idle_tick(cpu, ts))
  824. expires = tick_nohz_next_event(ts, cpu);
  825. else
  826. return;
  827. ts->idle_calls++;
  828. if (expires > 0LL) {
  829. int was_stopped = ts->tick_stopped;
  830. tick_nohz_stop_tick(ts, cpu);
  831. ts->idle_sleeps++;
  832. ts->idle_expires = expires;
  833. if (!was_stopped && ts->tick_stopped) {
  834. ts->idle_jiffies = ts->last_jiffies;
  835. nohz_balance_enter_idle(cpu);
  836. }
  837. } else {
  838. tick_nohz_retain_tick(ts);
  839. }
  840. }
  841. /**
  842. * tick_nohz_idle_stop_tick - stop the idle tick from the idle task
  843. *
  844. * When the next event is more than a tick into the future, stop the idle tick
  845. */
  846. void tick_nohz_idle_stop_tick(void)
  847. {
  848. __tick_nohz_idle_stop_tick(this_cpu_ptr(&tick_cpu_sched));
  849. }
  850. void tick_nohz_idle_retain_tick(void)
  851. {
  852. tick_nohz_retain_tick(this_cpu_ptr(&tick_cpu_sched));
  853. /*
  854. * Undo the effect of get_next_timer_interrupt() called from
  855. * tick_nohz_next_event().
  856. */
  857. timer_clear_idle();
  858. }
  859. /**
  860. * tick_nohz_idle_enter - prepare for entering idle on the current CPU
  861. *
  862. * Called when we start the idle loop.
  863. */
  864. void tick_nohz_idle_enter(void)
  865. {
  866. struct tick_sched *ts;
  867. WARN_ON_ONCE(irqs_disabled());
  868. /*
  869. * Update the idle state in the scheduler domain hierarchy
  870. * when tick_nohz_stop_tick() is called from the idle loop.
  871. * State will be updated to busy during the first busy tick after
  872. * exiting idle.
  873. */
  874. set_cpu_sd_state_idle();
  875. local_irq_disable();
  876. ts = this_cpu_ptr(&tick_cpu_sched);
  877. WARN_ON_ONCE(ts->timer_expires_base);
  878. ts->inidle = 1;
  879. tick_nohz_start_idle(ts);
  880. local_irq_enable();
  881. }
  882. /**
  883. * tick_nohz_irq_exit - update next tick event from interrupt exit
  884. *
  885. * When an interrupt fires while we are idle and it doesn't cause
  886. * a reschedule, it may still add, modify or delete a timer, enqueue
  887. * an RCU callback, etc...
  888. * So we need to re-calculate and reprogram the next tick event.
  889. */
  890. void tick_nohz_irq_exit(void)
  891. {
  892. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  893. if (ts->inidle)
  894. tick_nohz_start_idle(ts);
  895. else
  896. tick_nohz_full_update_tick(ts);
  897. }
  898. /**
  899. * tick_nohz_idle_got_tick - Check whether or not the tick handler has run
  900. */
  901. bool tick_nohz_idle_got_tick(void)
  902. {
  903. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  904. if (ts->inidle > 1) {
  905. ts->inidle = 1;
  906. return true;
  907. }
  908. return false;
  909. }
  910. /**
  911. * tick_nohz_get_sleep_length - return the expected length of the current sleep
  912. * @delta_next: duration until the next event if the tick cannot be stopped
  913. *
  914. * Called from power state control code with interrupts disabled
  915. */
  916. ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next)
  917. {
  918. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  919. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  920. int cpu = smp_processor_id();
  921. /*
  922. * The idle entry time is expected to be a sufficient approximation of
  923. * the current time at this point.
  924. */
  925. ktime_t now = ts->idle_entrytime;
  926. ktime_t next_event;
  927. WARN_ON_ONCE(!ts->inidle);
  928. *delta_next = ktime_sub(dev->next_event, now);
  929. if (!can_stop_idle_tick(cpu, ts))
  930. return *delta_next;
  931. next_event = tick_nohz_next_event(ts, cpu);
  932. if (!next_event)
  933. return *delta_next;
  934. /*
  935. * If the next highres timer to expire is earlier than next_event, the
  936. * idle governor needs to know that.
  937. */
  938. next_event = min_t(u64, next_event,
  939. hrtimer_next_event_without(&ts->sched_timer));
  940. return ktime_sub(next_event, now);
  941. }
  942. /**
  943. * tick_nohz_get_idle_calls_cpu - return the current idle calls counter value
  944. * for a particular CPU.
  945. *
  946. * Called from the schedutil frequency scaling governor in scheduler context.
  947. */
  948. unsigned long tick_nohz_get_idle_calls_cpu(int cpu)
  949. {
  950. struct tick_sched *ts = tick_get_tick_sched(cpu);
  951. return ts->idle_calls;
  952. }
  953. /**
  954. * tick_nohz_get_idle_calls - return the current idle calls counter value
  955. *
  956. * Called from the schedutil frequency scaling governor in scheduler context.
  957. */
  958. unsigned long tick_nohz_get_idle_calls(void)
  959. {
  960. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  961. return ts->idle_calls;
  962. }
  963. static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
  964. {
  965. #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  966. unsigned long ticks;
  967. if (vtime_accounting_cpu_enabled())
  968. return;
  969. /*
  970. * We stopped the tick in idle. Update process times would miss the
  971. * time we slept as update_process_times does only a 1 tick
  972. * accounting. Enforce that this is accounted to idle !
  973. */
  974. ticks = jiffies - ts->idle_jiffies;
  975. /*
  976. * We might be one off. Do not randomly account a huge number of ticks!
  977. */
  978. if (ticks && ticks < LONG_MAX)
  979. account_idle_ticks(ticks);
  980. #endif
  981. }
  982. static void __tick_nohz_idle_restart_tick(struct tick_sched *ts, ktime_t now)
  983. {
  984. tick_nohz_restart_sched_tick(ts, now);
  985. tick_nohz_account_idle_ticks(ts);
  986. }
  987. void tick_nohz_idle_restart_tick(void)
  988. {
  989. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  990. if (ts->tick_stopped)
  991. __tick_nohz_idle_restart_tick(ts, ktime_get());
  992. }
  993. /**
  994. * tick_nohz_idle_exit - restart the idle tick from the idle task
  995. *
  996. * Restart the idle tick when the CPU is woken up from idle
  997. * This also exit the RCU extended quiescent state. The CPU
  998. * can use RCU again after this function is called.
  999. */
  1000. void tick_nohz_idle_exit(void)
  1001. {
  1002. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1003. ktime_t now;
  1004. local_irq_disable();
  1005. WARN_ON_ONCE(!ts->inidle);
  1006. WARN_ON_ONCE(ts->timer_expires_base);
  1007. ts->inidle = 0;
  1008. if (ts->idle_active || ts->tick_stopped)
  1009. now = ktime_get();
  1010. if (ts->idle_active)
  1011. tick_nohz_stop_idle(ts, now);
  1012. if (ts->tick_stopped)
  1013. __tick_nohz_idle_restart_tick(ts, now);
  1014. local_irq_enable();
  1015. }
  1016. /*
  1017. * The nohz low res interrupt handler
  1018. */
  1019. static void tick_nohz_handler(struct clock_event_device *dev)
  1020. {
  1021. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1022. struct pt_regs *regs = get_irq_regs();
  1023. ktime_t now = ktime_get();
  1024. if (ts->inidle)
  1025. ts->inidle = 2;
  1026. dev->next_event = KTIME_MAX;
  1027. tick_sched_do_timer(now);
  1028. tick_sched_handle(ts, regs);
  1029. /* No need to reprogram if we are running tickless */
  1030. if (unlikely(ts->tick_stopped))
  1031. return;
  1032. hrtimer_forward(&ts->sched_timer, now, tick_period);
  1033. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  1034. }
  1035. static inline void tick_nohz_activate(struct tick_sched *ts, int mode)
  1036. {
  1037. if (!tick_nohz_enabled)
  1038. return;
  1039. ts->nohz_mode = mode;
  1040. /* One update is enough */
  1041. if (!test_and_set_bit(0, &tick_nohz_active))
  1042. timers_update_migration(true);
  1043. }
  1044. /**
  1045. * tick_nohz_switch_to_nohz - switch to nohz mode
  1046. */
  1047. static void tick_nohz_switch_to_nohz(void)
  1048. {
  1049. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1050. ktime_t next;
  1051. if (!tick_nohz_enabled)
  1052. return;
  1053. if (tick_switch_to_oneshot(tick_nohz_handler))
  1054. return;
  1055. /*
  1056. * Recycle the hrtimer in ts, so we can share the
  1057. * hrtimer_forward with the highres code.
  1058. */
  1059. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1060. /* Get the next period */
  1061. next = tick_init_jiffy_update();
  1062. hrtimer_set_expires(&ts->sched_timer, next);
  1063. hrtimer_forward_now(&ts->sched_timer, tick_period);
  1064. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  1065. tick_nohz_activate(ts, NOHZ_MODE_LOWRES);
  1066. }
  1067. static inline void tick_nohz_irq_enter(void)
  1068. {
  1069. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1070. ktime_t now;
  1071. if (!ts->idle_active && !ts->tick_stopped)
  1072. return;
  1073. now = ktime_get();
  1074. if (ts->idle_active)
  1075. tick_nohz_stop_idle(ts, now);
  1076. if (ts->tick_stopped)
  1077. tick_nohz_update_jiffies(now);
  1078. }
  1079. #else
  1080. static inline void tick_nohz_switch_to_nohz(void) { }
  1081. static inline void tick_nohz_irq_enter(void) { }
  1082. static inline void tick_nohz_activate(struct tick_sched *ts, int mode) { }
  1083. #endif /* CONFIG_NO_HZ_COMMON */
  1084. /*
  1085. * Called from irq_enter to notify about the possible interruption of idle()
  1086. */
  1087. void tick_irq_enter(void)
  1088. {
  1089. tick_check_oneshot_broadcast_this_cpu();
  1090. tick_nohz_irq_enter();
  1091. }
  1092. /*
  1093. * High resolution timer specific code
  1094. */
  1095. #ifdef CONFIG_HIGH_RES_TIMERS
  1096. /*
  1097. * We rearm the timer until we get disabled by the idle code.
  1098. * Called with interrupts disabled.
  1099. */
  1100. static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
  1101. {
  1102. struct tick_sched *ts =
  1103. container_of(timer, struct tick_sched, sched_timer);
  1104. struct pt_regs *regs = get_irq_regs();
  1105. ktime_t now = ktime_get();
  1106. if (ts->inidle)
  1107. ts->inidle = 2;
  1108. tick_sched_do_timer(now);
  1109. /*
  1110. * Do not call, when we are not in irq context and have
  1111. * no valid regs pointer
  1112. */
  1113. if (regs)
  1114. tick_sched_handle(ts, regs);
  1115. else
  1116. ts->next_tick = 0;
  1117. /* No need to reprogram if we are in idle or full dynticks mode */
  1118. if (unlikely(ts->tick_stopped))
  1119. return HRTIMER_NORESTART;
  1120. hrtimer_forward(timer, now, tick_period);
  1121. return HRTIMER_RESTART;
  1122. }
  1123. static int sched_skew_tick;
  1124. static int __init skew_tick(char *str)
  1125. {
  1126. get_option(&str, &sched_skew_tick);
  1127. return 0;
  1128. }
  1129. early_param("skew_tick", skew_tick);
  1130. /**
  1131. * tick_setup_sched_timer - setup the tick emulation timer
  1132. */
  1133. void tick_setup_sched_timer(void)
  1134. {
  1135. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1136. ktime_t now = ktime_get();
  1137. /*
  1138. * Emulate tick processing via per-CPU hrtimers:
  1139. */
  1140. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1141. ts->sched_timer.function = tick_sched_timer;
  1142. /* Get the next period (per-CPU) */
  1143. hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
  1144. /* Offset the tick to avert jiffies_lock contention. */
  1145. if (sched_skew_tick) {
  1146. u64 offset = ktime_to_ns(tick_period) >> 1;
  1147. do_div(offset, num_possible_cpus());
  1148. offset *= smp_processor_id();
  1149. hrtimer_add_expires_ns(&ts->sched_timer, offset);
  1150. }
  1151. hrtimer_forward(&ts->sched_timer, now, tick_period);
  1152. hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
  1153. tick_nohz_activate(ts, NOHZ_MODE_HIGHRES);
  1154. }
  1155. #endif /* HIGH_RES_TIMERS */
  1156. #if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
  1157. void tick_cancel_sched_timer(int cpu)
  1158. {
  1159. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  1160. # ifdef CONFIG_HIGH_RES_TIMERS
  1161. if (ts->sched_timer.base)
  1162. hrtimer_cancel(&ts->sched_timer);
  1163. # endif
  1164. memset(ts, 0, sizeof(*ts));
  1165. }
  1166. #endif
  1167. /**
  1168. * Async notification about clocksource changes
  1169. */
  1170. void tick_clock_notify(void)
  1171. {
  1172. int cpu;
  1173. for_each_possible_cpu(cpu)
  1174. set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
  1175. }
  1176. /*
  1177. * Async notification about clock event changes
  1178. */
  1179. void tick_oneshot_notify(void)
  1180. {
  1181. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1182. set_bit(0, &ts->check_clocks);
  1183. }
  1184. /**
  1185. * Check, if a change happened, which makes oneshot possible.
  1186. *
  1187. * Called cyclic from the hrtimer softirq (driven by the timer
  1188. * softirq) allow_nohz signals, that we can switch into low-res nohz
  1189. * mode, because high resolution timers are disabled (either compile
  1190. * or runtime). Called with interrupts disabled.
  1191. */
  1192. int tick_check_oneshot_change(int allow_nohz)
  1193. {
  1194. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1195. if (!test_and_clear_bit(0, &ts->check_clocks))
  1196. return 0;
  1197. if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
  1198. return 0;
  1199. if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
  1200. return 0;
  1201. if (!allow_nohz)
  1202. return 1;
  1203. tick_nohz_switch_to_nohz();
  1204. return 0;
  1205. }