tick-sched.c 30 KB

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