hrtimer.c 48 KB

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