hrtimer.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  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 <asm/uaccess.h>
  46. /**
  47. * ktime_get - get the monotonic time in ktime_t format
  48. *
  49. * returns the time in ktime_t format
  50. */
  51. ktime_t ktime_get(void)
  52. {
  53. struct timespec now;
  54. ktime_get_ts(&now);
  55. return timespec_to_ktime(now);
  56. }
  57. /**
  58. * ktime_get_real - get the real (wall-) time in ktime_t format
  59. *
  60. * returns the time in ktime_t format
  61. */
  62. ktime_t ktime_get_real(void)
  63. {
  64. struct timespec now;
  65. getnstimeofday(&now);
  66. return timespec_to_ktime(now);
  67. }
  68. EXPORT_SYMBOL_GPL(ktime_get_real);
  69. /*
  70. * The timer bases:
  71. *
  72. * Note: If we want to add new timer bases, we have to skip the two
  73. * clock ids captured by the cpu-timers. We do this by holding empty
  74. * entries rather than doing math adjustment of the clock ids.
  75. * This ensures that we capture erroneous accesses to these clock ids
  76. * rather than moving them into the range of valid clock id's.
  77. */
  78. DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
  79. {
  80. .clock_base =
  81. {
  82. {
  83. .index = CLOCK_REALTIME,
  84. .get_time = &ktime_get_real,
  85. .resolution = KTIME_LOW_RES,
  86. },
  87. {
  88. .index = CLOCK_MONOTONIC,
  89. .get_time = &ktime_get,
  90. .resolution = KTIME_LOW_RES,
  91. },
  92. }
  93. };
  94. /**
  95. * ktime_get_ts - get the monotonic clock in timespec format
  96. * @ts: pointer to timespec variable
  97. *
  98. * The function calculates the monotonic clock from the realtime
  99. * clock and the wall_to_monotonic offset and stores the result
  100. * in normalized timespec format in the variable pointed to by @ts.
  101. */
  102. void ktime_get_ts(struct timespec *ts)
  103. {
  104. struct timespec tomono;
  105. unsigned long seq;
  106. do {
  107. seq = read_seqbegin(&xtime_lock);
  108. getnstimeofday(ts);
  109. tomono = wall_to_monotonic;
  110. } while (read_seqretry(&xtime_lock, seq));
  111. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
  112. ts->tv_nsec + tomono.tv_nsec);
  113. }
  114. EXPORT_SYMBOL_GPL(ktime_get_ts);
  115. /*
  116. * Get the coarse grained time at the softirq based on xtime and
  117. * wall_to_monotonic.
  118. */
  119. static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
  120. {
  121. ktime_t xtim, tomono;
  122. struct timespec xts;
  123. unsigned long seq;
  124. do {
  125. seq = read_seqbegin(&xtime_lock);
  126. #ifdef CONFIG_NO_HZ
  127. getnstimeofday(&xts);
  128. #else
  129. xts = xtime;
  130. #endif
  131. } while (read_seqretry(&xtime_lock, seq));
  132. xtim = timespec_to_ktime(xts);
  133. tomono = timespec_to_ktime(wall_to_monotonic);
  134. base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
  135. base->clock_base[CLOCK_MONOTONIC].softirq_time =
  136. ktime_add(xtim, tomono);
  137. }
  138. /*
  139. * Helper function to check, whether the timer is running the callback
  140. * function
  141. */
  142. static inline int hrtimer_callback_running(struct hrtimer *timer)
  143. {
  144. return timer->state & HRTIMER_STATE_CALLBACK;
  145. }
  146. /*
  147. * Functions and macros which are different for UP/SMP systems are kept in a
  148. * single place
  149. */
  150. #ifdef CONFIG_SMP
  151. /*
  152. * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
  153. * means that all timers which are tied to this base via timer->base are
  154. * locked, and the base itself is locked too.
  155. *
  156. * So __run_timers/migrate_timers can safely modify all timers which could
  157. * be found on the lists/queues.
  158. *
  159. * When the timer's base is locked, and the timer removed from list, it is
  160. * possible to set timer->base = NULL and drop the lock: the timer remains
  161. * locked.
  162. */
  163. static
  164. struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
  165. unsigned long *flags)
  166. {
  167. struct hrtimer_clock_base *base;
  168. for (;;) {
  169. base = timer->base;
  170. if (likely(base != NULL)) {
  171. spin_lock_irqsave(&base->cpu_base->lock, *flags);
  172. if (likely(base == timer->base))
  173. return base;
  174. /* The timer has migrated to another CPU: */
  175. spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
  176. }
  177. cpu_relax();
  178. }
  179. }
  180. /*
  181. * Switch the timer base to the current CPU when possible.
  182. */
  183. static inline struct hrtimer_clock_base *
  184. switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
  185. {
  186. struct hrtimer_clock_base *new_base;
  187. struct hrtimer_cpu_base *new_cpu_base;
  188. new_cpu_base = &__get_cpu_var(hrtimer_bases);
  189. new_base = &new_cpu_base->clock_base[base->index];
  190. if (base != new_base) {
  191. /*
  192. * We are trying to schedule the timer on the local CPU.
  193. * However we can't change timer's base while it is running,
  194. * so we keep it on the same CPU. No hassle vs. reprogramming
  195. * the event source in the high resolution case. The softirq
  196. * code will take care of this when the timer function has
  197. * completed. There is no conflict as we hold the lock until
  198. * the timer is enqueued.
  199. */
  200. if (unlikely(hrtimer_callback_running(timer)))
  201. return base;
  202. /* See the comment in lock_timer_base() */
  203. timer->base = NULL;
  204. spin_unlock(&base->cpu_base->lock);
  205. spin_lock(&new_base->cpu_base->lock);
  206. timer->base = new_base;
  207. }
  208. return new_base;
  209. }
  210. #else /* CONFIG_SMP */
  211. static inline struct hrtimer_clock_base *
  212. lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  213. {
  214. struct hrtimer_clock_base *base = timer->base;
  215. spin_lock_irqsave(&base->cpu_base->lock, *flags);
  216. return base;
  217. }
  218. # define switch_hrtimer_base(t, b) (b)
  219. #endif /* !CONFIG_SMP */
  220. /*
  221. * Functions for the union type storage format of ktime_t which are
  222. * too large for inlining:
  223. */
  224. #if BITS_PER_LONG < 64
  225. # ifndef CONFIG_KTIME_SCALAR
  226. /**
  227. * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
  228. * @kt: addend
  229. * @nsec: the scalar nsec value to add
  230. *
  231. * Returns the sum of kt and nsec in ktime_t format
  232. */
  233. ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
  234. {
  235. ktime_t tmp;
  236. if (likely(nsec < NSEC_PER_SEC)) {
  237. tmp.tv64 = nsec;
  238. } else {
  239. unsigned long rem = do_div(nsec, NSEC_PER_SEC);
  240. tmp = ktime_set((long)nsec, rem);
  241. }
  242. return ktime_add(kt, tmp);
  243. }
  244. # endif /* !CONFIG_KTIME_SCALAR */
  245. /*
  246. * Divide a ktime value by a nanosecond value
  247. */
  248. unsigned long ktime_divns(const ktime_t kt, s64 div)
  249. {
  250. u64 dclc, inc, dns;
  251. int sft = 0;
  252. dclc = dns = ktime_to_ns(kt);
  253. inc = div;
  254. /* Make sure the divisor is less than 2^32: */
  255. while (div >> 32) {
  256. sft++;
  257. div >>= 1;
  258. }
  259. dclc >>= sft;
  260. do_div(dclc, (unsigned long) div);
  261. return (unsigned long) dclc;
  262. }
  263. #endif /* BITS_PER_LONG >= 64 */
  264. /* High resolution timer related functions */
  265. #ifdef CONFIG_HIGH_RES_TIMERS
  266. /*
  267. * High resolution timer enabled ?
  268. */
  269. static int hrtimer_hres_enabled __read_mostly = 1;
  270. /*
  271. * Enable / Disable high resolution mode
  272. */
  273. static int __init setup_hrtimer_hres(char *str)
  274. {
  275. if (!strcmp(str, "off"))
  276. hrtimer_hres_enabled = 0;
  277. else if (!strcmp(str, "on"))
  278. hrtimer_hres_enabled = 1;
  279. else
  280. return 0;
  281. return 1;
  282. }
  283. __setup("highres=", setup_hrtimer_hres);
  284. /*
  285. * hrtimer_high_res_enabled - query, if the highres mode is enabled
  286. */
  287. static inline int hrtimer_is_hres_enabled(void)
  288. {
  289. return hrtimer_hres_enabled;
  290. }
  291. /*
  292. * Is the high resolution mode active ?
  293. */
  294. static inline int hrtimer_hres_active(void)
  295. {
  296. return __get_cpu_var(hrtimer_bases).hres_active;
  297. }
  298. /*
  299. * Reprogram the event source with checking both queues for the
  300. * next event
  301. * Called with interrupts disabled and base->lock held
  302. */
  303. static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
  304. {
  305. int i;
  306. struct hrtimer_clock_base *base = cpu_base->clock_base;
  307. ktime_t expires;
  308. cpu_base->expires_next.tv64 = KTIME_MAX;
  309. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  310. struct hrtimer *timer;
  311. if (!base->first)
  312. continue;
  313. timer = rb_entry(base->first, struct hrtimer, node);
  314. expires = ktime_sub(timer->expires, base->offset);
  315. if (expires.tv64 < cpu_base->expires_next.tv64)
  316. cpu_base->expires_next = expires;
  317. }
  318. if (cpu_base->expires_next.tv64 != KTIME_MAX)
  319. tick_program_event(cpu_base->expires_next, 1);
  320. }
  321. /*
  322. * Shared reprogramming for clock_realtime and clock_monotonic
  323. *
  324. * When a timer is enqueued and expires earlier than the already enqueued
  325. * timers, we have to check, whether it expires earlier than the timer for
  326. * which the clock event device was armed.
  327. *
  328. * Called with interrupts disabled and base->cpu_base.lock held
  329. */
  330. static int hrtimer_reprogram(struct hrtimer *timer,
  331. struct hrtimer_clock_base *base)
  332. {
  333. ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
  334. ktime_t expires = ktime_sub(timer->expires, base->offset);
  335. int res;
  336. /*
  337. * When the callback is running, we do not reprogram the clock event
  338. * device. The timer callback is either running on a different CPU or
  339. * the callback is executed in the hrtimer_interupt context. The
  340. * reprogramming is handled either by the softirq, which called the
  341. * callback or at the end of the hrtimer_interrupt.
  342. */
  343. if (hrtimer_callback_running(timer))
  344. return 0;
  345. if (expires.tv64 >= expires_next->tv64)
  346. return 0;
  347. /*
  348. * Clockevents returns -ETIME, when the event was in the past.
  349. */
  350. res = tick_program_event(expires, 0);
  351. if (!IS_ERR_VALUE(res))
  352. *expires_next = expires;
  353. return res;
  354. }
  355. /*
  356. * Retrigger next event is called after clock was set
  357. *
  358. * Called with interrupts disabled via on_each_cpu()
  359. */
  360. static void retrigger_next_event(void *arg)
  361. {
  362. struct hrtimer_cpu_base *base;
  363. struct timespec realtime_offset;
  364. unsigned long seq;
  365. if (!hrtimer_hres_active())
  366. return;
  367. do {
  368. seq = read_seqbegin(&xtime_lock);
  369. set_normalized_timespec(&realtime_offset,
  370. -wall_to_monotonic.tv_sec,
  371. -wall_to_monotonic.tv_nsec);
  372. } while (read_seqretry(&xtime_lock, seq));
  373. base = &__get_cpu_var(hrtimer_bases);
  374. /* Adjust CLOCK_REALTIME offset */
  375. spin_lock(&base->lock);
  376. base->clock_base[CLOCK_REALTIME].offset =
  377. timespec_to_ktime(realtime_offset);
  378. hrtimer_force_reprogram(base);
  379. spin_unlock(&base->lock);
  380. }
  381. /*
  382. * Clock realtime was set
  383. *
  384. * Change the offset of the realtime clock vs. the monotonic
  385. * clock.
  386. *
  387. * We might have to reprogram the high resolution timer interrupt. On
  388. * SMP we call the architecture specific code to retrigger _all_ high
  389. * resolution timer interrupts. On UP we just disable interrupts and
  390. * call the high resolution interrupt code.
  391. */
  392. void clock_was_set(void)
  393. {
  394. /* Retrigger the CPU local events everywhere */
  395. on_each_cpu(retrigger_next_event, NULL, 0, 1);
  396. }
  397. /*
  398. * Check, whether the timer is on the callback pending list
  399. */
  400. static inline int hrtimer_cb_pending(const struct hrtimer *timer)
  401. {
  402. return timer->state & HRTIMER_STATE_PENDING;
  403. }
  404. /*
  405. * Remove a timer from the callback pending list
  406. */
  407. static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
  408. {
  409. list_del_init(&timer->cb_entry);
  410. }
  411. /*
  412. * Initialize the high resolution related parts of cpu_base
  413. */
  414. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
  415. {
  416. base->expires_next.tv64 = KTIME_MAX;
  417. base->hres_active = 0;
  418. INIT_LIST_HEAD(&base->cb_pending);
  419. }
  420. /*
  421. * Initialize the high resolution related parts of a hrtimer
  422. */
  423. static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
  424. {
  425. INIT_LIST_HEAD(&timer->cb_entry);
  426. }
  427. /*
  428. * When High resolution timers are active, try to reprogram. Note, that in case
  429. * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
  430. * check happens. The timer gets enqueued into the rbtree. The reprogramming
  431. * and expiry check is done in the hrtimer_interrupt or in the softirq.
  432. */
  433. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  434. struct hrtimer_clock_base *base)
  435. {
  436. if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
  437. /* Timer is expired, act upon the callback mode */
  438. switch(timer->cb_mode) {
  439. case HRTIMER_CB_IRQSAFE_NO_RESTART:
  440. /*
  441. * We can call the callback from here. No restart
  442. * happens, so no danger of recursion
  443. */
  444. BUG_ON(timer->function(timer) != HRTIMER_NORESTART);
  445. return 1;
  446. case HRTIMER_CB_IRQSAFE_NO_SOFTIRQ:
  447. /*
  448. * This is solely for the sched tick emulation with
  449. * dynamic tick support to ensure that we do not
  450. * restart the tick right on the edge and end up with
  451. * the tick timer in the softirq ! The calling site
  452. * takes care of this.
  453. */
  454. return 1;
  455. case HRTIMER_CB_IRQSAFE:
  456. case HRTIMER_CB_SOFTIRQ:
  457. /*
  458. * Move everything else into the softirq pending list !
  459. */
  460. list_add_tail(&timer->cb_entry,
  461. &base->cpu_base->cb_pending);
  462. timer->state = HRTIMER_STATE_PENDING;
  463. raise_softirq(HRTIMER_SOFTIRQ);
  464. return 1;
  465. default:
  466. BUG();
  467. }
  468. }
  469. return 0;
  470. }
  471. /*
  472. * Switch to high resolution mode
  473. */
  474. static int hrtimer_switch_to_hres(void)
  475. {
  476. struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
  477. unsigned long flags;
  478. if (base->hres_active)
  479. return 1;
  480. local_irq_save(flags);
  481. if (tick_init_highres()) {
  482. local_irq_restore(flags);
  483. return 0;
  484. }
  485. base->hres_active = 1;
  486. base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
  487. base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
  488. tick_setup_sched_timer();
  489. /* "Retrigger" the interrupt to get things going */
  490. retrigger_next_event(NULL);
  491. local_irq_restore(flags);
  492. printk(KERN_INFO "Switched to high resolution mode on CPU %d\n",
  493. smp_processor_id());
  494. return 1;
  495. }
  496. #else
  497. static inline int hrtimer_hres_active(void) { return 0; }
  498. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  499. static inline int hrtimer_switch_to_hres(void) { return 0; }
  500. static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
  501. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  502. struct hrtimer_clock_base *base)
  503. {
  504. return 0;
  505. }
  506. static inline int hrtimer_cb_pending(struct hrtimer *timer) { return 0; }
  507. static inline void hrtimer_remove_cb_pending(struct hrtimer *timer) { }
  508. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  509. static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
  510. #endif /* CONFIG_HIGH_RES_TIMERS */
  511. #ifdef CONFIG_TIMER_STATS
  512. void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
  513. {
  514. if (timer->start_site)
  515. return;
  516. timer->start_site = addr;
  517. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  518. timer->start_pid = current->pid;
  519. }
  520. #endif
  521. /*
  522. * Counterpart to lock_timer_base above:
  523. */
  524. static inline
  525. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  526. {
  527. spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  528. }
  529. /**
  530. * hrtimer_forward - forward the timer expiry
  531. * @timer: hrtimer to forward
  532. * @now: forward past this time
  533. * @interval: the interval to forward
  534. *
  535. * Forward the timer expiry so it will expire in the future.
  536. * Returns the number of overruns.
  537. */
  538. unsigned long
  539. hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  540. {
  541. unsigned long orun = 1;
  542. ktime_t delta;
  543. delta = ktime_sub(now, timer->expires);
  544. if (delta.tv64 < 0)
  545. return 0;
  546. if (interval.tv64 < timer->base->resolution.tv64)
  547. interval.tv64 = timer->base->resolution.tv64;
  548. if (unlikely(delta.tv64 >= interval.tv64)) {
  549. s64 incr = ktime_to_ns(interval);
  550. orun = ktime_divns(delta, incr);
  551. timer->expires = ktime_add_ns(timer->expires, incr * orun);
  552. if (timer->expires.tv64 > now.tv64)
  553. return orun;
  554. /*
  555. * This (and the ktime_add() below) is the
  556. * correction for exact:
  557. */
  558. orun++;
  559. }
  560. timer->expires = ktime_add(timer->expires, interval);
  561. return orun;
  562. }
  563. /*
  564. * enqueue_hrtimer - internal function to (re)start a timer
  565. *
  566. * The timer is inserted in expiry order. Insertion into the
  567. * red black tree is O(log(n)). Must hold the base lock.
  568. */
  569. static void enqueue_hrtimer(struct hrtimer *timer,
  570. struct hrtimer_clock_base *base, int reprogram)
  571. {
  572. struct rb_node **link = &base->active.rb_node;
  573. struct rb_node *parent = NULL;
  574. struct hrtimer *entry;
  575. /*
  576. * Find the right place in the rbtree:
  577. */
  578. while (*link) {
  579. parent = *link;
  580. entry = rb_entry(parent, struct hrtimer, node);
  581. /*
  582. * We dont care about collisions. Nodes with
  583. * the same expiry time stay together.
  584. */
  585. if (timer->expires.tv64 < entry->expires.tv64)
  586. link = &(*link)->rb_left;
  587. else
  588. link = &(*link)->rb_right;
  589. }
  590. /*
  591. * Insert the timer to the rbtree and check whether it
  592. * replaces the first pending timer
  593. */
  594. if (!base->first || timer->expires.tv64 <
  595. rb_entry(base->first, struct hrtimer, node)->expires.tv64) {
  596. /*
  597. * Reprogram the clock event device. When the timer is already
  598. * expired hrtimer_enqueue_reprogram has either called the
  599. * callback or added it to the pending list and raised the
  600. * softirq.
  601. *
  602. * This is a NOP for !HIGHRES
  603. */
  604. if (reprogram && hrtimer_enqueue_reprogram(timer, base))
  605. return;
  606. base->first = &timer->node;
  607. }
  608. rb_link_node(&timer->node, parent, link);
  609. rb_insert_color(&timer->node, &base->active);
  610. /*
  611. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  612. * state of a possibly running callback.
  613. */
  614. timer->state |= HRTIMER_STATE_ENQUEUED;
  615. }
  616. /*
  617. * __remove_hrtimer - internal function to remove a timer
  618. *
  619. * Caller must hold the base lock.
  620. *
  621. * High resolution timer mode reprograms the clock event device when the
  622. * timer is the one which expires next. The caller can disable this by setting
  623. * reprogram to zero. This is useful, when the context does a reprogramming
  624. * anyway (e.g. timer interrupt)
  625. */
  626. static void __remove_hrtimer(struct hrtimer *timer,
  627. struct hrtimer_clock_base *base,
  628. unsigned long newstate, int reprogram)
  629. {
  630. /* High res. callback list. NOP for !HIGHRES */
  631. if (hrtimer_cb_pending(timer))
  632. hrtimer_remove_cb_pending(timer);
  633. else {
  634. /*
  635. * Remove the timer from the rbtree and replace the
  636. * first entry pointer if necessary.
  637. */
  638. if (base->first == &timer->node) {
  639. base->first = rb_next(&timer->node);
  640. /* Reprogram the clock event device. if enabled */
  641. if (reprogram && hrtimer_hres_active())
  642. hrtimer_force_reprogram(base->cpu_base);
  643. }
  644. rb_erase(&timer->node, &base->active);
  645. }
  646. timer->state = newstate;
  647. }
  648. /*
  649. * remove hrtimer, called with base lock held
  650. */
  651. static inline int
  652. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  653. {
  654. if (hrtimer_is_queued(timer)) {
  655. int reprogram;
  656. /*
  657. * Remove the timer and force reprogramming when high
  658. * resolution mode is active and the timer is on the current
  659. * CPU. If we remove a timer on another CPU, reprogramming is
  660. * skipped. The interrupt event on this CPU is fired and
  661. * reprogramming happens in the interrupt handler. This is a
  662. * rare case and less expensive than a smp call.
  663. */
  664. timer_stats_hrtimer_clear_start_info(timer);
  665. reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
  666. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
  667. reprogram);
  668. return 1;
  669. }
  670. return 0;
  671. }
  672. /**
  673. * hrtimer_start - (re)start an relative timer on the current CPU
  674. * @timer: the timer to be added
  675. * @tim: expiry time
  676. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  677. *
  678. * Returns:
  679. * 0 on success
  680. * 1 when the timer was active
  681. */
  682. int
  683. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  684. {
  685. struct hrtimer_clock_base *base, *new_base;
  686. unsigned long flags;
  687. int ret;
  688. base = lock_hrtimer_base(timer, &flags);
  689. /* Remove an active timer from the queue: */
  690. ret = remove_hrtimer(timer, base);
  691. /* Switch the timer base, if necessary: */
  692. new_base = switch_hrtimer_base(timer, base);
  693. if (mode == HRTIMER_MODE_REL) {
  694. tim = ktime_add(tim, new_base->get_time());
  695. /*
  696. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  697. * to signal that they simply return xtime in
  698. * do_gettimeoffset(). In this case we want to round up by
  699. * resolution when starting a relative timer, to avoid short
  700. * timeouts. This will go away with the GTOD framework.
  701. */
  702. #ifdef CONFIG_TIME_LOW_RES
  703. tim = ktime_add(tim, base->resolution);
  704. #endif
  705. }
  706. timer->expires = tim;
  707. timer_stats_hrtimer_set_start_info(timer);
  708. enqueue_hrtimer(timer, new_base, base == new_base);
  709. unlock_hrtimer_base(timer, &flags);
  710. return ret;
  711. }
  712. EXPORT_SYMBOL_GPL(hrtimer_start);
  713. /**
  714. * hrtimer_try_to_cancel - try to deactivate a timer
  715. * @timer: hrtimer to stop
  716. *
  717. * Returns:
  718. * 0 when the timer was not active
  719. * 1 when the timer was active
  720. * -1 when the timer is currently excuting the callback function and
  721. * cannot be stopped
  722. */
  723. int hrtimer_try_to_cancel(struct hrtimer *timer)
  724. {
  725. struct hrtimer_clock_base *base;
  726. unsigned long flags;
  727. int ret = -1;
  728. base = lock_hrtimer_base(timer, &flags);
  729. if (!hrtimer_callback_running(timer))
  730. ret = remove_hrtimer(timer, base);
  731. unlock_hrtimer_base(timer, &flags);
  732. return ret;
  733. }
  734. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  735. /**
  736. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  737. * @timer: the timer to be cancelled
  738. *
  739. * Returns:
  740. * 0 when the timer was not active
  741. * 1 when the timer was active
  742. */
  743. int hrtimer_cancel(struct hrtimer *timer)
  744. {
  745. for (;;) {
  746. int ret = hrtimer_try_to_cancel(timer);
  747. if (ret >= 0)
  748. return ret;
  749. cpu_relax();
  750. }
  751. }
  752. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  753. /**
  754. * hrtimer_get_remaining - get remaining time for the timer
  755. * @timer: the timer to read
  756. */
  757. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  758. {
  759. struct hrtimer_clock_base *base;
  760. unsigned long flags;
  761. ktime_t rem;
  762. base = lock_hrtimer_base(timer, &flags);
  763. rem = ktime_sub(timer->expires, base->get_time());
  764. unlock_hrtimer_base(timer, &flags);
  765. return rem;
  766. }
  767. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  768. #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
  769. /**
  770. * hrtimer_get_next_event - get the time until next expiry event
  771. *
  772. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  773. * is pending.
  774. */
  775. ktime_t hrtimer_get_next_event(void)
  776. {
  777. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  778. struct hrtimer_clock_base *base = cpu_base->clock_base;
  779. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  780. unsigned long flags;
  781. int i;
  782. spin_lock_irqsave(&cpu_base->lock, flags);
  783. if (!hrtimer_hres_active()) {
  784. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  785. struct hrtimer *timer;
  786. if (!base->first)
  787. continue;
  788. timer = rb_entry(base->first, struct hrtimer, node);
  789. delta.tv64 = timer->expires.tv64;
  790. delta = ktime_sub(delta, base->get_time());
  791. if (delta.tv64 < mindelta.tv64)
  792. mindelta.tv64 = delta.tv64;
  793. }
  794. }
  795. spin_unlock_irqrestore(&cpu_base->lock, flags);
  796. if (mindelta.tv64 < 0)
  797. mindelta.tv64 = 0;
  798. return mindelta;
  799. }
  800. #endif
  801. /**
  802. * hrtimer_init - initialize a timer to the given clock
  803. * @timer: the timer to be initialized
  804. * @clock_id: the clock to be used
  805. * @mode: timer mode abs/rel
  806. */
  807. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  808. enum hrtimer_mode mode)
  809. {
  810. struct hrtimer_cpu_base *cpu_base;
  811. memset(timer, 0, sizeof(struct hrtimer));
  812. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  813. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  814. clock_id = CLOCK_MONOTONIC;
  815. timer->base = &cpu_base->clock_base[clock_id];
  816. hrtimer_init_timer_hres(timer);
  817. #ifdef CONFIG_TIMER_STATS
  818. timer->start_site = NULL;
  819. timer->start_pid = -1;
  820. memset(timer->start_comm, 0, TASK_COMM_LEN);
  821. #endif
  822. }
  823. EXPORT_SYMBOL_GPL(hrtimer_init);
  824. /**
  825. * hrtimer_get_res - get the timer resolution for a clock
  826. * @which_clock: which clock to query
  827. * @tp: pointer to timespec variable to store the resolution
  828. *
  829. * Store the resolution of the clock selected by @which_clock in the
  830. * variable pointed to by @tp.
  831. */
  832. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  833. {
  834. struct hrtimer_cpu_base *cpu_base;
  835. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  836. *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
  837. return 0;
  838. }
  839. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  840. #ifdef CONFIG_HIGH_RES_TIMERS
  841. /*
  842. * High resolution timer interrupt
  843. * Called with interrupts disabled
  844. */
  845. void hrtimer_interrupt(struct clock_event_device *dev)
  846. {
  847. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  848. struct hrtimer_clock_base *base;
  849. ktime_t expires_next, now;
  850. int i, raise = 0;
  851. BUG_ON(!cpu_base->hres_active);
  852. cpu_base->nr_events++;
  853. dev->next_event.tv64 = KTIME_MAX;
  854. retry:
  855. now = ktime_get();
  856. expires_next.tv64 = KTIME_MAX;
  857. base = cpu_base->clock_base;
  858. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  859. ktime_t basenow;
  860. struct rb_node *node;
  861. spin_lock(&cpu_base->lock);
  862. basenow = ktime_add(now, base->offset);
  863. while ((node = base->first)) {
  864. struct hrtimer *timer;
  865. timer = rb_entry(node, struct hrtimer, node);
  866. if (basenow.tv64 < timer->expires.tv64) {
  867. ktime_t expires;
  868. expires = ktime_sub(timer->expires,
  869. base->offset);
  870. if (expires.tv64 < expires_next.tv64)
  871. expires_next = expires;
  872. break;
  873. }
  874. /* Move softirq callbacks to the pending list */
  875. if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
  876. __remove_hrtimer(timer, base,
  877. HRTIMER_STATE_PENDING, 0);
  878. list_add_tail(&timer->cb_entry,
  879. &base->cpu_base->cb_pending);
  880. raise = 1;
  881. continue;
  882. }
  883. __remove_hrtimer(timer, base,
  884. HRTIMER_STATE_CALLBACK, 0);
  885. timer_stats_account_hrtimer(timer);
  886. /*
  887. * Note: We clear the CALLBACK bit after
  888. * enqueue_hrtimer to avoid reprogramming of
  889. * the event hardware. This happens at the end
  890. * of this function anyway.
  891. */
  892. if (timer->function(timer) != HRTIMER_NORESTART) {
  893. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  894. enqueue_hrtimer(timer, base, 0);
  895. }
  896. timer->state &= ~HRTIMER_STATE_CALLBACK;
  897. }
  898. spin_unlock(&cpu_base->lock);
  899. base++;
  900. }
  901. cpu_base->expires_next = expires_next;
  902. /* Reprogramming necessary ? */
  903. if (expires_next.tv64 != KTIME_MAX) {
  904. if (tick_program_event(expires_next, 0))
  905. goto retry;
  906. }
  907. /* Raise softirq ? */
  908. if (raise)
  909. raise_softirq(HRTIMER_SOFTIRQ);
  910. }
  911. static void run_hrtimer_softirq(struct softirq_action *h)
  912. {
  913. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  914. spin_lock_irq(&cpu_base->lock);
  915. while (!list_empty(&cpu_base->cb_pending)) {
  916. enum hrtimer_restart (*fn)(struct hrtimer *);
  917. struct hrtimer *timer;
  918. int restart;
  919. timer = list_entry(cpu_base->cb_pending.next,
  920. struct hrtimer, cb_entry);
  921. timer_stats_account_hrtimer(timer);
  922. fn = timer->function;
  923. __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
  924. spin_unlock_irq(&cpu_base->lock);
  925. restart = fn(timer);
  926. spin_lock_irq(&cpu_base->lock);
  927. timer->state &= ~HRTIMER_STATE_CALLBACK;
  928. if (restart == HRTIMER_RESTART) {
  929. BUG_ON(hrtimer_active(timer));
  930. /*
  931. * Enqueue the timer, allow reprogramming of the event
  932. * device
  933. */
  934. enqueue_hrtimer(timer, timer->base, 1);
  935. } else if (hrtimer_active(timer)) {
  936. /*
  937. * If the timer was rearmed on another CPU, reprogram
  938. * the event device.
  939. */
  940. if (timer->base->first == &timer->node)
  941. hrtimer_reprogram(timer, timer->base);
  942. }
  943. }
  944. spin_unlock_irq(&cpu_base->lock);
  945. }
  946. #endif /* CONFIG_HIGH_RES_TIMERS */
  947. /*
  948. * Expire the per base hrtimer-queue:
  949. */
  950. static inline void run_hrtimer_queue(struct hrtimer_cpu_base *cpu_base,
  951. int index)
  952. {
  953. struct rb_node *node;
  954. struct hrtimer_clock_base *base = &cpu_base->clock_base[index];
  955. if (!base->first)
  956. return;
  957. if (base->get_softirq_time)
  958. base->softirq_time = base->get_softirq_time();
  959. spin_lock_irq(&cpu_base->lock);
  960. while ((node = base->first)) {
  961. struct hrtimer *timer;
  962. enum hrtimer_restart (*fn)(struct hrtimer *);
  963. int restart;
  964. timer = rb_entry(node, struct hrtimer, node);
  965. if (base->softirq_time.tv64 <= timer->expires.tv64)
  966. break;
  967. #ifdef CONFIG_HIGH_RES_TIMERS
  968. WARN_ON_ONCE(timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ);
  969. #endif
  970. timer_stats_account_hrtimer(timer);
  971. fn = timer->function;
  972. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  973. spin_unlock_irq(&cpu_base->lock);
  974. restart = fn(timer);
  975. spin_lock_irq(&cpu_base->lock);
  976. timer->state &= ~HRTIMER_STATE_CALLBACK;
  977. if (restart != HRTIMER_NORESTART) {
  978. BUG_ON(hrtimer_active(timer));
  979. enqueue_hrtimer(timer, base, 0);
  980. }
  981. }
  982. spin_unlock_irq(&cpu_base->lock);
  983. }
  984. /*
  985. * Called from timer softirq every jiffy, expire hrtimers:
  986. *
  987. * For HRT its the fall back code to run the softirq in the timer
  988. * softirq context in case the hrtimer initialization failed or has
  989. * not been done yet.
  990. */
  991. void hrtimer_run_queues(void)
  992. {
  993. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  994. int i;
  995. if (hrtimer_hres_active())
  996. return;
  997. /*
  998. * This _is_ ugly: We have to check in the softirq context,
  999. * whether we can switch to highres and / or nohz mode. The
  1000. * clocksource switch happens in the timer interrupt with
  1001. * xtime_lock held. Notification from there only sets the
  1002. * check bit in the tick_oneshot code, otherwise we might
  1003. * deadlock vs. xtime_lock.
  1004. */
  1005. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
  1006. if (hrtimer_switch_to_hres())
  1007. return;
  1008. hrtimer_get_softirq_time(cpu_base);
  1009. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  1010. run_hrtimer_queue(cpu_base, i);
  1011. }
  1012. /*
  1013. * Sleep related functions:
  1014. */
  1015. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1016. {
  1017. struct hrtimer_sleeper *t =
  1018. container_of(timer, struct hrtimer_sleeper, timer);
  1019. struct task_struct *task = t->task;
  1020. t->task = NULL;
  1021. if (task)
  1022. wake_up_process(task);
  1023. return HRTIMER_NORESTART;
  1024. }
  1025. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1026. {
  1027. sl->timer.function = hrtimer_wakeup;
  1028. sl->task = task;
  1029. #ifdef CONFIG_HIGH_RES_TIMERS
  1030. sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_RESTART;
  1031. #endif
  1032. }
  1033. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1034. {
  1035. hrtimer_init_sleeper(t, current);
  1036. do {
  1037. set_current_state(TASK_INTERRUPTIBLE);
  1038. hrtimer_start(&t->timer, t->timer.expires, mode);
  1039. if (likely(t->task))
  1040. schedule();
  1041. hrtimer_cancel(&t->timer);
  1042. mode = HRTIMER_MODE_ABS;
  1043. } while (t->task && !signal_pending(current));
  1044. return t->task == NULL;
  1045. }
  1046. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1047. {
  1048. struct hrtimer_sleeper t;
  1049. struct timespec __user *rmtp;
  1050. struct timespec tu;
  1051. ktime_t time;
  1052. restart->fn = do_no_restart_syscall;
  1053. hrtimer_init(&t.timer, restart->arg0, HRTIMER_MODE_ABS);
  1054. t.timer.expires.tv64 = ((u64)restart->arg3 << 32) | (u64) restart->arg2;
  1055. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  1056. return 0;
  1057. rmtp = (struct timespec __user *) restart->arg1;
  1058. if (rmtp) {
  1059. time = ktime_sub(t.timer.expires, t.timer.base->get_time());
  1060. if (time.tv64 <= 0)
  1061. return 0;
  1062. tu = ktime_to_timespec(time);
  1063. if (copy_to_user(rmtp, &tu, sizeof(tu)))
  1064. return -EFAULT;
  1065. }
  1066. restart->fn = hrtimer_nanosleep_restart;
  1067. /* The other values in restart are already filled in */
  1068. return -ERESTART_RESTARTBLOCK;
  1069. }
  1070. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  1071. const enum hrtimer_mode mode, const clockid_t clockid)
  1072. {
  1073. struct restart_block *restart;
  1074. struct hrtimer_sleeper t;
  1075. struct timespec tu;
  1076. ktime_t rem;
  1077. hrtimer_init(&t.timer, clockid, mode);
  1078. t.timer.expires = timespec_to_ktime(*rqtp);
  1079. if (do_nanosleep(&t, mode))
  1080. return 0;
  1081. /* Absolute timers do not update the rmtp value and restart: */
  1082. if (mode == HRTIMER_MODE_ABS)
  1083. return -ERESTARTNOHAND;
  1084. if (rmtp) {
  1085. rem = ktime_sub(t.timer.expires, t.timer.base->get_time());
  1086. if (rem.tv64 <= 0)
  1087. return 0;
  1088. tu = ktime_to_timespec(rem);
  1089. if (copy_to_user(rmtp, &tu, sizeof(tu)))
  1090. return -EFAULT;
  1091. }
  1092. restart = &current_thread_info()->restart_block;
  1093. restart->fn = hrtimer_nanosleep_restart;
  1094. restart->arg0 = (unsigned long) t.timer.base->index;
  1095. restart->arg1 = (unsigned long) rmtp;
  1096. restart->arg2 = t.timer.expires.tv64 & 0xFFFFFFFF;
  1097. restart->arg3 = t.timer.expires.tv64 >> 32;
  1098. return -ERESTART_RESTARTBLOCK;
  1099. }
  1100. asmlinkage long
  1101. sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
  1102. {
  1103. struct timespec tu;
  1104. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  1105. return -EFAULT;
  1106. if (!timespec_valid(&tu))
  1107. return -EINVAL;
  1108. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1109. }
  1110. /*
  1111. * Functions related to boot-time initialization:
  1112. */
  1113. static void __devinit init_hrtimers_cpu(int cpu)
  1114. {
  1115. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1116. int i;
  1117. spin_lock_init(&cpu_base->lock);
  1118. lockdep_set_class(&cpu_base->lock, &cpu_base->lock_key);
  1119. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  1120. cpu_base->clock_base[i].cpu_base = cpu_base;
  1121. hrtimer_init_hres(cpu_base);
  1122. }
  1123. #ifdef CONFIG_HOTPLUG_CPU
  1124. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1125. struct hrtimer_clock_base *new_base)
  1126. {
  1127. struct hrtimer *timer;
  1128. struct rb_node *node;
  1129. while ((node = rb_first(&old_base->active))) {
  1130. timer = rb_entry(node, struct hrtimer, node);
  1131. BUG_ON(hrtimer_callback_running(timer));
  1132. __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE, 0);
  1133. timer->base = new_base;
  1134. /*
  1135. * Enqueue the timer. Allow reprogramming of the event device
  1136. */
  1137. enqueue_hrtimer(timer, new_base, 1);
  1138. }
  1139. }
  1140. static void migrate_hrtimers(int cpu)
  1141. {
  1142. struct hrtimer_cpu_base *old_base, *new_base;
  1143. int i;
  1144. BUG_ON(cpu_online(cpu));
  1145. old_base = &per_cpu(hrtimer_bases, cpu);
  1146. new_base = &get_cpu_var(hrtimer_bases);
  1147. tick_cancel_sched_timer(cpu);
  1148. local_irq_disable();
  1149. double_spin_lock(&new_base->lock, &old_base->lock,
  1150. smp_processor_id() < cpu);
  1151. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1152. migrate_hrtimer_list(&old_base->clock_base[i],
  1153. &new_base->clock_base[i]);
  1154. }
  1155. double_spin_unlock(&new_base->lock, &old_base->lock,
  1156. smp_processor_id() < cpu);
  1157. local_irq_enable();
  1158. put_cpu_var(hrtimer_bases);
  1159. }
  1160. #endif /* CONFIG_HOTPLUG_CPU */
  1161. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  1162. unsigned long action, void *hcpu)
  1163. {
  1164. long cpu = (long)hcpu;
  1165. switch (action) {
  1166. case CPU_UP_PREPARE:
  1167. init_hrtimers_cpu(cpu);
  1168. break;
  1169. #ifdef CONFIG_HOTPLUG_CPU
  1170. case CPU_DEAD:
  1171. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
  1172. migrate_hrtimers(cpu);
  1173. break;
  1174. #endif
  1175. default:
  1176. break;
  1177. }
  1178. return NOTIFY_OK;
  1179. }
  1180. static struct notifier_block __cpuinitdata hrtimers_nb = {
  1181. .notifier_call = hrtimer_cpu_notify,
  1182. };
  1183. void __init hrtimers_init(void)
  1184. {
  1185. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1186. (void *)(long)smp_processor_id());
  1187. register_cpu_notifier(&hrtimers_nb);
  1188. #ifdef CONFIG_HIGH_RES_TIMERS
  1189. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq, NULL);
  1190. #endif
  1191. }