hrtimer.c 48 KB

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