hrtimer.c 48 KB

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