timer.c 42 KB

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