hrtimer.c 50 KB

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