hrtimer.c 45 KB

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