hrtimer.c 48 KB

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