hrtimer.c 44 KB

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