tick-sched.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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/nmi.h>
  21. #include <linux/profile.h>
  22. #include <linux/sched/signal.h>
  23. #include <linux/sched/clock.h>
  24. #include <linux/sched/stat.h>
  25. #include <linux/sched/nohz.h>
  26. #include <linux/module.h>
  27. #include <linux/irq_work.h>
  28. #include <linux/posix-timers.h>
  29. #include <linux/context_tracking.h>
  30. #include <linux/mm.h>
  31. #include <asm/irq_regs.h>
  32. #include "tick-internal.h"
  33. #include <trace/events/timer.h>
  34. /*
  35. * Per-CPU nohz control structure
  36. */
  37. static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
  38. struct tick_sched *tick_get_tick_sched(int cpu)
  39. {
  40. return &per_cpu(tick_cpu_sched, cpu);
  41. }
  42. #if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
  43. /*
  44. * The time, when the last jiffy update happened. Protected by jiffies_lock.
  45. */
  46. static ktime_t last_jiffies_update;
  47. /*
  48. * Must be called with interrupts disabled !
  49. */
  50. static void tick_do_update_jiffies64(ktime_t now)
  51. {
  52. unsigned long ticks = 0;
  53. ktime_t delta;
  54. /*
  55. * Do a quick check without holding jiffies_lock:
  56. * The READ_ONCE() pairs with two updates done later in this function.
  57. */
  58. delta = ktime_sub(now, READ_ONCE(last_jiffies_update));
  59. if (delta < tick_period)
  60. return;
  61. /* Reevaluate with jiffies_lock held */
  62. write_seqlock(&jiffies_lock);
  63. delta = ktime_sub(now, last_jiffies_update);
  64. if (delta >= tick_period) {
  65. delta = ktime_sub(delta, tick_period);
  66. /* Pairs with the lockless read in this function. */
  67. WRITE_ONCE(last_jiffies_update,
  68. ktime_add(last_jiffies_update, tick_period));
  69. /* Slow path for long timeouts */
  70. if (unlikely(delta >= tick_period)) {
  71. s64 incr = ktime_to_ns(tick_period);
  72. ticks = ktime_divns(delta, incr);
  73. /* Pairs with the lockless read in this function. */
  74. WRITE_ONCE(last_jiffies_update,
  75. ktime_add_ns(last_jiffies_update,
  76. incr * ticks));
  77. }
  78. do_timer(++ticks);
  79. /* Keep the tick_next_period variable up to date */
  80. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  81. } else {
  82. write_sequnlock(&jiffies_lock);
  83. return;
  84. }
  85. write_sequnlock(&jiffies_lock);
  86. update_wall_time();
  87. }
  88. /*
  89. * Initialize and return retrieve the jiffies update.
  90. */
  91. static ktime_t tick_init_jiffy_update(void)
  92. {
  93. ktime_t period;
  94. write_seqlock(&jiffies_lock);
  95. /* Did we start the jiffies update yet ? */
  96. if (last_jiffies_update == 0)
  97. last_jiffies_update = tick_next_period;
  98. period = last_jiffies_update;
  99. write_sequnlock(&jiffies_lock);
  100. return period;
  101. }
  102. static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
  103. {
  104. int cpu = smp_processor_id();
  105. #ifdef CONFIG_NO_HZ_COMMON
  106. /*
  107. * Check if the do_timer duty was dropped. We don't care about
  108. * concurrency: This happens only when the CPU in charge went
  109. * into a long sleep. If two CPUs happen to assign themselves to
  110. * this duty, then the jiffies update is still serialized by
  111. * jiffies_lock.
  112. */
  113. if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE)
  114. && !tick_nohz_full_cpu(cpu))
  115. tick_do_timer_cpu = cpu;
  116. #endif
  117. /* Check, if the jiffies need an update */
  118. if (tick_do_timer_cpu == cpu)
  119. tick_do_update_jiffies64(now);
  120. if (ts->inidle)
  121. ts->got_idle_tick = 1;
  122. }
  123. static void tick_sched_handle(struct tick_sched *ts, struct pt_regs *regs)
  124. {
  125. #ifdef CONFIG_NO_HZ_COMMON
  126. /*
  127. * When we are idle and the tick is stopped, we have to touch
  128. * the watchdog as we might not schedule for a really long
  129. * time. This happens on complete idle SMP systems while
  130. * waiting on the login prompt. We also increment the "start of
  131. * idle" jiffy stamp so the idle accounting adjustment we do
  132. * when we go busy again does not account too much ticks.
  133. */
  134. if (ts->tick_stopped) {
  135. touch_softlockup_watchdog_sched();
  136. if (is_idle_task(current))
  137. ts->idle_jiffies++;
  138. /*
  139. * In case the current tick fired too early past its expected
  140. * expiration, make sure we don't bypass the next clock reprogramming
  141. * to the same deadline.
  142. */
  143. ts->next_tick = 0;
  144. }
  145. #endif
  146. update_process_times(user_mode(regs));
  147. profile_tick(CPU_PROFILING);
  148. }
  149. #endif
  150. #ifdef CONFIG_NO_HZ_FULL
  151. cpumask_var_t tick_nohz_full_mask;
  152. bool tick_nohz_full_running;
  153. static atomic_t tick_dep_mask;
  154. static bool check_tick_dependency(atomic_t *dep)
  155. {
  156. int val = atomic_read(dep);
  157. if (val & TICK_DEP_MASK_POSIX_TIMER) {
  158. trace_tick_stop(0, TICK_DEP_MASK_POSIX_TIMER);
  159. return true;
  160. }
  161. if (val & TICK_DEP_MASK_PERF_EVENTS) {
  162. trace_tick_stop(0, TICK_DEP_MASK_PERF_EVENTS);
  163. return true;
  164. }
  165. if (val & TICK_DEP_MASK_SCHED) {
  166. trace_tick_stop(0, TICK_DEP_MASK_SCHED);
  167. return true;
  168. }
  169. if (val & TICK_DEP_MASK_CLOCK_UNSTABLE) {
  170. trace_tick_stop(0, TICK_DEP_MASK_CLOCK_UNSTABLE);
  171. return true;
  172. }
  173. return false;
  174. }
  175. static bool can_stop_full_tick(int cpu, struct tick_sched *ts)
  176. {
  177. lockdep_assert_irqs_disabled();
  178. if (unlikely(!cpu_online(cpu)))
  179. return false;
  180. if (check_tick_dependency(&tick_dep_mask))
  181. return false;
  182. if (check_tick_dependency(&ts->tick_dep_mask))
  183. return false;
  184. if (check_tick_dependency(&current->tick_dep_mask))
  185. return false;
  186. if (check_tick_dependency(&current->signal->tick_dep_mask))
  187. return false;
  188. return true;
  189. }
  190. static void nohz_full_kick_func(struct irq_work *work)
  191. {
  192. /* Empty, the tick restart happens on tick_nohz_irq_exit() */
  193. }
  194. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  195. .func = nohz_full_kick_func,
  196. };
  197. /*
  198. * Kick this CPU if it's full dynticks in order to force it to
  199. * re-evaluate its dependency on the tick and restart it if necessary.
  200. * This kick, unlike tick_nohz_full_kick_cpu() and tick_nohz_full_kick_all(),
  201. * is NMI safe.
  202. */
  203. static void tick_nohz_full_kick(void)
  204. {
  205. if (!tick_nohz_full_cpu(smp_processor_id()))
  206. return;
  207. irq_work_queue(this_cpu_ptr(&nohz_full_kick_work));
  208. }
  209. /*
  210. * Kick the CPU if it's full dynticks in order to force it to
  211. * re-evaluate its dependency on the tick and restart it if necessary.
  212. */
  213. void tick_nohz_full_kick_cpu(int cpu)
  214. {
  215. if (!tick_nohz_full_cpu(cpu))
  216. return;
  217. irq_work_queue_on(&per_cpu(nohz_full_kick_work, cpu), cpu);
  218. }
  219. /*
  220. * Kick all full dynticks CPUs in order to force these to re-evaluate
  221. * their dependency on the tick and restart it if necessary.
  222. */
  223. static void tick_nohz_full_kick_all(void)
  224. {
  225. int cpu;
  226. if (!tick_nohz_full_running)
  227. return;
  228. preempt_disable();
  229. for_each_cpu_and(cpu, tick_nohz_full_mask, cpu_online_mask)
  230. tick_nohz_full_kick_cpu(cpu);
  231. preempt_enable();
  232. }
  233. static void tick_nohz_dep_set_all(atomic_t *dep,
  234. enum tick_dep_bits bit)
  235. {
  236. int prev;
  237. prev = atomic_fetch_or(BIT(bit), dep);
  238. if (!prev)
  239. tick_nohz_full_kick_all();
  240. }
  241. /*
  242. * Set a global tick dependency. Used by perf events that rely on freq and
  243. * by unstable clock.
  244. */
  245. void tick_nohz_dep_set(enum tick_dep_bits bit)
  246. {
  247. tick_nohz_dep_set_all(&tick_dep_mask, bit);
  248. }
  249. void tick_nohz_dep_clear(enum tick_dep_bits bit)
  250. {
  251. atomic_andnot(BIT(bit), &tick_dep_mask);
  252. }
  253. /*
  254. * Set per-CPU tick dependency. Used by scheduler and perf events in order to
  255. * manage events throttling.
  256. */
  257. void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit)
  258. {
  259. int prev;
  260. struct tick_sched *ts;
  261. ts = per_cpu_ptr(&tick_cpu_sched, cpu);
  262. prev = atomic_fetch_or(BIT(bit), &ts->tick_dep_mask);
  263. if (!prev) {
  264. preempt_disable();
  265. /* Perf needs local kick that is NMI safe */
  266. if (cpu == smp_processor_id()) {
  267. tick_nohz_full_kick();
  268. } else {
  269. /* Remote irq work not NMI-safe */
  270. if (!WARN_ON_ONCE(in_nmi()))
  271. tick_nohz_full_kick_cpu(cpu);
  272. }
  273. preempt_enable();
  274. }
  275. }
  276. void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit)
  277. {
  278. struct tick_sched *ts = per_cpu_ptr(&tick_cpu_sched, cpu);
  279. atomic_andnot(BIT(bit), &ts->tick_dep_mask);
  280. }
  281. /*
  282. * Set a per-task tick dependency. Posix CPU timers need this in order to elapse
  283. * per task timers.
  284. */
  285. void tick_nohz_dep_set_task(struct task_struct *tsk, enum tick_dep_bits bit)
  286. {
  287. /*
  288. * We could optimize this with just kicking the target running the task
  289. * if that noise matters for nohz full users.
  290. */
  291. tick_nohz_dep_set_all(&tsk->tick_dep_mask, bit);
  292. }
  293. void tick_nohz_dep_clear_task(struct task_struct *tsk, enum tick_dep_bits bit)
  294. {
  295. atomic_andnot(BIT(bit), &tsk->tick_dep_mask);
  296. }
  297. /*
  298. * Set a per-taskgroup tick dependency. Posix CPU timers need this in order to elapse
  299. * per process timers.
  300. */
  301. void tick_nohz_dep_set_signal(struct signal_struct *sig, enum tick_dep_bits bit)
  302. {
  303. tick_nohz_dep_set_all(&sig->tick_dep_mask, bit);
  304. }
  305. void tick_nohz_dep_clear_signal(struct signal_struct *sig, enum tick_dep_bits bit)
  306. {
  307. atomic_andnot(BIT(bit), &sig->tick_dep_mask);
  308. }
  309. /*
  310. * Re-evaluate the need for the tick as we switch the current task.
  311. * It might need the tick due to per task/process properties:
  312. * perf events, posix CPU timers, ...
  313. */
  314. void __tick_nohz_task_switch(void)
  315. {
  316. unsigned long flags;
  317. struct tick_sched *ts;
  318. local_irq_save(flags);
  319. if (!tick_nohz_full_cpu(smp_processor_id()))
  320. goto out;
  321. ts = this_cpu_ptr(&tick_cpu_sched);
  322. if (ts->tick_stopped) {
  323. if (atomic_read(&current->tick_dep_mask) ||
  324. atomic_read(&current->signal->tick_dep_mask))
  325. tick_nohz_full_kick();
  326. }
  327. out:
  328. local_irq_restore(flags);
  329. }
  330. /* Get the boot-time nohz CPU list from the kernel parameters. */
  331. void __init tick_nohz_full_setup(cpumask_var_t cpumask)
  332. {
  333. alloc_bootmem_cpumask_var(&tick_nohz_full_mask);
  334. cpumask_copy(tick_nohz_full_mask, cpumask);
  335. tick_nohz_full_running = true;
  336. }
  337. static int tick_nohz_cpu_down(unsigned int cpu)
  338. {
  339. /*
  340. * The boot CPU handles housekeeping duty (unbound timers,
  341. * workqueues, timekeeping, ...) on behalf of full dynticks
  342. * CPUs. It must remain online when nohz full is enabled.
  343. */
  344. if (tick_nohz_full_running && tick_do_timer_cpu == cpu)
  345. return -EBUSY;
  346. return 0;
  347. }
  348. void __init tick_nohz_init(void)
  349. {
  350. int cpu, ret;
  351. if (!tick_nohz_full_running)
  352. return;
  353. /*
  354. * Full dynticks uses irq work to drive the tick rescheduling on safe
  355. * locking contexts. But then we need irq work to raise its own
  356. * interrupts to avoid circular dependency on the tick
  357. */
  358. if (!arch_irq_work_has_interrupt()) {
  359. pr_warn("NO_HZ: Can't run full dynticks because arch doesn't support irq work self-IPIs\n");
  360. cpumask_clear(tick_nohz_full_mask);
  361. tick_nohz_full_running = false;
  362. return;
  363. }
  364. cpu = smp_processor_id();
  365. if (cpumask_test_cpu(cpu, tick_nohz_full_mask)) {
  366. pr_warn("NO_HZ: Clearing %d from nohz_full range for timekeeping\n",
  367. cpu);
  368. cpumask_clear_cpu(cpu, tick_nohz_full_mask);
  369. }
  370. for_each_cpu(cpu, tick_nohz_full_mask)
  371. context_tracking_cpu_set(cpu);
  372. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  373. "kernel/nohz:predown", NULL,
  374. tick_nohz_cpu_down);
  375. WARN_ON(ret < 0);
  376. pr_info("NO_HZ: Full dynticks CPUs: %*pbl.\n",
  377. cpumask_pr_args(tick_nohz_full_mask));
  378. }
  379. #endif
  380. /*
  381. * NOHZ - aka dynamic tick functionality
  382. */
  383. #ifdef CONFIG_NO_HZ_COMMON
  384. /*
  385. * NO HZ enabled ?
  386. */
  387. bool tick_nohz_enabled __read_mostly = true;
  388. unsigned long tick_nohz_active __read_mostly;
  389. /*
  390. * Enable / Disable tickless mode
  391. */
  392. static int __init setup_tick_nohz(char *str)
  393. {
  394. return (kstrtobool(str, &tick_nohz_enabled) == 0);
  395. }
  396. __setup("nohz=", setup_tick_nohz);
  397. bool tick_nohz_tick_stopped(void)
  398. {
  399. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  400. return ts->tick_stopped;
  401. }
  402. bool tick_nohz_tick_stopped_cpu(int cpu)
  403. {
  404. struct tick_sched *ts = per_cpu_ptr(&tick_cpu_sched, cpu);
  405. return ts->tick_stopped;
  406. }
  407. /**
  408. * tick_nohz_update_jiffies - update jiffies when idle was interrupted
  409. *
  410. * Called from interrupt entry when the CPU was idle
  411. *
  412. * In case the sched_tick was stopped on this CPU, we have to check if jiffies
  413. * must be updated. Otherwise an interrupt handler could use a stale jiffy
  414. * value. We do this unconditionally on any CPU, as we don't know whether the
  415. * CPU, which has the update task assigned is in a long sleep.
  416. */
  417. static void tick_nohz_update_jiffies(ktime_t now)
  418. {
  419. unsigned long flags;
  420. __this_cpu_write(tick_cpu_sched.idle_waketime, now);
  421. local_irq_save(flags);
  422. tick_do_update_jiffies64(now);
  423. local_irq_restore(flags);
  424. touch_softlockup_watchdog_sched();
  425. }
  426. /*
  427. * Updates the per-CPU time idle statistics counters
  428. */
  429. static void
  430. update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
  431. {
  432. ktime_t delta;
  433. if (ts->idle_active) {
  434. delta = ktime_sub(now, ts->idle_entrytime);
  435. if (nr_iowait_cpu(cpu) > 0)
  436. ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
  437. else
  438. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  439. ts->idle_entrytime = now;
  440. }
  441. if (last_update_time)
  442. *last_update_time = ktime_to_us(now);
  443. }
  444. static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now)
  445. {
  446. update_ts_time_stats(smp_processor_id(), ts, now, NULL);
  447. ts->idle_active = 0;
  448. sched_clock_idle_wakeup_event();
  449. }
  450. static void tick_nohz_start_idle(struct tick_sched *ts)
  451. {
  452. ts->idle_entrytime = ktime_get();
  453. ts->idle_active = 1;
  454. sched_clock_idle_sleep_event();
  455. }
  456. /**
  457. * get_cpu_idle_time_us - get the total idle time of a CPU
  458. * @cpu: CPU number to query
  459. * @last_update_time: variable to store update time in. Do not update
  460. * counters if NULL.
  461. *
  462. * Return the cumulative idle time (since boot) for a given
  463. * CPU, in microseconds.
  464. *
  465. * This time is measured via accounting rather than sampling,
  466. * and is as accurate as ktime_get() is.
  467. *
  468. * This function returns -1 if NOHZ is not enabled.
  469. */
  470. u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
  471. {
  472. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  473. ktime_t now, idle;
  474. if (!tick_nohz_active)
  475. return -1;
  476. now = ktime_get();
  477. if (last_update_time) {
  478. update_ts_time_stats(cpu, ts, now, last_update_time);
  479. idle = ts->idle_sleeptime;
  480. } else {
  481. if (ts->idle_active && !nr_iowait_cpu(cpu)) {
  482. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  483. idle = ktime_add(ts->idle_sleeptime, delta);
  484. } else {
  485. idle = ts->idle_sleeptime;
  486. }
  487. }
  488. return ktime_to_us(idle);
  489. }
  490. EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
  491. /**
  492. * get_cpu_iowait_time_us - get the total iowait time of a CPU
  493. * @cpu: CPU number to query
  494. * @last_update_time: variable to store update time in. Do not update
  495. * counters if NULL.
  496. *
  497. * Return the cumulative iowait time (since boot) for a given
  498. * CPU, in microseconds.
  499. *
  500. * This time is measured via accounting rather than sampling,
  501. * and is as accurate as ktime_get() is.
  502. *
  503. * This function returns -1 if NOHZ is not enabled.
  504. */
  505. u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
  506. {
  507. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  508. ktime_t now, iowait;
  509. if (!tick_nohz_active)
  510. return -1;
  511. now = ktime_get();
  512. if (last_update_time) {
  513. update_ts_time_stats(cpu, ts, now, last_update_time);
  514. iowait = ts->iowait_sleeptime;
  515. } else {
  516. if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
  517. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  518. iowait = ktime_add(ts->iowait_sleeptime, delta);
  519. } else {
  520. iowait = ts->iowait_sleeptime;
  521. }
  522. }
  523. return ktime_to_us(iowait);
  524. }
  525. EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
  526. static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
  527. {
  528. hrtimer_cancel(&ts->sched_timer);
  529. hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
  530. /* Forward the time to expire in the future */
  531. hrtimer_forward(&ts->sched_timer, now, tick_period);
  532. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  533. hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
  534. else
  535. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  536. /*
  537. * Reset to make sure next tick stop doesn't get fooled by past
  538. * cached clock deadline.
  539. */
  540. ts->next_tick = 0;
  541. }
  542. static inline bool local_timer_softirq_pending(void)
  543. {
  544. return local_softirq_pending() & BIT(TIMER_SOFTIRQ);
  545. }
  546. static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
  547. {
  548. u64 basemono, next_tick, next_tmr, next_rcu, delta, expires;
  549. unsigned long seq, basejiff;
  550. /* Read jiffies and the time when jiffies were updated last */
  551. do {
  552. seq = read_seqbegin(&jiffies_lock);
  553. basemono = last_jiffies_update;
  554. basejiff = jiffies;
  555. } while (read_seqretry(&jiffies_lock, seq));
  556. ts->last_jiffies = basejiff;
  557. ts->timer_expires_base = basemono;
  558. /*
  559. * Keep the periodic tick, when RCU, architecture or irq_work
  560. * requests it.
  561. * Aside of that check whether the local timer softirq is
  562. * pending. If so its a bad idea to call get_next_timer_interrupt()
  563. * because there is an already expired timer, so it will request
  564. * immeditate expiry, which rearms the hardware timer with a
  565. * minimal delta which brings us back to this place
  566. * immediately. Lather, rinse and repeat...
  567. */
  568. if (rcu_needs_cpu(basemono, &next_rcu) || arch_needs_cpu() ||
  569. irq_work_needs_cpu() || local_timer_softirq_pending()) {
  570. next_tick = basemono + TICK_NSEC;
  571. } else {
  572. /*
  573. * Get the next pending timer. If high resolution
  574. * timers are enabled this only takes the timer wheel
  575. * timers into account. If high resolution timers are
  576. * disabled this also looks at the next expiring
  577. * hrtimer.
  578. */
  579. next_tmr = get_next_timer_interrupt(basejiff, basemono);
  580. ts->next_timer = next_tmr;
  581. /* Take the next rcu event into account */
  582. next_tick = next_rcu < next_tmr ? next_rcu : next_tmr;
  583. }
  584. /*
  585. * If the tick is due in the next period, keep it ticking or
  586. * force prod the timer.
  587. */
  588. delta = next_tick - basemono;
  589. if (delta <= (u64)TICK_NSEC) {
  590. /*
  591. * Tell the timer code that the base is not idle, i.e. undo
  592. * the effect of get_next_timer_interrupt():
  593. */
  594. timer_clear_idle();
  595. /*
  596. * We've not stopped the tick yet, and there's a timer in the
  597. * next period, so no point in stopping it either, bail.
  598. */
  599. if (!ts->tick_stopped) {
  600. ts->timer_expires = 0;
  601. goto out;
  602. }
  603. }
  604. /*
  605. * If this CPU is the one which had the do_timer() duty last, we limit
  606. * the sleep time to the timekeeping max_deferment value.
  607. * Otherwise we can sleep as long as we want.
  608. */
  609. delta = timekeeping_max_deferment();
  610. if (cpu != tick_do_timer_cpu &&
  611. (tick_do_timer_cpu != TICK_DO_TIMER_NONE || !ts->do_timer_last))
  612. delta = KTIME_MAX;
  613. /* Calculate the next expiry time */
  614. if (delta < (KTIME_MAX - basemono))
  615. expires = basemono + delta;
  616. else
  617. expires = KTIME_MAX;
  618. ts->timer_expires = min_t(u64, expires, next_tick);
  619. out:
  620. return ts->timer_expires;
  621. }
  622. static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu)
  623. {
  624. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  625. u64 basemono = ts->timer_expires_base;
  626. u64 expires = ts->timer_expires;
  627. ktime_t tick = expires;
  628. /* Make sure we won't be trying to stop it twice in a row. */
  629. ts->timer_expires_base = 0;
  630. /*
  631. * If this CPU is the one which updates jiffies, then give up
  632. * the assignment and let it be taken by the CPU which runs
  633. * the tick timer next, which might be this CPU as well. If we
  634. * don't drop this here the jiffies might be stale and
  635. * do_timer() never invoked. Keep track of the fact that it
  636. * was the one which had the do_timer() duty last.
  637. */
  638. if (cpu == tick_do_timer_cpu) {
  639. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  640. ts->do_timer_last = 1;
  641. } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
  642. ts->do_timer_last = 0;
  643. }
  644. /* Skip reprogram of event if its not changed */
  645. if (ts->tick_stopped && (expires == ts->next_tick)) {
  646. /* Sanity check: make sure clockevent is actually programmed */
  647. if (tick == KTIME_MAX || ts->next_tick == hrtimer_get_expires(&ts->sched_timer))
  648. return;
  649. WARN_ON_ONCE(1);
  650. printk_once("basemono: %llu ts->next_tick: %llu dev->next_event: %llu timer->active: %d timer->expires: %llu\n",
  651. basemono, ts->next_tick, dev->next_event,
  652. hrtimer_active(&ts->sched_timer), hrtimer_get_expires(&ts->sched_timer));
  653. }
  654. /*
  655. * nohz_stop_sched_tick can be called several times before
  656. * the nohz_restart_sched_tick is called. This happens when
  657. * interrupts arrive which do not cause a reschedule. In the
  658. * first call we save the current tick time, so we can restart
  659. * the scheduler tick in nohz_restart_sched_tick.
  660. */
  661. if (!ts->tick_stopped) {
  662. calc_load_nohz_start();
  663. cpu_load_update_nohz_start();
  664. quiet_vmstat();
  665. ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
  666. ts->tick_stopped = 1;
  667. trace_tick_stop(1, TICK_DEP_MASK_NONE);
  668. }
  669. ts->next_tick = tick;
  670. /*
  671. * If the expiration time == KTIME_MAX, then we simply stop
  672. * the tick timer.
  673. */
  674. if (unlikely(expires == KTIME_MAX)) {
  675. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  676. hrtimer_cancel(&ts->sched_timer);
  677. return;
  678. }
  679. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  680. hrtimer_start(&ts->sched_timer, tick, HRTIMER_MODE_ABS_PINNED);
  681. } else {
  682. hrtimer_set_expires(&ts->sched_timer, tick);
  683. tick_program_event(tick, 1);
  684. }
  685. }
  686. static void tick_nohz_retain_tick(struct tick_sched *ts)
  687. {
  688. ts->timer_expires_base = 0;
  689. }
  690. #ifdef CONFIG_NO_HZ_FULL
  691. static void tick_nohz_stop_sched_tick(struct tick_sched *ts, int cpu)
  692. {
  693. if (tick_nohz_next_event(ts, cpu))
  694. tick_nohz_stop_tick(ts, cpu);
  695. else
  696. tick_nohz_retain_tick(ts);
  697. }
  698. #endif /* CONFIG_NO_HZ_FULL */
  699. static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
  700. {
  701. /* Update jiffies first */
  702. tick_do_update_jiffies64(now);
  703. cpu_load_update_nohz_stop();
  704. /*
  705. * Clear the timer idle flag, so we avoid IPIs on remote queueing and
  706. * the clock forward checks in the enqueue path:
  707. */
  708. timer_clear_idle();
  709. calc_load_nohz_stop();
  710. touch_softlockup_watchdog_sched();
  711. /*
  712. * Cancel the scheduled timer and restore the tick
  713. */
  714. ts->tick_stopped = 0;
  715. ts->idle_exittime = now;
  716. tick_nohz_restart(ts, now);
  717. }
  718. static void tick_nohz_full_update_tick(struct tick_sched *ts)
  719. {
  720. #ifdef CONFIG_NO_HZ_FULL
  721. int cpu = smp_processor_id();
  722. if (!tick_nohz_full_cpu(cpu))
  723. return;
  724. if (!ts->tick_stopped && ts->nohz_mode == NOHZ_MODE_INACTIVE)
  725. return;
  726. if (can_stop_full_tick(cpu, ts))
  727. tick_nohz_stop_sched_tick(ts, cpu);
  728. else if (ts->tick_stopped)
  729. tick_nohz_restart_sched_tick(ts, ktime_get());
  730. #endif
  731. }
  732. static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
  733. {
  734. /*
  735. * If this CPU is offline and it is the one which updates
  736. * jiffies, then give up the assignment and let it be taken by
  737. * the CPU which runs the tick timer next. If we don't drop
  738. * this here the jiffies might be stale and do_timer() never
  739. * invoked.
  740. */
  741. if (unlikely(!cpu_online(cpu))) {
  742. if (cpu == tick_do_timer_cpu)
  743. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  744. /*
  745. * Make sure the CPU doesn't get fooled by obsolete tick
  746. * deadline if it comes back online later.
  747. */
  748. ts->next_tick = 0;
  749. return false;
  750. }
  751. if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
  752. return false;
  753. if (need_resched())
  754. return false;
  755. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  756. static int ratelimit;
  757. if (ratelimit < 10 &&
  758. (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  759. pr_warn("NOHZ: local_softirq_pending %02x\n",
  760. (unsigned int) local_softirq_pending());
  761. ratelimit++;
  762. }
  763. return false;
  764. }
  765. if (tick_nohz_full_enabled()) {
  766. /*
  767. * Keep the tick alive to guarantee timekeeping progression
  768. * if there are full dynticks CPUs around
  769. */
  770. if (tick_do_timer_cpu == cpu)
  771. return false;
  772. /*
  773. * Boot safety: make sure the timekeeping duty has been
  774. * assigned before entering dyntick-idle mode,
  775. */
  776. if (tick_do_timer_cpu == TICK_DO_TIMER_NONE)
  777. return false;
  778. }
  779. return true;
  780. }
  781. static void __tick_nohz_idle_stop_tick(struct tick_sched *ts)
  782. {
  783. ktime_t expires;
  784. int cpu = smp_processor_id();
  785. /*
  786. * If tick_nohz_get_sleep_length() ran tick_nohz_next_event(), the
  787. * tick timer expiration time is known already.
  788. */
  789. if (ts->timer_expires_base)
  790. expires = ts->timer_expires;
  791. else if (can_stop_idle_tick(cpu, ts))
  792. expires = tick_nohz_next_event(ts, cpu);
  793. else
  794. return;
  795. ts->idle_calls++;
  796. if (expires > 0LL) {
  797. int was_stopped = ts->tick_stopped;
  798. tick_nohz_stop_tick(ts, cpu);
  799. ts->idle_sleeps++;
  800. ts->idle_expires = expires;
  801. if (!was_stopped && ts->tick_stopped) {
  802. ts->idle_jiffies = ts->last_jiffies;
  803. nohz_balance_enter_idle(cpu);
  804. }
  805. } else {
  806. tick_nohz_retain_tick(ts);
  807. }
  808. }
  809. /**
  810. * tick_nohz_idle_stop_tick - stop the idle tick from the idle task
  811. *
  812. * When the next event is more than a tick into the future, stop the idle tick
  813. */
  814. void tick_nohz_idle_stop_tick(void)
  815. {
  816. __tick_nohz_idle_stop_tick(this_cpu_ptr(&tick_cpu_sched));
  817. }
  818. void tick_nohz_idle_retain_tick(void)
  819. {
  820. tick_nohz_retain_tick(this_cpu_ptr(&tick_cpu_sched));
  821. /*
  822. * Undo the effect of get_next_timer_interrupt() called from
  823. * tick_nohz_next_event().
  824. */
  825. timer_clear_idle();
  826. }
  827. /**
  828. * tick_nohz_idle_enter - prepare for entering idle on the current CPU
  829. *
  830. * Called when we start the idle loop.
  831. */
  832. void tick_nohz_idle_enter(void)
  833. {
  834. struct tick_sched *ts;
  835. lockdep_assert_irqs_enabled();
  836. local_irq_disable();
  837. ts = this_cpu_ptr(&tick_cpu_sched);
  838. WARN_ON_ONCE(ts->timer_expires_base);
  839. ts->inidle = 1;
  840. tick_nohz_start_idle(ts);
  841. local_irq_enable();
  842. }
  843. /**
  844. * tick_nohz_irq_exit - update next tick event from interrupt exit
  845. *
  846. * When an interrupt fires while we are idle and it doesn't cause
  847. * a reschedule, it may still add, modify or delete a timer, enqueue
  848. * an RCU callback, etc...
  849. * So we need to re-calculate and reprogram the next tick event.
  850. */
  851. void tick_nohz_irq_exit(void)
  852. {
  853. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  854. if (ts->inidle)
  855. tick_nohz_start_idle(ts);
  856. else
  857. tick_nohz_full_update_tick(ts);
  858. }
  859. /**
  860. * tick_nohz_idle_got_tick - Check whether or not the tick handler has run
  861. */
  862. bool tick_nohz_idle_got_tick(void)
  863. {
  864. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  865. if (ts->got_idle_tick) {
  866. ts->got_idle_tick = 0;
  867. return true;
  868. }
  869. return false;
  870. }
  871. /**
  872. * tick_nohz_get_sleep_length - return the expected length of the current sleep
  873. * @delta_next: duration until the next event if the tick cannot be stopped
  874. *
  875. * Called from power state control code with interrupts disabled
  876. */
  877. ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next)
  878. {
  879. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  880. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  881. int cpu = smp_processor_id();
  882. /*
  883. * The idle entry time is expected to be a sufficient approximation of
  884. * the current time at this point.
  885. */
  886. ktime_t now = ts->idle_entrytime;
  887. ktime_t next_event;
  888. WARN_ON_ONCE(!ts->inidle);
  889. *delta_next = ktime_sub(dev->next_event, now);
  890. if (!can_stop_idle_tick(cpu, ts))
  891. return *delta_next;
  892. next_event = tick_nohz_next_event(ts, cpu);
  893. if (!next_event)
  894. return *delta_next;
  895. /*
  896. * If the next highres timer to expire is earlier than next_event, the
  897. * idle governor needs to know that.
  898. */
  899. next_event = min_t(u64, next_event,
  900. hrtimer_next_event_without(&ts->sched_timer));
  901. return ktime_sub(next_event, now);
  902. }
  903. /**
  904. * tick_nohz_get_idle_calls_cpu - return the current idle calls counter value
  905. * for a particular CPU.
  906. *
  907. * Called from the schedutil frequency scaling governor in scheduler context.
  908. */
  909. unsigned long tick_nohz_get_idle_calls_cpu(int cpu)
  910. {
  911. struct tick_sched *ts = tick_get_tick_sched(cpu);
  912. return ts->idle_calls;
  913. }
  914. /**
  915. * tick_nohz_get_idle_calls - return the current idle calls counter value
  916. *
  917. * Called from the schedutil frequency scaling governor in scheduler context.
  918. */
  919. unsigned long tick_nohz_get_idle_calls(void)
  920. {
  921. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  922. return ts->idle_calls;
  923. }
  924. static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
  925. {
  926. #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  927. unsigned long ticks;
  928. if (vtime_accounting_cpu_enabled())
  929. return;
  930. /*
  931. * We stopped the tick in idle. Update process times would miss the
  932. * time we slept as update_process_times does only a 1 tick
  933. * accounting. Enforce that this is accounted to idle !
  934. */
  935. ticks = jiffies - ts->idle_jiffies;
  936. /*
  937. * We might be one off. Do not randomly account a huge number of ticks!
  938. */
  939. if (ticks && ticks < LONG_MAX)
  940. account_idle_ticks(ticks);
  941. #endif
  942. }
  943. static void __tick_nohz_idle_restart_tick(struct tick_sched *ts, ktime_t now)
  944. {
  945. tick_nohz_restart_sched_tick(ts, now);
  946. tick_nohz_account_idle_ticks(ts);
  947. }
  948. void tick_nohz_idle_restart_tick(void)
  949. {
  950. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  951. if (ts->tick_stopped)
  952. __tick_nohz_idle_restart_tick(ts, ktime_get());
  953. }
  954. /**
  955. * tick_nohz_idle_exit - restart the idle tick from the idle task
  956. *
  957. * Restart the idle tick when the CPU is woken up from idle
  958. * This also exit the RCU extended quiescent state. The CPU
  959. * can use RCU again after this function is called.
  960. */
  961. void tick_nohz_idle_exit(void)
  962. {
  963. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  964. bool idle_active, tick_stopped;
  965. ktime_t now;
  966. local_irq_disable();
  967. WARN_ON_ONCE(!ts->inidle);
  968. WARN_ON_ONCE(ts->timer_expires_base);
  969. ts->inidle = 0;
  970. idle_active = ts->idle_active;
  971. tick_stopped = ts->tick_stopped;
  972. if (idle_active || tick_stopped)
  973. now = ktime_get();
  974. if (idle_active)
  975. tick_nohz_stop_idle(ts, now);
  976. if (tick_stopped)
  977. __tick_nohz_idle_restart_tick(ts, now);
  978. local_irq_enable();
  979. }
  980. /*
  981. * The nohz low res interrupt handler
  982. */
  983. static void tick_nohz_handler(struct clock_event_device *dev)
  984. {
  985. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  986. struct pt_regs *regs = get_irq_regs();
  987. ktime_t now = ktime_get();
  988. dev->next_event = KTIME_MAX;
  989. tick_sched_do_timer(ts, now);
  990. tick_sched_handle(ts, regs);
  991. /* No need to reprogram if we are running tickless */
  992. if (unlikely(ts->tick_stopped))
  993. return;
  994. hrtimer_forward(&ts->sched_timer, now, tick_period);
  995. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  996. }
  997. static inline void tick_nohz_activate(struct tick_sched *ts, int mode)
  998. {
  999. if (!tick_nohz_enabled)
  1000. return;
  1001. ts->nohz_mode = mode;
  1002. /* One update is enough */
  1003. if (!test_and_set_bit(0, &tick_nohz_active))
  1004. timers_update_nohz();
  1005. }
  1006. /**
  1007. * tick_nohz_switch_to_nohz - switch to nohz mode
  1008. */
  1009. static void tick_nohz_switch_to_nohz(void)
  1010. {
  1011. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1012. ktime_t next;
  1013. if (!tick_nohz_enabled)
  1014. return;
  1015. if (tick_switch_to_oneshot(tick_nohz_handler))
  1016. return;
  1017. /*
  1018. * Recycle the hrtimer in ts, so we can share the
  1019. * hrtimer_forward with the highres code.
  1020. */
  1021. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1022. /* Get the next period */
  1023. next = tick_init_jiffy_update();
  1024. hrtimer_set_expires(&ts->sched_timer, next);
  1025. hrtimer_forward_now(&ts->sched_timer, tick_period);
  1026. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  1027. tick_nohz_activate(ts, NOHZ_MODE_LOWRES);
  1028. }
  1029. static inline void tick_nohz_irq_enter(void)
  1030. {
  1031. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1032. ktime_t now;
  1033. if (!ts->idle_active && !ts->tick_stopped)
  1034. return;
  1035. now = ktime_get();
  1036. if (ts->idle_active)
  1037. tick_nohz_stop_idle(ts, now);
  1038. if (ts->tick_stopped)
  1039. tick_nohz_update_jiffies(now);
  1040. }
  1041. #else
  1042. static inline void tick_nohz_switch_to_nohz(void) { }
  1043. static inline void tick_nohz_irq_enter(void) { }
  1044. static inline void tick_nohz_activate(struct tick_sched *ts, int mode) { }
  1045. #endif /* CONFIG_NO_HZ_COMMON */
  1046. /*
  1047. * Called from irq_enter to notify about the possible interruption of idle()
  1048. */
  1049. void tick_irq_enter(void)
  1050. {
  1051. tick_check_oneshot_broadcast_this_cpu();
  1052. tick_nohz_irq_enter();
  1053. }
  1054. /*
  1055. * High resolution timer specific code
  1056. */
  1057. #ifdef CONFIG_HIGH_RES_TIMERS
  1058. /*
  1059. * We rearm the timer until we get disabled by the idle code.
  1060. * Called with interrupts disabled.
  1061. */
  1062. static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
  1063. {
  1064. struct tick_sched *ts =
  1065. container_of(timer, struct tick_sched, sched_timer);
  1066. struct pt_regs *regs = get_irq_regs();
  1067. ktime_t now = ktime_get();
  1068. tick_sched_do_timer(ts, now);
  1069. /*
  1070. * Do not call, when we are not in irq context and have
  1071. * no valid regs pointer
  1072. */
  1073. if (regs)
  1074. tick_sched_handle(ts, regs);
  1075. else
  1076. ts->next_tick = 0;
  1077. /* No need to reprogram if we are in idle or full dynticks mode */
  1078. if (unlikely(ts->tick_stopped))
  1079. return HRTIMER_NORESTART;
  1080. hrtimer_forward(timer, now, tick_period);
  1081. return HRTIMER_RESTART;
  1082. }
  1083. static int sched_skew_tick;
  1084. static int __init skew_tick(char *str)
  1085. {
  1086. get_option(&str, &sched_skew_tick);
  1087. return 0;
  1088. }
  1089. early_param("skew_tick", skew_tick);
  1090. /**
  1091. * tick_setup_sched_timer - setup the tick emulation timer
  1092. */
  1093. void tick_setup_sched_timer(void)
  1094. {
  1095. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1096. ktime_t now = ktime_get();
  1097. /*
  1098. * Emulate tick processing via per-CPU hrtimers:
  1099. */
  1100. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1101. ts->sched_timer.function = tick_sched_timer;
  1102. /* Get the next period (per-CPU) */
  1103. hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
  1104. /* Offset the tick to avert jiffies_lock contention. */
  1105. if (sched_skew_tick) {
  1106. u64 offset = ktime_to_ns(tick_period) >> 1;
  1107. do_div(offset, num_possible_cpus());
  1108. offset *= smp_processor_id();
  1109. hrtimer_add_expires_ns(&ts->sched_timer, offset);
  1110. }
  1111. hrtimer_forward(&ts->sched_timer, now, tick_period);
  1112. hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
  1113. tick_nohz_activate(ts, NOHZ_MODE_HIGHRES);
  1114. }
  1115. #endif /* HIGH_RES_TIMERS */
  1116. #if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
  1117. void tick_cancel_sched_timer(int cpu)
  1118. {
  1119. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  1120. # ifdef CONFIG_HIGH_RES_TIMERS
  1121. if (ts->sched_timer.base)
  1122. hrtimer_cancel(&ts->sched_timer);
  1123. # endif
  1124. memset(ts, 0, sizeof(*ts));
  1125. }
  1126. #endif
  1127. /**
  1128. * Async notification about clocksource changes
  1129. */
  1130. void tick_clock_notify(void)
  1131. {
  1132. int cpu;
  1133. for_each_possible_cpu(cpu)
  1134. set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
  1135. }
  1136. /*
  1137. * Async notification about clock event changes
  1138. */
  1139. void tick_oneshot_notify(void)
  1140. {
  1141. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1142. set_bit(0, &ts->check_clocks);
  1143. }
  1144. /**
  1145. * Check, if a change happened, which makes oneshot possible.
  1146. *
  1147. * Called cyclic from the hrtimer softirq (driven by the timer
  1148. * softirq) allow_nohz signals, that we can switch into low-res nohz
  1149. * mode, because high resolution timers are disabled (either compile
  1150. * or runtime). Called with interrupts disabled.
  1151. */
  1152. int tick_check_oneshot_change(int allow_nohz)
  1153. {
  1154. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  1155. if (!test_and_clear_bit(0, &ts->check_clocks))
  1156. return 0;
  1157. if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
  1158. return 0;
  1159. if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
  1160. return 0;
  1161. if (!allow_nohz)
  1162. return 1;
  1163. tick_nohz_switch_to_nohz();
  1164. return 0;
  1165. }