hrtimer.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  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/module.h>
  35. #include <linux/percpu.h>
  36. #include <linux/hrtimer.h>
  37. #include <linux/notifier.h>
  38. #include <linux/syscalls.h>
  39. #include <linux/kallsyms.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/tick.h>
  42. #include <linux/seq_file.h>
  43. #include <linux/err.h>
  44. #include <linux/debugobjects.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. * Functions and macros which are different for UP/SMP systems are kept in a
  138. * single place
  139. */
  140. #ifdef CONFIG_SMP
  141. /*
  142. * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
  143. * means that all timers which are tied to this base via timer->base are
  144. * locked, and the base itself is locked too.
  145. *
  146. * So __run_timers/migrate_timers can safely modify all timers which could
  147. * be found on the lists/queues.
  148. *
  149. * When the timer's base is locked, and the timer removed from list, it is
  150. * possible to set timer->base = NULL and drop the lock: the timer remains
  151. * locked.
  152. */
  153. static
  154. struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
  155. unsigned long *flags)
  156. {
  157. struct hrtimer_clock_base *base;
  158. for (;;) {
  159. base = timer->base;
  160. if (likely(base != NULL)) {
  161. spin_lock_irqsave(&base->cpu_base->lock, *flags);
  162. if (likely(base == timer->base))
  163. return base;
  164. /* The timer has migrated to another CPU: */
  165. spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
  166. }
  167. cpu_relax();
  168. }
  169. }
  170. /*
  171. * Switch the timer base to the current CPU when possible.
  172. */
  173. static inline struct hrtimer_clock_base *
  174. switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
  175. {
  176. struct hrtimer_clock_base *new_base;
  177. struct hrtimer_cpu_base *new_cpu_base;
  178. new_cpu_base = &__get_cpu_var(hrtimer_bases);
  179. new_base = &new_cpu_base->clock_base[base->index];
  180. if (base != new_base) {
  181. /*
  182. * We are trying to schedule the timer on the local CPU.
  183. * However we can't change timer's base while it is running,
  184. * so we keep it on the same CPU. No hassle vs. reprogramming
  185. * the event source in the high resolution case. The softirq
  186. * code will take care of this when the timer function has
  187. * completed. There is no conflict as we hold the lock until
  188. * the timer is enqueued.
  189. */
  190. if (unlikely(hrtimer_callback_running(timer)))
  191. return base;
  192. /* See the comment in lock_timer_base() */
  193. timer->base = NULL;
  194. spin_unlock(&base->cpu_base->lock);
  195. spin_lock(&new_base->cpu_base->lock);
  196. timer->base = new_base;
  197. }
  198. return new_base;
  199. }
  200. #else /* CONFIG_SMP */
  201. static inline struct hrtimer_clock_base *
  202. lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  203. {
  204. struct hrtimer_clock_base *base = timer->base;
  205. spin_lock_irqsave(&base->cpu_base->lock, *flags);
  206. return base;
  207. }
  208. # define switch_hrtimer_base(t, b) (b)
  209. #endif /* !CONFIG_SMP */
  210. /*
  211. * Functions for the union type storage format of ktime_t which are
  212. * too large for inlining:
  213. */
  214. #if BITS_PER_LONG < 64
  215. # ifndef CONFIG_KTIME_SCALAR
  216. /**
  217. * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
  218. * @kt: addend
  219. * @nsec: the scalar nsec value to add
  220. *
  221. * Returns the sum of kt and nsec in ktime_t format
  222. */
  223. ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
  224. {
  225. ktime_t tmp;
  226. if (likely(nsec < NSEC_PER_SEC)) {
  227. tmp.tv64 = nsec;
  228. } else {
  229. unsigned long rem = do_div(nsec, NSEC_PER_SEC);
  230. tmp = ktime_set((long)nsec, rem);
  231. }
  232. return ktime_add(kt, tmp);
  233. }
  234. EXPORT_SYMBOL_GPL(ktime_add_ns);
  235. /**
  236. * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
  237. * @kt: minuend
  238. * @nsec: the scalar nsec value to subtract
  239. *
  240. * Returns the subtraction of @nsec from @kt in ktime_t format
  241. */
  242. ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
  243. {
  244. ktime_t tmp;
  245. if (likely(nsec < NSEC_PER_SEC)) {
  246. tmp.tv64 = nsec;
  247. } else {
  248. unsigned long rem = do_div(nsec, NSEC_PER_SEC);
  249. tmp = ktime_set((long)nsec, rem);
  250. }
  251. return ktime_sub(kt, tmp);
  252. }
  253. EXPORT_SYMBOL_GPL(ktime_sub_ns);
  254. # endif /* !CONFIG_KTIME_SCALAR */
  255. /*
  256. * Divide a ktime value by a nanosecond value
  257. */
  258. u64 ktime_divns(const ktime_t kt, s64 div)
  259. {
  260. u64 dclc;
  261. int sft = 0;
  262. dclc = ktime_to_ns(kt);
  263. /* Make sure the divisor is less than 2^32: */
  264. while (div >> 32) {
  265. sft++;
  266. div >>= 1;
  267. }
  268. dclc >>= sft;
  269. do_div(dclc, (unsigned long) div);
  270. return dclc;
  271. }
  272. #endif /* BITS_PER_LONG >= 64 */
  273. /*
  274. * Add two ktime values and do a safety check for overflow:
  275. */
  276. ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
  277. {
  278. ktime_t res = ktime_add(lhs, rhs);
  279. /*
  280. * We use KTIME_SEC_MAX here, the maximum timeout which we can
  281. * return to user space in a timespec:
  282. */
  283. if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
  284. res = ktime_set(KTIME_SEC_MAX, 0);
  285. return res;
  286. }
  287. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  288. static struct debug_obj_descr hrtimer_debug_descr;
  289. /*
  290. * fixup_init is called when:
  291. * - an active object is initialized
  292. */
  293. static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
  294. {
  295. struct hrtimer *timer = addr;
  296. switch (state) {
  297. case ODEBUG_STATE_ACTIVE:
  298. hrtimer_cancel(timer);
  299. debug_object_init(timer, &hrtimer_debug_descr);
  300. return 1;
  301. default:
  302. return 0;
  303. }
  304. }
  305. /*
  306. * fixup_activate is called when:
  307. * - an active object is activated
  308. * - an unknown object is activated (might be a statically initialized object)
  309. */
  310. static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
  311. {
  312. switch (state) {
  313. case ODEBUG_STATE_NOTAVAILABLE:
  314. WARN_ON_ONCE(1);
  315. return 0;
  316. case ODEBUG_STATE_ACTIVE:
  317. WARN_ON(1);
  318. default:
  319. return 0;
  320. }
  321. }
  322. /*
  323. * fixup_free is called when:
  324. * - an active object is freed
  325. */
  326. static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
  327. {
  328. struct hrtimer *timer = addr;
  329. switch (state) {
  330. case ODEBUG_STATE_ACTIVE:
  331. hrtimer_cancel(timer);
  332. debug_object_free(timer, &hrtimer_debug_descr);
  333. return 1;
  334. default:
  335. return 0;
  336. }
  337. }
  338. static struct debug_obj_descr hrtimer_debug_descr = {
  339. .name = "hrtimer",
  340. .fixup_init = hrtimer_fixup_init,
  341. .fixup_activate = hrtimer_fixup_activate,
  342. .fixup_free = hrtimer_fixup_free,
  343. };
  344. static inline void debug_hrtimer_init(struct hrtimer *timer)
  345. {
  346. debug_object_init(timer, &hrtimer_debug_descr);
  347. }
  348. static inline void debug_hrtimer_activate(struct hrtimer *timer)
  349. {
  350. debug_object_activate(timer, &hrtimer_debug_descr);
  351. }
  352. static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
  353. {
  354. debug_object_deactivate(timer, &hrtimer_debug_descr);
  355. }
  356. static inline void debug_hrtimer_free(struct hrtimer *timer)
  357. {
  358. debug_object_free(timer, &hrtimer_debug_descr);
  359. }
  360. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  361. enum hrtimer_mode mode);
  362. void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
  363. enum hrtimer_mode mode)
  364. {
  365. debug_object_init_on_stack(timer, &hrtimer_debug_descr);
  366. __hrtimer_init(timer, clock_id, mode);
  367. }
  368. void destroy_hrtimer_on_stack(struct hrtimer *timer)
  369. {
  370. debug_object_free(timer, &hrtimer_debug_descr);
  371. }
  372. #else
  373. static inline void debug_hrtimer_init(struct hrtimer *timer) { }
  374. static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
  375. static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
  376. #endif
  377. /* High resolution timer related functions */
  378. #ifdef CONFIG_HIGH_RES_TIMERS
  379. /*
  380. * High resolution timer enabled ?
  381. */
  382. static int hrtimer_hres_enabled __read_mostly = 1;
  383. /*
  384. * Enable / Disable high resolution mode
  385. */
  386. static int __init setup_hrtimer_hres(char *str)
  387. {
  388. if (!strcmp(str, "off"))
  389. hrtimer_hres_enabled = 0;
  390. else if (!strcmp(str, "on"))
  391. hrtimer_hres_enabled = 1;
  392. else
  393. return 0;
  394. return 1;
  395. }
  396. __setup("highres=", setup_hrtimer_hres);
  397. /*
  398. * hrtimer_high_res_enabled - query, if the highres mode is enabled
  399. */
  400. static inline int hrtimer_is_hres_enabled(void)
  401. {
  402. return hrtimer_hres_enabled;
  403. }
  404. /*
  405. * Is the high resolution mode active ?
  406. */
  407. static inline int hrtimer_hres_active(void)
  408. {
  409. return __get_cpu_var(hrtimer_bases).hres_active;
  410. }
  411. /*
  412. * Reprogram the event source with checking both queues for the
  413. * next event
  414. * Called with interrupts disabled and base->lock held
  415. */
  416. static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
  417. {
  418. int i;
  419. struct hrtimer_clock_base *base = cpu_base->clock_base;
  420. ktime_t expires;
  421. cpu_base->expires_next.tv64 = KTIME_MAX;
  422. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  423. struct hrtimer *timer;
  424. if (!base->first)
  425. continue;
  426. timer = rb_entry(base->first, struct hrtimer, node);
  427. expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  428. if (expires.tv64 < cpu_base->expires_next.tv64)
  429. cpu_base->expires_next = expires;
  430. }
  431. if (cpu_base->expires_next.tv64 != KTIME_MAX)
  432. tick_program_event(cpu_base->expires_next, 1);
  433. }
  434. /*
  435. * Shared reprogramming for clock_realtime and clock_monotonic
  436. *
  437. * When a timer is enqueued and expires earlier than the already enqueued
  438. * timers, we have to check, whether it expires earlier than the timer for
  439. * which the clock event device was armed.
  440. *
  441. * Called with interrupts disabled and base->cpu_base.lock held
  442. */
  443. static int hrtimer_reprogram(struct hrtimer *timer,
  444. struct hrtimer_clock_base *base)
  445. {
  446. ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
  447. ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  448. int res;
  449. WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
  450. /*
  451. * When the callback is running, we do not reprogram the clock event
  452. * device. The timer callback is either running on a different CPU or
  453. * the callback is executed in the hrtimer_interrupt context. The
  454. * reprogramming is handled either by the softirq, which called the
  455. * callback or at the end of the hrtimer_interrupt.
  456. */
  457. if (hrtimer_callback_running(timer))
  458. return 0;
  459. /*
  460. * CLOCK_REALTIME timer might be requested with an absolute
  461. * expiry time which is less than base->offset. Nothing wrong
  462. * about that, just avoid to call into the tick code, which
  463. * has now objections against negative expiry values.
  464. */
  465. if (expires.tv64 < 0)
  466. return -ETIME;
  467. if (expires.tv64 >= expires_next->tv64)
  468. return 0;
  469. /*
  470. * Clockevents returns -ETIME, when the event was in the past.
  471. */
  472. res = tick_program_event(expires, 0);
  473. if (!IS_ERR_VALUE(res))
  474. *expires_next = expires;
  475. return res;
  476. }
  477. /*
  478. * Retrigger next event is called after clock was set
  479. *
  480. * Called with interrupts disabled via on_each_cpu()
  481. */
  482. static void retrigger_next_event(void *arg)
  483. {
  484. struct hrtimer_cpu_base *base;
  485. struct timespec realtime_offset;
  486. unsigned long seq;
  487. if (!hrtimer_hres_active())
  488. return;
  489. do {
  490. seq = read_seqbegin(&xtime_lock);
  491. set_normalized_timespec(&realtime_offset,
  492. -wall_to_monotonic.tv_sec,
  493. -wall_to_monotonic.tv_nsec);
  494. } while (read_seqretry(&xtime_lock, seq));
  495. base = &__get_cpu_var(hrtimer_bases);
  496. /* Adjust CLOCK_REALTIME offset */
  497. spin_lock(&base->lock);
  498. base->clock_base[CLOCK_REALTIME].offset =
  499. timespec_to_ktime(realtime_offset);
  500. hrtimer_force_reprogram(base);
  501. spin_unlock(&base->lock);
  502. }
  503. /*
  504. * Clock realtime was set
  505. *
  506. * Change the offset of the realtime clock vs. the monotonic
  507. * clock.
  508. *
  509. * We might have to reprogram the high resolution timer interrupt. On
  510. * SMP we call the architecture specific code to retrigger _all_ high
  511. * resolution timer interrupts. On UP we just disable interrupts and
  512. * call the high resolution interrupt code.
  513. */
  514. void clock_was_set(void)
  515. {
  516. /* Retrigger the CPU local events everywhere */
  517. on_each_cpu(retrigger_next_event, NULL, 1);
  518. }
  519. /*
  520. * During resume we might have to reprogram the high resolution timer
  521. * interrupt (on the local CPU):
  522. */
  523. void hres_timers_resume(void)
  524. {
  525. /* Retrigger the CPU local events: */
  526. retrigger_next_event(NULL);
  527. }
  528. /*
  529. * Initialize the high resolution related parts of cpu_base
  530. */
  531. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
  532. {
  533. base->expires_next.tv64 = KTIME_MAX;
  534. base->hres_active = 0;
  535. }
  536. /*
  537. * Initialize the high resolution related parts of a hrtimer
  538. */
  539. static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
  540. {
  541. }
  542. /*
  543. * When High resolution timers are active, try to reprogram. Note, that in case
  544. * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
  545. * check happens. The timer gets enqueued into the rbtree. The reprogramming
  546. * and expiry check is done in the hrtimer_interrupt or in the softirq.
  547. */
  548. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  549. struct hrtimer_clock_base *base)
  550. {
  551. if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
  552. spin_unlock(&base->cpu_base->lock);
  553. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  554. spin_lock(&base->cpu_base->lock);
  555. return 1;
  556. }
  557. return 0;
  558. }
  559. /*
  560. * Switch to high resolution mode
  561. */
  562. static int hrtimer_switch_to_hres(void)
  563. {
  564. int cpu = smp_processor_id();
  565. struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
  566. unsigned long flags;
  567. if (base->hres_active)
  568. return 1;
  569. local_irq_save(flags);
  570. if (tick_init_highres()) {
  571. local_irq_restore(flags);
  572. printk(KERN_WARNING "Could not switch to high resolution "
  573. "mode on CPU %d\n", cpu);
  574. return 0;
  575. }
  576. base->hres_active = 1;
  577. base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
  578. base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
  579. tick_setup_sched_timer();
  580. /* "Retrigger" the interrupt to get things going */
  581. retrigger_next_event(NULL);
  582. local_irq_restore(flags);
  583. printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
  584. smp_processor_id());
  585. return 1;
  586. }
  587. #else
  588. static inline int hrtimer_hres_active(void) { return 0; }
  589. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  590. static inline int hrtimer_switch_to_hres(void) { return 0; }
  591. static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
  592. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  593. struct hrtimer_clock_base *base)
  594. {
  595. return 0;
  596. }
  597. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  598. static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
  599. #endif /* CONFIG_HIGH_RES_TIMERS */
  600. #ifdef CONFIG_TIMER_STATS
  601. void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
  602. {
  603. if (timer->start_site)
  604. return;
  605. timer->start_site = addr;
  606. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  607. timer->start_pid = current->pid;
  608. }
  609. #endif
  610. /*
  611. * Counterpart to lock_hrtimer_base above:
  612. */
  613. static inline
  614. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  615. {
  616. spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  617. }
  618. /**
  619. * hrtimer_forward - forward the timer expiry
  620. * @timer: hrtimer to forward
  621. * @now: forward past this time
  622. * @interval: the interval to forward
  623. *
  624. * Forward the timer expiry so it will expire in the future.
  625. * Returns the number of overruns.
  626. */
  627. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  628. {
  629. u64 orun = 1;
  630. ktime_t delta;
  631. delta = ktime_sub(now, hrtimer_get_expires(timer));
  632. if (delta.tv64 < 0)
  633. return 0;
  634. if (interval.tv64 < timer->base->resolution.tv64)
  635. interval.tv64 = timer->base->resolution.tv64;
  636. if (unlikely(delta.tv64 >= interval.tv64)) {
  637. s64 incr = ktime_to_ns(interval);
  638. orun = ktime_divns(delta, incr);
  639. hrtimer_add_expires_ns(timer, incr * orun);
  640. if (hrtimer_get_expires_tv64(timer) > now.tv64)
  641. return orun;
  642. /*
  643. * This (and the ktime_add() below) is the
  644. * correction for exact:
  645. */
  646. orun++;
  647. }
  648. hrtimer_add_expires(timer, interval);
  649. return orun;
  650. }
  651. EXPORT_SYMBOL_GPL(hrtimer_forward);
  652. /*
  653. * enqueue_hrtimer - internal function to (re)start a timer
  654. *
  655. * The timer is inserted in expiry order. Insertion into the
  656. * red black tree is O(log(n)). Must hold the base lock.
  657. *
  658. * Returns 1 when the new timer is the leftmost timer in the tree.
  659. */
  660. static int enqueue_hrtimer(struct hrtimer *timer,
  661. struct hrtimer_clock_base *base)
  662. {
  663. struct rb_node **link = &base->active.rb_node;
  664. struct rb_node *parent = NULL;
  665. struct hrtimer *entry;
  666. int leftmost = 1;
  667. debug_hrtimer_activate(timer);
  668. /*
  669. * Find the right place in the rbtree:
  670. */
  671. while (*link) {
  672. parent = *link;
  673. entry = rb_entry(parent, struct hrtimer, node);
  674. /*
  675. * We dont care about collisions. Nodes with
  676. * the same expiry time stay together.
  677. */
  678. if (hrtimer_get_expires_tv64(timer) <
  679. hrtimer_get_expires_tv64(entry)) {
  680. link = &(*link)->rb_left;
  681. } else {
  682. link = &(*link)->rb_right;
  683. leftmost = 0;
  684. }
  685. }
  686. /*
  687. * Insert the timer to the rbtree and check whether it
  688. * replaces the first pending timer
  689. */
  690. if (leftmost)
  691. base->first = &timer->node;
  692. rb_link_node(&timer->node, parent, link);
  693. rb_insert_color(&timer->node, &base->active);
  694. /*
  695. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  696. * state of a possibly running callback.
  697. */
  698. timer->state |= HRTIMER_STATE_ENQUEUED;
  699. return leftmost;
  700. }
  701. /*
  702. * __remove_hrtimer - internal function to remove a timer
  703. *
  704. * Caller must hold the base lock.
  705. *
  706. * High resolution timer mode reprograms the clock event device when the
  707. * timer is the one which expires next. The caller can disable this by setting
  708. * reprogram to zero. This is useful, when the context does a reprogramming
  709. * anyway (e.g. timer interrupt)
  710. */
  711. static void __remove_hrtimer(struct hrtimer *timer,
  712. struct hrtimer_clock_base *base,
  713. unsigned long newstate, int reprogram)
  714. {
  715. if (timer->state & HRTIMER_STATE_ENQUEUED) {
  716. /*
  717. * Remove the timer from the rbtree and replace the
  718. * first entry pointer if necessary.
  719. */
  720. if (base->first == &timer->node) {
  721. base->first = rb_next(&timer->node);
  722. /* Reprogram the clock event device. if enabled */
  723. if (reprogram && hrtimer_hres_active())
  724. hrtimer_force_reprogram(base->cpu_base);
  725. }
  726. rb_erase(&timer->node, &base->active);
  727. }
  728. timer->state = newstate;
  729. }
  730. /*
  731. * remove hrtimer, called with base lock held
  732. */
  733. static inline int
  734. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  735. {
  736. if (hrtimer_is_queued(timer)) {
  737. int reprogram;
  738. /*
  739. * Remove the timer and force reprogramming when high
  740. * resolution mode is active and the timer is on the current
  741. * CPU. If we remove a timer on another CPU, reprogramming is
  742. * skipped. The interrupt event on this CPU is fired and
  743. * reprogramming happens in the interrupt handler. This is a
  744. * rare case and less expensive than a smp call.
  745. */
  746. debug_hrtimer_deactivate(timer);
  747. timer_stats_hrtimer_clear_start_info(timer);
  748. reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
  749. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
  750. reprogram);
  751. return 1;
  752. }
  753. return 0;
  754. }
  755. /**
  756. * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
  757. * @timer: the timer to be added
  758. * @tim: expiry time
  759. * @delta_ns: "slack" range for the timer
  760. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  761. *
  762. * Returns:
  763. * 0 on success
  764. * 1 when the timer was active
  765. */
  766. int
  767. hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, unsigned long delta_ns,
  768. const enum hrtimer_mode mode)
  769. {
  770. struct hrtimer_clock_base *base, *new_base;
  771. unsigned long flags;
  772. int ret, leftmost;
  773. base = lock_hrtimer_base(timer, &flags);
  774. /* Remove an active timer from the queue: */
  775. ret = remove_hrtimer(timer, base);
  776. /* Switch the timer base, if necessary: */
  777. new_base = switch_hrtimer_base(timer, base);
  778. if (mode == HRTIMER_MODE_REL) {
  779. tim = ktime_add_safe(tim, new_base->get_time());
  780. /*
  781. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  782. * to signal that they simply return xtime in
  783. * do_gettimeoffset(). In this case we want to round up by
  784. * resolution when starting a relative timer, to avoid short
  785. * timeouts. This will go away with the GTOD framework.
  786. */
  787. #ifdef CONFIG_TIME_LOW_RES
  788. tim = ktime_add_safe(tim, base->resolution);
  789. #endif
  790. }
  791. hrtimer_set_expires_range_ns(timer, tim, delta_ns);
  792. timer_stats_hrtimer_set_start_info(timer);
  793. leftmost = enqueue_hrtimer(timer, new_base);
  794. /*
  795. * Only allow reprogramming if the new base is on this CPU.
  796. * (it might still be on another CPU if the timer was pending)
  797. *
  798. * XXX send_remote_softirq() ?
  799. */
  800. if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
  801. hrtimer_enqueue_reprogram(timer, new_base);
  802. unlock_hrtimer_base(timer, &flags);
  803. return ret;
  804. }
  805. EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
  806. /**
  807. * hrtimer_start - (re)start an hrtimer on the current CPU
  808. * @timer: the timer to be added
  809. * @tim: expiry time
  810. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  811. *
  812. * Returns:
  813. * 0 on success
  814. * 1 when the timer was active
  815. */
  816. int
  817. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  818. {
  819. return hrtimer_start_range_ns(timer, tim, 0, mode);
  820. }
  821. EXPORT_SYMBOL_GPL(hrtimer_start);
  822. /**
  823. * hrtimer_try_to_cancel - try to deactivate a timer
  824. * @timer: hrtimer to stop
  825. *
  826. * Returns:
  827. * 0 when the timer was not active
  828. * 1 when the timer was active
  829. * -1 when the timer is currently excuting the callback function and
  830. * cannot be stopped
  831. */
  832. int hrtimer_try_to_cancel(struct hrtimer *timer)
  833. {
  834. struct hrtimer_clock_base *base;
  835. unsigned long flags;
  836. int ret = -1;
  837. base = lock_hrtimer_base(timer, &flags);
  838. if (!hrtimer_callback_running(timer))
  839. ret = remove_hrtimer(timer, base);
  840. unlock_hrtimer_base(timer, &flags);
  841. return ret;
  842. }
  843. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  844. /**
  845. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  846. * @timer: the timer to be cancelled
  847. *
  848. * Returns:
  849. * 0 when the timer was not active
  850. * 1 when the timer was active
  851. */
  852. int hrtimer_cancel(struct hrtimer *timer)
  853. {
  854. for (;;) {
  855. int ret = hrtimer_try_to_cancel(timer);
  856. if (ret >= 0)
  857. return ret;
  858. cpu_relax();
  859. }
  860. }
  861. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  862. /**
  863. * hrtimer_get_remaining - get remaining time for the timer
  864. * @timer: the timer to read
  865. */
  866. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  867. {
  868. struct hrtimer_clock_base *base;
  869. unsigned long flags;
  870. ktime_t rem;
  871. base = lock_hrtimer_base(timer, &flags);
  872. rem = hrtimer_expires_remaining(timer);
  873. unlock_hrtimer_base(timer, &flags);
  874. return rem;
  875. }
  876. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  877. #ifdef CONFIG_NO_HZ
  878. /**
  879. * hrtimer_get_next_event - get the time until next expiry event
  880. *
  881. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  882. * is pending.
  883. */
  884. ktime_t hrtimer_get_next_event(void)
  885. {
  886. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  887. struct hrtimer_clock_base *base = cpu_base->clock_base;
  888. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  889. unsigned long flags;
  890. int i;
  891. spin_lock_irqsave(&cpu_base->lock, flags);
  892. if (!hrtimer_hres_active()) {
  893. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  894. struct hrtimer *timer;
  895. if (!base->first)
  896. continue;
  897. timer = rb_entry(base->first, struct hrtimer, node);
  898. delta.tv64 = hrtimer_get_expires_tv64(timer);
  899. delta = ktime_sub(delta, base->get_time());
  900. if (delta.tv64 < mindelta.tv64)
  901. mindelta.tv64 = delta.tv64;
  902. }
  903. }
  904. spin_unlock_irqrestore(&cpu_base->lock, flags);
  905. if (mindelta.tv64 < 0)
  906. mindelta.tv64 = 0;
  907. return mindelta;
  908. }
  909. #endif
  910. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  911. enum hrtimer_mode mode)
  912. {
  913. struct hrtimer_cpu_base *cpu_base;
  914. memset(timer, 0, sizeof(struct hrtimer));
  915. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  916. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  917. clock_id = CLOCK_MONOTONIC;
  918. timer->base = &cpu_base->clock_base[clock_id];
  919. INIT_LIST_HEAD(&timer->cb_entry);
  920. hrtimer_init_timer_hres(timer);
  921. #ifdef CONFIG_TIMER_STATS
  922. timer->start_site = NULL;
  923. timer->start_pid = -1;
  924. memset(timer->start_comm, 0, TASK_COMM_LEN);
  925. #endif
  926. }
  927. /**
  928. * hrtimer_init - initialize a timer to the given clock
  929. * @timer: the timer to be initialized
  930. * @clock_id: the clock to be used
  931. * @mode: timer mode abs/rel
  932. */
  933. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  934. enum hrtimer_mode mode)
  935. {
  936. debug_hrtimer_init(timer);
  937. __hrtimer_init(timer, clock_id, mode);
  938. }
  939. EXPORT_SYMBOL_GPL(hrtimer_init);
  940. /**
  941. * hrtimer_get_res - get the timer resolution for a clock
  942. * @which_clock: which clock to query
  943. * @tp: pointer to timespec variable to store the resolution
  944. *
  945. * Store the resolution of the clock selected by @which_clock in the
  946. * variable pointed to by @tp.
  947. */
  948. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  949. {
  950. struct hrtimer_cpu_base *cpu_base;
  951. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  952. *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
  953. return 0;
  954. }
  955. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  956. static void __run_hrtimer(struct hrtimer *timer)
  957. {
  958. struct hrtimer_clock_base *base = timer->base;
  959. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  960. enum hrtimer_restart (*fn)(struct hrtimer *);
  961. int restart;
  962. WARN_ON(!irqs_disabled());
  963. debug_hrtimer_deactivate(timer);
  964. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  965. timer_stats_account_hrtimer(timer);
  966. fn = timer->function;
  967. /*
  968. * Because we run timers from hardirq context, there is no chance
  969. * they get migrated to another cpu, therefore its safe to unlock
  970. * the timer base.
  971. */
  972. spin_unlock(&cpu_base->lock);
  973. restart = fn(timer);
  974. spin_lock(&cpu_base->lock);
  975. /*
  976. * Note: We clear the CALLBACK bit after enqueue_hrtimer and
  977. * we do not reprogramm the event hardware. Happens either in
  978. * hrtimer_start_range_ns() or in hrtimer_interrupt()
  979. */
  980. if (restart != HRTIMER_NORESTART) {
  981. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  982. enqueue_hrtimer(timer, base);
  983. }
  984. timer->state &= ~HRTIMER_STATE_CALLBACK;
  985. }
  986. #ifdef CONFIG_HIGH_RES_TIMERS
  987. /*
  988. * High resolution timer interrupt
  989. * Called with interrupts disabled
  990. */
  991. void hrtimer_interrupt(struct clock_event_device *dev)
  992. {
  993. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  994. struct hrtimer_clock_base *base;
  995. ktime_t expires_next, now;
  996. int i;
  997. BUG_ON(!cpu_base->hres_active);
  998. cpu_base->nr_events++;
  999. dev->next_event.tv64 = KTIME_MAX;
  1000. retry:
  1001. now = ktime_get();
  1002. expires_next.tv64 = KTIME_MAX;
  1003. base = cpu_base->clock_base;
  1004. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1005. ktime_t basenow;
  1006. struct rb_node *node;
  1007. spin_lock(&cpu_base->lock);
  1008. basenow = ktime_add(now, base->offset);
  1009. while ((node = base->first)) {
  1010. struct hrtimer *timer;
  1011. timer = rb_entry(node, struct hrtimer, node);
  1012. /*
  1013. * The immediate goal for using the softexpires is
  1014. * minimizing wakeups, not running timers at the
  1015. * earliest interrupt after their soft expiration.
  1016. * This allows us to avoid using a Priority Search
  1017. * Tree, which can answer a stabbing querry for
  1018. * overlapping intervals and instead use the simple
  1019. * BST we already have.
  1020. * We don't add extra wakeups by delaying timers that
  1021. * are right-of a not yet expired timer, because that
  1022. * timer will have to trigger a wakeup anyway.
  1023. */
  1024. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
  1025. ktime_t expires;
  1026. expires = ktime_sub(hrtimer_get_expires(timer),
  1027. base->offset);
  1028. if (expires.tv64 < expires_next.tv64)
  1029. expires_next = expires;
  1030. break;
  1031. }
  1032. __run_hrtimer(timer);
  1033. }
  1034. spin_unlock(&cpu_base->lock);
  1035. base++;
  1036. }
  1037. cpu_base->expires_next = expires_next;
  1038. /* Reprogramming necessary ? */
  1039. if (expires_next.tv64 != KTIME_MAX) {
  1040. if (tick_program_event(expires_next, 0))
  1041. goto retry;
  1042. }
  1043. }
  1044. /*
  1045. * local version of hrtimer_peek_ahead_timers() called with interrupts
  1046. * disabled.
  1047. */
  1048. static void __hrtimer_peek_ahead_timers(void)
  1049. {
  1050. struct tick_device *td;
  1051. if (!hrtimer_hres_active())
  1052. return;
  1053. td = &__get_cpu_var(tick_cpu_device);
  1054. if (td && td->evtdev)
  1055. hrtimer_interrupt(td->evtdev);
  1056. }
  1057. /**
  1058. * hrtimer_peek_ahead_timers -- run soft-expired timers now
  1059. *
  1060. * hrtimer_peek_ahead_timers will peek at the timer queue of
  1061. * the current cpu and check if there are any timers for which
  1062. * the soft expires time has passed. If any such timers exist,
  1063. * they are run immediately and then removed from the timer queue.
  1064. *
  1065. */
  1066. void hrtimer_peek_ahead_timers(void)
  1067. {
  1068. unsigned long flags;
  1069. local_irq_save(flags);
  1070. __hrtimer_peek_ahead_timers();
  1071. local_irq_restore(flags);
  1072. }
  1073. static void run_hrtimer_softirq(struct softirq_action *h)
  1074. {
  1075. hrtimer_peek_ahead_timers();
  1076. }
  1077. #else /* CONFIG_HIGH_RES_TIMERS */
  1078. static inline void __hrtimer_peek_ahead_timers(void) { }
  1079. #endif /* !CONFIG_HIGH_RES_TIMERS */
  1080. /*
  1081. * Called from timer softirq every jiffy, expire hrtimers:
  1082. *
  1083. * For HRT its the fall back code to run the softirq in the timer
  1084. * softirq context in case the hrtimer initialization failed or has
  1085. * not been done yet.
  1086. */
  1087. void hrtimer_run_pending(void)
  1088. {
  1089. if (hrtimer_hres_active())
  1090. return;
  1091. /*
  1092. * This _is_ ugly: We have to check in the softirq context,
  1093. * whether we can switch to highres and / or nohz mode. The
  1094. * clocksource switch happens in the timer interrupt with
  1095. * xtime_lock held. Notification from there only sets the
  1096. * check bit in the tick_oneshot code, otherwise we might
  1097. * deadlock vs. xtime_lock.
  1098. */
  1099. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
  1100. hrtimer_switch_to_hres();
  1101. }
  1102. /*
  1103. * Called from hardirq context every jiffy
  1104. */
  1105. void hrtimer_run_queues(void)
  1106. {
  1107. struct rb_node *node;
  1108. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1109. struct hrtimer_clock_base *base;
  1110. int index, gettime = 1;
  1111. if (hrtimer_hres_active())
  1112. return;
  1113. for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  1114. base = &cpu_base->clock_base[index];
  1115. if (!base->first)
  1116. continue;
  1117. if (gettime) {
  1118. hrtimer_get_softirq_time(cpu_base);
  1119. gettime = 0;
  1120. }
  1121. spin_lock(&cpu_base->lock);
  1122. while ((node = base->first)) {
  1123. struct hrtimer *timer;
  1124. timer = rb_entry(node, struct hrtimer, node);
  1125. if (base->softirq_time.tv64 <=
  1126. hrtimer_get_expires_tv64(timer))
  1127. break;
  1128. __run_hrtimer(timer);
  1129. }
  1130. spin_unlock(&cpu_base->lock);
  1131. }
  1132. }
  1133. /*
  1134. * Sleep related functions:
  1135. */
  1136. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1137. {
  1138. struct hrtimer_sleeper *t =
  1139. container_of(timer, struct hrtimer_sleeper, timer);
  1140. struct task_struct *task = t->task;
  1141. t->task = NULL;
  1142. if (task)
  1143. wake_up_process(task);
  1144. return HRTIMER_NORESTART;
  1145. }
  1146. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1147. {
  1148. sl->timer.function = hrtimer_wakeup;
  1149. sl->task = task;
  1150. }
  1151. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1152. {
  1153. hrtimer_init_sleeper(t, current);
  1154. do {
  1155. set_current_state(TASK_INTERRUPTIBLE);
  1156. hrtimer_start_expires(&t->timer, mode);
  1157. if (!hrtimer_active(&t->timer))
  1158. t->task = NULL;
  1159. if (likely(t->task))
  1160. schedule();
  1161. hrtimer_cancel(&t->timer);
  1162. mode = HRTIMER_MODE_ABS;
  1163. } while (t->task && !signal_pending(current));
  1164. __set_current_state(TASK_RUNNING);
  1165. return t->task == NULL;
  1166. }
  1167. static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
  1168. {
  1169. struct timespec rmt;
  1170. ktime_t rem;
  1171. rem = hrtimer_expires_remaining(timer);
  1172. if (rem.tv64 <= 0)
  1173. return 0;
  1174. rmt = ktime_to_timespec(rem);
  1175. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  1176. return -EFAULT;
  1177. return 1;
  1178. }
  1179. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1180. {
  1181. struct hrtimer_sleeper t;
  1182. struct timespec __user *rmtp;
  1183. int ret = 0;
  1184. hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
  1185. HRTIMER_MODE_ABS);
  1186. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  1187. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  1188. goto out;
  1189. rmtp = restart->nanosleep.rmtp;
  1190. if (rmtp) {
  1191. ret = update_rmtp(&t.timer, rmtp);
  1192. if (ret <= 0)
  1193. goto out;
  1194. }
  1195. /* The other values in restart are already filled in */
  1196. ret = -ERESTART_RESTARTBLOCK;
  1197. out:
  1198. destroy_hrtimer_on_stack(&t.timer);
  1199. return ret;
  1200. }
  1201. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  1202. const enum hrtimer_mode mode, const clockid_t clockid)
  1203. {
  1204. struct restart_block *restart;
  1205. struct hrtimer_sleeper t;
  1206. int ret = 0;
  1207. unsigned long slack;
  1208. slack = current->timer_slack_ns;
  1209. if (rt_task(current))
  1210. slack = 0;
  1211. hrtimer_init_on_stack(&t.timer, clockid, mode);
  1212. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  1213. if (do_nanosleep(&t, mode))
  1214. goto out;
  1215. /* Absolute timers do not update the rmtp value and restart: */
  1216. if (mode == HRTIMER_MODE_ABS) {
  1217. ret = -ERESTARTNOHAND;
  1218. goto out;
  1219. }
  1220. if (rmtp) {
  1221. ret = update_rmtp(&t.timer, rmtp);
  1222. if (ret <= 0)
  1223. goto out;
  1224. }
  1225. restart = &current_thread_info()->restart_block;
  1226. restart->fn = hrtimer_nanosleep_restart;
  1227. restart->nanosleep.index = t.timer.base->index;
  1228. restart->nanosleep.rmtp = rmtp;
  1229. restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
  1230. ret = -ERESTART_RESTARTBLOCK;
  1231. out:
  1232. destroy_hrtimer_on_stack(&t.timer);
  1233. return ret;
  1234. }
  1235. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  1236. struct timespec __user *, rmtp)
  1237. {
  1238. struct timespec tu;
  1239. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  1240. return -EFAULT;
  1241. if (!timespec_valid(&tu))
  1242. return -EINVAL;
  1243. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1244. }
  1245. /*
  1246. * Functions related to boot-time initialization:
  1247. */
  1248. static void __cpuinit init_hrtimers_cpu(int cpu)
  1249. {
  1250. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1251. int i;
  1252. spin_lock_init(&cpu_base->lock);
  1253. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  1254. cpu_base->clock_base[i].cpu_base = cpu_base;
  1255. hrtimer_init_hres(cpu_base);
  1256. }
  1257. #ifdef CONFIG_HOTPLUG_CPU
  1258. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1259. struct hrtimer_clock_base *new_base)
  1260. {
  1261. struct hrtimer *timer;
  1262. struct rb_node *node;
  1263. while ((node = rb_first(&old_base->active))) {
  1264. timer = rb_entry(node, struct hrtimer, node);
  1265. BUG_ON(hrtimer_callback_running(timer));
  1266. debug_hrtimer_deactivate(timer);
  1267. /*
  1268. * Mark it as STATE_MIGRATE not INACTIVE otherwise the
  1269. * timer could be seen as !active and just vanish away
  1270. * under us on another CPU
  1271. */
  1272. __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
  1273. timer->base = new_base;
  1274. /*
  1275. * Enqueue the timers on the new cpu. This does not
  1276. * reprogram the event device in case the timer
  1277. * expires before the earliest on this CPU, but we run
  1278. * hrtimer_interrupt after we migrated everything to
  1279. * sort out already expired timers and reprogram the
  1280. * event device.
  1281. */
  1282. enqueue_hrtimer(timer, new_base);
  1283. /* Clear the migration state bit */
  1284. timer->state &= ~HRTIMER_STATE_MIGRATE;
  1285. }
  1286. }
  1287. static void migrate_hrtimers(int scpu)
  1288. {
  1289. struct hrtimer_cpu_base *old_base, *new_base;
  1290. int i;
  1291. BUG_ON(cpu_online(scpu));
  1292. tick_cancel_sched_timer(scpu);
  1293. local_irq_disable();
  1294. old_base = &per_cpu(hrtimer_bases, scpu);
  1295. new_base = &__get_cpu_var(hrtimer_bases);
  1296. /*
  1297. * The caller is globally serialized and nobody else
  1298. * takes two locks at once, deadlock is not possible.
  1299. */
  1300. spin_lock(&new_base->lock);
  1301. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1302. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1303. migrate_hrtimer_list(&old_base->clock_base[i],
  1304. &new_base->clock_base[i]);
  1305. }
  1306. spin_unlock(&old_base->lock);
  1307. spin_unlock(&new_base->lock);
  1308. /* Check, if we got expired work to do */
  1309. __hrtimer_peek_ahead_timers();
  1310. local_irq_enable();
  1311. }
  1312. #endif /* CONFIG_HOTPLUG_CPU */
  1313. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  1314. unsigned long action, void *hcpu)
  1315. {
  1316. int scpu = (long)hcpu;
  1317. switch (action) {
  1318. case CPU_UP_PREPARE:
  1319. case CPU_UP_PREPARE_FROZEN:
  1320. init_hrtimers_cpu(scpu);
  1321. break;
  1322. #ifdef CONFIG_HOTPLUG_CPU
  1323. case CPU_DEAD:
  1324. case CPU_DEAD_FROZEN:
  1325. {
  1326. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
  1327. migrate_hrtimers(scpu);
  1328. break;
  1329. }
  1330. #endif
  1331. default:
  1332. break;
  1333. }
  1334. return NOTIFY_OK;
  1335. }
  1336. static struct notifier_block __cpuinitdata hrtimers_nb = {
  1337. .notifier_call = hrtimer_cpu_notify,
  1338. };
  1339. void __init hrtimers_init(void)
  1340. {
  1341. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1342. (void *)(long)smp_processor_id());
  1343. register_cpu_notifier(&hrtimers_nb);
  1344. #ifdef CONFIG_HIGH_RES_TIMERS
  1345. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  1346. #endif
  1347. }
  1348. /**
  1349. * schedule_hrtimeout_range - sleep until timeout
  1350. * @expires: timeout value (ktime_t)
  1351. * @delta: slack in expires timeout (ktime_t)
  1352. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1353. *
  1354. * Make the current task sleep until the given expiry time has
  1355. * elapsed. The routine will return immediately unless
  1356. * the current task state has been set (see set_current_state()).
  1357. *
  1358. * The @delta argument gives the kernel the freedom to schedule the
  1359. * actual wakeup to a time that is both power and performance friendly.
  1360. * The kernel give the normal best effort behavior for "@expires+@delta",
  1361. * but may decide to fire the timer earlier, but no earlier than @expires.
  1362. *
  1363. * You can set the task state as follows -
  1364. *
  1365. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1366. * pass before the routine returns.
  1367. *
  1368. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1369. * delivered to the current task.
  1370. *
  1371. * The current task state is guaranteed to be TASK_RUNNING when this
  1372. * routine returns.
  1373. *
  1374. * Returns 0 when the timer has expired otherwise -EINTR
  1375. */
  1376. int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
  1377. const enum hrtimer_mode mode)
  1378. {
  1379. struct hrtimer_sleeper t;
  1380. /*
  1381. * Optimize when a zero timeout value is given. It does not
  1382. * matter whether this is an absolute or a relative time.
  1383. */
  1384. if (expires && !expires->tv64) {
  1385. __set_current_state(TASK_RUNNING);
  1386. return 0;
  1387. }
  1388. /*
  1389. * A NULL parameter means "inifinte"
  1390. */
  1391. if (!expires) {
  1392. schedule();
  1393. __set_current_state(TASK_RUNNING);
  1394. return -EINTR;
  1395. }
  1396. hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
  1397. hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
  1398. hrtimer_init_sleeper(&t, current);
  1399. hrtimer_start_expires(&t.timer, mode);
  1400. if (!hrtimer_active(&t.timer))
  1401. t.task = NULL;
  1402. if (likely(t.task))
  1403. schedule();
  1404. hrtimer_cancel(&t.timer);
  1405. destroy_hrtimer_on_stack(&t.timer);
  1406. __set_current_state(TASK_RUNNING);
  1407. return !t.task ? 0 : -EINTR;
  1408. }
  1409. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
  1410. /**
  1411. * schedule_hrtimeout - sleep until timeout
  1412. * @expires: timeout value (ktime_t)
  1413. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1414. *
  1415. * Make the current task sleep until the given expiry time has
  1416. * elapsed. The routine will return immediately unless
  1417. * the current task state has been set (see set_current_state()).
  1418. *
  1419. * You can set the task state as follows -
  1420. *
  1421. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1422. * pass before the routine returns.
  1423. *
  1424. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1425. * delivered to the current task.
  1426. *
  1427. * The current task state is guaranteed to be TASK_RUNNING when this
  1428. * routine returns.
  1429. *
  1430. * Returns 0 when the timer has expired otherwise -EINTR
  1431. */
  1432. int __sched schedule_hrtimeout(ktime_t *expires,
  1433. const enum hrtimer_mode mode)
  1434. {
  1435. return schedule_hrtimeout_range(expires, 0, mode);
  1436. }
  1437. EXPORT_SYMBOL_GPL(schedule_hrtimeout);