hrtimer.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. /*
  2. * linux/kernel/hrtimer.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. * High-resolution kernel timers
  9. *
  10. * In contrast to the low-resolution timeout API implemented in
  11. * kernel/timer.c, hrtimers provide finer resolution and accuracy
  12. * depending on system configuration and capabilities.
  13. *
  14. * These timers are currently used for:
  15. * - itimers
  16. * - POSIX timers
  17. * - nanosleep
  18. * - precise in-kernel timing
  19. *
  20. * Started by: Thomas Gleixner and Ingo Molnar
  21. *
  22. * Credits:
  23. * based on kernel/timer.c
  24. *
  25. * Help, testing, suggestions, bugfixes, improvements were
  26. * provided by:
  27. *
  28. * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
  29. * et. al.
  30. *
  31. * For licencing details see kernel-base/COPYING
  32. */
  33. #include <linux/cpu.h>
  34. #include <linux/export.h>
  35. #include <linux/percpu.h>
  36. #include <linux/hrtimer.h>
  37. #include <linux/notifier.h>
  38. #include <linux/syscalls.h>
  39. #include <linux/kallsyms.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/tick.h>
  42. #include <linux/seq_file.h>
  43. #include <linux/err.h>
  44. #include <linux/debugobjects.h>
  45. #include <linux/sched.h>
  46. #include <linux/sched/sysctl.h>
  47. #include <linux/sched/rt.h>
  48. #include <linux/sched/deadline.h>
  49. #include <linux/timer.h>
  50. #include <linux/freezer.h>
  51. #include <asm/uaccess.h>
  52. #include <trace/events/timer.h>
  53. /*
  54. * The timer bases:
  55. *
  56. * There are more clockids then hrtimer bases. Thus, we index
  57. * into the timer bases by the hrtimer_base_type enum. When trying
  58. * to reach a base using a clockid, hrtimer_clockid_to_base()
  59. * is used to convert from clockid to the proper hrtimer_base_type.
  60. */
  61. DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
  62. {
  63. .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
  64. .clock_base =
  65. {
  66. {
  67. .index = HRTIMER_BASE_MONOTONIC,
  68. .clockid = CLOCK_MONOTONIC,
  69. .get_time = &ktime_get,
  70. .resolution = KTIME_LOW_RES,
  71. },
  72. {
  73. .index = HRTIMER_BASE_REALTIME,
  74. .clockid = CLOCK_REALTIME,
  75. .get_time = &ktime_get_real,
  76. .resolution = KTIME_LOW_RES,
  77. },
  78. {
  79. .index = HRTIMER_BASE_BOOTTIME,
  80. .clockid = CLOCK_BOOTTIME,
  81. .get_time = &ktime_get_boottime,
  82. .resolution = KTIME_LOW_RES,
  83. },
  84. {
  85. .index = HRTIMER_BASE_TAI,
  86. .clockid = CLOCK_TAI,
  87. .get_time = &ktime_get_clocktai,
  88. .resolution = KTIME_LOW_RES,
  89. },
  90. }
  91. };
  92. static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
  93. [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
  94. [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
  95. [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
  96. [CLOCK_TAI] = HRTIMER_BASE_TAI,
  97. };
  98. static inline int hrtimer_clockid_to_base(clockid_t clock_id)
  99. {
  100. return hrtimer_clock_to_base_table[clock_id];
  101. }
  102. /*
  103. * Get the coarse grained time at the softirq based on xtime and
  104. * wall_to_monotonic.
  105. */
  106. static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
  107. {
  108. ktime_t xtim, mono, boot, tai;
  109. ktime_t off_real, off_boot, off_tai;
  110. mono = ktime_get_update_offsets_tick(&off_real, &off_boot, &off_tai);
  111. boot = ktime_add(mono, off_boot);
  112. xtim = ktime_add(mono, off_real);
  113. tai = ktime_add(xtim, off_tai);
  114. base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
  115. base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
  116. base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
  117. base->clock_base[HRTIMER_BASE_TAI].softirq_time = tai;
  118. }
  119. /*
  120. * Functions and macros which are different for UP/SMP systems are kept in a
  121. * single place
  122. */
  123. #ifdef CONFIG_SMP
  124. /*
  125. * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
  126. * means that all timers which are tied to this base via timer->base are
  127. * locked, and the base itself is locked too.
  128. *
  129. * So __run_timers/migrate_timers can safely modify all timers which could
  130. * be found on the lists/queues.
  131. *
  132. * When the timer's base is locked, and the timer removed from list, it is
  133. * possible to set timer->base = NULL and drop the lock: the timer remains
  134. * locked.
  135. */
  136. static
  137. struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
  138. unsigned long *flags)
  139. {
  140. struct hrtimer_clock_base *base;
  141. for (;;) {
  142. base = timer->base;
  143. if (likely(base != NULL)) {
  144. raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
  145. if (likely(base == timer->base))
  146. return base;
  147. /* The timer has migrated to another CPU: */
  148. raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
  149. }
  150. cpu_relax();
  151. }
  152. }
  153. /*
  154. * With HIGHRES=y we do not migrate the timer when it is expiring
  155. * before the next event on the target cpu because we cannot reprogram
  156. * the target cpu hardware and we would cause it to fire late.
  157. *
  158. * Called with cpu_base->lock of target cpu held.
  159. */
  160. static int
  161. hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
  162. {
  163. #ifdef CONFIG_HIGH_RES_TIMERS
  164. ktime_t expires;
  165. if (!new_base->cpu_base->hres_active)
  166. return 0;
  167. expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
  168. return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
  169. #else
  170. return 0;
  171. #endif
  172. }
  173. /*
  174. * Switch the timer base to the current CPU when possible.
  175. */
  176. static inline struct hrtimer_clock_base *
  177. switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
  178. int pinned)
  179. {
  180. struct hrtimer_clock_base *new_base;
  181. struct hrtimer_cpu_base *new_cpu_base;
  182. int this_cpu = smp_processor_id();
  183. int cpu = get_nohz_timer_target(pinned);
  184. int basenum = base->index;
  185. again:
  186. new_cpu_base = &per_cpu(hrtimer_bases, cpu);
  187. new_base = &new_cpu_base->clock_base[basenum];
  188. if (base != new_base) {
  189. /*
  190. * We are trying to move timer to new_base.
  191. * However we can't change timer's base while it is running,
  192. * so we keep it on the same CPU. No hassle vs. reprogramming
  193. * the event source in the high resolution case. The softirq
  194. * code will take care of this when the timer function has
  195. * completed. There is no conflict as we hold the lock until
  196. * the timer is enqueued.
  197. */
  198. if (unlikely(hrtimer_callback_running(timer)))
  199. return base;
  200. /* See the comment in lock_timer_base() */
  201. timer->base = NULL;
  202. raw_spin_unlock(&base->cpu_base->lock);
  203. raw_spin_lock(&new_base->cpu_base->lock);
  204. if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
  205. cpu = this_cpu;
  206. raw_spin_unlock(&new_base->cpu_base->lock);
  207. raw_spin_lock(&base->cpu_base->lock);
  208. timer->base = base;
  209. goto again;
  210. }
  211. timer->base = new_base;
  212. } else {
  213. if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
  214. cpu = this_cpu;
  215. goto again;
  216. }
  217. }
  218. return new_base;
  219. }
  220. #else /* CONFIG_SMP */
  221. static inline struct hrtimer_clock_base *
  222. lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  223. {
  224. struct hrtimer_clock_base *base = timer->base;
  225. raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
  226. return base;
  227. }
  228. # define switch_hrtimer_base(t, b, p) (b)
  229. #endif /* !CONFIG_SMP */
  230. /*
  231. * Functions for the union type storage format of ktime_t which are
  232. * too large for inlining:
  233. */
  234. #if BITS_PER_LONG < 64
  235. /*
  236. * Divide a ktime value by a nanosecond value
  237. */
  238. u64 ktime_divns(const ktime_t kt, s64 div)
  239. {
  240. u64 dclc;
  241. int sft = 0;
  242. dclc = ktime_to_ns(kt);
  243. /* Make sure the divisor is less than 2^32: */
  244. while (div >> 32) {
  245. sft++;
  246. div >>= 1;
  247. }
  248. dclc >>= sft;
  249. do_div(dclc, (unsigned long) div);
  250. return dclc;
  251. }
  252. EXPORT_SYMBOL_GPL(ktime_divns);
  253. #endif /* BITS_PER_LONG >= 64 */
  254. /*
  255. * Add two ktime values and do a safety check for overflow:
  256. */
  257. ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
  258. {
  259. ktime_t res = ktime_add(lhs, rhs);
  260. /*
  261. * We use KTIME_SEC_MAX here, the maximum timeout which we can
  262. * return to user space in a timespec:
  263. */
  264. if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
  265. res = ktime_set(KTIME_SEC_MAX, 0);
  266. return res;
  267. }
  268. EXPORT_SYMBOL_GPL(ktime_add_safe);
  269. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  270. static struct debug_obj_descr hrtimer_debug_descr;
  271. static void *hrtimer_debug_hint(void *addr)
  272. {
  273. return ((struct hrtimer *) addr)->function;
  274. }
  275. /*
  276. * fixup_init is called when:
  277. * - an active object is initialized
  278. */
  279. static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
  280. {
  281. struct hrtimer *timer = addr;
  282. switch (state) {
  283. case ODEBUG_STATE_ACTIVE:
  284. hrtimer_cancel(timer);
  285. debug_object_init(timer, &hrtimer_debug_descr);
  286. return 1;
  287. default:
  288. return 0;
  289. }
  290. }
  291. /*
  292. * fixup_activate is called when:
  293. * - an active object is activated
  294. * - an unknown object is activated (might be a statically initialized object)
  295. */
  296. static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
  297. {
  298. switch (state) {
  299. case ODEBUG_STATE_NOTAVAILABLE:
  300. WARN_ON_ONCE(1);
  301. return 0;
  302. case ODEBUG_STATE_ACTIVE:
  303. WARN_ON(1);
  304. default:
  305. return 0;
  306. }
  307. }
  308. /*
  309. * fixup_free is called when:
  310. * - an active object is freed
  311. */
  312. static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
  313. {
  314. struct hrtimer *timer = addr;
  315. switch (state) {
  316. case ODEBUG_STATE_ACTIVE:
  317. hrtimer_cancel(timer);
  318. debug_object_free(timer, &hrtimer_debug_descr);
  319. return 1;
  320. default:
  321. return 0;
  322. }
  323. }
  324. static struct debug_obj_descr hrtimer_debug_descr = {
  325. .name = "hrtimer",
  326. .debug_hint = hrtimer_debug_hint,
  327. .fixup_init = hrtimer_fixup_init,
  328. .fixup_activate = hrtimer_fixup_activate,
  329. .fixup_free = hrtimer_fixup_free,
  330. };
  331. static inline void debug_hrtimer_init(struct hrtimer *timer)
  332. {
  333. debug_object_init(timer, &hrtimer_debug_descr);
  334. }
  335. static inline void debug_hrtimer_activate(struct hrtimer *timer)
  336. {
  337. debug_object_activate(timer, &hrtimer_debug_descr);
  338. }
  339. static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
  340. {
  341. debug_object_deactivate(timer, &hrtimer_debug_descr);
  342. }
  343. static inline void debug_hrtimer_free(struct hrtimer *timer)
  344. {
  345. debug_object_free(timer, &hrtimer_debug_descr);
  346. }
  347. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  348. enum hrtimer_mode mode);
  349. void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
  350. enum hrtimer_mode mode)
  351. {
  352. debug_object_init_on_stack(timer, &hrtimer_debug_descr);
  353. __hrtimer_init(timer, clock_id, mode);
  354. }
  355. EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
  356. void destroy_hrtimer_on_stack(struct hrtimer *timer)
  357. {
  358. debug_object_free(timer, &hrtimer_debug_descr);
  359. }
  360. #else
  361. static inline void debug_hrtimer_init(struct hrtimer *timer) { }
  362. static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
  363. static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
  364. #endif
  365. static inline void
  366. debug_init(struct hrtimer *timer, clockid_t clockid,
  367. enum hrtimer_mode mode)
  368. {
  369. debug_hrtimer_init(timer);
  370. trace_hrtimer_init(timer, clockid, mode);
  371. }
  372. static inline void debug_activate(struct hrtimer *timer)
  373. {
  374. debug_hrtimer_activate(timer);
  375. trace_hrtimer_start(timer);
  376. }
  377. static inline void debug_deactivate(struct hrtimer *timer)
  378. {
  379. debug_hrtimer_deactivate(timer);
  380. trace_hrtimer_cancel(timer);
  381. }
  382. /* High resolution timer related functions */
  383. #ifdef CONFIG_HIGH_RES_TIMERS
  384. /*
  385. * High resolution timer enabled ?
  386. */
  387. static int hrtimer_hres_enabled __read_mostly = 1;
  388. /*
  389. * Enable / Disable high resolution mode
  390. */
  391. static int __init setup_hrtimer_hres(char *str)
  392. {
  393. if (!strcmp(str, "off"))
  394. hrtimer_hres_enabled = 0;
  395. else if (!strcmp(str, "on"))
  396. hrtimer_hres_enabled = 1;
  397. else
  398. return 0;
  399. return 1;
  400. }
  401. __setup("highres=", setup_hrtimer_hres);
  402. /*
  403. * hrtimer_high_res_enabled - query, if the highres mode is enabled
  404. */
  405. static inline int hrtimer_is_hres_enabled(void)
  406. {
  407. return hrtimer_hres_enabled;
  408. }
  409. /*
  410. * Is the high resolution mode active ?
  411. */
  412. static inline int hrtimer_hres_active(void)
  413. {
  414. return __this_cpu_read(hrtimer_bases.hres_active);
  415. }
  416. /*
  417. * Reprogram the event source with checking both queues for the
  418. * next event
  419. * Called with interrupts disabled and base->lock held
  420. */
  421. static void
  422. hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
  423. {
  424. int i;
  425. struct hrtimer_clock_base *base = cpu_base->clock_base;
  426. ktime_t expires, expires_next;
  427. expires_next.tv64 = KTIME_MAX;
  428. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  429. struct hrtimer *timer;
  430. struct timerqueue_node *next;
  431. next = timerqueue_getnext(&base->active);
  432. if (!next)
  433. continue;
  434. timer = container_of(next, struct hrtimer, node);
  435. expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  436. /*
  437. * clock_was_set() has changed base->offset so the
  438. * result might be negative. Fix it up to prevent a
  439. * false positive in clockevents_program_event()
  440. */
  441. if (expires.tv64 < 0)
  442. expires.tv64 = 0;
  443. if (expires.tv64 < expires_next.tv64)
  444. expires_next = expires;
  445. }
  446. if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
  447. return;
  448. cpu_base->expires_next.tv64 = expires_next.tv64;
  449. /*
  450. * If a hang was detected in the last timer interrupt then we
  451. * leave the hang delay active in the hardware. We want the
  452. * system to make progress. That also prevents the following
  453. * scenario:
  454. * T1 expires 50ms from now
  455. * T2 expires 5s from now
  456. *
  457. * T1 is removed, so this code is called and would reprogram
  458. * the hardware to 5s from now. Any hrtimer_start after that
  459. * will not reprogram the hardware due to hang_detected being
  460. * set. So we'd effectivly block all timers until the T2 event
  461. * fires.
  462. */
  463. if (cpu_base->hang_detected)
  464. return;
  465. if (cpu_base->expires_next.tv64 != KTIME_MAX)
  466. tick_program_event(cpu_base->expires_next, 1);
  467. }
  468. /*
  469. * Shared reprogramming for clock_realtime and clock_monotonic
  470. *
  471. * When a timer is enqueued and expires earlier than the already enqueued
  472. * timers, we have to check, whether it expires earlier than the timer for
  473. * which the clock event device was armed.
  474. *
  475. * Note, that in case the state has HRTIMER_STATE_CALLBACK set, no reprogramming
  476. * and no expiry check happens. The timer gets enqueued into the rbtree. The
  477. * reprogramming and expiry check is done in the hrtimer_interrupt or in the
  478. * softirq.
  479. *
  480. * Called with interrupts disabled and base->cpu_base.lock held
  481. */
  482. static int hrtimer_reprogram(struct hrtimer *timer,
  483. struct hrtimer_clock_base *base)
  484. {
  485. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  486. ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  487. int res;
  488. WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
  489. /*
  490. * When the callback is running, we do not reprogram the clock event
  491. * device. The timer callback is either running on a different CPU or
  492. * the callback is executed in the hrtimer_interrupt context. The
  493. * reprogramming is handled either by the softirq, which called the
  494. * callback or at the end of the hrtimer_interrupt.
  495. */
  496. if (hrtimer_callback_running(timer))
  497. return 0;
  498. /*
  499. * CLOCK_REALTIME timer might be requested with an absolute
  500. * expiry time which is less than base->offset. Nothing wrong
  501. * about that, just avoid to call into the tick code, which
  502. * has now objections against negative expiry values.
  503. */
  504. if (expires.tv64 < 0)
  505. return -ETIME;
  506. if (expires.tv64 >= cpu_base->expires_next.tv64)
  507. return 0;
  508. /*
  509. * If a hang was detected in the last timer interrupt then we
  510. * do not schedule a timer which is earlier than the expiry
  511. * which we enforced in the hang detection. We want the system
  512. * to make progress.
  513. */
  514. if (cpu_base->hang_detected)
  515. return 0;
  516. /*
  517. * Clockevents returns -ETIME, when the event was in the past.
  518. */
  519. res = tick_program_event(expires, 0);
  520. if (!IS_ERR_VALUE(res))
  521. cpu_base->expires_next = expires;
  522. return res;
  523. }
  524. /*
  525. * Initialize the high resolution related parts of cpu_base
  526. */
  527. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
  528. {
  529. base->expires_next.tv64 = KTIME_MAX;
  530. base->hres_active = 0;
  531. }
  532. static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
  533. {
  534. ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
  535. ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
  536. ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
  537. return ktime_get_update_offsets_now(offs_real, offs_boot, offs_tai);
  538. }
  539. /*
  540. * Retrigger next event is called after clock was set
  541. *
  542. * Called with interrupts disabled via on_each_cpu()
  543. */
  544. static void retrigger_next_event(void *arg)
  545. {
  546. struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
  547. if (!hrtimer_hres_active())
  548. return;
  549. raw_spin_lock(&base->lock);
  550. hrtimer_update_base(base);
  551. hrtimer_force_reprogram(base, 0);
  552. raw_spin_unlock(&base->lock);
  553. }
  554. /*
  555. * Switch to high resolution mode
  556. */
  557. static int hrtimer_switch_to_hres(void)
  558. {
  559. int i, cpu = smp_processor_id();
  560. struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
  561. unsigned long flags;
  562. if (base->hres_active)
  563. return 1;
  564. local_irq_save(flags);
  565. if (tick_init_highres()) {
  566. local_irq_restore(flags);
  567. printk(KERN_WARNING "Could not switch to high resolution "
  568. "mode on CPU %d\n", cpu);
  569. return 0;
  570. }
  571. base->hres_active = 1;
  572. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  573. base->clock_base[i].resolution = KTIME_HIGH_RES;
  574. tick_setup_sched_timer();
  575. /* "Retrigger" the interrupt to get things going */
  576. retrigger_next_event(NULL);
  577. local_irq_restore(flags);
  578. return 1;
  579. }
  580. static void clock_was_set_work(struct work_struct *work)
  581. {
  582. clock_was_set();
  583. }
  584. static DECLARE_WORK(hrtimer_work, clock_was_set_work);
  585. /*
  586. * Called from timekeeping and resume code to reprogramm the hrtimer
  587. * interrupt device on all cpus.
  588. */
  589. void clock_was_set_delayed(void)
  590. {
  591. schedule_work(&hrtimer_work);
  592. }
  593. #else
  594. static inline int hrtimer_hres_active(void) { return 0; }
  595. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  596. static inline int hrtimer_switch_to_hres(void) { return 0; }
  597. static inline void
  598. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  599. static inline int hrtimer_reprogram(struct hrtimer *timer,
  600. struct hrtimer_clock_base *base)
  601. {
  602. return 0;
  603. }
  604. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  605. static inline void retrigger_next_event(void *arg) { }
  606. #endif /* CONFIG_HIGH_RES_TIMERS */
  607. /*
  608. * Clock realtime was set
  609. *
  610. * Change the offset of the realtime clock vs. the monotonic
  611. * clock.
  612. *
  613. * We might have to reprogram the high resolution timer interrupt. On
  614. * SMP we call the architecture specific code to retrigger _all_ high
  615. * resolution timer interrupts. On UP we just disable interrupts and
  616. * call the high resolution interrupt code.
  617. */
  618. void clock_was_set(void)
  619. {
  620. #ifdef CONFIG_HIGH_RES_TIMERS
  621. /* Retrigger the CPU local events everywhere */
  622. on_each_cpu(retrigger_next_event, NULL, 1);
  623. #endif
  624. timerfd_clock_was_set();
  625. }
  626. /*
  627. * During resume we might have to reprogram the high resolution timer
  628. * interrupt on all online CPUs. However, all other CPUs will be
  629. * stopped with IRQs interrupts disabled so the clock_was_set() call
  630. * must be deferred.
  631. */
  632. void hrtimers_resume(void)
  633. {
  634. WARN_ONCE(!irqs_disabled(),
  635. KERN_INFO "hrtimers_resume() called with IRQs enabled!");
  636. /* Retrigger on the local CPU */
  637. retrigger_next_event(NULL);
  638. /* And schedule a retrigger for all others */
  639. clock_was_set_delayed();
  640. }
  641. static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
  642. {
  643. #ifdef CONFIG_TIMER_STATS
  644. if (timer->start_site)
  645. return;
  646. timer->start_site = __builtin_return_address(0);
  647. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  648. timer->start_pid = current->pid;
  649. #endif
  650. }
  651. static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
  652. {
  653. #ifdef CONFIG_TIMER_STATS
  654. timer->start_site = NULL;
  655. #endif
  656. }
  657. static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
  658. {
  659. #ifdef CONFIG_TIMER_STATS
  660. if (likely(!timer_stats_active))
  661. return;
  662. timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
  663. timer->function, timer->start_comm, 0);
  664. #endif
  665. }
  666. /*
  667. * Counterpart to lock_hrtimer_base above:
  668. */
  669. static inline
  670. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  671. {
  672. raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  673. }
  674. /**
  675. * hrtimer_forward - forward the timer expiry
  676. * @timer: hrtimer to forward
  677. * @now: forward past this time
  678. * @interval: the interval to forward
  679. *
  680. * Forward the timer expiry so it will expire in the future.
  681. * Returns the number of overruns.
  682. */
  683. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  684. {
  685. u64 orun = 1;
  686. ktime_t delta;
  687. delta = ktime_sub(now, hrtimer_get_expires(timer));
  688. if (delta.tv64 < 0)
  689. return 0;
  690. if (interval.tv64 < timer->base->resolution.tv64)
  691. interval.tv64 = timer->base->resolution.tv64;
  692. if (unlikely(delta.tv64 >= interval.tv64)) {
  693. s64 incr = ktime_to_ns(interval);
  694. orun = ktime_divns(delta, incr);
  695. hrtimer_add_expires_ns(timer, incr * orun);
  696. if (hrtimer_get_expires_tv64(timer) > now.tv64)
  697. return orun;
  698. /*
  699. * This (and the ktime_add() below) is the
  700. * correction for exact:
  701. */
  702. orun++;
  703. }
  704. hrtimer_add_expires(timer, interval);
  705. return orun;
  706. }
  707. EXPORT_SYMBOL_GPL(hrtimer_forward);
  708. /*
  709. * enqueue_hrtimer - internal function to (re)start a timer
  710. *
  711. * The timer is inserted in expiry order. Insertion into the
  712. * red black tree is O(log(n)). Must hold the base lock.
  713. *
  714. * Returns 1 when the new timer is the leftmost timer in the tree.
  715. */
  716. static int enqueue_hrtimer(struct hrtimer *timer,
  717. struct hrtimer_clock_base *base)
  718. {
  719. debug_activate(timer);
  720. timerqueue_add(&base->active, &timer->node);
  721. base->cpu_base->active_bases |= 1 << base->index;
  722. /*
  723. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  724. * state of a possibly running callback.
  725. */
  726. timer->state |= HRTIMER_STATE_ENQUEUED;
  727. return (&timer->node == base->active.next);
  728. }
  729. /*
  730. * __remove_hrtimer - internal function to remove a timer
  731. *
  732. * Caller must hold the base lock.
  733. *
  734. * High resolution timer mode reprograms the clock event device when the
  735. * timer is the one which expires next. The caller can disable this by setting
  736. * reprogram to zero. This is useful, when the context does a reprogramming
  737. * anyway (e.g. timer interrupt)
  738. */
  739. static void __remove_hrtimer(struct hrtimer *timer,
  740. struct hrtimer_clock_base *base,
  741. unsigned long newstate, int reprogram)
  742. {
  743. struct timerqueue_node *next_timer;
  744. if (!(timer->state & HRTIMER_STATE_ENQUEUED))
  745. goto out;
  746. next_timer = timerqueue_getnext(&base->active);
  747. timerqueue_del(&base->active, &timer->node);
  748. if (&timer->node == next_timer) {
  749. #ifdef CONFIG_HIGH_RES_TIMERS
  750. /* Reprogram the clock event device. if enabled */
  751. if (reprogram && hrtimer_hres_active()) {
  752. ktime_t expires;
  753. expires = ktime_sub(hrtimer_get_expires(timer),
  754. base->offset);
  755. if (base->cpu_base->expires_next.tv64 == expires.tv64)
  756. hrtimer_force_reprogram(base->cpu_base, 1);
  757. }
  758. #endif
  759. }
  760. if (!timerqueue_getnext(&base->active))
  761. base->cpu_base->active_bases &= ~(1 << base->index);
  762. out:
  763. timer->state = newstate;
  764. }
  765. /*
  766. * remove hrtimer, called with base lock held
  767. */
  768. static inline int
  769. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  770. {
  771. if (hrtimer_is_queued(timer)) {
  772. unsigned long state;
  773. int reprogram;
  774. /*
  775. * Remove the timer and force reprogramming when high
  776. * resolution mode is active and the timer is on the current
  777. * CPU. If we remove a timer on another CPU, reprogramming is
  778. * skipped. The interrupt event on this CPU is fired and
  779. * reprogramming happens in the interrupt handler. This is a
  780. * rare case and less expensive than a smp call.
  781. */
  782. debug_deactivate(timer);
  783. timer_stats_hrtimer_clear_start_info(timer);
  784. reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
  785. /*
  786. * We must preserve the CALLBACK state flag here,
  787. * otherwise we could move the timer base in
  788. * switch_hrtimer_base.
  789. */
  790. state = timer->state & HRTIMER_STATE_CALLBACK;
  791. __remove_hrtimer(timer, base, state, reprogram);
  792. return 1;
  793. }
  794. return 0;
  795. }
  796. int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  797. unsigned long delta_ns, const enum hrtimer_mode mode,
  798. int wakeup)
  799. {
  800. struct hrtimer_clock_base *base, *new_base;
  801. unsigned long flags;
  802. int ret, leftmost;
  803. base = lock_hrtimer_base(timer, &flags);
  804. /* Remove an active timer from the queue: */
  805. ret = remove_hrtimer(timer, base);
  806. if (mode & HRTIMER_MODE_REL) {
  807. tim = ktime_add_safe(tim, base->get_time());
  808. /*
  809. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  810. * to signal that they simply return xtime in
  811. * do_gettimeoffset(). In this case we want to round up by
  812. * resolution when starting a relative timer, to avoid short
  813. * timeouts. This will go away with the GTOD framework.
  814. */
  815. #ifdef CONFIG_TIME_LOW_RES
  816. tim = ktime_add_safe(tim, base->resolution);
  817. #endif
  818. }
  819. hrtimer_set_expires_range_ns(timer, tim, delta_ns);
  820. /* Switch the timer base, if necessary: */
  821. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  822. timer_stats_hrtimer_set_start_info(timer);
  823. leftmost = enqueue_hrtimer(timer, new_base);
  824. if (!leftmost) {
  825. unlock_hrtimer_base(timer, &flags);
  826. return ret;
  827. }
  828. if (!hrtimer_is_hres_active(timer)) {
  829. /*
  830. * Kick to reschedule the next tick to handle the new timer
  831. * on dynticks target.
  832. */
  833. wake_up_nohz_cpu(new_base->cpu_base->cpu);
  834. } else if (new_base->cpu_base == &__get_cpu_var(hrtimer_bases) &&
  835. hrtimer_reprogram(timer, new_base)) {
  836. /*
  837. * Only allow reprogramming if the new base is on this CPU.
  838. * (it might still be on another CPU if the timer was pending)
  839. *
  840. * XXX send_remote_softirq() ?
  841. */
  842. if (wakeup) {
  843. /*
  844. * We need to drop cpu_base->lock to avoid a
  845. * lock ordering issue vs. rq->lock.
  846. */
  847. raw_spin_unlock(&new_base->cpu_base->lock);
  848. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  849. local_irq_restore(flags);
  850. return ret;
  851. } else {
  852. __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  853. }
  854. }
  855. unlock_hrtimer_base(timer, &flags);
  856. return ret;
  857. }
  858. EXPORT_SYMBOL_GPL(__hrtimer_start_range_ns);
  859. /**
  860. * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
  861. * @timer: the timer to be added
  862. * @tim: expiry time
  863. * @delta_ns: "slack" range for the timer
  864. * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
  865. * relative (HRTIMER_MODE_REL)
  866. *
  867. * Returns:
  868. * 0 on success
  869. * 1 when the timer was active
  870. */
  871. int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  872. unsigned long delta_ns, const enum hrtimer_mode mode)
  873. {
  874. return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
  875. }
  876. EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
  877. /**
  878. * hrtimer_start - (re)start an hrtimer on the current CPU
  879. * @timer: the timer to be added
  880. * @tim: expiry time
  881. * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
  882. * relative (HRTIMER_MODE_REL)
  883. *
  884. * Returns:
  885. * 0 on success
  886. * 1 when the timer was active
  887. */
  888. int
  889. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  890. {
  891. return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
  892. }
  893. EXPORT_SYMBOL_GPL(hrtimer_start);
  894. /**
  895. * hrtimer_try_to_cancel - try to deactivate a timer
  896. * @timer: hrtimer to stop
  897. *
  898. * Returns:
  899. * 0 when the timer was not active
  900. * 1 when the timer was active
  901. * -1 when the timer is currently excuting the callback function and
  902. * cannot be stopped
  903. */
  904. int hrtimer_try_to_cancel(struct hrtimer *timer)
  905. {
  906. struct hrtimer_clock_base *base;
  907. unsigned long flags;
  908. int ret = -1;
  909. base = lock_hrtimer_base(timer, &flags);
  910. if (!hrtimer_callback_running(timer))
  911. ret = remove_hrtimer(timer, base);
  912. unlock_hrtimer_base(timer, &flags);
  913. return ret;
  914. }
  915. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  916. /**
  917. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  918. * @timer: the timer to be cancelled
  919. *
  920. * Returns:
  921. * 0 when the timer was not active
  922. * 1 when the timer was active
  923. */
  924. int hrtimer_cancel(struct hrtimer *timer)
  925. {
  926. for (;;) {
  927. int ret = hrtimer_try_to_cancel(timer);
  928. if (ret >= 0)
  929. return ret;
  930. cpu_relax();
  931. }
  932. }
  933. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  934. /**
  935. * hrtimer_get_remaining - get remaining time for the timer
  936. * @timer: the timer to read
  937. */
  938. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  939. {
  940. unsigned long flags;
  941. ktime_t rem;
  942. lock_hrtimer_base(timer, &flags);
  943. rem = hrtimer_expires_remaining(timer);
  944. unlock_hrtimer_base(timer, &flags);
  945. return rem;
  946. }
  947. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  948. #ifdef CONFIG_NO_HZ_COMMON
  949. /**
  950. * hrtimer_get_next_event - get the time until next expiry event
  951. *
  952. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  953. * is pending.
  954. */
  955. ktime_t hrtimer_get_next_event(void)
  956. {
  957. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  958. struct hrtimer_clock_base *base = cpu_base->clock_base;
  959. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  960. unsigned long flags;
  961. int i;
  962. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  963. if (!hrtimer_hres_active()) {
  964. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  965. struct hrtimer *timer;
  966. struct timerqueue_node *next;
  967. next = timerqueue_getnext(&base->active);
  968. if (!next)
  969. continue;
  970. timer = container_of(next, struct hrtimer, node);
  971. delta.tv64 = hrtimer_get_expires_tv64(timer);
  972. delta = ktime_sub(delta, base->get_time());
  973. if (delta.tv64 < mindelta.tv64)
  974. mindelta.tv64 = delta.tv64;
  975. }
  976. }
  977. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  978. if (mindelta.tv64 < 0)
  979. mindelta.tv64 = 0;
  980. return mindelta;
  981. }
  982. #endif
  983. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  984. enum hrtimer_mode mode)
  985. {
  986. struct hrtimer_cpu_base *cpu_base;
  987. int base;
  988. memset(timer, 0, sizeof(struct hrtimer));
  989. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  990. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  991. clock_id = CLOCK_MONOTONIC;
  992. base = hrtimer_clockid_to_base(clock_id);
  993. timer->base = &cpu_base->clock_base[base];
  994. timerqueue_init(&timer->node);
  995. #ifdef CONFIG_TIMER_STATS
  996. timer->start_site = NULL;
  997. timer->start_pid = -1;
  998. memset(timer->start_comm, 0, TASK_COMM_LEN);
  999. #endif
  1000. }
  1001. /**
  1002. * hrtimer_init - initialize a timer to the given clock
  1003. * @timer: the timer to be initialized
  1004. * @clock_id: the clock to be used
  1005. * @mode: timer mode abs/rel
  1006. */
  1007. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  1008. enum hrtimer_mode mode)
  1009. {
  1010. debug_init(timer, clock_id, mode);
  1011. __hrtimer_init(timer, clock_id, mode);
  1012. }
  1013. EXPORT_SYMBOL_GPL(hrtimer_init);
  1014. /**
  1015. * hrtimer_get_res - get the timer resolution for a clock
  1016. * @which_clock: which clock to query
  1017. * @tp: pointer to timespec variable to store the resolution
  1018. *
  1019. * Store the resolution of the clock selected by @which_clock in the
  1020. * variable pointed to by @tp.
  1021. */
  1022. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  1023. {
  1024. struct hrtimer_cpu_base *cpu_base;
  1025. int base = hrtimer_clockid_to_base(which_clock);
  1026. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  1027. *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
  1028. return 0;
  1029. }
  1030. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  1031. static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
  1032. {
  1033. struct hrtimer_clock_base *base = timer->base;
  1034. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  1035. enum hrtimer_restart (*fn)(struct hrtimer *);
  1036. int restart;
  1037. WARN_ON(!irqs_disabled());
  1038. debug_deactivate(timer);
  1039. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  1040. timer_stats_account_hrtimer(timer);
  1041. fn = timer->function;
  1042. /*
  1043. * Because we run timers from hardirq context, there is no chance
  1044. * they get migrated to another cpu, therefore its safe to unlock
  1045. * the timer base.
  1046. */
  1047. raw_spin_unlock(&cpu_base->lock);
  1048. trace_hrtimer_expire_entry(timer, now);
  1049. restart = fn(timer);
  1050. trace_hrtimer_expire_exit(timer);
  1051. raw_spin_lock(&cpu_base->lock);
  1052. /*
  1053. * Note: We clear the CALLBACK bit after enqueue_hrtimer and
  1054. * we do not reprogramm the event hardware. Happens either in
  1055. * hrtimer_start_range_ns() or in hrtimer_interrupt()
  1056. */
  1057. if (restart != HRTIMER_NORESTART) {
  1058. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  1059. enqueue_hrtimer(timer, base);
  1060. }
  1061. WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
  1062. timer->state &= ~HRTIMER_STATE_CALLBACK;
  1063. }
  1064. #ifdef CONFIG_HIGH_RES_TIMERS
  1065. /*
  1066. * High resolution timer interrupt
  1067. * Called with interrupts disabled
  1068. */
  1069. void hrtimer_interrupt(struct clock_event_device *dev)
  1070. {
  1071. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1072. ktime_t expires_next, now, entry_time, delta;
  1073. int i, retries = 0;
  1074. BUG_ON(!cpu_base->hres_active);
  1075. cpu_base->nr_events++;
  1076. dev->next_event.tv64 = KTIME_MAX;
  1077. raw_spin_lock(&cpu_base->lock);
  1078. entry_time = now = hrtimer_update_base(cpu_base);
  1079. retry:
  1080. expires_next.tv64 = KTIME_MAX;
  1081. /*
  1082. * We set expires_next to KTIME_MAX here with cpu_base->lock
  1083. * held to prevent that a timer is enqueued in our queue via
  1084. * the migration code. This does not affect enqueueing of
  1085. * timers which run their callback and need to be requeued on
  1086. * this CPU.
  1087. */
  1088. cpu_base->expires_next.tv64 = KTIME_MAX;
  1089. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1090. struct hrtimer_clock_base *base;
  1091. struct timerqueue_node *node;
  1092. ktime_t basenow;
  1093. if (!(cpu_base->active_bases & (1 << i)))
  1094. continue;
  1095. base = cpu_base->clock_base + i;
  1096. basenow = ktime_add(now, base->offset);
  1097. while ((node = timerqueue_getnext(&base->active))) {
  1098. struct hrtimer *timer;
  1099. timer = container_of(node, struct hrtimer, node);
  1100. /*
  1101. * The immediate goal for using the softexpires is
  1102. * minimizing wakeups, not running timers at the
  1103. * earliest interrupt after their soft expiration.
  1104. * This allows us to avoid using a Priority Search
  1105. * Tree, which can answer a stabbing querry for
  1106. * overlapping intervals and instead use the simple
  1107. * BST we already have.
  1108. * We don't add extra wakeups by delaying timers that
  1109. * are right-of a not yet expired timer, because that
  1110. * timer will have to trigger a wakeup anyway.
  1111. */
  1112. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
  1113. ktime_t expires;
  1114. expires = ktime_sub(hrtimer_get_expires(timer),
  1115. base->offset);
  1116. if (expires.tv64 < 0)
  1117. expires.tv64 = KTIME_MAX;
  1118. if (expires.tv64 < expires_next.tv64)
  1119. expires_next = expires;
  1120. break;
  1121. }
  1122. __run_hrtimer(timer, &basenow);
  1123. }
  1124. }
  1125. /*
  1126. * Store the new expiry value so the migration code can verify
  1127. * against it.
  1128. */
  1129. cpu_base->expires_next = expires_next;
  1130. raw_spin_unlock(&cpu_base->lock);
  1131. /* Reprogramming necessary ? */
  1132. if (expires_next.tv64 == KTIME_MAX ||
  1133. !tick_program_event(expires_next, 0)) {
  1134. cpu_base->hang_detected = 0;
  1135. return;
  1136. }
  1137. /*
  1138. * The next timer was already expired due to:
  1139. * - tracing
  1140. * - long lasting callbacks
  1141. * - being scheduled away when running in a VM
  1142. *
  1143. * We need to prevent that we loop forever in the hrtimer
  1144. * interrupt routine. We give it 3 attempts to avoid
  1145. * overreacting on some spurious event.
  1146. *
  1147. * Acquire base lock for updating the offsets and retrieving
  1148. * the current time.
  1149. */
  1150. raw_spin_lock(&cpu_base->lock);
  1151. now = hrtimer_update_base(cpu_base);
  1152. cpu_base->nr_retries++;
  1153. if (++retries < 3)
  1154. goto retry;
  1155. /*
  1156. * Give the system a chance to do something else than looping
  1157. * here. We stored the entry time, so we know exactly how long
  1158. * we spent here. We schedule the next event this amount of
  1159. * time away.
  1160. */
  1161. cpu_base->nr_hangs++;
  1162. cpu_base->hang_detected = 1;
  1163. raw_spin_unlock(&cpu_base->lock);
  1164. delta = ktime_sub(now, entry_time);
  1165. if (delta.tv64 > cpu_base->max_hang_time.tv64)
  1166. cpu_base->max_hang_time = delta;
  1167. /*
  1168. * Limit it to a sensible value as we enforce a longer
  1169. * delay. Give the CPU at least 100ms to catch up.
  1170. */
  1171. if (delta.tv64 > 100 * NSEC_PER_MSEC)
  1172. expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
  1173. else
  1174. expires_next = ktime_add(now, delta);
  1175. tick_program_event(expires_next, 1);
  1176. printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
  1177. ktime_to_ns(delta));
  1178. }
  1179. /*
  1180. * local version of hrtimer_peek_ahead_timers() called with interrupts
  1181. * disabled.
  1182. */
  1183. static void __hrtimer_peek_ahead_timers(void)
  1184. {
  1185. struct tick_device *td;
  1186. if (!hrtimer_hres_active())
  1187. return;
  1188. td = &__get_cpu_var(tick_cpu_device);
  1189. if (td && td->evtdev)
  1190. hrtimer_interrupt(td->evtdev);
  1191. }
  1192. /**
  1193. * hrtimer_peek_ahead_timers -- run soft-expired timers now
  1194. *
  1195. * hrtimer_peek_ahead_timers will peek at the timer queue of
  1196. * the current cpu and check if there are any timers for which
  1197. * the soft expires time has passed. If any such timers exist,
  1198. * they are run immediately and then removed from the timer queue.
  1199. *
  1200. */
  1201. void hrtimer_peek_ahead_timers(void)
  1202. {
  1203. unsigned long flags;
  1204. local_irq_save(flags);
  1205. __hrtimer_peek_ahead_timers();
  1206. local_irq_restore(flags);
  1207. }
  1208. static void run_hrtimer_softirq(struct softirq_action *h)
  1209. {
  1210. hrtimer_peek_ahead_timers();
  1211. }
  1212. #else /* CONFIG_HIGH_RES_TIMERS */
  1213. static inline void __hrtimer_peek_ahead_timers(void) { }
  1214. #endif /* !CONFIG_HIGH_RES_TIMERS */
  1215. /*
  1216. * Called from timer softirq every jiffy, expire hrtimers:
  1217. *
  1218. * For HRT its the fall back code to run the softirq in the timer
  1219. * softirq context in case the hrtimer initialization failed or has
  1220. * not been done yet.
  1221. */
  1222. void hrtimer_run_pending(void)
  1223. {
  1224. if (hrtimer_hres_active())
  1225. return;
  1226. /*
  1227. * This _is_ ugly: We have to check in the softirq context,
  1228. * whether we can switch to highres and / or nohz mode. The
  1229. * clocksource switch happens in the timer interrupt with
  1230. * xtime_lock held. Notification from there only sets the
  1231. * check bit in the tick_oneshot code, otherwise we might
  1232. * deadlock vs. xtime_lock.
  1233. */
  1234. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
  1235. hrtimer_switch_to_hres();
  1236. }
  1237. /*
  1238. * Called from hardirq context every jiffy
  1239. */
  1240. void hrtimer_run_queues(void)
  1241. {
  1242. struct timerqueue_node *node;
  1243. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1244. struct hrtimer_clock_base *base;
  1245. int index, gettime = 1;
  1246. if (hrtimer_hres_active())
  1247. return;
  1248. for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  1249. base = &cpu_base->clock_base[index];
  1250. if (!timerqueue_getnext(&base->active))
  1251. continue;
  1252. if (gettime) {
  1253. hrtimer_get_softirq_time(cpu_base);
  1254. gettime = 0;
  1255. }
  1256. raw_spin_lock(&cpu_base->lock);
  1257. while ((node = timerqueue_getnext(&base->active))) {
  1258. struct hrtimer *timer;
  1259. timer = container_of(node, struct hrtimer, node);
  1260. if (base->softirq_time.tv64 <=
  1261. hrtimer_get_expires_tv64(timer))
  1262. break;
  1263. __run_hrtimer(timer, &base->softirq_time);
  1264. }
  1265. raw_spin_unlock(&cpu_base->lock);
  1266. }
  1267. }
  1268. /*
  1269. * Sleep related functions:
  1270. */
  1271. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1272. {
  1273. struct hrtimer_sleeper *t =
  1274. container_of(timer, struct hrtimer_sleeper, timer);
  1275. struct task_struct *task = t->task;
  1276. t->task = NULL;
  1277. if (task)
  1278. wake_up_process(task);
  1279. return HRTIMER_NORESTART;
  1280. }
  1281. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1282. {
  1283. sl->timer.function = hrtimer_wakeup;
  1284. sl->task = task;
  1285. }
  1286. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  1287. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1288. {
  1289. hrtimer_init_sleeper(t, current);
  1290. do {
  1291. set_current_state(TASK_INTERRUPTIBLE);
  1292. hrtimer_start_expires(&t->timer, mode);
  1293. if (!hrtimer_active(&t->timer))
  1294. t->task = NULL;
  1295. if (likely(t->task))
  1296. freezable_schedule();
  1297. hrtimer_cancel(&t->timer);
  1298. mode = HRTIMER_MODE_ABS;
  1299. } while (t->task && !signal_pending(current));
  1300. __set_current_state(TASK_RUNNING);
  1301. return t->task == NULL;
  1302. }
  1303. static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
  1304. {
  1305. struct timespec rmt;
  1306. ktime_t rem;
  1307. rem = hrtimer_expires_remaining(timer);
  1308. if (rem.tv64 <= 0)
  1309. return 0;
  1310. rmt = ktime_to_timespec(rem);
  1311. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  1312. return -EFAULT;
  1313. return 1;
  1314. }
  1315. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1316. {
  1317. struct hrtimer_sleeper t;
  1318. struct timespec __user *rmtp;
  1319. int ret = 0;
  1320. hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
  1321. HRTIMER_MODE_ABS);
  1322. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  1323. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  1324. goto out;
  1325. rmtp = restart->nanosleep.rmtp;
  1326. if (rmtp) {
  1327. ret = update_rmtp(&t.timer, rmtp);
  1328. if (ret <= 0)
  1329. goto out;
  1330. }
  1331. /* The other values in restart are already filled in */
  1332. ret = -ERESTART_RESTARTBLOCK;
  1333. out:
  1334. destroy_hrtimer_on_stack(&t.timer);
  1335. return ret;
  1336. }
  1337. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  1338. const enum hrtimer_mode mode, const clockid_t clockid)
  1339. {
  1340. struct restart_block *restart;
  1341. struct hrtimer_sleeper t;
  1342. int ret = 0;
  1343. unsigned long slack;
  1344. slack = current->timer_slack_ns;
  1345. if (dl_task(current) || rt_task(current))
  1346. slack = 0;
  1347. hrtimer_init_on_stack(&t.timer, clockid, mode);
  1348. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  1349. if (do_nanosleep(&t, mode))
  1350. goto out;
  1351. /* Absolute timers do not update the rmtp value and restart: */
  1352. if (mode == HRTIMER_MODE_ABS) {
  1353. ret = -ERESTARTNOHAND;
  1354. goto out;
  1355. }
  1356. if (rmtp) {
  1357. ret = update_rmtp(&t.timer, rmtp);
  1358. if (ret <= 0)
  1359. goto out;
  1360. }
  1361. restart = &current_thread_info()->restart_block;
  1362. restart->fn = hrtimer_nanosleep_restart;
  1363. restart->nanosleep.clockid = t.timer.base->clockid;
  1364. restart->nanosleep.rmtp = rmtp;
  1365. restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
  1366. ret = -ERESTART_RESTARTBLOCK;
  1367. out:
  1368. destroy_hrtimer_on_stack(&t.timer);
  1369. return ret;
  1370. }
  1371. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  1372. struct timespec __user *, rmtp)
  1373. {
  1374. struct timespec tu;
  1375. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  1376. return -EFAULT;
  1377. if (!timespec_valid(&tu))
  1378. return -EINVAL;
  1379. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1380. }
  1381. /*
  1382. * Functions related to boot-time initialization:
  1383. */
  1384. static void init_hrtimers_cpu(int cpu)
  1385. {
  1386. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1387. int i;
  1388. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1389. cpu_base->clock_base[i].cpu_base = cpu_base;
  1390. timerqueue_init_head(&cpu_base->clock_base[i].active);
  1391. }
  1392. cpu_base->cpu = cpu;
  1393. hrtimer_init_hres(cpu_base);
  1394. }
  1395. #ifdef CONFIG_HOTPLUG_CPU
  1396. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1397. struct hrtimer_clock_base *new_base)
  1398. {
  1399. struct hrtimer *timer;
  1400. struct timerqueue_node *node;
  1401. while ((node = timerqueue_getnext(&old_base->active))) {
  1402. timer = container_of(node, struct hrtimer, node);
  1403. BUG_ON(hrtimer_callback_running(timer));
  1404. debug_deactivate(timer);
  1405. /*
  1406. * Mark it as STATE_MIGRATE not INACTIVE otherwise the
  1407. * timer could be seen as !active and just vanish away
  1408. * under us on another CPU
  1409. */
  1410. __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
  1411. timer->base = new_base;
  1412. /*
  1413. * Enqueue the timers on the new cpu. This does not
  1414. * reprogram the event device in case the timer
  1415. * expires before the earliest on this CPU, but we run
  1416. * hrtimer_interrupt after we migrated everything to
  1417. * sort out already expired timers and reprogram the
  1418. * event device.
  1419. */
  1420. enqueue_hrtimer(timer, new_base);
  1421. /* Clear the migration state bit */
  1422. timer->state &= ~HRTIMER_STATE_MIGRATE;
  1423. }
  1424. }
  1425. static void migrate_hrtimers(int scpu)
  1426. {
  1427. struct hrtimer_cpu_base *old_base, *new_base;
  1428. int i;
  1429. BUG_ON(cpu_online(scpu));
  1430. tick_cancel_sched_timer(scpu);
  1431. local_irq_disable();
  1432. old_base = &per_cpu(hrtimer_bases, scpu);
  1433. new_base = &__get_cpu_var(hrtimer_bases);
  1434. /*
  1435. * The caller is globally serialized and nobody else
  1436. * takes two locks at once, deadlock is not possible.
  1437. */
  1438. raw_spin_lock(&new_base->lock);
  1439. raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1440. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1441. migrate_hrtimer_list(&old_base->clock_base[i],
  1442. &new_base->clock_base[i]);
  1443. }
  1444. raw_spin_unlock(&old_base->lock);
  1445. raw_spin_unlock(&new_base->lock);
  1446. /* Check, if we got expired work to do */
  1447. __hrtimer_peek_ahead_timers();
  1448. local_irq_enable();
  1449. }
  1450. #endif /* CONFIG_HOTPLUG_CPU */
  1451. static int hrtimer_cpu_notify(struct notifier_block *self,
  1452. unsigned long action, void *hcpu)
  1453. {
  1454. int scpu = (long)hcpu;
  1455. switch (action) {
  1456. case CPU_UP_PREPARE:
  1457. case CPU_UP_PREPARE_FROZEN:
  1458. init_hrtimers_cpu(scpu);
  1459. break;
  1460. #ifdef CONFIG_HOTPLUG_CPU
  1461. case CPU_DYING:
  1462. case CPU_DYING_FROZEN:
  1463. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
  1464. break;
  1465. case CPU_DEAD:
  1466. case CPU_DEAD_FROZEN:
  1467. {
  1468. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
  1469. migrate_hrtimers(scpu);
  1470. break;
  1471. }
  1472. #endif
  1473. default:
  1474. break;
  1475. }
  1476. return NOTIFY_OK;
  1477. }
  1478. static struct notifier_block hrtimers_nb = {
  1479. .notifier_call = hrtimer_cpu_notify,
  1480. };
  1481. void __init hrtimers_init(void)
  1482. {
  1483. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1484. (void *)(long)smp_processor_id());
  1485. register_cpu_notifier(&hrtimers_nb);
  1486. #ifdef CONFIG_HIGH_RES_TIMERS
  1487. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  1488. #endif
  1489. }
  1490. /**
  1491. * schedule_hrtimeout_range_clock - sleep until timeout
  1492. * @expires: timeout value (ktime_t)
  1493. * @delta: slack in expires timeout (ktime_t)
  1494. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1495. * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
  1496. */
  1497. int __sched
  1498. schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
  1499. const enum hrtimer_mode mode, int clock)
  1500. {
  1501. struct hrtimer_sleeper t;
  1502. /*
  1503. * Optimize when a zero timeout value is given. It does not
  1504. * matter whether this is an absolute or a relative time.
  1505. */
  1506. if (expires && !expires->tv64) {
  1507. __set_current_state(TASK_RUNNING);
  1508. return 0;
  1509. }
  1510. /*
  1511. * A NULL parameter means "infinite"
  1512. */
  1513. if (!expires) {
  1514. schedule();
  1515. __set_current_state(TASK_RUNNING);
  1516. return -EINTR;
  1517. }
  1518. hrtimer_init_on_stack(&t.timer, clock, mode);
  1519. hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
  1520. hrtimer_init_sleeper(&t, current);
  1521. hrtimer_start_expires(&t.timer, mode);
  1522. if (!hrtimer_active(&t.timer))
  1523. t.task = NULL;
  1524. if (likely(t.task))
  1525. schedule();
  1526. hrtimer_cancel(&t.timer);
  1527. destroy_hrtimer_on_stack(&t.timer);
  1528. __set_current_state(TASK_RUNNING);
  1529. return !t.task ? 0 : -EINTR;
  1530. }
  1531. /**
  1532. * schedule_hrtimeout_range - sleep until timeout
  1533. * @expires: timeout value (ktime_t)
  1534. * @delta: slack in expires timeout (ktime_t)
  1535. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1536. *
  1537. * Make the current task sleep until the given expiry time has
  1538. * elapsed. The routine will return immediately unless
  1539. * the current task state has been set (see set_current_state()).
  1540. *
  1541. * The @delta argument gives the kernel the freedom to schedule the
  1542. * actual wakeup to a time that is both power and performance friendly.
  1543. * The kernel give the normal best effort behavior for "@expires+@delta",
  1544. * but may decide to fire the timer earlier, but no earlier than @expires.
  1545. *
  1546. * You can set the task state as follows -
  1547. *
  1548. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1549. * pass before the routine returns.
  1550. *
  1551. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1552. * delivered to the current task.
  1553. *
  1554. * The current task state is guaranteed to be TASK_RUNNING when this
  1555. * routine returns.
  1556. *
  1557. * Returns 0 when the timer has expired otherwise -EINTR
  1558. */
  1559. int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
  1560. const enum hrtimer_mode mode)
  1561. {
  1562. return schedule_hrtimeout_range_clock(expires, delta, mode,
  1563. CLOCK_MONOTONIC);
  1564. }
  1565. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
  1566. /**
  1567. * schedule_hrtimeout - sleep until timeout
  1568. * @expires: timeout value (ktime_t)
  1569. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1570. *
  1571. * Make the current task sleep until the given expiry time has
  1572. * elapsed. The routine will return immediately unless
  1573. * the current task state has been set (see set_current_state()).
  1574. *
  1575. * You can set the task state as follows -
  1576. *
  1577. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1578. * pass before the routine returns.
  1579. *
  1580. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1581. * delivered to the current task.
  1582. *
  1583. * The current task state is guaranteed to be TASK_RUNNING when this
  1584. * routine returns.
  1585. *
  1586. * Returns 0 when the timer has expired otherwise -EINTR
  1587. */
  1588. int __sched schedule_hrtimeout(ktime_t *expires,
  1589. const enum hrtimer_mode mode)
  1590. {
  1591. return schedule_hrtimeout_range(expires, 0, mode);
  1592. }
  1593. EXPORT_SYMBOL_GPL(schedule_hrtimeout);