tick-sched.c 27 KB

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