hrtimer.c 54 KB

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