hrtimer.c 46 KB

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