hrtimer.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  1. /*
  2. * linux/kernel/hrtimer.c
  3. *
  4. * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
  6. * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
  7. *
  8. * High-resolution kernel timers
  9. *
  10. * In contrast to the low-resolution timeout API implemented in
  11. * kernel/timer.c, hrtimers provide finer resolution and accuracy
  12. * depending on system configuration and capabilities.
  13. *
  14. * These timers are currently used for:
  15. * - itimers
  16. * - POSIX timers
  17. * - nanosleep
  18. * - precise in-kernel timing
  19. *
  20. * Started by: Thomas Gleixner and Ingo Molnar
  21. *
  22. * Credits:
  23. * based on kernel/timer.c
  24. *
  25. * Help, testing, suggestions, bugfixes, improvements were
  26. * provided by:
  27. *
  28. * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
  29. * et. al.
  30. *
  31. * For licencing details see kernel-base/COPYING
  32. */
  33. #include <linux/cpu.h>
  34. #include <linux/irq.h>
  35. #include <linux/module.h>
  36. #include <linux/percpu.h>
  37. #include <linux/hrtimer.h>
  38. #include <linux/notifier.h>
  39. #include <linux/syscalls.h>
  40. #include <linux/kallsyms.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/tick.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/err.h>
  45. #include <asm/uaccess.h>
  46. /**
  47. * ktime_get - get the monotonic time in ktime_t format
  48. *
  49. * returns the time in ktime_t format
  50. */
  51. ktime_t ktime_get(void)
  52. {
  53. struct timespec now;
  54. ktime_get_ts(&now);
  55. return timespec_to_ktime(now);
  56. }
  57. EXPORT_SYMBOL_GPL(ktime_get);
  58. /**
  59. * ktime_get_real - get the real (wall-) time in ktime_t format
  60. *
  61. * returns the time in ktime_t format
  62. */
  63. ktime_t ktime_get_real(void)
  64. {
  65. struct timespec now;
  66. getnstimeofday(&now);
  67. return timespec_to_ktime(now);
  68. }
  69. EXPORT_SYMBOL_GPL(ktime_get_real);
  70. /*
  71. * The timer bases:
  72. *
  73. * Note: If we want to add new timer bases, we have to skip the two
  74. * clock ids captured by the cpu-timers. We do this by holding empty
  75. * entries rather than doing math adjustment of the clock ids.
  76. * This ensures that we capture erroneous accesses to these clock ids
  77. * rather than moving them into the range of valid clock id's.
  78. */
  79. DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
  80. {
  81. .clock_base =
  82. {
  83. {
  84. .index = CLOCK_REALTIME,
  85. .get_time = &ktime_get_real,
  86. .resolution = KTIME_LOW_RES,
  87. },
  88. {
  89. .index = CLOCK_MONOTONIC,
  90. .get_time = &ktime_get,
  91. .resolution = KTIME_LOW_RES,
  92. },
  93. }
  94. };
  95. /**
  96. * ktime_get_ts - get the monotonic clock in timespec format
  97. * @ts: pointer to timespec variable
  98. *
  99. * The function calculates the monotonic clock from the realtime
  100. * clock and the wall_to_monotonic offset and stores the result
  101. * in normalized timespec format in the variable pointed to by @ts.
  102. */
  103. void ktime_get_ts(struct timespec *ts)
  104. {
  105. struct timespec tomono;
  106. unsigned long seq;
  107. do {
  108. seq = read_seqbegin(&xtime_lock);
  109. getnstimeofday(ts);
  110. tomono = wall_to_monotonic;
  111. } while (read_seqretry(&xtime_lock, seq));
  112. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
  113. ts->tv_nsec + tomono.tv_nsec);
  114. }
  115. EXPORT_SYMBOL_GPL(ktime_get_ts);
  116. /*
  117. * Get the coarse grained time at the softirq based on xtime and
  118. * wall_to_monotonic.
  119. */
  120. static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
  121. {
  122. ktime_t xtim, tomono;
  123. struct timespec xts, tom;
  124. unsigned long seq;
  125. do {
  126. seq = read_seqbegin(&xtime_lock);
  127. xts = current_kernel_time();
  128. tom = wall_to_monotonic;
  129. } while (read_seqretry(&xtime_lock, seq));
  130. xtim = timespec_to_ktime(xts);
  131. tomono = timespec_to_ktime(tom);
  132. base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
  133. base->clock_base[CLOCK_MONOTONIC].softirq_time =
  134. ktime_add(xtim, tomono);
  135. }
  136. /*
  137. * Helper function to check, whether the timer is running the callback
  138. * function
  139. */
  140. static inline int hrtimer_callback_running(struct hrtimer *timer)
  141. {
  142. return timer->state & HRTIMER_STATE_CALLBACK;
  143. }
  144. /*
  145. * Functions and macros which are different for UP/SMP systems are kept in a
  146. * single place
  147. */
  148. #ifdef CONFIG_SMP
  149. /*
  150. * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
  151. * means that all timers which are tied to this base via timer->base are
  152. * locked, and the base itself is locked too.
  153. *
  154. * So __run_timers/migrate_timers can safely modify all timers which could
  155. * be found on the lists/queues.
  156. *
  157. * When the timer's base is locked, and the timer removed from list, it is
  158. * possible to set timer->base = NULL and drop the lock: the timer remains
  159. * locked.
  160. */
  161. static
  162. struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
  163. unsigned long *flags)
  164. {
  165. struct hrtimer_clock_base *base;
  166. for (;;) {
  167. base = timer->base;
  168. if (likely(base != NULL)) {
  169. spin_lock_irqsave(&base->cpu_base->lock, *flags);
  170. if (likely(base == timer->base))
  171. return base;
  172. /* The timer has migrated to another CPU: */
  173. spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
  174. }
  175. cpu_relax();
  176. }
  177. }
  178. /*
  179. * Switch the timer base to the current CPU when possible.
  180. */
  181. static inline struct hrtimer_clock_base *
  182. switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
  183. {
  184. struct hrtimer_clock_base *new_base;
  185. struct hrtimer_cpu_base *new_cpu_base;
  186. new_cpu_base = &__get_cpu_var(hrtimer_bases);
  187. new_base = &new_cpu_base->clock_base[base->index];
  188. if (base != new_base) {
  189. /*
  190. * We are trying to schedule the timer on the local CPU.
  191. * However we can't change timer's base while it is running,
  192. * so we keep it on the same CPU. No hassle vs. reprogramming
  193. * the event source in the high resolution case. The softirq
  194. * code will take care of this when the timer function has
  195. * completed. There is no conflict as we hold the lock until
  196. * the timer is enqueued.
  197. */
  198. if (unlikely(hrtimer_callback_running(timer)))
  199. return base;
  200. /* See the comment in lock_timer_base() */
  201. timer->base = NULL;
  202. spin_unlock(&base->cpu_base->lock);
  203. spin_lock(&new_base->cpu_base->lock);
  204. timer->base = new_base;
  205. }
  206. return new_base;
  207. }
  208. #else /* CONFIG_SMP */
  209. static inline struct hrtimer_clock_base *
  210. lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  211. {
  212. struct hrtimer_clock_base *base = timer->base;
  213. spin_lock_irqsave(&base->cpu_base->lock, *flags);
  214. return base;
  215. }
  216. # define switch_hrtimer_base(t, b) (b)
  217. #endif /* !CONFIG_SMP */
  218. /*
  219. * Functions for the union type storage format of ktime_t which are
  220. * too large for inlining:
  221. */
  222. #if BITS_PER_LONG < 64
  223. # ifndef CONFIG_KTIME_SCALAR
  224. /**
  225. * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
  226. * @kt: addend
  227. * @nsec: the scalar nsec value to add
  228. *
  229. * Returns the sum of kt and nsec in ktime_t format
  230. */
  231. ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
  232. {
  233. ktime_t tmp;
  234. if (likely(nsec < NSEC_PER_SEC)) {
  235. tmp.tv64 = nsec;
  236. } else {
  237. unsigned long rem = do_div(nsec, NSEC_PER_SEC);
  238. tmp = ktime_set((long)nsec, rem);
  239. }
  240. return ktime_add(kt, tmp);
  241. }
  242. EXPORT_SYMBOL_GPL(ktime_add_ns);
  243. /**
  244. * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
  245. * @kt: minuend
  246. * @nsec: the scalar nsec value to subtract
  247. *
  248. * Returns the subtraction of @nsec from @kt in ktime_t format
  249. */
  250. ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
  251. {
  252. ktime_t tmp;
  253. if (likely(nsec < NSEC_PER_SEC)) {
  254. tmp.tv64 = nsec;
  255. } else {
  256. unsigned long rem = do_div(nsec, NSEC_PER_SEC);
  257. tmp = ktime_set((long)nsec, rem);
  258. }
  259. return ktime_sub(kt, tmp);
  260. }
  261. EXPORT_SYMBOL_GPL(ktime_sub_ns);
  262. # endif /* !CONFIG_KTIME_SCALAR */
  263. /*
  264. * Divide a ktime value by a nanosecond value
  265. */
  266. u64 ktime_divns(const ktime_t kt, s64 div)
  267. {
  268. u64 dclc, inc, dns;
  269. int sft = 0;
  270. dclc = dns = ktime_to_ns(kt);
  271. inc = div;
  272. /* Make sure the divisor is less than 2^32: */
  273. while (div >> 32) {
  274. sft++;
  275. div >>= 1;
  276. }
  277. dclc >>= sft;
  278. do_div(dclc, (unsigned long) div);
  279. return dclc;
  280. }
  281. #endif /* BITS_PER_LONG >= 64 */
  282. /*
  283. * Add two ktime values and do a safety check for overflow:
  284. */
  285. ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
  286. {
  287. ktime_t res = ktime_add(lhs, rhs);
  288. /*
  289. * We use KTIME_SEC_MAX here, the maximum timeout which we can
  290. * return to user space in a timespec:
  291. */
  292. if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
  293. res = ktime_set(KTIME_SEC_MAX, 0);
  294. return res;
  295. }
  296. /*
  297. * Check, whether the timer is on the callback pending list
  298. */
  299. static inline int hrtimer_cb_pending(const struct hrtimer *timer)
  300. {
  301. return timer->state & HRTIMER_STATE_PENDING;
  302. }
  303. /*
  304. * Remove a timer from the callback pending list
  305. */
  306. static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
  307. {
  308. list_del_init(&timer->cb_entry);
  309. }
  310. /* High resolution timer related functions */
  311. #ifdef CONFIG_HIGH_RES_TIMERS
  312. /*
  313. * High resolution timer enabled ?
  314. */
  315. static int hrtimer_hres_enabled __read_mostly = 1;
  316. /*
  317. * Enable / Disable high resolution mode
  318. */
  319. static int __init setup_hrtimer_hres(char *str)
  320. {
  321. if (!strcmp(str, "off"))
  322. hrtimer_hres_enabled = 0;
  323. else if (!strcmp(str, "on"))
  324. hrtimer_hres_enabled = 1;
  325. else
  326. return 0;
  327. return 1;
  328. }
  329. __setup("highres=", setup_hrtimer_hres);
  330. /*
  331. * hrtimer_high_res_enabled - query, if the highres mode is enabled
  332. */
  333. static inline int hrtimer_is_hres_enabled(void)
  334. {
  335. return hrtimer_hres_enabled;
  336. }
  337. /*
  338. * Is the high resolution mode active ?
  339. */
  340. static inline int hrtimer_hres_active(void)
  341. {
  342. return __get_cpu_var(hrtimer_bases).hres_active;
  343. }
  344. /*
  345. * Reprogram the event source with checking both queues for the
  346. * next event
  347. * Called with interrupts disabled and base->lock held
  348. */
  349. static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
  350. {
  351. int i;
  352. struct hrtimer_clock_base *base = cpu_base->clock_base;
  353. ktime_t expires;
  354. cpu_base->expires_next.tv64 = KTIME_MAX;
  355. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  356. struct hrtimer *timer;
  357. if (!base->first)
  358. continue;
  359. timer = rb_entry(base->first, struct hrtimer, node);
  360. expires = ktime_sub(timer->expires, base->offset);
  361. if (expires.tv64 < cpu_base->expires_next.tv64)
  362. cpu_base->expires_next = expires;
  363. }
  364. if (cpu_base->expires_next.tv64 != KTIME_MAX)
  365. tick_program_event(cpu_base->expires_next, 1);
  366. }
  367. /*
  368. * Shared reprogramming for clock_realtime and clock_monotonic
  369. *
  370. * When a timer is enqueued and expires earlier than the already enqueued
  371. * timers, we have to check, whether it expires earlier than the timer for
  372. * which the clock event device was armed.
  373. *
  374. * Called with interrupts disabled and base->cpu_base.lock held
  375. */
  376. static int hrtimer_reprogram(struct hrtimer *timer,
  377. struct hrtimer_clock_base *base)
  378. {
  379. ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
  380. ktime_t expires = ktime_sub(timer->expires, base->offset);
  381. int res;
  382. WARN_ON_ONCE(timer->expires.tv64 < 0);
  383. /*
  384. * When the callback is running, we do not reprogram the clock event
  385. * device. The timer callback is either running on a different CPU or
  386. * the callback is executed in the hrtimer_interrupt context. The
  387. * reprogramming is handled either by the softirq, which called the
  388. * callback or at the end of the hrtimer_interrupt.
  389. */
  390. if (hrtimer_callback_running(timer))
  391. return 0;
  392. /*
  393. * CLOCK_REALTIME timer might be requested with an absolute
  394. * expiry time which is less than base->offset. Nothing wrong
  395. * about that, just avoid to call into the tick code, which
  396. * has now objections against negative expiry values.
  397. */
  398. if (expires.tv64 < 0)
  399. return -ETIME;
  400. if (expires.tv64 >= expires_next->tv64)
  401. return 0;
  402. /*
  403. * Clockevents returns -ETIME, when the event was in the past.
  404. */
  405. res = tick_program_event(expires, 0);
  406. if (!IS_ERR_VALUE(res))
  407. *expires_next = expires;
  408. return res;
  409. }
  410. /*
  411. * Retrigger next event is called after clock was set
  412. *
  413. * Called with interrupts disabled via on_each_cpu()
  414. */
  415. static void retrigger_next_event(void *arg)
  416. {
  417. struct hrtimer_cpu_base *base;
  418. struct timespec realtime_offset;
  419. unsigned long seq;
  420. if (!hrtimer_hres_active())
  421. return;
  422. do {
  423. seq = read_seqbegin(&xtime_lock);
  424. set_normalized_timespec(&realtime_offset,
  425. -wall_to_monotonic.tv_sec,
  426. -wall_to_monotonic.tv_nsec);
  427. } while (read_seqretry(&xtime_lock, seq));
  428. base = &__get_cpu_var(hrtimer_bases);
  429. /* Adjust CLOCK_REALTIME offset */
  430. spin_lock(&base->lock);
  431. base->clock_base[CLOCK_REALTIME].offset =
  432. timespec_to_ktime(realtime_offset);
  433. hrtimer_force_reprogram(base);
  434. spin_unlock(&base->lock);
  435. }
  436. /*
  437. * Clock realtime was set
  438. *
  439. * Change the offset of the realtime clock vs. the monotonic
  440. * clock.
  441. *
  442. * We might have to reprogram the high resolution timer interrupt. On
  443. * SMP we call the architecture specific code to retrigger _all_ high
  444. * resolution timer interrupts. On UP we just disable interrupts and
  445. * call the high resolution interrupt code.
  446. */
  447. void clock_was_set(void)
  448. {
  449. /* Retrigger the CPU local events everywhere */
  450. on_each_cpu(retrigger_next_event, NULL, 0, 1);
  451. }
  452. /*
  453. * During resume we might have to reprogram the high resolution timer
  454. * interrupt (on the local CPU):
  455. */
  456. void hres_timers_resume(void)
  457. {
  458. WARN_ON_ONCE(num_online_cpus() > 1);
  459. /* Retrigger the CPU local events: */
  460. retrigger_next_event(NULL);
  461. }
  462. /*
  463. * Initialize the high resolution related parts of cpu_base
  464. */
  465. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
  466. {
  467. base->expires_next.tv64 = KTIME_MAX;
  468. base->hres_active = 0;
  469. }
  470. /*
  471. * Initialize the high resolution related parts of a hrtimer
  472. */
  473. static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
  474. {
  475. }
  476. /*
  477. * When High resolution timers are active, try to reprogram. Note, that in case
  478. * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
  479. * check happens. The timer gets enqueued into the rbtree. The reprogramming
  480. * and expiry check is done in the hrtimer_interrupt or in the softirq.
  481. */
  482. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  483. struct hrtimer_clock_base *base)
  484. {
  485. if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
  486. /* Timer is expired, act upon the callback mode */
  487. switch(timer->cb_mode) {
  488. case HRTIMER_CB_IRQSAFE_NO_RESTART:
  489. /*
  490. * We can call the callback from here. No restart
  491. * happens, so no danger of recursion
  492. */
  493. BUG_ON(timer->function(timer) != HRTIMER_NORESTART);
  494. return 1;
  495. case HRTIMER_CB_IRQSAFE_NO_SOFTIRQ:
  496. /*
  497. * This is solely for the sched tick emulation with
  498. * dynamic tick support to ensure that we do not
  499. * restart the tick right on the edge and end up with
  500. * the tick timer in the softirq ! The calling site
  501. * takes care of this.
  502. */
  503. return 1;
  504. case HRTIMER_CB_IRQSAFE:
  505. case HRTIMER_CB_SOFTIRQ:
  506. /*
  507. * Move everything else into the softirq pending list !
  508. */
  509. list_add_tail(&timer->cb_entry,
  510. &base->cpu_base->cb_pending);
  511. timer->state = HRTIMER_STATE_PENDING;
  512. raise_softirq(HRTIMER_SOFTIRQ);
  513. return 1;
  514. default:
  515. BUG();
  516. }
  517. }
  518. return 0;
  519. }
  520. /*
  521. * Switch to high resolution mode
  522. */
  523. static int hrtimer_switch_to_hres(void)
  524. {
  525. int cpu = smp_processor_id();
  526. struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
  527. unsigned long flags;
  528. if (base->hres_active)
  529. return 1;
  530. local_irq_save(flags);
  531. if (tick_init_highres()) {
  532. local_irq_restore(flags);
  533. printk(KERN_WARNING "Could not switch to high resolution "
  534. "mode on CPU %d\n", cpu);
  535. return 0;
  536. }
  537. base->hres_active = 1;
  538. base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
  539. base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
  540. tick_setup_sched_timer();
  541. /* "Retrigger" the interrupt to get things going */
  542. retrigger_next_event(NULL);
  543. local_irq_restore(flags);
  544. printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
  545. smp_processor_id());
  546. return 1;
  547. }
  548. #else
  549. static inline int hrtimer_hres_active(void) { return 0; }
  550. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  551. static inline int hrtimer_switch_to_hres(void) { return 0; }
  552. static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
  553. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  554. struct hrtimer_clock_base *base)
  555. {
  556. return 0;
  557. }
  558. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  559. static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
  560. static inline int hrtimer_reprogram(struct hrtimer *timer,
  561. struct hrtimer_clock_base *base)
  562. {
  563. return 0;
  564. }
  565. #endif /* CONFIG_HIGH_RES_TIMERS */
  566. #ifdef CONFIG_TIMER_STATS
  567. void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
  568. {
  569. if (timer->start_site)
  570. return;
  571. timer->start_site = addr;
  572. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  573. timer->start_pid = current->pid;
  574. }
  575. #endif
  576. /*
  577. * Counterpart to lock_hrtimer_base above:
  578. */
  579. static inline
  580. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  581. {
  582. spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  583. }
  584. /**
  585. * hrtimer_forward - forward the timer expiry
  586. * @timer: hrtimer to forward
  587. * @now: forward past this time
  588. * @interval: the interval to forward
  589. *
  590. * Forward the timer expiry so it will expire in the future.
  591. * Returns the number of overruns.
  592. */
  593. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  594. {
  595. u64 orun = 1;
  596. ktime_t delta;
  597. delta = ktime_sub(now, timer->expires);
  598. if (delta.tv64 < 0)
  599. return 0;
  600. if (interval.tv64 < timer->base->resolution.tv64)
  601. interval.tv64 = timer->base->resolution.tv64;
  602. if (unlikely(delta.tv64 >= interval.tv64)) {
  603. s64 incr = ktime_to_ns(interval);
  604. orun = ktime_divns(delta, incr);
  605. timer->expires = ktime_add_ns(timer->expires, incr * orun);
  606. if (timer->expires.tv64 > now.tv64)
  607. return orun;
  608. /*
  609. * This (and the ktime_add() below) is the
  610. * correction for exact:
  611. */
  612. orun++;
  613. }
  614. timer->expires = ktime_add_safe(timer->expires, interval);
  615. return orun;
  616. }
  617. EXPORT_SYMBOL_GPL(hrtimer_forward);
  618. /*
  619. * enqueue_hrtimer - internal function to (re)start a timer
  620. *
  621. * The timer is inserted in expiry order. Insertion into the
  622. * red black tree is O(log(n)). Must hold the base lock.
  623. */
  624. static void enqueue_hrtimer(struct hrtimer *timer,
  625. struct hrtimer_clock_base *base, int reprogram)
  626. {
  627. struct rb_node **link = &base->active.rb_node;
  628. struct rb_node *parent = NULL;
  629. struct hrtimer *entry;
  630. int leftmost = 1;
  631. /*
  632. * Find the right place in the rbtree:
  633. */
  634. while (*link) {
  635. parent = *link;
  636. entry = rb_entry(parent, struct hrtimer, node);
  637. /*
  638. * We dont care about collisions. Nodes with
  639. * the same expiry time stay together.
  640. */
  641. if (timer->expires.tv64 < entry->expires.tv64) {
  642. link = &(*link)->rb_left;
  643. } else {
  644. link = &(*link)->rb_right;
  645. leftmost = 0;
  646. }
  647. }
  648. /*
  649. * Insert the timer to the rbtree and check whether it
  650. * replaces the first pending timer
  651. */
  652. if (leftmost) {
  653. /*
  654. * Reprogram the clock event device. When the timer is already
  655. * expired hrtimer_enqueue_reprogram has either called the
  656. * callback or added it to the pending list and raised the
  657. * softirq.
  658. *
  659. * This is a NOP for !HIGHRES
  660. */
  661. if (reprogram && hrtimer_enqueue_reprogram(timer, base))
  662. return;
  663. base->first = &timer->node;
  664. }
  665. rb_link_node(&timer->node, parent, link);
  666. rb_insert_color(&timer->node, &base->active);
  667. /*
  668. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  669. * state of a possibly running callback.
  670. */
  671. timer->state |= HRTIMER_STATE_ENQUEUED;
  672. }
  673. /*
  674. * __remove_hrtimer - internal function to remove a timer
  675. *
  676. * Caller must hold the base lock.
  677. *
  678. * High resolution timer mode reprograms the clock event device when the
  679. * timer is the one which expires next. The caller can disable this by setting
  680. * reprogram to zero. This is useful, when the context does a reprogramming
  681. * anyway (e.g. timer interrupt)
  682. */
  683. static void __remove_hrtimer(struct hrtimer *timer,
  684. struct hrtimer_clock_base *base,
  685. unsigned long newstate, int reprogram)
  686. {
  687. /* High res. callback list. NOP for !HIGHRES */
  688. if (hrtimer_cb_pending(timer))
  689. hrtimer_remove_cb_pending(timer);
  690. else {
  691. /*
  692. * Remove the timer from the rbtree and replace the
  693. * first entry pointer if necessary.
  694. */
  695. if (base->first == &timer->node) {
  696. base->first = rb_next(&timer->node);
  697. /* Reprogram the clock event device. if enabled */
  698. if (reprogram && hrtimer_hres_active())
  699. hrtimer_force_reprogram(base->cpu_base);
  700. }
  701. rb_erase(&timer->node, &base->active);
  702. }
  703. timer->state = newstate;
  704. }
  705. /*
  706. * remove hrtimer, called with base lock held
  707. */
  708. static inline int
  709. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  710. {
  711. if (hrtimer_is_queued(timer)) {
  712. int reprogram;
  713. /*
  714. * Remove the timer and force reprogramming when high
  715. * resolution mode is active and the timer is on the current
  716. * CPU. If we remove a timer on another CPU, reprogramming is
  717. * skipped. The interrupt event on this CPU is fired and
  718. * reprogramming happens in the interrupt handler. This is a
  719. * rare case and less expensive than a smp call.
  720. */
  721. timer_stats_hrtimer_clear_start_info(timer);
  722. reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
  723. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
  724. reprogram);
  725. return 1;
  726. }
  727. return 0;
  728. }
  729. /**
  730. * hrtimer_start - (re)start an relative timer on the current CPU
  731. * @timer: the timer to be added
  732. * @tim: expiry time
  733. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  734. *
  735. * Returns:
  736. * 0 on success
  737. * 1 when the timer was active
  738. */
  739. int
  740. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  741. {
  742. struct hrtimer_clock_base *base, *new_base;
  743. unsigned long flags;
  744. int ret;
  745. base = lock_hrtimer_base(timer, &flags);
  746. /* Remove an active timer from the queue: */
  747. ret = remove_hrtimer(timer, base);
  748. /* Switch the timer base, if necessary: */
  749. new_base = switch_hrtimer_base(timer, base);
  750. if (mode == HRTIMER_MODE_REL) {
  751. tim = ktime_add_safe(tim, new_base->get_time());
  752. /*
  753. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  754. * to signal that they simply return xtime in
  755. * do_gettimeoffset(). In this case we want to round up by
  756. * resolution when starting a relative timer, to avoid short
  757. * timeouts. This will go away with the GTOD framework.
  758. */
  759. #ifdef CONFIG_TIME_LOW_RES
  760. tim = ktime_add_safe(tim, base->resolution);
  761. #endif
  762. }
  763. timer->expires = tim;
  764. timer_stats_hrtimer_set_start_info(timer);
  765. /*
  766. * Only allow reprogramming if the new base is on this CPU.
  767. * (it might still be on another CPU if the timer was pending)
  768. */
  769. enqueue_hrtimer(timer, new_base,
  770. new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
  771. unlock_hrtimer_base(timer, &flags);
  772. return ret;
  773. }
  774. EXPORT_SYMBOL_GPL(hrtimer_start);
  775. /**
  776. * hrtimer_try_to_cancel - try to deactivate a timer
  777. * @timer: hrtimer to stop
  778. *
  779. * Returns:
  780. * 0 when the timer was not active
  781. * 1 when the timer was active
  782. * -1 when the timer is currently excuting the callback function and
  783. * cannot be stopped
  784. */
  785. int hrtimer_try_to_cancel(struct hrtimer *timer)
  786. {
  787. struct hrtimer_clock_base *base;
  788. unsigned long flags;
  789. int ret = -1;
  790. base = lock_hrtimer_base(timer, &flags);
  791. if (!hrtimer_callback_running(timer))
  792. ret = remove_hrtimer(timer, base);
  793. unlock_hrtimer_base(timer, &flags);
  794. return ret;
  795. }
  796. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  797. /**
  798. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  799. * @timer: the timer to be cancelled
  800. *
  801. * Returns:
  802. * 0 when the timer was not active
  803. * 1 when the timer was active
  804. */
  805. int hrtimer_cancel(struct hrtimer *timer)
  806. {
  807. for (;;) {
  808. int ret = hrtimer_try_to_cancel(timer);
  809. if (ret >= 0)
  810. return ret;
  811. cpu_relax();
  812. }
  813. }
  814. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  815. /**
  816. * hrtimer_get_remaining - get remaining time for the timer
  817. * @timer: the timer to read
  818. */
  819. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  820. {
  821. struct hrtimer_clock_base *base;
  822. unsigned long flags;
  823. ktime_t rem;
  824. base = lock_hrtimer_base(timer, &flags);
  825. rem = ktime_sub(timer->expires, base->get_time());
  826. unlock_hrtimer_base(timer, &flags);
  827. return rem;
  828. }
  829. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  830. #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
  831. /**
  832. * hrtimer_get_next_event - get the time until next expiry event
  833. *
  834. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  835. * is pending.
  836. */
  837. ktime_t hrtimer_get_next_event(void)
  838. {
  839. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  840. struct hrtimer_clock_base *base = cpu_base->clock_base;
  841. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  842. unsigned long flags;
  843. int i;
  844. spin_lock_irqsave(&cpu_base->lock, flags);
  845. if (!hrtimer_hres_active()) {
  846. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  847. struct hrtimer *timer;
  848. if (!base->first)
  849. continue;
  850. timer = rb_entry(base->first, struct hrtimer, node);
  851. delta.tv64 = timer->expires.tv64;
  852. delta = ktime_sub(delta, base->get_time());
  853. if (delta.tv64 < mindelta.tv64)
  854. mindelta.tv64 = delta.tv64;
  855. }
  856. }
  857. spin_unlock_irqrestore(&cpu_base->lock, flags);
  858. if (mindelta.tv64 < 0)
  859. mindelta.tv64 = 0;
  860. return mindelta;
  861. }
  862. #endif
  863. /**
  864. * hrtimer_init - initialize a timer to the given clock
  865. * @timer: the timer to be initialized
  866. * @clock_id: the clock to be used
  867. * @mode: timer mode abs/rel
  868. */
  869. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  870. enum hrtimer_mode mode)
  871. {
  872. struct hrtimer_cpu_base *cpu_base;
  873. memset(timer, 0, sizeof(struct hrtimer));
  874. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  875. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  876. clock_id = CLOCK_MONOTONIC;
  877. timer->base = &cpu_base->clock_base[clock_id];
  878. INIT_LIST_HEAD(&timer->cb_entry);
  879. hrtimer_init_timer_hres(timer);
  880. #ifdef CONFIG_TIMER_STATS
  881. timer->start_site = NULL;
  882. timer->start_pid = -1;
  883. memset(timer->start_comm, 0, TASK_COMM_LEN);
  884. #endif
  885. }
  886. EXPORT_SYMBOL_GPL(hrtimer_init);
  887. /**
  888. * hrtimer_get_res - get the timer resolution for a clock
  889. * @which_clock: which clock to query
  890. * @tp: pointer to timespec variable to store the resolution
  891. *
  892. * Store the resolution of the clock selected by @which_clock in the
  893. * variable pointed to by @tp.
  894. */
  895. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  896. {
  897. struct hrtimer_cpu_base *cpu_base;
  898. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  899. *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
  900. return 0;
  901. }
  902. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  903. static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base)
  904. {
  905. spin_lock_irq(&cpu_base->lock);
  906. while (!list_empty(&cpu_base->cb_pending)) {
  907. enum hrtimer_restart (*fn)(struct hrtimer *);
  908. struct hrtimer *timer;
  909. int restart;
  910. timer = list_entry(cpu_base->cb_pending.next,
  911. struct hrtimer, cb_entry);
  912. timer_stats_account_hrtimer(timer);
  913. fn = timer->function;
  914. __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
  915. spin_unlock_irq(&cpu_base->lock);
  916. restart = fn(timer);
  917. spin_lock_irq(&cpu_base->lock);
  918. timer->state &= ~HRTIMER_STATE_CALLBACK;
  919. if (restart == HRTIMER_RESTART) {
  920. BUG_ON(hrtimer_active(timer));
  921. /*
  922. * Enqueue the timer, allow reprogramming of the event
  923. * device
  924. */
  925. enqueue_hrtimer(timer, timer->base, 1);
  926. } else if (hrtimer_active(timer)) {
  927. /*
  928. * If the timer was rearmed on another CPU, reprogram
  929. * the event device.
  930. */
  931. if (timer->base->first == &timer->node)
  932. hrtimer_reprogram(timer, timer->base);
  933. }
  934. }
  935. spin_unlock_irq(&cpu_base->lock);
  936. }
  937. static void __run_hrtimer(struct hrtimer *timer)
  938. {
  939. struct hrtimer_clock_base *base = timer->base;
  940. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  941. enum hrtimer_restart (*fn)(struct hrtimer *);
  942. int restart;
  943. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  944. timer_stats_account_hrtimer(timer);
  945. fn = timer->function;
  946. if (timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ) {
  947. /*
  948. * Used for scheduler timers, avoid lock inversion with
  949. * rq->lock and tasklist_lock.
  950. *
  951. * These timers are required to deal with enqueue expiry
  952. * themselves and are not allowed to migrate.
  953. */
  954. spin_unlock(&cpu_base->lock);
  955. restart = fn(timer);
  956. spin_lock(&cpu_base->lock);
  957. } else
  958. restart = fn(timer);
  959. /*
  960. * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
  961. * reprogramming of the event hardware. This happens at the end of this
  962. * function anyway.
  963. */
  964. if (restart != HRTIMER_NORESTART) {
  965. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  966. enqueue_hrtimer(timer, base, 0);
  967. }
  968. timer->state &= ~HRTIMER_STATE_CALLBACK;
  969. }
  970. #ifdef CONFIG_HIGH_RES_TIMERS
  971. /*
  972. * High resolution timer interrupt
  973. * Called with interrupts disabled
  974. */
  975. void hrtimer_interrupt(struct clock_event_device *dev)
  976. {
  977. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  978. struct hrtimer_clock_base *base;
  979. ktime_t expires_next, now;
  980. int i, raise = 0;
  981. BUG_ON(!cpu_base->hres_active);
  982. cpu_base->nr_events++;
  983. dev->next_event.tv64 = KTIME_MAX;
  984. retry:
  985. now = ktime_get();
  986. expires_next.tv64 = KTIME_MAX;
  987. base = cpu_base->clock_base;
  988. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  989. ktime_t basenow;
  990. struct rb_node *node;
  991. spin_lock(&cpu_base->lock);
  992. basenow = ktime_add(now, base->offset);
  993. while ((node = base->first)) {
  994. struct hrtimer *timer;
  995. timer = rb_entry(node, struct hrtimer, node);
  996. if (basenow.tv64 < timer->expires.tv64) {
  997. ktime_t expires;
  998. expires = ktime_sub(timer->expires,
  999. base->offset);
  1000. if (expires.tv64 < expires_next.tv64)
  1001. expires_next = expires;
  1002. break;
  1003. }
  1004. /* Move softirq callbacks to the pending list */
  1005. if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
  1006. __remove_hrtimer(timer, base,
  1007. HRTIMER_STATE_PENDING, 0);
  1008. list_add_tail(&timer->cb_entry,
  1009. &base->cpu_base->cb_pending);
  1010. raise = 1;
  1011. continue;
  1012. }
  1013. __run_hrtimer(timer);
  1014. }
  1015. spin_unlock(&cpu_base->lock);
  1016. base++;
  1017. }
  1018. cpu_base->expires_next = expires_next;
  1019. /* Reprogramming necessary ? */
  1020. if (expires_next.tv64 != KTIME_MAX) {
  1021. if (tick_program_event(expires_next, 0))
  1022. goto retry;
  1023. }
  1024. /* Raise softirq ? */
  1025. if (raise)
  1026. raise_softirq(HRTIMER_SOFTIRQ);
  1027. }
  1028. static void run_hrtimer_softirq(struct softirq_action *h)
  1029. {
  1030. run_hrtimer_pending(&__get_cpu_var(hrtimer_bases));
  1031. }
  1032. #endif /* CONFIG_HIGH_RES_TIMERS */
  1033. /*
  1034. * Called from timer softirq every jiffy, expire hrtimers:
  1035. *
  1036. * For HRT its the fall back code to run the softirq in the timer
  1037. * softirq context in case the hrtimer initialization failed or has
  1038. * not been done yet.
  1039. */
  1040. void hrtimer_run_pending(void)
  1041. {
  1042. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1043. if (hrtimer_hres_active())
  1044. return;
  1045. /*
  1046. * This _is_ ugly: We have to check in the softirq context,
  1047. * whether we can switch to highres and / or nohz mode. The
  1048. * clocksource switch happens in the timer interrupt with
  1049. * xtime_lock held. Notification from there only sets the
  1050. * check bit in the tick_oneshot code, otherwise we might
  1051. * deadlock vs. xtime_lock.
  1052. */
  1053. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
  1054. hrtimer_switch_to_hres();
  1055. run_hrtimer_pending(cpu_base);
  1056. }
  1057. /*
  1058. * Called from hardirq context every jiffy
  1059. */
  1060. void hrtimer_run_queues(void)
  1061. {
  1062. struct rb_node *node;
  1063. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1064. struct hrtimer_clock_base *base;
  1065. int index, gettime = 1;
  1066. if (hrtimer_hres_active())
  1067. return;
  1068. for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  1069. base = &cpu_base->clock_base[index];
  1070. if (!base->first)
  1071. continue;
  1072. if (base->get_softirq_time)
  1073. base->softirq_time = base->get_softirq_time();
  1074. else if (gettime) {
  1075. hrtimer_get_softirq_time(cpu_base);
  1076. gettime = 0;
  1077. }
  1078. spin_lock(&cpu_base->lock);
  1079. while ((node = base->first)) {
  1080. struct hrtimer *timer;
  1081. timer = rb_entry(node, struct hrtimer, node);
  1082. if (base->softirq_time.tv64 <= timer->expires.tv64)
  1083. break;
  1084. if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
  1085. __remove_hrtimer(timer, base,
  1086. HRTIMER_STATE_PENDING, 0);
  1087. list_add_tail(&timer->cb_entry,
  1088. &base->cpu_base->cb_pending);
  1089. continue;
  1090. }
  1091. __run_hrtimer(timer);
  1092. }
  1093. spin_unlock(&cpu_base->lock);
  1094. }
  1095. }
  1096. /*
  1097. * Sleep related functions:
  1098. */
  1099. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1100. {
  1101. struct hrtimer_sleeper *t =
  1102. container_of(timer, struct hrtimer_sleeper, timer);
  1103. struct task_struct *task = t->task;
  1104. t->task = NULL;
  1105. if (task)
  1106. wake_up_process(task);
  1107. return HRTIMER_NORESTART;
  1108. }
  1109. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1110. {
  1111. sl->timer.function = hrtimer_wakeup;
  1112. sl->task = task;
  1113. #ifdef CONFIG_HIGH_RES_TIMERS
  1114. sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
  1115. #endif
  1116. }
  1117. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1118. {
  1119. hrtimer_init_sleeper(t, current);
  1120. do {
  1121. set_current_state(TASK_INTERRUPTIBLE);
  1122. hrtimer_start(&t->timer, t->timer.expires, mode);
  1123. if (!hrtimer_active(&t->timer))
  1124. t->task = NULL;
  1125. if (likely(t->task))
  1126. schedule();
  1127. hrtimer_cancel(&t->timer);
  1128. mode = HRTIMER_MODE_ABS;
  1129. } while (t->task && !signal_pending(current));
  1130. __set_current_state(TASK_RUNNING);
  1131. return t->task == NULL;
  1132. }
  1133. static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
  1134. {
  1135. struct timespec rmt;
  1136. ktime_t rem;
  1137. rem = ktime_sub(timer->expires, timer->base->get_time());
  1138. if (rem.tv64 <= 0)
  1139. return 0;
  1140. rmt = ktime_to_timespec(rem);
  1141. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  1142. return -EFAULT;
  1143. return 1;
  1144. }
  1145. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1146. {
  1147. struct hrtimer_sleeper t;
  1148. struct timespec __user *rmtp;
  1149. hrtimer_init(&t.timer, restart->nanosleep.index, HRTIMER_MODE_ABS);
  1150. t.timer.expires.tv64 = restart->nanosleep.expires;
  1151. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  1152. return 0;
  1153. rmtp = restart->nanosleep.rmtp;
  1154. if (rmtp) {
  1155. int ret = update_rmtp(&t.timer, rmtp);
  1156. if (ret <= 0)
  1157. return ret;
  1158. }
  1159. /* The other values in restart are already filled in */
  1160. return -ERESTART_RESTARTBLOCK;
  1161. }
  1162. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  1163. const enum hrtimer_mode mode, const clockid_t clockid)
  1164. {
  1165. struct restart_block *restart;
  1166. struct hrtimer_sleeper t;
  1167. hrtimer_init(&t.timer, clockid, mode);
  1168. t.timer.expires = timespec_to_ktime(*rqtp);
  1169. if (do_nanosleep(&t, mode))
  1170. return 0;
  1171. /* Absolute timers do not update the rmtp value and restart: */
  1172. if (mode == HRTIMER_MODE_ABS)
  1173. return -ERESTARTNOHAND;
  1174. if (rmtp) {
  1175. int ret = update_rmtp(&t.timer, rmtp);
  1176. if (ret <= 0)
  1177. return ret;
  1178. }
  1179. restart = &current_thread_info()->restart_block;
  1180. restart->fn = hrtimer_nanosleep_restart;
  1181. restart->nanosleep.index = t.timer.base->index;
  1182. restart->nanosleep.rmtp = rmtp;
  1183. restart->nanosleep.expires = t.timer.expires.tv64;
  1184. return -ERESTART_RESTARTBLOCK;
  1185. }
  1186. asmlinkage long
  1187. sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
  1188. {
  1189. struct timespec tu;
  1190. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  1191. return -EFAULT;
  1192. if (!timespec_valid(&tu))
  1193. return -EINVAL;
  1194. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1195. }
  1196. /*
  1197. * Functions related to boot-time initialization:
  1198. */
  1199. static void __cpuinit init_hrtimers_cpu(int cpu)
  1200. {
  1201. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1202. int i;
  1203. spin_lock_init(&cpu_base->lock);
  1204. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  1205. cpu_base->clock_base[i].cpu_base = cpu_base;
  1206. INIT_LIST_HEAD(&cpu_base->cb_pending);
  1207. hrtimer_init_hres(cpu_base);
  1208. }
  1209. #ifdef CONFIG_HOTPLUG_CPU
  1210. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1211. struct hrtimer_clock_base *new_base)
  1212. {
  1213. struct hrtimer *timer;
  1214. struct rb_node *node;
  1215. while ((node = rb_first(&old_base->active))) {
  1216. timer = rb_entry(node, struct hrtimer, node);
  1217. BUG_ON(hrtimer_callback_running(timer));
  1218. __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE, 0);
  1219. timer->base = new_base;
  1220. /*
  1221. * Enqueue the timer. Allow reprogramming of the event device
  1222. */
  1223. enqueue_hrtimer(timer, new_base, 1);
  1224. }
  1225. }
  1226. static void migrate_hrtimers(int cpu)
  1227. {
  1228. struct hrtimer_cpu_base *old_base, *new_base;
  1229. int i;
  1230. BUG_ON(cpu_online(cpu));
  1231. old_base = &per_cpu(hrtimer_bases, cpu);
  1232. new_base = &get_cpu_var(hrtimer_bases);
  1233. tick_cancel_sched_timer(cpu);
  1234. local_irq_disable();
  1235. spin_lock(&new_base->lock);
  1236. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1237. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1238. migrate_hrtimer_list(&old_base->clock_base[i],
  1239. &new_base->clock_base[i]);
  1240. }
  1241. spin_unlock(&old_base->lock);
  1242. spin_unlock(&new_base->lock);
  1243. local_irq_enable();
  1244. put_cpu_var(hrtimer_bases);
  1245. }
  1246. #endif /* CONFIG_HOTPLUG_CPU */
  1247. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  1248. unsigned long action, void *hcpu)
  1249. {
  1250. unsigned int cpu = (long)hcpu;
  1251. switch (action) {
  1252. case CPU_UP_PREPARE:
  1253. case CPU_UP_PREPARE_FROZEN:
  1254. init_hrtimers_cpu(cpu);
  1255. break;
  1256. #ifdef CONFIG_HOTPLUG_CPU
  1257. case CPU_DEAD:
  1258. case CPU_DEAD_FROZEN:
  1259. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
  1260. migrate_hrtimers(cpu);
  1261. break;
  1262. #endif
  1263. default:
  1264. break;
  1265. }
  1266. return NOTIFY_OK;
  1267. }
  1268. static struct notifier_block __cpuinitdata hrtimers_nb = {
  1269. .notifier_call = hrtimer_cpu_notify,
  1270. };
  1271. void __init hrtimers_init(void)
  1272. {
  1273. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1274. (void *)(long)smp_processor_id());
  1275. register_cpu_notifier(&hrtimers_nb);
  1276. #ifdef CONFIG_HIGH_RES_TIMERS
  1277. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq, NULL);
  1278. #endif
  1279. }