hrtimer.c 45 KB

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