hrtimer.c 43 KB

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