hrtimer.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923
  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. /*
  378. * Check, whether the timer is on the callback pending list
  379. */
  380. static inline int hrtimer_cb_pending(const struct hrtimer *timer)
  381. {
  382. return timer->state & HRTIMER_STATE_PENDING;
  383. }
  384. /*
  385. * Remove a timer from the callback pending list
  386. */
  387. static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
  388. {
  389. list_del_init(&timer->cb_entry);
  390. }
  391. /* High resolution timer related functions */
  392. #ifdef CONFIG_HIGH_RES_TIMERS
  393. /*
  394. * High resolution timer enabled ?
  395. */
  396. static int hrtimer_hres_enabled __read_mostly = 1;
  397. /*
  398. * Enable / Disable high resolution mode
  399. */
  400. static int __init setup_hrtimer_hres(char *str)
  401. {
  402. if (!strcmp(str, "off"))
  403. hrtimer_hres_enabled = 0;
  404. else if (!strcmp(str, "on"))
  405. hrtimer_hres_enabled = 1;
  406. else
  407. return 0;
  408. return 1;
  409. }
  410. __setup("highres=", setup_hrtimer_hres);
  411. /*
  412. * hrtimer_high_res_enabled - query, if the highres mode is enabled
  413. */
  414. static inline int hrtimer_is_hres_enabled(void)
  415. {
  416. return hrtimer_hres_enabled;
  417. }
  418. /*
  419. * Is the high resolution mode active ?
  420. */
  421. static inline int hrtimer_hres_active(void)
  422. {
  423. return __get_cpu_var(hrtimer_bases).hres_active;
  424. }
  425. /*
  426. * Reprogram the event source with checking both queues for the
  427. * next event
  428. * Called with interrupts disabled and base->lock held
  429. */
  430. static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
  431. {
  432. int i;
  433. struct hrtimer_clock_base *base = cpu_base->clock_base;
  434. ktime_t expires;
  435. cpu_base->expires_next.tv64 = KTIME_MAX;
  436. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  437. struct hrtimer *timer;
  438. if (!base->first)
  439. continue;
  440. timer = rb_entry(base->first, struct hrtimer, node);
  441. expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  442. if (expires.tv64 < cpu_base->expires_next.tv64)
  443. cpu_base->expires_next = expires;
  444. }
  445. if (cpu_base->expires_next.tv64 != KTIME_MAX)
  446. tick_program_event(cpu_base->expires_next, 1);
  447. }
  448. /*
  449. * Shared reprogramming for clock_realtime and clock_monotonic
  450. *
  451. * When a timer is enqueued and expires earlier than the already enqueued
  452. * timers, we have to check, whether it expires earlier than the timer for
  453. * which the clock event device was armed.
  454. *
  455. * Called with interrupts disabled and base->cpu_base.lock held
  456. */
  457. static int hrtimer_reprogram(struct hrtimer *timer,
  458. struct hrtimer_clock_base *base)
  459. {
  460. ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
  461. ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  462. int res;
  463. WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
  464. /*
  465. * When the callback is running, we do not reprogram the clock event
  466. * device. The timer callback is either running on a different CPU or
  467. * the callback is executed in the hrtimer_interrupt context. The
  468. * reprogramming is handled either by the softirq, which called the
  469. * callback or at the end of the hrtimer_interrupt.
  470. */
  471. if (hrtimer_callback_running(timer))
  472. return 0;
  473. /*
  474. * CLOCK_REALTIME timer might be requested with an absolute
  475. * expiry time which is less than base->offset. Nothing wrong
  476. * about that, just avoid to call into the tick code, which
  477. * has now objections against negative expiry values.
  478. */
  479. if (expires.tv64 < 0)
  480. return -ETIME;
  481. if (expires.tv64 >= expires_next->tv64)
  482. return 0;
  483. /*
  484. * Clockevents returns -ETIME, when the event was in the past.
  485. */
  486. res = tick_program_event(expires, 0);
  487. if (!IS_ERR_VALUE(res))
  488. *expires_next = expires;
  489. return res;
  490. }
  491. /*
  492. * Retrigger next event is called after clock was set
  493. *
  494. * Called with interrupts disabled via on_each_cpu()
  495. */
  496. static void retrigger_next_event(void *arg)
  497. {
  498. struct hrtimer_cpu_base *base;
  499. struct timespec realtime_offset;
  500. unsigned long seq;
  501. if (!hrtimer_hres_active())
  502. return;
  503. do {
  504. seq = read_seqbegin(&xtime_lock);
  505. set_normalized_timespec(&realtime_offset,
  506. -wall_to_monotonic.tv_sec,
  507. -wall_to_monotonic.tv_nsec);
  508. } while (read_seqretry(&xtime_lock, seq));
  509. base = &__get_cpu_var(hrtimer_bases);
  510. /* Adjust CLOCK_REALTIME offset */
  511. spin_lock(&base->lock);
  512. base->clock_base[CLOCK_REALTIME].offset =
  513. timespec_to_ktime(realtime_offset);
  514. hrtimer_force_reprogram(base);
  515. spin_unlock(&base->lock);
  516. }
  517. /*
  518. * Clock realtime was set
  519. *
  520. * Change the offset of the realtime clock vs. the monotonic
  521. * clock.
  522. *
  523. * We might have to reprogram the high resolution timer interrupt. On
  524. * SMP we call the architecture specific code to retrigger _all_ high
  525. * resolution timer interrupts. On UP we just disable interrupts and
  526. * call the high resolution interrupt code.
  527. */
  528. void clock_was_set(void)
  529. {
  530. /* Retrigger the CPU local events everywhere */
  531. on_each_cpu(retrigger_next_event, NULL, 1);
  532. }
  533. /*
  534. * During resume we might have to reprogram the high resolution timer
  535. * interrupt (on the local CPU):
  536. */
  537. void hres_timers_resume(void)
  538. {
  539. /* Retrigger the CPU local events: */
  540. retrigger_next_event(NULL);
  541. }
  542. /*
  543. * Initialize the high resolution related parts of cpu_base
  544. */
  545. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
  546. {
  547. base->expires_next.tv64 = KTIME_MAX;
  548. base->hres_active = 0;
  549. }
  550. /*
  551. * Initialize the high resolution related parts of a hrtimer
  552. */
  553. static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
  554. {
  555. }
  556. /*
  557. * When High resolution timers are active, try to reprogram. Note, that in case
  558. * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
  559. * check happens. The timer gets enqueued into the rbtree. The reprogramming
  560. * and expiry check is done in the hrtimer_interrupt or in the softirq.
  561. */
  562. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  563. struct hrtimer_clock_base *base)
  564. {
  565. if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
  566. /* Timer is expired, act upon the callback mode */
  567. switch(timer->cb_mode) {
  568. case HRTIMER_CB_IRQSAFE_PERCPU:
  569. case HRTIMER_CB_IRQSAFE_UNLOCKED:
  570. /*
  571. * This is solely for the sched tick emulation with
  572. * dynamic tick support to ensure that we do not
  573. * restart the tick right on the edge and end up with
  574. * the tick timer in the softirq ! The calling site
  575. * takes care of this. Also used for hrtimer sleeper !
  576. */
  577. debug_hrtimer_deactivate(timer);
  578. return 1;
  579. case HRTIMER_CB_SOFTIRQ:
  580. /*
  581. * Move everything else into the softirq pending list !
  582. */
  583. list_add_tail(&timer->cb_entry,
  584. &base->cpu_base->cb_pending);
  585. timer->state = HRTIMER_STATE_PENDING;
  586. return 1;
  587. default:
  588. BUG();
  589. }
  590. }
  591. return 0;
  592. }
  593. /*
  594. * Switch to high resolution mode
  595. */
  596. static int hrtimer_switch_to_hres(void)
  597. {
  598. int cpu = smp_processor_id();
  599. struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
  600. unsigned long flags;
  601. if (base->hres_active)
  602. return 1;
  603. local_irq_save(flags);
  604. if (tick_init_highres()) {
  605. local_irq_restore(flags);
  606. printk(KERN_WARNING "Could not switch to high resolution "
  607. "mode on CPU %d\n", cpu);
  608. return 0;
  609. }
  610. base->hres_active = 1;
  611. base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
  612. base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
  613. tick_setup_sched_timer();
  614. /* "Retrigger" the interrupt to get things going */
  615. retrigger_next_event(NULL);
  616. local_irq_restore(flags);
  617. printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
  618. smp_processor_id());
  619. return 1;
  620. }
  621. static inline void hrtimer_raise_softirq(void)
  622. {
  623. raise_softirq(HRTIMER_SOFTIRQ);
  624. }
  625. #else
  626. static inline int hrtimer_hres_active(void) { return 0; }
  627. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  628. static inline int hrtimer_switch_to_hres(void) { return 0; }
  629. static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
  630. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  631. struct hrtimer_clock_base *base)
  632. {
  633. return 0;
  634. }
  635. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  636. static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
  637. static inline int hrtimer_reprogram(struct hrtimer *timer,
  638. struct hrtimer_clock_base *base)
  639. {
  640. return 0;
  641. }
  642. static inline void hrtimer_raise_softirq(void) { }
  643. #endif /* CONFIG_HIGH_RES_TIMERS */
  644. #ifdef CONFIG_TIMER_STATS
  645. void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
  646. {
  647. if (timer->start_site)
  648. return;
  649. timer->start_site = addr;
  650. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  651. timer->start_pid = current->pid;
  652. }
  653. #endif
  654. /*
  655. * Counterpart to lock_hrtimer_base above:
  656. */
  657. static inline
  658. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  659. {
  660. spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  661. }
  662. /**
  663. * hrtimer_forward - forward the timer expiry
  664. * @timer: hrtimer to forward
  665. * @now: forward past this time
  666. * @interval: the interval to forward
  667. *
  668. * Forward the timer expiry so it will expire in the future.
  669. * Returns the number of overruns.
  670. */
  671. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  672. {
  673. u64 orun = 1;
  674. ktime_t delta;
  675. delta = ktime_sub(now, hrtimer_get_expires(timer));
  676. if (delta.tv64 < 0)
  677. return 0;
  678. if (interval.tv64 < timer->base->resolution.tv64)
  679. interval.tv64 = timer->base->resolution.tv64;
  680. if (unlikely(delta.tv64 >= interval.tv64)) {
  681. s64 incr = ktime_to_ns(interval);
  682. orun = ktime_divns(delta, incr);
  683. hrtimer_add_expires_ns(timer, incr * orun);
  684. if (hrtimer_get_expires_tv64(timer) > now.tv64)
  685. return orun;
  686. /*
  687. * This (and the ktime_add() below) is the
  688. * correction for exact:
  689. */
  690. orun++;
  691. }
  692. hrtimer_add_expires(timer, interval);
  693. return orun;
  694. }
  695. EXPORT_SYMBOL_GPL(hrtimer_forward);
  696. /*
  697. * enqueue_hrtimer - internal function to (re)start a timer
  698. *
  699. * The timer is inserted in expiry order. Insertion into the
  700. * red black tree is O(log(n)). Must hold the base lock.
  701. */
  702. static void enqueue_hrtimer(struct hrtimer *timer,
  703. struct hrtimer_clock_base *base, int reprogram)
  704. {
  705. struct rb_node **link = &base->active.rb_node;
  706. struct rb_node *parent = NULL;
  707. struct hrtimer *entry;
  708. int leftmost = 1;
  709. debug_hrtimer_activate(timer);
  710. /*
  711. * Find the right place in the rbtree:
  712. */
  713. while (*link) {
  714. parent = *link;
  715. entry = rb_entry(parent, struct hrtimer, node);
  716. /*
  717. * We dont care about collisions. Nodes with
  718. * the same expiry time stay together.
  719. */
  720. if (hrtimer_get_expires_tv64(timer) <
  721. hrtimer_get_expires_tv64(entry)) {
  722. link = &(*link)->rb_left;
  723. } else {
  724. link = &(*link)->rb_right;
  725. leftmost = 0;
  726. }
  727. }
  728. /*
  729. * Insert the timer to the rbtree and check whether it
  730. * replaces the first pending timer
  731. */
  732. if (leftmost) {
  733. /*
  734. * Reprogram the clock event device. When the timer is already
  735. * expired hrtimer_enqueue_reprogram has either called the
  736. * callback or added it to the pending list and raised the
  737. * softirq.
  738. *
  739. * This is a NOP for !HIGHRES
  740. */
  741. if (reprogram && hrtimer_enqueue_reprogram(timer, base))
  742. return;
  743. base->first = &timer->node;
  744. }
  745. rb_link_node(&timer->node, parent, link);
  746. rb_insert_color(&timer->node, &base->active);
  747. /*
  748. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  749. * state of a possibly running callback.
  750. */
  751. timer->state |= HRTIMER_STATE_ENQUEUED;
  752. }
  753. /*
  754. * __remove_hrtimer - internal function to remove a timer
  755. *
  756. * Caller must hold the base lock.
  757. *
  758. * High resolution timer mode reprograms the clock event device when the
  759. * timer is the one which expires next. The caller can disable this by setting
  760. * reprogram to zero. This is useful, when the context does a reprogramming
  761. * anyway (e.g. timer interrupt)
  762. */
  763. static void __remove_hrtimer(struct hrtimer *timer,
  764. struct hrtimer_clock_base *base,
  765. unsigned long newstate, int reprogram)
  766. {
  767. /* High res. callback list. NOP for !HIGHRES */
  768. if (hrtimer_cb_pending(timer))
  769. hrtimer_remove_cb_pending(timer);
  770. else {
  771. /*
  772. * Remove the timer from the rbtree and replace the
  773. * first entry pointer if necessary.
  774. */
  775. if (base->first == &timer->node) {
  776. base->first = rb_next(&timer->node);
  777. /* Reprogram the clock event device. if enabled */
  778. if (reprogram && hrtimer_hres_active())
  779. hrtimer_force_reprogram(base->cpu_base);
  780. }
  781. rb_erase(&timer->node, &base->active);
  782. }
  783. timer->state = newstate;
  784. }
  785. /*
  786. * remove hrtimer, called with base lock held
  787. */
  788. static inline int
  789. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  790. {
  791. if (hrtimer_is_queued(timer)) {
  792. int reprogram;
  793. /*
  794. * Remove the timer and force reprogramming when high
  795. * resolution mode is active and the timer is on the current
  796. * CPU. If we remove a timer on another CPU, reprogramming is
  797. * skipped. The interrupt event on this CPU is fired and
  798. * reprogramming happens in the interrupt handler. This is a
  799. * rare case and less expensive than a smp call.
  800. */
  801. debug_hrtimer_deactivate(timer);
  802. timer_stats_hrtimer_clear_start_info(timer);
  803. reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
  804. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
  805. reprogram);
  806. return 1;
  807. }
  808. return 0;
  809. }
  810. /**
  811. * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
  812. * @timer: the timer to be added
  813. * @tim: expiry time
  814. * @delta_ns: "slack" range for the timer
  815. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  816. *
  817. * Returns:
  818. * 0 on success
  819. * 1 when the timer was active
  820. */
  821. int
  822. hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, unsigned long delta_ns,
  823. const enum hrtimer_mode mode)
  824. {
  825. struct hrtimer_clock_base *base, *new_base;
  826. unsigned long flags;
  827. int ret, raise;
  828. base = lock_hrtimer_base(timer, &flags);
  829. /* Remove an active timer from the queue: */
  830. ret = remove_hrtimer(timer, base);
  831. /* Switch the timer base, if necessary: */
  832. new_base = switch_hrtimer_base(timer, base);
  833. if (mode == HRTIMER_MODE_REL) {
  834. tim = ktime_add_safe(tim, new_base->get_time());
  835. /*
  836. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  837. * to signal that they simply return xtime in
  838. * do_gettimeoffset(). In this case we want to round up by
  839. * resolution when starting a relative timer, to avoid short
  840. * timeouts. This will go away with the GTOD framework.
  841. */
  842. #ifdef CONFIG_TIME_LOW_RES
  843. tim = ktime_add_safe(tim, base->resolution);
  844. #endif
  845. }
  846. hrtimer_set_expires_range_ns(timer, tim, delta_ns);
  847. timer_stats_hrtimer_set_start_info(timer);
  848. /*
  849. * Only allow reprogramming if the new base is on this CPU.
  850. * (it might still be on another CPU if the timer was pending)
  851. */
  852. enqueue_hrtimer(timer, new_base,
  853. new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
  854. /*
  855. * The timer may be expired and moved to the cb_pending
  856. * list. We can not raise the softirq with base lock held due
  857. * to a possible deadlock with runqueue lock.
  858. */
  859. raise = timer->state == HRTIMER_STATE_PENDING;
  860. /*
  861. * We use preempt_disable to prevent this task from migrating after
  862. * setting up the softirq and raising it. Otherwise, if me migrate
  863. * we will raise the softirq on the wrong CPU.
  864. */
  865. preempt_disable();
  866. unlock_hrtimer_base(timer, &flags);
  867. if (raise)
  868. hrtimer_raise_softirq();
  869. preempt_enable();
  870. return ret;
  871. }
  872. EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
  873. /**
  874. * hrtimer_start - (re)start an hrtimer on the current CPU
  875. * @timer: the timer to be added
  876. * @tim: expiry time
  877. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  878. *
  879. * Returns:
  880. * 0 on success
  881. * 1 when the timer was active
  882. */
  883. int
  884. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  885. {
  886. return hrtimer_start_range_ns(timer, tim, 0, mode);
  887. }
  888. EXPORT_SYMBOL_GPL(hrtimer_start);
  889. /**
  890. * hrtimer_try_to_cancel - try to deactivate a timer
  891. * @timer: hrtimer to stop
  892. *
  893. * Returns:
  894. * 0 when the timer was not active
  895. * 1 when the timer was active
  896. * -1 when the timer is currently excuting the callback function and
  897. * cannot be stopped
  898. */
  899. int hrtimer_try_to_cancel(struct hrtimer *timer)
  900. {
  901. struct hrtimer_clock_base *base;
  902. unsigned long flags;
  903. int ret = -1;
  904. base = lock_hrtimer_base(timer, &flags);
  905. if (!hrtimer_callback_running(timer))
  906. ret = remove_hrtimer(timer, base);
  907. unlock_hrtimer_base(timer, &flags);
  908. return ret;
  909. }
  910. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  911. /**
  912. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  913. * @timer: the timer to be cancelled
  914. *
  915. * Returns:
  916. * 0 when the timer was not active
  917. * 1 when the timer was active
  918. */
  919. int hrtimer_cancel(struct hrtimer *timer)
  920. {
  921. for (;;) {
  922. int ret = hrtimer_try_to_cancel(timer);
  923. if (ret >= 0)
  924. return ret;
  925. cpu_relax();
  926. }
  927. }
  928. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  929. /**
  930. * hrtimer_get_remaining - get remaining time for the timer
  931. * @timer: the timer to read
  932. */
  933. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  934. {
  935. struct hrtimer_clock_base *base;
  936. unsigned long flags;
  937. ktime_t rem;
  938. base = lock_hrtimer_base(timer, &flags);
  939. rem = hrtimer_expires_remaining(timer);
  940. unlock_hrtimer_base(timer, &flags);
  941. return rem;
  942. }
  943. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  944. #ifdef CONFIG_NO_HZ
  945. /**
  946. * hrtimer_get_next_event - get the time until next expiry event
  947. *
  948. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  949. * is pending.
  950. */
  951. ktime_t hrtimer_get_next_event(void)
  952. {
  953. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  954. struct hrtimer_clock_base *base = cpu_base->clock_base;
  955. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  956. unsigned long flags;
  957. int i;
  958. spin_lock_irqsave(&cpu_base->lock, flags);
  959. if (!hrtimer_hres_active()) {
  960. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  961. struct hrtimer *timer;
  962. if (!base->first)
  963. continue;
  964. timer = rb_entry(base->first, struct hrtimer, node);
  965. delta.tv64 = hrtimer_get_expires_tv64(timer);
  966. delta = ktime_sub(delta, base->get_time());
  967. if (delta.tv64 < mindelta.tv64)
  968. mindelta.tv64 = delta.tv64;
  969. }
  970. }
  971. spin_unlock_irqrestore(&cpu_base->lock, flags);
  972. if (mindelta.tv64 < 0)
  973. mindelta.tv64 = 0;
  974. return mindelta;
  975. }
  976. #endif
  977. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  978. enum hrtimer_mode mode)
  979. {
  980. struct hrtimer_cpu_base *cpu_base;
  981. memset(timer, 0, sizeof(struct hrtimer));
  982. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  983. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  984. clock_id = CLOCK_MONOTONIC;
  985. timer->base = &cpu_base->clock_base[clock_id];
  986. INIT_LIST_HEAD(&timer->cb_entry);
  987. hrtimer_init_timer_hres(timer);
  988. #ifdef CONFIG_TIMER_STATS
  989. timer->start_site = NULL;
  990. timer->start_pid = -1;
  991. memset(timer->start_comm, 0, TASK_COMM_LEN);
  992. #endif
  993. }
  994. /**
  995. * hrtimer_init - initialize a timer to the given clock
  996. * @timer: the timer to be initialized
  997. * @clock_id: the clock to be used
  998. * @mode: timer mode abs/rel
  999. */
  1000. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  1001. enum hrtimer_mode mode)
  1002. {
  1003. debug_hrtimer_init(timer);
  1004. __hrtimer_init(timer, clock_id, mode);
  1005. }
  1006. EXPORT_SYMBOL_GPL(hrtimer_init);
  1007. /**
  1008. * hrtimer_get_res - get the timer resolution for a clock
  1009. * @which_clock: which clock to query
  1010. * @tp: pointer to timespec variable to store the resolution
  1011. *
  1012. * Store the resolution of the clock selected by @which_clock in the
  1013. * variable pointed to by @tp.
  1014. */
  1015. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  1016. {
  1017. struct hrtimer_cpu_base *cpu_base;
  1018. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  1019. *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
  1020. return 0;
  1021. }
  1022. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  1023. static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base)
  1024. {
  1025. spin_lock_irq(&cpu_base->lock);
  1026. while (!list_empty(&cpu_base->cb_pending)) {
  1027. enum hrtimer_restart (*fn)(struct hrtimer *);
  1028. struct hrtimer *timer;
  1029. int restart;
  1030. int emulate_hardirq_ctx = 0;
  1031. timer = list_entry(cpu_base->cb_pending.next,
  1032. struct hrtimer, cb_entry);
  1033. debug_hrtimer_deactivate(timer);
  1034. timer_stats_account_hrtimer(timer);
  1035. fn = timer->function;
  1036. /*
  1037. * A timer might have been added to the cb_pending list
  1038. * when it was migrated during a cpu-offline operation.
  1039. * Emulate hardirq context for such timers.
  1040. */
  1041. if (timer->cb_mode == HRTIMER_CB_IRQSAFE_PERCPU ||
  1042. timer->cb_mode == HRTIMER_CB_IRQSAFE_UNLOCKED)
  1043. emulate_hardirq_ctx = 1;
  1044. __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
  1045. spin_unlock_irq(&cpu_base->lock);
  1046. if (unlikely(emulate_hardirq_ctx)) {
  1047. local_irq_disable();
  1048. restart = fn(timer);
  1049. local_irq_enable();
  1050. } else
  1051. restart = fn(timer);
  1052. spin_lock_irq(&cpu_base->lock);
  1053. timer->state &= ~HRTIMER_STATE_CALLBACK;
  1054. if (restart == HRTIMER_RESTART) {
  1055. BUG_ON(hrtimer_active(timer));
  1056. /*
  1057. * Enqueue the timer, allow reprogramming of the event
  1058. * device
  1059. */
  1060. enqueue_hrtimer(timer, timer->base, 1);
  1061. } else if (hrtimer_active(timer)) {
  1062. /*
  1063. * If the timer was rearmed on another CPU, reprogram
  1064. * the event device.
  1065. */
  1066. struct hrtimer_clock_base *base = timer->base;
  1067. if (base->first == &timer->node &&
  1068. hrtimer_reprogram(timer, base)) {
  1069. /*
  1070. * Timer is expired. Thus move it from tree to
  1071. * pending list again.
  1072. */
  1073. __remove_hrtimer(timer, base,
  1074. HRTIMER_STATE_PENDING, 0);
  1075. list_add_tail(&timer->cb_entry,
  1076. &base->cpu_base->cb_pending);
  1077. }
  1078. }
  1079. }
  1080. spin_unlock_irq(&cpu_base->lock);
  1081. }
  1082. static void __run_hrtimer(struct hrtimer *timer)
  1083. {
  1084. struct hrtimer_clock_base *base = timer->base;
  1085. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  1086. enum hrtimer_restart (*fn)(struct hrtimer *);
  1087. int restart;
  1088. debug_hrtimer_deactivate(timer);
  1089. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  1090. timer_stats_account_hrtimer(timer);
  1091. fn = timer->function;
  1092. if (timer->cb_mode == HRTIMER_CB_IRQSAFE_PERCPU ||
  1093. timer->cb_mode == HRTIMER_CB_IRQSAFE_UNLOCKED) {
  1094. /*
  1095. * Used for scheduler timers, avoid lock inversion with
  1096. * rq->lock and tasklist_lock.
  1097. *
  1098. * These timers are required to deal with enqueue expiry
  1099. * themselves and are not allowed to migrate.
  1100. */
  1101. spin_unlock(&cpu_base->lock);
  1102. restart = fn(timer);
  1103. spin_lock(&cpu_base->lock);
  1104. } else
  1105. restart = fn(timer);
  1106. /*
  1107. * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
  1108. * reprogramming of the event hardware. This happens at the end of this
  1109. * function anyway.
  1110. */
  1111. if (restart != HRTIMER_NORESTART) {
  1112. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  1113. enqueue_hrtimer(timer, base, 0);
  1114. }
  1115. timer->state &= ~HRTIMER_STATE_CALLBACK;
  1116. }
  1117. #ifdef CONFIG_HIGH_RES_TIMERS
  1118. /*
  1119. * High resolution timer interrupt
  1120. * Called with interrupts disabled
  1121. */
  1122. void hrtimer_interrupt(struct clock_event_device *dev)
  1123. {
  1124. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1125. struct hrtimer_clock_base *base;
  1126. ktime_t expires_next, now;
  1127. int i, raise = 0;
  1128. BUG_ON(!cpu_base->hres_active);
  1129. cpu_base->nr_events++;
  1130. dev->next_event.tv64 = KTIME_MAX;
  1131. retry:
  1132. now = ktime_get();
  1133. expires_next.tv64 = KTIME_MAX;
  1134. base = cpu_base->clock_base;
  1135. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1136. ktime_t basenow;
  1137. struct rb_node *node;
  1138. spin_lock(&cpu_base->lock);
  1139. basenow = ktime_add(now, base->offset);
  1140. while ((node = base->first)) {
  1141. struct hrtimer *timer;
  1142. timer = rb_entry(node, struct hrtimer, node);
  1143. /*
  1144. * The immediate goal for using the softexpires is
  1145. * minimizing wakeups, not running timers at the
  1146. * earliest interrupt after their soft expiration.
  1147. * This allows us to avoid using a Priority Search
  1148. * Tree, which can answer a stabbing querry for
  1149. * overlapping intervals and instead use the simple
  1150. * BST we already have.
  1151. * We don't add extra wakeups by delaying timers that
  1152. * are right-of a not yet expired timer, because that
  1153. * timer will have to trigger a wakeup anyway.
  1154. */
  1155. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
  1156. ktime_t expires;
  1157. expires = ktime_sub(hrtimer_get_expires(timer),
  1158. base->offset);
  1159. if (expires.tv64 < expires_next.tv64)
  1160. expires_next = expires;
  1161. break;
  1162. }
  1163. /* Move softirq callbacks to the pending list */
  1164. if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
  1165. __remove_hrtimer(timer, base,
  1166. HRTIMER_STATE_PENDING, 0);
  1167. list_add_tail(&timer->cb_entry,
  1168. &base->cpu_base->cb_pending);
  1169. raise = 1;
  1170. continue;
  1171. }
  1172. __run_hrtimer(timer);
  1173. }
  1174. spin_unlock(&cpu_base->lock);
  1175. base++;
  1176. }
  1177. cpu_base->expires_next = expires_next;
  1178. /* Reprogramming necessary ? */
  1179. if (expires_next.tv64 != KTIME_MAX) {
  1180. if (tick_program_event(expires_next, 0))
  1181. goto retry;
  1182. }
  1183. /* Raise softirq ? */
  1184. if (raise)
  1185. raise_softirq(HRTIMER_SOFTIRQ);
  1186. }
  1187. /**
  1188. * hrtimer_peek_ahead_timers -- run soft-expired timers now
  1189. *
  1190. * hrtimer_peek_ahead_timers will peek at the timer queue of
  1191. * the current cpu and check if there are any timers for which
  1192. * the soft expires time has passed. If any such timers exist,
  1193. * they are run immediately and then removed from the timer queue.
  1194. *
  1195. */
  1196. void hrtimer_peek_ahead_timers(void)
  1197. {
  1198. struct tick_device *td;
  1199. unsigned long flags;
  1200. if (!hrtimer_hres_active())
  1201. return;
  1202. local_irq_save(flags);
  1203. td = &__get_cpu_var(tick_cpu_device);
  1204. if (td && td->evtdev)
  1205. hrtimer_interrupt(td->evtdev);
  1206. local_irq_restore(flags);
  1207. }
  1208. static void run_hrtimer_softirq(struct softirq_action *h)
  1209. {
  1210. run_hrtimer_pending(&__get_cpu_var(hrtimer_bases));
  1211. }
  1212. #endif /* CONFIG_HIGH_RES_TIMERS */
  1213. /*
  1214. * Called from timer softirq every jiffy, expire hrtimers:
  1215. *
  1216. * For HRT its the fall back code to run the softirq in the timer
  1217. * softirq context in case the hrtimer initialization failed or has
  1218. * not been done yet.
  1219. */
  1220. void hrtimer_run_pending(void)
  1221. {
  1222. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1223. if (hrtimer_hres_active())
  1224. return;
  1225. /*
  1226. * This _is_ ugly: We have to check in the softirq context,
  1227. * whether we can switch to highres and / or nohz mode. The
  1228. * clocksource switch happens in the timer interrupt with
  1229. * xtime_lock held. Notification from there only sets the
  1230. * check bit in the tick_oneshot code, otherwise we might
  1231. * deadlock vs. xtime_lock.
  1232. */
  1233. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
  1234. hrtimer_switch_to_hres();
  1235. run_hrtimer_pending(cpu_base);
  1236. }
  1237. /*
  1238. * Called from hardirq context every jiffy
  1239. */
  1240. void hrtimer_run_queues(void)
  1241. {
  1242. struct rb_node *node;
  1243. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1244. struct hrtimer_clock_base *base;
  1245. int index, gettime = 1;
  1246. if (hrtimer_hres_active())
  1247. return;
  1248. for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  1249. base = &cpu_base->clock_base[index];
  1250. if (!base->first)
  1251. continue;
  1252. if (gettime) {
  1253. hrtimer_get_softirq_time(cpu_base);
  1254. gettime = 0;
  1255. }
  1256. spin_lock(&cpu_base->lock);
  1257. while ((node = base->first)) {
  1258. struct hrtimer *timer;
  1259. timer = rb_entry(node, struct hrtimer, node);
  1260. if (base->softirq_time.tv64 <=
  1261. hrtimer_get_expires_tv64(timer))
  1262. break;
  1263. if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
  1264. __remove_hrtimer(timer, base,
  1265. HRTIMER_STATE_PENDING, 0);
  1266. list_add_tail(&timer->cb_entry,
  1267. &base->cpu_base->cb_pending);
  1268. continue;
  1269. }
  1270. __run_hrtimer(timer);
  1271. }
  1272. spin_unlock(&cpu_base->lock);
  1273. }
  1274. }
  1275. /*
  1276. * Sleep related functions:
  1277. */
  1278. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1279. {
  1280. struct hrtimer_sleeper *t =
  1281. container_of(timer, struct hrtimer_sleeper, timer);
  1282. struct task_struct *task = t->task;
  1283. t->task = NULL;
  1284. if (task)
  1285. wake_up_process(task);
  1286. return HRTIMER_NORESTART;
  1287. }
  1288. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1289. {
  1290. sl->timer.function = hrtimer_wakeup;
  1291. sl->task = task;
  1292. #ifdef CONFIG_HIGH_RES_TIMERS
  1293. sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_UNLOCKED;
  1294. #endif
  1295. }
  1296. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1297. {
  1298. hrtimer_init_sleeper(t, current);
  1299. do {
  1300. set_current_state(TASK_INTERRUPTIBLE);
  1301. hrtimer_start_expires(&t->timer, mode);
  1302. if (!hrtimer_active(&t->timer))
  1303. t->task = NULL;
  1304. if (likely(t->task))
  1305. schedule();
  1306. hrtimer_cancel(&t->timer);
  1307. mode = HRTIMER_MODE_ABS;
  1308. } while (t->task && !signal_pending(current));
  1309. __set_current_state(TASK_RUNNING);
  1310. return t->task == NULL;
  1311. }
  1312. static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
  1313. {
  1314. struct timespec rmt;
  1315. ktime_t rem;
  1316. rem = hrtimer_expires_remaining(timer);
  1317. if (rem.tv64 <= 0)
  1318. return 0;
  1319. rmt = ktime_to_timespec(rem);
  1320. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  1321. return -EFAULT;
  1322. return 1;
  1323. }
  1324. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1325. {
  1326. struct hrtimer_sleeper t;
  1327. struct timespec __user *rmtp;
  1328. int ret = 0;
  1329. hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
  1330. HRTIMER_MODE_ABS);
  1331. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  1332. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  1333. goto out;
  1334. rmtp = restart->nanosleep.rmtp;
  1335. if (rmtp) {
  1336. ret = update_rmtp(&t.timer, rmtp);
  1337. if (ret <= 0)
  1338. goto out;
  1339. }
  1340. /* The other values in restart are already filled in */
  1341. ret = -ERESTART_RESTARTBLOCK;
  1342. out:
  1343. destroy_hrtimer_on_stack(&t.timer);
  1344. return ret;
  1345. }
  1346. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  1347. const enum hrtimer_mode mode, const clockid_t clockid)
  1348. {
  1349. struct restart_block *restart;
  1350. struct hrtimer_sleeper t;
  1351. int ret = 0;
  1352. unsigned long slack;
  1353. slack = current->timer_slack_ns;
  1354. if (rt_task(current))
  1355. slack = 0;
  1356. hrtimer_init_on_stack(&t.timer, clockid, mode);
  1357. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  1358. if (do_nanosleep(&t, mode))
  1359. goto out;
  1360. /* Absolute timers do not update the rmtp value and restart: */
  1361. if (mode == HRTIMER_MODE_ABS) {
  1362. ret = -ERESTARTNOHAND;
  1363. goto out;
  1364. }
  1365. if (rmtp) {
  1366. ret = update_rmtp(&t.timer, rmtp);
  1367. if (ret <= 0)
  1368. goto out;
  1369. }
  1370. restart = &current_thread_info()->restart_block;
  1371. restart->fn = hrtimer_nanosleep_restart;
  1372. restart->nanosleep.index = t.timer.base->index;
  1373. restart->nanosleep.rmtp = rmtp;
  1374. restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
  1375. ret = -ERESTART_RESTARTBLOCK;
  1376. out:
  1377. destroy_hrtimer_on_stack(&t.timer);
  1378. return ret;
  1379. }
  1380. asmlinkage long
  1381. sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
  1382. {
  1383. struct timespec tu;
  1384. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  1385. return -EFAULT;
  1386. if (!timespec_valid(&tu))
  1387. return -EINVAL;
  1388. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1389. }
  1390. /*
  1391. * Functions related to boot-time initialization:
  1392. */
  1393. static void __cpuinit init_hrtimers_cpu(int cpu)
  1394. {
  1395. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1396. int i;
  1397. spin_lock_init(&cpu_base->lock);
  1398. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  1399. cpu_base->clock_base[i].cpu_base = cpu_base;
  1400. INIT_LIST_HEAD(&cpu_base->cb_pending);
  1401. hrtimer_init_hres(cpu_base);
  1402. }
  1403. #ifdef CONFIG_HOTPLUG_CPU
  1404. static int migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1405. struct hrtimer_clock_base *new_base, int dcpu)
  1406. {
  1407. struct hrtimer *timer;
  1408. struct rb_node *node;
  1409. int raise = 0;
  1410. while ((node = rb_first(&old_base->active))) {
  1411. timer = rb_entry(node, struct hrtimer, node);
  1412. BUG_ON(hrtimer_callback_running(timer));
  1413. debug_hrtimer_deactivate(timer);
  1414. /*
  1415. * Should not happen. Per CPU timers should be
  1416. * canceled _before_ the migration code is called
  1417. */
  1418. if (timer->cb_mode == HRTIMER_CB_IRQSAFE_PERCPU) {
  1419. __remove_hrtimer(timer, old_base,
  1420. HRTIMER_STATE_INACTIVE, 0);
  1421. WARN(1, "hrtimer (%p %p)active but cpu %d dead\n",
  1422. timer, timer->function, dcpu);
  1423. continue;
  1424. }
  1425. /*
  1426. * Mark it as STATE_MIGRATE not INACTIVE otherwise the
  1427. * timer could be seen as !active and just vanish away
  1428. * under us on another CPU
  1429. */
  1430. __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
  1431. timer->base = new_base;
  1432. /*
  1433. * Enqueue the timer. Allow reprogramming of the event device
  1434. */
  1435. enqueue_hrtimer(timer, new_base, 1);
  1436. #ifdef CONFIG_HIGH_RES_TIMERS
  1437. /*
  1438. * Happens with high res enabled when the timer was
  1439. * already expired and the callback mode is
  1440. * HRTIMER_CB_IRQSAFE_UNLOCKED (hrtimer_sleeper). The
  1441. * enqueue code does not move them to the soft irq
  1442. * pending list for performance/latency reasons, but
  1443. * in the migration state, we need to do that
  1444. * otherwise we end up with a stale timer.
  1445. */
  1446. if (timer->state == HRTIMER_STATE_MIGRATE) {
  1447. timer->state = HRTIMER_STATE_PENDING;
  1448. list_add_tail(&timer->cb_entry,
  1449. &new_base->cpu_base->cb_pending);
  1450. raise = 1;
  1451. }
  1452. #endif
  1453. /* Clear the migration state bit */
  1454. timer->state &= ~HRTIMER_STATE_MIGRATE;
  1455. }
  1456. return raise;
  1457. }
  1458. #ifdef CONFIG_HIGH_RES_TIMERS
  1459. static int migrate_hrtimer_pending(struct hrtimer_cpu_base *old_base,
  1460. struct hrtimer_cpu_base *new_base)
  1461. {
  1462. struct hrtimer *timer;
  1463. int raise = 0;
  1464. while (!list_empty(&old_base->cb_pending)) {
  1465. timer = list_entry(old_base->cb_pending.next,
  1466. struct hrtimer, cb_entry);
  1467. __remove_hrtimer(timer, timer->base, HRTIMER_STATE_PENDING, 0);
  1468. timer->base = &new_base->clock_base[timer->base->index];
  1469. list_add_tail(&timer->cb_entry, &new_base->cb_pending);
  1470. raise = 1;
  1471. }
  1472. return raise;
  1473. }
  1474. #else
  1475. static int migrate_hrtimer_pending(struct hrtimer_cpu_base *old_base,
  1476. struct hrtimer_cpu_base *new_base)
  1477. {
  1478. return 0;
  1479. }
  1480. #endif
  1481. static void migrate_hrtimers(int cpu)
  1482. {
  1483. struct hrtimer_cpu_base *old_base, *new_base;
  1484. int i, raise = 0;
  1485. BUG_ON(cpu_online(cpu));
  1486. old_base = &per_cpu(hrtimer_bases, cpu);
  1487. new_base = &get_cpu_var(hrtimer_bases);
  1488. tick_cancel_sched_timer(cpu);
  1489. /*
  1490. * The caller is globally serialized and nobody else
  1491. * takes two locks at once, deadlock is not possible.
  1492. */
  1493. spin_lock_irq(&new_base->lock);
  1494. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1495. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1496. if (migrate_hrtimer_list(&old_base->clock_base[i],
  1497. &new_base->clock_base[i], cpu))
  1498. raise = 1;
  1499. }
  1500. if (migrate_hrtimer_pending(old_base, new_base))
  1501. raise = 1;
  1502. spin_unlock(&old_base->lock);
  1503. spin_unlock_irq(&new_base->lock);
  1504. put_cpu_var(hrtimer_bases);
  1505. if (raise)
  1506. hrtimer_raise_softirq();
  1507. }
  1508. #endif /* CONFIG_HOTPLUG_CPU */
  1509. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  1510. unsigned long action, void *hcpu)
  1511. {
  1512. unsigned int cpu = (long)hcpu;
  1513. switch (action) {
  1514. case CPU_UP_PREPARE:
  1515. case CPU_UP_PREPARE_FROZEN:
  1516. init_hrtimers_cpu(cpu);
  1517. break;
  1518. #ifdef CONFIG_HOTPLUG_CPU
  1519. case CPU_DEAD:
  1520. case CPU_DEAD_FROZEN:
  1521. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
  1522. migrate_hrtimers(cpu);
  1523. break;
  1524. #endif
  1525. default:
  1526. break;
  1527. }
  1528. return NOTIFY_OK;
  1529. }
  1530. static struct notifier_block __cpuinitdata hrtimers_nb = {
  1531. .notifier_call = hrtimer_cpu_notify,
  1532. };
  1533. void __init hrtimers_init(void)
  1534. {
  1535. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1536. (void *)(long)smp_processor_id());
  1537. register_cpu_notifier(&hrtimers_nb);
  1538. #ifdef CONFIG_HIGH_RES_TIMERS
  1539. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  1540. #endif
  1541. }
  1542. /**
  1543. * schedule_hrtimeout_range - sleep until timeout
  1544. * @expires: timeout value (ktime_t)
  1545. * @delta: slack in expires timeout (ktime_t)
  1546. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1547. *
  1548. * Make the current task sleep until the given expiry time has
  1549. * elapsed. The routine will return immediately unless
  1550. * the current task state has been set (see set_current_state()).
  1551. *
  1552. * The @delta argument gives the kernel the freedom to schedule the
  1553. * actual wakeup to a time that is both power and performance friendly.
  1554. * The kernel give the normal best effort behavior for "@expires+@delta",
  1555. * but may decide to fire the timer earlier, but no earlier than @expires.
  1556. *
  1557. * You can set the task state as follows -
  1558. *
  1559. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1560. * pass before the routine returns.
  1561. *
  1562. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1563. * delivered to the current task.
  1564. *
  1565. * The current task state is guaranteed to be TASK_RUNNING when this
  1566. * routine returns.
  1567. *
  1568. * Returns 0 when the timer has expired otherwise -EINTR
  1569. */
  1570. int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
  1571. const enum hrtimer_mode mode)
  1572. {
  1573. struct hrtimer_sleeper t;
  1574. /*
  1575. * Optimize when a zero timeout value is given. It does not
  1576. * matter whether this is an absolute or a relative time.
  1577. */
  1578. if (expires && !expires->tv64) {
  1579. __set_current_state(TASK_RUNNING);
  1580. return 0;
  1581. }
  1582. /*
  1583. * A NULL parameter means "inifinte"
  1584. */
  1585. if (!expires) {
  1586. schedule();
  1587. __set_current_state(TASK_RUNNING);
  1588. return -EINTR;
  1589. }
  1590. hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
  1591. hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
  1592. hrtimer_init_sleeper(&t, current);
  1593. hrtimer_start_expires(&t.timer, mode);
  1594. if (!hrtimer_active(&t.timer))
  1595. t.task = NULL;
  1596. if (likely(t.task))
  1597. schedule();
  1598. hrtimer_cancel(&t.timer);
  1599. destroy_hrtimer_on_stack(&t.timer);
  1600. __set_current_state(TASK_RUNNING);
  1601. return !t.task ? 0 : -EINTR;
  1602. }
  1603. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
  1604. /**
  1605. * schedule_hrtimeout - sleep until timeout
  1606. * @expires: timeout value (ktime_t)
  1607. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1608. *
  1609. * Make the current task sleep until the given expiry time has
  1610. * elapsed. The routine will return immediately unless
  1611. * the current task state has been set (see set_current_state()).
  1612. *
  1613. * You can set the task state as follows -
  1614. *
  1615. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1616. * pass before the routine returns.
  1617. *
  1618. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1619. * delivered to the current task.
  1620. *
  1621. * The current task state is guaranteed to be TASK_RUNNING when this
  1622. * routine returns.
  1623. *
  1624. * Returns 0 when the timer has expired otherwise -EINTR
  1625. */
  1626. int __sched schedule_hrtimeout(ktime_t *expires,
  1627. const enum hrtimer_mode mode)
  1628. {
  1629. return schedule_hrtimeout_range(expires, 0, mode);
  1630. }
  1631. EXPORT_SYMBOL_GPL(schedule_hrtimeout);