timer.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  1. /*
  2. * linux/kernel/timer.c
  3. *
  4. * Kernel internal timers, kernel timekeeping, basic process system calls
  5. *
  6. * Copyright (C) 1991, 1992 Linus Torvalds
  7. *
  8. * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
  9. *
  10. * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
  11. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  12. * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
  13. * serialize accesses to xtime/lost_ticks).
  14. * Copyright (C) 1998 Andrea Arcangeli
  15. * 1999-03-10 Improved NTP compatibility by Ulrich Windl
  16. * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love
  17. * 2000-10-05 Implemented scalable SMP per-CPU timer handling.
  18. * Copyright (C) 2000, 2001, 2002 Ingo Molnar
  19. * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
  20. */
  21. #include <linux/kernel_stat.h>
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/percpu.h>
  25. #include <linux/init.h>
  26. #include <linux/mm.h>
  27. #include <linux/swap.h>
  28. #include <linux/notifier.h>
  29. #include <linux/thread_info.h>
  30. #include <linux/time.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/posix-timers.h>
  33. #include <linux/cpu.h>
  34. #include <linux/syscalls.h>
  35. #include <linux/delay.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/unistd.h>
  38. #include <asm/div64.h>
  39. #include <asm/timex.h>
  40. #include <asm/io.h>
  41. #ifdef CONFIG_TIME_INTERPOLATION
  42. static void time_interpolator_update(long delta_nsec);
  43. #else
  44. #define time_interpolator_update(x)
  45. #endif
  46. u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
  47. EXPORT_SYMBOL(jiffies_64);
  48. /*
  49. * per-CPU timer vector definitions:
  50. */
  51. #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
  52. #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
  53. #define TVN_SIZE (1 << TVN_BITS)
  54. #define TVR_SIZE (1 << TVR_BITS)
  55. #define TVN_MASK (TVN_SIZE - 1)
  56. #define TVR_MASK (TVR_SIZE - 1)
  57. typedef struct tvec_s {
  58. struct list_head vec[TVN_SIZE];
  59. } tvec_t;
  60. typedef struct tvec_root_s {
  61. struct list_head vec[TVR_SIZE];
  62. } tvec_root_t;
  63. struct tvec_t_base_s {
  64. spinlock_t lock;
  65. struct timer_list *running_timer;
  66. unsigned long timer_jiffies;
  67. tvec_root_t tv1;
  68. tvec_t tv2;
  69. tvec_t tv3;
  70. tvec_t tv4;
  71. tvec_t tv5;
  72. } ____cacheline_aligned_in_smp;
  73. typedef struct tvec_t_base_s tvec_base_t;
  74. static DEFINE_PER_CPU(tvec_base_t *, tvec_bases);
  75. tvec_base_t boot_tvec_bases;
  76. EXPORT_SYMBOL(boot_tvec_bases);
  77. static inline void set_running_timer(tvec_base_t *base,
  78. struct timer_list *timer)
  79. {
  80. #ifdef CONFIG_SMP
  81. base->running_timer = timer;
  82. #endif
  83. }
  84. static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
  85. {
  86. unsigned long expires = timer->expires;
  87. unsigned long idx = expires - base->timer_jiffies;
  88. struct list_head *vec;
  89. if (idx < TVR_SIZE) {
  90. int i = expires & TVR_MASK;
  91. vec = base->tv1.vec + i;
  92. } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
  93. int i = (expires >> TVR_BITS) & TVN_MASK;
  94. vec = base->tv2.vec + i;
  95. } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
  96. int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
  97. vec = base->tv3.vec + i;
  98. } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
  99. int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
  100. vec = base->tv4.vec + i;
  101. } else if ((signed long) idx < 0) {
  102. /*
  103. * Can happen if you add a timer with expires == jiffies,
  104. * or you set a timer to go off in the past
  105. */
  106. vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
  107. } else {
  108. int i;
  109. /* If the timeout is larger than 0xffffffff on 64-bit
  110. * architectures then we use the maximum timeout:
  111. */
  112. if (idx > 0xffffffffUL) {
  113. idx = 0xffffffffUL;
  114. expires = idx + base->timer_jiffies;
  115. }
  116. i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
  117. vec = base->tv5.vec + i;
  118. }
  119. /*
  120. * Timers are FIFO:
  121. */
  122. list_add_tail(&timer->entry, vec);
  123. }
  124. /***
  125. * init_timer - initialize a timer.
  126. * @timer: the timer to be initialized
  127. *
  128. * init_timer() must be done to a timer prior calling *any* of the
  129. * other timer functions.
  130. */
  131. void fastcall init_timer(struct timer_list *timer)
  132. {
  133. timer->entry.next = NULL;
  134. timer->base = per_cpu(tvec_bases, raw_smp_processor_id());
  135. }
  136. EXPORT_SYMBOL(init_timer);
  137. static inline void detach_timer(struct timer_list *timer,
  138. int clear_pending)
  139. {
  140. struct list_head *entry = &timer->entry;
  141. __list_del(entry->prev, entry->next);
  142. if (clear_pending)
  143. entry->next = NULL;
  144. entry->prev = LIST_POISON2;
  145. }
  146. /*
  147. * We are using hashed locking: holding per_cpu(tvec_bases).lock
  148. * means that all timers which are tied to this base via timer->base are
  149. * locked, and the base itself is locked too.
  150. *
  151. * So __run_timers/migrate_timers can safely modify all timers which could
  152. * be found on ->tvX lists.
  153. *
  154. * When the timer's base is locked, and the timer removed from list, it is
  155. * possible to set timer->base = NULL and drop the lock: the timer remains
  156. * locked.
  157. */
  158. static tvec_base_t *lock_timer_base(struct timer_list *timer,
  159. unsigned long *flags)
  160. {
  161. tvec_base_t *base;
  162. for (;;) {
  163. base = timer->base;
  164. if (likely(base != NULL)) {
  165. spin_lock_irqsave(&base->lock, *flags);
  166. if (likely(base == timer->base))
  167. return base;
  168. /* The timer has migrated to another CPU */
  169. spin_unlock_irqrestore(&base->lock, *flags);
  170. }
  171. cpu_relax();
  172. }
  173. }
  174. int __mod_timer(struct timer_list *timer, unsigned long expires)
  175. {
  176. tvec_base_t *base, *new_base;
  177. unsigned long flags;
  178. int ret = 0;
  179. BUG_ON(!timer->function);
  180. base = lock_timer_base(timer, &flags);
  181. if (timer_pending(timer)) {
  182. detach_timer(timer, 0);
  183. ret = 1;
  184. }
  185. new_base = __get_cpu_var(tvec_bases);
  186. if (base != new_base) {
  187. /*
  188. * We are trying to schedule the timer on the local CPU.
  189. * However we can't change timer's base while it is running,
  190. * otherwise del_timer_sync() can't detect that the timer's
  191. * handler yet has not finished. This also guarantees that
  192. * the timer is serialized wrt itself.
  193. */
  194. if (unlikely(base->running_timer == timer)) {
  195. /* The timer remains on a former base */
  196. new_base = base;
  197. } else {
  198. /* See the comment in lock_timer_base() */
  199. timer->base = NULL;
  200. spin_unlock(&base->lock);
  201. spin_lock(&new_base->lock);
  202. timer->base = new_base;
  203. }
  204. }
  205. timer->expires = expires;
  206. internal_add_timer(new_base, timer);
  207. spin_unlock_irqrestore(&new_base->lock, flags);
  208. return ret;
  209. }
  210. EXPORT_SYMBOL(__mod_timer);
  211. /***
  212. * add_timer_on - start a timer on a particular CPU
  213. * @timer: the timer to be added
  214. * @cpu: the CPU to start it on
  215. *
  216. * This is not very scalable on SMP. Double adds are not possible.
  217. */
  218. void add_timer_on(struct timer_list *timer, int cpu)
  219. {
  220. tvec_base_t *base = per_cpu(tvec_bases, cpu);
  221. unsigned long flags;
  222. BUG_ON(timer_pending(timer) || !timer->function);
  223. spin_lock_irqsave(&base->lock, flags);
  224. timer->base = base;
  225. internal_add_timer(base, timer);
  226. spin_unlock_irqrestore(&base->lock, flags);
  227. }
  228. /***
  229. * mod_timer - modify a timer's timeout
  230. * @timer: the timer to be modified
  231. *
  232. * mod_timer is a more efficient way to update the expire field of an
  233. * active timer (if the timer is inactive it will be activated)
  234. *
  235. * mod_timer(timer, expires) is equivalent to:
  236. *
  237. * del_timer(timer); timer->expires = expires; add_timer(timer);
  238. *
  239. * Note that if there are multiple unserialized concurrent users of the
  240. * same timer, then mod_timer() is the only safe way to modify the timeout,
  241. * since add_timer() cannot modify an already running timer.
  242. *
  243. * The function returns whether it has modified a pending timer or not.
  244. * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
  245. * active timer returns 1.)
  246. */
  247. int mod_timer(struct timer_list *timer, unsigned long expires)
  248. {
  249. BUG_ON(!timer->function);
  250. /*
  251. * This is a common optimization triggered by the
  252. * networking code - if the timer is re-modified
  253. * to be the same thing then just return:
  254. */
  255. if (timer->expires == expires && timer_pending(timer))
  256. return 1;
  257. return __mod_timer(timer, expires);
  258. }
  259. EXPORT_SYMBOL(mod_timer);
  260. /***
  261. * del_timer - deactive a timer.
  262. * @timer: the timer to be deactivated
  263. *
  264. * del_timer() deactivates a timer - this works on both active and inactive
  265. * timers.
  266. *
  267. * The function returns whether it has deactivated a pending timer or not.
  268. * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
  269. * active timer returns 1.)
  270. */
  271. int del_timer(struct timer_list *timer)
  272. {
  273. tvec_base_t *base;
  274. unsigned long flags;
  275. int ret = 0;
  276. if (timer_pending(timer)) {
  277. base = lock_timer_base(timer, &flags);
  278. if (timer_pending(timer)) {
  279. detach_timer(timer, 1);
  280. ret = 1;
  281. }
  282. spin_unlock_irqrestore(&base->lock, flags);
  283. }
  284. return ret;
  285. }
  286. EXPORT_SYMBOL(del_timer);
  287. #ifdef CONFIG_SMP
  288. /*
  289. * This function tries to deactivate a timer. Upon successful (ret >= 0)
  290. * exit the timer is not queued and the handler is not running on any CPU.
  291. *
  292. * It must not be called from interrupt contexts.
  293. */
  294. int try_to_del_timer_sync(struct timer_list *timer)
  295. {
  296. tvec_base_t *base;
  297. unsigned long flags;
  298. int ret = -1;
  299. base = lock_timer_base(timer, &flags);
  300. if (base->running_timer == timer)
  301. goto out;
  302. ret = 0;
  303. if (timer_pending(timer)) {
  304. detach_timer(timer, 1);
  305. ret = 1;
  306. }
  307. out:
  308. spin_unlock_irqrestore(&base->lock, flags);
  309. return ret;
  310. }
  311. /***
  312. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  313. * @timer: the timer to be deactivated
  314. *
  315. * This function only differs from del_timer() on SMP: besides deactivating
  316. * the timer it also makes sure the handler has finished executing on other
  317. * CPUs.
  318. *
  319. * Synchronization rules: callers must prevent restarting of the timer,
  320. * otherwise this function is meaningless. It must not be called from
  321. * interrupt contexts. The caller must not hold locks which would prevent
  322. * completion of the timer's handler. The timer's handler must not call
  323. * add_timer_on(). Upon exit the timer is not queued and the handler is
  324. * not running on any CPU.
  325. *
  326. * The function returns whether it has deactivated a pending timer or not.
  327. */
  328. int del_timer_sync(struct timer_list *timer)
  329. {
  330. for (;;) {
  331. int ret = try_to_del_timer_sync(timer);
  332. if (ret >= 0)
  333. return ret;
  334. }
  335. }
  336. EXPORT_SYMBOL(del_timer_sync);
  337. #endif
  338. static int cascade(tvec_base_t *base, tvec_t *tv, int index)
  339. {
  340. /* cascade all the timers from tv up one level */
  341. struct list_head *head, *curr;
  342. head = tv->vec + index;
  343. curr = head->next;
  344. /*
  345. * We are removing _all_ timers from the list, so we don't have to
  346. * detach them individually, just clear the list afterwards.
  347. */
  348. while (curr != head) {
  349. struct timer_list *tmp;
  350. tmp = list_entry(curr, struct timer_list, entry);
  351. BUG_ON(tmp->base != base);
  352. curr = curr->next;
  353. internal_add_timer(base, tmp);
  354. }
  355. INIT_LIST_HEAD(head);
  356. return index;
  357. }
  358. /***
  359. * __run_timers - run all expired timers (if any) on this CPU.
  360. * @base: the timer vector to be processed.
  361. *
  362. * This function cascades all vectors and executes all expired timer
  363. * vectors.
  364. */
  365. #define INDEX(N) (base->timer_jiffies >> (TVR_BITS + N * TVN_BITS)) & TVN_MASK
  366. static inline void __run_timers(tvec_base_t *base)
  367. {
  368. struct timer_list *timer;
  369. spin_lock_irq(&base->lock);
  370. while (time_after_eq(jiffies, base->timer_jiffies)) {
  371. struct list_head work_list = LIST_HEAD_INIT(work_list);
  372. struct list_head *head = &work_list;
  373. int index = base->timer_jiffies & TVR_MASK;
  374. /*
  375. * Cascade timers:
  376. */
  377. if (!index &&
  378. (!cascade(base, &base->tv2, INDEX(0))) &&
  379. (!cascade(base, &base->tv3, INDEX(1))) &&
  380. !cascade(base, &base->tv4, INDEX(2)))
  381. cascade(base, &base->tv5, INDEX(3));
  382. ++base->timer_jiffies;
  383. list_splice_init(base->tv1.vec + index, &work_list);
  384. while (!list_empty(head)) {
  385. void (*fn)(unsigned long);
  386. unsigned long data;
  387. timer = list_entry(head->next,struct timer_list,entry);
  388. fn = timer->function;
  389. data = timer->data;
  390. set_running_timer(base, timer);
  391. detach_timer(timer, 1);
  392. spin_unlock_irq(&base->lock);
  393. {
  394. int preempt_count = preempt_count();
  395. fn(data);
  396. if (preempt_count != preempt_count()) {
  397. printk(KERN_WARNING "huh, entered %p "
  398. "with preempt_count %08x, exited"
  399. " with %08x?\n",
  400. fn, preempt_count,
  401. preempt_count());
  402. BUG();
  403. }
  404. }
  405. spin_lock_irq(&base->lock);
  406. }
  407. }
  408. set_running_timer(base, NULL);
  409. spin_unlock_irq(&base->lock);
  410. }
  411. #ifdef CONFIG_NO_IDLE_HZ
  412. /*
  413. * Find out when the next timer event is due to happen. This
  414. * is used on S/390 to stop all activity when a cpus is idle.
  415. * This functions needs to be called disabled.
  416. */
  417. unsigned long next_timer_interrupt(void)
  418. {
  419. tvec_base_t *base;
  420. struct list_head *list;
  421. struct timer_list *nte;
  422. unsigned long expires;
  423. unsigned long hr_expires = MAX_JIFFY_OFFSET;
  424. ktime_t hr_delta;
  425. tvec_t *varray[4];
  426. int i, j;
  427. hr_delta = hrtimer_get_next_event();
  428. if (hr_delta.tv64 != KTIME_MAX) {
  429. struct timespec tsdelta;
  430. tsdelta = ktime_to_timespec(hr_delta);
  431. hr_expires = timespec_to_jiffies(&tsdelta);
  432. if (hr_expires < 3)
  433. return hr_expires + jiffies;
  434. }
  435. hr_expires += jiffies;
  436. base = __get_cpu_var(tvec_bases);
  437. spin_lock(&base->lock);
  438. expires = base->timer_jiffies + (LONG_MAX >> 1);
  439. list = NULL;
  440. /* Look for timer events in tv1. */
  441. j = base->timer_jiffies & TVR_MASK;
  442. do {
  443. list_for_each_entry(nte, base->tv1.vec + j, entry) {
  444. expires = nte->expires;
  445. if (j < (base->timer_jiffies & TVR_MASK))
  446. list = base->tv2.vec + (INDEX(0));
  447. goto found;
  448. }
  449. j = (j + 1) & TVR_MASK;
  450. } while (j != (base->timer_jiffies & TVR_MASK));
  451. /* Check tv2-tv5. */
  452. varray[0] = &base->tv2;
  453. varray[1] = &base->tv3;
  454. varray[2] = &base->tv4;
  455. varray[3] = &base->tv5;
  456. for (i = 0; i < 4; i++) {
  457. j = INDEX(i);
  458. do {
  459. if (list_empty(varray[i]->vec + j)) {
  460. j = (j + 1) & TVN_MASK;
  461. continue;
  462. }
  463. list_for_each_entry(nte, varray[i]->vec + j, entry)
  464. if (time_before(nte->expires, expires))
  465. expires = nte->expires;
  466. if (j < (INDEX(i)) && i < 3)
  467. list = varray[i + 1]->vec + (INDEX(i + 1));
  468. goto found;
  469. } while (j != (INDEX(i)));
  470. }
  471. found:
  472. if (list) {
  473. /*
  474. * The search wrapped. We need to look at the next list
  475. * from next tv element that would cascade into tv element
  476. * where we found the timer element.
  477. */
  478. list_for_each_entry(nte, list, entry) {
  479. if (time_before(nte->expires, expires))
  480. expires = nte->expires;
  481. }
  482. }
  483. spin_unlock(&base->lock);
  484. if (time_before(hr_expires, expires))
  485. return hr_expires;
  486. return expires;
  487. }
  488. #endif
  489. /******************************************************************/
  490. /*
  491. * Timekeeping variables
  492. */
  493. unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
  494. unsigned long tick_nsec = TICK_NSEC; /* ACTHZ period (nsec) */
  495. /*
  496. * The current time
  497. * wall_to_monotonic is what we need to add to xtime (or xtime corrected
  498. * for sub jiffie times) to get to monotonic time. Monotonic is pegged
  499. * at zero at system boot time, so wall_to_monotonic will be negative,
  500. * however, we will ALWAYS keep the tv_nsec part positive so we can use
  501. * the usual normalization.
  502. */
  503. struct timespec xtime __attribute__ ((aligned (16)));
  504. struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
  505. EXPORT_SYMBOL(xtime);
  506. /* Don't completely fail for HZ > 500. */
  507. int tickadj = 500/HZ ? : 1; /* microsecs */
  508. /*
  509. * phase-lock loop variables
  510. */
  511. /* TIME_ERROR prevents overwriting the CMOS clock */
  512. int time_state = TIME_OK; /* clock synchronization status */
  513. int time_status = STA_UNSYNC; /* clock status bits */
  514. long time_offset; /* time adjustment (us) */
  515. long time_constant = 2; /* pll time constant */
  516. long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
  517. long time_precision = 1; /* clock precision (us) */
  518. long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
  519. long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
  520. static long time_phase; /* phase offset (scaled us) */
  521. long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
  522. /* frequency offset (scaled ppm)*/
  523. static long time_adj; /* tick adjust (scaled 1 / HZ) */
  524. long time_reftime; /* time at last adjustment (s) */
  525. long time_adjust;
  526. long time_next_adjust;
  527. /*
  528. * this routine handles the overflow of the microsecond field
  529. *
  530. * The tricky bits of code to handle the accurate clock support
  531. * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
  532. * They were originally developed for SUN and DEC kernels.
  533. * All the kudos should go to Dave for this stuff.
  534. *
  535. */
  536. static void second_overflow(void)
  537. {
  538. long ltemp;
  539. /* Bump the maxerror field */
  540. time_maxerror += time_tolerance >> SHIFT_USEC;
  541. if (time_maxerror > NTP_PHASE_LIMIT) {
  542. time_maxerror = NTP_PHASE_LIMIT;
  543. time_status |= STA_UNSYNC;
  544. }
  545. /*
  546. * Leap second processing. If in leap-insert state at the end of the
  547. * day, the system clock is set back one second; if in leap-delete
  548. * state, the system clock is set ahead one second. The microtime()
  549. * routine or external clock driver will insure that reported time is
  550. * always monotonic. The ugly divides should be replaced.
  551. */
  552. switch (time_state) {
  553. case TIME_OK:
  554. if (time_status & STA_INS)
  555. time_state = TIME_INS;
  556. else if (time_status & STA_DEL)
  557. time_state = TIME_DEL;
  558. break;
  559. case TIME_INS:
  560. if (xtime.tv_sec % 86400 == 0) {
  561. xtime.tv_sec--;
  562. wall_to_monotonic.tv_sec++;
  563. /*
  564. * The timer interpolator will make time change
  565. * gradually instead of an immediate jump by one second
  566. */
  567. time_interpolator_update(-NSEC_PER_SEC);
  568. time_state = TIME_OOP;
  569. clock_was_set();
  570. printk(KERN_NOTICE "Clock: inserting leap second "
  571. "23:59:60 UTC\n");
  572. }
  573. break;
  574. case TIME_DEL:
  575. if ((xtime.tv_sec + 1) % 86400 == 0) {
  576. xtime.tv_sec++;
  577. wall_to_monotonic.tv_sec--;
  578. /*
  579. * Use of time interpolator for a gradual change of
  580. * time
  581. */
  582. time_interpolator_update(NSEC_PER_SEC);
  583. time_state = TIME_WAIT;
  584. clock_was_set();
  585. printk(KERN_NOTICE "Clock: deleting leap second "
  586. "23:59:59 UTC\n");
  587. }
  588. break;
  589. case TIME_OOP:
  590. time_state = TIME_WAIT;
  591. break;
  592. case TIME_WAIT:
  593. if (!(time_status & (STA_INS | STA_DEL)))
  594. time_state = TIME_OK;
  595. }
  596. /*
  597. * Compute the phase adjustment for the next second. In PLL mode, the
  598. * offset is reduced by a fixed factor times the time constant. In FLL
  599. * mode the offset is used directly. In either mode, the maximum phase
  600. * adjustment for each second is clamped so as to spread the adjustment
  601. * over not more than the number of seconds between updates.
  602. */
  603. ltemp = time_offset;
  604. if (!(time_status & STA_FLL))
  605. ltemp = shift_right(ltemp, SHIFT_KG + time_constant);
  606. ltemp = min(ltemp, (MAXPHASE / MINSEC) << SHIFT_UPDATE);
  607. ltemp = max(ltemp, -(MAXPHASE / MINSEC) << SHIFT_UPDATE);
  608. time_offset -= ltemp;
  609. time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
  610. /*
  611. * Compute the frequency estimate and additional phase adjustment due
  612. * to frequency error for the next second.
  613. */
  614. ltemp = time_freq;
  615. time_adj += shift_right(ltemp,(SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE));
  616. #if HZ == 100
  617. /*
  618. * Compensate for (HZ==100) != (1 << SHIFT_HZ). Add 25% and 3.125% to
  619. * get 128.125; => only 0.125% error (p. 14)
  620. */
  621. time_adj += shift_right(time_adj, 2) + shift_right(time_adj, 5);
  622. #endif
  623. #if HZ == 250
  624. /*
  625. * Compensate for (HZ==250) != (1 << SHIFT_HZ). Add 1.5625% and
  626. * 0.78125% to get 255.85938; => only 0.05% error (p. 14)
  627. */
  628. time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
  629. #endif
  630. #if HZ == 1000
  631. /*
  632. * Compensate for (HZ==1000) != (1 << SHIFT_HZ). Add 1.5625% and
  633. * 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
  634. */
  635. time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
  636. #endif
  637. }
  638. /*
  639. * Returns how many microseconds we need to add to xtime this tick
  640. * in doing an adjustment requested with adjtime.
  641. */
  642. static long adjtime_adjustment(void)
  643. {
  644. long time_adjust_step;
  645. time_adjust_step = time_adjust;
  646. if (time_adjust_step) {
  647. /*
  648. * We are doing an adjtime thing. Prepare time_adjust_step to
  649. * be within bounds. Note that a positive time_adjust means we
  650. * want the clock to run faster.
  651. *
  652. * Limit the amount of the step to be in the range
  653. * -tickadj .. +tickadj
  654. */
  655. time_adjust_step = min(time_adjust_step, (long)tickadj);
  656. time_adjust_step = max(time_adjust_step, (long)-tickadj);
  657. }
  658. return time_adjust_step;
  659. }
  660. /* in the NTP reference this is called "hardclock()" */
  661. static void update_wall_time_one_tick(void)
  662. {
  663. long time_adjust_step, delta_nsec;
  664. time_adjust_step = adjtime_adjustment();
  665. if (time_adjust_step)
  666. /* Reduce by this step the amount of time left */
  667. time_adjust -= time_adjust_step;
  668. delta_nsec = tick_nsec + time_adjust_step * 1000;
  669. /*
  670. * Advance the phase, once it gets to one microsecond, then
  671. * advance the tick more.
  672. */
  673. time_phase += time_adj;
  674. if ((time_phase >= FINENSEC) || (time_phase <= -FINENSEC)) {
  675. long ltemp = shift_right(time_phase, (SHIFT_SCALE - 10));
  676. time_phase -= ltemp << (SHIFT_SCALE - 10);
  677. delta_nsec += ltemp;
  678. }
  679. xtime.tv_nsec += delta_nsec;
  680. time_interpolator_update(delta_nsec);
  681. /* Changes by adjtime() do not take effect till next tick. */
  682. if (time_next_adjust != 0) {
  683. time_adjust = time_next_adjust;
  684. time_next_adjust = 0;
  685. }
  686. }
  687. /*
  688. * Return how long ticks are at the moment, that is, how much time
  689. * update_wall_time_one_tick will add to xtime next time we call it
  690. * (assuming no calls to do_adjtimex in the meantime).
  691. * The return value is in fixed-point nanoseconds with SHIFT_SCALE-10
  692. * bits to the right of the binary point.
  693. * This function has no side-effects.
  694. */
  695. u64 current_tick_length(void)
  696. {
  697. long delta_nsec;
  698. delta_nsec = tick_nsec + adjtime_adjustment() * 1000;
  699. return ((u64) delta_nsec << (SHIFT_SCALE - 10)) + time_adj;
  700. }
  701. /*
  702. * Using a loop looks inefficient, but "ticks" is
  703. * usually just one (we shouldn't be losing ticks,
  704. * we're doing this this way mainly for interrupt
  705. * latency reasons, not because we think we'll
  706. * have lots of lost timer ticks
  707. */
  708. static void update_wall_time(unsigned long ticks)
  709. {
  710. do {
  711. ticks--;
  712. update_wall_time_one_tick();
  713. if (xtime.tv_nsec >= 1000000000) {
  714. xtime.tv_nsec -= 1000000000;
  715. xtime.tv_sec++;
  716. second_overflow();
  717. }
  718. } while (ticks);
  719. }
  720. /*
  721. * Called from the timer interrupt handler to charge one tick to the current
  722. * process. user_tick is 1 if the tick is user time, 0 for system.
  723. */
  724. void update_process_times(int user_tick)
  725. {
  726. struct task_struct *p = current;
  727. int cpu = smp_processor_id();
  728. /* Note: this timer irq context must be accounted for as well. */
  729. if (user_tick)
  730. account_user_time(p, jiffies_to_cputime(1));
  731. else
  732. account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
  733. run_local_timers();
  734. if (rcu_pending(cpu))
  735. rcu_check_callbacks(cpu, user_tick);
  736. scheduler_tick();
  737. run_posix_cpu_timers(p);
  738. }
  739. /*
  740. * Nr of active tasks - counted in fixed-point numbers
  741. */
  742. static unsigned long count_active_tasks(void)
  743. {
  744. return (nr_running() + nr_uninterruptible()) * FIXED_1;
  745. }
  746. /*
  747. * Hmm.. Changed this, as the GNU make sources (load.c) seems to
  748. * imply that avenrun[] is the standard name for this kind of thing.
  749. * Nothing else seems to be standardized: the fractional size etc
  750. * all seem to differ on different machines.
  751. *
  752. * Requires xtime_lock to access.
  753. */
  754. unsigned long avenrun[3];
  755. EXPORT_SYMBOL(avenrun);
  756. /*
  757. * calc_load - given tick count, update the avenrun load estimates.
  758. * This is called while holding a write_lock on xtime_lock.
  759. */
  760. static inline void calc_load(unsigned long ticks)
  761. {
  762. unsigned long active_tasks; /* fixed-point */
  763. static int count = LOAD_FREQ;
  764. count -= ticks;
  765. if (count < 0) {
  766. count += LOAD_FREQ;
  767. active_tasks = count_active_tasks();
  768. CALC_LOAD(avenrun[0], EXP_1, active_tasks);
  769. CALC_LOAD(avenrun[1], EXP_5, active_tasks);
  770. CALC_LOAD(avenrun[2], EXP_15, active_tasks);
  771. }
  772. }
  773. /* jiffies at the most recent update of wall time */
  774. unsigned long wall_jiffies = INITIAL_JIFFIES;
  775. /*
  776. * This read-write spinlock protects us from races in SMP while
  777. * playing with xtime and avenrun.
  778. */
  779. #ifndef ARCH_HAVE_XTIME_LOCK
  780. seqlock_t xtime_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
  781. EXPORT_SYMBOL(xtime_lock);
  782. #endif
  783. /*
  784. * This function runs timers and the timer-tq in bottom half context.
  785. */
  786. static void run_timer_softirq(struct softirq_action *h)
  787. {
  788. tvec_base_t *base = __get_cpu_var(tvec_bases);
  789. hrtimer_run_queues();
  790. if (time_after_eq(jiffies, base->timer_jiffies))
  791. __run_timers(base);
  792. }
  793. /*
  794. * Called by the local, per-CPU timer interrupt on SMP.
  795. */
  796. void run_local_timers(void)
  797. {
  798. raise_softirq(TIMER_SOFTIRQ);
  799. softlockup_tick();
  800. }
  801. /*
  802. * Called by the timer interrupt. xtime_lock must already be taken
  803. * by the timer IRQ!
  804. */
  805. static inline void update_times(void)
  806. {
  807. unsigned long ticks;
  808. ticks = jiffies - wall_jiffies;
  809. if (ticks) {
  810. wall_jiffies += ticks;
  811. update_wall_time(ticks);
  812. }
  813. calc_load(ticks);
  814. }
  815. /*
  816. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  817. * without sampling the sequence number in xtime_lock.
  818. * jiffies is defined in the linker script...
  819. */
  820. void do_timer(struct pt_regs *regs)
  821. {
  822. jiffies_64++;
  823. /* prevent loading jiffies before storing new jiffies_64 value. */
  824. barrier();
  825. update_times();
  826. }
  827. #ifdef __ARCH_WANT_SYS_ALARM
  828. /*
  829. * For backwards compatibility? This can be done in libc so Alpha
  830. * and all newer ports shouldn't need it.
  831. */
  832. asmlinkage unsigned long sys_alarm(unsigned int seconds)
  833. {
  834. return alarm_setitimer(seconds);
  835. }
  836. #endif
  837. #ifndef __alpha__
  838. /*
  839. * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
  840. * should be moved into arch/i386 instead?
  841. */
  842. /**
  843. * sys_getpid - return the thread group id of the current process
  844. *
  845. * Note, despite the name, this returns the tgid not the pid. The tgid and
  846. * the pid are identical unless CLONE_THREAD was specified on clone() in
  847. * which case the tgid is the same in all threads of the same group.
  848. *
  849. * This is SMP safe as current->tgid does not change.
  850. */
  851. asmlinkage long sys_getpid(void)
  852. {
  853. return current->tgid;
  854. }
  855. /*
  856. * Accessing ->group_leader->real_parent is not SMP-safe, it could
  857. * change from under us. However, rather than getting any lock
  858. * we can use an optimistic algorithm: get the parent
  859. * pid, and go back and check that the parent is still
  860. * the same. If it has changed (which is extremely unlikely
  861. * indeed), we just try again..
  862. *
  863. * NOTE! This depends on the fact that even if we _do_
  864. * get an old value of "parent", we can happily dereference
  865. * the pointer (it was and remains a dereferencable kernel pointer
  866. * no matter what): we just can't necessarily trust the result
  867. * until we know that the parent pointer is valid.
  868. *
  869. * NOTE2: ->group_leader never changes from under us.
  870. */
  871. asmlinkage long sys_getppid(void)
  872. {
  873. int pid;
  874. struct task_struct *me = current;
  875. struct task_struct *parent;
  876. parent = me->group_leader->real_parent;
  877. for (;;) {
  878. pid = parent->tgid;
  879. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  880. {
  881. struct task_struct *old = parent;
  882. /*
  883. * Make sure we read the pid before re-reading the
  884. * parent pointer:
  885. */
  886. smp_rmb();
  887. parent = me->group_leader->real_parent;
  888. if (old != parent)
  889. continue;
  890. }
  891. #endif
  892. break;
  893. }
  894. return pid;
  895. }
  896. asmlinkage long sys_getuid(void)
  897. {
  898. /* Only we change this so SMP safe */
  899. return current->uid;
  900. }
  901. asmlinkage long sys_geteuid(void)
  902. {
  903. /* Only we change this so SMP safe */
  904. return current->euid;
  905. }
  906. asmlinkage long sys_getgid(void)
  907. {
  908. /* Only we change this so SMP safe */
  909. return current->gid;
  910. }
  911. asmlinkage long sys_getegid(void)
  912. {
  913. /* Only we change this so SMP safe */
  914. return current->egid;
  915. }
  916. #endif
  917. static void process_timeout(unsigned long __data)
  918. {
  919. wake_up_process((task_t *)__data);
  920. }
  921. /**
  922. * schedule_timeout - sleep until timeout
  923. * @timeout: timeout value in jiffies
  924. *
  925. * Make the current task sleep until @timeout jiffies have
  926. * elapsed. The routine will return immediately unless
  927. * the current task state has been set (see set_current_state()).
  928. *
  929. * You can set the task state as follows -
  930. *
  931. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  932. * pass before the routine returns. The routine will return 0
  933. *
  934. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  935. * delivered to the current task. In this case the remaining time
  936. * in jiffies will be returned, or 0 if the timer expired in time
  937. *
  938. * The current task state is guaranteed to be TASK_RUNNING when this
  939. * routine returns.
  940. *
  941. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  942. * the CPU away without a bound on the timeout. In this case the return
  943. * value will be %MAX_SCHEDULE_TIMEOUT.
  944. *
  945. * In all cases the return value is guaranteed to be non-negative.
  946. */
  947. fastcall signed long __sched schedule_timeout(signed long timeout)
  948. {
  949. struct timer_list timer;
  950. unsigned long expire;
  951. switch (timeout)
  952. {
  953. case MAX_SCHEDULE_TIMEOUT:
  954. /*
  955. * These two special cases are useful to be comfortable
  956. * in the caller. Nothing more. We could take
  957. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  958. * but I' d like to return a valid offset (>=0) to allow
  959. * the caller to do everything it want with the retval.
  960. */
  961. schedule();
  962. goto out;
  963. default:
  964. /*
  965. * Another bit of PARANOID. Note that the retval will be
  966. * 0 since no piece of kernel is supposed to do a check
  967. * for a negative retval of schedule_timeout() (since it
  968. * should never happens anyway). You just have the printk()
  969. * that will tell you if something is gone wrong and where.
  970. */
  971. if (timeout < 0)
  972. {
  973. printk(KERN_ERR "schedule_timeout: wrong timeout "
  974. "value %lx from %p\n", timeout,
  975. __builtin_return_address(0));
  976. current->state = TASK_RUNNING;
  977. goto out;
  978. }
  979. }
  980. expire = timeout + jiffies;
  981. setup_timer(&timer, process_timeout, (unsigned long)current);
  982. __mod_timer(&timer, expire);
  983. schedule();
  984. del_singleshot_timer_sync(&timer);
  985. timeout = expire - jiffies;
  986. out:
  987. return timeout < 0 ? 0 : timeout;
  988. }
  989. EXPORT_SYMBOL(schedule_timeout);
  990. /*
  991. * We can use __set_current_state() here because schedule_timeout() calls
  992. * schedule() unconditionally.
  993. */
  994. signed long __sched schedule_timeout_interruptible(signed long timeout)
  995. {
  996. __set_current_state(TASK_INTERRUPTIBLE);
  997. return schedule_timeout(timeout);
  998. }
  999. EXPORT_SYMBOL(schedule_timeout_interruptible);
  1000. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  1001. {
  1002. __set_current_state(TASK_UNINTERRUPTIBLE);
  1003. return schedule_timeout(timeout);
  1004. }
  1005. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  1006. /* Thread ID - the internal kernel "pid" */
  1007. asmlinkage long sys_gettid(void)
  1008. {
  1009. return current->pid;
  1010. }
  1011. /*
  1012. * sys_sysinfo - fill in sysinfo struct
  1013. */
  1014. asmlinkage long sys_sysinfo(struct sysinfo __user *info)
  1015. {
  1016. struct sysinfo val;
  1017. unsigned long mem_total, sav_total;
  1018. unsigned int mem_unit, bitcount;
  1019. unsigned long seq;
  1020. memset((char *)&val, 0, sizeof(struct sysinfo));
  1021. do {
  1022. struct timespec tp;
  1023. seq = read_seqbegin(&xtime_lock);
  1024. /*
  1025. * This is annoying. The below is the same thing
  1026. * posix_get_clock_monotonic() does, but it wants to
  1027. * take the lock which we want to cover the loads stuff
  1028. * too.
  1029. */
  1030. getnstimeofday(&tp);
  1031. tp.tv_sec += wall_to_monotonic.tv_sec;
  1032. tp.tv_nsec += wall_to_monotonic.tv_nsec;
  1033. if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
  1034. tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
  1035. tp.tv_sec++;
  1036. }
  1037. val.uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  1038. val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
  1039. val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
  1040. val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
  1041. val.procs = nr_threads;
  1042. } while (read_seqretry(&xtime_lock, seq));
  1043. si_meminfo(&val);
  1044. si_swapinfo(&val);
  1045. /*
  1046. * If the sum of all the available memory (i.e. ram + swap)
  1047. * is less than can be stored in a 32 bit unsigned long then
  1048. * we can be binary compatible with 2.2.x kernels. If not,
  1049. * well, in that case 2.2.x was broken anyways...
  1050. *
  1051. * -Erik Andersen <andersee@debian.org>
  1052. */
  1053. mem_total = val.totalram + val.totalswap;
  1054. if (mem_total < val.totalram || mem_total < val.totalswap)
  1055. goto out;
  1056. bitcount = 0;
  1057. mem_unit = val.mem_unit;
  1058. while (mem_unit > 1) {
  1059. bitcount++;
  1060. mem_unit >>= 1;
  1061. sav_total = mem_total;
  1062. mem_total <<= 1;
  1063. if (mem_total < sav_total)
  1064. goto out;
  1065. }
  1066. /*
  1067. * If mem_total did not overflow, multiply all memory values by
  1068. * val.mem_unit and set it to 1. This leaves things compatible
  1069. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  1070. * kernels...
  1071. */
  1072. val.mem_unit = 1;
  1073. val.totalram <<= bitcount;
  1074. val.freeram <<= bitcount;
  1075. val.sharedram <<= bitcount;
  1076. val.bufferram <<= bitcount;
  1077. val.totalswap <<= bitcount;
  1078. val.freeswap <<= bitcount;
  1079. val.totalhigh <<= bitcount;
  1080. val.freehigh <<= bitcount;
  1081. out:
  1082. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  1083. return -EFAULT;
  1084. return 0;
  1085. }
  1086. static int __devinit init_timers_cpu(int cpu)
  1087. {
  1088. int j;
  1089. tvec_base_t *base;
  1090. base = per_cpu(tvec_bases, cpu);
  1091. if (!base) {
  1092. static char boot_done;
  1093. /*
  1094. * Cannot do allocation in init_timers as that runs before the
  1095. * allocator initializes (and would waste memory if there are
  1096. * more possible CPUs than will ever be installed/brought up).
  1097. */
  1098. if (boot_done) {
  1099. base = kmalloc_node(sizeof(*base), GFP_KERNEL,
  1100. cpu_to_node(cpu));
  1101. if (!base)
  1102. return -ENOMEM;
  1103. memset(base, 0, sizeof(*base));
  1104. } else {
  1105. base = &boot_tvec_bases;
  1106. boot_done = 1;
  1107. }
  1108. per_cpu(tvec_bases, cpu) = base;
  1109. }
  1110. spin_lock_init(&base->lock);
  1111. for (j = 0; j < TVN_SIZE; j++) {
  1112. INIT_LIST_HEAD(base->tv5.vec + j);
  1113. INIT_LIST_HEAD(base->tv4.vec + j);
  1114. INIT_LIST_HEAD(base->tv3.vec + j);
  1115. INIT_LIST_HEAD(base->tv2.vec + j);
  1116. }
  1117. for (j = 0; j < TVR_SIZE; j++)
  1118. INIT_LIST_HEAD(base->tv1.vec + j);
  1119. base->timer_jiffies = jiffies;
  1120. return 0;
  1121. }
  1122. #ifdef CONFIG_HOTPLUG_CPU
  1123. static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
  1124. {
  1125. struct timer_list *timer;
  1126. while (!list_empty(head)) {
  1127. timer = list_entry(head->next, struct timer_list, entry);
  1128. detach_timer(timer, 0);
  1129. timer->base = new_base;
  1130. internal_add_timer(new_base, timer);
  1131. }
  1132. }
  1133. static void __devinit migrate_timers(int cpu)
  1134. {
  1135. tvec_base_t *old_base;
  1136. tvec_base_t *new_base;
  1137. int i;
  1138. BUG_ON(cpu_online(cpu));
  1139. old_base = per_cpu(tvec_bases, cpu);
  1140. new_base = get_cpu_var(tvec_bases);
  1141. local_irq_disable();
  1142. spin_lock(&new_base->lock);
  1143. spin_lock(&old_base->lock);
  1144. BUG_ON(old_base->running_timer);
  1145. for (i = 0; i < TVR_SIZE; i++)
  1146. migrate_timer_list(new_base, old_base->tv1.vec + i);
  1147. for (i = 0; i < TVN_SIZE; i++) {
  1148. migrate_timer_list(new_base, old_base->tv2.vec + i);
  1149. migrate_timer_list(new_base, old_base->tv3.vec + i);
  1150. migrate_timer_list(new_base, old_base->tv4.vec + i);
  1151. migrate_timer_list(new_base, old_base->tv5.vec + i);
  1152. }
  1153. spin_unlock(&old_base->lock);
  1154. spin_unlock(&new_base->lock);
  1155. local_irq_enable();
  1156. put_cpu_var(tvec_bases);
  1157. }
  1158. #endif /* CONFIG_HOTPLUG_CPU */
  1159. static int __devinit timer_cpu_notify(struct notifier_block *self,
  1160. unsigned long action, void *hcpu)
  1161. {
  1162. long cpu = (long)hcpu;
  1163. switch(action) {
  1164. case CPU_UP_PREPARE:
  1165. if (init_timers_cpu(cpu) < 0)
  1166. return NOTIFY_BAD;
  1167. break;
  1168. #ifdef CONFIG_HOTPLUG_CPU
  1169. case CPU_DEAD:
  1170. migrate_timers(cpu);
  1171. break;
  1172. #endif
  1173. default:
  1174. break;
  1175. }
  1176. return NOTIFY_OK;
  1177. }
  1178. static struct notifier_block __devinitdata timers_nb = {
  1179. .notifier_call = timer_cpu_notify,
  1180. };
  1181. void __init init_timers(void)
  1182. {
  1183. timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
  1184. (void *)(long)smp_processor_id());
  1185. register_cpu_notifier(&timers_nb);
  1186. open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
  1187. }
  1188. #ifdef CONFIG_TIME_INTERPOLATION
  1189. struct time_interpolator *time_interpolator __read_mostly;
  1190. static struct time_interpolator *time_interpolator_list __read_mostly;
  1191. static DEFINE_SPINLOCK(time_interpolator_lock);
  1192. static inline u64 time_interpolator_get_cycles(unsigned int src)
  1193. {
  1194. unsigned long (*x)(void);
  1195. switch (src)
  1196. {
  1197. case TIME_SOURCE_FUNCTION:
  1198. x = time_interpolator->addr;
  1199. return x();
  1200. case TIME_SOURCE_MMIO64 :
  1201. return readq_relaxed((void __iomem *)time_interpolator->addr);
  1202. case TIME_SOURCE_MMIO32 :
  1203. return readl_relaxed((void __iomem *)time_interpolator->addr);
  1204. default: return get_cycles();
  1205. }
  1206. }
  1207. static inline u64 time_interpolator_get_counter(int writelock)
  1208. {
  1209. unsigned int src = time_interpolator->source;
  1210. if (time_interpolator->jitter)
  1211. {
  1212. u64 lcycle;
  1213. u64 now;
  1214. do {
  1215. lcycle = time_interpolator->last_cycle;
  1216. now = time_interpolator_get_cycles(src);
  1217. if (lcycle && time_after(lcycle, now))
  1218. return lcycle;
  1219. /* When holding the xtime write lock, there's no need
  1220. * to add the overhead of the cmpxchg. Readers are
  1221. * force to retry until the write lock is released.
  1222. */
  1223. if (writelock) {
  1224. time_interpolator->last_cycle = now;
  1225. return now;
  1226. }
  1227. /* Keep track of the last timer value returned. The use of cmpxchg here
  1228. * will cause contention in an SMP environment.
  1229. */
  1230. } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
  1231. return now;
  1232. }
  1233. else
  1234. return time_interpolator_get_cycles(src);
  1235. }
  1236. void time_interpolator_reset(void)
  1237. {
  1238. time_interpolator->offset = 0;
  1239. time_interpolator->last_counter = time_interpolator_get_counter(1);
  1240. }
  1241. #define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
  1242. unsigned long time_interpolator_get_offset(void)
  1243. {
  1244. /* If we do not have a time interpolator set up then just return zero */
  1245. if (!time_interpolator)
  1246. return 0;
  1247. return time_interpolator->offset +
  1248. GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
  1249. }
  1250. #define INTERPOLATOR_ADJUST 65536
  1251. #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
  1252. static void time_interpolator_update(long delta_nsec)
  1253. {
  1254. u64 counter;
  1255. unsigned long offset;
  1256. /* If there is no time interpolator set up then do nothing */
  1257. if (!time_interpolator)
  1258. return;
  1259. /*
  1260. * The interpolator compensates for late ticks by accumulating the late
  1261. * time in time_interpolator->offset. A tick earlier than expected will
  1262. * lead to a reset of the offset and a corresponding jump of the clock
  1263. * forward. Again this only works if the interpolator clock is running
  1264. * slightly slower than the regular clock and the tuning logic insures
  1265. * that.
  1266. */
  1267. counter = time_interpolator_get_counter(1);
  1268. offset = time_interpolator->offset +
  1269. GET_TI_NSECS(counter, time_interpolator);
  1270. if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
  1271. time_interpolator->offset = offset - delta_nsec;
  1272. else {
  1273. time_interpolator->skips++;
  1274. time_interpolator->ns_skipped += delta_nsec - offset;
  1275. time_interpolator->offset = 0;
  1276. }
  1277. time_interpolator->last_counter = counter;
  1278. /* Tuning logic for time interpolator invoked every minute or so.
  1279. * Decrease interpolator clock speed if no skips occurred and an offset is carried.
  1280. * Increase interpolator clock speed if we skip too much time.
  1281. */
  1282. if (jiffies % INTERPOLATOR_ADJUST == 0)
  1283. {
  1284. if (time_interpolator->skips == 0 && time_interpolator->offset > TICK_NSEC)
  1285. time_interpolator->nsec_per_cyc--;
  1286. if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
  1287. time_interpolator->nsec_per_cyc++;
  1288. time_interpolator->skips = 0;
  1289. time_interpolator->ns_skipped = 0;
  1290. }
  1291. }
  1292. static inline int
  1293. is_better_time_interpolator(struct time_interpolator *new)
  1294. {
  1295. if (!time_interpolator)
  1296. return 1;
  1297. return new->frequency > 2*time_interpolator->frequency ||
  1298. (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
  1299. }
  1300. void
  1301. register_time_interpolator(struct time_interpolator *ti)
  1302. {
  1303. unsigned long flags;
  1304. /* Sanity check */
  1305. if (ti->frequency == 0 || ti->mask == 0)
  1306. BUG();
  1307. ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
  1308. spin_lock(&time_interpolator_lock);
  1309. write_seqlock_irqsave(&xtime_lock, flags);
  1310. if (is_better_time_interpolator(ti)) {
  1311. time_interpolator = ti;
  1312. time_interpolator_reset();
  1313. }
  1314. write_sequnlock_irqrestore(&xtime_lock, flags);
  1315. ti->next = time_interpolator_list;
  1316. time_interpolator_list = ti;
  1317. spin_unlock(&time_interpolator_lock);
  1318. }
  1319. void
  1320. unregister_time_interpolator(struct time_interpolator *ti)
  1321. {
  1322. struct time_interpolator *curr, **prev;
  1323. unsigned long flags;
  1324. spin_lock(&time_interpolator_lock);
  1325. prev = &time_interpolator_list;
  1326. for (curr = *prev; curr; curr = curr->next) {
  1327. if (curr == ti) {
  1328. *prev = curr->next;
  1329. break;
  1330. }
  1331. prev = &curr->next;
  1332. }
  1333. write_seqlock_irqsave(&xtime_lock, flags);
  1334. if (ti == time_interpolator) {
  1335. /* we lost the best time-interpolator: */
  1336. time_interpolator = NULL;
  1337. /* find the next-best interpolator */
  1338. for (curr = time_interpolator_list; curr; curr = curr->next)
  1339. if (is_better_time_interpolator(curr))
  1340. time_interpolator = curr;
  1341. time_interpolator_reset();
  1342. }
  1343. write_sequnlock_irqrestore(&xtime_lock, flags);
  1344. spin_unlock(&time_interpolator_lock);
  1345. }
  1346. #endif /* CONFIG_TIME_INTERPOLATION */
  1347. /**
  1348. * msleep - sleep safely even with waitqueue interruptions
  1349. * @msecs: Time in milliseconds to sleep for
  1350. */
  1351. void msleep(unsigned int msecs)
  1352. {
  1353. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1354. while (timeout)
  1355. timeout = schedule_timeout_uninterruptible(timeout);
  1356. }
  1357. EXPORT_SYMBOL(msleep);
  1358. /**
  1359. * msleep_interruptible - sleep waiting for signals
  1360. * @msecs: Time in milliseconds to sleep for
  1361. */
  1362. unsigned long msleep_interruptible(unsigned int msecs)
  1363. {
  1364. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1365. while (timeout && !signal_pending(current))
  1366. timeout = schedule_timeout_interruptible(timeout);
  1367. return jiffies_to_msecs(timeout);
  1368. }
  1369. EXPORT_SYMBOL(msleep_interruptible);