hrtimer.c 35 KB

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