hrtimer.c 19 KB

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