hrtimer.c 45 KB

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