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