tick-sched.c 17 KB

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