hrtimer.c 18 KB

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