hrtimer.c 52 KB

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