tick-sched.c 26 KB

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