hrtimer.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  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 hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
  439. {
  440. int i;
  441. struct hrtimer_clock_base *base = cpu_base->clock_base;
  442. ktime_t expires;
  443. cpu_base->expires_next.tv64 = KTIME_MAX;
  444. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  445. struct hrtimer *timer;
  446. if (!base->first)
  447. continue;
  448. timer = rb_entry(base->first, struct hrtimer, node);
  449. expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  450. /*
  451. * clock_was_set() has changed base->offset so the
  452. * result might be negative. Fix it up to prevent a
  453. * false positive in clockevents_program_event()
  454. */
  455. if (expires.tv64 < 0)
  456. expires.tv64 = 0;
  457. if (expires.tv64 < cpu_base->expires_next.tv64)
  458. cpu_base->expires_next = expires;
  459. }
  460. if (cpu_base->expires_next.tv64 != KTIME_MAX)
  461. tick_program_event(cpu_base->expires_next, 1);
  462. }
  463. /*
  464. * Shared reprogramming for clock_realtime and clock_monotonic
  465. *
  466. * When a timer is enqueued and expires earlier than the already enqueued
  467. * timers, we have to check, whether it expires earlier than the timer for
  468. * which the clock event device was armed.
  469. *
  470. * Called with interrupts disabled and base->cpu_base.lock held
  471. */
  472. static int hrtimer_reprogram(struct hrtimer *timer,
  473. struct hrtimer_clock_base *base)
  474. {
  475. ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
  476. ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  477. int res;
  478. WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
  479. /*
  480. * When the callback is running, we do not reprogram the clock event
  481. * device. The timer callback is either running on a different CPU or
  482. * the callback is executed in the hrtimer_interrupt context. The
  483. * reprogramming is handled either by the softirq, which called the
  484. * callback or at the end of the hrtimer_interrupt.
  485. */
  486. if (hrtimer_callback_running(timer))
  487. return 0;
  488. /*
  489. * CLOCK_REALTIME timer might be requested with an absolute
  490. * expiry time which is less than base->offset. Nothing wrong
  491. * about that, just avoid to call into the tick code, which
  492. * has now objections against negative expiry values.
  493. */
  494. if (expires.tv64 < 0)
  495. return -ETIME;
  496. if (expires.tv64 >= expires_next->tv64)
  497. return 0;
  498. /*
  499. * Clockevents returns -ETIME, when the event was in the past.
  500. */
  501. res = tick_program_event(expires, 0);
  502. if (!IS_ERR_VALUE(res))
  503. *expires_next = expires;
  504. return res;
  505. }
  506. /*
  507. * Retrigger next event is called after clock was set
  508. *
  509. * Called with interrupts disabled via on_each_cpu()
  510. */
  511. static void retrigger_next_event(void *arg)
  512. {
  513. struct hrtimer_cpu_base *base;
  514. struct timespec realtime_offset;
  515. unsigned long seq;
  516. if (!hrtimer_hres_active())
  517. return;
  518. do {
  519. seq = read_seqbegin(&xtime_lock);
  520. set_normalized_timespec(&realtime_offset,
  521. -wall_to_monotonic.tv_sec,
  522. -wall_to_monotonic.tv_nsec);
  523. } while (read_seqretry(&xtime_lock, seq));
  524. base = &__get_cpu_var(hrtimer_bases);
  525. /* Adjust CLOCK_REALTIME offset */
  526. spin_lock(&base->lock);
  527. base->clock_base[CLOCK_REALTIME].offset =
  528. timespec_to_ktime(realtime_offset);
  529. hrtimer_force_reprogram(base);
  530. spin_unlock(&base->lock);
  531. }
  532. /*
  533. * Clock realtime was set
  534. *
  535. * Change the offset of the realtime clock vs. the monotonic
  536. * clock.
  537. *
  538. * We might have to reprogram the high resolution timer interrupt. On
  539. * SMP we call the architecture specific code to retrigger _all_ high
  540. * resolution timer interrupts. On UP we just disable interrupts and
  541. * call the high resolution interrupt code.
  542. */
  543. void clock_was_set(void)
  544. {
  545. /* Retrigger the CPU local events everywhere */
  546. on_each_cpu(retrigger_next_event, NULL, 1);
  547. }
  548. /*
  549. * During resume we might have to reprogram the high resolution timer
  550. * interrupt (on the local CPU):
  551. */
  552. void hres_timers_resume(void)
  553. {
  554. WARN_ONCE(!irqs_disabled(),
  555. KERN_INFO "hres_timers_resume() called with IRQs enabled!");
  556. retrigger_next_event(NULL);
  557. }
  558. /*
  559. * Initialize the high resolution related parts of cpu_base
  560. */
  561. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
  562. {
  563. base->expires_next.tv64 = KTIME_MAX;
  564. base->hres_active = 0;
  565. }
  566. /*
  567. * Initialize the high resolution related parts of a hrtimer
  568. */
  569. static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
  570. {
  571. }
  572. /*
  573. * When High resolution timers are active, try to reprogram. Note, that in case
  574. * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
  575. * check happens. The timer gets enqueued into the rbtree. The reprogramming
  576. * and expiry check is done in the hrtimer_interrupt or in the softirq.
  577. */
  578. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  579. struct hrtimer_clock_base *base,
  580. int wakeup)
  581. {
  582. if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
  583. if (wakeup) {
  584. spin_unlock(&base->cpu_base->lock);
  585. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  586. spin_lock(&base->cpu_base->lock);
  587. } else
  588. __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  589. return 1;
  590. }
  591. return 0;
  592. }
  593. /*
  594. * Switch to high resolution mode
  595. */
  596. static int hrtimer_switch_to_hres(void)
  597. {
  598. int cpu = smp_processor_id();
  599. struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
  600. unsigned long flags;
  601. if (base->hres_active)
  602. return 1;
  603. local_irq_save(flags);
  604. if (tick_init_highres()) {
  605. local_irq_restore(flags);
  606. printk(KERN_WARNING "Could not switch to high resolution "
  607. "mode on CPU %d\n", cpu);
  608. return 0;
  609. }
  610. base->hres_active = 1;
  611. base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
  612. base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
  613. tick_setup_sched_timer();
  614. /* "Retrigger" the interrupt to get things going */
  615. retrigger_next_event(NULL);
  616. local_irq_restore(flags);
  617. printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
  618. smp_processor_id());
  619. return 1;
  620. }
  621. #else
  622. static inline int hrtimer_hres_active(void) { return 0; }
  623. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  624. static inline int hrtimer_switch_to_hres(void) { return 0; }
  625. static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
  626. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  627. struct hrtimer_clock_base *base,
  628. int wakeup)
  629. {
  630. return 0;
  631. }
  632. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  633. static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
  634. #endif /* CONFIG_HIGH_RES_TIMERS */
  635. #ifdef CONFIG_TIMER_STATS
  636. void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
  637. {
  638. if (timer->start_site)
  639. return;
  640. timer->start_site = addr;
  641. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  642. timer->start_pid = current->pid;
  643. }
  644. #endif
  645. /*
  646. * Counterpart to lock_hrtimer_base above:
  647. */
  648. static inline
  649. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  650. {
  651. spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  652. }
  653. /**
  654. * hrtimer_forward - forward the timer expiry
  655. * @timer: hrtimer to forward
  656. * @now: forward past this time
  657. * @interval: the interval to forward
  658. *
  659. * Forward the timer expiry so it will expire in the future.
  660. * Returns the number of overruns.
  661. */
  662. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  663. {
  664. u64 orun = 1;
  665. ktime_t delta;
  666. delta = ktime_sub(now, hrtimer_get_expires(timer));
  667. if (delta.tv64 < 0)
  668. return 0;
  669. if (interval.tv64 < timer->base->resolution.tv64)
  670. interval.tv64 = timer->base->resolution.tv64;
  671. if (unlikely(delta.tv64 >= interval.tv64)) {
  672. s64 incr = ktime_to_ns(interval);
  673. orun = ktime_divns(delta, incr);
  674. hrtimer_add_expires_ns(timer, incr * orun);
  675. if (hrtimer_get_expires_tv64(timer) > now.tv64)
  676. return orun;
  677. /*
  678. * This (and the ktime_add() below) is the
  679. * correction for exact:
  680. */
  681. orun++;
  682. }
  683. hrtimer_add_expires(timer, interval);
  684. return orun;
  685. }
  686. EXPORT_SYMBOL_GPL(hrtimer_forward);
  687. /*
  688. * enqueue_hrtimer - internal function to (re)start a timer
  689. *
  690. * The timer is inserted in expiry order. Insertion into the
  691. * red black tree is O(log(n)). Must hold the base lock.
  692. *
  693. * Returns 1 when the new timer is the leftmost timer in the tree.
  694. */
  695. static int enqueue_hrtimer(struct hrtimer *timer,
  696. struct hrtimer_clock_base *base)
  697. {
  698. struct rb_node **link = &base->active.rb_node;
  699. struct rb_node *parent = NULL;
  700. struct hrtimer *entry;
  701. int leftmost = 1;
  702. debug_activate(timer);
  703. /*
  704. * Find the right place in the rbtree:
  705. */
  706. while (*link) {
  707. parent = *link;
  708. entry = rb_entry(parent, struct hrtimer, node);
  709. /*
  710. * We dont care about collisions. Nodes with
  711. * the same expiry time stay together.
  712. */
  713. if (hrtimer_get_expires_tv64(timer) <
  714. hrtimer_get_expires_tv64(entry)) {
  715. link = &(*link)->rb_left;
  716. } else {
  717. link = &(*link)->rb_right;
  718. leftmost = 0;
  719. }
  720. }
  721. /*
  722. * Insert the timer to the rbtree and check whether it
  723. * replaces the first pending timer
  724. */
  725. if (leftmost)
  726. base->first = &timer->node;
  727. rb_link_node(&timer->node, parent, link);
  728. rb_insert_color(&timer->node, &base->active);
  729. /*
  730. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  731. * state of a possibly running callback.
  732. */
  733. timer->state |= HRTIMER_STATE_ENQUEUED;
  734. return leftmost;
  735. }
  736. /*
  737. * __remove_hrtimer - internal function to remove a timer
  738. *
  739. * Caller must hold the base lock.
  740. *
  741. * High resolution timer mode reprograms the clock event device when the
  742. * timer is the one which expires next. The caller can disable this by setting
  743. * reprogram to zero. This is useful, when the context does a reprogramming
  744. * anyway (e.g. timer interrupt)
  745. */
  746. static void __remove_hrtimer(struct hrtimer *timer,
  747. struct hrtimer_clock_base *base,
  748. unsigned long newstate, int reprogram)
  749. {
  750. if (timer->state & HRTIMER_STATE_ENQUEUED) {
  751. /*
  752. * Remove the timer from the rbtree and replace the
  753. * first entry pointer if necessary.
  754. */
  755. if (base->first == &timer->node) {
  756. base->first = rb_next(&timer->node);
  757. /* Reprogram the clock event device. if enabled */
  758. if (reprogram && hrtimer_hres_active())
  759. hrtimer_force_reprogram(base->cpu_base);
  760. }
  761. rb_erase(&timer->node, &base->active);
  762. }
  763. timer->state = newstate;
  764. }
  765. /*
  766. * remove hrtimer, called with base lock held
  767. */
  768. static inline int
  769. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  770. {
  771. if (hrtimer_is_queued(timer)) {
  772. int reprogram;
  773. /*
  774. * Remove the timer and force reprogramming when high
  775. * resolution mode is active and the timer is on the current
  776. * CPU. If we remove a timer on another CPU, reprogramming is
  777. * skipped. The interrupt event on this CPU is fired and
  778. * reprogramming happens in the interrupt handler. This is a
  779. * rare case and less expensive than a smp call.
  780. */
  781. debug_deactivate(timer);
  782. timer_stats_hrtimer_clear_start_info(timer);
  783. reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
  784. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
  785. reprogram);
  786. return 1;
  787. }
  788. return 0;
  789. }
  790. int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  791. unsigned long delta_ns, const enum hrtimer_mode mode,
  792. int wakeup)
  793. {
  794. struct hrtimer_clock_base *base, *new_base;
  795. unsigned long flags;
  796. int ret, leftmost;
  797. base = lock_hrtimer_base(timer, &flags);
  798. /* Remove an active timer from the queue: */
  799. ret = remove_hrtimer(timer, base);
  800. /* Switch the timer base, if necessary: */
  801. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  802. if (mode & HRTIMER_MODE_REL) {
  803. tim = ktime_add_safe(tim, new_base->get_time());
  804. /*
  805. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  806. * to signal that they simply return xtime in
  807. * do_gettimeoffset(). In this case we want to round up by
  808. * resolution when starting a relative timer, to avoid short
  809. * timeouts. This will go away with the GTOD framework.
  810. */
  811. #ifdef CONFIG_TIME_LOW_RES
  812. tim = ktime_add_safe(tim, base->resolution);
  813. #endif
  814. }
  815. hrtimer_set_expires_range_ns(timer, tim, delta_ns);
  816. timer_stats_hrtimer_set_start_info(timer);
  817. leftmost = enqueue_hrtimer(timer, new_base);
  818. /*
  819. * Only allow reprogramming if the new base is on this CPU.
  820. * (it might still be on another CPU if the timer was pending)
  821. *
  822. * XXX send_remote_softirq() ?
  823. */
  824. if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
  825. hrtimer_enqueue_reprogram(timer, new_base, wakeup);
  826. unlock_hrtimer_base(timer, &flags);
  827. return ret;
  828. }
  829. /**
  830. * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
  831. * @timer: the timer to be added
  832. * @tim: expiry time
  833. * @delta_ns: "slack" range for the timer
  834. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  835. *
  836. * Returns:
  837. * 0 on success
  838. * 1 when the timer was active
  839. */
  840. int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  841. unsigned long delta_ns, const enum hrtimer_mode mode)
  842. {
  843. return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
  844. }
  845. EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
  846. /**
  847. * hrtimer_start - (re)start an hrtimer on the current CPU
  848. * @timer: the timer to be added
  849. * @tim: expiry time
  850. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  851. *
  852. * Returns:
  853. * 0 on success
  854. * 1 when the timer was active
  855. */
  856. int
  857. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  858. {
  859. return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
  860. }
  861. EXPORT_SYMBOL_GPL(hrtimer_start);
  862. /**
  863. * hrtimer_try_to_cancel - try to deactivate a timer
  864. * @timer: hrtimer to stop
  865. *
  866. * Returns:
  867. * 0 when the timer was not active
  868. * 1 when the timer was active
  869. * -1 when the timer is currently excuting the callback function and
  870. * cannot be stopped
  871. */
  872. int hrtimer_try_to_cancel(struct hrtimer *timer)
  873. {
  874. struct hrtimer_clock_base *base;
  875. unsigned long flags;
  876. int ret = -1;
  877. base = lock_hrtimer_base(timer, &flags);
  878. if (!hrtimer_callback_running(timer))
  879. ret = remove_hrtimer(timer, base);
  880. unlock_hrtimer_base(timer, &flags);
  881. return ret;
  882. }
  883. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  884. /**
  885. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  886. * @timer: the timer to be cancelled
  887. *
  888. * Returns:
  889. * 0 when the timer was not active
  890. * 1 when the timer was active
  891. */
  892. int hrtimer_cancel(struct hrtimer *timer)
  893. {
  894. for (;;) {
  895. int ret = hrtimer_try_to_cancel(timer);
  896. if (ret >= 0)
  897. return ret;
  898. cpu_relax();
  899. }
  900. }
  901. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  902. /**
  903. * hrtimer_get_remaining - get remaining time for the timer
  904. * @timer: the timer to read
  905. */
  906. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  907. {
  908. struct hrtimer_clock_base *base;
  909. unsigned long flags;
  910. ktime_t rem;
  911. base = lock_hrtimer_base(timer, &flags);
  912. rem = hrtimer_expires_remaining(timer);
  913. unlock_hrtimer_base(timer, &flags);
  914. return rem;
  915. }
  916. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  917. #ifdef CONFIG_NO_HZ
  918. /**
  919. * hrtimer_get_next_event - get the time until next expiry event
  920. *
  921. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  922. * is pending.
  923. */
  924. ktime_t hrtimer_get_next_event(void)
  925. {
  926. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  927. struct hrtimer_clock_base *base = cpu_base->clock_base;
  928. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  929. unsigned long flags;
  930. int i;
  931. spin_lock_irqsave(&cpu_base->lock, flags);
  932. if (!hrtimer_hres_active()) {
  933. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  934. struct hrtimer *timer;
  935. if (!base->first)
  936. continue;
  937. timer = rb_entry(base->first, struct hrtimer, node);
  938. delta.tv64 = hrtimer_get_expires_tv64(timer);
  939. delta = ktime_sub(delta, base->get_time());
  940. if (delta.tv64 < mindelta.tv64)
  941. mindelta.tv64 = delta.tv64;
  942. }
  943. }
  944. spin_unlock_irqrestore(&cpu_base->lock, flags);
  945. if (mindelta.tv64 < 0)
  946. mindelta.tv64 = 0;
  947. return mindelta;
  948. }
  949. #endif
  950. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  951. enum hrtimer_mode mode)
  952. {
  953. struct hrtimer_cpu_base *cpu_base;
  954. memset(timer, 0, sizeof(struct hrtimer));
  955. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  956. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  957. clock_id = CLOCK_MONOTONIC;
  958. timer->base = &cpu_base->clock_base[clock_id];
  959. hrtimer_init_timer_hres(timer);
  960. #ifdef CONFIG_TIMER_STATS
  961. timer->start_site = NULL;
  962. timer->start_pid = -1;
  963. memset(timer->start_comm, 0, TASK_COMM_LEN);
  964. #endif
  965. }
  966. /**
  967. * hrtimer_init - initialize a timer to the given clock
  968. * @timer: the timer to be initialized
  969. * @clock_id: the clock to be used
  970. * @mode: timer mode abs/rel
  971. */
  972. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  973. enum hrtimer_mode mode)
  974. {
  975. debug_init(timer, clock_id, mode);
  976. __hrtimer_init(timer, clock_id, mode);
  977. }
  978. EXPORT_SYMBOL_GPL(hrtimer_init);
  979. /**
  980. * hrtimer_get_res - get the timer resolution for a clock
  981. * @which_clock: which clock to query
  982. * @tp: pointer to timespec variable to store the resolution
  983. *
  984. * Store the resolution of the clock selected by @which_clock in the
  985. * variable pointed to by @tp.
  986. */
  987. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  988. {
  989. struct hrtimer_cpu_base *cpu_base;
  990. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  991. *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
  992. return 0;
  993. }
  994. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  995. static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
  996. {
  997. struct hrtimer_clock_base *base = timer->base;
  998. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  999. enum hrtimer_restart (*fn)(struct hrtimer *);
  1000. int restart;
  1001. WARN_ON(!irqs_disabled());
  1002. debug_deactivate(timer);
  1003. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  1004. timer_stats_account_hrtimer(timer);
  1005. fn = timer->function;
  1006. /*
  1007. * Because we run timers from hardirq context, there is no chance
  1008. * they get migrated to another cpu, therefore its safe to unlock
  1009. * the timer base.
  1010. */
  1011. spin_unlock(&cpu_base->lock);
  1012. trace_hrtimer_expire_entry(timer, now);
  1013. restart = fn(timer);
  1014. trace_hrtimer_expire_exit(timer);
  1015. spin_lock(&cpu_base->lock);
  1016. /*
  1017. * Note: We clear the CALLBACK bit after enqueue_hrtimer and
  1018. * we do not reprogramm the event hardware. Happens either in
  1019. * hrtimer_start_range_ns() or in hrtimer_interrupt()
  1020. */
  1021. if (restart != HRTIMER_NORESTART) {
  1022. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  1023. enqueue_hrtimer(timer, base);
  1024. }
  1025. timer->state &= ~HRTIMER_STATE_CALLBACK;
  1026. }
  1027. #ifdef CONFIG_HIGH_RES_TIMERS
  1028. static int force_clock_reprogram;
  1029. /*
  1030. * After 5 iteration's attempts, we consider that hrtimer_interrupt()
  1031. * is hanging, which could happen with something that slows the interrupt
  1032. * such as the tracing. Then we force the clock reprogramming for each future
  1033. * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
  1034. * threshold that we will overwrite.
  1035. * The next tick event will be scheduled to 3 times we currently spend on
  1036. * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
  1037. * 1/4 of their time to process the hrtimer interrupts. This is enough to
  1038. * let it running without serious starvation.
  1039. */
  1040. static inline void
  1041. hrtimer_interrupt_hanging(struct clock_event_device *dev,
  1042. ktime_t try_time)
  1043. {
  1044. force_clock_reprogram = 1;
  1045. dev->min_delta_ns = (unsigned long)try_time.tv64 * 3;
  1046. printk(KERN_WARNING "hrtimer: interrupt too slow, "
  1047. "forcing clock min delta to %lu ns\n", dev->min_delta_ns);
  1048. }
  1049. /*
  1050. * High resolution timer interrupt
  1051. * Called with interrupts disabled
  1052. */
  1053. void hrtimer_interrupt(struct clock_event_device *dev)
  1054. {
  1055. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1056. struct hrtimer_clock_base *base;
  1057. ktime_t expires_next, now;
  1058. int nr_retries = 0;
  1059. int i;
  1060. BUG_ON(!cpu_base->hres_active);
  1061. cpu_base->nr_events++;
  1062. dev->next_event.tv64 = KTIME_MAX;
  1063. retry:
  1064. /* 5 retries is enough to notice a hang */
  1065. if (!(++nr_retries % 5))
  1066. hrtimer_interrupt_hanging(dev, ktime_sub(ktime_get(), now));
  1067. now = ktime_get();
  1068. expires_next.tv64 = KTIME_MAX;
  1069. spin_lock(&cpu_base->lock);
  1070. /*
  1071. * We set expires_next to KTIME_MAX here with cpu_base->lock
  1072. * held to prevent that a timer is enqueued in our queue via
  1073. * the migration code. This does not affect enqueueing of
  1074. * timers which run their callback and need to be requeued on
  1075. * this CPU.
  1076. */
  1077. cpu_base->expires_next.tv64 = KTIME_MAX;
  1078. base = cpu_base->clock_base;
  1079. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1080. ktime_t basenow;
  1081. struct rb_node *node;
  1082. basenow = ktime_add(now, base->offset);
  1083. while ((node = base->first)) {
  1084. struct hrtimer *timer;
  1085. timer = rb_entry(node, struct hrtimer, node);
  1086. /*
  1087. * The immediate goal for using the softexpires is
  1088. * minimizing wakeups, not running timers at the
  1089. * earliest interrupt after their soft expiration.
  1090. * This allows us to avoid using a Priority Search
  1091. * Tree, which can answer a stabbing querry for
  1092. * overlapping intervals and instead use the simple
  1093. * BST we already have.
  1094. * We don't add extra wakeups by delaying timers that
  1095. * are right-of a not yet expired timer, because that
  1096. * timer will have to trigger a wakeup anyway.
  1097. */
  1098. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
  1099. ktime_t expires;
  1100. expires = ktime_sub(hrtimer_get_expires(timer),
  1101. base->offset);
  1102. if (expires.tv64 < expires_next.tv64)
  1103. expires_next = expires;
  1104. break;
  1105. }
  1106. __run_hrtimer(timer, &basenow);
  1107. }
  1108. base++;
  1109. }
  1110. /*
  1111. * Store the new expiry value so the migration code can verify
  1112. * against it.
  1113. */
  1114. cpu_base->expires_next = expires_next;
  1115. spin_unlock(&cpu_base->lock);
  1116. /* Reprogramming necessary ? */
  1117. if (expires_next.tv64 != KTIME_MAX) {
  1118. if (tick_program_event(expires_next, force_clock_reprogram))
  1119. goto retry;
  1120. }
  1121. }
  1122. /*
  1123. * local version of hrtimer_peek_ahead_timers() called with interrupts
  1124. * disabled.
  1125. */
  1126. static void __hrtimer_peek_ahead_timers(void)
  1127. {
  1128. struct tick_device *td;
  1129. if (!hrtimer_hres_active())
  1130. return;
  1131. td = &__get_cpu_var(tick_cpu_device);
  1132. if (td && td->evtdev)
  1133. hrtimer_interrupt(td->evtdev);
  1134. }
  1135. /**
  1136. * hrtimer_peek_ahead_timers -- run soft-expired timers now
  1137. *
  1138. * hrtimer_peek_ahead_timers will peek at the timer queue of
  1139. * the current cpu and check if there are any timers for which
  1140. * the soft expires time has passed. If any such timers exist,
  1141. * they are run immediately and then removed from the timer queue.
  1142. *
  1143. */
  1144. void hrtimer_peek_ahead_timers(void)
  1145. {
  1146. unsigned long flags;
  1147. local_irq_save(flags);
  1148. __hrtimer_peek_ahead_timers();
  1149. local_irq_restore(flags);
  1150. }
  1151. static void run_hrtimer_softirq(struct softirq_action *h)
  1152. {
  1153. hrtimer_peek_ahead_timers();
  1154. }
  1155. #else /* CONFIG_HIGH_RES_TIMERS */
  1156. static inline void __hrtimer_peek_ahead_timers(void) { }
  1157. #endif /* !CONFIG_HIGH_RES_TIMERS */
  1158. /*
  1159. * Called from timer softirq every jiffy, expire hrtimers:
  1160. *
  1161. * For HRT its the fall back code to run the softirq in the timer
  1162. * softirq context in case the hrtimer initialization failed or has
  1163. * not been done yet.
  1164. */
  1165. void hrtimer_run_pending(void)
  1166. {
  1167. if (hrtimer_hres_active())
  1168. return;
  1169. /*
  1170. * This _is_ ugly: We have to check in the softirq context,
  1171. * whether we can switch to highres and / or nohz mode. The
  1172. * clocksource switch happens in the timer interrupt with
  1173. * xtime_lock held. Notification from there only sets the
  1174. * check bit in the tick_oneshot code, otherwise we might
  1175. * deadlock vs. xtime_lock.
  1176. */
  1177. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
  1178. hrtimer_switch_to_hres();
  1179. }
  1180. /*
  1181. * Called from hardirq context every jiffy
  1182. */
  1183. void hrtimer_run_queues(void)
  1184. {
  1185. struct rb_node *node;
  1186. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1187. struct hrtimer_clock_base *base;
  1188. int index, gettime = 1;
  1189. if (hrtimer_hres_active())
  1190. return;
  1191. for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  1192. base = &cpu_base->clock_base[index];
  1193. if (!base->first)
  1194. continue;
  1195. if (gettime) {
  1196. hrtimer_get_softirq_time(cpu_base);
  1197. gettime = 0;
  1198. }
  1199. spin_lock(&cpu_base->lock);
  1200. while ((node = base->first)) {
  1201. struct hrtimer *timer;
  1202. timer = rb_entry(node, struct hrtimer, node);
  1203. if (base->softirq_time.tv64 <=
  1204. hrtimer_get_expires_tv64(timer))
  1205. break;
  1206. __run_hrtimer(timer, &base->softirq_time);
  1207. }
  1208. spin_unlock(&cpu_base->lock);
  1209. }
  1210. }
  1211. /*
  1212. * Sleep related functions:
  1213. */
  1214. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1215. {
  1216. struct hrtimer_sleeper *t =
  1217. container_of(timer, struct hrtimer_sleeper, timer);
  1218. struct task_struct *task = t->task;
  1219. t->task = NULL;
  1220. if (task)
  1221. wake_up_process(task);
  1222. return HRTIMER_NORESTART;
  1223. }
  1224. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1225. {
  1226. sl->timer.function = hrtimer_wakeup;
  1227. sl->task = task;
  1228. }
  1229. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  1230. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1231. {
  1232. hrtimer_init_sleeper(t, current);
  1233. do {
  1234. set_current_state(TASK_INTERRUPTIBLE);
  1235. hrtimer_start_expires(&t->timer, mode);
  1236. if (!hrtimer_active(&t->timer))
  1237. t->task = NULL;
  1238. if (likely(t->task))
  1239. schedule();
  1240. hrtimer_cancel(&t->timer);
  1241. mode = HRTIMER_MODE_ABS;
  1242. } while (t->task && !signal_pending(current));
  1243. __set_current_state(TASK_RUNNING);
  1244. return t->task == NULL;
  1245. }
  1246. static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
  1247. {
  1248. struct timespec rmt;
  1249. ktime_t rem;
  1250. rem = hrtimer_expires_remaining(timer);
  1251. if (rem.tv64 <= 0)
  1252. return 0;
  1253. rmt = ktime_to_timespec(rem);
  1254. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  1255. return -EFAULT;
  1256. return 1;
  1257. }
  1258. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1259. {
  1260. struct hrtimer_sleeper t;
  1261. struct timespec __user *rmtp;
  1262. int ret = 0;
  1263. hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
  1264. HRTIMER_MODE_ABS);
  1265. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  1266. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  1267. goto out;
  1268. rmtp = restart->nanosleep.rmtp;
  1269. if (rmtp) {
  1270. ret = update_rmtp(&t.timer, rmtp);
  1271. if (ret <= 0)
  1272. goto out;
  1273. }
  1274. /* The other values in restart are already filled in */
  1275. ret = -ERESTART_RESTARTBLOCK;
  1276. out:
  1277. destroy_hrtimer_on_stack(&t.timer);
  1278. return ret;
  1279. }
  1280. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  1281. const enum hrtimer_mode mode, const clockid_t clockid)
  1282. {
  1283. struct restart_block *restart;
  1284. struct hrtimer_sleeper t;
  1285. int ret = 0;
  1286. unsigned long slack;
  1287. slack = current->timer_slack_ns;
  1288. if (rt_task(current))
  1289. slack = 0;
  1290. hrtimer_init_on_stack(&t.timer, clockid, mode);
  1291. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  1292. if (do_nanosleep(&t, mode))
  1293. goto out;
  1294. /* Absolute timers do not update the rmtp value and restart: */
  1295. if (mode == HRTIMER_MODE_ABS) {
  1296. ret = -ERESTARTNOHAND;
  1297. goto out;
  1298. }
  1299. if (rmtp) {
  1300. ret = update_rmtp(&t.timer, rmtp);
  1301. if (ret <= 0)
  1302. goto out;
  1303. }
  1304. restart = &current_thread_info()->restart_block;
  1305. restart->fn = hrtimer_nanosleep_restart;
  1306. restart->nanosleep.index = t.timer.base->index;
  1307. restart->nanosleep.rmtp = rmtp;
  1308. restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
  1309. ret = -ERESTART_RESTARTBLOCK;
  1310. out:
  1311. destroy_hrtimer_on_stack(&t.timer);
  1312. return ret;
  1313. }
  1314. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  1315. struct timespec __user *, rmtp)
  1316. {
  1317. struct timespec tu;
  1318. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  1319. return -EFAULT;
  1320. if (!timespec_valid(&tu))
  1321. return -EINVAL;
  1322. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1323. }
  1324. /*
  1325. * Functions related to boot-time initialization:
  1326. */
  1327. static void __cpuinit init_hrtimers_cpu(int cpu)
  1328. {
  1329. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1330. int i;
  1331. spin_lock_init(&cpu_base->lock);
  1332. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  1333. cpu_base->clock_base[i].cpu_base = cpu_base;
  1334. hrtimer_init_hres(cpu_base);
  1335. }
  1336. #ifdef CONFIG_HOTPLUG_CPU
  1337. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1338. struct hrtimer_clock_base *new_base)
  1339. {
  1340. struct hrtimer *timer;
  1341. struct rb_node *node;
  1342. while ((node = rb_first(&old_base->active))) {
  1343. timer = rb_entry(node, struct hrtimer, node);
  1344. BUG_ON(hrtimer_callback_running(timer));
  1345. debug_deactivate(timer);
  1346. /*
  1347. * Mark it as STATE_MIGRATE not INACTIVE otherwise the
  1348. * timer could be seen as !active and just vanish away
  1349. * under us on another CPU
  1350. */
  1351. __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
  1352. timer->base = new_base;
  1353. /*
  1354. * Enqueue the timers on the new cpu. This does not
  1355. * reprogram the event device in case the timer
  1356. * expires before the earliest on this CPU, but we run
  1357. * hrtimer_interrupt after we migrated everything to
  1358. * sort out already expired timers and reprogram the
  1359. * event device.
  1360. */
  1361. enqueue_hrtimer(timer, new_base);
  1362. /* Clear the migration state bit */
  1363. timer->state &= ~HRTIMER_STATE_MIGRATE;
  1364. }
  1365. }
  1366. static void migrate_hrtimers(int scpu)
  1367. {
  1368. struct hrtimer_cpu_base *old_base, *new_base;
  1369. int i;
  1370. BUG_ON(cpu_online(scpu));
  1371. tick_cancel_sched_timer(scpu);
  1372. local_irq_disable();
  1373. old_base = &per_cpu(hrtimer_bases, scpu);
  1374. new_base = &__get_cpu_var(hrtimer_bases);
  1375. /*
  1376. * The caller is globally serialized and nobody else
  1377. * takes two locks at once, deadlock is not possible.
  1378. */
  1379. spin_lock(&new_base->lock);
  1380. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1381. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1382. migrate_hrtimer_list(&old_base->clock_base[i],
  1383. &new_base->clock_base[i]);
  1384. }
  1385. spin_unlock(&old_base->lock);
  1386. spin_unlock(&new_base->lock);
  1387. /* Check, if we got expired work to do */
  1388. __hrtimer_peek_ahead_timers();
  1389. local_irq_enable();
  1390. }
  1391. #endif /* CONFIG_HOTPLUG_CPU */
  1392. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  1393. unsigned long action, void *hcpu)
  1394. {
  1395. int scpu = (long)hcpu;
  1396. switch (action) {
  1397. case CPU_UP_PREPARE:
  1398. case CPU_UP_PREPARE_FROZEN:
  1399. init_hrtimers_cpu(scpu);
  1400. break;
  1401. #ifdef CONFIG_HOTPLUG_CPU
  1402. case CPU_DYING:
  1403. case CPU_DYING_FROZEN:
  1404. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
  1405. break;
  1406. case CPU_DEAD:
  1407. case CPU_DEAD_FROZEN:
  1408. {
  1409. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
  1410. migrate_hrtimers(scpu);
  1411. break;
  1412. }
  1413. #endif
  1414. default:
  1415. break;
  1416. }
  1417. return NOTIFY_OK;
  1418. }
  1419. static struct notifier_block __cpuinitdata hrtimers_nb = {
  1420. .notifier_call = hrtimer_cpu_notify,
  1421. };
  1422. void __init hrtimers_init(void)
  1423. {
  1424. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1425. (void *)(long)smp_processor_id());
  1426. register_cpu_notifier(&hrtimers_nb);
  1427. #ifdef CONFIG_HIGH_RES_TIMERS
  1428. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  1429. #endif
  1430. }
  1431. /**
  1432. * schedule_hrtimeout_range - sleep until timeout
  1433. * @expires: timeout value (ktime_t)
  1434. * @delta: slack in expires timeout (ktime_t)
  1435. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1436. *
  1437. * Make the current task sleep until the given expiry time has
  1438. * elapsed. The routine will return immediately unless
  1439. * the current task state has been set (see set_current_state()).
  1440. *
  1441. * The @delta argument gives the kernel the freedom to schedule the
  1442. * actual wakeup to a time that is both power and performance friendly.
  1443. * The kernel give the normal best effort behavior for "@expires+@delta",
  1444. * but may decide to fire the timer earlier, but no earlier than @expires.
  1445. *
  1446. * You can set the task state as follows -
  1447. *
  1448. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1449. * pass before the routine returns.
  1450. *
  1451. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1452. * delivered to the current task.
  1453. *
  1454. * The current task state is guaranteed to be TASK_RUNNING when this
  1455. * routine returns.
  1456. *
  1457. * Returns 0 when the timer has expired otherwise -EINTR
  1458. */
  1459. int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
  1460. const enum hrtimer_mode mode)
  1461. {
  1462. struct hrtimer_sleeper t;
  1463. /*
  1464. * Optimize when a zero timeout value is given. It does not
  1465. * matter whether this is an absolute or a relative time.
  1466. */
  1467. if (expires && !expires->tv64) {
  1468. __set_current_state(TASK_RUNNING);
  1469. return 0;
  1470. }
  1471. /*
  1472. * A NULL parameter means "inifinte"
  1473. */
  1474. if (!expires) {
  1475. schedule();
  1476. __set_current_state(TASK_RUNNING);
  1477. return -EINTR;
  1478. }
  1479. hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
  1480. hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
  1481. hrtimer_init_sleeper(&t, current);
  1482. hrtimer_start_expires(&t.timer, mode);
  1483. if (!hrtimer_active(&t.timer))
  1484. t.task = NULL;
  1485. if (likely(t.task))
  1486. schedule();
  1487. hrtimer_cancel(&t.timer);
  1488. destroy_hrtimer_on_stack(&t.timer);
  1489. __set_current_state(TASK_RUNNING);
  1490. return !t.task ? 0 : -EINTR;
  1491. }
  1492. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
  1493. /**
  1494. * schedule_hrtimeout - sleep until timeout
  1495. * @expires: timeout value (ktime_t)
  1496. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1497. *
  1498. * Make the current task sleep until the given expiry time has
  1499. * elapsed. The routine will return immediately unless
  1500. * the current task state has been set (see set_current_state()).
  1501. *
  1502. * You can set the task state as follows -
  1503. *
  1504. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1505. * pass before the routine returns.
  1506. *
  1507. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1508. * delivered to the current task.
  1509. *
  1510. * The current task state is guaranteed to be TASK_RUNNING when this
  1511. * routine returns.
  1512. *
  1513. * Returns 0 when the timer has expired otherwise -EINTR
  1514. */
  1515. int __sched schedule_hrtimeout(ktime_t *expires,
  1516. const enum hrtimer_mode mode)
  1517. {
  1518. return schedule_hrtimeout_range(expires, 0, mode);
  1519. }
  1520. EXPORT_SYMBOL_GPL(schedule_hrtimeout);