hrtimer.c 20 KB

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