timer.c 43 KB

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