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