hrtimer.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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 list_head *prev = &base->pending;
  268. struct rb_node *parent = NULL;
  269. struct hrtimer *entry;
  270. /*
  271. * Find the right place in the rbtree:
  272. */
  273. while (*link) {
  274. parent = *link;
  275. entry = rb_entry(parent, struct hrtimer, node);
  276. /*
  277. * We dont care about collisions. Nodes with
  278. * the same expiry time stay together.
  279. */
  280. if (timer->expires.tv64 < entry->expires.tv64)
  281. link = &(*link)->rb_left;
  282. else {
  283. link = &(*link)->rb_right;
  284. prev = &entry->list;
  285. }
  286. }
  287. /*
  288. * Insert the timer to the rbtree and to the sorted list:
  289. */
  290. rb_link_node(&timer->node, parent, link);
  291. rb_insert_color(&timer->node, &base->active);
  292. list_add(&timer->list, prev);
  293. timer->state = HRTIMER_PENDING;
  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 sorted list and from the rbtree:
  304. */
  305. list_del(&timer->list);
  306. rb_erase(&timer->node, &base->active);
  307. }
  308. /*
  309. * remove hrtimer, called with base lock held
  310. */
  311. static inline int
  312. remove_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
  313. {
  314. if (hrtimer_active(timer)) {
  315. __remove_hrtimer(timer, base);
  316. timer->state = HRTIMER_INACTIVE;
  317. return 1;
  318. }
  319. return 0;
  320. }
  321. /**
  322. * hrtimer_start - (re)start an relative timer on the current CPU
  323. *
  324. * @timer: the timer to be added
  325. * @tim: expiry time
  326. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  327. *
  328. * Returns:
  329. * 0 on success
  330. * 1 when the timer was active
  331. */
  332. int
  333. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  334. {
  335. struct hrtimer_base *base, *new_base;
  336. unsigned long flags;
  337. int ret;
  338. base = lock_hrtimer_base(timer, &flags);
  339. /* Remove an active timer from the queue: */
  340. ret = remove_hrtimer(timer, base);
  341. /* Switch the timer base, if necessary: */
  342. new_base = switch_hrtimer_base(timer, base);
  343. if (mode == HRTIMER_REL)
  344. tim = ktime_add(tim, new_base->get_time());
  345. timer->expires = tim;
  346. enqueue_hrtimer(timer, new_base);
  347. unlock_hrtimer_base(timer, &flags);
  348. return ret;
  349. }
  350. /**
  351. * hrtimer_try_to_cancel - try to deactivate a timer
  352. *
  353. * @timer: hrtimer to stop
  354. *
  355. * Returns:
  356. * 0 when the timer was not active
  357. * 1 when the timer was active
  358. * -1 when the timer is currently excuting the callback function and
  359. * can not be stopped
  360. */
  361. int hrtimer_try_to_cancel(struct hrtimer *timer)
  362. {
  363. struct hrtimer_base *base;
  364. unsigned long flags;
  365. int ret = -1;
  366. base = lock_hrtimer_base(timer, &flags);
  367. if (base->curr_timer != timer)
  368. ret = remove_hrtimer(timer, base);
  369. unlock_hrtimer_base(timer, &flags);
  370. return ret;
  371. }
  372. /**
  373. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  374. *
  375. * @timer: the timer to be cancelled
  376. *
  377. * Returns:
  378. * 0 when the timer was not active
  379. * 1 when the timer was active
  380. */
  381. int hrtimer_cancel(struct hrtimer *timer)
  382. {
  383. for (;;) {
  384. int ret = hrtimer_try_to_cancel(timer);
  385. if (ret >= 0)
  386. return ret;
  387. }
  388. }
  389. /**
  390. * hrtimer_get_remaining - get remaining time for the timer
  391. *
  392. * @timer: the timer to read
  393. */
  394. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  395. {
  396. struct hrtimer_base *base;
  397. unsigned long flags;
  398. ktime_t rem;
  399. base = lock_hrtimer_base(timer, &flags);
  400. rem = ktime_sub(timer->expires, timer->base->get_time());
  401. unlock_hrtimer_base(timer, &flags);
  402. return rem;
  403. }
  404. /**
  405. * hrtimer_rebase - rebase an initialized hrtimer to a different base
  406. *
  407. * @timer: the timer to be rebased
  408. * @clock_id: the clock to be used
  409. */
  410. void hrtimer_rebase(struct hrtimer *timer, const clockid_t clock_id)
  411. {
  412. struct hrtimer_base *bases;
  413. bases = per_cpu(hrtimer_bases, raw_smp_processor_id());
  414. timer->base = &bases[clock_id];
  415. }
  416. /**
  417. * hrtimer_init - initialize a timer to the given clock
  418. *
  419. * @timer: the timer to be initialized
  420. * @clock_id: the clock to be used
  421. */
  422. void hrtimer_init(struct hrtimer *timer, const clockid_t clock_id)
  423. {
  424. memset(timer, 0, sizeof(struct hrtimer));
  425. hrtimer_rebase(timer, clock_id);
  426. }
  427. /**
  428. * hrtimer_get_res - get the timer resolution for a clock
  429. *
  430. * @which_clock: which clock to query
  431. * @tp: pointer to timespec variable to store the resolution
  432. *
  433. * Store the resolution of the clock selected by which_clock in the
  434. * variable pointed to by tp.
  435. */
  436. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  437. {
  438. struct hrtimer_base *bases;
  439. tp->tv_sec = 0;
  440. bases = per_cpu(hrtimer_bases, raw_smp_processor_id());
  441. tp->tv_nsec = bases[which_clock].resolution;
  442. return 0;
  443. }
  444. /*
  445. * Expire the per base hrtimer-queue:
  446. */
  447. static inline void run_hrtimer_queue(struct hrtimer_base *base)
  448. {
  449. ktime_t now = base->get_time();
  450. spin_lock_irq(&base->lock);
  451. while (!list_empty(&base->pending)) {
  452. struct hrtimer *timer;
  453. int (*fn)(void *);
  454. int restart;
  455. void *data;
  456. timer = list_entry(base->pending.next, struct hrtimer, list);
  457. if (now.tv64 <= timer->expires.tv64)
  458. break;
  459. fn = timer->function;
  460. data = timer->data;
  461. set_curr_timer(base, timer);
  462. __remove_hrtimer(timer, base);
  463. spin_unlock_irq(&base->lock);
  464. /*
  465. * fn == NULL is special case for the simplest timer
  466. * variant - wake up process and do not restart:
  467. */
  468. if (!fn) {
  469. wake_up_process(data);
  470. restart = HRTIMER_NORESTART;
  471. } else
  472. restart = fn(data);
  473. spin_lock_irq(&base->lock);
  474. if (restart == HRTIMER_RESTART)
  475. enqueue_hrtimer(timer, base);
  476. else
  477. timer->state = HRTIMER_EXPIRED;
  478. }
  479. set_curr_timer(base, NULL);
  480. spin_unlock_irq(&base->lock);
  481. }
  482. /*
  483. * Called from timer softirq every jiffy, expire hrtimers:
  484. */
  485. void hrtimer_run_queues(void)
  486. {
  487. struct hrtimer_base *base = __get_cpu_var(hrtimer_bases);
  488. int i;
  489. for (i = 0; i < MAX_HRTIMER_BASES; i++)
  490. run_hrtimer_queue(&base[i]);
  491. }
  492. /*
  493. * Sleep related functions:
  494. */
  495. /**
  496. * schedule_hrtimer - sleep until timeout
  497. *
  498. * @timer: hrtimer variable initialized with the correct clock base
  499. * @mode: timeout value is abs/rel
  500. *
  501. * Make the current task sleep until @timeout is
  502. * elapsed.
  503. *
  504. * You can set the task state as follows -
  505. *
  506. * %TASK_UNINTERRUPTIBLE - at least @timeout is guaranteed to
  507. * pass before the routine returns. The routine will return 0
  508. *
  509. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  510. * delivered to the current task. In this case the remaining time
  511. * will be returned
  512. *
  513. * The current task state is guaranteed to be TASK_RUNNING when this
  514. * routine returns.
  515. */
  516. static ktime_t __sched
  517. schedule_hrtimer(struct hrtimer *timer, const enum hrtimer_mode mode)
  518. {
  519. /* fn stays NULL, meaning single-shot wakeup: */
  520. timer->data = current;
  521. hrtimer_start(timer, timer->expires, mode);
  522. schedule();
  523. hrtimer_cancel(timer);
  524. /* Return the remaining time: */
  525. if (timer->state != HRTIMER_EXPIRED)
  526. return ktime_sub(timer->expires, timer->base->get_time());
  527. else
  528. return (ktime_t) {.tv64 = 0 };
  529. }
  530. static inline ktime_t __sched
  531. schedule_hrtimer_interruptible(struct hrtimer *timer,
  532. const enum hrtimer_mode mode)
  533. {
  534. set_current_state(TASK_INTERRUPTIBLE);
  535. return schedule_hrtimer(timer, mode);
  536. }
  537. static long __sched
  538. nanosleep_restart(struct restart_block *restart, clockid_t clockid)
  539. {
  540. struct timespec __user *rmtp, tu;
  541. void *rfn_save = restart->fn;
  542. struct hrtimer timer;
  543. ktime_t rem;
  544. restart->fn = do_no_restart_syscall;
  545. hrtimer_init(&timer, clockid);
  546. timer.expires.tv64 = ((u64)restart->arg1 << 32) | (u64) restart->arg0;
  547. rem = schedule_hrtimer_interruptible(&timer, HRTIMER_ABS);
  548. if (rem.tv64 <= 0)
  549. return 0;
  550. rmtp = (struct timespec __user *) restart->arg2;
  551. tu = ktime_to_timespec(rem);
  552. if (rmtp && copy_to_user(rmtp, &tu, sizeof(tu)))
  553. return -EFAULT;
  554. restart->fn = rfn_save;
  555. /* The other values in restart are already filled in */
  556. return -ERESTART_RESTARTBLOCK;
  557. }
  558. static long __sched nanosleep_restart_mono(struct restart_block *restart)
  559. {
  560. return nanosleep_restart(restart, CLOCK_MONOTONIC);
  561. }
  562. static long __sched nanosleep_restart_real(struct restart_block *restart)
  563. {
  564. return nanosleep_restart(restart, CLOCK_REALTIME);
  565. }
  566. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  567. const enum hrtimer_mode mode, const clockid_t clockid)
  568. {
  569. struct restart_block *restart;
  570. struct hrtimer timer;
  571. struct timespec tu;
  572. ktime_t rem;
  573. hrtimer_init(&timer, clockid);
  574. timer.expires = timespec_to_ktime(*rqtp);
  575. rem = schedule_hrtimer_interruptible(&timer, mode);
  576. if (rem.tv64 <= 0)
  577. return 0;
  578. /* Absolute timers do not update the rmtp value: */
  579. if (mode == HRTIMER_ABS)
  580. return -ERESTARTNOHAND;
  581. tu = ktime_to_timespec(rem);
  582. if (rmtp && copy_to_user(rmtp, &tu, sizeof(tu)))
  583. return -EFAULT;
  584. restart = &current_thread_info()->restart_block;
  585. restart->fn = (clockid == CLOCK_MONOTONIC) ?
  586. nanosleep_restart_mono : nanosleep_restart_real;
  587. restart->arg0 = timer.expires.tv64 & 0xFFFFFFFF;
  588. restart->arg1 = timer.expires.tv64 >> 32;
  589. restart->arg2 = (unsigned long) rmtp;
  590. return -ERESTART_RESTARTBLOCK;
  591. }
  592. asmlinkage long
  593. sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
  594. {
  595. struct timespec tu;
  596. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  597. return -EFAULT;
  598. if (!timespec_valid(&tu))
  599. return -EINVAL;
  600. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_REL, CLOCK_MONOTONIC);
  601. }
  602. /*
  603. * Functions related to boot-time initialization:
  604. */
  605. static void __devinit init_hrtimers_cpu(int cpu)
  606. {
  607. struct hrtimer_base *base = per_cpu(hrtimer_bases, cpu);
  608. int i;
  609. for (i = 0; i < MAX_HRTIMER_BASES; i++) {
  610. spin_lock_init(&base->lock);
  611. INIT_LIST_HEAD(&base->pending);
  612. base++;
  613. }
  614. }
  615. #ifdef CONFIG_HOTPLUG_CPU
  616. static void migrate_hrtimer_list(struct hrtimer_base *old_base,
  617. struct hrtimer_base *new_base)
  618. {
  619. struct hrtimer *timer;
  620. struct rb_node *node;
  621. while ((node = rb_first(&old_base->active))) {
  622. timer = rb_entry(node, struct hrtimer, node);
  623. __remove_hrtimer(timer, old_base);
  624. timer->base = new_base;
  625. enqueue_hrtimer(timer, new_base);
  626. }
  627. }
  628. static void migrate_hrtimers(int cpu)
  629. {
  630. struct hrtimer_base *old_base, *new_base;
  631. int i;
  632. BUG_ON(cpu_online(cpu));
  633. old_base = per_cpu(hrtimer_bases, cpu);
  634. new_base = get_cpu_var(hrtimer_bases);
  635. local_irq_disable();
  636. for (i = 0; i < MAX_HRTIMER_BASES; i++) {
  637. spin_lock(&new_base->lock);
  638. spin_lock(&old_base->lock);
  639. BUG_ON(old_base->curr_timer);
  640. migrate_hrtimer_list(old_base, new_base);
  641. spin_unlock(&old_base->lock);
  642. spin_unlock(&new_base->lock);
  643. old_base++;
  644. new_base++;
  645. }
  646. local_irq_enable();
  647. put_cpu_var(hrtimer_bases);
  648. }
  649. #endif /* CONFIG_HOTPLUG_CPU */
  650. static int __devinit hrtimer_cpu_notify(struct notifier_block *self,
  651. unsigned long action, void *hcpu)
  652. {
  653. long cpu = (long)hcpu;
  654. switch (action) {
  655. case CPU_UP_PREPARE:
  656. init_hrtimers_cpu(cpu);
  657. break;
  658. #ifdef CONFIG_HOTPLUG_CPU
  659. case CPU_DEAD:
  660. migrate_hrtimers(cpu);
  661. break;
  662. #endif
  663. default:
  664. break;
  665. }
  666. return NOTIFY_OK;
  667. }
  668. static struct notifier_block __devinitdata hrtimers_nb = {
  669. .notifier_call = hrtimer_cpu_notify,
  670. };
  671. void __init hrtimers_init(void)
  672. {
  673. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  674. (void *)(long)smp_processor_id());
  675. register_cpu_notifier(&hrtimers_nb);
  676. }