hrtimer.c 40 KB

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