hrtimer.c 43 KB

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