hrtimer.c 44 KB

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