hrtimer.c 47 KB

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