hrtimer.c 46 KB

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