tick-sched.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /*
  2. * linux/kernel/time/tick-sched.c
  3. *
  4. * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
  6. * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
  7. *
  8. * No idle tick implementation for low and high resolution timers
  9. *
  10. * Started by: Thomas Gleixner and Ingo Molnar
  11. *
  12. * Distribute under GPLv2.
  13. */
  14. #include <linux/cpu.h>
  15. #include <linux/err.h>
  16. #include <linux/hrtimer.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/percpu.h>
  20. #include <linux/profile.h>
  21. #include <linux/sched.h>
  22. #include <linux/module.h>
  23. #include <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. static void tick_nohz_update_jiffies(ktime_t now)
  117. {
  118. int cpu = smp_processor_id();
  119. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  120. unsigned long flags;
  121. ts->idle_waketime = now;
  122. local_irq_save(flags);
  123. tick_do_update_jiffies64(now);
  124. local_irq_restore(flags);
  125. touch_softlockup_watchdog();
  126. }
  127. /*
  128. * Updates the per cpu time idle statistics counters
  129. */
  130. static void
  131. update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
  132. {
  133. ktime_t delta;
  134. if (ts->idle_active) {
  135. delta = ktime_sub(now, ts->idle_entrytime);
  136. if (nr_iowait_cpu(cpu) > 0)
  137. ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
  138. else
  139. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  140. ts->idle_entrytime = now;
  141. }
  142. if (last_update_time)
  143. *last_update_time = ktime_to_us(now);
  144. }
  145. static void tick_nohz_stop_idle(int cpu, ktime_t now)
  146. {
  147. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  148. update_ts_time_stats(cpu, ts, now, NULL);
  149. ts->idle_active = 0;
  150. sched_clock_idle_wakeup_event(0);
  151. }
  152. static ktime_t tick_nohz_start_idle(int cpu, struct tick_sched *ts)
  153. {
  154. ktime_t now;
  155. now = ktime_get();
  156. update_ts_time_stats(cpu, ts, now, NULL);
  157. ts->idle_entrytime = now;
  158. ts->idle_active = 1;
  159. sched_clock_idle_sleep_event();
  160. return now;
  161. }
  162. /**
  163. * get_cpu_idle_time_us - get the total idle time of a cpu
  164. * @cpu: CPU number to query
  165. * @last_update_time: variable to store update time in. Do not update
  166. * counters if NULL.
  167. *
  168. * Return the cummulative idle time (since boot) for a given
  169. * CPU, in microseconds.
  170. *
  171. * This time is measured via accounting rather than sampling,
  172. * and is as accurate as ktime_get() is.
  173. *
  174. * This function returns -1 if NOHZ is not enabled.
  175. */
  176. u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
  177. {
  178. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  179. ktime_t now, idle;
  180. if (!tick_nohz_enabled)
  181. return -1;
  182. now = ktime_get();
  183. if (last_update_time) {
  184. update_ts_time_stats(cpu, ts, now, last_update_time);
  185. idle = ts->idle_sleeptime;
  186. } else {
  187. if (ts->idle_active && !nr_iowait_cpu(cpu)) {
  188. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  189. idle = ktime_add(ts->idle_sleeptime, delta);
  190. } else {
  191. idle = ts->idle_sleeptime;
  192. }
  193. }
  194. return ktime_to_us(idle);
  195. }
  196. EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
  197. /**
  198. * get_cpu_iowait_time_us - get the total iowait time of a cpu
  199. * @cpu: CPU number to query
  200. * @last_update_time: variable to store update time in. Do not update
  201. * counters if NULL.
  202. *
  203. * Return the cummulative iowait time (since boot) for a given
  204. * CPU, in microseconds.
  205. *
  206. * This time is measured via accounting rather than sampling,
  207. * and is as accurate as ktime_get() is.
  208. *
  209. * This function returns -1 if NOHZ is not enabled.
  210. */
  211. u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
  212. {
  213. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  214. ktime_t now, iowait;
  215. if (!tick_nohz_enabled)
  216. return -1;
  217. now = ktime_get();
  218. if (last_update_time) {
  219. update_ts_time_stats(cpu, ts, now, last_update_time);
  220. iowait = ts->iowait_sleeptime;
  221. } else {
  222. if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
  223. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  224. iowait = ktime_add(ts->iowait_sleeptime, delta);
  225. } else {
  226. iowait = ts->iowait_sleeptime;
  227. }
  228. }
  229. return ktime_to_us(iowait);
  230. }
  231. EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
  232. static void tick_nohz_stop_sched_tick(struct tick_sched *ts)
  233. {
  234. unsigned long seq, last_jiffies, next_jiffies, delta_jiffies;
  235. ktime_t last_update, expires, now;
  236. struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
  237. u64 time_delta;
  238. int cpu;
  239. cpu = smp_processor_id();
  240. ts = &per_cpu(tick_cpu_sched, cpu);
  241. now = tick_nohz_start_idle(cpu, ts);
  242. /*
  243. * If this cpu is offline and it is the one which updates
  244. * jiffies, then give up the assignment and let it be taken by
  245. * the cpu which runs the tick timer next. If we don't drop
  246. * this here the jiffies might be stale and do_timer() never
  247. * invoked.
  248. */
  249. if (unlikely(!cpu_online(cpu))) {
  250. if (cpu == tick_do_timer_cpu)
  251. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  252. }
  253. if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
  254. return;
  255. if (need_resched())
  256. return;
  257. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  258. static int ratelimit;
  259. if (ratelimit < 10) {
  260. printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  261. (unsigned int) local_softirq_pending());
  262. ratelimit++;
  263. }
  264. return;
  265. }
  266. ts->idle_calls++;
  267. /* Read jiffies and the time when jiffies were updated last */
  268. do {
  269. seq = read_seqbegin(&xtime_lock);
  270. last_update = last_jiffies_update;
  271. last_jiffies = jiffies;
  272. time_delta = timekeeping_max_deferment();
  273. } while (read_seqretry(&xtime_lock, seq));
  274. if (rcu_needs_cpu(cpu) || printk_needs_cpu(cpu) ||
  275. arch_needs_cpu(cpu)) {
  276. next_jiffies = last_jiffies + 1;
  277. delta_jiffies = 1;
  278. } else {
  279. /* Get the next timer wheel timer */
  280. next_jiffies = get_next_timer_interrupt(last_jiffies);
  281. delta_jiffies = next_jiffies - last_jiffies;
  282. }
  283. /*
  284. * Do not stop the tick, if we are only one off
  285. * or if the cpu is required for rcu
  286. */
  287. if (!ts->tick_stopped && delta_jiffies == 1)
  288. goto out;
  289. /* Schedule the tick, if we are at least one jiffie off */
  290. if ((long)delta_jiffies >= 1) {
  291. /*
  292. * If this cpu is the one which updates jiffies, then
  293. * give up the assignment and let it be taken by the
  294. * cpu which runs the tick timer next, which might be
  295. * this cpu as well. If we don't drop this here the
  296. * jiffies might be stale and do_timer() never
  297. * invoked. Keep track of the fact that it was the one
  298. * which had the do_timer() duty last. If this cpu is
  299. * the one which had the do_timer() duty last, we
  300. * limit the sleep time to the timekeeping
  301. * max_deferement value which we retrieved
  302. * above. Otherwise we can sleep as long as we want.
  303. */
  304. if (cpu == tick_do_timer_cpu) {
  305. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  306. ts->do_timer_last = 1;
  307. } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
  308. time_delta = KTIME_MAX;
  309. ts->do_timer_last = 0;
  310. } else if (!ts->do_timer_last) {
  311. time_delta = KTIME_MAX;
  312. }
  313. /*
  314. * calculate the expiry time for the next timer wheel
  315. * timer. delta_jiffies >= NEXT_TIMER_MAX_DELTA signals
  316. * that there is no timer pending or at least extremely
  317. * far into the future (12 days for HZ=1000). In this
  318. * case we set the expiry to the end of time.
  319. */
  320. if (likely(delta_jiffies < NEXT_TIMER_MAX_DELTA)) {
  321. /*
  322. * Calculate the time delta for the next timer event.
  323. * If the time delta exceeds the maximum time delta
  324. * permitted by the current clocksource then adjust
  325. * the time delta accordingly to ensure the
  326. * clocksource does not wrap.
  327. */
  328. time_delta = min_t(u64, time_delta,
  329. tick_period.tv64 * delta_jiffies);
  330. }
  331. if (time_delta < KTIME_MAX)
  332. expires = ktime_add_ns(last_update, time_delta);
  333. else
  334. expires.tv64 = KTIME_MAX;
  335. /* Skip reprogram of event if its not changed */
  336. if (ts->tick_stopped && ktime_equal(expires, dev->next_event))
  337. goto out;
  338. /*
  339. * nohz_stop_sched_tick can be called several times before
  340. * the nohz_restart_sched_tick is called. This happens when
  341. * interrupts arrive which do not cause a reschedule. In the
  342. * first call we save the current tick time, so we can restart
  343. * the scheduler tick in nohz_restart_sched_tick.
  344. */
  345. if (!ts->tick_stopped) {
  346. select_nohz_load_balancer(1);
  347. ts->idle_tick = hrtimer_get_expires(&ts->sched_timer);
  348. ts->tick_stopped = 1;
  349. ts->idle_jiffies = last_jiffies;
  350. }
  351. ts->idle_sleeps++;
  352. /* Mark expires */
  353. ts->idle_expires = expires;
  354. /*
  355. * If the expiration time == KTIME_MAX, then
  356. * in this case we simply stop the tick timer.
  357. */
  358. if (unlikely(expires.tv64 == KTIME_MAX)) {
  359. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  360. hrtimer_cancel(&ts->sched_timer);
  361. goto out;
  362. }
  363. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  364. hrtimer_start(&ts->sched_timer, expires,
  365. HRTIMER_MODE_ABS_PINNED);
  366. /* Check, if the timer was already in the past */
  367. if (hrtimer_active(&ts->sched_timer))
  368. goto out;
  369. } else if (!tick_program_event(expires, 0))
  370. goto out;
  371. /*
  372. * We are past the event already. So we crossed a
  373. * jiffie boundary. Update jiffies and raise the
  374. * softirq.
  375. */
  376. tick_do_update_jiffies64(ktime_get());
  377. }
  378. raise_softirq_irqoff(TIMER_SOFTIRQ);
  379. out:
  380. ts->next_jiffies = next_jiffies;
  381. ts->last_jiffies = last_jiffies;
  382. ts->sleep_length = ktime_sub(dev->next_event, now);
  383. }
  384. /**
  385. * tick_nohz_idle_enter - stop the idle tick from the idle task
  386. *
  387. * When the next event is more than a tick into the future, stop the idle tick
  388. * Called when we start the idle loop.
  389. *
  390. * The arch is responsible of calling:
  391. *
  392. * - rcu_idle_enter() after its last use of RCU before the CPU is put
  393. * to sleep.
  394. * - rcu_idle_exit() before the first use of RCU after the CPU is woken up.
  395. */
  396. void tick_nohz_idle_enter(void)
  397. {
  398. struct tick_sched *ts;
  399. WARN_ON_ONCE(irqs_disabled());
  400. /*
  401. * Update the idle state in the scheduler domain hierarchy
  402. * when tick_nohz_stop_sched_tick() is called from the idle loop.
  403. * State will be updated to busy during the first busy tick after
  404. * exiting idle.
  405. */
  406. set_cpu_sd_state_idle();
  407. local_irq_disable();
  408. ts = &__get_cpu_var(tick_cpu_sched);
  409. /*
  410. * set ts->inidle unconditionally. even if the system did not
  411. * switch to nohz mode the cpu frequency governers rely on the
  412. * update of the idle time accounting in tick_nohz_start_idle().
  413. */
  414. ts->inidle = 1;
  415. tick_nohz_stop_sched_tick(ts);
  416. local_irq_enable();
  417. }
  418. /**
  419. * tick_nohz_irq_exit - update next tick event from interrupt exit
  420. *
  421. * When an interrupt fires while we are idle and it doesn't cause
  422. * a reschedule, it may still add, modify or delete a timer, enqueue
  423. * an RCU callback, etc...
  424. * So we need to re-calculate and reprogram the next tick event.
  425. */
  426. void tick_nohz_irq_exit(void)
  427. {
  428. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  429. if (!ts->inidle)
  430. return;
  431. tick_nohz_stop_sched_tick(ts);
  432. }
  433. /**
  434. * tick_nohz_get_sleep_length - return the length of the current sleep
  435. *
  436. * Called from power state control code with interrupts disabled
  437. */
  438. ktime_t tick_nohz_get_sleep_length(void)
  439. {
  440. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  441. return ts->sleep_length;
  442. }
  443. static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
  444. {
  445. hrtimer_cancel(&ts->sched_timer);
  446. hrtimer_set_expires(&ts->sched_timer, ts->idle_tick);
  447. while (1) {
  448. /* Forward the time to expire in the future */
  449. hrtimer_forward(&ts->sched_timer, now, tick_period);
  450. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  451. hrtimer_start_expires(&ts->sched_timer,
  452. HRTIMER_MODE_ABS_PINNED);
  453. /* Check, if the timer was already in the past */
  454. if (hrtimer_active(&ts->sched_timer))
  455. break;
  456. } else {
  457. if (!tick_program_event(
  458. hrtimer_get_expires(&ts->sched_timer), 0))
  459. break;
  460. }
  461. /* Update jiffies and reread time */
  462. tick_do_update_jiffies64(now);
  463. now = ktime_get();
  464. }
  465. }
  466. /**
  467. * tick_nohz_idle_exit - restart the idle tick from the idle task
  468. *
  469. * Restart the idle tick when the CPU is woken up from idle
  470. * This also exit the RCU extended quiescent state. The CPU
  471. * can use RCU again after this function is called.
  472. */
  473. void tick_nohz_idle_exit(void)
  474. {
  475. int cpu = smp_processor_id();
  476. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  477. #ifndef CONFIG_VIRT_CPU_ACCOUNTING
  478. unsigned long ticks;
  479. #endif
  480. ktime_t now;
  481. local_irq_disable();
  482. if (ts->idle_active || (ts->inidle && ts->tick_stopped))
  483. now = ktime_get();
  484. if (ts->idle_active)
  485. tick_nohz_stop_idle(cpu, now);
  486. if (!ts->inidle || !ts->tick_stopped) {
  487. ts->inidle = 0;
  488. local_irq_enable();
  489. return;
  490. }
  491. ts->inidle = 0;
  492. /* Update jiffies first */
  493. select_nohz_load_balancer(0);
  494. tick_do_update_jiffies64(now);
  495. #ifndef CONFIG_VIRT_CPU_ACCOUNTING
  496. /*
  497. * We stopped the tick in idle. Update process times would miss the
  498. * time we slept as update_process_times does only a 1 tick
  499. * accounting. Enforce that this is accounted to idle !
  500. */
  501. ticks = jiffies - ts->idle_jiffies;
  502. /*
  503. * We might be one off. Do not randomly account a huge number of ticks!
  504. */
  505. if (ticks && ticks < LONG_MAX)
  506. account_idle_ticks(ticks);
  507. #endif
  508. touch_softlockup_watchdog();
  509. /*
  510. * Cancel the scheduled timer and restore the tick
  511. */
  512. ts->tick_stopped = 0;
  513. ts->idle_exittime = now;
  514. tick_nohz_restart(ts, now);
  515. local_irq_enable();
  516. }
  517. static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
  518. {
  519. hrtimer_forward(&ts->sched_timer, now, tick_period);
  520. return tick_program_event(hrtimer_get_expires(&ts->sched_timer), 0);
  521. }
  522. /*
  523. * The nohz low res interrupt handler
  524. */
  525. static void tick_nohz_handler(struct clock_event_device *dev)
  526. {
  527. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  528. struct pt_regs *regs = get_irq_regs();
  529. int cpu = smp_processor_id();
  530. ktime_t now = ktime_get();
  531. dev->next_event.tv64 = KTIME_MAX;
  532. /*
  533. * Check if the do_timer duty was dropped. We don't care about
  534. * concurrency: This happens only when the cpu in charge went
  535. * into a long sleep. If two cpus happen to assign themself to
  536. * this duty, then the jiffies update is still serialized by
  537. * xtime_lock.
  538. */
  539. if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
  540. tick_do_timer_cpu = cpu;
  541. /* Check, if the jiffies need an update */
  542. if (tick_do_timer_cpu == cpu)
  543. tick_do_update_jiffies64(now);
  544. /*
  545. * When we are idle and the tick is stopped, we have to touch
  546. * the watchdog as we might not schedule for a really long
  547. * time. This happens on complete idle SMP systems while
  548. * waiting on the login prompt. We also increment the "start
  549. * of idle" jiffy stamp so the idle accounting adjustment we
  550. * do when we go busy again does not account too much ticks.
  551. */
  552. if (ts->tick_stopped) {
  553. touch_softlockup_watchdog();
  554. ts->idle_jiffies++;
  555. }
  556. update_process_times(user_mode(regs));
  557. profile_tick(CPU_PROFILING);
  558. while (tick_nohz_reprogram(ts, now)) {
  559. now = ktime_get();
  560. tick_do_update_jiffies64(now);
  561. }
  562. }
  563. /**
  564. * tick_nohz_switch_to_nohz - switch to nohz mode
  565. */
  566. static void tick_nohz_switch_to_nohz(void)
  567. {
  568. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  569. ktime_t next;
  570. if (!tick_nohz_enabled)
  571. return;
  572. local_irq_disable();
  573. if (tick_switch_to_oneshot(tick_nohz_handler)) {
  574. local_irq_enable();
  575. return;
  576. }
  577. ts->nohz_mode = NOHZ_MODE_LOWRES;
  578. /*
  579. * Recycle the hrtimer in ts, so we can share the
  580. * hrtimer_forward with the highres code.
  581. */
  582. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  583. /* Get the next period */
  584. next = tick_init_jiffy_update();
  585. for (;;) {
  586. hrtimer_set_expires(&ts->sched_timer, next);
  587. if (!tick_program_event(next, 0))
  588. break;
  589. next = ktime_add(next, tick_period);
  590. }
  591. local_irq_enable();
  592. }
  593. /*
  594. * When NOHZ is enabled and the tick is stopped, we need to kick the
  595. * tick timer from irq_enter() so that the jiffies update is kept
  596. * alive during long running softirqs. That's ugly as hell, but
  597. * correctness is key even if we need to fix the offending softirq in
  598. * the first place.
  599. *
  600. * Note, this is different to tick_nohz_restart. We just kick the
  601. * timer and do not touch the other magic bits which need to be done
  602. * when idle is left.
  603. */
  604. static void tick_nohz_kick_tick(int cpu, ktime_t now)
  605. {
  606. #if 0
  607. /* Switch back to 2.6.27 behaviour */
  608. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  609. ktime_t delta;
  610. /*
  611. * Do not touch the tick device, when the next expiry is either
  612. * already reached or less/equal than the tick period.
  613. */
  614. delta = ktime_sub(hrtimer_get_expires(&ts->sched_timer), now);
  615. if (delta.tv64 <= tick_period.tv64)
  616. return;
  617. tick_nohz_restart(ts, now);
  618. #endif
  619. }
  620. static inline void tick_check_nohz(int cpu)
  621. {
  622. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  623. ktime_t now;
  624. if (!ts->idle_active && !ts->tick_stopped)
  625. return;
  626. now = ktime_get();
  627. if (ts->idle_active)
  628. tick_nohz_stop_idle(cpu, now);
  629. if (ts->tick_stopped) {
  630. tick_nohz_update_jiffies(now);
  631. tick_nohz_kick_tick(cpu, now);
  632. }
  633. }
  634. #else
  635. static inline void tick_nohz_switch_to_nohz(void) { }
  636. static inline void tick_check_nohz(int cpu) { }
  637. #endif /* NO_HZ */
  638. /*
  639. * Called from irq_enter to notify about the possible interruption of idle()
  640. */
  641. void tick_check_idle(int cpu)
  642. {
  643. tick_check_oneshot_broadcast(cpu);
  644. tick_check_nohz(cpu);
  645. }
  646. /*
  647. * High resolution timer specific code
  648. */
  649. #ifdef CONFIG_HIGH_RES_TIMERS
  650. /*
  651. * We rearm the timer until we get disabled by the idle code.
  652. * Called with interrupts disabled and timer->base->cpu_base->lock held.
  653. */
  654. static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
  655. {
  656. struct tick_sched *ts =
  657. container_of(timer, struct tick_sched, sched_timer);
  658. struct pt_regs *regs = get_irq_regs();
  659. ktime_t now = ktime_get();
  660. int cpu = smp_processor_id();
  661. #ifdef CONFIG_NO_HZ
  662. /*
  663. * Check if the do_timer duty was dropped. We don't care about
  664. * concurrency: This happens only when the cpu in charge went
  665. * into a long sleep. If two cpus happen to assign themself to
  666. * this duty, then the jiffies update is still serialized by
  667. * xtime_lock.
  668. */
  669. if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
  670. tick_do_timer_cpu = cpu;
  671. #endif
  672. /* Check, if the jiffies need an update */
  673. if (tick_do_timer_cpu == cpu)
  674. tick_do_update_jiffies64(now);
  675. /*
  676. * Do not call, when we are not in irq context and have
  677. * no valid regs pointer
  678. */
  679. if (regs) {
  680. /*
  681. * When we are idle and the tick is stopped, we have to touch
  682. * the watchdog as we might not schedule for a really long
  683. * time. This happens on complete idle SMP systems while
  684. * waiting on the login prompt. We also increment the "start of
  685. * idle" jiffy stamp so the idle accounting adjustment we do
  686. * when we go busy again does not account too much ticks.
  687. */
  688. if (ts->tick_stopped) {
  689. touch_softlockup_watchdog();
  690. ts->idle_jiffies++;
  691. }
  692. update_process_times(user_mode(regs));
  693. profile_tick(CPU_PROFILING);
  694. }
  695. hrtimer_forward(timer, now, tick_period);
  696. return HRTIMER_RESTART;
  697. }
  698. /**
  699. * tick_setup_sched_timer - setup the tick emulation timer
  700. */
  701. void tick_setup_sched_timer(void)
  702. {
  703. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  704. ktime_t now = ktime_get();
  705. /*
  706. * Emulate tick processing via per-CPU hrtimers:
  707. */
  708. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  709. ts->sched_timer.function = tick_sched_timer;
  710. /* Get the next period (per cpu) */
  711. hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
  712. for (;;) {
  713. hrtimer_forward(&ts->sched_timer, now, tick_period);
  714. hrtimer_start_expires(&ts->sched_timer,
  715. HRTIMER_MODE_ABS_PINNED);
  716. /* Check, if the timer was already in the past */
  717. if (hrtimer_active(&ts->sched_timer))
  718. break;
  719. now = ktime_get();
  720. }
  721. #ifdef CONFIG_NO_HZ
  722. if (tick_nohz_enabled)
  723. ts->nohz_mode = NOHZ_MODE_HIGHRES;
  724. #endif
  725. }
  726. #endif /* HIGH_RES_TIMERS */
  727. #if defined CONFIG_NO_HZ || defined CONFIG_HIGH_RES_TIMERS
  728. void tick_cancel_sched_timer(int cpu)
  729. {
  730. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  731. # ifdef CONFIG_HIGH_RES_TIMERS
  732. if (ts->sched_timer.base)
  733. hrtimer_cancel(&ts->sched_timer);
  734. # endif
  735. ts->nohz_mode = NOHZ_MODE_INACTIVE;
  736. }
  737. #endif
  738. /**
  739. * Async notification about clocksource changes
  740. */
  741. void tick_clock_notify(void)
  742. {
  743. int cpu;
  744. for_each_possible_cpu(cpu)
  745. set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
  746. }
  747. /*
  748. * Async notification about clock event changes
  749. */
  750. void tick_oneshot_notify(void)
  751. {
  752. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  753. set_bit(0, &ts->check_clocks);
  754. }
  755. /**
  756. * Check, if a change happened, which makes oneshot possible.
  757. *
  758. * Called cyclic from the hrtimer softirq (driven by the timer
  759. * softirq) allow_nohz signals, that we can switch into low-res nohz
  760. * mode, because high resolution timers are disabled (either compile
  761. * or runtime).
  762. */
  763. int tick_check_oneshot_change(int allow_nohz)
  764. {
  765. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  766. if (!test_and_clear_bit(0, &ts->check_clocks))
  767. return 0;
  768. if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
  769. return 0;
  770. if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
  771. return 0;
  772. if (!allow_nohz)
  773. return 1;
  774. tick_nohz_switch_to_nohz();
  775. return 0;
  776. }