tick-sched.c 24 KB

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