tick-sched.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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/tick.h>
  23. #include <asm/irq_regs.h>
  24. #include "tick-internal.h"
  25. /*
  26. * Per cpu nohz control structure
  27. */
  28. static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
  29. /*
  30. * The time, when the last jiffy update happened. Protected by xtime_lock.
  31. */
  32. static ktime_t last_jiffies_update;
  33. struct tick_sched *tick_get_tick_sched(int cpu)
  34. {
  35. return &per_cpu(tick_cpu_sched, cpu);
  36. }
  37. /*
  38. * Must be called with interrupts disabled !
  39. */
  40. static void tick_do_update_jiffies64(ktime_t now)
  41. {
  42. unsigned long ticks = 0;
  43. ktime_t delta;
  44. /*
  45. * Do a quick check without holding xtime_lock:
  46. */
  47. delta = ktime_sub(now, last_jiffies_update);
  48. if (delta.tv64 < tick_period.tv64)
  49. return;
  50. /* Reevalute with xtime_lock held */
  51. write_seqlock(&xtime_lock);
  52. delta = ktime_sub(now, last_jiffies_update);
  53. if (delta.tv64 >= tick_period.tv64) {
  54. delta = ktime_sub(delta, tick_period);
  55. last_jiffies_update = ktime_add(last_jiffies_update,
  56. tick_period);
  57. /* Slow path for long timeouts */
  58. if (unlikely(delta.tv64 >= tick_period.tv64)) {
  59. s64 incr = ktime_to_ns(tick_period);
  60. ticks = ktime_divns(delta, incr);
  61. last_jiffies_update = ktime_add_ns(last_jiffies_update,
  62. incr * ticks);
  63. }
  64. do_timer(++ticks);
  65. }
  66. write_sequnlock(&xtime_lock);
  67. }
  68. /*
  69. * Initialize and return retrieve the jiffies update.
  70. */
  71. static ktime_t tick_init_jiffy_update(void)
  72. {
  73. ktime_t period;
  74. write_seqlock(&xtime_lock);
  75. /* Did we start the jiffies update yet ? */
  76. if (last_jiffies_update.tv64 == 0)
  77. last_jiffies_update = tick_next_period;
  78. period = last_jiffies_update;
  79. write_sequnlock(&xtime_lock);
  80. return period;
  81. }
  82. /*
  83. * NOHZ - aka dynamic tick functionality
  84. */
  85. #ifdef CONFIG_NO_HZ
  86. /*
  87. * NO HZ enabled ?
  88. */
  89. static int tick_nohz_enabled __read_mostly = 1;
  90. /*
  91. * Enable / Disable tickless mode
  92. */
  93. static int __init setup_tick_nohz(char *str)
  94. {
  95. if (!strcmp(str, "off"))
  96. tick_nohz_enabled = 0;
  97. else if (!strcmp(str, "on"))
  98. tick_nohz_enabled = 1;
  99. else
  100. return 0;
  101. return 1;
  102. }
  103. __setup("nohz=", setup_tick_nohz);
  104. /**
  105. * tick_nohz_update_jiffies - update jiffies when idle was interrupted
  106. *
  107. * Called from interrupt entry when the CPU was idle
  108. *
  109. * In case the sched_tick was stopped on this CPU, we have to check if jiffies
  110. * must be updated. Otherwise an interrupt handler could use a stale jiffy
  111. * value. We do this unconditionally on any cpu, as we don't know whether the
  112. * cpu, which has the update task assigned is in a long sleep.
  113. */
  114. void tick_nohz_update_jiffies(void)
  115. {
  116. int cpu = smp_processor_id();
  117. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  118. unsigned long flags;
  119. ktime_t now;
  120. if (!ts->tick_stopped)
  121. return;
  122. touch_softlockup_watchdog();
  123. cpu_clear(cpu, nohz_cpu_mask);
  124. now = ktime_get();
  125. ts->idle_waketime = now;
  126. local_irq_save(flags);
  127. tick_do_update_jiffies64(now);
  128. local_irq_restore(flags);
  129. }
  130. void tick_nohz_stop_idle(int cpu)
  131. {
  132. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  133. if (ts->idle_active) {
  134. ktime_t now, delta;
  135. now = ktime_get();
  136. delta = ktime_sub(now, ts->idle_entrytime);
  137. ts->idle_lastupdate = now;
  138. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  139. ts->idle_active = 0;
  140. }
  141. }
  142. static ktime_t tick_nohz_start_idle(struct tick_sched *ts)
  143. {
  144. ktime_t now, delta;
  145. now = ktime_get();
  146. if (ts->idle_active) {
  147. delta = ktime_sub(now, ts->idle_entrytime);
  148. ts->idle_lastupdate = now;
  149. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  150. }
  151. ts->idle_entrytime = now;
  152. ts->idle_active = 1;
  153. return now;
  154. }
  155. u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
  156. {
  157. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  158. *last_update_time = ktime_to_us(ts->idle_lastupdate);
  159. return ktime_to_us(ts->idle_sleeptime);
  160. }
  161. /**
  162. * tick_nohz_stop_sched_tick - stop the idle tick from the idle task
  163. *
  164. * When the next event is more than a tick into the future, stop the idle tick
  165. * Called either from the idle loop or from irq_exit() when an idle period was
  166. * just interrupted by an interrupt which did not cause a reschedule.
  167. */
  168. void tick_nohz_stop_sched_tick(void)
  169. {
  170. unsigned long seq, last_jiffies, next_jiffies, delta_jiffies, flags;
  171. struct tick_sched *ts;
  172. ktime_t last_update, expires, now;
  173. struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
  174. int cpu;
  175. local_irq_save(flags);
  176. cpu = smp_processor_id();
  177. ts = &per_cpu(tick_cpu_sched, cpu);
  178. now = tick_nohz_start_idle(ts);
  179. /*
  180. * If this cpu is offline and it is the one which updates
  181. * jiffies, then give up the assignment and let it be taken by
  182. * the cpu which runs the tick timer next. If we don't drop
  183. * this here the jiffies might be stale and do_timer() never
  184. * invoked.
  185. */
  186. if (unlikely(!cpu_online(cpu))) {
  187. if (cpu == tick_do_timer_cpu)
  188. tick_do_timer_cpu = -1;
  189. }
  190. if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
  191. goto end;
  192. if (need_resched())
  193. goto end;
  194. if (unlikely(local_softirq_pending())) {
  195. static int ratelimit;
  196. if (ratelimit < 10) {
  197. printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  198. local_softirq_pending());
  199. ratelimit++;
  200. }
  201. goto end;
  202. }
  203. ts->idle_calls++;
  204. /* Read jiffies and the time when jiffies were updated last */
  205. do {
  206. seq = read_seqbegin(&xtime_lock);
  207. last_update = last_jiffies_update;
  208. last_jiffies = jiffies;
  209. } while (read_seqretry(&xtime_lock, seq));
  210. /* Get the next timer wheel timer */
  211. next_jiffies = get_next_timer_interrupt(last_jiffies);
  212. delta_jiffies = next_jiffies - last_jiffies;
  213. if (rcu_needs_cpu(cpu))
  214. delta_jiffies = 1;
  215. /*
  216. * Do not stop the tick, if we are only one off
  217. * or if the cpu is required for rcu
  218. */
  219. if (!ts->tick_stopped && delta_jiffies == 1)
  220. goto out;
  221. /* Schedule the tick, if we are at least one jiffie off */
  222. if ((long)delta_jiffies >= 1) {
  223. if (delta_jiffies > 1)
  224. cpu_set(cpu, nohz_cpu_mask);
  225. /*
  226. * nohz_stop_sched_tick can be called several times before
  227. * the nohz_restart_sched_tick is called. This happens when
  228. * interrupts arrive which do not cause a reschedule. In the
  229. * first call we save the current tick time, so we can restart
  230. * the scheduler tick in nohz_restart_sched_tick.
  231. */
  232. if (!ts->tick_stopped) {
  233. if (select_nohz_load_balancer(1)) {
  234. /*
  235. * sched tick not stopped!
  236. */
  237. cpu_clear(cpu, nohz_cpu_mask);
  238. goto out;
  239. }
  240. ts->idle_tick = ts->sched_timer.expires;
  241. ts->tick_stopped = 1;
  242. ts->idle_jiffies = last_jiffies;
  243. rcu_enter_nohz();
  244. sched_clock_tick_stop(cpu);
  245. }
  246. /*
  247. * If this cpu is the one which updates jiffies, then
  248. * give up the assignment and let it be taken by the
  249. * cpu which runs the tick timer next, which might be
  250. * this cpu as well. If we don't drop this here the
  251. * jiffies might be stale and do_timer() never
  252. * invoked.
  253. */
  254. if (cpu == tick_do_timer_cpu)
  255. tick_do_timer_cpu = -1;
  256. ts->idle_sleeps++;
  257. /*
  258. * delta_jiffies >= NEXT_TIMER_MAX_DELTA signals that
  259. * there is no timer pending or at least extremly far
  260. * into the future (12 days for HZ=1000). In this case
  261. * we simply stop the tick timer:
  262. */
  263. if (unlikely(delta_jiffies >= NEXT_TIMER_MAX_DELTA)) {
  264. ts->idle_expires.tv64 = KTIME_MAX;
  265. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  266. hrtimer_cancel(&ts->sched_timer);
  267. goto out;
  268. }
  269. /*
  270. * calculate the expiry time for the next timer wheel
  271. * timer
  272. */
  273. expires = ktime_add_ns(last_update, tick_period.tv64 *
  274. delta_jiffies);
  275. ts->idle_expires = expires;
  276. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  277. hrtimer_start(&ts->sched_timer, expires,
  278. HRTIMER_MODE_ABS);
  279. /* Check, if the timer was already in the past */
  280. if (hrtimer_active(&ts->sched_timer))
  281. goto out;
  282. } else if (!tick_program_event(expires, 0))
  283. goto out;
  284. /*
  285. * We are past the event already. So we crossed a
  286. * jiffie boundary. Update jiffies and raise the
  287. * softirq.
  288. */
  289. tick_do_update_jiffies64(ktime_get());
  290. cpu_clear(cpu, nohz_cpu_mask);
  291. }
  292. raise_softirq_irqoff(TIMER_SOFTIRQ);
  293. out:
  294. ts->next_jiffies = next_jiffies;
  295. ts->last_jiffies = last_jiffies;
  296. ts->sleep_length = ktime_sub(dev->next_event, now);
  297. end:
  298. local_irq_restore(flags);
  299. }
  300. /**
  301. * tick_nohz_get_sleep_length - return the length of the current sleep
  302. *
  303. * Called from power state control code with interrupts disabled
  304. */
  305. ktime_t tick_nohz_get_sleep_length(void)
  306. {
  307. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  308. return ts->sleep_length;
  309. }
  310. /**
  311. * tick_nohz_restart_sched_tick - restart the idle tick from the idle task
  312. *
  313. * Restart the idle tick when the CPU is woken up from idle
  314. */
  315. void tick_nohz_restart_sched_tick(void)
  316. {
  317. int cpu = smp_processor_id();
  318. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  319. unsigned long ticks;
  320. ktime_t now;
  321. local_irq_disable();
  322. tick_nohz_stop_idle(cpu);
  323. if (!ts->tick_stopped) {
  324. local_irq_enable();
  325. return;
  326. }
  327. rcu_exit_nohz();
  328. /* Update jiffies first */
  329. select_nohz_load_balancer(0);
  330. now = ktime_get();
  331. tick_do_update_jiffies64(now);
  332. sched_clock_tick_start(cpu);
  333. cpu_clear(cpu, nohz_cpu_mask);
  334. /*
  335. * We stopped the tick in idle. Update process times would miss the
  336. * time we slept as update_process_times does only a 1 tick
  337. * accounting. Enforce that this is accounted to idle !
  338. */
  339. ticks = jiffies - ts->idle_jiffies;
  340. /*
  341. * We might be one off. Do not randomly account a huge number of ticks!
  342. */
  343. if (ticks && ticks < LONG_MAX) {
  344. add_preempt_count(HARDIRQ_OFFSET);
  345. account_system_time(current, HARDIRQ_OFFSET,
  346. jiffies_to_cputime(ticks));
  347. sub_preempt_count(HARDIRQ_OFFSET);
  348. }
  349. touch_softlockup_watchdog();
  350. /*
  351. * Cancel the scheduled timer and restore the tick
  352. */
  353. ts->tick_stopped = 0;
  354. ts->idle_exittime = now;
  355. hrtimer_cancel(&ts->sched_timer);
  356. ts->sched_timer.expires = ts->idle_tick;
  357. while (1) {
  358. /* Forward the time to expire in the future */
  359. hrtimer_forward(&ts->sched_timer, now, tick_period);
  360. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  361. hrtimer_start(&ts->sched_timer,
  362. ts->sched_timer.expires,
  363. HRTIMER_MODE_ABS);
  364. /* Check, if the timer was already in the past */
  365. if (hrtimer_active(&ts->sched_timer))
  366. break;
  367. } else {
  368. if (!tick_program_event(ts->sched_timer.expires, 0))
  369. break;
  370. }
  371. /* Update jiffies and reread time */
  372. tick_do_update_jiffies64(now);
  373. now = ktime_get();
  374. }
  375. local_irq_enable();
  376. }
  377. static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
  378. {
  379. hrtimer_forward(&ts->sched_timer, now, tick_period);
  380. return tick_program_event(ts->sched_timer.expires, 0);
  381. }
  382. /*
  383. * The nohz low res interrupt handler
  384. */
  385. static void tick_nohz_handler(struct clock_event_device *dev)
  386. {
  387. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  388. struct pt_regs *regs = get_irq_regs();
  389. int cpu = smp_processor_id();
  390. ktime_t now = ktime_get();
  391. dev->next_event.tv64 = KTIME_MAX;
  392. /*
  393. * Check if the do_timer duty was dropped. We don't care about
  394. * concurrency: This happens only when the cpu in charge went
  395. * into a long sleep. If two cpus happen to assign themself to
  396. * this duty, then the jiffies update is still serialized by
  397. * xtime_lock.
  398. */
  399. if (unlikely(tick_do_timer_cpu == -1))
  400. tick_do_timer_cpu = cpu;
  401. /* Check, if the jiffies need an update */
  402. if (tick_do_timer_cpu == cpu)
  403. tick_do_update_jiffies64(now);
  404. /*
  405. * When we are idle and the tick is stopped, we have to touch
  406. * the watchdog as we might not schedule for a really long
  407. * time. This happens on complete idle SMP systems while
  408. * waiting on the login prompt. We also increment the "start
  409. * of idle" jiffy stamp so the idle accounting adjustment we
  410. * do when we go busy again does not account too much ticks.
  411. */
  412. if (ts->tick_stopped) {
  413. touch_softlockup_watchdog();
  414. ts->idle_jiffies++;
  415. }
  416. update_process_times(user_mode(regs));
  417. profile_tick(CPU_PROFILING);
  418. /* Do not restart, when we are in the idle loop */
  419. if (ts->tick_stopped)
  420. return;
  421. while (tick_nohz_reprogram(ts, now)) {
  422. now = ktime_get();
  423. tick_do_update_jiffies64(now);
  424. }
  425. }
  426. /**
  427. * tick_nohz_switch_to_nohz - switch to nohz mode
  428. */
  429. static void tick_nohz_switch_to_nohz(void)
  430. {
  431. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  432. ktime_t next;
  433. if (!tick_nohz_enabled)
  434. return;
  435. local_irq_disable();
  436. if (tick_switch_to_oneshot(tick_nohz_handler)) {
  437. local_irq_enable();
  438. return;
  439. }
  440. ts->nohz_mode = NOHZ_MODE_LOWRES;
  441. /*
  442. * Recycle the hrtimer in ts, so we can share the
  443. * hrtimer_forward with the highres code.
  444. */
  445. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  446. /* Get the next period */
  447. next = tick_init_jiffy_update();
  448. for (;;) {
  449. ts->sched_timer.expires = next;
  450. if (!tick_program_event(next, 0))
  451. break;
  452. next = ktime_add(next, tick_period);
  453. }
  454. local_irq_enable();
  455. printk(KERN_INFO "Switched to NOHz mode on CPU #%d\n",
  456. smp_processor_id());
  457. }
  458. #else
  459. static inline void tick_nohz_switch_to_nohz(void) { }
  460. #endif /* NO_HZ */
  461. /*
  462. * High resolution timer specific code
  463. */
  464. #ifdef CONFIG_HIGH_RES_TIMERS
  465. /*
  466. * We rearm the timer until we get disabled by the idle code.
  467. * Called with interrupts disabled and timer->base->cpu_base->lock held.
  468. */
  469. static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
  470. {
  471. struct tick_sched *ts =
  472. container_of(timer, struct tick_sched, sched_timer);
  473. struct pt_regs *regs = get_irq_regs();
  474. ktime_t now = ktime_get();
  475. int cpu = smp_processor_id();
  476. #ifdef CONFIG_NO_HZ
  477. /*
  478. * Check if the do_timer duty was dropped. We don't care about
  479. * concurrency: This happens only when the cpu in charge went
  480. * into a long sleep. If two cpus happen to assign themself to
  481. * this duty, then the jiffies update is still serialized by
  482. * xtime_lock.
  483. */
  484. if (unlikely(tick_do_timer_cpu == -1))
  485. tick_do_timer_cpu = cpu;
  486. #endif
  487. /* Check, if the jiffies need an update */
  488. if (tick_do_timer_cpu == cpu)
  489. tick_do_update_jiffies64(now);
  490. /*
  491. * Do not call, when we are not in irq context and have
  492. * no valid regs pointer
  493. */
  494. if (regs) {
  495. /*
  496. * When we are idle and the tick is stopped, we have to touch
  497. * the watchdog as we might not schedule for a really long
  498. * time. This happens on complete idle SMP systems while
  499. * waiting on the login prompt. We also increment the "start of
  500. * idle" jiffy stamp so the idle accounting adjustment we do
  501. * when we go busy again does not account too much ticks.
  502. */
  503. if (ts->tick_stopped) {
  504. touch_softlockup_watchdog();
  505. ts->idle_jiffies++;
  506. }
  507. update_process_times(user_mode(regs));
  508. profile_tick(CPU_PROFILING);
  509. }
  510. /* Do not restart, when we are in the idle loop */
  511. if (ts->tick_stopped)
  512. return HRTIMER_NORESTART;
  513. hrtimer_forward(timer, now, tick_period);
  514. return HRTIMER_RESTART;
  515. }
  516. /**
  517. * tick_setup_sched_timer - setup the tick emulation timer
  518. */
  519. void tick_setup_sched_timer(void)
  520. {
  521. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  522. ktime_t now = ktime_get();
  523. u64 offset;
  524. /*
  525. * Emulate tick processing via per-CPU hrtimers:
  526. */
  527. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  528. ts->sched_timer.function = tick_sched_timer;
  529. ts->sched_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
  530. /* Get the next period (per cpu) */
  531. ts->sched_timer.expires = tick_init_jiffy_update();
  532. offset = ktime_to_ns(tick_period) >> 1;
  533. do_div(offset, num_possible_cpus());
  534. offset *= smp_processor_id();
  535. ts->sched_timer.expires = ktime_add_ns(ts->sched_timer.expires, offset);
  536. for (;;) {
  537. hrtimer_forward(&ts->sched_timer, now, tick_period);
  538. hrtimer_start(&ts->sched_timer, ts->sched_timer.expires,
  539. HRTIMER_MODE_ABS);
  540. /* Check, if the timer was already in the past */
  541. if (hrtimer_active(&ts->sched_timer))
  542. break;
  543. now = ktime_get();
  544. }
  545. #ifdef CONFIG_NO_HZ
  546. if (tick_nohz_enabled)
  547. ts->nohz_mode = NOHZ_MODE_HIGHRES;
  548. #endif
  549. }
  550. void tick_cancel_sched_timer(int cpu)
  551. {
  552. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  553. if (ts->sched_timer.base)
  554. hrtimer_cancel(&ts->sched_timer);
  555. ts->nohz_mode = NOHZ_MODE_INACTIVE;
  556. }
  557. #endif /* HIGH_RES_TIMERS */
  558. /**
  559. * Async notification about clocksource changes
  560. */
  561. void tick_clock_notify(void)
  562. {
  563. int cpu;
  564. for_each_possible_cpu(cpu)
  565. set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
  566. }
  567. /*
  568. * Async notification about clock event changes
  569. */
  570. void tick_oneshot_notify(void)
  571. {
  572. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  573. set_bit(0, &ts->check_clocks);
  574. }
  575. /**
  576. * Check, if a change happened, which makes oneshot possible.
  577. *
  578. * Called cyclic from the hrtimer softirq (driven by the timer
  579. * softirq) allow_nohz signals, that we can switch into low-res nohz
  580. * mode, because high resolution timers are disabled (either compile
  581. * or runtime).
  582. */
  583. int tick_check_oneshot_change(int allow_nohz)
  584. {
  585. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  586. if (!test_and_clear_bit(0, &ts->check_clocks))
  587. return 0;
  588. if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
  589. return 0;
  590. if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
  591. return 0;
  592. if (!allow_nohz)
  593. return 1;
  594. tick_nohz_switch_to_nohz();
  595. return 0;
  596. }