tick-sched.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /*
  2. * linux/kernel/time/tick-sched.c
  3. *
  4. * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
  6. * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
  7. *
  8. * No idle tick implementation for low and high resolution timers
  9. *
  10. * Started by: Thomas Gleixner and Ingo Molnar
  11. *
  12. * Distribute under GPLv2.
  13. */
  14. #include <linux/cpu.h>
  15. #include <linux/err.h>
  16. #include <linux/hrtimer.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/percpu.h>
  20. #include <linux/profile.h>
  21. #include <linux/sched.h>
  22. #include <linux/module.h>
  23. #include <linux/irq_work.h>
  24. #include <linux/posix-timers.h>
  25. #include <linux/perf_event.h>
  26. #include <linux/context_tracking.h>
  27. #include <asm/irq_regs.h>
  28. #include "tick-internal.h"
  29. #include <trace/events/timer.h>
  30. /*
  31. * Per cpu nohz control structure
  32. */
  33. DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
  34. /*
  35. * The time, when the last jiffy update happened. Protected by jiffies_lock.
  36. */
  37. static ktime_t last_jiffies_update;
  38. struct tick_sched *tick_get_tick_sched(int cpu)
  39. {
  40. return &per_cpu(tick_cpu_sched, cpu);
  41. }
  42. /*
  43. * Must be called with interrupts disabled !
  44. */
  45. static void tick_do_update_jiffies64(ktime_t now)
  46. {
  47. unsigned long ticks = 0;
  48. ktime_t delta;
  49. /*
  50. * Do a quick check without holding jiffies_lock:
  51. */
  52. delta = ktime_sub(now, last_jiffies_update);
  53. if (delta.tv64 < tick_period.tv64)
  54. return;
  55. /* Reevalute with jiffies_lock held */
  56. write_seqlock(&jiffies_lock);
  57. delta = ktime_sub(now, last_jiffies_update);
  58. if (delta.tv64 >= tick_period.tv64) {
  59. delta = ktime_sub(delta, tick_period);
  60. last_jiffies_update = ktime_add(last_jiffies_update,
  61. tick_period);
  62. /* Slow path for long timeouts */
  63. if (unlikely(delta.tv64 >= tick_period.tv64)) {
  64. s64 incr = ktime_to_ns(tick_period);
  65. ticks = ktime_divns(delta, incr);
  66. last_jiffies_update = ktime_add_ns(last_jiffies_update,
  67. incr * ticks);
  68. }
  69. do_timer(++ticks);
  70. /* Keep the tick_next_period variable up to date */
  71. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  72. } else {
  73. write_sequnlock(&jiffies_lock);
  74. return;
  75. }
  76. write_sequnlock(&jiffies_lock);
  77. update_wall_time();
  78. }
  79. /*
  80. * Initialize and return retrieve the jiffies update.
  81. */
  82. static ktime_t tick_init_jiffy_update(void)
  83. {
  84. ktime_t period;
  85. write_seqlock(&jiffies_lock);
  86. /* Did we start the jiffies update yet ? */
  87. if (last_jiffies_update.tv64 == 0)
  88. last_jiffies_update = tick_next_period;
  89. period = last_jiffies_update;
  90. write_sequnlock(&jiffies_lock);
  91. return period;
  92. }
  93. static void tick_sched_do_timer(ktime_t now)
  94. {
  95. int cpu = smp_processor_id();
  96. #ifdef CONFIG_NO_HZ_COMMON
  97. /*
  98. * Check if the do_timer duty was dropped. We don't care about
  99. * concurrency: This happens only when the cpu in charge went
  100. * into a long sleep. If two cpus happen to assign themself to
  101. * this duty, then the jiffies update is still serialized by
  102. * jiffies_lock.
  103. */
  104. if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE)
  105. && !tick_nohz_full_cpu(cpu))
  106. tick_do_timer_cpu = cpu;
  107. #endif
  108. /* Check, if the jiffies need an update */
  109. if (tick_do_timer_cpu == cpu)
  110. tick_do_update_jiffies64(now);
  111. }
  112. static void tick_sched_handle(struct tick_sched *ts, struct pt_regs *regs)
  113. {
  114. #ifdef CONFIG_NO_HZ_COMMON
  115. /*
  116. * When we are idle and the tick is stopped, we have to touch
  117. * the watchdog as we might not schedule for a really long
  118. * time. This happens on complete idle SMP systems while
  119. * waiting on the login prompt. We also increment the "start of
  120. * idle" jiffy stamp so the idle accounting adjustment we do
  121. * when we go busy again does not account too much ticks.
  122. */
  123. if (ts->tick_stopped) {
  124. touch_softlockup_watchdog();
  125. if (is_idle_task(current))
  126. ts->idle_jiffies++;
  127. }
  128. #endif
  129. update_process_times(user_mode(regs));
  130. profile_tick(CPU_PROFILING);
  131. }
  132. #ifdef CONFIG_NO_HZ_FULL
  133. cpumask_var_t tick_nohz_full_mask;
  134. cpumask_var_t housekeeping_mask;
  135. bool tick_nohz_full_running;
  136. static bool can_stop_full_tick(void)
  137. {
  138. WARN_ON_ONCE(!irqs_disabled());
  139. if (!sched_can_stop_tick()) {
  140. trace_tick_stop(0, "more than 1 task in runqueue\n");
  141. return false;
  142. }
  143. if (!posix_cpu_timers_can_stop_tick(current)) {
  144. trace_tick_stop(0, "posix timers running\n");
  145. return false;
  146. }
  147. if (!perf_event_can_stop_tick()) {
  148. trace_tick_stop(0, "perf events running\n");
  149. return false;
  150. }
  151. /* sched_clock_tick() needs us? */
  152. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  153. /*
  154. * TODO: kick full dynticks CPUs when
  155. * sched_clock_stable is set.
  156. */
  157. if (!sched_clock_stable()) {
  158. trace_tick_stop(0, "unstable sched clock\n");
  159. /*
  160. * Don't allow the user to think they can get
  161. * full NO_HZ with this machine.
  162. */
  163. WARN_ONCE(tick_nohz_full_running,
  164. "NO_HZ FULL will not work with unstable sched clock");
  165. return false;
  166. }
  167. #endif
  168. return true;
  169. }
  170. static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now);
  171. /*
  172. * Re-evaluate the need for the tick on the current CPU
  173. * and restart it if necessary.
  174. */
  175. void __tick_nohz_full_check(void)
  176. {
  177. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  178. if (tick_nohz_full_cpu(smp_processor_id())) {
  179. if (ts->tick_stopped && !is_idle_task(current)) {
  180. if (!can_stop_full_tick())
  181. tick_nohz_restart_sched_tick(ts, ktime_get());
  182. }
  183. }
  184. }
  185. static void nohz_full_kick_work_func(struct irq_work *work)
  186. {
  187. __tick_nohz_full_check();
  188. }
  189. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  190. .func = nohz_full_kick_work_func,
  191. };
  192. /*
  193. * Kick this CPU if it's full dynticks in order to force it to
  194. * re-evaluate its dependency on the tick and restart it if necessary.
  195. * This kick, unlike tick_nohz_full_kick_cpu() and tick_nohz_full_kick_all(),
  196. * is NMI safe.
  197. */
  198. void tick_nohz_full_kick(void)
  199. {
  200. if (!tick_nohz_full_cpu(smp_processor_id()))
  201. return;
  202. irq_work_queue(&__get_cpu_var(nohz_full_kick_work));
  203. }
  204. /*
  205. * Kick the CPU if it's full dynticks in order to force it to
  206. * re-evaluate its dependency on the tick and restart it if necessary.
  207. */
  208. void tick_nohz_full_kick_cpu(int cpu)
  209. {
  210. if (!tick_nohz_full_cpu(cpu))
  211. return;
  212. irq_work_queue_on(&per_cpu(nohz_full_kick_work, cpu), cpu);
  213. }
  214. static void nohz_full_kick_ipi(void *info)
  215. {
  216. __tick_nohz_full_check();
  217. }
  218. /*
  219. * Kick all full dynticks CPUs in order to force these to re-evaluate
  220. * their dependency on the tick and restart it if necessary.
  221. */
  222. void tick_nohz_full_kick_all(void)
  223. {
  224. if (!tick_nohz_full_running)
  225. return;
  226. preempt_disable();
  227. smp_call_function_many(tick_nohz_full_mask,
  228. nohz_full_kick_ipi, NULL, false);
  229. tick_nohz_full_kick();
  230. preempt_enable();
  231. }
  232. /*
  233. * Re-evaluate the need for the tick as we switch the current task.
  234. * It might need the tick due to per task/process properties:
  235. * perf events, posix cpu timers, ...
  236. */
  237. void __tick_nohz_task_switch(struct task_struct *tsk)
  238. {
  239. unsigned long flags;
  240. local_irq_save(flags);
  241. if (!tick_nohz_full_cpu(smp_processor_id()))
  242. goto out;
  243. if (tick_nohz_tick_stopped() && !can_stop_full_tick())
  244. tick_nohz_full_kick();
  245. out:
  246. local_irq_restore(flags);
  247. }
  248. /* Parse the boot-time nohz CPU list from the kernel parameters. */
  249. static int __init tick_nohz_full_setup(char *str)
  250. {
  251. alloc_bootmem_cpumask_var(&tick_nohz_full_mask);
  252. if (cpulist_parse(str, tick_nohz_full_mask) < 0) {
  253. pr_warning("NOHZ: Incorrect nohz_full cpumask\n");
  254. free_bootmem_cpumask_var(tick_nohz_full_mask);
  255. return 1;
  256. }
  257. tick_nohz_full_running = true;
  258. return 1;
  259. }
  260. __setup("nohz_full=", tick_nohz_full_setup);
  261. static int tick_nohz_cpu_down_callback(struct notifier_block *nfb,
  262. unsigned long action,
  263. void *hcpu)
  264. {
  265. unsigned int cpu = (unsigned long)hcpu;
  266. switch (action & ~CPU_TASKS_FROZEN) {
  267. case CPU_DOWN_PREPARE:
  268. /*
  269. * If we handle the timekeeping duty for full dynticks CPUs,
  270. * we can't safely shutdown that CPU.
  271. */
  272. if (tick_nohz_full_running && tick_do_timer_cpu == cpu)
  273. return NOTIFY_BAD;
  274. break;
  275. }
  276. return NOTIFY_OK;
  277. }
  278. /*
  279. * Worst case string length in chunks of CPU range seems 2 steps
  280. * separations: 0,2,4,6,...
  281. * This is NR_CPUS + sizeof('\0')
  282. */
  283. static char __initdata nohz_full_buf[NR_CPUS + 1];
  284. static int tick_nohz_init_all(void)
  285. {
  286. int err = -1;
  287. #ifdef CONFIG_NO_HZ_FULL_ALL
  288. if (!alloc_cpumask_var(&tick_nohz_full_mask, GFP_KERNEL)) {
  289. WARN(1, "NO_HZ: Can't allocate full dynticks cpumask\n");
  290. return err;
  291. }
  292. err = 0;
  293. cpumask_setall(tick_nohz_full_mask);
  294. tick_nohz_full_running = true;
  295. #endif
  296. return err;
  297. }
  298. void __init tick_nohz_init(void)
  299. {
  300. int cpu;
  301. if (!tick_nohz_full_running) {
  302. if (tick_nohz_init_all() < 0)
  303. return;
  304. }
  305. if (!alloc_cpumask_var(&housekeeping_mask, GFP_KERNEL)) {
  306. WARN(1, "NO_HZ: Can't allocate not-full dynticks cpumask\n");
  307. cpumask_clear(tick_nohz_full_mask);
  308. tick_nohz_full_running = false;
  309. return;
  310. }
  311. /*
  312. * Full dynticks uses irq work to drive the tick rescheduling on safe
  313. * locking contexts. But then we need irq work to raise its own
  314. * interrupts to avoid circular dependency on the tick
  315. */
  316. if (!arch_irq_work_has_interrupt()) {
  317. pr_warning("NO_HZ: Can't run full dynticks because arch doesn't "
  318. "support irq work self-IPIs\n");
  319. cpumask_clear(tick_nohz_full_mask);
  320. cpumask_copy(housekeeping_mask, cpu_possible_mask);
  321. tick_nohz_full_running = false;
  322. return;
  323. }
  324. cpu = smp_processor_id();
  325. if (cpumask_test_cpu(cpu, tick_nohz_full_mask)) {
  326. pr_warning("NO_HZ: Clearing %d from nohz_full range for timekeeping\n", cpu);
  327. cpumask_clear_cpu(cpu, tick_nohz_full_mask);
  328. }
  329. cpumask_andnot(housekeeping_mask,
  330. cpu_possible_mask, tick_nohz_full_mask);
  331. for_each_cpu(cpu, tick_nohz_full_mask)
  332. context_tracking_cpu_set(cpu);
  333. cpu_notifier(tick_nohz_cpu_down_callback, 0);
  334. cpulist_scnprintf(nohz_full_buf, sizeof(nohz_full_buf), tick_nohz_full_mask);
  335. pr_info("NO_HZ: Full dynticks CPUs: %s.\n", nohz_full_buf);
  336. }
  337. #endif
  338. /*
  339. * NOHZ - aka dynamic tick functionality
  340. */
  341. #ifdef CONFIG_NO_HZ_COMMON
  342. /*
  343. * NO HZ enabled ?
  344. */
  345. static int tick_nohz_enabled __read_mostly = 1;
  346. int tick_nohz_active __read_mostly;
  347. /*
  348. * Enable / Disable tickless mode
  349. */
  350. static int __init setup_tick_nohz(char *str)
  351. {
  352. if (!strcmp(str, "off"))
  353. tick_nohz_enabled = 0;
  354. else if (!strcmp(str, "on"))
  355. tick_nohz_enabled = 1;
  356. else
  357. return 0;
  358. return 1;
  359. }
  360. __setup("nohz=", setup_tick_nohz);
  361. /**
  362. * tick_nohz_update_jiffies - update jiffies when idle was interrupted
  363. *
  364. * Called from interrupt entry when the CPU was idle
  365. *
  366. * In case the sched_tick was stopped on this CPU, we have to check if jiffies
  367. * must be updated. Otherwise an interrupt handler could use a stale jiffy
  368. * value. We do this unconditionally on any cpu, as we don't know whether the
  369. * cpu, which has the update task assigned is in a long sleep.
  370. */
  371. static void tick_nohz_update_jiffies(ktime_t now)
  372. {
  373. unsigned long flags;
  374. __this_cpu_write(tick_cpu_sched.idle_waketime, now);
  375. local_irq_save(flags);
  376. tick_do_update_jiffies64(now);
  377. local_irq_restore(flags);
  378. touch_softlockup_watchdog();
  379. }
  380. /*
  381. * Updates the per cpu time idle statistics counters
  382. */
  383. static void
  384. update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
  385. {
  386. ktime_t delta;
  387. if (ts->idle_active) {
  388. delta = ktime_sub(now, ts->idle_entrytime);
  389. if (nr_iowait_cpu(cpu) > 0)
  390. ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
  391. else
  392. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  393. ts->idle_entrytime = now;
  394. }
  395. if (last_update_time)
  396. *last_update_time = ktime_to_us(now);
  397. }
  398. static void
  399. update_ts_time_stats_wo_cpuoffline(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
  400. {
  401. ktime_t delta;
  402. if (ts->idle_active && (!ts->cpu_plug_off_flag)) {
  403. delta = ktime_sub(now, ts->idle_entrytime_wo_cpuoffline);
  404. if (nr_iowait_cpu(cpu) > 0)
  405. ts->iowait_sleeptime_wo_cpuoffline = ktime_add(ts->iowait_sleeptime_wo_cpuoffline, delta);
  406. else
  407. ts->idle_sleeptime_wo_cpuoffline = ktime_add(ts->idle_sleeptime_wo_cpuoffline, delta);
  408. ts->idle_entrytime_wo_cpuoffline = now;
  409. }
  410. if (last_update_time)
  411. *last_update_time = ktime_to_us(now);
  412. }
  413. void tick_set_cpu_plugoff_flag(int flag)
  414. {
  415. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  416. ts->cpu_plug_off_flag = flag;
  417. }
  418. static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now)
  419. {
  420. update_ts_time_stats(smp_processor_id(), ts, now, NULL);
  421. update_ts_time_stats_wo_cpuoffline(smp_processor_id(), ts, now, NULL);
  422. ts->idle_active = 0;
  423. sched_clock_idle_wakeup_event(0);
  424. }
  425. static ktime_t tick_nohz_start_idle(struct tick_sched *ts)
  426. {
  427. ktime_t now = ktime_get();
  428. ts->idle_entrytime = now;
  429. ts->idle_entrytime_wo_cpuoffline = now;
  430. ts->idle_active = 1;
  431. sched_clock_idle_sleep_event();
  432. return now;
  433. }
  434. /**
  435. * get_cpu_idle_time_us - get the total idle time of a cpu
  436. * @cpu: CPU number to query
  437. * @last_update_time: variable to store update time in. Do not update
  438. * counters if NULL.
  439. *
  440. * Return the cummulative idle time (since boot) for a given
  441. * CPU, in microseconds.
  442. *
  443. * This time is measured via accounting rather than sampling,
  444. * and is as accurate as ktime_get() is.
  445. *
  446. * This function returns -1 if NOHZ is not enabled.
  447. */
  448. u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
  449. {
  450. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  451. ktime_t now, idle;
  452. if (!tick_nohz_active)
  453. return -1;
  454. now = ktime_get();
  455. if (last_update_time) {
  456. update_ts_time_stats(cpu, ts, now, last_update_time);
  457. idle = ts->idle_sleeptime;
  458. } else {
  459. if (ts->idle_active && !nr_iowait_cpu(cpu)) {
  460. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  461. idle = ktime_add(ts->idle_sleeptime, delta);
  462. } else {
  463. idle = ts->idle_sleeptime;
  464. }
  465. }
  466. return ktime_to_us(idle);
  467. }
  468. EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
  469. u64 get_cpu_idle_time_us_wo_cpuoffline(int cpu, u64 *last_update_time)
  470. {
  471. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  472. ktime_t now, idle;
  473. if (!tick_nohz_enabled)
  474. return -1;
  475. now = ktime_get();
  476. if (last_update_time) {
  477. update_ts_time_stats_wo_cpuoffline(cpu, ts, now, last_update_time);
  478. idle = ts->idle_sleeptime_wo_cpuoffline;
  479. } else {
  480. if (ts->idle_active && !nr_iowait_cpu(cpu) && cpu_online(cpu) && (!ts->cpu_plug_off_flag)) {
  481. ktime_t delta = ktime_sub(now, ts->idle_entrytime_wo_cpuoffline);
  482. idle = ktime_add(ts->idle_sleeptime_wo_cpuoffline, delta);
  483. } else {
  484. idle = ts->idle_sleeptime_wo_cpuoffline;
  485. }
  486. }
  487. return ktime_to_us(idle);
  488. }
  489. EXPORT_SYMBOL_GPL(get_cpu_idle_time_us_wo_cpuoffline);
  490. /**
  491. * get_cpu_iowait_time_us - get the total iowait time of a cpu
  492. * @cpu: CPU number to query
  493. * @last_update_time: variable to store update time in. Do not update
  494. * counters if NULL.
  495. *
  496. * Return the cummulative iowait time (since boot) for a given
  497. * CPU, in microseconds.
  498. *
  499. * This time is measured via accounting rather than sampling,
  500. * and is as accurate as ktime_get() is.
  501. *
  502. * This function returns -1 if NOHZ is not enabled.
  503. */
  504. u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
  505. {
  506. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  507. ktime_t now, iowait;
  508. if (!tick_nohz_active)
  509. return -1;
  510. now = ktime_get();
  511. if (last_update_time) {
  512. update_ts_time_stats(cpu, ts, now, last_update_time);
  513. iowait = ts->iowait_sleeptime;
  514. } else {
  515. if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
  516. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  517. iowait = ktime_add(ts->iowait_sleeptime, delta);
  518. } else {
  519. iowait = ts->iowait_sleeptime;
  520. }
  521. }
  522. return ktime_to_us(iowait);
  523. }
  524. EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
  525. u64 get_cpu_iowait_time_us_wo_cpuoffline(int cpu, u64 *last_update_time)
  526. {
  527. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  528. ktime_t now, iowait;
  529. if (!tick_nohz_enabled)
  530. return -1;
  531. now = ktime_get();
  532. if (last_update_time) {
  533. update_ts_time_stats_wo_cpuoffline(cpu, ts, now, last_update_time);
  534. iowait = ts->iowait_sleeptime_wo_cpuoffline;
  535. } else {
  536. if (ts->idle_active && nr_iowait_cpu(cpu) > 0 && cpu_online(cpu) && (!ts->cpu_plug_off_flag)) {
  537. ktime_t delta = ktime_sub(now, ts->idle_entrytime_wo_cpuoffline);
  538. iowait = ktime_add(ts->iowait_sleeptime_wo_cpuoffline, delta);
  539. } else {
  540. iowait = ts->iowait_sleeptime_wo_cpuoffline;
  541. }
  542. }
  543. return ktime_to_us(iowait);
  544. }
  545. EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us_wo_cpuoffline);
  546. static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
  547. ktime_t now, int cpu)
  548. {
  549. unsigned long seq, last_jiffies, next_jiffies, delta_jiffies;
  550. ktime_t last_update, expires, ret = { .tv64 = 0 };
  551. unsigned long rcu_delta_jiffies;
  552. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  553. u64 time_delta;
  554. time_delta = timekeeping_max_deferment();
  555. /* Read jiffies and the time when jiffies were updated last */
  556. do {
  557. seq = read_seqbegin(&jiffies_lock);
  558. last_update = last_jiffies_update;
  559. last_jiffies = jiffies;
  560. } while (read_seqretry(&jiffies_lock, seq));
  561. if (rcu_needs_cpu(cpu, &rcu_delta_jiffies) ||
  562. arch_needs_cpu() || irq_work_needs_cpu()) {
  563. next_jiffies = last_jiffies + 1;
  564. delta_jiffies = 1;
  565. } else {
  566. /* Get the next timer wheel timer */
  567. next_jiffies = get_next_timer_interrupt(last_jiffies);
  568. delta_jiffies = next_jiffies - last_jiffies;
  569. if (rcu_delta_jiffies < delta_jiffies) {
  570. next_jiffies = last_jiffies + rcu_delta_jiffies;
  571. delta_jiffies = rcu_delta_jiffies;
  572. }
  573. }
  574. /*
  575. * Do not stop the tick, if we are only one off (or less)
  576. * or if the cpu is required for RCU:
  577. */
  578. if (!ts->tick_stopped && delta_jiffies <= 1)
  579. goto out;
  580. /* Schedule the tick, if we are at least one jiffie off */
  581. if ((long)delta_jiffies >= 1) {
  582. /*
  583. * If this cpu is the one which updates jiffies, then
  584. * give up the assignment and let it be taken by the
  585. * cpu which runs the tick timer next, which might be
  586. * this cpu as well. If we don't drop this here the
  587. * jiffies might be stale and do_timer() never
  588. * invoked. Keep track of the fact that it was the one
  589. * which had the do_timer() duty last. If this cpu is
  590. * the one which had the do_timer() duty last, we
  591. * limit the sleep time to the timekeeping
  592. * max_deferement value which we retrieved
  593. * above. Otherwise we can sleep as long as we want.
  594. */
  595. if (cpu == tick_do_timer_cpu) {
  596. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  597. ts->do_timer_last = 1;
  598. } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
  599. time_delta = KTIME_MAX;
  600. ts->do_timer_last = 0;
  601. } else if (!ts->do_timer_last) {
  602. time_delta = KTIME_MAX;
  603. }
  604. #ifdef CONFIG_NO_HZ_FULL
  605. if (!ts->inidle) {
  606. time_delta = min(time_delta,
  607. scheduler_tick_max_deferment());
  608. }
  609. #endif
  610. /*
  611. * calculate the expiry time for the next timer wheel
  612. * timer. delta_jiffies >= NEXT_TIMER_MAX_DELTA signals
  613. * that there is no timer pending or at least extremely
  614. * far into the future (12 days for HZ=1000). In this
  615. * case we set the expiry to the end of time.
  616. */
  617. if (likely(delta_jiffies < NEXT_TIMER_MAX_DELTA)) {
  618. /*
  619. * Calculate the time delta for the next timer event.
  620. * If the time delta exceeds the maximum time delta
  621. * permitted by the current clocksource then adjust
  622. * the time delta accordingly to ensure the
  623. * clocksource does not wrap.
  624. */
  625. time_delta = min_t(u64, time_delta,
  626. tick_period.tv64 * delta_jiffies);
  627. }
  628. if (time_delta < KTIME_MAX)
  629. expires = ktime_add_ns(last_update, time_delta);
  630. else
  631. expires.tv64 = KTIME_MAX;
  632. /* Skip reprogram of event if its not changed */
  633. if (ts->tick_stopped && ktime_equal(expires, dev->next_event))
  634. goto out;
  635. ret = expires;
  636. /*
  637. * nohz_stop_sched_tick can be called several times before
  638. * the nohz_restart_sched_tick is called. This happens when
  639. * interrupts arrive which do not cause a reschedule. In the
  640. * first call we save the current tick time, so we can restart
  641. * the scheduler tick in nohz_restart_sched_tick.
  642. */
  643. if (!ts->tick_stopped) {
  644. nohz_balance_enter_idle(cpu);
  645. calc_load_enter_idle();
  646. ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
  647. ts->tick_stopped = 1;
  648. trace_tick_stop(1, " ");
  649. }
  650. /*
  651. * If the expiration time == KTIME_MAX, then
  652. * in this case we simply stop the tick timer.
  653. */
  654. if (unlikely(expires.tv64 == KTIME_MAX)) {
  655. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  656. hrtimer_cancel(&ts->sched_timer);
  657. goto out;
  658. }
  659. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  660. hrtimer_start(&ts->sched_timer, expires,
  661. HRTIMER_MODE_ABS_PINNED);
  662. /* Check, if the timer was already in the past */
  663. if (hrtimer_active(&ts->sched_timer))
  664. goto out;
  665. } else if (!tick_program_event(expires, 0))
  666. goto out;
  667. /*
  668. * We are past the event already. So we crossed a
  669. * jiffie boundary. Update jiffies and raise the
  670. * softirq.
  671. */
  672. tick_do_update_jiffies64(ktime_get());
  673. }
  674. raise_softirq_irqoff(TIMER_SOFTIRQ);
  675. out:
  676. ts->next_jiffies = next_jiffies;
  677. ts->last_jiffies = last_jiffies;
  678. ts->sleep_length = ktime_sub(dev->next_event, now);
  679. return ret;
  680. }
  681. static void tick_nohz_full_stop_tick(struct tick_sched *ts)
  682. {
  683. #ifdef CONFIG_NO_HZ_FULL
  684. int cpu = smp_processor_id();
  685. if (!tick_nohz_full_cpu(cpu) || is_idle_task(current))
  686. return;
  687. if (!ts->tick_stopped && ts->nohz_mode == NOHZ_MODE_INACTIVE)
  688. return;
  689. if (!can_stop_full_tick())
  690. return;
  691. tick_nohz_stop_sched_tick(ts, ktime_get(), cpu);
  692. #endif
  693. }
  694. static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
  695. {
  696. /*
  697. * If this cpu is offline and it is the one which updates
  698. * jiffies, then give up the assignment and let it be taken by
  699. * the cpu which runs the tick timer next. If we don't drop
  700. * this here the jiffies might be stale and do_timer() never
  701. * invoked.
  702. */
  703. if (unlikely(!cpu_online(cpu))) {
  704. if (cpu == tick_do_timer_cpu)
  705. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  706. return false;
  707. }
  708. if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE)) {
  709. ts->sleep_length = (ktime_t) { .tv64 = NSEC_PER_SEC/HZ };
  710. return false;
  711. }
  712. if (need_resched())
  713. return false;
  714. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  715. static int ratelimit;
  716. if (ratelimit < 10 &&
  717. (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  718. pr_warn("NOHZ: local_softirq_pending %02x\n",
  719. (unsigned int) local_softirq_pending());
  720. ratelimit++;
  721. }
  722. return false;
  723. }
  724. if (tick_nohz_full_enabled()) {
  725. /*
  726. * Keep the tick alive to guarantee timekeeping progression
  727. * if there are full dynticks CPUs around
  728. */
  729. if (tick_do_timer_cpu == cpu)
  730. return false;
  731. /*
  732. * Boot safety: make sure the timekeeping duty has been
  733. * assigned before entering dyntick-idle mode,
  734. */
  735. if (tick_do_timer_cpu == TICK_DO_TIMER_NONE)
  736. return false;
  737. }
  738. return true;
  739. }
  740. static void __tick_nohz_idle_enter(struct tick_sched *ts)
  741. {
  742. ktime_t now, expires;
  743. int cpu = smp_processor_id();
  744. now = tick_nohz_start_idle(ts);
  745. if (can_stop_idle_tick(cpu, ts)) {
  746. int was_stopped = ts->tick_stopped;
  747. ts->idle_calls++;
  748. expires = tick_nohz_stop_sched_tick(ts, now, cpu);
  749. if (expires.tv64 > 0LL) {
  750. ts->idle_sleeps++;
  751. ts->idle_expires = expires;
  752. }
  753. if (!was_stopped && ts->tick_stopped)
  754. ts->idle_jiffies = ts->last_jiffies;
  755. }
  756. }
  757. /**
  758. * tick_nohz_idle_enter - stop the idle tick from the idle task
  759. *
  760. * When the next event is more than a tick into the future, stop the idle tick
  761. * Called when we start the idle loop.
  762. *
  763. * The arch is responsible of calling:
  764. *
  765. * - rcu_idle_enter() after its last use of RCU before the CPU is put
  766. * to sleep.
  767. * - rcu_idle_exit() before the first use of RCU after the CPU is woken up.
  768. */
  769. void tick_nohz_idle_enter(void)
  770. {
  771. struct tick_sched *ts;
  772. WARN_ON_ONCE(irqs_disabled());
  773. /*
  774. * Update the idle state in the scheduler domain hierarchy
  775. * when tick_nohz_stop_sched_tick() is called from the idle loop.
  776. * State will be updated to busy during the first busy tick after
  777. * exiting idle.
  778. */
  779. set_cpu_sd_state_idle();
  780. local_irq_disable();
  781. ts = this_cpu_ptr(&tick_cpu_sched);
  782. ts->inidle = 1;
  783. __tick_nohz_idle_enter(ts);
  784. tick_set_cpu_plugoff_flag(0);
  785. local_irq_enable();
  786. }
  787. /**
  788. * tick_nohz_irq_exit - update next tick event from interrupt exit
  789. *
  790. * When an interrupt fires while we are idle and it doesn't cause
  791. * a reschedule, it may still add, modify or delete a timer, enqueue
  792. * an RCU callback, etc...
  793. * So we need to re-calculate and reprogram the next tick event.
  794. */
  795. void tick_nohz_irq_exit(void)
  796. {
  797. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  798. if (ts->inidle)
  799. __tick_nohz_idle_enter(ts);
  800. else
  801. tick_nohz_full_stop_tick(ts);
  802. }
  803. /**
  804. * tick_nohz_get_sleep_length - return the length of the current sleep
  805. *
  806. * Called from power state control code with interrupts disabled
  807. */
  808. ktime_t tick_nohz_get_sleep_length(void)
  809. {
  810. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  811. return ts->sleep_length;
  812. }
  813. static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
  814. {
  815. hrtimer_cancel(&ts->sched_timer);
  816. hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
  817. while (1) {
  818. /* Forward the time to expire in the future */
  819. hrtimer_forward(&ts->sched_timer, now, tick_period);
  820. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  821. hrtimer_start_expires(&ts->sched_timer,
  822. HRTIMER_MODE_ABS_PINNED);
  823. /* Check, if the timer was already in the past */
  824. if (hrtimer_active(&ts->sched_timer))
  825. break;
  826. } else {
  827. if (!tick_program_event(
  828. hrtimer_get_expires(&ts->sched_timer), 0))
  829. break;
  830. }
  831. /* Reread time and update jiffies */
  832. now = ktime_get();
  833. tick_do_update_jiffies64(now);
  834. }
  835. }
  836. static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
  837. {
  838. /* Update jiffies first */
  839. tick_do_update_jiffies64(now);
  840. update_cpu_load_nohz();
  841. calc_load_exit_idle();
  842. touch_softlockup_watchdog();
  843. /*
  844. * Cancel the scheduled timer and restore the tick
  845. */
  846. ts->tick_stopped = 0;
  847. ts->idle_exittime = now;
  848. tick_nohz_restart(ts, now);
  849. }
  850. static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
  851. {
  852. #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  853. unsigned long ticks;
  854. if (vtime_accounting_enabled())
  855. return;
  856. /*
  857. * We stopped the tick in idle. Update process times would miss the
  858. * time we slept as update_process_times does only a 1 tick
  859. * accounting. Enforce that this is accounted to idle !
  860. */
  861. ticks = jiffies - ts->idle_jiffies;
  862. /*
  863. * We might be one off. Do not randomly account a huge number of ticks!
  864. */
  865. if (ticks && ticks < LONG_MAX)
  866. account_idle_ticks(ticks);
  867. #endif
  868. }
  869. /**
  870. * tick_nohz_idle_exit - restart the idle tick from the idle task
  871. *
  872. * Restart the idle tick when the CPU is woken up from idle
  873. * This also exit the RCU extended quiescent state. The CPU
  874. * can use RCU again after this function is called.
  875. */
  876. void tick_nohz_idle_exit(void)
  877. {
  878. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  879. ktime_t now;
  880. local_irq_disable();
  881. WARN_ON_ONCE(!ts->inidle);
  882. ts->inidle = 0;
  883. if (ts->idle_active || ts->tick_stopped)
  884. now = ktime_get();
  885. if (ts->idle_active)
  886. tick_nohz_stop_idle(ts, now);
  887. if (ts->tick_stopped) {
  888. tick_nohz_restart_sched_tick(ts, now);
  889. tick_nohz_account_idle_ticks(ts);
  890. }
  891. local_irq_enable();
  892. }
  893. static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
  894. {
  895. hrtimer_forward(&ts->sched_timer, now, tick_period);
  896. return tick_program_event(hrtimer_get_expires(&ts->sched_timer), 0);
  897. }
  898. /*
  899. * The nohz low res interrupt handler
  900. */
  901. static void tick_nohz_handler(struct clock_event_device *dev)
  902. {
  903. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  904. struct pt_regs *regs = get_irq_regs();
  905. ktime_t now = ktime_get();
  906. dev->next_event.tv64 = KTIME_MAX;
  907. tick_sched_do_timer(now);
  908. tick_sched_handle(ts, regs);
  909. /* No need to reprogram if we are running tickless */
  910. if (unlikely(ts->tick_stopped))
  911. return;
  912. while (tick_nohz_reprogram(ts, now)) {
  913. now = ktime_get();
  914. tick_do_update_jiffies64(now);
  915. }
  916. }
  917. /**
  918. * tick_nohz_switch_to_nohz - switch to nohz mode
  919. */
  920. static void tick_nohz_switch_to_nohz(void)
  921. {
  922. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  923. ktime_t next;
  924. if (!tick_nohz_enabled)
  925. return;
  926. local_irq_disable();
  927. if (tick_switch_to_oneshot(tick_nohz_handler)) {
  928. local_irq_enable();
  929. return;
  930. }
  931. tick_nohz_active = 1;
  932. ts->nohz_mode = NOHZ_MODE_LOWRES;
  933. /*
  934. * Recycle the hrtimer in ts, so we can share the
  935. * hrtimer_forward with the highres code.
  936. */
  937. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  938. /* Get the next period */
  939. next = tick_init_jiffy_update();
  940. for (;;) {
  941. hrtimer_set_expires(&ts->sched_timer, next);
  942. if (!tick_program_event(next, 0))
  943. break;
  944. next = ktime_add(next, tick_period);
  945. }
  946. local_irq_enable();
  947. }
  948. /*
  949. * When NOHZ is enabled and the tick is stopped, we need to kick the
  950. * tick timer from irq_enter() so that the jiffies update is kept
  951. * alive during long running softirqs. That's ugly as hell, but
  952. * correctness is key even if we need to fix the offending softirq in
  953. * the first place.
  954. *
  955. * Note, this is different to tick_nohz_restart. We just kick the
  956. * timer and do not touch the other magic bits which need to be done
  957. * when idle is left.
  958. */
  959. static void tick_nohz_kick_tick(struct tick_sched *ts, ktime_t now)
  960. {
  961. #if 0
  962. /* Switch back to 2.6.27 behaviour */
  963. ktime_t delta;
  964. /*
  965. * Do not touch the tick device, when the next expiry is either
  966. * already reached or less/equal than the tick period.
  967. */
  968. delta = ktime_sub(hrtimer_get_expires(&ts->sched_timer), now);
  969. if (delta.tv64 <= tick_period.tv64)
  970. return;
  971. tick_nohz_restart(ts, now);
  972. #endif
  973. }
  974. static inline void tick_nohz_irq_enter(void)
  975. {
  976. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  977. ktime_t now;
  978. if (!ts->idle_active && !ts->tick_stopped)
  979. return;
  980. now = ktime_get();
  981. if (ts->idle_active)
  982. tick_nohz_stop_idle(ts, now);
  983. if (ts->tick_stopped) {
  984. tick_nohz_update_jiffies(now);
  985. tick_nohz_kick_tick(ts, now);
  986. }
  987. }
  988. #else
  989. static inline void tick_nohz_switch_to_nohz(void) { }
  990. static inline void tick_nohz_irq_enter(void) { }
  991. #endif /* CONFIG_NO_HZ_COMMON */
  992. /*
  993. * Called from irq_enter to notify about the possible interruption of idle()
  994. */
  995. void tick_irq_enter(void)
  996. {
  997. tick_check_oneshot_broadcast_this_cpu();
  998. tick_nohz_irq_enter();
  999. }
  1000. /*
  1001. * High resolution timer specific code
  1002. */
  1003. #ifdef CONFIG_HIGH_RES_TIMERS
  1004. /*
  1005. * We rearm the timer until we get disabled by the idle code.
  1006. * Called with interrupts disabled.
  1007. */
  1008. static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
  1009. {
  1010. struct tick_sched *ts =
  1011. container_of(timer, struct tick_sched, sched_timer);
  1012. struct pt_regs *regs = get_irq_regs();
  1013. ktime_t now = ktime_get();
  1014. tick_sched_do_timer(now);
  1015. /*
  1016. * Do not call, when we are not in irq context and have
  1017. * no valid regs pointer
  1018. */
  1019. if (regs)
  1020. tick_sched_handle(ts, regs);
  1021. /* No need to reprogram if we are in idle or full dynticks mode */
  1022. if (unlikely(ts->tick_stopped))
  1023. return HRTIMER_NORESTART;
  1024. hrtimer_forward(timer, now, tick_period);
  1025. return HRTIMER_RESTART;
  1026. }
  1027. static int sched_skew_tick;
  1028. static int __init skew_tick(char *str)
  1029. {
  1030. get_option(&str, &sched_skew_tick);
  1031. return 0;
  1032. }
  1033. early_param("skew_tick", skew_tick);
  1034. /**
  1035. * tick_setup_sched_timer - setup the tick emulation timer
  1036. */
  1037. void tick_setup_sched_timer(void)
  1038. {
  1039. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1040. ktime_t now = ktime_get();
  1041. /*
  1042. * Emulate tick processing via per-CPU hrtimers:
  1043. */
  1044. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1045. ts->sched_timer.function = tick_sched_timer;
  1046. /* Get the next period (per cpu) */
  1047. hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
  1048. /* Offset the tick to avert jiffies_lock contention. */
  1049. if (sched_skew_tick) {
  1050. u64 offset = ktime_to_ns(tick_period) >> 1;
  1051. do_div(offset, num_possible_cpus());
  1052. offset *= smp_processor_id();
  1053. hrtimer_add_expires_ns(&ts->sched_timer, offset);
  1054. }
  1055. for (;;) {
  1056. hrtimer_forward(&ts->sched_timer, now, tick_period);
  1057. hrtimer_start_expires(&ts->sched_timer,
  1058. HRTIMER_MODE_ABS_PINNED);
  1059. /* Check, if the timer was already in the past */
  1060. if (hrtimer_active(&ts->sched_timer))
  1061. break;
  1062. now = ktime_get();
  1063. }
  1064. #ifdef CONFIG_NO_HZ_COMMON
  1065. if (tick_nohz_enabled) {
  1066. ts->nohz_mode = NOHZ_MODE_HIGHRES;
  1067. tick_nohz_active = 1;
  1068. }
  1069. #endif
  1070. }
  1071. #endif /* HIGH_RES_TIMERS */
  1072. #if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
  1073. void tick_cancel_sched_timer(int cpu)
  1074. {
  1075. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  1076. # ifdef CONFIG_HIGH_RES_TIMERS
  1077. if (ts->sched_timer.base)
  1078. hrtimer_cancel(&ts->sched_timer);
  1079. # endif
  1080. /*memset(ts, 0, sizeof(*ts));*/ /*to avoid idle time clear to 0 after CPU plug off*/
  1081. ts->nohz_mode = NOHZ_MODE_INACTIVE;
  1082. }
  1083. #endif
  1084. /**
  1085. * Async notification about clocksource changes
  1086. */
  1087. void tick_clock_notify(void)
  1088. {
  1089. int cpu;
  1090. for_each_possible_cpu(cpu)
  1091. set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
  1092. }
  1093. /*
  1094. * Async notification about clock event changes
  1095. */
  1096. void tick_oneshot_notify(void)
  1097. {
  1098. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1099. set_bit(0, &ts->check_clocks);
  1100. }
  1101. /**
  1102. * Check, if a change happened, which makes oneshot possible.
  1103. *
  1104. * Called cyclic from the hrtimer softirq (driven by the timer
  1105. * softirq) allow_nohz signals, that we can switch into low-res nohz
  1106. * mode, because high resolution timers are disabled (either compile
  1107. * or runtime).
  1108. */
  1109. int tick_check_oneshot_change(int allow_nohz)
  1110. {
  1111. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1112. if (!test_and_clear_bit(0, &ts->check_clocks))
  1113. return 0;
  1114. if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
  1115. return 0;
  1116. if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
  1117. return 0;
  1118. if (!allow_nohz)
  1119. return 1;
  1120. tick_nohz_switch_to_nohz();
  1121. return 0;
  1122. }