hrtimer.c 20 KB

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