timer.c 41 KB

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