hrtimer.c 35 KB

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