hrtimer.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049
  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
  472. * but does not set cpu_base::*expires_next, that is done by
  473. * hrtimer[_force]_reprogram and hrtimer_interrupt only. When updating
  474. * cpu_base::*expires_next right away, reprogramming logic would no longer
  475. * work.
  476. *
  477. * When a softirq is pending, we can ignore the HRTIMER_ACTIVE_SOFT bases,
  478. * those timers will get run whenever the softirq gets handled, at the end of
  479. * hrtimer_run_softirq(), hrtimer_update_softirq_timer() will re-add these bases.
  480. *
  481. * Therefore softirq values are those from the HRTIMER_ACTIVE_SOFT clock bases.
  482. * The !softirq values are the minima across HRTIMER_ACTIVE_ALL, unless an actual
  483. * softirq is pending, in which case they're the minima of HRTIMER_ACTIVE_HARD.
  484. *
  485. * @active_mask must be one of:
  486. * - HRTIMER_ACTIVE_ALL,
  487. * - HRTIMER_ACTIVE_SOFT, or
  488. * - HRTIMER_ACTIVE_HARD.
  489. */
  490. static ktime_t
  491. __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base, unsigned int active_mask)
  492. {
  493. unsigned int active;
  494. struct hrtimer *next_timer = NULL;
  495. ktime_t expires_next = KTIME_MAX;
  496. if (!cpu_base->softirq_activated && (active_mask & HRTIMER_ACTIVE_SOFT)) {
  497. active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
  498. cpu_base->softirq_next_timer = NULL;
  499. expires_next = __hrtimer_next_event_base(cpu_base, NULL,
  500. active, KTIME_MAX);
  501. next_timer = cpu_base->softirq_next_timer;
  502. }
  503. if (active_mask & HRTIMER_ACTIVE_HARD) {
  504. active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
  505. cpu_base->next_timer = next_timer;
  506. expires_next = __hrtimer_next_event_base(cpu_base, NULL, active,
  507. expires_next);
  508. }
  509. return expires_next;
  510. }
  511. static ktime_t hrtimer_update_next_event(struct hrtimer_cpu_base *cpu_base)
  512. {
  513. ktime_t expires_next, soft = KTIME_MAX;
  514. /*
  515. * If the soft interrupt has already been activated, ignore the
  516. * soft bases. They will be handled in the already raised soft
  517. * interrupt.
  518. */
  519. if (!cpu_base->softirq_activated) {
  520. soft = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
  521. /*
  522. * Update the soft expiry time. clock_settime() might have
  523. * affected it.
  524. */
  525. cpu_base->softirq_expires_next = soft;
  526. }
  527. expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_HARD);
  528. /*
  529. * If a softirq timer is expiring first, update cpu_base->next_timer
  530. * and program the hardware with the soft expiry time.
  531. */
  532. if (expires_next > soft) {
  533. cpu_base->next_timer = cpu_base->softirq_next_timer;
  534. expires_next = soft;
  535. }
  536. return expires_next;
  537. }
  538. static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
  539. {
  540. ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
  541. ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
  542. ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
  543. ktime_t now = ktime_get_update_offsets_now(&base->clock_was_set_seq,
  544. offs_real, offs_boot, offs_tai);
  545. base->clock_base[HRTIMER_BASE_REALTIME_SOFT].offset = *offs_real;
  546. base->clock_base[HRTIMER_BASE_BOOTTIME_SOFT].offset = *offs_boot;
  547. base->clock_base[HRTIMER_BASE_TAI_SOFT].offset = *offs_tai;
  548. return now;
  549. }
  550. /*
  551. * Is the high resolution mode active ?
  552. */
  553. static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
  554. {
  555. return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ?
  556. cpu_base->hres_active : 0;
  557. }
  558. static inline int hrtimer_hres_active(void)
  559. {
  560. return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases));
  561. }
  562. /*
  563. * Reprogram the event source with checking both queues for the
  564. * next event
  565. * Called with interrupts disabled and base->lock held
  566. */
  567. static void
  568. hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
  569. {
  570. ktime_t expires_next;
  571. expires_next = hrtimer_update_next_event(cpu_base);
  572. if (skip_equal && expires_next == cpu_base->expires_next)
  573. return;
  574. cpu_base->expires_next = expires_next;
  575. /*
  576. * If hres is not active, hardware does not have to be
  577. * reprogrammed yet.
  578. *
  579. * If a hang was detected in the last timer interrupt then we
  580. * leave the hang delay active in the hardware. We want the
  581. * system to make progress. That also prevents the following
  582. * scenario:
  583. * T1 expires 50ms from now
  584. * T2 expires 5s from now
  585. *
  586. * T1 is removed, so this code is called and would reprogram
  587. * the hardware to 5s from now. Any hrtimer_start after that
  588. * will not reprogram the hardware due to hang_detected being
  589. * set. So we'd effectivly block all timers until the T2 event
  590. * fires.
  591. */
  592. if (!__hrtimer_hres_active(cpu_base) || cpu_base->hang_detected)
  593. return;
  594. tick_program_event(cpu_base->expires_next, 1);
  595. }
  596. /* High resolution timer related functions */
  597. #ifdef CONFIG_HIGH_RES_TIMERS
  598. /*
  599. * High resolution timer enabled ?
  600. */
  601. static bool hrtimer_hres_enabled __read_mostly = true;
  602. unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC;
  603. EXPORT_SYMBOL_GPL(hrtimer_resolution);
  604. /*
  605. * Enable / Disable high resolution mode
  606. */
  607. static int __init setup_hrtimer_hres(char *str)
  608. {
  609. return (kstrtobool(str, &hrtimer_hres_enabled) == 0);
  610. }
  611. __setup("highres=", setup_hrtimer_hres);
  612. /*
  613. * hrtimer_high_res_enabled - query, if the highres mode is enabled
  614. */
  615. static inline int hrtimer_is_hres_enabled(void)
  616. {
  617. return hrtimer_hres_enabled;
  618. }
  619. /*
  620. * Retrigger next event is called after clock was set
  621. *
  622. * Called with interrupts disabled via on_each_cpu()
  623. */
  624. static void retrigger_next_event(void *arg)
  625. {
  626. struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
  627. if (!__hrtimer_hres_active(base))
  628. return;
  629. raw_spin_lock(&base->lock);
  630. hrtimer_update_base(base);
  631. hrtimer_force_reprogram(base, 0);
  632. raw_spin_unlock(&base->lock);
  633. }
  634. /*
  635. * Switch to high resolution mode
  636. */
  637. static void hrtimer_switch_to_hres(void)
  638. {
  639. struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
  640. if (tick_init_highres()) {
  641. pr_warn("Could not switch to high resolution mode on CPU %u\n",
  642. base->cpu);
  643. return;
  644. }
  645. base->hres_active = 1;
  646. hrtimer_resolution = HIGH_RES_NSEC;
  647. tick_setup_sched_timer();
  648. /* "Retrigger" the interrupt to get things going */
  649. retrigger_next_event(NULL);
  650. }
  651. static void clock_was_set_work(struct work_struct *work)
  652. {
  653. clock_was_set();
  654. }
  655. static DECLARE_WORK(hrtimer_work, clock_was_set_work);
  656. /*
  657. * Called from timekeeping and resume code to reprogram the hrtimer
  658. * interrupt device on all cpus.
  659. */
  660. void clock_was_set_delayed(void)
  661. {
  662. schedule_work(&hrtimer_work);
  663. }
  664. #else
  665. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  666. static inline void hrtimer_switch_to_hres(void) { }
  667. static inline void retrigger_next_event(void *arg) { }
  668. #endif /* CONFIG_HIGH_RES_TIMERS */
  669. /*
  670. * When a timer is enqueued and expires earlier than the already enqueued
  671. * timers, we have to check, whether it expires earlier than the timer for
  672. * which the clock event device was armed.
  673. *
  674. * Called with interrupts disabled and base->cpu_base.lock held
  675. */
  676. static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
  677. {
  678. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  679. struct hrtimer_clock_base *base = timer->base;
  680. ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  681. WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
  682. /*
  683. * CLOCK_REALTIME timer might be requested with an absolute
  684. * expiry time which is less than base->offset. Set it to 0.
  685. */
  686. if (expires < 0)
  687. expires = 0;
  688. if (timer->is_soft) {
  689. /*
  690. * soft hrtimer could be started on a remote CPU. In this
  691. * case softirq_expires_next needs to be updated on the
  692. * remote CPU. The soft hrtimer will not expire before the
  693. * first hard hrtimer on the remote CPU -
  694. * hrtimer_check_target() prevents this case.
  695. */
  696. struct hrtimer_cpu_base *timer_cpu_base = base->cpu_base;
  697. if (timer_cpu_base->softirq_activated)
  698. return;
  699. if (!ktime_before(expires, timer_cpu_base->softirq_expires_next))
  700. return;
  701. timer_cpu_base->softirq_next_timer = timer;
  702. timer_cpu_base->softirq_expires_next = expires;
  703. if (!ktime_before(expires, timer_cpu_base->expires_next) ||
  704. !reprogram)
  705. return;
  706. }
  707. /*
  708. * If the timer is not on the current cpu, we cannot reprogram
  709. * the other cpus clock event device.
  710. */
  711. if (base->cpu_base != cpu_base)
  712. return;
  713. /*
  714. * If the hrtimer interrupt is running, then it will
  715. * reevaluate the clock bases and reprogram the clock event
  716. * device. The callbacks are always executed in hard interrupt
  717. * context so we don't need an extra check for a running
  718. * callback.
  719. */
  720. if (cpu_base->in_hrtirq)
  721. return;
  722. if (expires >= cpu_base->expires_next)
  723. return;
  724. /* Update the pointer to the next expiring timer */
  725. cpu_base->next_timer = timer;
  726. cpu_base->expires_next = expires;
  727. /*
  728. * If hres is not active, hardware does not have to be
  729. * programmed yet.
  730. *
  731. * If a hang was detected in the last timer interrupt then we
  732. * do not schedule a timer which is earlier than the expiry
  733. * which we enforced in the hang detection. We want the system
  734. * to make progress.
  735. */
  736. if (!__hrtimer_hres_active(cpu_base) || cpu_base->hang_detected)
  737. return;
  738. /*
  739. * Program the timer hardware. We enforce the expiry for
  740. * events which are already in the past.
  741. */
  742. tick_program_event(expires, 1);
  743. }
  744. /*
  745. * Clock realtime was set
  746. *
  747. * Change the offset of the realtime clock vs. the monotonic
  748. * clock.
  749. *
  750. * We might have to reprogram the high resolution timer interrupt. On
  751. * SMP we call the architecture specific code to retrigger _all_ high
  752. * resolution timer interrupts. On UP we just disable interrupts and
  753. * call the high resolution interrupt code.
  754. */
  755. void clock_was_set(void)
  756. {
  757. #ifdef CONFIG_HIGH_RES_TIMERS
  758. /* Retrigger the CPU local events everywhere */
  759. on_each_cpu(retrigger_next_event, NULL, 1);
  760. #endif
  761. timerfd_clock_was_set();
  762. }
  763. /*
  764. * During resume we might have to reprogram the high resolution timer
  765. * interrupt on all online CPUs. However, all other CPUs will be
  766. * stopped with IRQs interrupts disabled so the clock_was_set() call
  767. * must be deferred.
  768. */
  769. void hrtimers_resume(void)
  770. {
  771. lockdep_assert_irqs_disabled();
  772. /* Retrigger on the local CPU */
  773. retrigger_next_event(NULL);
  774. /* And schedule a retrigger for all others */
  775. clock_was_set_delayed();
  776. }
  777. /*
  778. * Counterpart to lock_hrtimer_base above:
  779. */
  780. static inline
  781. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  782. {
  783. raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  784. }
  785. /**
  786. * hrtimer_forward - forward the timer expiry
  787. * @timer: hrtimer to forward
  788. * @now: forward past this time
  789. * @interval: the interval to forward
  790. *
  791. * Forward the timer expiry so it will expire in the future.
  792. * Returns the number of overruns.
  793. *
  794. * Can be safely called from the callback function of @timer. If
  795. * called from other contexts @timer must neither be enqueued nor
  796. * running the callback and the caller needs to take care of
  797. * serialization.
  798. *
  799. * Note: This only updates the timer expiry value and does not requeue
  800. * the timer.
  801. */
  802. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  803. {
  804. u64 orun = 1;
  805. ktime_t delta;
  806. delta = ktime_sub(now, hrtimer_get_expires(timer));
  807. if (delta < 0)
  808. return 0;
  809. if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED))
  810. return 0;
  811. if (interval < hrtimer_resolution)
  812. interval = hrtimer_resolution;
  813. if (unlikely(delta >= interval)) {
  814. s64 incr = ktime_to_ns(interval);
  815. orun = ktime_divns(delta, incr);
  816. hrtimer_add_expires_ns(timer, incr * orun);
  817. if (hrtimer_get_expires_tv64(timer) > now)
  818. return orun;
  819. /*
  820. * This (and the ktime_add() below) is the
  821. * correction for exact:
  822. */
  823. orun++;
  824. }
  825. hrtimer_add_expires(timer, interval);
  826. return orun;
  827. }
  828. EXPORT_SYMBOL_GPL(hrtimer_forward);
  829. /*
  830. * enqueue_hrtimer - internal function to (re)start a timer
  831. *
  832. * The timer is inserted in expiry order. Insertion into the
  833. * red black tree is O(log(n)). Must hold the base lock.
  834. *
  835. * Returns 1 when the new timer is the leftmost timer in the tree.
  836. */
  837. static int enqueue_hrtimer(struct hrtimer *timer,
  838. struct hrtimer_clock_base *base,
  839. enum hrtimer_mode mode)
  840. {
  841. debug_activate(timer, mode);
  842. base->cpu_base->active_bases |= 1 << base->index;
  843. /* Pairs with the lockless read in hrtimer_is_queued() */
  844. WRITE_ONCE(timer->state, HRTIMER_STATE_ENQUEUED);
  845. return timerqueue_add(&base->active, &timer->node);
  846. }
  847. /*
  848. * __remove_hrtimer - internal function to remove a timer
  849. *
  850. * Caller must hold the base lock.
  851. *
  852. * High resolution timer mode reprograms the clock event device when the
  853. * timer is the one which expires next. The caller can disable this by setting
  854. * reprogram to zero. This is useful, when the context does a reprogramming
  855. * anyway (e.g. timer interrupt)
  856. */
  857. static void __remove_hrtimer(struct hrtimer *timer,
  858. struct hrtimer_clock_base *base,
  859. u8 newstate, int reprogram)
  860. {
  861. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  862. u8 state = timer->state;
  863. /* Pairs with the lockless read in hrtimer_is_queued() */
  864. WRITE_ONCE(timer->state, newstate);
  865. if (!(state & HRTIMER_STATE_ENQUEUED))
  866. return;
  867. if (!timerqueue_del(&base->active, &timer->node))
  868. cpu_base->active_bases &= ~(1 << base->index);
  869. /*
  870. * Note: If reprogram is false we do not update
  871. * cpu_base->next_timer. This happens when we remove the first
  872. * timer on a remote cpu. No harm as we never dereference
  873. * cpu_base->next_timer. So the worst thing what can happen is
  874. * an superflous call to hrtimer_force_reprogram() on the
  875. * remote cpu later on if the same timer gets enqueued again.
  876. */
  877. if (reprogram && timer == cpu_base->next_timer)
  878. hrtimer_force_reprogram(cpu_base, 1);
  879. }
  880. /*
  881. * remove hrtimer, called with base lock held
  882. */
  883. static inline int
  884. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base, bool restart)
  885. {
  886. u8 state = timer->state;
  887. if (state & HRTIMER_STATE_ENQUEUED) {
  888. int reprogram;
  889. /*
  890. * Remove the timer and force reprogramming when high
  891. * resolution mode is active and the timer is on the current
  892. * CPU. If we remove a timer on another CPU, reprogramming is
  893. * skipped. The interrupt event on this CPU is fired and
  894. * reprogramming happens in the interrupt handler. This is a
  895. * rare case and less expensive than a smp call.
  896. */
  897. debug_deactivate(timer);
  898. reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
  899. if (!restart)
  900. state = HRTIMER_STATE_INACTIVE;
  901. __remove_hrtimer(timer, base, state, reprogram);
  902. return 1;
  903. }
  904. return 0;
  905. }
  906. static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim,
  907. const enum hrtimer_mode mode)
  908. {
  909. #ifdef CONFIG_TIME_LOW_RES
  910. /*
  911. * CONFIG_TIME_LOW_RES indicates that the system has no way to return
  912. * granular time values. For relative timers we add hrtimer_resolution
  913. * (i.e. one jiffie) to prevent short timeouts.
  914. */
  915. timer->is_rel = mode & HRTIMER_MODE_REL;
  916. if (timer->is_rel)
  917. tim = ktime_add_safe(tim, hrtimer_resolution);
  918. #endif
  919. return tim;
  920. }
  921. static void
  922. hrtimer_update_softirq_timer(struct hrtimer_cpu_base *cpu_base, bool reprogram)
  923. {
  924. ktime_t expires;
  925. /*
  926. * Find the next SOFT expiration.
  927. */
  928. expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
  929. /*
  930. * reprogramming needs to be triggered, even if the next soft
  931. * hrtimer expires at the same time than the next hard
  932. * hrtimer. cpu_base->softirq_expires_next needs to be updated!
  933. */
  934. if (expires == KTIME_MAX)
  935. return;
  936. /*
  937. * cpu_base->*next_timer is recomputed by __hrtimer_get_next_event()
  938. * cpu_base->*expires_next is only set by hrtimer_reprogram()
  939. */
  940. hrtimer_reprogram(cpu_base->softirq_next_timer, reprogram);
  941. }
  942. static int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  943. u64 delta_ns, const enum hrtimer_mode mode,
  944. struct hrtimer_clock_base *base)
  945. {
  946. struct hrtimer_clock_base *new_base;
  947. /* Remove an active timer from the queue: */
  948. remove_hrtimer(timer, base, true);
  949. if (mode & HRTIMER_MODE_REL)
  950. tim = ktime_add_safe(tim, base->get_time());
  951. tim = hrtimer_update_lowres(timer, tim, mode);
  952. hrtimer_set_expires_range_ns(timer, tim, delta_ns);
  953. /* Switch the timer base, if necessary: */
  954. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  955. return enqueue_hrtimer(timer, new_base, mode);
  956. }
  957. /**
  958. * hrtimer_start_range_ns - (re)start an hrtimer
  959. * @timer: the timer to be added
  960. * @tim: expiry time
  961. * @delta_ns: "slack" range for the timer
  962. * @mode: timer mode: absolute (HRTIMER_MODE_ABS) or
  963. * relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
  964. * softirq based mode is considered for debug purpose only!
  965. */
  966. void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  967. u64 delta_ns, const enum hrtimer_mode mode)
  968. {
  969. struct hrtimer_clock_base *base;
  970. unsigned long flags;
  971. /*
  972. * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft
  973. * match.
  974. */
  975. WARN_ON_ONCE(!(mode & HRTIMER_MODE_SOFT) ^ !timer->is_soft);
  976. base = lock_hrtimer_base(timer, &flags);
  977. if (__hrtimer_start_range_ns(timer, tim, delta_ns, mode, base))
  978. hrtimer_reprogram(timer, true);
  979. unlock_hrtimer_base(timer, &flags);
  980. }
  981. EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
  982. /**
  983. * hrtimer_try_to_cancel - try to deactivate a timer
  984. * @timer: hrtimer to stop
  985. *
  986. * Returns:
  987. * 0 when the timer was not active
  988. * 1 when the timer was active
  989. * -1 when the timer is currently executing the callback function and
  990. * cannot be stopped
  991. */
  992. int hrtimer_try_to_cancel(struct hrtimer *timer)
  993. {
  994. struct hrtimer_clock_base *base;
  995. unsigned long flags;
  996. int ret = -1;
  997. /*
  998. * Check lockless first. If the timer is not active (neither
  999. * enqueued nor running the callback, nothing to do here. The
  1000. * base lock does not serialize against a concurrent enqueue,
  1001. * so we can avoid taking it.
  1002. */
  1003. if (!hrtimer_active(timer))
  1004. return 0;
  1005. base = lock_hrtimer_base(timer, &flags);
  1006. if (!hrtimer_callback_running(timer))
  1007. ret = remove_hrtimer(timer, base, false);
  1008. unlock_hrtimer_base(timer, &flags);
  1009. return ret;
  1010. }
  1011. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  1012. /**
  1013. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  1014. * @timer: the timer to be cancelled
  1015. *
  1016. * Returns:
  1017. * 0 when the timer was not active
  1018. * 1 when the timer was active
  1019. */
  1020. int hrtimer_cancel(struct hrtimer *timer)
  1021. {
  1022. for (;;) {
  1023. int ret = hrtimer_try_to_cancel(timer);
  1024. if (ret >= 0)
  1025. return ret;
  1026. cpu_relax();
  1027. }
  1028. }
  1029. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  1030. /**
  1031. * hrtimer_get_remaining - get remaining time for the timer
  1032. * @timer: the timer to read
  1033. * @adjust: adjust relative timers when CONFIG_TIME_LOW_RES=y
  1034. */
  1035. ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust)
  1036. {
  1037. unsigned long flags;
  1038. ktime_t rem;
  1039. lock_hrtimer_base(timer, &flags);
  1040. if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust)
  1041. rem = hrtimer_expires_remaining_adjusted(timer);
  1042. else
  1043. rem = hrtimer_expires_remaining(timer);
  1044. unlock_hrtimer_base(timer, &flags);
  1045. return rem;
  1046. }
  1047. EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);
  1048. #ifdef CONFIG_NO_HZ_COMMON
  1049. /**
  1050. * hrtimer_get_next_event - get the time until next expiry event
  1051. *
  1052. * Returns the next expiry time or KTIME_MAX if no timer is pending.
  1053. */
  1054. u64 hrtimer_get_next_event(void)
  1055. {
  1056. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1057. u64 expires = KTIME_MAX;
  1058. unsigned long flags;
  1059. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1060. if (!__hrtimer_hres_active(cpu_base))
  1061. expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
  1062. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1063. return expires;
  1064. }
  1065. /**
  1066. * hrtimer_next_event_without - time until next expiry event w/o one timer
  1067. * @exclude: timer to exclude
  1068. *
  1069. * Returns the next expiry time over all timers except for the @exclude one or
  1070. * KTIME_MAX if none of them is pending.
  1071. */
  1072. u64 hrtimer_next_event_without(const struct hrtimer *exclude)
  1073. {
  1074. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1075. u64 expires = KTIME_MAX;
  1076. unsigned long flags;
  1077. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1078. if (__hrtimer_hres_active(cpu_base)) {
  1079. unsigned int active;
  1080. if (!cpu_base->softirq_activated) {
  1081. active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
  1082. expires = __hrtimer_next_event_base(cpu_base, exclude,
  1083. active, KTIME_MAX);
  1084. }
  1085. active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
  1086. expires = __hrtimer_next_event_base(cpu_base, exclude, active,
  1087. expires);
  1088. }
  1089. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1090. return expires;
  1091. }
  1092. #endif
  1093. static inline int hrtimer_clockid_to_base(clockid_t clock_id)
  1094. {
  1095. if (likely(clock_id < MAX_CLOCKS)) {
  1096. int base = hrtimer_clock_to_base_table[clock_id];
  1097. if (likely(base != HRTIMER_MAX_CLOCK_BASES))
  1098. return base;
  1099. }
  1100. WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id);
  1101. return HRTIMER_BASE_MONOTONIC;
  1102. }
  1103. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  1104. enum hrtimer_mode mode)
  1105. {
  1106. bool softtimer = !!(mode & HRTIMER_MODE_SOFT);
  1107. int base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0;
  1108. struct hrtimer_cpu_base *cpu_base;
  1109. memset(timer, 0, sizeof(struct hrtimer));
  1110. cpu_base = raw_cpu_ptr(&hrtimer_bases);
  1111. /*
  1112. * POSIX magic: Relative CLOCK_REALTIME timers are not affected by
  1113. * clock modifications, so they needs to become CLOCK_MONOTONIC to
  1114. * ensure POSIX compliance.
  1115. */
  1116. if (clock_id == CLOCK_REALTIME && mode & HRTIMER_MODE_REL)
  1117. clock_id = CLOCK_MONOTONIC;
  1118. base += hrtimer_clockid_to_base(clock_id);
  1119. timer->is_soft = softtimer;
  1120. timer->base = &cpu_base->clock_base[base];
  1121. timerqueue_init(&timer->node);
  1122. }
  1123. /**
  1124. * hrtimer_init - initialize a timer to the given clock
  1125. * @timer: the timer to be initialized
  1126. * @clock_id: the clock to be used
  1127. * @mode: The modes which are relevant for intitialization:
  1128. * HRTIMER_MODE_ABS, HRTIMER_MODE_REL, HRTIMER_MODE_ABS_SOFT,
  1129. * HRTIMER_MODE_REL_SOFT
  1130. *
  1131. * The PINNED variants of the above can be handed in,
  1132. * but the PINNED bit is ignored as pinning happens
  1133. * when the hrtimer is started
  1134. */
  1135. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  1136. enum hrtimer_mode mode)
  1137. {
  1138. debug_init(timer, clock_id, mode);
  1139. __hrtimer_init(timer, clock_id, mode);
  1140. }
  1141. EXPORT_SYMBOL_GPL(hrtimer_init);
  1142. /*
  1143. * A timer is active, when it is enqueued into the rbtree or the
  1144. * callback function is running or it's in the state of being migrated
  1145. * to another cpu.
  1146. *
  1147. * It is important for this function to not return a false negative.
  1148. */
  1149. bool hrtimer_active(const struct hrtimer *timer)
  1150. {
  1151. struct hrtimer_clock_base *base;
  1152. unsigned int seq;
  1153. do {
  1154. base = READ_ONCE(timer->base);
  1155. seq = raw_read_seqcount_begin(&base->seq);
  1156. if (timer->state != HRTIMER_STATE_INACTIVE ||
  1157. base->running == timer)
  1158. return true;
  1159. } while (read_seqcount_retry(&base->seq, seq) ||
  1160. base != READ_ONCE(timer->base));
  1161. return false;
  1162. }
  1163. EXPORT_SYMBOL_GPL(hrtimer_active);
  1164. /*
  1165. * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3
  1166. * distinct sections:
  1167. *
  1168. * - queued: the timer is queued
  1169. * - callback: the timer is being ran
  1170. * - post: the timer is inactive or (re)queued
  1171. *
  1172. * On the read side we ensure we observe timer->state and cpu_base->running
  1173. * from the same section, if anything changed while we looked at it, we retry.
  1174. * This includes timer->base changing because sequence numbers alone are
  1175. * insufficient for that.
  1176. *
  1177. * The sequence numbers are required because otherwise we could still observe
  1178. * a false negative if the read side got smeared over multiple consequtive
  1179. * __run_hrtimer() invocations.
  1180. */
  1181. static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
  1182. struct hrtimer_clock_base *base,
  1183. struct hrtimer *timer, ktime_t *now,
  1184. unsigned long flags)
  1185. {
  1186. enum hrtimer_restart (*fn)(struct hrtimer *);
  1187. int restart;
  1188. lockdep_assert_held(&cpu_base->lock);
  1189. debug_deactivate(timer);
  1190. base->running = timer;
  1191. /*
  1192. * Separate the ->running assignment from the ->state assignment.
  1193. *
  1194. * As with a regular write barrier, this ensures the read side in
  1195. * hrtimer_active() cannot observe base->running == NULL &&
  1196. * timer->state == INACTIVE.
  1197. */
  1198. raw_write_seqcount_barrier(&base->seq);
  1199. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
  1200. fn = timer->function;
  1201. /*
  1202. * Clear the 'is relative' flag for the TIME_LOW_RES case. If the
  1203. * timer is restarted with a period then it becomes an absolute
  1204. * timer. If its not restarted it does not matter.
  1205. */
  1206. if (IS_ENABLED(CONFIG_TIME_LOW_RES))
  1207. timer->is_rel = false;
  1208. /*
  1209. * The timer is marked as running in the CPU base, so it is
  1210. * protected against migration to a different CPU even if the lock
  1211. * is dropped.
  1212. */
  1213. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1214. trace_hrtimer_expire_entry(timer, now);
  1215. restart = fn(timer);
  1216. trace_hrtimer_expire_exit(timer);
  1217. raw_spin_lock_irq(&cpu_base->lock);
  1218. /*
  1219. * Note: We clear the running state after enqueue_hrtimer and
  1220. * we do not reprogram the event hardware. Happens either in
  1221. * hrtimer_start_range_ns() or in hrtimer_interrupt()
  1222. *
  1223. * Note: Because we dropped the cpu_base->lock above,
  1224. * hrtimer_start_range_ns() can have popped in and enqueued the timer
  1225. * for us already.
  1226. */
  1227. if (restart != HRTIMER_NORESTART &&
  1228. !(timer->state & HRTIMER_STATE_ENQUEUED))
  1229. enqueue_hrtimer(timer, base, HRTIMER_MODE_ABS);
  1230. /*
  1231. * Separate the ->running assignment from the ->state assignment.
  1232. *
  1233. * As with a regular write barrier, this ensures the read side in
  1234. * hrtimer_active() cannot observe base->running.timer == NULL &&
  1235. * timer->state == INACTIVE.
  1236. */
  1237. raw_write_seqcount_barrier(&base->seq);
  1238. WARN_ON_ONCE(base->running != timer);
  1239. base->running = NULL;
  1240. }
  1241. static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now,
  1242. unsigned long flags, unsigned int active_mask)
  1243. {
  1244. struct hrtimer_clock_base *base;
  1245. unsigned int active = cpu_base->active_bases & active_mask;
  1246. for_each_active_base(base, cpu_base, active) {
  1247. struct timerqueue_node *node;
  1248. ktime_t basenow;
  1249. basenow = ktime_add(now, base->offset);
  1250. while ((node = timerqueue_getnext(&base->active))) {
  1251. struct hrtimer *timer;
  1252. timer = container_of(node, struct hrtimer, node);
  1253. /*
  1254. * The immediate goal for using the softexpires is
  1255. * minimizing wakeups, not running timers at the
  1256. * earliest interrupt after their soft expiration.
  1257. * This allows us to avoid using a Priority Search
  1258. * Tree, which can answer a stabbing querry for
  1259. * overlapping intervals and instead use the simple
  1260. * BST we already have.
  1261. * We don't add extra wakeups by delaying timers that
  1262. * are right-of a not yet expired timer, because that
  1263. * timer will have to trigger a wakeup anyway.
  1264. */
  1265. if (basenow < hrtimer_get_softexpires_tv64(timer))
  1266. break;
  1267. __run_hrtimer(cpu_base, base, timer, &basenow, flags);
  1268. }
  1269. }
  1270. }
  1271. static __latent_entropy void hrtimer_run_softirq(struct softirq_action *h)
  1272. {
  1273. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1274. unsigned long flags;
  1275. ktime_t now;
  1276. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1277. now = hrtimer_update_base(cpu_base);
  1278. __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_SOFT);
  1279. cpu_base->softirq_activated = 0;
  1280. hrtimer_update_softirq_timer(cpu_base, true);
  1281. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1282. }
  1283. #ifdef CONFIG_HIGH_RES_TIMERS
  1284. /*
  1285. * High resolution timer interrupt
  1286. * Called with interrupts disabled
  1287. */
  1288. void hrtimer_interrupt(struct clock_event_device *dev)
  1289. {
  1290. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1291. ktime_t expires_next, now, entry_time, delta;
  1292. unsigned long flags;
  1293. int retries = 0;
  1294. BUG_ON(!cpu_base->hres_active);
  1295. cpu_base->nr_events++;
  1296. dev->next_event = KTIME_MAX;
  1297. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1298. entry_time = now = hrtimer_update_base(cpu_base);
  1299. retry:
  1300. cpu_base->in_hrtirq = 1;
  1301. /*
  1302. * We set expires_next to KTIME_MAX here with cpu_base->lock
  1303. * held to prevent that a timer is enqueued in our queue via
  1304. * the migration code. This does not affect enqueueing of
  1305. * timers which run their callback and need to be requeued on
  1306. * this CPU.
  1307. */
  1308. cpu_base->expires_next = KTIME_MAX;
  1309. if (!ktime_before(now, cpu_base->softirq_expires_next)) {
  1310. cpu_base->softirq_expires_next = KTIME_MAX;
  1311. cpu_base->softirq_activated = 1;
  1312. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  1313. }
  1314. __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
  1315. /* Reevaluate the clock bases for the [soft] next expiry */
  1316. expires_next = hrtimer_update_next_event(cpu_base);
  1317. /*
  1318. * Store the new expiry value so the migration code can verify
  1319. * against it.
  1320. */
  1321. cpu_base->expires_next = expires_next;
  1322. cpu_base->in_hrtirq = 0;
  1323. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1324. /* Reprogramming necessary ? */
  1325. if (!tick_program_event(expires_next, 0)) {
  1326. cpu_base->hang_detected = 0;
  1327. return;
  1328. }
  1329. /*
  1330. * The next timer was already expired due to:
  1331. * - tracing
  1332. * - long lasting callbacks
  1333. * - being scheduled away when running in a VM
  1334. *
  1335. * We need to prevent that we loop forever in the hrtimer
  1336. * interrupt routine. We give it 3 attempts to avoid
  1337. * overreacting on some spurious event.
  1338. *
  1339. * Acquire base lock for updating the offsets and retrieving
  1340. * the current time.
  1341. */
  1342. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1343. now = hrtimer_update_base(cpu_base);
  1344. cpu_base->nr_retries++;
  1345. if (++retries < 3)
  1346. goto retry;
  1347. /*
  1348. * Give the system a chance to do something else than looping
  1349. * here. We stored the entry time, so we know exactly how long
  1350. * we spent here. We schedule the next event this amount of
  1351. * time away.
  1352. */
  1353. cpu_base->nr_hangs++;
  1354. cpu_base->hang_detected = 1;
  1355. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1356. delta = ktime_sub(now, entry_time);
  1357. if ((unsigned int)delta > cpu_base->max_hang_time)
  1358. cpu_base->max_hang_time = (unsigned int) delta;
  1359. /*
  1360. * Limit it to a sensible value as we enforce a longer
  1361. * delay. Give the CPU at least 100ms to catch up.
  1362. */
  1363. if (delta > 100 * NSEC_PER_MSEC)
  1364. expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
  1365. else
  1366. expires_next = ktime_add(now, delta);
  1367. tick_program_event(expires_next, 1);
  1368. pr_warn_once("hrtimer: interrupt took %llu ns\n", ktime_to_ns(delta));
  1369. }
  1370. /* called with interrupts disabled */
  1371. static inline void __hrtimer_peek_ahead_timers(void)
  1372. {
  1373. struct tick_device *td;
  1374. if (!hrtimer_hres_active())
  1375. return;
  1376. td = this_cpu_ptr(&tick_cpu_device);
  1377. if (td && td->evtdev)
  1378. hrtimer_interrupt(td->evtdev);
  1379. }
  1380. #else /* CONFIG_HIGH_RES_TIMERS */
  1381. static inline void __hrtimer_peek_ahead_timers(void) { }
  1382. #endif /* !CONFIG_HIGH_RES_TIMERS */
  1383. /*
  1384. * Called from run_local_timers in hardirq context every jiffy
  1385. */
  1386. void hrtimer_run_queues(void)
  1387. {
  1388. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1389. unsigned long flags;
  1390. ktime_t now;
  1391. if (__hrtimer_hres_active(cpu_base))
  1392. return;
  1393. /*
  1394. * This _is_ ugly: We have to check periodically, whether we
  1395. * can switch to highres and / or nohz mode. The clocksource
  1396. * switch happens with xtime_lock held. Notification from
  1397. * there only sets the check bit in the tick_oneshot code,
  1398. * otherwise we might deadlock vs. xtime_lock.
  1399. */
  1400. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) {
  1401. hrtimer_switch_to_hres();
  1402. return;
  1403. }
  1404. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1405. now = hrtimer_update_base(cpu_base);
  1406. if (!ktime_before(now, cpu_base->softirq_expires_next)) {
  1407. cpu_base->softirq_expires_next = KTIME_MAX;
  1408. cpu_base->softirq_activated = 1;
  1409. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  1410. }
  1411. __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
  1412. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1413. }
  1414. /*
  1415. * Sleep related functions:
  1416. */
  1417. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1418. {
  1419. struct hrtimer_sleeper *t =
  1420. container_of(timer, struct hrtimer_sleeper, timer);
  1421. struct task_struct *task = t->task;
  1422. t->task = NULL;
  1423. if (task)
  1424. wake_up_process(task);
  1425. return HRTIMER_NORESTART;
  1426. }
  1427. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1428. {
  1429. sl->timer.function = hrtimer_wakeup;
  1430. sl->task = task;
  1431. }
  1432. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  1433. int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts)
  1434. {
  1435. switch(restart->nanosleep.type) {
  1436. #ifdef CONFIG_COMPAT_32BIT_TIME
  1437. case TT_COMPAT:
  1438. if (compat_put_timespec64(ts, restart->nanosleep.compat_rmtp))
  1439. return -EFAULT;
  1440. break;
  1441. #endif
  1442. case TT_NATIVE:
  1443. if (put_timespec64(ts, restart->nanosleep.rmtp))
  1444. return -EFAULT;
  1445. break;
  1446. default:
  1447. BUG();
  1448. }
  1449. return -ERESTART_RESTARTBLOCK;
  1450. }
  1451. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1452. {
  1453. struct restart_block *restart;
  1454. hrtimer_init_sleeper(t, current);
  1455. do {
  1456. set_current_state(TASK_INTERRUPTIBLE);
  1457. hrtimer_start_expires(&t->timer, mode);
  1458. if (likely(t->task))
  1459. freezable_schedule();
  1460. hrtimer_cancel(&t->timer);
  1461. mode = HRTIMER_MODE_ABS;
  1462. } while (t->task && !signal_pending(current));
  1463. __set_current_state(TASK_RUNNING);
  1464. if (!t->task)
  1465. return 0;
  1466. restart = &current->restart_block;
  1467. if (restart->nanosleep.type != TT_NONE) {
  1468. ktime_t rem = hrtimer_expires_remaining(&t->timer);
  1469. struct timespec64 rmt;
  1470. if (rem <= 0)
  1471. return 0;
  1472. rmt = ktime_to_timespec64(rem);
  1473. return nanosleep_copyout(restart, &rmt);
  1474. }
  1475. return -ERESTART_RESTARTBLOCK;
  1476. }
  1477. static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1478. {
  1479. struct hrtimer_sleeper t;
  1480. int ret;
  1481. hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
  1482. HRTIMER_MODE_ABS);
  1483. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  1484. ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
  1485. destroy_hrtimer_on_stack(&t.timer);
  1486. return ret;
  1487. }
  1488. long hrtimer_nanosleep(const struct timespec64 *rqtp,
  1489. const enum hrtimer_mode mode, const clockid_t clockid)
  1490. {
  1491. struct restart_block *restart;
  1492. struct hrtimer_sleeper t;
  1493. int ret = 0;
  1494. u64 slack;
  1495. slack = current->timer_slack_ns;
  1496. if (dl_task(current) || rt_task(current))
  1497. slack = 0;
  1498. hrtimer_init_on_stack(&t.timer, clockid, mode);
  1499. hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack);
  1500. ret = do_nanosleep(&t, mode);
  1501. if (ret != -ERESTART_RESTARTBLOCK)
  1502. goto out;
  1503. /* Absolute timers do not update the rmtp value and restart: */
  1504. if (mode == HRTIMER_MODE_ABS) {
  1505. ret = -ERESTARTNOHAND;
  1506. goto out;
  1507. }
  1508. restart = &current->restart_block;
  1509. restart->nanosleep.clockid = t.timer.base->clockid;
  1510. restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
  1511. set_restart_fn(restart, hrtimer_nanosleep_restart);
  1512. out:
  1513. destroy_hrtimer_on_stack(&t.timer);
  1514. return ret;
  1515. }
  1516. #if !defined(CONFIG_64BIT_TIME) || defined(CONFIG_64BIT)
  1517. SYSCALL_DEFINE2(nanosleep, struct __kernel_timespec __user *, rqtp,
  1518. struct __kernel_timespec __user *, rmtp)
  1519. {
  1520. struct timespec64 tu;
  1521. if (get_timespec64(&tu, rqtp))
  1522. return -EFAULT;
  1523. if (!timespec64_valid(&tu))
  1524. return -EINVAL;
  1525. current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
  1526. current->restart_block.nanosleep.rmtp = rmtp;
  1527. return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1528. }
  1529. #endif
  1530. #ifdef CONFIG_COMPAT_32BIT_TIME
  1531. COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
  1532. struct compat_timespec __user *, rmtp)
  1533. {
  1534. struct timespec64 tu;
  1535. if (compat_get_timespec64(&tu, rqtp))
  1536. return -EFAULT;
  1537. if (!timespec64_valid(&tu))
  1538. return -EINVAL;
  1539. current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
  1540. current->restart_block.nanosleep.compat_rmtp = rmtp;
  1541. return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1542. }
  1543. #endif
  1544. /*
  1545. * Functions related to boot-time initialization:
  1546. */
  1547. int hrtimers_prepare_cpu(unsigned int cpu)
  1548. {
  1549. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1550. int i;
  1551. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1552. cpu_base->clock_base[i].cpu_base = cpu_base;
  1553. timerqueue_init_head(&cpu_base->clock_base[i].active);
  1554. }
  1555. cpu_base->cpu = cpu;
  1556. cpu_base->active_bases = 0;
  1557. cpu_base->hres_active = 0;
  1558. cpu_base->hang_detected = 0;
  1559. cpu_base->next_timer = NULL;
  1560. cpu_base->softirq_next_timer = NULL;
  1561. cpu_base->expires_next = KTIME_MAX;
  1562. cpu_base->softirq_expires_next = KTIME_MAX;
  1563. return 0;
  1564. }
  1565. #ifdef CONFIG_HOTPLUG_CPU
  1566. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1567. struct hrtimer_clock_base *new_base)
  1568. {
  1569. struct hrtimer *timer;
  1570. struct timerqueue_node *node;
  1571. while ((node = timerqueue_getnext(&old_base->active))) {
  1572. timer = container_of(node, struct hrtimer, node);
  1573. BUG_ON(hrtimer_callback_running(timer));
  1574. debug_deactivate(timer);
  1575. /*
  1576. * Mark it as ENQUEUED not INACTIVE otherwise the
  1577. * timer could be seen as !active and just vanish away
  1578. * under us on another CPU
  1579. */
  1580. __remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0);
  1581. timer->base = new_base;
  1582. /*
  1583. * Enqueue the timers on the new cpu. This does not
  1584. * reprogram the event device in case the timer
  1585. * expires before the earliest on this CPU, but we run
  1586. * hrtimer_interrupt after we migrated everything to
  1587. * sort out already expired timers and reprogram the
  1588. * event device.
  1589. */
  1590. enqueue_hrtimer(timer, new_base, HRTIMER_MODE_ABS);
  1591. }
  1592. }
  1593. int hrtimers_dead_cpu(unsigned int scpu)
  1594. {
  1595. struct hrtimer_cpu_base *old_base, *new_base;
  1596. int i;
  1597. BUG_ON(cpu_online(scpu));
  1598. tick_cancel_sched_timer(scpu);
  1599. /*
  1600. * this BH disable ensures that raise_softirq_irqoff() does
  1601. * not wakeup ksoftirqd (and acquire the pi-lock) while
  1602. * holding the cpu_base lock
  1603. */
  1604. local_bh_disable();
  1605. local_irq_disable();
  1606. old_base = &per_cpu(hrtimer_bases, scpu);
  1607. new_base = this_cpu_ptr(&hrtimer_bases);
  1608. /*
  1609. * The caller is globally serialized and nobody else
  1610. * takes two locks at once, deadlock is not possible.
  1611. */
  1612. raw_spin_lock(&new_base->lock);
  1613. raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1614. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1615. migrate_hrtimer_list(&old_base->clock_base[i],
  1616. &new_base->clock_base[i]);
  1617. }
  1618. /*
  1619. * The migration might have changed the first expiring softirq
  1620. * timer on this CPU. Update it.
  1621. */
  1622. hrtimer_update_softirq_timer(new_base, false);
  1623. raw_spin_unlock(&old_base->lock);
  1624. raw_spin_unlock(&new_base->lock);
  1625. /* Check, if we got expired work to do */
  1626. __hrtimer_peek_ahead_timers();
  1627. local_irq_enable();
  1628. local_bh_enable();
  1629. return 0;
  1630. }
  1631. #endif /* CONFIG_HOTPLUG_CPU */
  1632. void __init hrtimers_init(void)
  1633. {
  1634. hrtimers_prepare_cpu(smp_processor_id());
  1635. open_softirq(HRTIMER_SOFTIRQ, hrtimer_run_softirq);
  1636. }
  1637. /**
  1638. * schedule_hrtimeout_range_clock - sleep until timeout
  1639. * @expires: timeout value (ktime_t)
  1640. * @delta: slack in expires timeout (ktime_t)
  1641. * @mode: timer mode
  1642. * @clock_id: timer clock to be used
  1643. */
  1644. int __sched
  1645. schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta,
  1646. const enum hrtimer_mode mode, clockid_t clock_id)
  1647. {
  1648. struct hrtimer_sleeper t;
  1649. /*
  1650. * Optimize when a zero timeout value is given. It does not
  1651. * matter whether this is an absolute or a relative time.
  1652. */
  1653. if (expires && *expires == 0) {
  1654. __set_current_state(TASK_RUNNING);
  1655. return 0;
  1656. }
  1657. /*
  1658. * A NULL parameter means "infinite"
  1659. */
  1660. if (!expires) {
  1661. schedule();
  1662. return -EINTR;
  1663. }
  1664. hrtimer_init_on_stack(&t.timer, clock_id, mode);
  1665. hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
  1666. hrtimer_init_sleeper(&t, current);
  1667. hrtimer_start_expires(&t.timer, mode);
  1668. if (likely(t.task))
  1669. schedule();
  1670. hrtimer_cancel(&t.timer);
  1671. destroy_hrtimer_on_stack(&t.timer);
  1672. __set_current_state(TASK_RUNNING);
  1673. return !t.task ? 0 : -EINTR;
  1674. }
  1675. /**
  1676. * schedule_hrtimeout_range - sleep until timeout
  1677. * @expires: timeout value (ktime_t)
  1678. * @delta: slack in expires timeout (ktime_t)
  1679. * @mode: timer mode
  1680. *
  1681. * Make the current task sleep until the given expiry time has
  1682. * elapsed. The routine will return immediately unless
  1683. * the current task state has been set (see set_current_state()).
  1684. *
  1685. * The @delta argument gives the kernel the freedom to schedule the
  1686. * actual wakeup to a time that is both power and performance friendly.
  1687. * The kernel give the normal best effort behavior for "@expires+@delta",
  1688. * but may decide to fire the timer earlier, but no earlier than @expires.
  1689. *
  1690. * You can set the task state as follows -
  1691. *
  1692. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1693. * pass before the routine returns unless the current task is explicitly
  1694. * woken up, (e.g. by wake_up_process()).
  1695. *
  1696. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1697. * delivered to the current task or the current task is explicitly woken
  1698. * up.
  1699. *
  1700. * The current task state is guaranteed to be TASK_RUNNING when this
  1701. * routine returns.
  1702. *
  1703. * Returns 0 when the timer has expired. If the task was woken before the
  1704. * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
  1705. * by an explicit wakeup, it returns -EINTR.
  1706. */
  1707. int __sched schedule_hrtimeout_range(ktime_t *expires, u64 delta,
  1708. const enum hrtimer_mode mode)
  1709. {
  1710. return schedule_hrtimeout_range_clock(expires, delta, mode,
  1711. CLOCK_MONOTONIC);
  1712. }
  1713. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
  1714. /**
  1715. * schedule_hrtimeout - sleep until timeout
  1716. * @expires: timeout value (ktime_t)
  1717. * @mode: timer mode
  1718. *
  1719. * Make the current task sleep until the given expiry time has
  1720. * elapsed. The routine will return immediately unless
  1721. * the current task state has been set (see set_current_state()).
  1722. *
  1723. * You can set the task state as follows -
  1724. *
  1725. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1726. * pass before the routine returns unless the current task is explicitly
  1727. * woken up, (e.g. by wake_up_process()).
  1728. *
  1729. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1730. * delivered to the current task or the current task is explicitly woken
  1731. * up.
  1732. *
  1733. * The current task state is guaranteed to be TASK_RUNNING when this
  1734. * routine returns.
  1735. *
  1736. * Returns 0 when the timer has expired. If the task was woken before the
  1737. * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
  1738. * by an explicit wakeup, it returns -EINTR.
  1739. */
  1740. int __sched schedule_hrtimeout(ktime_t *expires,
  1741. const enum hrtimer_mode mode)
  1742. {
  1743. return schedule_hrtimeout_range(expires, 0, mode);
  1744. }
  1745. EXPORT_SYMBOL_GPL(schedule_hrtimeout);