tick-sched.c 19 KB

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