hrtimer.c 45 KB

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