hrtimer.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
  4. * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
  5. * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
  6. *
  7. * High-resolution kernel timers
  8. *
  9. * In contrast to the low-resolution timeout API, aka timer wheel,
  10. * hrtimers provide finer resolution and accuracy depending on system
  11. * configuration and capabilities.
  12. *
  13. * Started by: Thomas Gleixner and Ingo Molnar
  14. *
  15. * Credits:
  16. * Based on the original timer wheel code
  17. *
  18. * Help, testing, suggestions, bugfixes, improvements were
  19. * provided by:
  20. *
  21. * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
  22. * et. al.
  23. */
  24. #include <linux/cpu.h>
  25. #include <linux/export.h>
  26. #include <linux/percpu.h>
  27. #include <linux/hrtimer.h>
  28. #include <linux/notifier.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/tick.h>
  32. #include <linux/err.h>
  33. #include <linux/debugobjects.h>
  34. #include <linux/sched/signal.h>
  35. #include <linux/sched/sysctl.h>
  36. #include <linux/sched/rt.h>
  37. #include <linux/sched/deadline.h>
  38. #include <linux/sched/nohz.h>
  39. #include <linux/sched/debug.h>
  40. #include <linux/sched/isolation.h>
  41. #include <linux/timer.h>
  42. #include <linux/freezer.h>
  43. #include <linux/compat.h>
  44. #include <linux/uaccess.h>
  45. #include <trace/events/timer.h>
  46. #include "tick-internal.h"
  47. /*
  48. * Masks for selecting the soft and hard context timers from
  49. * cpu_base->active
  50. */
  51. #define MASK_SHIFT (HRTIMER_BASE_MONOTONIC_SOFT)
  52. #define HRTIMER_ACTIVE_HARD ((1U << MASK_SHIFT) - 1)
  53. #define HRTIMER_ACTIVE_SOFT (HRTIMER_ACTIVE_HARD << MASK_SHIFT)
  54. #define HRTIMER_ACTIVE_ALL (HRTIMER_ACTIVE_SOFT | HRTIMER_ACTIVE_HARD)
  55. static void retrigger_next_event(void *arg);
  56. /*
  57. * The timer bases:
  58. *
  59. * There are more clockids than hrtimer bases. Thus, we index
  60. * into the timer bases by the hrtimer_base_type enum. When trying
  61. * to reach a base using a clockid, hrtimer_clockid_to_base()
  62. * is used to convert from clockid to the proper hrtimer_base_type.
  63. */
  64. DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
  65. {
  66. .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
  67. .clock_base =
  68. {
  69. {
  70. .index = HRTIMER_BASE_MONOTONIC,
  71. .clockid = CLOCK_MONOTONIC,
  72. .get_time = &ktime_get,
  73. },
  74. {
  75. .index = HRTIMER_BASE_REALTIME,
  76. .clockid = CLOCK_REALTIME,
  77. .get_time = &ktime_get_real,
  78. },
  79. {
  80. .index = HRTIMER_BASE_BOOTTIME,
  81. .clockid = CLOCK_BOOTTIME,
  82. .get_time = &ktime_get_boottime,
  83. },
  84. {
  85. .index = HRTIMER_BASE_TAI,
  86. .clockid = CLOCK_TAI,
  87. .get_time = &ktime_get_clocktai,
  88. },
  89. {
  90. .index = HRTIMER_BASE_MONOTONIC_SOFT,
  91. .clockid = CLOCK_MONOTONIC,
  92. .get_time = &ktime_get,
  93. },
  94. {
  95. .index = HRTIMER_BASE_REALTIME_SOFT,
  96. .clockid = CLOCK_REALTIME,
  97. .get_time = &ktime_get_real,
  98. },
  99. {
  100. .index = HRTIMER_BASE_BOOTTIME_SOFT,
  101. .clockid = CLOCK_BOOTTIME,
  102. .get_time = &ktime_get_boottime,
  103. },
  104. {
  105. .index = HRTIMER_BASE_TAI_SOFT,
  106. .clockid = CLOCK_TAI,
  107. .get_time = &ktime_get_clocktai,
  108. },
  109. },
  110. .csd = CSD_INIT(retrigger_next_event, NULL)
  111. };
  112. static inline bool hrtimer_base_is_online(struct hrtimer_cpu_base *base)
  113. {
  114. if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
  115. return true;
  116. else
  117. return likely(base->online);
  118. }
  119. /*
  120. * Functions and macros which are different for UP/SMP systems are kept in a
  121. * single place
  122. */
  123. #ifdef CONFIG_SMP
  124. /*
  125. * We require the migration_base for lock_hrtimer_base()/switch_hrtimer_base()
  126. * such that hrtimer_callback_running() can unconditionally dereference
  127. * timer->base->cpu_base
  128. */
  129. static struct hrtimer_cpu_base migration_cpu_base = {
  130. .clock_base = { {
  131. .cpu_base = &migration_cpu_base,
  132. .seq = SEQCNT_RAW_SPINLOCK_ZERO(migration_cpu_base.seq,
  133. &migration_cpu_base.lock),
  134. }, },
  135. };
  136. #define migration_base migration_cpu_base.clock_base[0]
  137. /*
  138. * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
  139. * means that all timers which are tied to this base via timer->base are
  140. * locked, and the base itself is locked too.
  141. *
  142. * So __run_timers/migrate_timers can safely modify all timers which could
  143. * be found on the lists/queues.
  144. *
  145. * When the timer's base is locked, and the timer removed from list, it is
  146. * possible to set timer->base = &migration_base and drop the lock: the timer
  147. * remains locked.
  148. */
  149. static
  150. struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
  151. unsigned long *flags)
  152. __acquires(&timer->base->lock)
  153. {
  154. struct hrtimer_clock_base *base;
  155. for (;;) {
  156. base = READ_ONCE(timer->base);
  157. if (likely(base != &migration_base)) {
  158. raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
  159. if (likely(base == timer->base))
  160. return base;
  161. /* The timer has migrated to another CPU: */
  162. raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
  163. }
  164. cpu_relax();
  165. }
  166. }
  167. /*
  168. * Check if the elected target is suitable considering its next
  169. * event and the hotplug state of the current CPU.
  170. *
  171. * If the elected target is remote and its next event is after the timer
  172. * to queue, then a remote reprogram is necessary. However there is no
  173. * guarantee the IPI handling the operation would arrive in time to meet
  174. * the high resolution deadline. In this case the local CPU becomes a
  175. * preferred target, unless it is offline.
  176. *
  177. * High and low resolution modes are handled the same way for simplicity.
  178. *
  179. * Called with cpu_base->lock of target cpu held.
  180. */
  181. static bool hrtimer_suitable_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base,
  182. struct hrtimer_cpu_base *new_cpu_base,
  183. struct hrtimer_cpu_base *this_cpu_base)
  184. {
  185. ktime_t expires;
  186. /*
  187. * The local CPU clockevent can be reprogrammed. Also get_target_base()
  188. * guarantees it is online.
  189. */
  190. if (new_cpu_base == this_cpu_base)
  191. return true;
  192. /*
  193. * The offline local CPU can't be the default target if the
  194. * next remote target event is after this timer. Keep the
  195. * elected new base. An IPI will we issued to reprogram
  196. * it as a last resort.
  197. */
  198. if (!hrtimer_base_is_online(this_cpu_base))
  199. return true;
  200. expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
  201. return expires >= new_base->cpu_base->expires_next;
  202. }
  203. static inline struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base, int pinned)
  204. {
  205. if (!hrtimer_base_is_online(base)) {
  206. int cpu = cpumask_any_and(cpu_online_mask, housekeeping_cpumask(HK_TYPE_TIMER));
  207. return &per_cpu(hrtimer_bases, cpu);
  208. }
  209. #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
  210. if (static_branch_likely(&timers_migration_enabled) && !pinned)
  211. return &per_cpu(hrtimer_bases, get_nohz_timer_target());
  212. #endif
  213. return base;
  214. }
  215. /*
  216. * We switch the timer base to a power-optimized selected CPU target,
  217. * if:
  218. * - NO_HZ_COMMON is enabled
  219. * - timer migration is enabled
  220. * - the timer callback is not running
  221. * - the timer is not the first expiring timer on the new target
  222. *
  223. * If one of the above requirements is not fulfilled we move the timer
  224. * to the current CPU or leave it on the previously assigned CPU if
  225. * the timer callback is currently running.
  226. */
  227. static inline struct hrtimer_clock_base *
  228. switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
  229. int pinned)
  230. {
  231. struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base;
  232. struct hrtimer_clock_base *new_base;
  233. int basenum = base->index;
  234. this_cpu_base = this_cpu_ptr(&hrtimer_bases);
  235. new_cpu_base = get_target_base(this_cpu_base, pinned);
  236. again:
  237. new_base = &new_cpu_base->clock_base[basenum];
  238. if (base != new_base) {
  239. /*
  240. * We are trying to move timer to new_base.
  241. * However we can't change timer's base while it is running,
  242. * so we keep it on the same CPU. No hassle vs. reprogramming
  243. * the event source in the high resolution case. The softirq
  244. * code will take care of this when the timer function has
  245. * completed. There is no conflict as we hold the lock until
  246. * the timer is enqueued.
  247. */
  248. if (unlikely(hrtimer_callback_running(timer)))
  249. return base;
  250. /* See the comment in lock_hrtimer_base() */
  251. WRITE_ONCE(timer->base, &migration_base);
  252. raw_spin_unlock(&base->cpu_base->lock);
  253. raw_spin_lock(&new_base->cpu_base->lock);
  254. if (!hrtimer_suitable_target(timer, new_base, new_cpu_base,
  255. this_cpu_base)) {
  256. raw_spin_unlock(&new_base->cpu_base->lock);
  257. raw_spin_lock(&base->cpu_base->lock);
  258. new_cpu_base = this_cpu_base;
  259. WRITE_ONCE(timer->base, base);
  260. goto again;
  261. }
  262. WRITE_ONCE(timer->base, new_base);
  263. } else {
  264. if (!hrtimer_suitable_target(timer, new_base, new_cpu_base, this_cpu_base)) {
  265. new_cpu_base = this_cpu_base;
  266. goto again;
  267. }
  268. }
  269. return new_base;
  270. }
  271. #else /* CONFIG_SMP */
  272. static inline struct hrtimer_clock_base *
  273. lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  274. __acquires(&timer->base->cpu_base->lock)
  275. {
  276. struct hrtimer_clock_base *base = timer->base;
  277. raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
  278. return base;
  279. }
  280. # define switch_hrtimer_base(t, b, p) (b)
  281. #endif /* !CONFIG_SMP */
  282. /*
  283. * Functions for the union type storage format of ktime_t which are
  284. * too large for inlining:
  285. */
  286. #if BITS_PER_LONG < 64
  287. /*
  288. * Divide a ktime value by a nanosecond value
  289. */
  290. s64 __ktime_divns(const ktime_t kt, s64 div)
  291. {
  292. int sft = 0;
  293. s64 dclc;
  294. u64 tmp;
  295. dclc = ktime_to_ns(kt);
  296. tmp = dclc < 0 ? -dclc : dclc;
  297. /* Make sure the divisor is less than 2^32: */
  298. while (div >> 32) {
  299. sft++;
  300. div >>= 1;
  301. }
  302. tmp >>= sft;
  303. do_div(tmp, (u32) div);
  304. return dclc < 0 ? -tmp : tmp;
  305. }
  306. EXPORT_SYMBOL_GPL(__ktime_divns);
  307. #endif /* BITS_PER_LONG >= 64 */
  308. /*
  309. * Add two ktime values and do a safety check for overflow:
  310. */
  311. ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
  312. {
  313. ktime_t res = ktime_add_unsafe(lhs, rhs);
  314. /*
  315. * We use KTIME_SEC_MAX here, the maximum timeout which we can
  316. * return to user space in a timespec:
  317. */
  318. if (res < 0 || res < lhs || res < rhs)
  319. res = ktime_set(KTIME_SEC_MAX, 0);
  320. return res;
  321. }
  322. EXPORT_SYMBOL_GPL(ktime_add_safe);
  323. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  324. static const struct debug_obj_descr hrtimer_debug_descr;
  325. static void *hrtimer_debug_hint(void *addr)
  326. {
  327. return ((struct hrtimer *) addr)->function;
  328. }
  329. /*
  330. * fixup_init is called when:
  331. * - an active object is initialized
  332. */
  333. static bool hrtimer_fixup_init(void *addr, enum debug_obj_state state)
  334. {
  335. struct hrtimer *timer = addr;
  336. switch (state) {
  337. case ODEBUG_STATE_ACTIVE:
  338. hrtimer_cancel(timer);
  339. debug_object_init(timer, &hrtimer_debug_descr);
  340. return true;
  341. default:
  342. return false;
  343. }
  344. }
  345. /*
  346. * fixup_activate is called when:
  347. * - an active object is activated
  348. * - an unknown non-static object is activated
  349. */
  350. static bool hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
  351. {
  352. switch (state) {
  353. case ODEBUG_STATE_ACTIVE:
  354. WARN_ON(1);
  355. fallthrough;
  356. default:
  357. return false;
  358. }
  359. }
  360. /*
  361. * fixup_free is called when:
  362. * - an active object is freed
  363. */
  364. static bool hrtimer_fixup_free(void *addr, enum debug_obj_state state)
  365. {
  366. struct hrtimer *timer = addr;
  367. switch (state) {
  368. case ODEBUG_STATE_ACTIVE:
  369. hrtimer_cancel(timer);
  370. debug_object_free(timer, &hrtimer_debug_descr);
  371. return true;
  372. default:
  373. return false;
  374. }
  375. }
  376. static const struct debug_obj_descr hrtimer_debug_descr = {
  377. .name = "hrtimer",
  378. .debug_hint = hrtimer_debug_hint,
  379. .fixup_init = hrtimer_fixup_init,
  380. .fixup_activate = hrtimer_fixup_activate,
  381. .fixup_free = hrtimer_fixup_free,
  382. };
  383. static inline void debug_hrtimer_init(struct hrtimer *timer)
  384. {
  385. debug_object_init(timer, &hrtimer_debug_descr);
  386. }
  387. static inline void debug_hrtimer_activate(struct hrtimer *timer,
  388. enum hrtimer_mode mode)
  389. {
  390. debug_object_activate(timer, &hrtimer_debug_descr);
  391. }
  392. static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
  393. {
  394. debug_object_deactivate(timer, &hrtimer_debug_descr);
  395. }
  396. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  397. enum hrtimer_mode mode);
  398. void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
  399. enum hrtimer_mode mode)
  400. {
  401. debug_object_init_on_stack(timer, &hrtimer_debug_descr);
  402. __hrtimer_init(timer, clock_id, mode);
  403. }
  404. EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
  405. static void __hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
  406. clockid_t clock_id, enum hrtimer_mode mode);
  407. void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
  408. clockid_t clock_id, enum hrtimer_mode mode)
  409. {
  410. debug_object_init_on_stack(&sl->timer, &hrtimer_debug_descr);
  411. __hrtimer_init_sleeper(sl, clock_id, mode);
  412. }
  413. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper_on_stack);
  414. void destroy_hrtimer_on_stack(struct hrtimer *timer)
  415. {
  416. debug_object_free(timer, &hrtimer_debug_descr);
  417. }
  418. EXPORT_SYMBOL_GPL(destroy_hrtimer_on_stack);
  419. #else
  420. static inline void debug_hrtimer_init(struct hrtimer *timer) { }
  421. static inline void debug_hrtimer_activate(struct hrtimer *timer,
  422. enum hrtimer_mode mode) { }
  423. static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
  424. #endif
  425. static inline void
  426. debug_init(struct hrtimer *timer, clockid_t clockid,
  427. enum hrtimer_mode mode)
  428. {
  429. debug_hrtimer_init(timer);
  430. trace_hrtimer_init(timer, clockid, mode);
  431. }
  432. static inline void debug_activate(struct hrtimer *timer,
  433. enum hrtimer_mode mode)
  434. {
  435. debug_hrtimer_activate(timer, mode);
  436. trace_hrtimer_start(timer, mode);
  437. }
  438. static inline void debug_deactivate(struct hrtimer *timer)
  439. {
  440. debug_hrtimer_deactivate(timer);
  441. trace_hrtimer_cancel(timer);
  442. }
  443. static struct hrtimer_clock_base *
  444. __next_base(struct hrtimer_cpu_base *cpu_base, unsigned int *active)
  445. {
  446. unsigned int idx;
  447. if (!*active)
  448. return NULL;
  449. idx = __ffs(*active);
  450. *active &= ~(1U << idx);
  451. return &cpu_base->clock_base[idx];
  452. }
  453. #define for_each_active_base(base, cpu_base, active) \
  454. while ((base = __next_base((cpu_base), &(active))))
  455. static ktime_t __hrtimer_next_event_base(struct hrtimer_cpu_base *cpu_base,
  456. const struct hrtimer *exclude,
  457. unsigned int active,
  458. ktime_t expires_next)
  459. {
  460. struct hrtimer_clock_base *base;
  461. ktime_t expires;
  462. for_each_active_base(base, cpu_base, active) {
  463. struct timerqueue_node *next;
  464. struct hrtimer *timer;
  465. next = timerqueue_getnext(&base->active);
  466. timer = container_of(next, struct hrtimer, node);
  467. if (timer == exclude) {
  468. /* Get to the next timer in the queue. */
  469. next = timerqueue_iterate_next(next);
  470. if (!next)
  471. continue;
  472. timer = container_of(next, struct hrtimer, node);
  473. }
  474. expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  475. if (expires < expires_next) {
  476. expires_next = expires;
  477. /* Skip cpu_base update if a timer is being excluded. */
  478. if (exclude)
  479. continue;
  480. if (timer->is_soft)
  481. cpu_base->softirq_next_timer = timer;
  482. else
  483. cpu_base->next_timer = timer;
  484. }
  485. }
  486. /*
  487. * clock_was_set() might have changed base->offset of any of
  488. * the clock bases so the result might be negative. Fix it up
  489. * to prevent a false positive in clockevents_program_event().
  490. */
  491. if (expires_next < 0)
  492. expires_next = 0;
  493. return expires_next;
  494. }
  495. /*
  496. * Recomputes cpu_base::*next_timer and returns the earliest expires_next
  497. * but does not set cpu_base::*expires_next, that is done by
  498. * hrtimer[_force]_reprogram and hrtimer_interrupt only. When updating
  499. * cpu_base::*expires_next right away, reprogramming logic would no longer
  500. * work.
  501. *
  502. * When a softirq is pending, we can ignore the HRTIMER_ACTIVE_SOFT bases,
  503. * those timers will get run whenever the softirq gets handled, at the end of
  504. * hrtimer_run_softirq(), hrtimer_update_softirq_timer() will re-add these bases.
  505. *
  506. * Therefore softirq values are those from the HRTIMER_ACTIVE_SOFT clock bases.
  507. * The !softirq values are the minima across HRTIMER_ACTIVE_ALL, unless an actual
  508. * softirq is pending, in which case they're the minima of HRTIMER_ACTIVE_HARD.
  509. *
  510. * @active_mask must be one of:
  511. * - HRTIMER_ACTIVE_ALL,
  512. * - HRTIMER_ACTIVE_SOFT, or
  513. * - HRTIMER_ACTIVE_HARD.
  514. */
  515. static ktime_t
  516. __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base, unsigned int active_mask)
  517. {
  518. unsigned int active;
  519. struct hrtimer *next_timer = NULL;
  520. ktime_t expires_next = KTIME_MAX;
  521. if (!cpu_base->softirq_activated && (active_mask & HRTIMER_ACTIVE_SOFT)) {
  522. active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
  523. cpu_base->softirq_next_timer = NULL;
  524. expires_next = __hrtimer_next_event_base(cpu_base, NULL,
  525. active, KTIME_MAX);
  526. next_timer = cpu_base->softirq_next_timer;
  527. }
  528. if (active_mask & HRTIMER_ACTIVE_HARD) {
  529. active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
  530. cpu_base->next_timer = next_timer;
  531. expires_next = __hrtimer_next_event_base(cpu_base, NULL, active,
  532. expires_next);
  533. }
  534. return expires_next;
  535. }
  536. static ktime_t hrtimer_update_next_event(struct hrtimer_cpu_base *cpu_base)
  537. {
  538. ktime_t expires_next, soft = KTIME_MAX;
  539. /*
  540. * If the soft interrupt has already been activated, ignore the
  541. * soft bases. They will be handled in the already raised soft
  542. * interrupt.
  543. */
  544. if (!cpu_base->softirq_activated) {
  545. soft = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
  546. /*
  547. * Update the soft expiry time. clock_settime() might have
  548. * affected it.
  549. */
  550. cpu_base->softirq_expires_next = soft;
  551. }
  552. expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_HARD);
  553. /*
  554. * If a softirq timer is expiring first, update cpu_base->next_timer
  555. * and program the hardware with the soft expiry time.
  556. */
  557. if (expires_next > soft) {
  558. cpu_base->next_timer = cpu_base->softirq_next_timer;
  559. expires_next = soft;
  560. }
  561. return expires_next;
  562. }
  563. static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
  564. {
  565. ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
  566. ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
  567. ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
  568. ktime_t now = ktime_get_update_offsets_now(&base->clock_was_set_seq,
  569. offs_real, offs_boot, offs_tai);
  570. base->clock_base[HRTIMER_BASE_REALTIME_SOFT].offset = *offs_real;
  571. base->clock_base[HRTIMER_BASE_BOOTTIME_SOFT].offset = *offs_boot;
  572. base->clock_base[HRTIMER_BASE_TAI_SOFT].offset = *offs_tai;
  573. return now;
  574. }
  575. /*
  576. * Is the high resolution mode active ?
  577. */
  578. static inline int hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
  579. {
  580. return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ?
  581. cpu_base->hres_active : 0;
  582. }
  583. static void __hrtimer_reprogram(struct hrtimer_cpu_base *cpu_base,
  584. struct hrtimer *next_timer,
  585. ktime_t expires_next)
  586. {
  587. cpu_base->expires_next = expires_next;
  588. /*
  589. * If hres is not active, hardware does not have to be
  590. * reprogrammed yet.
  591. *
  592. * If a hang was detected in the last timer interrupt then we
  593. * leave the hang delay active in the hardware. We want the
  594. * system to make progress. That also prevents the following
  595. * scenario:
  596. * T1 expires 50ms from now
  597. * T2 expires 5s from now
  598. *
  599. * T1 is removed, so this code is called and would reprogram
  600. * the hardware to 5s from now. Any hrtimer_start after that
  601. * will not reprogram the hardware due to hang_detected being
  602. * set. So we'd effectively block all timers until the T2 event
  603. * fires.
  604. */
  605. if (!hrtimer_hres_active(cpu_base) || cpu_base->hang_detected)
  606. return;
  607. tick_program_event(expires_next, 1);
  608. }
  609. /*
  610. * Reprogram the event source with checking both queues for the
  611. * next event
  612. * Called with interrupts disabled and base->lock held
  613. */
  614. static void
  615. hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
  616. {
  617. ktime_t expires_next;
  618. expires_next = hrtimer_update_next_event(cpu_base);
  619. if (skip_equal && expires_next == cpu_base->expires_next)
  620. return;
  621. __hrtimer_reprogram(cpu_base, cpu_base->next_timer, expires_next);
  622. }
  623. /* High resolution timer related functions */
  624. #ifdef CONFIG_HIGH_RES_TIMERS
  625. /*
  626. * High resolution timer enabled ?
  627. */
  628. static bool hrtimer_hres_enabled __read_mostly = true;
  629. unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC;
  630. EXPORT_SYMBOL_GPL(hrtimer_resolution);
  631. /*
  632. * Enable / Disable high resolution mode
  633. */
  634. static int __init setup_hrtimer_hres(char *str)
  635. {
  636. return (kstrtobool(str, &hrtimer_hres_enabled) == 0);
  637. }
  638. __setup("highres=", setup_hrtimer_hres);
  639. /*
  640. * hrtimer_high_res_enabled - query, if the highres mode is enabled
  641. */
  642. static inline int hrtimer_is_hres_enabled(void)
  643. {
  644. return hrtimer_hres_enabled;
  645. }
  646. /*
  647. * Switch to high resolution mode
  648. */
  649. static void hrtimer_switch_to_hres(void)
  650. {
  651. struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
  652. if (tick_init_highres()) {
  653. pr_warn("Could not switch to high resolution mode on CPU %u\n",
  654. base->cpu);
  655. return;
  656. }
  657. base->hres_active = 1;
  658. hrtimer_resolution = HIGH_RES_NSEC;
  659. tick_setup_sched_timer(true);
  660. /* "Retrigger" the interrupt to get things going */
  661. retrigger_next_event(NULL);
  662. }
  663. #else
  664. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  665. static inline void hrtimer_switch_to_hres(void) { }
  666. #endif /* CONFIG_HIGH_RES_TIMERS */
  667. /*
  668. * Retrigger next event is called after clock was set with interrupts
  669. * disabled through an SMP function call or directly from low level
  670. * resume code.
  671. *
  672. * This is only invoked when:
  673. * - CONFIG_HIGH_RES_TIMERS is enabled.
  674. * - CONFIG_NOHZ_COMMON is enabled
  675. *
  676. * For the other cases this function is empty and because the call sites
  677. * are optimized out it vanishes as well, i.e. no need for lots of
  678. * #ifdeffery.
  679. */
  680. static void retrigger_next_event(void *arg)
  681. {
  682. struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
  683. /*
  684. * When high resolution mode or nohz is active, then the offsets of
  685. * CLOCK_REALTIME/TAI/BOOTTIME have to be updated. Otherwise the
  686. * next tick will take care of that.
  687. *
  688. * If high resolution mode is active then the next expiring timer
  689. * must be reevaluated and the clock event device reprogrammed if
  690. * necessary.
  691. *
  692. * In the NOHZ case the update of the offset and the reevaluation
  693. * of the next expiring timer is enough. The return from the SMP
  694. * function call will take care of the reprogramming in case the
  695. * CPU was in a NOHZ idle sleep.
  696. *
  697. * In periodic low resolution mode, the next softirq expiration
  698. * must also be updated.
  699. */
  700. raw_spin_lock(&base->lock);
  701. hrtimer_update_base(base);
  702. if (hrtimer_hres_active(base))
  703. hrtimer_force_reprogram(base, 0);
  704. else
  705. hrtimer_update_next_event(base);
  706. raw_spin_unlock(&base->lock);
  707. }
  708. /*
  709. * When a timer is enqueued and expires earlier than the already enqueued
  710. * timers, we have to check, whether it expires earlier than the timer for
  711. * which the clock event device was armed.
  712. *
  713. * Called with interrupts disabled and base->cpu_base.lock held
  714. */
  715. static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
  716. {
  717. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  718. struct hrtimer_clock_base *base = timer->base;
  719. ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  720. WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
  721. /*
  722. * CLOCK_REALTIME timer might be requested with an absolute
  723. * expiry time which is less than base->offset. Set it to 0.
  724. */
  725. if (expires < 0)
  726. expires = 0;
  727. if (timer->is_soft) {
  728. /*
  729. * soft hrtimer could be started on a remote CPU. In this
  730. * case softirq_expires_next needs to be updated on the
  731. * remote CPU. The soft hrtimer will not expire before the
  732. * first hard hrtimer on the remote CPU -
  733. * hrtimer_check_target() prevents this case.
  734. */
  735. struct hrtimer_cpu_base *timer_cpu_base = base->cpu_base;
  736. if (timer_cpu_base->softirq_activated)
  737. return;
  738. if (!ktime_before(expires, timer_cpu_base->softirq_expires_next))
  739. return;
  740. timer_cpu_base->softirq_next_timer = timer;
  741. timer_cpu_base->softirq_expires_next = expires;
  742. if (!ktime_before(expires, timer_cpu_base->expires_next) ||
  743. !reprogram)
  744. return;
  745. }
  746. /*
  747. * If the timer is not on the current cpu, we cannot reprogram
  748. * the other cpus clock event device.
  749. */
  750. if (base->cpu_base != cpu_base)
  751. return;
  752. if (expires >= cpu_base->expires_next)
  753. return;
  754. /*
  755. * If the hrtimer interrupt is running, then it will reevaluate the
  756. * clock bases and reprogram the clock event device.
  757. */
  758. if (cpu_base->in_hrtirq)
  759. return;
  760. cpu_base->next_timer = timer;
  761. __hrtimer_reprogram(cpu_base, timer, expires);
  762. }
  763. static bool update_needs_ipi(struct hrtimer_cpu_base *cpu_base,
  764. unsigned int active)
  765. {
  766. struct hrtimer_clock_base *base;
  767. unsigned int seq;
  768. ktime_t expires;
  769. /*
  770. * Update the base offsets unconditionally so the following
  771. * checks whether the SMP function call is required works.
  772. *
  773. * The update is safe even when the remote CPU is in the hrtimer
  774. * interrupt or the hrtimer soft interrupt and expiring affected
  775. * bases. Either it will see the update before handling a base or
  776. * it will see it when it finishes the processing and reevaluates
  777. * the next expiring timer.
  778. */
  779. seq = cpu_base->clock_was_set_seq;
  780. hrtimer_update_base(cpu_base);
  781. /*
  782. * If the sequence did not change over the update then the
  783. * remote CPU already handled it.
  784. */
  785. if (seq == cpu_base->clock_was_set_seq)
  786. return false;
  787. /*
  788. * If the remote CPU is currently handling an hrtimer interrupt, it
  789. * will reevaluate the first expiring timer of all clock bases
  790. * before reprogramming. Nothing to do here.
  791. */
  792. if (cpu_base->in_hrtirq)
  793. return false;
  794. /*
  795. * Walk the affected clock bases and check whether the first expiring
  796. * timer in a clock base is moving ahead of the first expiring timer of
  797. * @cpu_base. If so, the IPI must be invoked because per CPU clock
  798. * event devices cannot be remotely reprogrammed.
  799. */
  800. active &= cpu_base->active_bases;
  801. for_each_active_base(base, cpu_base, active) {
  802. struct timerqueue_node *next;
  803. next = timerqueue_getnext(&base->active);
  804. expires = ktime_sub(next->expires, base->offset);
  805. if (expires < cpu_base->expires_next)
  806. return true;
  807. /* Extra check for softirq clock bases */
  808. if (base->clockid < HRTIMER_BASE_MONOTONIC_SOFT)
  809. continue;
  810. if (cpu_base->softirq_activated)
  811. continue;
  812. if (expires < cpu_base->softirq_expires_next)
  813. return true;
  814. }
  815. return false;
  816. }
  817. /*
  818. * Clock was set. This might affect CLOCK_REALTIME, CLOCK_TAI and
  819. * CLOCK_BOOTTIME (for late sleep time injection).
  820. *
  821. * This requires to update the offsets for these clocks
  822. * vs. CLOCK_MONOTONIC. When high resolution timers are enabled, then this
  823. * also requires to eventually reprogram the per CPU clock event devices
  824. * when the change moves an affected timer ahead of the first expiring
  825. * timer on that CPU. Obviously remote per CPU clock event devices cannot
  826. * be reprogrammed. The other reason why an IPI has to be sent is when the
  827. * system is in !HIGH_RES and NOHZ mode. The NOHZ mode updates the offsets
  828. * in the tick, which obviously might be stopped, so this has to bring out
  829. * the remote CPU which might sleep in idle to get this sorted.
  830. */
  831. void clock_was_set(unsigned int bases)
  832. {
  833. struct hrtimer_cpu_base *cpu_base = raw_cpu_ptr(&hrtimer_bases);
  834. cpumask_var_t mask;
  835. int cpu;
  836. if (!hrtimer_hres_active(cpu_base) && !tick_nohz_active)
  837. goto out_timerfd;
  838. if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
  839. on_each_cpu(retrigger_next_event, NULL, 1);
  840. goto out_timerfd;
  841. }
  842. /* Avoid interrupting CPUs if possible */
  843. cpus_read_lock();
  844. for_each_online_cpu(cpu) {
  845. unsigned long flags;
  846. cpu_base = &per_cpu(hrtimer_bases, cpu);
  847. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  848. if (update_needs_ipi(cpu_base, bases))
  849. cpumask_set_cpu(cpu, mask);
  850. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  851. }
  852. preempt_disable();
  853. smp_call_function_many(mask, retrigger_next_event, NULL, 1);
  854. preempt_enable();
  855. cpus_read_unlock();
  856. free_cpumask_var(mask);
  857. out_timerfd:
  858. timerfd_clock_was_set();
  859. }
  860. static void clock_was_set_work(struct work_struct *work)
  861. {
  862. clock_was_set(CLOCK_SET_WALL);
  863. }
  864. static DECLARE_WORK(hrtimer_work, clock_was_set_work);
  865. /*
  866. * Called from timekeeping code to reprogram the hrtimer interrupt device
  867. * on all cpus and to notify timerfd.
  868. */
  869. void clock_was_set_delayed(void)
  870. {
  871. schedule_work(&hrtimer_work);
  872. }
  873. /*
  874. * Called during resume either directly from via timekeeping_resume()
  875. * or in the case of s2idle from tick_unfreeze() to ensure that the
  876. * hrtimers are up to date.
  877. */
  878. void hrtimers_resume_local(void)
  879. {
  880. lockdep_assert_irqs_disabled();
  881. /* Retrigger on the local CPU */
  882. retrigger_next_event(NULL);
  883. }
  884. /*
  885. * Counterpart to lock_hrtimer_base above:
  886. */
  887. static inline
  888. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  889. __releases(&timer->base->cpu_base->lock)
  890. {
  891. raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  892. }
  893. /**
  894. * hrtimer_forward() - forward the timer expiry
  895. * @timer: hrtimer to forward
  896. * @now: forward past this time
  897. * @interval: the interval to forward
  898. *
  899. * Forward the timer expiry so it will expire in the future.
  900. *
  901. * .. note::
  902. * This only updates the timer expiry value and does not requeue the timer.
  903. *
  904. * There is also a variant of the function hrtimer_forward_now().
  905. *
  906. * Context: Can be safely called from the callback function of @timer. If called
  907. * from other contexts @timer must neither be enqueued nor running the
  908. * callback and the caller needs to take care of serialization.
  909. *
  910. * Return: The number of overruns are returned.
  911. */
  912. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  913. {
  914. u64 orun = 1;
  915. ktime_t delta;
  916. delta = ktime_sub(now, hrtimer_get_expires(timer));
  917. if (delta < 0)
  918. return 0;
  919. if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED))
  920. return 0;
  921. if (interval < hrtimer_resolution)
  922. interval = hrtimer_resolution;
  923. if (unlikely(delta >= interval)) {
  924. s64 incr = ktime_to_ns(interval);
  925. orun = ktime_divns(delta, incr);
  926. hrtimer_add_expires_ns(timer, incr * orun);
  927. if (hrtimer_get_expires_tv64(timer) > now)
  928. return orun;
  929. /*
  930. * This (and the ktime_add() below) is the
  931. * correction for exact:
  932. */
  933. orun++;
  934. }
  935. hrtimer_add_expires(timer, interval);
  936. return orun;
  937. }
  938. EXPORT_SYMBOL_GPL(hrtimer_forward);
  939. /*
  940. * enqueue_hrtimer - internal function to (re)start a timer
  941. *
  942. * The timer is inserted in expiry order. Insertion into the
  943. * red black tree is O(log(n)). Must hold the base lock.
  944. *
  945. * Returns 1 when the new timer is the leftmost timer in the tree.
  946. */
  947. static int enqueue_hrtimer(struct hrtimer *timer,
  948. struct hrtimer_clock_base *base,
  949. enum hrtimer_mode mode)
  950. {
  951. debug_activate(timer, mode);
  952. WARN_ON_ONCE(!base->cpu_base->online);
  953. base->cpu_base->active_bases |= 1 << base->index;
  954. /* Pairs with the lockless read in hrtimer_is_queued() */
  955. WRITE_ONCE(timer->state, HRTIMER_STATE_ENQUEUED);
  956. return timerqueue_add(&base->active, &timer->node);
  957. }
  958. /*
  959. * __remove_hrtimer - internal function to remove a timer
  960. *
  961. * Caller must hold the base lock.
  962. *
  963. * High resolution timer mode reprograms the clock event device when the
  964. * timer is the one which expires next. The caller can disable this by setting
  965. * reprogram to zero. This is useful, when the context does a reprogramming
  966. * anyway (e.g. timer interrupt)
  967. */
  968. static void __remove_hrtimer(struct hrtimer *timer,
  969. struct hrtimer_clock_base *base,
  970. u8 newstate, int reprogram)
  971. {
  972. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  973. u8 state = timer->state;
  974. /* Pairs with the lockless read in hrtimer_is_queued() */
  975. WRITE_ONCE(timer->state, newstate);
  976. if (!(state & HRTIMER_STATE_ENQUEUED))
  977. return;
  978. if (!timerqueue_del(&base->active, &timer->node))
  979. cpu_base->active_bases &= ~(1 << base->index);
  980. /*
  981. * Note: If reprogram is false we do not update
  982. * cpu_base->next_timer. This happens when we remove the first
  983. * timer on a remote cpu. No harm as we never dereference
  984. * cpu_base->next_timer. So the worst thing what can happen is
  985. * an superfluous call to hrtimer_force_reprogram() on the
  986. * remote cpu later on if the same timer gets enqueued again.
  987. */
  988. if (reprogram && timer == cpu_base->next_timer)
  989. hrtimer_force_reprogram(cpu_base, 1);
  990. }
  991. /*
  992. * remove hrtimer, called with base lock held
  993. */
  994. static inline int
  995. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base,
  996. bool restart, bool keep_local)
  997. {
  998. u8 state = timer->state;
  999. if (state & HRTIMER_STATE_ENQUEUED) {
  1000. bool reprogram;
  1001. /*
  1002. * Remove the timer and force reprogramming when high
  1003. * resolution mode is active and the timer is on the current
  1004. * CPU. If we remove a timer on another CPU, reprogramming is
  1005. * skipped. The interrupt event on this CPU is fired and
  1006. * reprogramming happens in the interrupt handler. This is a
  1007. * rare case and less expensive than a smp call.
  1008. */
  1009. debug_deactivate(timer);
  1010. reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
  1011. /*
  1012. * If the timer is not restarted then reprogramming is
  1013. * required if the timer is local. If it is local and about
  1014. * to be restarted, avoid programming it twice (on removal
  1015. * and a moment later when it's requeued).
  1016. */
  1017. if (!restart)
  1018. state = HRTIMER_STATE_INACTIVE;
  1019. else
  1020. reprogram &= !keep_local;
  1021. __remove_hrtimer(timer, base, state, reprogram);
  1022. return 1;
  1023. }
  1024. return 0;
  1025. }
  1026. static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim,
  1027. const enum hrtimer_mode mode)
  1028. {
  1029. #ifdef CONFIG_TIME_LOW_RES
  1030. /*
  1031. * CONFIG_TIME_LOW_RES indicates that the system has no way to return
  1032. * granular time values. For relative timers we add hrtimer_resolution
  1033. * (i.e. one jiffy) to prevent short timeouts.
  1034. */
  1035. timer->is_rel = mode & HRTIMER_MODE_REL;
  1036. if (timer->is_rel)
  1037. tim = ktime_add_safe(tim, hrtimer_resolution);
  1038. #endif
  1039. return tim;
  1040. }
  1041. static void
  1042. hrtimer_update_softirq_timer(struct hrtimer_cpu_base *cpu_base, bool reprogram)
  1043. {
  1044. ktime_t expires;
  1045. /*
  1046. * Find the next SOFT expiration.
  1047. */
  1048. expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
  1049. /*
  1050. * reprogramming needs to be triggered, even if the next soft
  1051. * hrtimer expires at the same time than the next hard
  1052. * hrtimer. cpu_base->softirq_expires_next needs to be updated!
  1053. */
  1054. if (expires == KTIME_MAX)
  1055. return;
  1056. /*
  1057. * cpu_base->*next_timer is recomputed by __hrtimer_get_next_event()
  1058. * cpu_base->*expires_next is only set by hrtimer_reprogram()
  1059. */
  1060. hrtimer_reprogram(cpu_base->softirq_next_timer, reprogram);
  1061. }
  1062. static int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  1063. u64 delta_ns, const enum hrtimer_mode mode,
  1064. struct hrtimer_clock_base *base)
  1065. {
  1066. struct hrtimer_cpu_base *this_cpu_base = this_cpu_ptr(&hrtimer_bases);
  1067. struct hrtimer_clock_base *new_base;
  1068. bool force_local, first;
  1069. /*
  1070. * If the timer is on the local cpu base and is the first expiring
  1071. * timer then this might end up reprogramming the hardware twice
  1072. * (on removal and on enqueue). To avoid that by prevent the
  1073. * reprogram on removal, keep the timer local to the current CPU
  1074. * and enforce reprogramming after it is queued no matter whether
  1075. * it is the new first expiring timer again or not.
  1076. */
  1077. force_local = base->cpu_base == this_cpu_base;
  1078. force_local &= base->cpu_base->next_timer == timer;
  1079. /*
  1080. * Don't force local queuing if this enqueue happens on a unplugged
  1081. * CPU after hrtimer_cpu_dying() has been invoked.
  1082. */
  1083. force_local &= this_cpu_base->online;
  1084. /*
  1085. * Remove an active timer from the queue. In case it is not queued
  1086. * on the current CPU, make sure that remove_hrtimer() updates the
  1087. * remote data correctly.
  1088. *
  1089. * If it's on the current CPU and the first expiring timer, then
  1090. * skip reprogramming, keep the timer local and enforce
  1091. * reprogramming later if it was the first expiring timer. This
  1092. * avoids programming the underlying clock event twice (once at
  1093. * removal and once after enqueue).
  1094. */
  1095. remove_hrtimer(timer, base, true, force_local);
  1096. if (mode & HRTIMER_MODE_REL)
  1097. tim = ktime_add_safe(tim, base->get_time());
  1098. tim = hrtimer_update_lowres(timer, tim, mode);
  1099. hrtimer_set_expires_range_ns(timer, tim, delta_ns);
  1100. /* Switch the timer base, if necessary: */
  1101. if (!force_local) {
  1102. new_base = switch_hrtimer_base(timer, base,
  1103. mode & HRTIMER_MODE_PINNED);
  1104. } else {
  1105. new_base = base;
  1106. }
  1107. first = enqueue_hrtimer(timer, new_base, mode);
  1108. if (!force_local) {
  1109. /*
  1110. * If the current CPU base is online, then the timer is
  1111. * never queued on a remote CPU if it would be the first
  1112. * expiring timer there.
  1113. */
  1114. if (hrtimer_base_is_online(this_cpu_base))
  1115. return first;
  1116. /*
  1117. * Timer was enqueued remote because the current base is
  1118. * already offline. If the timer is the first to expire,
  1119. * kick the remote CPU to reprogram the clock event.
  1120. */
  1121. if (first) {
  1122. struct hrtimer_cpu_base *new_cpu_base = new_base->cpu_base;
  1123. smp_call_function_single_async(new_cpu_base->cpu, &new_cpu_base->csd);
  1124. }
  1125. return 0;
  1126. }
  1127. /*
  1128. * Timer was forced to stay on the current CPU to avoid
  1129. * reprogramming on removal and enqueue. Force reprogram the
  1130. * hardware by evaluating the new first expiring timer.
  1131. */
  1132. hrtimer_force_reprogram(new_base->cpu_base, 1);
  1133. return 0;
  1134. }
  1135. /**
  1136. * hrtimer_start_range_ns - (re)start an hrtimer
  1137. * @timer: the timer to be added
  1138. * @tim: expiry time
  1139. * @delta_ns: "slack" range for the timer
  1140. * @mode: timer mode: absolute (HRTIMER_MODE_ABS) or
  1141. * relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
  1142. * softirq based mode is considered for debug purpose only!
  1143. */
  1144. void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  1145. u64 delta_ns, const enum hrtimer_mode mode)
  1146. {
  1147. struct hrtimer_clock_base *base;
  1148. unsigned long flags;
  1149. if (WARN_ON_ONCE(!timer->function))
  1150. return;
  1151. /*
  1152. * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft
  1153. * match on CONFIG_PREEMPT_RT = n. With PREEMPT_RT check the hard
  1154. * expiry mode because unmarked timers are moved to softirq expiry.
  1155. */
  1156. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  1157. WARN_ON_ONCE(!(mode & HRTIMER_MODE_SOFT) ^ !timer->is_soft);
  1158. else
  1159. WARN_ON_ONCE(!(mode & HRTIMER_MODE_HARD) ^ !timer->is_hard);
  1160. base = lock_hrtimer_base(timer, &flags);
  1161. if (__hrtimer_start_range_ns(timer, tim, delta_ns, mode, base))
  1162. hrtimer_reprogram(timer, true);
  1163. unlock_hrtimer_base(timer, &flags);
  1164. }
  1165. EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
  1166. /**
  1167. * hrtimer_try_to_cancel - try to deactivate a timer
  1168. * @timer: hrtimer to stop
  1169. *
  1170. * Returns:
  1171. *
  1172. * * 0 when the timer was not active
  1173. * * 1 when the timer was active
  1174. * * -1 when the timer is currently executing the callback function and
  1175. * cannot be stopped
  1176. */
  1177. int hrtimer_try_to_cancel(struct hrtimer *timer)
  1178. {
  1179. struct hrtimer_clock_base *base;
  1180. unsigned long flags;
  1181. int ret = -1;
  1182. /*
  1183. * Check lockless first. If the timer is not active (neither
  1184. * enqueued nor running the callback, nothing to do here. The
  1185. * base lock does not serialize against a concurrent enqueue,
  1186. * so we can avoid taking it.
  1187. */
  1188. if (!hrtimer_active(timer))
  1189. return 0;
  1190. base = lock_hrtimer_base(timer, &flags);
  1191. if (!hrtimer_callback_running(timer))
  1192. ret = remove_hrtimer(timer, base, false, false);
  1193. unlock_hrtimer_base(timer, &flags);
  1194. return ret;
  1195. }
  1196. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  1197. #ifdef CONFIG_PREEMPT_RT
  1198. static void hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base)
  1199. {
  1200. spin_lock_init(&base->softirq_expiry_lock);
  1201. }
  1202. static void hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base)
  1203. __acquires(&base->softirq_expiry_lock)
  1204. {
  1205. spin_lock(&base->softirq_expiry_lock);
  1206. }
  1207. static void hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base)
  1208. __releases(&base->softirq_expiry_lock)
  1209. {
  1210. spin_unlock(&base->softirq_expiry_lock);
  1211. }
  1212. /*
  1213. * The counterpart to hrtimer_cancel_wait_running().
  1214. *
  1215. * If there is a waiter for cpu_base->expiry_lock, then it was waiting for
  1216. * the timer callback to finish. Drop expiry_lock and reacquire it. That
  1217. * allows the waiter to acquire the lock and make progress.
  1218. */
  1219. static void hrtimer_sync_wait_running(struct hrtimer_cpu_base *cpu_base,
  1220. unsigned long flags)
  1221. {
  1222. if (atomic_read(&cpu_base->timer_waiters)) {
  1223. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1224. spin_unlock(&cpu_base->softirq_expiry_lock);
  1225. spin_lock(&cpu_base->softirq_expiry_lock);
  1226. raw_spin_lock_irq(&cpu_base->lock);
  1227. }
  1228. }
  1229. #ifdef CONFIG_SMP
  1230. static __always_inline bool is_migration_base(struct hrtimer_clock_base *base)
  1231. {
  1232. return base == &migration_base;
  1233. }
  1234. #else
  1235. static __always_inline bool is_migration_base(struct hrtimer_clock_base *base)
  1236. {
  1237. return false;
  1238. }
  1239. #endif
  1240. /*
  1241. * This function is called on PREEMPT_RT kernels when the fast path
  1242. * deletion of a timer failed because the timer callback function was
  1243. * running.
  1244. *
  1245. * This prevents priority inversion: if the soft irq thread is preempted
  1246. * in the middle of a timer callback, then calling del_timer_sync() can
  1247. * lead to two issues:
  1248. *
  1249. * - If the caller is on a remote CPU then it has to spin wait for the timer
  1250. * handler to complete. This can result in unbound priority inversion.
  1251. *
  1252. * - If the caller originates from the task which preempted the timer
  1253. * handler on the same CPU, then spin waiting for the timer handler to
  1254. * complete is never going to end.
  1255. */
  1256. void hrtimer_cancel_wait_running(const struct hrtimer *timer)
  1257. {
  1258. /* Lockless read. Prevent the compiler from reloading it below */
  1259. struct hrtimer_clock_base *base = READ_ONCE(timer->base);
  1260. /*
  1261. * Just relax if the timer expires in hard interrupt context or if
  1262. * it is currently on the migration base.
  1263. */
  1264. if (!timer->is_soft || is_migration_base(base)) {
  1265. cpu_relax();
  1266. return;
  1267. }
  1268. /*
  1269. * Mark the base as contended and grab the expiry lock, which is
  1270. * held by the softirq across the timer callback. Drop the lock
  1271. * immediately so the softirq can expire the next timer. In theory
  1272. * the timer could already be running again, but that's more than
  1273. * unlikely and just causes another wait loop.
  1274. */
  1275. atomic_inc(&base->cpu_base->timer_waiters);
  1276. spin_lock_bh(&base->cpu_base->softirq_expiry_lock);
  1277. atomic_dec(&base->cpu_base->timer_waiters);
  1278. spin_unlock_bh(&base->cpu_base->softirq_expiry_lock);
  1279. }
  1280. #else
  1281. static inline void
  1282. hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base) { }
  1283. static inline void
  1284. hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base) { }
  1285. static inline void
  1286. hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base) { }
  1287. static inline void hrtimer_sync_wait_running(struct hrtimer_cpu_base *base,
  1288. unsigned long flags) { }
  1289. #endif
  1290. /**
  1291. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  1292. * @timer: the timer to be cancelled
  1293. *
  1294. * Returns:
  1295. * 0 when the timer was not active
  1296. * 1 when the timer was active
  1297. */
  1298. int hrtimer_cancel(struct hrtimer *timer)
  1299. {
  1300. int ret;
  1301. do {
  1302. ret = hrtimer_try_to_cancel(timer);
  1303. if (ret < 0)
  1304. hrtimer_cancel_wait_running(timer);
  1305. } while (ret < 0);
  1306. return ret;
  1307. }
  1308. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  1309. /**
  1310. * __hrtimer_get_remaining - get remaining time for the timer
  1311. * @timer: the timer to read
  1312. * @adjust: adjust relative timers when CONFIG_TIME_LOW_RES=y
  1313. */
  1314. ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust)
  1315. {
  1316. unsigned long flags;
  1317. ktime_t rem;
  1318. lock_hrtimer_base(timer, &flags);
  1319. if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust)
  1320. rem = hrtimer_expires_remaining_adjusted(timer);
  1321. else
  1322. rem = hrtimer_expires_remaining(timer);
  1323. unlock_hrtimer_base(timer, &flags);
  1324. return rem;
  1325. }
  1326. EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);
  1327. #ifdef CONFIG_NO_HZ_COMMON
  1328. /**
  1329. * hrtimer_get_next_event - get the time until next expiry event
  1330. *
  1331. * Returns the next expiry time or KTIME_MAX if no timer is pending.
  1332. */
  1333. u64 hrtimer_get_next_event(void)
  1334. {
  1335. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1336. u64 expires = KTIME_MAX;
  1337. unsigned long flags;
  1338. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1339. if (!hrtimer_hres_active(cpu_base))
  1340. expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
  1341. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1342. return expires;
  1343. }
  1344. /**
  1345. * hrtimer_next_event_without - time until next expiry event w/o one timer
  1346. * @exclude: timer to exclude
  1347. *
  1348. * Returns the next expiry time over all timers except for the @exclude one or
  1349. * KTIME_MAX if none of them is pending.
  1350. */
  1351. u64 hrtimer_next_event_without(const struct hrtimer *exclude)
  1352. {
  1353. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1354. u64 expires = KTIME_MAX;
  1355. unsigned long flags;
  1356. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1357. if (hrtimer_hres_active(cpu_base)) {
  1358. unsigned int active;
  1359. if (!cpu_base->softirq_activated) {
  1360. active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
  1361. expires = __hrtimer_next_event_base(cpu_base, exclude,
  1362. active, KTIME_MAX);
  1363. }
  1364. active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
  1365. expires = __hrtimer_next_event_base(cpu_base, exclude, active,
  1366. expires);
  1367. }
  1368. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1369. return expires;
  1370. }
  1371. #endif
  1372. static inline int hrtimer_clockid_to_base(clockid_t clock_id)
  1373. {
  1374. switch (clock_id) {
  1375. case CLOCK_REALTIME:
  1376. return HRTIMER_BASE_REALTIME;
  1377. case CLOCK_MONOTONIC:
  1378. return HRTIMER_BASE_MONOTONIC;
  1379. case CLOCK_BOOTTIME:
  1380. return HRTIMER_BASE_BOOTTIME;
  1381. case CLOCK_TAI:
  1382. return HRTIMER_BASE_TAI;
  1383. default:
  1384. WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id);
  1385. return HRTIMER_BASE_MONOTONIC;
  1386. }
  1387. }
  1388. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  1389. enum hrtimer_mode mode)
  1390. {
  1391. bool softtimer = !!(mode & HRTIMER_MODE_SOFT);
  1392. struct hrtimer_cpu_base *cpu_base;
  1393. int base;
  1394. /*
  1395. * On PREEMPT_RT enabled kernels hrtimers which are not explicitly
  1396. * marked for hard interrupt expiry mode are moved into soft
  1397. * interrupt context for latency reasons and because the callbacks
  1398. * can invoke functions which might sleep on RT, e.g. spin_lock().
  1399. */
  1400. if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(mode & HRTIMER_MODE_HARD))
  1401. softtimer = true;
  1402. memset(timer, 0, sizeof(struct hrtimer));
  1403. cpu_base = raw_cpu_ptr(&hrtimer_bases);
  1404. /*
  1405. * POSIX magic: Relative CLOCK_REALTIME timers are not affected by
  1406. * clock modifications, so they needs to become CLOCK_MONOTONIC to
  1407. * ensure POSIX compliance.
  1408. */
  1409. if (clock_id == CLOCK_REALTIME && mode & HRTIMER_MODE_REL)
  1410. clock_id = CLOCK_MONOTONIC;
  1411. base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0;
  1412. base += hrtimer_clockid_to_base(clock_id);
  1413. timer->is_soft = softtimer;
  1414. timer->is_hard = !!(mode & HRTIMER_MODE_HARD);
  1415. timer->base = &cpu_base->clock_base[base];
  1416. timerqueue_init(&timer->node);
  1417. }
  1418. /**
  1419. * hrtimer_init - initialize a timer to the given clock
  1420. * @timer: the timer to be initialized
  1421. * @clock_id: the clock to be used
  1422. * @mode: The modes which are relevant for initialization:
  1423. * HRTIMER_MODE_ABS, HRTIMER_MODE_REL, HRTIMER_MODE_ABS_SOFT,
  1424. * HRTIMER_MODE_REL_SOFT
  1425. *
  1426. * The PINNED variants of the above can be handed in,
  1427. * but the PINNED bit is ignored as pinning happens
  1428. * when the hrtimer is started
  1429. */
  1430. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  1431. enum hrtimer_mode mode)
  1432. {
  1433. debug_init(timer, clock_id, mode);
  1434. __hrtimer_init(timer, clock_id, mode);
  1435. }
  1436. EXPORT_SYMBOL_GPL(hrtimer_init);
  1437. /*
  1438. * A timer is active, when it is enqueued into the rbtree or the
  1439. * callback function is running or it's in the state of being migrated
  1440. * to another cpu.
  1441. *
  1442. * It is important for this function to not return a false negative.
  1443. */
  1444. bool hrtimer_active(const struct hrtimer *timer)
  1445. {
  1446. struct hrtimer_clock_base *base;
  1447. unsigned int seq;
  1448. do {
  1449. base = READ_ONCE(timer->base);
  1450. seq = raw_read_seqcount_begin(&base->seq);
  1451. if (timer->state != HRTIMER_STATE_INACTIVE ||
  1452. base->running == timer)
  1453. return true;
  1454. } while (read_seqcount_retry(&base->seq, seq) ||
  1455. base != READ_ONCE(timer->base));
  1456. return false;
  1457. }
  1458. EXPORT_SYMBOL_GPL(hrtimer_active);
  1459. /*
  1460. * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3
  1461. * distinct sections:
  1462. *
  1463. * - queued: the timer is queued
  1464. * - callback: the timer is being ran
  1465. * - post: the timer is inactive or (re)queued
  1466. *
  1467. * On the read side we ensure we observe timer->state and cpu_base->running
  1468. * from the same section, if anything changed while we looked at it, we retry.
  1469. * This includes timer->base changing because sequence numbers alone are
  1470. * insufficient for that.
  1471. *
  1472. * The sequence numbers are required because otherwise we could still observe
  1473. * a false negative if the read side got smeared over multiple consecutive
  1474. * __run_hrtimer() invocations.
  1475. */
  1476. static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
  1477. struct hrtimer_clock_base *base,
  1478. struct hrtimer *timer, ktime_t *now,
  1479. unsigned long flags) __must_hold(&cpu_base->lock)
  1480. {
  1481. enum hrtimer_restart (*fn)(struct hrtimer *);
  1482. bool expires_in_hardirq;
  1483. int restart;
  1484. lockdep_assert_held(&cpu_base->lock);
  1485. debug_deactivate(timer);
  1486. base->running = timer;
  1487. /*
  1488. * Separate the ->running assignment from the ->state assignment.
  1489. *
  1490. * As with a regular write barrier, this ensures the read side in
  1491. * hrtimer_active() cannot observe base->running == NULL &&
  1492. * timer->state == INACTIVE.
  1493. */
  1494. raw_write_seqcount_barrier(&base->seq);
  1495. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
  1496. fn = timer->function;
  1497. /*
  1498. * Clear the 'is relative' flag for the TIME_LOW_RES case. If the
  1499. * timer is restarted with a period then it becomes an absolute
  1500. * timer. If its not restarted it does not matter.
  1501. */
  1502. if (IS_ENABLED(CONFIG_TIME_LOW_RES))
  1503. timer->is_rel = false;
  1504. /*
  1505. * The timer is marked as running in the CPU base, so it is
  1506. * protected against migration to a different CPU even if the lock
  1507. * is dropped.
  1508. */
  1509. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1510. trace_hrtimer_expire_entry(timer, now);
  1511. expires_in_hardirq = lockdep_hrtimer_enter(timer);
  1512. restart = fn(timer);
  1513. lockdep_hrtimer_exit(expires_in_hardirq);
  1514. trace_hrtimer_expire_exit(timer);
  1515. raw_spin_lock_irq(&cpu_base->lock);
  1516. /*
  1517. * Note: We clear the running state after enqueue_hrtimer and
  1518. * we do not reprogram the event hardware. Happens either in
  1519. * hrtimer_start_range_ns() or in hrtimer_interrupt()
  1520. *
  1521. * Note: Because we dropped the cpu_base->lock above,
  1522. * hrtimer_start_range_ns() can have popped in and enqueued the timer
  1523. * for us already.
  1524. */
  1525. if (restart != HRTIMER_NORESTART &&
  1526. !(timer->state & HRTIMER_STATE_ENQUEUED))
  1527. enqueue_hrtimer(timer, base, HRTIMER_MODE_ABS);
  1528. /*
  1529. * Separate the ->running assignment from the ->state assignment.
  1530. *
  1531. * As with a regular write barrier, this ensures the read side in
  1532. * hrtimer_active() cannot observe base->running.timer == NULL &&
  1533. * timer->state == INACTIVE.
  1534. */
  1535. raw_write_seqcount_barrier(&base->seq);
  1536. WARN_ON_ONCE(base->running != timer);
  1537. base->running = NULL;
  1538. }
  1539. static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now,
  1540. unsigned long flags, unsigned int active_mask)
  1541. {
  1542. struct hrtimer_clock_base *base;
  1543. unsigned int active = cpu_base->active_bases & active_mask;
  1544. for_each_active_base(base, cpu_base, active) {
  1545. struct timerqueue_node *node;
  1546. ktime_t basenow;
  1547. basenow = ktime_add(now, base->offset);
  1548. while ((node = timerqueue_getnext(&base->active))) {
  1549. struct hrtimer *timer;
  1550. timer = container_of(node, struct hrtimer, node);
  1551. /*
  1552. * The immediate goal for using the softexpires is
  1553. * minimizing wakeups, not running timers at the
  1554. * earliest interrupt after their soft expiration.
  1555. * This allows us to avoid using a Priority Search
  1556. * Tree, which can answer a stabbing query for
  1557. * overlapping intervals and instead use the simple
  1558. * BST we already have.
  1559. * We don't add extra wakeups by delaying timers that
  1560. * are right-of a not yet expired timer, because that
  1561. * timer will have to trigger a wakeup anyway.
  1562. */
  1563. if (basenow < hrtimer_get_softexpires_tv64(timer))
  1564. break;
  1565. __run_hrtimer(cpu_base, base, timer, &basenow, flags);
  1566. if (active_mask == HRTIMER_ACTIVE_SOFT)
  1567. hrtimer_sync_wait_running(cpu_base, flags);
  1568. }
  1569. }
  1570. }
  1571. static __latent_entropy void hrtimer_run_softirq(void)
  1572. {
  1573. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1574. unsigned long flags;
  1575. ktime_t now;
  1576. hrtimer_cpu_base_lock_expiry(cpu_base);
  1577. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1578. now = hrtimer_update_base(cpu_base);
  1579. __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_SOFT);
  1580. cpu_base->softirq_activated = 0;
  1581. hrtimer_update_softirq_timer(cpu_base, true);
  1582. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1583. hrtimer_cpu_base_unlock_expiry(cpu_base);
  1584. }
  1585. #ifdef CONFIG_HIGH_RES_TIMERS
  1586. /*
  1587. * High resolution timer interrupt
  1588. * Called with interrupts disabled
  1589. */
  1590. void hrtimer_interrupt(struct clock_event_device *dev)
  1591. {
  1592. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1593. ktime_t expires_next, now, entry_time, delta;
  1594. unsigned long flags;
  1595. int retries = 0;
  1596. BUG_ON(!cpu_base->hres_active);
  1597. cpu_base->nr_events++;
  1598. dev->next_event = KTIME_MAX;
  1599. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1600. entry_time = now = hrtimer_update_base(cpu_base);
  1601. retry:
  1602. cpu_base->in_hrtirq = 1;
  1603. /*
  1604. * We set expires_next to KTIME_MAX here with cpu_base->lock
  1605. * held to prevent that a timer is enqueued in our queue via
  1606. * the migration code. This does not affect enqueueing of
  1607. * timers which run their callback and need to be requeued on
  1608. * this CPU.
  1609. */
  1610. cpu_base->expires_next = KTIME_MAX;
  1611. if (!ktime_before(now, cpu_base->softirq_expires_next)) {
  1612. cpu_base->softirq_expires_next = KTIME_MAX;
  1613. cpu_base->softirq_activated = 1;
  1614. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  1615. }
  1616. __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
  1617. /* Reevaluate the clock bases for the [soft] next expiry */
  1618. expires_next = hrtimer_update_next_event(cpu_base);
  1619. /*
  1620. * Store the new expiry value so the migration code can verify
  1621. * against it.
  1622. */
  1623. cpu_base->expires_next = expires_next;
  1624. cpu_base->in_hrtirq = 0;
  1625. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1626. /* Reprogramming necessary ? */
  1627. if (!tick_program_event(expires_next, 0)) {
  1628. cpu_base->hang_detected = 0;
  1629. return;
  1630. }
  1631. /*
  1632. * The next timer was already expired due to:
  1633. * - tracing
  1634. * - long lasting callbacks
  1635. * - being scheduled away when running in a VM
  1636. *
  1637. * We need to prevent that we loop forever in the hrtimer
  1638. * interrupt routine. We give it 3 attempts to avoid
  1639. * overreacting on some spurious event.
  1640. *
  1641. * Acquire base lock for updating the offsets and retrieving
  1642. * the current time.
  1643. */
  1644. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1645. now = hrtimer_update_base(cpu_base);
  1646. cpu_base->nr_retries++;
  1647. if (++retries < 3)
  1648. goto retry;
  1649. /*
  1650. * Give the system a chance to do something else than looping
  1651. * here. We stored the entry time, so we know exactly how long
  1652. * we spent here. We schedule the next event this amount of
  1653. * time away.
  1654. */
  1655. cpu_base->nr_hangs++;
  1656. cpu_base->hang_detected = 1;
  1657. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1658. delta = ktime_sub(now, entry_time);
  1659. if ((unsigned int)delta > cpu_base->max_hang_time)
  1660. cpu_base->max_hang_time = (unsigned int) delta;
  1661. /*
  1662. * Limit it to a sensible value as we enforce a longer
  1663. * delay. Give the CPU at least 100ms to catch up.
  1664. */
  1665. if (delta > 100 * NSEC_PER_MSEC)
  1666. expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
  1667. else
  1668. expires_next = ktime_add(now, delta);
  1669. tick_program_event(expires_next, 1);
  1670. pr_warn_once("hrtimer: interrupt took %llu ns\n", ktime_to_ns(delta));
  1671. }
  1672. #endif /* !CONFIG_HIGH_RES_TIMERS */
  1673. /*
  1674. * Called from run_local_timers in hardirq context every jiffy
  1675. */
  1676. void hrtimer_run_queues(void)
  1677. {
  1678. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1679. unsigned long flags;
  1680. ktime_t now;
  1681. if (hrtimer_hres_active(cpu_base))
  1682. return;
  1683. /*
  1684. * This _is_ ugly: We have to check periodically, whether we
  1685. * can switch to highres and / or nohz mode. The clocksource
  1686. * switch happens with xtime_lock held. Notification from
  1687. * there only sets the check bit in the tick_oneshot code,
  1688. * otherwise we might deadlock vs. xtime_lock.
  1689. */
  1690. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) {
  1691. hrtimer_switch_to_hres();
  1692. return;
  1693. }
  1694. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1695. now = hrtimer_update_base(cpu_base);
  1696. if (!ktime_before(now, cpu_base->softirq_expires_next)) {
  1697. cpu_base->softirq_expires_next = KTIME_MAX;
  1698. cpu_base->softirq_activated = 1;
  1699. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  1700. }
  1701. __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
  1702. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1703. }
  1704. /*
  1705. * Sleep related functions:
  1706. */
  1707. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1708. {
  1709. struct hrtimer_sleeper *t =
  1710. container_of(timer, struct hrtimer_sleeper, timer);
  1711. struct task_struct *task = t->task;
  1712. t->task = NULL;
  1713. if (task)
  1714. wake_up_process(task);
  1715. return HRTIMER_NORESTART;
  1716. }
  1717. /**
  1718. * hrtimer_sleeper_start_expires - Start a hrtimer sleeper timer
  1719. * @sl: sleeper to be started
  1720. * @mode: timer mode abs/rel
  1721. *
  1722. * Wrapper around hrtimer_start_expires() for hrtimer_sleeper based timers
  1723. * to allow PREEMPT_RT to tweak the delivery mode (soft/hardirq context)
  1724. */
  1725. void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl,
  1726. enum hrtimer_mode mode)
  1727. {
  1728. /*
  1729. * Make the enqueue delivery mode check work on RT. If the sleeper
  1730. * was initialized for hard interrupt delivery, force the mode bit.
  1731. * This is a special case for hrtimer_sleepers because
  1732. * hrtimer_init_sleeper() determines the delivery mode on RT so the
  1733. * fiddling with this decision is avoided at the call sites.
  1734. */
  1735. if (IS_ENABLED(CONFIG_PREEMPT_RT) && sl->timer.is_hard)
  1736. mode |= HRTIMER_MODE_HARD;
  1737. hrtimer_start_expires(&sl->timer, mode);
  1738. }
  1739. EXPORT_SYMBOL_GPL(hrtimer_sleeper_start_expires);
  1740. static void __hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
  1741. clockid_t clock_id, enum hrtimer_mode mode)
  1742. {
  1743. /*
  1744. * On PREEMPT_RT enabled kernels hrtimers which are not explicitly
  1745. * marked for hard interrupt expiry mode are moved into soft
  1746. * interrupt context either for latency reasons or because the
  1747. * hrtimer callback takes regular spinlocks or invokes other
  1748. * functions which are not suitable for hard interrupt context on
  1749. * PREEMPT_RT.
  1750. *
  1751. * The hrtimer_sleeper callback is RT compatible in hard interrupt
  1752. * context, but there is a latency concern: Untrusted userspace can
  1753. * spawn many threads which arm timers for the same expiry time on
  1754. * the same CPU. That causes a latency spike due to the wakeup of
  1755. * a gazillion threads.
  1756. *
  1757. * OTOH, privileged real-time user space applications rely on the
  1758. * low latency of hard interrupt wakeups. If the current task is in
  1759. * a real-time scheduling class, mark the mode for hard interrupt
  1760. * expiry.
  1761. */
  1762. if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
  1763. if (rt_or_dl_task_policy(current) && !(mode & HRTIMER_MODE_SOFT))
  1764. mode |= HRTIMER_MODE_HARD;
  1765. }
  1766. __hrtimer_init(&sl->timer, clock_id, mode);
  1767. sl->timer.function = hrtimer_wakeup;
  1768. sl->task = current;
  1769. }
  1770. /**
  1771. * hrtimer_init_sleeper - initialize sleeper to the given clock
  1772. * @sl: sleeper to be initialized
  1773. * @clock_id: the clock to be used
  1774. * @mode: timer mode abs/rel
  1775. */
  1776. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id,
  1777. enum hrtimer_mode mode)
  1778. {
  1779. debug_init(&sl->timer, clock_id, mode);
  1780. __hrtimer_init_sleeper(sl, clock_id, mode);
  1781. }
  1782. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  1783. int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts)
  1784. {
  1785. switch(restart->nanosleep.type) {
  1786. #ifdef CONFIG_COMPAT_32BIT_TIME
  1787. case TT_COMPAT:
  1788. if (put_old_timespec32(ts, restart->nanosleep.compat_rmtp))
  1789. return -EFAULT;
  1790. break;
  1791. #endif
  1792. case TT_NATIVE:
  1793. if (put_timespec64(ts, restart->nanosleep.rmtp))
  1794. return -EFAULT;
  1795. break;
  1796. default:
  1797. BUG();
  1798. }
  1799. return -ERESTART_RESTARTBLOCK;
  1800. }
  1801. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1802. {
  1803. struct restart_block *restart;
  1804. do {
  1805. set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
  1806. hrtimer_sleeper_start_expires(t, mode);
  1807. if (likely(t->task))
  1808. schedule();
  1809. hrtimer_cancel(&t->timer);
  1810. mode = HRTIMER_MODE_ABS;
  1811. } while (t->task && !signal_pending(current));
  1812. __set_current_state(TASK_RUNNING);
  1813. if (!t->task)
  1814. return 0;
  1815. restart = &current->restart_block;
  1816. if (restart->nanosleep.type != TT_NONE) {
  1817. ktime_t rem = hrtimer_expires_remaining(&t->timer);
  1818. struct timespec64 rmt;
  1819. if (rem <= 0)
  1820. return 0;
  1821. rmt = ktime_to_timespec64(rem);
  1822. return nanosleep_copyout(restart, &rmt);
  1823. }
  1824. return -ERESTART_RESTARTBLOCK;
  1825. }
  1826. static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1827. {
  1828. struct hrtimer_sleeper t;
  1829. int ret;
  1830. hrtimer_init_sleeper_on_stack(&t, restart->nanosleep.clockid,
  1831. HRTIMER_MODE_ABS);
  1832. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  1833. ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
  1834. destroy_hrtimer_on_stack(&t.timer);
  1835. return ret;
  1836. }
  1837. long hrtimer_nanosleep(ktime_t rqtp, const enum hrtimer_mode mode,
  1838. const clockid_t clockid)
  1839. {
  1840. struct restart_block *restart;
  1841. struct hrtimer_sleeper t;
  1842. int ret = 0;
  1843. hrtimer_init_sleeper_on_stack(&t, clockid, mode);
  1844. hrtimer_set_expires_range_ns(&t.timer, rqtp, current->timer_slack_ns);
  1845. ret = do_nanosleep(&t, mode);
  1846. if (ret != -ERESTART_RESTARTBLOCK)
  1847. goto out;
  1848. /* Absolute timers do not update the rmtp value and restart: */
  1849. if (mode == HRTIMER_MODE_ABS) {
  1850. ret = -ERESTARTNOHAND;
  1851. goto out;
  1852. }
  1853. restart = &current->restart_block;
  1854. restart->nanosleep.clockid = t.timer.base->clockid;
  1855. restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
  1856. set_restart_fn(restart, hrtimer_nanosleep_restart);
  1857. out:
  1858. destroy_hrtimer_on_stack(&t.timer);
  1859. return ret;
  1860. }
  1861. #ifdef CONFIG_64BIT
  1862. SYSCALL_DEFINE2(nanosleep, struct __kernel_timespec __user *, rqtp,
  1863. struct __kernel_timespec __user *, rmtp)
  1864. {
  1865. struct timespec64 tu;
  1866. if (get_timespec64(&tu, rqtp))
  1867. return -EFAULT;
  1868. if (!timespec64_valid(&tu))
  1869. return -EINVAL;
  1870. current->restart_block.fn = do_no_restart_syscall;
  1871. current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
  1872. current->restart_block.nanosleep.rmtp = rmtp;
  1873. return hrtimer_nanosleep(timespec64_to_ktime(tu), HRTIMER_MODE_REL,
  1874. CLOCK_MONOTONIC);
  1875. }
  1876. #endif
  1877. #ifdef CONFIG_COMPAT_32BIT_TIME
  1878. SYSCALL_DEFINE2(nanosleep_time32, struct old_timespec32 __user *, rqtp,
  1879. struct old_timespec32 __user *, rmtp)
  1880. {
  1881. struct timespec64 tu;
  1882. if (get_old_timespec32(&tu, rqtp))
  1883. return -EFAULT;
  1884. if (!timespec64_valid(&tu))
  1885. return -EINVAL;
  1886. current->restart_block.fn = do_no_restart_syscall;
  1887. current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
  1888. current->restart_block.nanosleep.compat_rmtp = rmtp;
  1889. return hrtimer_nanosleep(timespec64_to_ktime(tu), HRTIMER_MODE_REL,
  1890. CLOCK_MONOTONIC);
  1891. }
  1892. #endif
  1893. /*
  1894. * Functions related to boot-time initialization:
  1895. */
  1896. int hrtimers_prepare_cpu(unsigned int cpu)
  1897. {
  1898. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1899. int i;
  1900. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1901. struct hrtimer_clock_base *clock_b = &cpu_base->clock_base[i];
  1902. clock_b->cpu_base = cpu_base;
  1903. seqcount_raw_spinlock_init(&clock_b->seq, &cpu_base->lock);
  1904. timerqueue_init_head(&clock_b->active);
  1905. }
  1906. cpu_base->cpu = cpu;
  1907. hrtimer_cpu_base_init_expiry_lock(cpu_base);
  1908. return 0;
  1909. }
  1910. int hrtimers_cpu_starting(unsigned int cpu)
  1911. {
  1912. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1913. /* Clear out any left over state from a CPU down operation */
  1914. cpu_base->active_bases = 0;
  1915. cpu_base->hres_active = 0;
  1916. cpu_base->hang_detected = 0;
  1917. cpu_base->next_timer = NULL;
  1918. cpu_base->softirq_next_timer = NULL;
  1919. cpu_base->expires_next = KTIME_MAX;
  1920. cpu_base->softirq_expires_next = KTIME_MAX;
  1921. cpu_base->online = 1;
  1922. return 0;
  1923. }
  1924. #ifdef CONFIG_HOTPLUG_CPU
  1925. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1926. struct hrtimer_clock_base *new_base)
  1927. {
  1928. struct hrtimer *timer;
  1929. struct timerqueue_node *node;
  1930. while ((node = timerqueue_getnext(&old_base->active))) {
  1931. timer = container_of(node, struct hrtimer, node);
  1932. BUG_ON(hrtimer_callback_running(timer));
  1933. debug_deactivate(timer);
  1934. /*
  1935. * Mark it as ENQUEUED not INACTIVE otherwise the
  1936. * timer could be seen as !active and just vanish away
  1937. * under us on another CPU
  1938. */
  1939. __remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0);
  1940. timer->base = new_base;
  1941. /*
  1942. * Enqueue the timers on the new cpu. This does not
  1943. * reprogram the event device in case the timer
  1944. * expires before the earliest on this CPU, but we run
  1945. * hrtimer_interrupt after we migrated everything to
  1946. * sort out already expired timers and reprogram the
  1947. * event device.
  1948. */
  1949. enqueue_hrtimer(timer, new_base, HRTIMER_MODE_ABS);
  1950. }
  1951. }
  1952. int hrtimers_cpu_dying(unsigned int dying_cpu)
  1953. {
  1954. int i, ncpu = cpumask_any_and(cpu_active_mask, housekeeping_cpumask(HK_TYPE_TIMER));
  1955. struct hrtimer_cpu_base *old_base, *new_base;
  1956. old_base = this_cpu_ptr(&hrtimer_bases);
  1957. new_base = &per_cpu(hrtimer_bases, ncpu);
  1958. /*
  1959. * The caller is globally serialized and nobody else
  1960. * takes two locks at once, deadlock is not possible.
  1961. */
  1962. raw_spin_lock(&old_base->lock);
  1963. raw_spin_lock_nested(&new_base->lock, SINGLE_DEPTH_NESTING);
  1964. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1965. migrate_hrtimer_list(&old_base->clock_base[i],
  1966. &new_base->clock_base[i]);
  1967. }
  1968. /* Tell the other CPU to retrigger the next event */
  1969. smp_call_function_single(ncpu, retrigger_next_event, NULL, 0);
  1970. raw_spin_unlock(&new_base->lock);
  1971. old_base->online = 0;
  1972. raw_spin_unlock(&old_base->lock);
  1973. return 0;
  1974. }
  1975. #endif /* CONFIG_HOTPLUG_CPU */
  1976. void __init hrtimers_init(void)
  1977. {
  1978. hrtimers_prepare_cpu(smp_processor_id());
  1979. hrtimers_cpu_starting(smp_processor_id());
  1980. open_softirq(HRTIMER_SOFTIRQ, hrtimer_run_softirq);
  1981. }
  1982. /**
  1983. * schedule_hrtimeout_range_clock - sleep until timeout
  1984. * @expires: timeout value (ktime_t)
  1985. * @delta: slack in expires timeout (ktime_t)
  1986. * @mode: timer mode
  1987. * @clock_id: timer clock to be used
  1988. */
  1989. int __sched
  1990. schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta,
  1991. const enum hrtimer_mode mode, clockid_t clock_id)
  1992. {
  1993. struct hrtimer_sleeper t;
  1994. /*
  1995. * Optimize when a zero timeout value is given. It does not
  1996. * matter whether this is an absolute or a relative time.
  1997. */
  1998. if (expires && *expires == 0) {
  1999. __set_current_state(TASK_RUNNING);
  2000. return 0;
  2001. }
  2002. /*
  2003. * A NULL parameter means "infinite"
  2004. */
  2005. if (!expires) {
  2006. schedule();
  2007. return -EINTR;
  2008. }
  2009. hrtimer_init_sleeper_on_stack(&t, clock_id, mode);
  2010. hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
  2011. hrtimer_sleeper_start_expires(&t, mode);
  2012. if (likely(t.task))
  2013. schedule();
  2014. hrtimer_cancel(&t.timer);
  2015. destroy_hrtimer_on_stack(&t.timer);
  2016. __set_current_state(TASK_RUNNING);
  2017. return !t.task ? 0 : -EINTR;
  2018. }
  2019. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range_clock);
  2020. /**
  2021. * schedule_hrtimeout_range - sleep until timeout
  2022. * @expires: timeout value (ktime_t)
  2023. * @delta: slack in expires timeout (ktime_t)
  2024. * @mode: timer mode
  2025. *
  2026. * Make the current task sleep until the given expiry time has
  2027. * elapsed. The routine will return immediately unless
  2028. * the current task state has been set (see set_current_state()).
  2029. *
  2030. * The @delta argument gives the kernel the freedom to schedule the
  2031. * actual wakeup to a time that is both power and performance friendly
  2032. * for regular (non RT/DL) tasks.
  2033. * The kernel give the normal best effort behavior for "@expires+@delta",
  2034. * but may decide to fire the timer earlier, but no earlier than @expires.
  2035. *
  2036. * You can set the task state as follows -
  2037. *
  2038. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  2039. * pass before the routine returns unless the current task is explicitly
  2040. * woken up, (e.g. by wake_up_process()).
  2041. *
  2042. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  2043. * delivered to the current task or the current task is explicitly woken
  2044. * up.
  2045. *
  2046. * The current task state is guaranteed to be TASK_RUNNING when this
  2047. * routine returns.
  2048. *
  2049. * Returns 0 when the timer has expired. If the task was woken before the
  2050. * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
  2051. * by an explicit wakeup, it returns -EINTR.
  2052. */
  2053. int __sched schedule_hrtimeout_range(ktime_t *expires, u64 delta,
  2054. const enum hrtimer_mode mode)
  2055. {
  2056. return schedule_hrtimeout_range_clock(expires, delta, mode,
  2057. CLOCK_MONOTONIC);
  2058. }
  2059. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
  2060. /**
  2061. * schedule_hrtimeout - sleep until timeout
  2062. * @expires: timeout value (ktime_t)
  2063. * @mode: timer mode
  2064. *
  2065. * Make the current task sleep until the given expiry time has
  2066. * elapsed. The routine will return immediately unless
  2067. * the current task state has been set (see set_current_state()).
  2068. *
  2069. * You can set the task state as follows -
  2070. *
  2071. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  2072. * pass before the routine returns unless the current task is explicitly
  2073. * woken up, (e.g. by wake_up_process()).
  2074. *
  2075. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  2076. * delivered to the current task or the current task is explicitly woken
  2077. * up.
  2078. *
  2079. * The current task state is guaranteed to be TASK_RUNNING when this
  2080. * routine returns.
  2081. *
  2082. * Returns 0 when the timer has expired. If the task was woken before the
  2083. * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
  2084. * by an explicit wakeup, it returns -EINTR.
  2085. */
  2086. int __sched schedule_hrtimeout(ktime_t *expires,
  2087. const enum hrtimer_mode mode)
  2088. {
  2089. return schedule_hrtimeout_range(expires, 0, mode);
  2090. }
  2091. EXPORT_SYMBOL_GPL(schedule_hrtimeout);