tick-sched.c 18 KB

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