hrtimer.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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. static void __run_hrtimer(struct hrtimer *timer);
  543. /*
  544. * When High resolution timers are active, try to reprogram. Note, that in case
  545. * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
  546. * check happens. The timer gets enqueued into the rbtree. The reprogramming
  547. * and expiry check is done in the hrtimer_interrupt or in the softirq.
  548. */
  549. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  550. struct hrtimer_clock_base *base)
  551. {
  552. if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
  553. /*
  554. * XXX: recursion check?
  555. * hrtimer_forward() should round up with timer granularity
  556. * so that we never get into inf recursion here,
  557. * it doesn't do that though
  558. */
  559. __run_hrtimer(timer);
  560. return 1;
  561. }
  562. return 0;
  563. }
  564. /*
  565. * Switch to high resolution mode
  566. */
  567. static int hrtimer_switch_to_hres(void)
  568. {
  569. int cpu = smp_processor_id();
  570. struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
  571. unsigned long flags;
  572. if (base->hres_active)
  573. return 1;
  574. local_irq_save(flags);
  575. if (tick_init_highres()) {
  576. local_irq_restore(flags);
  577. printk(KERN_WARNING "Could not switch to high resolution "
  578. "mode on CPU %d\n", cpu);
  579. return 0;
  580. }
  581. base->hres_active = 1;
  582. base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
  583. base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
  584. tick_setup_sched_timer();
  585. /* "Retrigger" the interrupt to get things going */
  586. retrigger_next_event(NULL);
  587. local_irq_restore(flags);
  588. printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
  589. smp_processor_id());
  590. return 1;
  591. }
  592. #else
  593. static inline int hrtimer_hres_active(void) { return 0; }
  594. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  595. static inline int hrtimer_switch_to_hres(void) { return 0; }
  596. static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
  597. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  598. struct hrtimer_clock_base *base)
  599. {
  600. return 0;
  601. }
  602. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  603. static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
  604. static inline int hrtimer_reprogram(struct hrtimer *timer,
  605. struct hrtimer_clock_base *base)
  606. {
  607. return 0;
  608. }
  609. #endif /* CONFIG_HIGH_RES_TIMERS */
  610. #ifdef CONFIG_TIMER_STATS
  611. void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
  612. {
  613. if (timer->start_site)
  614. return;
  615. timer->start_site = addr;
  616. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  617. timer->start_pid = current->pid;
  618. }
  619. #endif
  620. /*
  621. * Counterpart to lock_hrtimer_base above:
  622. */
  623. static inline
  624. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  625. {
  626. spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  627. }
  628. /**
  629. * hrtimer_forward - forward the timer expiry
  630. * @timer: hrtimer to forward
  631. * @now: forward past this time
  632. * @interval: the interval to forward
  633. *
  634. * Forward the timer expiry so it will expire in the future.
  635. * Returns the number of overruns.
  636. */
  637. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  638. {
  639. u64 orun = 1;
  640. ktime_t delta;
  641. delta = ktime_sub(now, hrtimer_get_expires(timer));
  642. if (delta.tv64 < 0)
  643. return 0;
  644. if (interval.tv64 < timer->base->resolution.tv64)
  645. interval.tv64 = timer->base->resolution.tv64;
  646. if (unlikely(delta.tv64 >= interval.tv64)) {
  647. s64 incr = ktime_to_ns(interval);
  648. orun = ktime_divns(delta, incr);
  649. hrtimer_add_expires_ns(timer, incr * orun);
  650. if (hrtimer_get_expires_tv64(timer) > now.tv64)
  651. return orun;
  652. /*
  653. * This (and the ktime_add() below) is the
  654. * correction for exact:
  655. */
  656. orun++;
  657. }
  658. hrtimer_add_expires(timer, interval);
  659. return orun;
  660. }
  661. EXPORT_SYMBOL_GPL(hrtimer_forward);
  662. /*
  663. * enqueue_hrtimer - internal function to (re)start a timer
  664. *
  665. * The timer is inserted in expiry order. Insertion into the
  666. * red black tree is O(log(n)). Must hold the base lock.
  667. */
  668. static void enqueue_hrtimer(struct hrtimer *timer,
  669. struct hrtimer_clock_base *base, int reprogram)
  670. {
  671. struct rb_node **link = &base->active.rb_node;
  672. struct rb_node *parent = NULL;
  673. struct hrtimer *entry;
  674. int leftmost = 1;
  675. debug_hrtimer_activate(timer);
  676. /*
  677. * Find the right place in the rbtree:
  678. */
  679. while (*link) {
  680. parent = *link;
  681. entry = rb_entry(parent, struct hrtimer, node);
  682. /*
  683. * We dont care about collisions. Nodes with
  684. * the same expiry time stay together.
  685. */
  686. if (hrtimer_get_expires_tv64(timer) <
  687. hrtimer_get_expires_tv64(entry)) {
  688. link = &(*link)->rb_left;
  689. } else {
  690. link = &(*link)->rb_right;
  691. leftmost = 0;
  692. }
  693. }
  694. /*
  695. * Insert the timer to the rbtree and check whether it
  696. * replaces the first pending timer
  697. */
  698. if (leftmost) {
  699. /*
  700. * Reprogram the clock event device. When the timer is already
  701. * expired hrtimer_enqueue_reprogram has either called the
  702. * callback or added it to the pending list and raised the
  703. * softirq.
  704. *
  705. * This is a NOP for !HIGHRES
  706. */
  707. if (reprogram && hrtimer_enqueue_reprogram(timer, base))
  708. return;
  709. base->first = &timer->node;
  710. }
  711. rb_link_node(&timer->node, parent, link);
  712. rb_insert_color(&timer->node, &base->active);
  713. /*
  714. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  715. * state of a possibly running callback.
  716. */
  717. timer->state |= HRTIMER_STATE_ENQUEUED;
  718. }
  719. /*
  720. * __remove_hrtimer - internal function to remove a timer
  721. *
  722. * Caller must hold the base lock.
  723. *
  724. * High resolution timer mode reprograms the clock event device when the
  725. * timer is the one which expires next. The caller can disable this by setting
  726. * reprogram to zero. This is useful, when the context does a reprogramming
  727. * anyway (e.g. timer interrupt)
  728. */
  729. static void __remove_hrtimer(struct hrtimer *timer,
  730. struct hrtimer_clock_base *base,
  731. unsigned long newstate, int reprogram)
  732. {
  733. if (timer->state & HRTIMER_STATE_ENQUEUED) {
  734. /*
  735. * Remove the timer from the rbtree and replace the
  736. * first entry pointer if necessary.
  737. */
  738. if (base->first == &timer->node) {
  739. base->first = rb_next(&timer->node);
  740. /* Reprogram the clock event device. if enabled */
  741. if (reprogram && hrtimer_hres_active())
  742. hrtimer_force_reprogram(base->cpu_base);
  743. }
  744. rb_erase(&timer->node, &base->active);
  745. }
  746. timer->state = newstate;
  747. }
  748. /*
  749. * remove hrtimer, called with base lock held
  750. */
  751. static inline int
  752. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  753. {
  754. if (hrtimer_is_queued(timer)) {
  755. int reprogram;
  756. /*
  757. * Remove the timer and force reprogramming when high
  758. * resolution mode is active and the timer is on the current
  759. * CPU. If we remove a timer on another CPU, reprogramming is
  760. * skipped. The interrupt event on this CPU is fired and
  761. * reprogramming happens in the interrupt handler. This is a
  762. * rare case and less expensive than a smp call.
  763. */
  764. debug_hrtimer_deactivate(timer);
  765. timer_stats_hrtimer_clear_start_info(timer);
  766. reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
  767. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
  768. reprogram);
  769. return 1;
  770. }
  771. return 0;
  772. }
  773. /**
  774. * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
  775. * @timer: the timer to be added
  776. * @tim: expiry time
  777. * @delta_ns: "slack" range for the timer
  778. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  779. *
  780. * Returns:
  781. * 0 on success
  782. * 1 when the timer was active
  783. */
  784. int
  785. hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, unsigned long delta_ns,
  786. const enum hrtimer_mode mode)
  787. {
  788. struct hrtimer_clock_base *base, *new_base;
  789. unsigned long flags;
  790. int ret;
  791. base = lock_hrtimer_base(timer, &flags);
  792. /* Remove an active timer from the queue: */
  793. ret = remove_hrtimer(timer, base);
  794. /* Switch the timer base, if necessary: */
  795. new_base = switch_hrtimer_base(timer, base);
  796. if (mode == HRTIMER_MODE_REL) {
  797. tim = ktime_add_safe(tim, new_base->get_time());
  798. /*
  799. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  800. * to signal that they simply return xtime in
  801. * do_gettimeoffset(). In this case we want to round up by
  802. * resolution when starting a relative timer, to avoid short
  803. * timeouts. This will go away with the GTOD framework.
  804. */
  805. #ifdef CONFIG_TIME_LOW_RES
  806. tim = ktime_add_safe(tim, base->resolution);
  807. #endif
  808. }
  809. hrtimer_set_expires_range_ns(timer, tim, delta_ns);
  810. timer_stats_hrtimer_set_start_info(timer);
  811. /*
  812. * Only allow reprogramming if the new base is on this CPU.
  813. * (it might still be on another CPU if the timer was pending)
  814. */
  815. enqueue_hrtimer(timer, new_base,
  816. new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
  817. unlock_hrtimer_base(timer, &flags);
  818. return ret;
  819. }
  820. EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
  821. /**
  822. * hrtimer_start - (re)start an hrtimer on the current CPU
  823. * @timer: the timer to be added
  824. * @tim: expiry time
  825. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  826. *
  827. * Returns:
  828. * 0 on success
  829. * 1 when the timer was active
  830. */
  831. int
  832. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  833. {
  834. return hrtimer_start_range_ns(timer, tim, 0, mode);
  835. }
  836. EXPORT_SYMBOL_GPL(hrtimer_start);
  837. /**
  838. * hrtimer_try_to_cancel - try to deactivate a timer
  839. * @timer: hrtimer to stop
  840. *
  841. * Returns:
  842. * 0 when the timer was not active
  843. * 1 when the timer was active
  844. * -1 when the timer is currently excuting the callback function and
  845. * cannot be stopped
  846. */
  847. int hrtimer_try_to_cancel(struct hrtimer *timer)
  848. {
  849. struct hrtimer_clock_base *base;
  850. unsigned long flags;
  851. int ret = -1;
  852. base = lock_hrtimer_base(timer, &flags);
  853. if (!hrtimer_callback_running(timer))
  854. ret = remove_hrtimer(timer, base);
  855. unlock_hrtimer_base(timer, &flags);
  856. return ret;
  857. }
  858. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  859. /**
  860. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  861. * @timer: the timer to be cancelled
  862. *
  863. * Returns:
  864. * 0 when the timer was not active
  865. * 1 when the timer was active
  866. */
  867. int hrtimer_cancel(struct hrtimer *timer)
  868. {
  869. for (;;) {
  870. int ret = hrtimer_try_to_cancel(timer);
  871. if (ret >= 0)
  872. return ret;
  873. cpu_relax();
  874. }
  875. }
  876. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  877. /**
  878. * hrtimer_get_remaining - get remaining time for the timer
  879. * @timer: the timer to read
  880. */
  881. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  882. {
  883. struct hrtimer_clock_base *base;
  884. unsigned long flags;
  885. ktime_t rem;
  886. base = lock_hrtimer_base(timer, &flags);
  887. rem = hrtimer_expires_remaining(timer);
  888. unlock_hrtimer_base(timer, &flags);
  889. return rem;
  890. }
  891. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  892. #ifdef CONFIG_NO_HZ
  893. /**
  894. * hrtimer_get_next_event - get the time until next expiry event
  895. *
  896. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  897. * is pending.
  898. */
  899. ktime_t hrtimer_get_next_event(void)
  900. {
  901. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  902. struct hrtimer_clock_base *base = cpu_base->clock_base;
  903. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  904. unsigned long flags;
  905. int i;
  906. spin_lock_irqsave(&cpu_base->lock, flags);
  907. if (!hrtimer_hres_active()) {
  908. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  909. struct hrtimer *timer;
  910. if (!base->first)
  911. continue;
  912. timer = rb_entry(base->first, struct hrtimer, node);
  913. delta.tv64 = hrtimer_get_expires_tv64(timer);
  914. delta = ktime_sub(delta, base->get_time());
  915. if (delta.tv64 < mindelta.tv64)
  916. mindelta.tv64 = delta.tv64;
  917. }
  918. }
  919. spin_unlock_irqrestore(&cpu_base->lock, flags);
  920. if (mindelta.tv64 < 0)
  921. mindelta.tv64 = 0;
  922. return mindelta;
  923. }
  924. #endif
  925. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  926. enum hrtimer_mode mode)
  927. {
  928. struct hrtimer_cpu_base *cpu_base;
  929. memset(timer, 0, sizeof(struct hrtimer));
  930. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  931. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  932. clock_id = CLOCK_MONOTONIC;
  933. timer->base = &cpu_base->clock_base[clock_id];
  934. INIT_LIST_HEAD(&timer->cb_entry);
  935. hrtimer_init_timer_hres(timer);
  936. #ifdef CONFIG_TIMER_STATS
  937. timer->start_site = NULL;
  938. timer->start_pid = -1;
  939. memset(timer->start_comm, 0, TASK_COMM_LEN);
  940. #endif
  941. }
  942. /**
  943. * hrtimer_init - initialize a timer to the given clock
  944. * @timer: the timer to be initialized
  945. * @clock_id: the clock to be used
  946. * @mode: timer mode abs/rel
  947. */
  948. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  949. enum hrtimer_mode mode)
  950. {
  951. debug_hrtimer_init(timer);
  952. __hrtimer_init(timer, clock_id, mode);
  953. }
  954. EXPORT_SYMBOL_GPL(hrtimer_init);
  955. /**
  956. * hrtimer_get_res - get the timer resolution for a clock
  957. * @which_clock: which clock to query
  958. * @tp: pointer to timespec variable to store the resolution
  959. *
  960. * Store the resolution of the clock selected by @which_clock in the
  961. * variable pointed to by @tp.
  962. */
  963. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  964. {
  965. struct hrtimer_cpu_base *cpu_base;
  966. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  967. *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
  968. return 0;
  969. }
  970. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  971. static void __run_hrtimer(struct hrtimer *timer)
  972. {
  973. struct hrtimer_clock_base *base = timer->base;
  974. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  975. enum hrtimer_restart (*fn)(struct hrtimer *);
  976. int restart;
  977. WARN_ON(!irqs_disabled());
  978. debug_hrtimer_deactivate(timer);
  979. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  980. timer_stats_account_hrtimer(timer);
  981. fn = timer->function;
  982. /*
  983. * Because we run timers from hardirq context, there is no chance
  984. * they get migrated to another cpu, therefore its safe to unlock
  985. * the timer base.
  986. */
  987. spin_unlock(&cpu_base->lock);
  988. restart = fn(timer);
  989. spin_lock(&cpu_base->lock);
  990. /*
  991. * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
  992. * reprogramming of the event hardware. This happens at the end of this
  993. * function anyway.
  994. */
  995. if (restart != HRTIMER_NORESTART) {
  996. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  997. enqueue_hrtimer(timer, base, 0);
  998. }
  999. timer->state &= ~HRTIMER_STATE_CALLBACK;
  1000. }
  1001. #ifdef CONFIG_HIGH_RES_TIMERS
  1002. /*
  1003. * High resolution timer interrupt
  1004. * Called with interrupts disabled
  1005. */
  1006. void hrtimer_interrupt(struct clock_event_device *dev)
  1007. {
  1008. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1009. struct hrtimer_clock_base *base;
  1010. ktime_t expires_next, now;
  1011. int i;
  1012. BUG_ON(!cpu_base->hres_active);
  1013. cpu_base->nr_events++;
  1014. dev->next_event.tv64 = KTIME_MAX;
  1015. retry:
  1016. now = ktime_get();
  1017. expires_next.tv64 = KTIME_MAX;
  1018. base = cpu_base->clock_base;
  1019. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1020. ktime_t basenow;
  1021. struct rb_node *node;
  1022. spin_lock(&cpu_base->lock);
  1023. basenow = ktime_add(now, base->offset);
  1024. while ((node = base->first)) {
  1025. struct hrtimer *timer;
  1026. timer = rb_entry(node, struct hrtimer, node);
  1027. /*
  1028. * The immediate goal for using the softexpires is
  1029. * minimizing wakeups, not running timers at the
  1030. * earliest interrupt after their soft expiration.
  1031. * This allows us to avoid using a Priority Search
  1032. * Tree, which can answer a stabbing querry for
  1033. * overlapping intervals and instead use the simple
  1034. * BST we already have.
  1035. * We don't add extra wakeups by delaying timers that
  1036. * are right-of a not yet expired timer, because that
  1037. * timer will have to trigger a wakeup anyway.
  1038. */
  1039. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
  1040. ktime_t expires;
  1041. expires = ktime_sub(hrtimer_get_expires(timer),
  1042. base->offset);
  1043. if (expires.tv64 < expires_next.tv64)
  1044. expires_next = expires;
  1045. break;
  1046. }
  1047. __run_hrtimer(timer);
  1048. }
  1049. spin_unlock(&cpu_base->lock);
  1050. base++;
  1051. }
  1052. cpu_base->expires_next = expires_next;
  1053. /* Reprogramming necessary ? */
  1054. if (expires_next.tv64 != KTIME_MAX) {
  1055. if (tick_program_event(expires_next, 0))
  1056. goto retry;
  1057. }
  1058. }
  1059. /*
  1060. * local version of hrtimer_peek_ahead_timers() called with interrupts
  1061. * disabled.
  1062. */
  1063. static void __hrtimer_peek_ahead_timers(void)
  1064. {
  1065. struct tick_device *td;
  1066. if (!hrtimer_hres_active())
  1067. return;
  1068. td = &__get_cpu_var(tick_cpu_device);
  1069. if (td && td->evtdev)
  1070. hrtimer_interrupt(td->evtdev);
  1071. }
  1072. /**
  1073. * hrtimer_peek_ahead_timers -- run soft-expired timers now
  1074. *
  1075. * hrtimer_peek_ahead_timers will peek at the timer queue of
  1076. * the current cpu and check if there are any timers for which
  1077. * the soft expires time has passed. If any such timers exist,
  1078. * they are run immediately and then removed from the timer queue.
  1079. *
  1080. */
  1081. void hrtimer_peek_ahead_timers(void)
  1082. {
  1083. unsigned long flags;
  1084. local_irq_save(flags);
  1085. __hrtimer_peek_ahead_timers();
  1086. local_irq_restore(flags);
  1087. }
  1088. #endif /* CONFIG_HIGH_RES_TIMERS */
  1089. /*
  1090. * Called from timer softirq every jiffy, expire hrtimers:
  1091. *
  1092. * For HRT its the fall back code to run the softirq in the timer
  1093. * softirq context in case the hrtimer initialization failed or has
  1094. * not been done yet.
  1095. */
  1096. void hrtimer_run_pending(void)
  1097. {
  1098. if (hrtimer_hres_active())
  1099. return;
  1100. /*
  1101. * This _is_ ugly: We have to check in the softirq context,
  1102. * whether we can switch to highres and / or nohz mode. The
  1103. * clocksource switch happens in the timer interrupt with
  1104. * xtime_lock held. Notification from there only sets the
  1105. * check bit in the tick_oneshot code, otherwise we might
  1106. * deadlock vs. xtime_lock.
  1107. */
  1108. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
  1109. hrtimer_switch_to_hres();
  1110. }
  1111. /*
  1112. * Called from hardirq context every jiffy
  1113. */
  1114. void hrtimer_run_queues(void)
  1115. {
  1116. struct rb_node *node;
  1117. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1118. struct hrtimer_clock_base *base;
  1119. int index, gettime = 1;
  1120. if (hrtimer_hres_active())
  1121. return;
  1122. for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  1123. base = &cpu_base->clock_base[index];
  1124. if (!base->first)
  1125. continue;
  1126. if (gettime) {
  1127. hrtimer_get_softirq_time(cpu_base);
  1128. gettime = 0;
  1129. }
  1130. spin_lock(&cpu_base->lock);
  1131. while ((node = base->first)) {
  1132. struct hrtimer *timer;
  1133. timer = rb_entry(node, struct hrtimer, node);
  1134. if (base->softirq_time.tv64 <=
  1135. hrtimer_get_expires_tv64(timer))
  1136. break;
  1137. __run_hrtimer(timer);
  1138. }
  1139. spin_unlock(&cpu_base->lock);
  1140. }
  1141. }
  1142. /*
  1143. * Sleep related functions:
  1144. */
  1145. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1146. {
  1147. struct hrtimer_sleeper *t =
  1148. container_of(timer, struct hrtimer_sleeper, timer);
  1149. struct task_struct *task = t->task;
  1150. t->task = NULL;
  1151. if (task)
  1152. wake_up_process(task);
  1153. return HRTIMER_NORESTART;
  1154. }
  1155. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1156. {
  1157. sl->timer.function = hrtimer_wakeup;
  1158. sl->task = task;
  1159. }
  1160. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1161. {
  1162. hrtimer_init_sleeper(t, current);
  1163. do {
  1164. set_current_state(TASK_INTERRUPTIBLE);
  1165. hrtimer_start_expires(&t->timer, mode);
  1166. if (!hrtimer_active(&t->timer))
  1167. t->task = NULL;
  1168. if (likely(t->task))
  1169. schedule();
  1170. hrtimer_cancel(&t->timer);
  1171. mode = HRTIMER_MODE_ABS;
  1172. } while (t->task && !signal_pending(current));
  1173. __set_current_state(TASK_RUNNING);
  1174. return t->task == NULL;
  1175. }
  1176. static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
  1177. {
  1178. struct timespec rmt;
  1179. ktime_t rem;
  1180. rem = hrtimer_expires_remaining(timer);
  1181. if (rem.tv64 <= 0)
  1182. return 0;
  1183. rmt = ktime_to_timespec(rem);
  1184. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  1185. return -EFAULT;
  1186. return 1;
  1187. }
  1188. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1189. {
  1190. struct hrtimer_sleeper t;
  1191. struct timespec __user *rmtp;
  1192. int ret = 0;
  1193. hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
  1194. HRTIMER_MODE_ABS);
  1195. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  1196. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  1197. goto out;
  1198. rmtp = restart->nanosleep.rmtp;
  1199. if (rmtp) {
  1200. ret = update_rmtp(&t.timer, rmtp);
  1201. if (ret <= 0)
  1202. goto out;
  1203. }
  1204. /* The other values in restart are already filled in */
  1205. ret = -ERESTART_RESTARTBLOCK;
  1206. out:
  1207. destroy_hrtimer_on_stack(&t.timer);
  1208. return ret;
  1209. }
  1210. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  1211. const enum hrtimer_mode mode, const clockid_t clockid)
  1212. {
  1213. struct restart_block *restart;
  1214. struct hrtimer_sleeper t;
  1215. int ret = 0;
  1216. unsigned long slack;
  1217. slack = current->timer_slack_ns;
  1218. if (rt_task(current))
  1219. slack = 0;
  1220. hrtimer_init_on_stack(&t.timer, clockid, mode);
  1221. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  1222. if (do_nanosleep(&t, mode))
  1223. goto out;
  1224. /* Absolute timers do not update the rmtp value and restart: */
  1225. if (mode == HRTIMER_MODE_ABS) {
  1226. ret = -ERESTARTNOHAND;
  1227. goto out;
  1228. }
  1229. if (rmtp) {
  1230. ret = update_rmtp(&t.timer, rmtp);
  1231. if (ret <= 0)
  1232. goto out;
  1233. }
  1234. restart = &current_thread_info()->restart_block;
  1235. restart->fn = hrtimer_nanosleep_restart;
  1236. restart->nanosleep.index = t.timer.base->index;
  1237. restart->nanosleep.rmtp = rmtp;
  1238. restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
  1239. ret = -ERESTART_RESTARTBLOCK;
  1240. out:
  1241. destroy_hrtimer_on_stack(&t.timer);
  1242. return ret;
  1243. }
  1244. asmlinkage long
  1245. sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
  1246. {
  1247. struct timespec tu;
  1248. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  1249. return -EFAULT;
  1250. if (!timespec_valid(&tu))
  1251. return -EINVAL;
  1252. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1253. }
  1254. /*
  1255. * Functions related to boot-time initialization:
  1256. */
  1257. static void __cpuinit init_hrtimers_cpu(int cpu)
  1258. {
  1259. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1260. int i;
  1261. spin_lock_init(&cpu_base->lock);
  1262. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  1263. cpu_base->clock_base[i].cpu_base = cpu_base;
  1264. hrtimer_init_hres(cpu_base);
  1265. }
  1266. #ifdef CONFIG_HOTPLUG_CPU
  1267. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1268. struct hrtimer_clock_base *new_base)
  1269. {
  1270. struct hrtimer *timer;
  1271. struct rb_node *node;
  1272. while ((node = rb_first(&old_base->active))) {
  1273. timer = rb_entry(node, struct hrtimer, node);
  1274. BUG_ON(hrtimer_callback_running(timer));
  1275. debug_hrtimer_deactivate(timer);
  1276. /*
  1277. * Mark it as STATE_MIGRATE not INACTIVE otherwise the
  1278. * timer could be seen as !active and just vanish away
  1279. * under us on another CPU
  1280. */
  1281. __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
  1282. timer->base = new_base;
  1283. /*
  1284. * Enqueue the timers on the new cpu, but do not reprogram
  1285. * the timer as that would enable a deadlock between
  1286. * hrtimer_enqueue_reprogramm() running the timer and us still
  1287. * holding a nested base lock.
  1288. *
  1289. * Instead we tickle the hrtimer interrupt after the migration
  1290. * is done, which will run all expired timers and re-programm
  1291. * the timer device.
  1292. */
  1293. enqueue_hrtimer(timer, new_base, 0);
  1294. /* Clear the migration state bit */
  1295. timer->state &= ~HRTIMER_STATE_MIGRATE;
  1296. }
  1297. }
  1298. static int migrate_hrtimers(int scpu)
  1299. {
  1300. struct hrtimer_cpu_base *old_base, *new_base;
  1301. int dcpu, i;
  1302. BUG_ON(cpu_online(scpu));
  1303. old_base = &per_cpu(hrtimer_bases, scpu);
  1304. new_base = &get_cpu_var(hrtimer_bases);
  1305. dcpu = smp_processor_id();
  1306. tick_cancel_sched_timer(scpu);
  1307. /*
  1308. * The caller is globally serialized and nobody else
  1309. * takes two locks at once, deadlock is not possible.
  1310. */
  1311. spin_lock_irq(&new_base->lock);
  1312. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1313. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1314. migrate_hrtimer_list(&old_base->clock_base[i],
  1315. &new_base->clock_base[i]);
  1316. }
  1317. spin_unlock(&old_base->lock);
  1318. spin_unlock_irq(&new_base->lock);
  1319. put_cpu_var(hrtimer_bases);
  1320. return dcpu;
  1321. }
  1322. static void tickle_timers(void *arg)
  1323. {
  1324. hrtimer_peek_ahead_timers();
  1325. }
  1326. #endif /* CONFIG_HOTPLUG_CPU */
  1327. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  1328. unsigned long action, void *hcpu)
  1329. {
  1330. int scpu = (long)hcpu;
  1331. switch (action) {
  1332. case CPU_UP_PREPARE:
  1333. case CPU_UP_PREPARE_FROZEN:
  1334. init_hrtimers_cpu(scpu);
  1335. break;
  1336. #ifdef CONFIG_HOTPLUG_CPU
  1337. case CPU_DEAD:
  1338. case CPU_DEAD_FROZEN:
  1339. {
  1340. int dcpu;
  1341. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
  1342. dcpu = migrate_hrtimers(scpu);
  1343. smp_call_function_single(dcpu, tickle_timers, NULL, 0);
  1344. break;
  1345. }
  1346. #endif
  1347. default:
  1348. break;
  1349. }
  1350. return NOTIFY_OK;
  1351. }
  1352. static struct notifier_block __cpuinitdata hrtimers_nb = {
  1353. .notifier_call = hrtimer_cpu_notify,
  1354. };
  1355. void __init hrtimers_init(void)
  1356. {
  1357. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1358. (void *)(long)smp_processor_id());
  1359. register_cpu_notifier(&hrtimers_nb);
  1360. }
  1361. /**
  1362. * schedule_hrtimeout_range - sleep until timeout
  1363. * @expires: timeout value (ktime_t)
  1364. * @delta: slack in expires timeout (ktime_t)
  1365. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1366. *
  1367. * Make the current task sleep until the given expiry time has
  1368. * elapsed. The routine will return immediately unless
  1369. * the current task state has been set (see set_current_state()).
  1370. *
  1371. * The @delta argument gives the kernel the freedom to schedule the
  1372. * actual wakeup to a time that is both power and performance friendly.
  1373. * The kernel give the normal best effort behavior for "@expires+@delta",
  1374. * but may decide to fire the timer earlier, but no earlier than @expires.
  1375. *
  1376. * You can set the task state as follows -
  1377. *
  1378. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1379. * pass before the routine returns.
  1380. *
  1381. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1382. * delivered to the current task.
  1383. *
  1384. * The current task state is guaranteed to be TASK_RUNNING when this
  1385. * routine returns.
  1386. *
  1387. * Returns 0 when the timer has expired otherwise -EINTR
  1388. */
  1389. int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
  1390. const enum hrtimer_mode mode)
  1391. {
  1392. struct hrtimer_sleeper t;
  1393. /*
  1394. * Optimize when a zero timeout value is given. It does not
  1395. * matter whether this is an absolute or a relative time.
  1396. */
  1397. if (expires && !expires->tv64) {
  1398. __set_current_state(TASK_RUNNING);
  1399. return 0;
  1400. }
  1401. /*
  1402. * A NULL parameter means "inifinte"
  1403. */
  1404. if (!expires) {
  1405. schedule();
  1406. __set_current_state(TASK_RUNNING);
  1407. return -EINTR;
  1408. }
  1409. hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
  1410. hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
  1411. hrtimer_init_sleeper(&t, current);
  1412. hrtimer_start_expires(&t.timer, mode);
  1413. if (!hrtimer_active(&t.timer))
  1414. t.task = NULL;
  1415. if (likely(t.task))
  1416. schedule();
  1417. hrtimer_cancel(&t.timer);
  1418. destroy_hrtimer_on_stack(&t.timer);
  1419. __set_current_state(TASK_RUNNING);
  1420. return !t.task ? 0 : -EINTR;
  1421. }
  1422. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
  1423. /**
  1424. * schedule_hrtimeout - sleep until timeout
  1425. * @expires: timeout value (ktime_t)
  1426. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1427. *
  1428. * Make the current task sleep until the given expiry time has
  1429. * elapsed. The routine will return immediately unless
  1430. * the current task state has been set (see set_current_state()).
  1431. *
  1432. * You can set the task state as follows -
  1433. *
  1434. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1435. * pass before the routine returns.
  1436. *
  1437. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1438. * delivered to the current task.
  1439. *
  1440. * The current task state is guaranteed to be TASK_RUNNING when this
  1441. * routine returns.
  1442. *
  1443. * Returns 0 when the timer has expired otherwise -EINTR
  1444. */
  1445. int __sched schedule_hrtimeout(ktime_t *expires,
  1446. const enum hrtimer_mode mode)
  1447. {
  1448. return schedule_hrtimeout_range(expires, 0, mode);
  1449. }
  1450. EXPORT_SYMBOL_GPL(schedule_hrtimeout);