hrtimer.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * linux/kernel/hrtimer.c
  3. *
  4. * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
  6. *
  7. * High-resolution kernel timers
  8. *
  9. * In contrast to the low-resolution timeout API implemented in
  10. * kernel/timer.c, hrtimers provide finer resolution and accuracy
  11. * depending on system configuration and capabilities.
  12. *
  13. * These timers are currently used for:
  14. * - itimers
  15. * - POSIX timers
  16. * - nanosleep
  17. * - precise in-kernel timing
  18. *
  19. * Started by: Thomas Gleixner and Ingo Molnar
  20. *
  21. * Credits:
  22. * based on kernel/timer.c
  23. *
  24. * For licencing details see kernel-base/COPYING
  25. */
  26. #include <linux/cpu.h>
  27. #include <linux/module.h>
  28. #include <linux/percpu.h>
  29. #include <linux/hrtimer.h>
  30. #include <linux/notifier.h>
  31. #include <linux/syscalls.h>
  32. #include <linux/interrupt.h>
  33. #include <asm/uaccess.h>
  34. /**
  35. * ktime_get - get the monotonic time in ktime_t format
  36. *
  37. * returns the time in ktime_t format
  38. */
  39. static ktime_t ktime_get(void)
  40. {
  41. struct timespec now;
  42. ktime_get_ts(&now);
  43. return timespec_to_ktime(now);
  44. }
  45. /**
  46. * ktime_get_real - get the real (wall-) time in ktime_t format
  47. *
  48. * returns the time in ktime_t format
  49. */
  50. static ktime_t ktime_get_real(void)
  51. {
  52. struct timespec now;
  53. getnstimeofday(&now);
  54. return timespec_to_ktime(now);
  55. }
  56. EXPORT_SYMBOL_GPL(ktime_get_real);
  57. /*
  58. * The timer bases:
  59. */
  60. #define MAX_HRTIMER_BASES 2
  61. static DEFINE_PER_CPU(struct hrtimer_base, hrtimer_bases[MAX_HRTIMER_BASES]) =
  62. {
  63. {
  64. .index = CLOCK_REALTIME,
  65. .get_time = &ktime_get_real,
  66. .resolution = KTIME_REALTIME_RES,
  67. },
  68. {
  69. .index = CLOCK_MONOTONIC,
  70. .get_time = &ktime_get,
  71. .resolution = KTIME_MONOTONIC_RES,
  72. },
  73. };
  74. /**
  75. * ktime_get_ts - get the monotonic clock in timespec format
  76. *
  77. * @ts: pointer to timespec variable
  78. *
  79. * The function calculates the monotonic clock from the realtime
  80. * clock and the wall_to_monotonic offset and stores the result
  81. * in normalized timespec format in the variable pointed to by ts.
  82. */
  83. void ktime_get_ts(struct timespec *ts)
  84. {
  85. struct timespec tomono;
  86. unsigned long seq;
  87. do {
  88. seq = read_seqbegin(&xtime_lock);
  89. getnstimeofday(ts);
  90. tomono = wall_to_monotonic;
  91. } while (read_seqretry(&xtime_lock, seq));
  92. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
  93. ts->tv_nsec + tomono.tv_nsec);
  94. }
  95. EXPORT_SYMBOL_GPL(ktime_get_ts);
  96. /*
  97. * Functions and macros which are different for UP/SMP systems are kept in a
  98. * single place
  99. */
  100. #ifdef CONFIG_SMP
  101. #define set_curr_timer(b, t) do { (b)->curr_timer = (t); } while (0)
  102. /*
  103. * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
  104. * means that all timers which are tied to this base via timer->base are
  105. * locked, and the base itself is locked too.
  106. *
  107. * So __run_timers/migrate_timers can safely modify all timers which could
  108. * be found on the lists/queues.
  109. *
  110. * When the timer's base is locked, and the timer removed from list, it is
  111. * possible to set timer->base = NULL and drop the lock: the timer remains
  112. * locked.
  113. */
  114. static struct hrtimer_base *lock_hrtimer_base(const struct hrtimer *timer,
  115. unsigned long *flags)
  116. {
  117. struct hrtimer_base *base;
  118. for (;;) {
  119. base = timer->base;
  120. if (likely(base != NULL)) {
  121. spin_lock_irqsave(&base->lock, *flags);
  122. if (likely(base == timer->base))
  123. return base;
  124. /* The timer has migrated to another CPU: */
  125. spin_unlock_irqrestore(&base->lock, *flags);
  126. }
  127. cpu_relax();
  128. }
  129. }
  130. /*
  131. * Switch the timer base to the current CPU when possible.
  132. */
  133. static inline struct hrtimer_base *
  134. switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_base *base)
  135. {
  136. struct hrtimer_base *new_base;
  137. new_base = &__get_cpu_var(hrtimer_bases[base->index]);
  138. if (base != new_base) {
  139. /*
  140. * We are trying to schedule the timer on the local CPU.
  141. * However we can't change timer's base while it is running,
  142. * so we keep it on the same CPU. No hassle vs. reprogramming
  143. * the event source in the high resolution case. The softirq
  144. * code will take care of this when the timer function has
  145. * completed. There is no conflict as we hold the lock until
  146. * the timer is enqueued.
  147. */
  148. if (unlikely(base->curr_timer == timer))
  149. return base;
  150. /* See the comment in lock_timer_base() */
  151. timer->base = NULL;
  152. spin_unlock(&base->lock);
  153. spin_lock(&new_base->lock);
  154. timer->base = new_base;
  155. }
  156. return new_base;
  157. }
  158. #else /* CONFIG_SMP */
  159. #define set_curr_timer(b, t) do { } while (0)
  160. static inline struct hrtimer_base *
  161. lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  162. {
  163. struct hrtimer_base *base = timer->base;
  164. spin_lock_irqsave(&base->lock, *flags);
  165. return base;
  166. }
  167. #define switch_hrtimer_base(t, b) (b)
  168. #endif /* !CONFIG_SMP */
  169. /*
  170. * Functions for the union type storage format of ktime_t which are
  171. * too large for inlining:
  172. */
  173. #if BITS_PER_LONG < 64
  174. # ifndef CONFIG_KTIME_SCALAR
  175. /**
  176. * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
  177. *
  178. * @kt: addend
  179. * @nsec: the scalar nsec value to add
  180. *
  181. * Returns the sum of kt and nsec in ktime_t format
  182. */
  183. ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
  184. {
  185. ktime_t tmp;
  186. if (likely(nsec < NSEC_PER_SEC)) {
  187. tmp.tv64 = nsec;
  188. } else {
  189. unsigned long rem = do_div(nsec, NSEC_PER_SEC);
  190. tmp = ktime_set((long)nsec, rem);
  191. }
  192. return ktime_add(kt, tmp);
  193. }
  194. #else /* CONFIG_KTIME_SCALAR */
  195. # endif /* !CONFIG_KTIME_SCALAR */
  196. /*
  197. * Divide a ktime value by a nanosecond value
  198. */
  199. static unsigned long ktime_divns(const ktime_t kt, nsec_t div)
  200. {
  201. u64 dclc, inc, dns;
  202. int sft = 0;
  203. dclc = dns = ktime_to_ns(kt);
  204. inc = div;
  205. /* Make sure the divisor is less than 2^32: */
  206. while (div >> 32) {
  207. sft++;
  208. div >>= 1;
  209. }
  210. dclc >>= sft;
  211. do_div(dclc, (unsigned long) div);
  212. return (unsigned long) dclc;
  213. }
  214. #else /* BITS_PER_LONG < 64 */
  215. # define ktime_divns(kt, div) (unsigned long)((kt).tv64 / (div))
  216. #endif /* BITS_PER_LONG >= 64 */
  217. /*
  218. * Counterpart to lock_timer_base above:
  219. */
  220. static inline
  221. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  222. {
  223. spin_unlock_irqrestore(&timer->base->lock, *flags);
  224. }
  225. /**
  226. * hrtimer_forward - forward the timer expiry
  227. *
  228. * @timer: hrtimer to forward
  229. * @interval: the interval to forward
  230. *
  231. * Forward the timer expiry so it will expire in the future.
  232. * The number of overruns is added to the overrun field.
  233. */
  234. unsigned long
  235. hrtimer_forward(struct hrtimer *timer, const ktime_t interval)
  236. {
  237. unsigned long orun = 1;
  238. ktime_t delta, now;
  239. now = timer->base->get_time();
  240. delta = ktime_sub(now, timer->expires);
  241. if (delta.tv64 < 0)
  242. return 0;
  243. if (unlikely(delta.tv64 >= interval.tv64)) {
  244. nsec_t incr = ktime_to_ns(interval);
  245. orun = ktime_divns(delta, incr);
  246. timer->expires = ktime_add_ns(timer->expires, incr * orun);
  247. if (timer->expires.tv64 > now.tv64)
  248. return orun;
  249. /*
  250. * This (and the ktime_add() below) is the
  251. * correction for exact:
  252. */
  253. orun++;
  254. }
  255. timer->expires = ktime_add(timer->expires, interval);
  256. return orun;
  257. }
  258. /*
  259. * enqueue_hrtimer - internal function to (re)start a timer
  260. *
  261. * The timer is inserted in expiry order. Insertion into the
  262. * red black tree is O(log(n)). Must hold the base lock.
  263. */
  264. static void enqueue_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
  265. {
  266. struct rb_node **link = &base->active.rb_node;
  267. struct rb_node *parent = NULL;
  268. struct hrtimer *entry;
  269. /*
  270. * Find the right place in the rbtree:
  271. */
  272. while (*link) {
  273. parent = *link;
  274. entry = rb_entry(parent, struct hrtimer, node);
  275. /*
  276. * We dont care about collisions. Nodes with
  277. * the same expiry time stay together.
  278. */
  279. if (timer->expires.tv64 < entry->expires.tv64)
  280. link = &(*link)->rb_left;
  281. else
  282. link = &(*link)->rb_right;
  283. }
  284. /*
  285. * Insert the timer to the rbtree and check whether it
  286. * replaces the first pending timer
  287. */
  288. rb_link_node(&timer->node, parent, link);
  289. rb_insert_color(&timer->node, &base->active);
  290. timer->state = HRTIMER_PENDING;
  291. if (!base->first || timer->expires.tv64 <
  292. rb_entry(base->first, struct hrtimer, node)->expires.tv64)
  293. base->first = &timer->node;
  294. }
  295. /*
  296. * __remove_hrtimer - internal function to remove a timer
  297. *
  298. * Caller must hold the base lock.
  299. */
  300. static void __remove_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
  301. {
  302. /*
  303. * Remove the timer from the rbtree and replace the
  304. * first entry pointer if necessary.
  305. */
  306. if (base->first == &timer->node)
  307. base->first = rb_next(&timer->node);
  308. rb_erase(&timer->node, &base->active);
  309. }
  310. /*
  311. * remove hrtimer, called with base lock held
  312. */
  313. static inline int
  314. remove_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
  315. {
  316. if (hrtimer_active(timer)) {
  317. __remove_hrtimer(timer, base);
  318. timer->state = HRTIMER_INACTIVE;
  319. return 1;
  320. }
  321. return 0;
  322. }
  323. /**
  324. * hrtimer_start - (re)start an relative timer on the current CPU
  325. *
  326. * @timer: the timer to be added
  327. * @tim: expiry time
  328. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  329. *
  330. * Returns:
  331. * 0 on success
  332. * 1 when the timer was active
  333. */
  334. int
  335. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  336. {
  337. struct hrtimer_base *base, *new_base;
  338. unsigned long flags;
  339. int ret;
  340. base = lock_hrtimer_base(timer, &flags);
  341. /* Remove an active timer from the queue: */
  342. ret = remove_hrtimer(timer, base);
  343. /* Switch the timer base, if necessary: */
  344. new_base = switch_hrtimer_base(timer, base);
  345. if (mode == HRTIMER_REL)
  346. tim = ktime_add(tim, new_base->get_time());
  347. timer->expires = tim;
  348. enqueue_hrtimer(timer, new_base);
  349. unlock_hrtimer_base(timer, &flags);
  350. return ret;
  351. }
  352. /**
  353. * hrtimer_try_to_cancel - try to deactivate a timer
  354. *
  355. * @timer: hrtimer to stop
  356. *
  357. * Returns:
  358. * 0 when the timer was not active
  359. * 1 when the timer was active
  360. * -1 when the timer is currently excuting the callback function and
  361. * can not be stopped
  362. */
  363. int hrtimer_try_to_cancel(struct hrtimer *timer)
  364. {
  365. struct hrtimer_base *base;
  366. unsigned long flags;
  367. int ret = -1;
  368. base = lock_hrtimer_base(timer, &flags);
  369. if (base->curr_timer != timer)
  370. ret = remove_hrtimer(timer, base);
  371. unlock_hrtimer_base(timer, &flags);
  372. return ret;
  373. }
  374. /**
  375. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  376. *
  377. * @timer: the timer to be cancelled
  378. *
  379. * Returns:
  380. * 0 when the timer was not active
  381. * 1 when the timer was active
  382. */
  383. int hrtimer_cancel(struct hrtimer *timer)
  384. {
  385. for (;;) {
  386. int ret = hrtimer_try_to_cancel(timer);
  387. if (ret >= 0)
  388. return ret;
  389. }
  390. }
  391. /**
  392. * hrtimer_get_remaining - get remaining time for the timer
  393. *
  394. * @timer: the timer to read
  395. */
  396. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  397. {
  398. struct hrtimer_base *base;
  399. unsigned long flags;
  400. ktime_t rem;
  401. base = lock_hrtimer_base(timer, &flags);
  402. rem = ktime_sub(timer->expires, timer->base->get_time());
  403. unlock_hrtimer_base(timer, &flags);
  404. return rem;
  405. }
  406. /**
  407. * hrtimer_rebase - rebase an initialized hrtimer to a different base
  408. *
  409. * @timer: the timer to be rebased
  410. * @clock_id: the clock to be used
  411. */
  412. void hrtimer_rebase(struct hrtimer *timer, const clockid_t clock_id)
  413. {
  414. struct hrtimer_base *bases;
  415. bases = per_cpu(hrtimer_bases, raw_smp_processor_id());
  416. timer->base = &bases[clock_id];
  417. }
  418. /**
  419. * hrtimer_init - initialize a timer to the given clock
  420. *
  421. * @timer: the timer to be initialized
  422. * @clock_id: the clock to be used
  423. */
  424. void hrtimer_init(struct hrtimer *timer, const clockid_t clock_id)
  425. {
  426. memset(timer, 0, sizeof(struct hrtimer));
  427. hrtimer_rebase(timer, clock_id);
  428. }
  429. /**
  430. * hrtimer_get_res - get the timer resolution for a clock
  431. *
  432. * @which_clock: which clock to query
  433. * @tp: pointer to timespec variable to store the resolution
  434. *
  435. * Store the resolution of the clock selected by which_clock in the
  436. * variable pointed to by tp.
  437. */
  438. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  439. {
  440. struct hrtimer_base *bases;
  441. tp->tv_sec = 0;
  442. bases = per_cpu(hrtimer_bases, raw_smp_processor_id());
  443. tp->tv_nsec = bases[which_clock].resolution;
  444. return 0;
  445. }
  446. /*
  447. * Expire the per base hrtimer-queue:
  448. */
  449. static inline void run_hrtimer_queue(struct hrtimer_base *base)
  450. {
  451. ktime_t now = base->get_time();
  452. struct rb_node *node;
  453. spin_lock_irq(&base->lock);
  454. while ((node = base->first)) {
  455. struct hrtimer *timer;
  456. int (*fn)(void *);
  457. int restart;
  458. void *data;
  459. timer = rb_entry(node, struct hrtimer, node);
  460. if (now.tv64 <= timer->expires.tv64)
  461. break;
  462. fn = timer->function;
  463. data = timer->data;
  464. set_curr_timer(base, timer);
  465. __remove_hrtimer(timer, base);
  466. spin_unlock_irq(&base->lock);
  467. /*
  468. * fn == NULL is special case for the simplest timer
  469. * variant - wake up process and do not restart:
  470. */
  471. if (!fn) {
  472. wake_up_process(data);
  473. restart = HRTIMER_NORESTART;
  474. } else
  475. restart = fn(data);
  476. spin_lock_irq(&base->lock);
  477. if (restart == HRTIMER_RESTART)
  478. enqueue_hrtimer(timer, base);
  479. else
  480. timer->state = HRTIMER_EXPIRED;
  481. }
  482. set_curr_timer(base, NULL);
  483. spin_unlock_irq(&base->lock);
  484. }
  485. /*
  486. * Called from timer softirq every jiffy, expire hrtimers:
  487. */
  488. void hrtimer_run_queues(void)
  489. {
  490. struct hrtimer_base *base = __get_cpu_var(hrtimer_bases);
  491. int i;
  492. for (i = 0; i < MAX_HRTIMER_BASES; i++)
  493. run_hrtimer_queue(&base[i]);
  494. }
  495. /*
  496. * Sleep related functions:
  497. */
  498. /**
  499. * schedule_hrtimer - sleep until timeout
  500. *
  501. * @timer: hrtimer variable initialized with the correct clock base
  502. * @mode: timeout value is abs/rel
  503. *
  504. * Make the current task sleep until @timeout is
  505. * elapsed.
  506. *
  507. * You can set the task state as follows -
  508. *
  509. * %TASK_UNINTERRUPTIBLE - at least @timeout is guaranteed to
  510. * pass before the routine returns. The routine will return 0
  511. *
  512. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  513. * delivered to the current task. In this case the remaining time
  514. * will be returned
  515. *
  516. * The current task state is guaranteed to be TASK_RUNNING when this
  517. * routine returns.
  518. */
  519. static ktime_t __sched
  520. schedule_hrtimer(struct hrtimer *timer, const enum hrtimer_mode mode)
  521. {
  522. /* fn stays NULL, meaning single-shot wakeup: */
  523. timer->data = current;
  524. hrtimer_start(timer, timer->expires, mode);
  525. schedule();
  526. hrtimer_cancel(timer);
  527. /* Return the remaining time: */
  528. if (timer->state != HRTIMER_EXPIRED)
  529. return ktime_sub(timer->expires, timer->base->get_time());
  530. else
  531. return (ktime_t) {.tv64 = 0 };
  532. }
  533. static inline ktime_t __sched
  534. schedule_hrtimer_interruptible(struct hrtimer *timer,
  535. const enum hrtimer_mode mode)
  536. {
  537. set_current_state(TASK_INTERRUPTIBLE);
  538. return schedule_hrtimer(timer, mode);
  539. }
  540. static long __sched
  541. nanosleep_restart(struct restart_block *restart, clockid_t clockid)
  542. {
  543. struct timespec __user *rmtp, tu;
  544. void *rfn_save = restart->fn;
  545. struct hrtimer timer;
  546. ktime_t rem;
  547. restart->fn = do_no_restart_syscall;
  548. hrtimer_init(&timer, clockid);
  549. timer.expires.tv64 = ((u64)restart->arg1 << 32) | (u64) restart->arg0;
  550. rem = schedule_hrtimer_interruptible(&timer, HRTIMER_ABS);
  551. if (rem.tv64 <= 0)
  552. return 0;
  553. rmtp = (struct timespec __user *) restart->arg2;
  554. tu = ktime_to_timespec(rem);
  555. if (rmtp && copy_to_user(rmtp, &tu, sizeof(tu)))
  556. return -EFAULT;
  557. restart->fn = rfn_save;
  558. /* The other values in restart are already filled in */
  559. return -ERESTART_RESTARTBLOCK;
  560. }
  561. static long __sched nanosleep_restart_mono(struct restart_block *restart)
  562. {
  563. return nanosleep_restart(restart, CLOCK_MONOTONIC);
  564. }
  565. static long __sched nanosleep_restart_real(struct restart_block *restart)
  566. {
  567. return nanosleep_restart(restart, CLOCK_REALTIME);
  568. }
  569. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  570. const enum hrtimer_mode mode, const clockid_t clockid)
  571. {
  572. struct restart_block *restart;
  573. struct hrtimer timer;
  574. struct timespec tu;
  575. ktime_t rem;
  576. hrtimer_init(&timer, clockid);
  577. timer.expires = timespec_to_ktime(*rqtp);
  578. rem = schedule_hrtimer_interruptible(&timer, mode);
  579. if (rem.tv64 <= 0)
  580. return 0;
  581. /* Absolute timers do not update the rmtp value: */
  582. if (mode == HRTIMER_ABS)
  583. return -ERESTARTNOHAND;
  584. tu = ktime_to_timespec(rem);
  585. if (rmtp && copy_to_user(rmtp, &tu, sizeof(tu)))
  586. return -EFAULT;
  587. restart = &current_thread_info()->restart_block;
  588. restart->fn = (clockid == CLOCK_MONOTONIC) ?
  589. nanosleep_restart_mono : nanosleep_restart_real;
  590. restart->arg0 = timer.expires.tv64 & 0xFFFFFFFF;
  591. restart->arg1 = timer.expires.tv64 >> 32;
  592. restart->arg2 = (unsigned long) rmtp;
  593. return -ERESTART_RESTARTBLOCK;
  594. }
  595. asmlinkage long
  596. sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
  597. {
  598. struct timespec tu;
  599. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  600. return -EFAULT;
  601. if (!timespec_valid(&tu))
  602. return -EINVAL;
  603. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_REL, CLOCK_MONOTONIC);
  604. }
  605. /*
  606. * Functions related to boot-time initialization:
  607. */
  608. static void __devinit init_hrtimers_cpu(int cpu)
  609. {
  610. struct hrtimer_base *base = per_cpu(hrtimer_bases, cpu);
  611. int i;
  612. for (i = 0; i < MAX_HRTIMER_BASES; i++) {
  613. spin_lock_init(&base->lock);
  614. base++;
  615. }
  616. }
  617. #ifdef CONFIG_HOTPLUG_CPU
  618. static void migrate_hrtimer_list(struct hrtimer_base *old_base,
  619. struct hrtimer_base *new_base)
  620. {
  621. struct hrtimer *timer;
  622. struct rb_node *node;
  623. while ((node = rb_first(&old_base->active))) {
  624. timer = rb_entry(node, struct hrtimer, node);
  625. __remove_hrtimer(timer, old_base);
  626. timer->base = new_base;
  627. enqueue_hrtimer(timer, new_base);
  628. }
  629. }
  630. static void migrate_hrtimers(int cpu)
  631. {
  632. struct hrtimer_base *old_base, *new_base;
  633. int i;
  634. BUG_ON(cpu_online(cpu));
  635. old_base = per_cpu(hrtimer_bases, cpu);
  636. new_base = get_cpu_var(hrtimer_bases);
  637. local_irq_disable();
  638. for (i = 0; i < MAX_HRTIMER_BASES; i++) {
  639. spin_lock(&new_base->lock);
  640. spin_lock(&old_base->lock);
  641. BUG_ON(old_base->curr_timer);
  642. migrate_hrtimer_list(old_base, new_base);
  643. spin_unlock(&old_base->lock);
  644. spin_unlock(&new_base->lock);
  645. old_base++;
  646. new_base++;
  647. }
  648. local_irq_enable();
  649. put_cpu_var(hrtimer_bases);
  650. }
  651. #endif /* CONFIG_HOTPLUG_CPU */
  652. static int __devinit hrtimer_cpu_notify(struct notifier_block *self,
  653. unsigned long action, void *hcpu)
  654. {
  655. long cpu = (long)hcpu;
  656. switch (action) {
  657. case CPU_UP_PREPARE:
  658. init_hrtimers_cpu(cpu);
  659. break;
  660. #ifdef CONFIG_HOTPLUG_CPU
  661. case CPU_DEAD:
  662. migrate_hrtimers(cpu);
  663. break;
  664. #endif
  665. default:
  666. break;
  667. }
  668. return NOTIFY_OK;
  669. }
  670. static struct notifier_block __devinitdata hrtimers_nb = {
  671. .notifier_call = hrtimer_cpu_notify,
  672. };
  673. void __init hrtimers_init(void)
  674. {
  675. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  676. (void *)(long)smp_processor_id());
  677. register_cpu_notifier(&hrtimers_nb);
  678. }