hrtimer.c 44 KB

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