fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / kernel / timer.c
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
22 #include <linux/kernel_stat.h>
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/percpu.h>
26 #include <linux/init.h>
27 #include <linux/mm.h>
28 #include <linux/swap.h>
29 #include <linux/notifier.h>
30 #include <linux/thread_info.h>
31 #include <linux/time.h>
32 #include <linux/jiffies.h>
33 #include <linux/posix-timers.h>
34 #include <linux/cpu.h>
35 #include <linux/syscalls.h>
36 #include <linux/delay.h>
37 #include <linux/vs_base.h>
38 #include <linux/vs_cvirt.h>
39 #include <linux/vs_pid.h>
40 #include <linux/vserver/sched.h>
41
42 #include <asm/uaccess.h>
43 #include <asm/unistd.h>
44 #include <asm/div64.h>
45 #include <asm/timex.h>
46 #include <asm/io.h>
47
48 u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
49
50 EXPORT_SYMBOL(jiffies_64);
51
52 /*
53  * per-CPU timer vector definitions:
54  */
55 #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
56 #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
57 #define TVN_SIZE (1 << TVN_BITS)
58 #define TVR_SIZE (1 << TVR_BITS)
59 #define TVN_MASK (TVN_SIZE - 1)
60 #define TVR_MASK (TVR_SIZE - 1)
61
62 typedef struct tvec_s {
63         struct list_head vec[TVN_SIZE];
64 } tvec_t;
65
66 typedef struct tvec_root_s {
67         struct list_head vec[TVR_SIZE];
68 } tvec_root_t;
69
70 struct tvec_t_base_s {
71         spinlock_t lock;
72         struct timer_list *running_timer;
73         unsigned long timer_jiffies;
74         tvec_root_t tv1;
75         tvec_t tv2;
76         tvec_t tv3;
77         tvec_t tv4;
78         tvec_t tv5;
79 } ____cacheline_aligned_in_smp;
80
81 typedef struct tvec_t_base_s tvec_base_t;
82
83 tvec_base_t boot_tvec_bases;
84 EXPORT_SYMBOL(boot_tvec_bases);
85 static DEFINE_PER_CPU(tvec_base_t *, tvec_bases) = &boot_tvec_bases;
86
87 /**
88  * __round_jiffies - function to round jiffies to a full second
89  * @j: the time in (absolute) jiffies that should be rounded
90  * @cpu: the processor number on which the timeout will happen
91  *
92  * __round_jiffies rounds an absolute time in the future (in jiffies)
93  * up or down to (approximately) full seconds. This is useful for timers
94  * for which the exact time they fire does not matter too much, as long as
95  * they fire approximately every X seconds.
96  *
97  * By rounding these timers to whole seconds, all such timers will fire
98  * at the same time, rather than at various times spread out. The goal
99  * of this is to have the CPU wake up less, which saves power.
100  *
101  * The exact rounding is skewed for each processor to avoid all
102  * processors firing at the exact same time, which could lead
103  * to lock contention or spurious cache line bouncing.
104  *
105  * The return value is the rounded version of the "j" parameter.
106  */
107 unsigned long __round_jiffies(unsigned long j, int cpu)
108 {
109         int rem;
110         unsigned long original = j;
111
112         /*
113          * We don't want all cpus firing their timers at once hitting the
114          * same lock or cachelines, so we skew each extra cpu with an extra
115          * 3 jiffies. This 3 jiffies came originally from the mm/ code which
116          * already did this.
117          * The skew is done by adding 3*cpunr, then round, then subtract this
118          * extra offset again.
119          */
120         j += cpu * 3;
121
122         rem = j % HZ;
123
124         /*
125          * If the target jiffie is just after a whole second (which can happen
126          * due to delays of the timer irq, long irq off times etc etc) then
127          * we should round down to the whole second, not up. Use 1/4th second
128          * as cutoff for this rounding as an extreme upper bound for this.
129          */
130         if (rem < HZ/4) /* round down */
131                 j = j - rem;
132         else /* round up */
133                 j = j - rem + HZ;
134
135         /* now that we have rounded, subtract the extra skew again */
136         j -= cpu * 3;
137
138         if (j <= jiffies) /* rounding ate our timeout entirely; */
139                 return original;
140         return j;
141 }
142 EXPORT_SYMBOL_GPL(__round_jiffies);
143
144 /**
145  * __round_jiffies_relative - function to round jiffies to a full second
146  * @j: the time in (relative) jiffies that should be rounded
147  * @cpu: the processor number on which the timeout will happen
148  *
149  * __round_jiffies_relative rounds a time delta  in the future (in jiffies)
150  * up or down to (approximately) full seconds. This is useful for timers
151  * for which the exact time they fire does not matter too much, as long as
152  * they fire approximately every X seconds.
153  *
154  * By rounding these timers to whole seconds, all such timers will fire
155  * at the same time, rather than at various times spread out. The goal
156  * of this is to have the CPU wake up less, which saves power.
157  *
158  * The exact rounding is skewed for each processor to avoid all
159  * processors firing at the exact same time, which could lead
160  * to lock contention or spurious cache line bouncing.
161  *
162  * The return value is the rounded version of the "j" parameter.
163  */
164 unsigned long __round_jiffies_relative(unsigned long j, int cpu)
165 {
166         /*
167          * In theory the following code can skip a jiffy in case jiffies
168          * increments right between the addition and the later subtraction.
169          * However since the entire point of this function is to use approximate
170          * timeouts, it's entirely ok to not handle that.
171          */
172         return  __round_jiffies(j + jiffies, cpu) - jiffies;
173 }
174 EXPORT_SYMBOL_GPL(__round_jiffies_relative);
175
176 /**
177  * round_jiffies - function to round jiffies to a full second
178  * @j: the time in (absolute) jiffies that should be rounded
179  *
180  * round_jiffies rounds an absolute time in the future (in jiffies)
181  * up or down to (approximately) full seconds. This is useful for timers
182  * for which the exact time they fire does not matter too much, as long as
183  * they fire approximately every X seconds.
184  *
185  * By rounding these timers to whole seconds, all such timers will fire
186  * at the same time, rather than at various times spread out. The goal
187  * of this is to have the CPU wake up less, which saves power.
188  *
189  * The return value is the rounded version of the "j" parameter.
190  */
191 unsigned long round_jiffies(unsigned long j)
192 {
193         return __round_jiffies(j, raw_smp_processor_id());
194 }
195 EXPORT_SYMBOL_GPL(round_jiffies);
196
197 /**
198  * round_jiffies_relative - function to round jiffies to a full second
199  * @j: the time in (relative) jiffies that should be rounded
200  *
201  * round_jiffies_relative rounds a time delta  in the future (in jiffies)
202  * up or down to (approximately) full seconds. This is useful for timers
203  * for which the exact time they fire does not matter too much, as long as
204  * they fire approximately every X seconds.
205  *
206  * By rounding these timers to whole seconds, all such timers will fire
207  * at the same time, rather than at various times spread out. The goal
208  * of this is to have the CPU wake up less, which saves power.
209  *
210  * The return value is the rounded version of the "j" parameter.
211  */
212 unsigned long round_jiffies_relative(unsigned long j)
213 {
214         return __round_jiffies_relative(j, raw_smp_processor_id());
215 }
216 EXPORT_SYMBOL_GPL(round_jiffies_relative);
217
218
219 static inline void set_running_timer(tvec_base_t *base,
220                                         struct timer_list *timer)
221 {
222 #ifdef CONFIG_SMP
223         base->running_timer = timer;
224 #endif
225 }
226
227 static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
228 {
229         unsigned long expires = timer->expires;
230         unsigned long idx = expires - base->timer_jiffies;
231         struct list_head *vec;
232
233         if (idx < TVR_SIZE) {
234                 int i = expires & TVR_MASK;
235                 vec = base->tv1.vec + i;
236         } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
237                 int i = (expires >> TVR_BITS) & TVN_MASK;
238                 vec = base->tv2.vec + i;
239         } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
240                 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
241                 vec = base->tv3.vec + i;
242         } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
243                 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
244                 vec = base->tv4.vec + i;
245         } else if ((signed long) idx < 0) {
246                 /*
247                  * Can happen if you add a timer with expires == jiffies,
248                  * or you set a timer to go off in the past
249                  */
250                 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
251         } else {
252                 int i;
253                 /* If the timeout is larger than 0xffffffff on 64-bit
254                  * architectures then we use the maximum timeout:
255                  */
256                 if (idx > 0xffffffffUL) {
257                         idx = 0xffffffffUL;
258                         expires = idx + base->timer_jiffies;
259                 }
260                 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
261                 vec = base->tv5.vec + i;
262         }
263         /*
264          * Timers are FIFO:
265          */
266         list_add_tail(&timer->entry, vec);
267 }
268
269 /**
270  * init_timer - initialize a timer.
271  * @timer: the timer to be initialized
272  *
273  * init_timer() must be done to a timer prior calling *any* of the
274  * other timer functions.
275  */
276 void fastcall init_timer(struct timer_list *timer)
277 {
278         timer->entry.next = NULL;
279         timer->base = __raw_get_cpu_var(tvec_bases);
280 }
281 EXPORT_SYMBOL(init_timer);
282
283 static inline void detach_timer(struct timer_list *timer,
284                                         int clear_pending)
285 {
286         struct list_head *entry = &timer->entry;
287
288         __list_del(entry->prev, entry->next);
289         if (clear_pending)
290                 entry->next = NULL;
291         entry->prev = LIST_POISON2;
292 }
293
294 /*
295  * We are using hashed locking: holding per_cpu(tvec_bases).lock
296  * means that all timers which are tied to this base via timer->base are
297  * locked, and the base itself is locked too.
298  *
299  * So __run_timers/migrate_timers can safely modify all timers which could
300  * be found on ->tvX lists.
301  *
302  * When the timer's base is locked, and the timer removed from list, it is
303  * possible to set timer->base = NULL and drop the lock: the timer remains
304  * locked.
305  */
306 static tvec_base_t *lock_timer_base(struct timer_list *timer,
307                                         unsigned long *flags)
308         __acquires(timer->base->lock)
309 {
310         tvec_base_t *base;
311
312         for (;;) {
313                 base = timer->base;
314                 if (likely(base != NULL)) {
315                         spin_lock_irqsave(&base->lock, *flags);
316                         if (likely(base == timer->base))
317                                 return base;
318                         /* The timer has migrated to another CPU */
319                         spin_unlock_irqrestore(&base->lock, *flags);
320                 }
321                 cpu_relax();
322         }
323 }
324
325 int __mod_timer(struct timer_list *timer, unsigned long expires)
326 {
327         tvec_base_t *base, *new_base;
328         unsigned long flags;
329         int ret = 0;
330
331         BUG_ON(!timer->function);
332
333         base = lock_timer_base(timer, &flags);
334
335         if (timer_pending(timer)) {
336                 detach_timer(timer, 0);
337                 ret = 1;
338         }
339
340         new_base = __get_cpu_var(tvec_bases);
341
342         if (base != new_base) {
343                 /*
344                  * We are trying to schedule the timer on the local CPU.
345                  * However we can't change timer's base while it is running,
346                  * otherwise del_timer_sync() can't detect that the timer's
347                  * handler yet has not finished. This also guarantees that
348                  * the timer is serialized wrt itself.
349                  */
350                 if (likely(base->running_timer != timer)) {
351                         /* See the comment in lock_timer_base() */
352                         timer->base = NULL;
353                         spin_unlock(&base->lock);
354                         base = new_base;
355                         spin_lock(&base->lock);
356                         timer->base = base;
357                 }
358         }
359
360         timer->expires = expires;
361         internal_add_timer(base, timer);
362         spin_unlock_irqrestore(&base->lock, flags);
363
364         return ret;
365 }
366
367 EXPORT_SYMBOL(__mod_timer);
368
369 /**
370  * add_timer_on - start a timer on a particular CPU
371  * @timer: the timer to be added
372  * @cpu: the CPU to start it on
373  *
374  * This is not very scalable on SMP. Double adds are not possible.
375  */
376 void add_timer_on(struct timer_list *timer, int cpu)
377 {
378         tvec_base_t *base = per_cpu(tvec_bases, cpu);
379         unsigned long flags;
380
381         BUG_ON(timer_pending(timer) || !timer->function);
382         spin_lock_irqsave(&base->lock, flags);
383         timer->base = base;
384         internal_add_timer(base, timer);
385         spin_unlock_irqrestore(&base->lock, flags);
386 }
387
388
389 /**
390  * mod_timer - modify a timer's timeout
391  * @timer: the timer to be modified
392  * @expires: new timeout in jiffies
393  *
394  * mod_timer is a more efficient way to update the expire field of an
395  * active timer (if the timer is inactive it will be activated)
396  *
397  * mod_timer(timer, expires) is equivalent to:
398  *
399  *     del_timer(timer); timer->expires = expires; add_timer(timer);
400  *
401  * Note that if there are multiple unserialized concurrent users of the
402  * same timer, then mod_timer() is the only safe way to modify the timeout,
403  * since add_timer() cannot modify an already running timer.
404  *
405  * The function returns whether it has modified a pending timer or not.
406  * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
407  * active timer returns 1.)
408  */
409 int mod_timer(struct timer_list *timer, unsigned long expires)
410 {
411         BUG_ON(!timer->function);
412
413         /*
414          * This is a common optimization triggered by the
415          * networking code - if the timer is re-modified
416          * to be the same thing then just return:
417          */
418         if (timer->expires == expires && timer_pending(timer))
419                 return 1;
420
421         return __mod_timer(timer, expires);
422 }
423
424 EXPORT_SYMBOL(mod_timer);
425
426 /**
427  * del_timer - deactive a timer.
428  * @timer: the timer to be deactivated
429  *
430  * del_timer() deactivates a timer - this works on both active and inactive
431  * timers.
432  *
433  * The function returns whether it has deactivated a pending timer or not.
434  * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
435  * active timer returns 1.)
436  */
437 int del_timer(struct timer_list *timer)
438 {
439         tvec_base_t *base;
440         unsigned long flags;
441         int ret = 0;
442
443         if (timer_pending(timer)) {
444                 base = lock_timer_base(timer, &flags);
445                 if (timer_pending(timer)) {
446                         detach_timer(timer, 1);
447                         ret = 1;
448                 }
449                 spin_unlock_irqrestore(&base->lock, flags);
450         }
451
452         return ret;
453 }
454
455 EXPORT_SYMBOL(del_timer);
456
457 #ifdef CONFIG_SMP
458 /**
459  * try_to_del_timer_sync - Try to deactivate a timer
460  * @timer: timer do del
461  *
462  * This function tries to deactivate a timer. Upon successful (ret >= 0)
463  * exit the timer is not queued and the handler is not running on any CPU.
464  *
465  * It must not be called from interrupt contexts.
466  */
467 int try_to_del_timer_sync(struct timer_list *timer)
468 {
469         tvec_base_t *base;
470         unsigned long flags;
471         int ret = -1;
472
473         base = lock_timer_base(timer, &flags);
474
475         if (base->running_timer == timer)
476                 goto out;
477
478         ret = 0;
479         if (timer_pending(timer)) {
480                 detach_timer(timer, 1);
481                 ret = 1;
482         }
483 out:
484         spin_unlock_irqrestore(&base->lock, flags);
485
486         return ret;
487 }
488
489 /**
490  * del_timer_sync - deactivate a timer and wait for the handler to finish.
491  * @timer: the timer to be deactivated
492  *
493  * This function only differs from del_timer() on SMP: besides deactivating
494  * the timer it also makes sure the handler has finished executing on other
495  * CPUs.
496  *
497  * Synchronization rules: callers must prevent restarting of the timer,
498  * otherwise this function is meaningless. It must not be called from
499  * interrupt contexts. The caller must not hold locks which would prevent
500  * completion of the timer's handler. The timer's handler must not call
501  * add_timer_on(). Upon exit the timer is not queued and the handler is
502  * not running on any CPU.
503  *
504  * The function returns whether it has deactivated a pending timer or not.
505  */
506 int del_timer_sync(struct timer_list *timer)
507 {
508         for (;;) {
509                 int ret = try_to_del_timer_sync(timer);
510                 if (ret >= 0)
511                         return ret;
512                 cpu_relax();
513         }
514 }
515
516 EXPORT_SYMBOL(del_timer_sync);
517 #endif
518
519 static int cascade(tvec_base_t *base, tvec_t *tv, int index)
520 {
521         /* cascade all the timers from tv up one level */
522         struct timer_list *timer, *tmp;
523         struct list_head tv_list;
524
525         list_replace_init(tv->vec + index, &tv_list);
526
527         /*
528          * We are removing _all_ timers from the list, so we
529          * don't have to detach them individually.
530          */
531         list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
532                 BUG_ON(timer->base != base);
533                 internal_add_timer(base, timer);
534         }
535
536         return index;
537 }
538
539 #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
540
541 /**
542  * __run_timers - run all expired timers (if any) on this CPU.
543  * @base: the timer vector to be processed.
544  *
545  * This function cascades all vectors and executes all expired timer
546  * vectors.
547  */
548 static inline void __run_timers(tvec_base_t *base)
549 {
550         struct timer_list *timer;
551
552         spin_lock_irq(&base->lock);
553         while (time_after_eq(jiffies, base->timer_jiffies)) {
554                 struct list_head work_list;
555                 struct list_head *head = &work_list;
556                 int index = base->timer_jiffies & TVR_MASK;
557
558                 /*
559                  * Cascade timers:
560                  */
561                 if (!index &&
562                         (!cascade(base, &base->tv2, INDEX(0))) &&
563                                 (!cascade(base, &base->tv3, INDEX(1))) &&
564                                         !cascade(base, &base->tv4, INDEX(2)))
565                         cascade(base, &base->tv5, INDEX(3));
566                 ++base->timer_jiffies;
567                 list_replace_init(base->tv1.vec + index, &work_list);
568                 while (!list_empty(head)) {
569                         void (*fn)(unsigned long);
570                         unsigned long data;
571
572                         timer = list_entry(head->next,struct timer_list,entry);
573                         fn = timer->function;
574                         data = timer->data;
575
576                         set_running_timer(base, timer);
577                         detach_timer(timer, 1);
578                         spin_unlock_irq(&base->lock);
579                         {
580                                 int preempt_count = preempt_count();
581                                 fn(data);
582                                 if (preempt_count != preempt_count()) {
583                                         printk(KERN_WARNING "huh, entered %p "
584                                                "with preempt_count %08x, exited"
585                                                " with %08x?\n",
586                                                fn, preempt_count,
587                                                preempt_count());
588                                         BUG();
589                                 }
590                         }
591                         spin_lock_irq(&base->lock);
592                 }
593         }
594         set_running_timer(base, NULL);
595         spin_unlock_irq(&base->lock);
596 }
597
598 #ifdef CONFIG_NO_IDLE_HZ
599 /*
600  * Find out when the next timer event is due to happen. This
601  * is used on S/390 to stop all activity when a cpus is idle.
602  * This functions needs to be called disabled.
603  */
604 unsigned long next_timer_interrupt(void)
605 {
606         tvec_base_t *base;
607         struct list_head *list;
608         struct timer_list *nte;
609         unsigned long expires;
610         unsigned long hr_expires = MAX_JIFFY_OFFSET;
611         ktime_t hr_delta;
612         tvec_t *varray[4];
613         int i, j;
614
615         hr_delta = hrtimer_get_next_event();
616         if (hr_delta.tv64 != KTIME_MAX) {
617                 struct timespec tsdelta;
618                 tsdelta = ktime_to_timespec(hr_delta);
619                 hr_expires = timespec_to_jiffies(&tsdelta);
620                 if (hr_expires < 3)
621                         return hr_expires + jiffies;
622         }
623         hr_expires += jiffies;
624
625         base = __get_cpu_var(tvec_bases);
626         spin_lock(&base->lock);
627         expires = base->timer_jiffies + (LONG_MAX >> 1);
628         list = NULL;
629
630         /* Look for timer events in tv1. */
631         j = base->timer_jiffies & TVR_MASK;
632         do {
633                 list_for_each_entry(nte, base->tv1.vec + j, entry) {
634                         expires = nte->expires;
635                         if (j < (base->timer_jiffies & TVR_MASK))
636                                 list = base->tv2.vec + (INDEX(0));
637                         goto found;
638                 }
639                 j = (j + 1) & TVR_MASK;
640         } while (j != (base->timer_jiffies & TVR_MASK));
641
642         /* Check tv2-tv5. */
643         varray[0] = &base->tv2;
644         varray[1] = &base->tv3;
645         varray[2] = &base->tv4;
646         varray[3] = &base->tv5;
647         for (i = 0; i < 4; i++) {
648                 j = INDEX(i);
649                 do {
650                         if (list_empty(varray[i]->vec + j)) {
651                                 j = (j + 1) & TVN_MASK;
652                                 continue;
653                         }
654                         list_for_each_entry(nte, varray[i]->vec + j, entry)
655                                 if (time_before(nte->expires, expires))
656                                         expires = nte->expires;
657                         if (j < (INDEX(i)) && i < 3)
658                                 list = varray[i + 1]->vec + (INDEX(i + 1));
659                         goto found;
660                 } while (j != (INDEX(i)));
661         }
662 found:
663         if (list) {
664                 /*
665                  * The search wrapped. We need to look at the next list
666                  * from next tv element that would cascade into tv element
667                  * where we found the timer element.
668                  */
669                 list_for_each_entry(nte, list, entry) {
670                         if (time_before(nte->expires, expires))
671                                 expires = nte->expires;
672                 }
673         }
674         spin_unlock(&base->lock);
675
676         /*
677          * It can happen that other CPUs service timer IRQs and increment
678          * jiffies, but we have not yet got a local timer tick to process
679          * the timer wheels.  In that case, the expiry time can be before
680          * jiffies, but since the high-resolution timer here is relative to
681          * jiffies, the default expression when high-resolution timers are
682          * not active,
683          *
684          *   time_before(MAX_JIFFY_OFFSET + jiffies, expires)
685          *
686          * would falsely evaluate to true.  If that is the case, just
687          * return jiffies so that we can immediately fire the local timer
688          */
689         if (time_before(expires, jiffies))
690                 return jiffies;
691
692         if (time_before(hr_expires, expires))
693                 return hr_expires;
694
695         return expires;
696 }
697 #endif
698
699 /******************************************************************/
700
701 /* 
702  * The current time 
703  * wall_to_monotonic is what we need to add to xtime (or xtime corrected 
704  * for sub jiffie times) to get to monotonic time.  Monotonic is pegged
705  * at zero at system boot time, so wall_to_monotonic will be negative,
706  * however, we will ALWAYS keep the tv_nsec part positive so we can use
707  * the usual normalization.
708  */
709 struct timespec xtime __attribute__ ((aligned (16)));
710 struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
711
712 EXPORT_SYMBOL(xtime);
713
714
715 /* XXX - all of this timekeeping code should be later moved to time.c */
716 #include <linux/clocksource.h>
717 static struct clocksource *clock; /* pointer to current clocksource */
718
719 #ifdef CONFIG_GENERIC_TIME
720 /**
721  * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook
722  *
723  * private function, must hold xtime_lock lock when being
724  * called. Returns the number of nanoseconds since the
725  * last call to update_wall_time() (adjusted by NTP scaling)
726  */
727 static inline s64 __get_nsec_offset(void)
728 {
729         cycle_t cycle_now, cycle_delta;
730         s64 ns_offset;
731
732         /* read clocksource: */
733         cycle_now = clocksource_read(clock);
734
735         /* calculate the delta since the last update_wall_time: */
736         cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
737
738         /* convert to nanoseconds: */
739         ns_offset = cyc2ns(clock, cycle_delta);
740
741         return ns_offset;
742 }
743
744 /**
745  * __get_realtime_clock_ts - Returns the time of day in a timespec
746  * @ts:         pointer to the timespec to be set
747  *
748  * Returns the time of day in a timespec. Used by
749  * do_gettimeofday() and get_realtime_clock_ts().
750  */
751 static inline void __get_realtime_clock_ts(struct timespec *ts)
752 {
753         unsigned long seq;
754         s64 nsecs;
755
756         do {
757                 seq = read_seqbegin(&xtime_lock);
758
759                 *ts = xtime;
760                 nsecs = __get_nsec_offset();
761
762         } while (read_seqretry(&xtime_lock, seq));
763
764         timespec_add_ns(ts, nsecs);
765 }
766
767 /**
768  * getnstimeofday - Returns the time of day in a timespec
769  * @ts:         pointer to the timespec to be set
770  *
771  * Returns the time of day in a timespec.
772  */
773 void getnstimeofday(struct timespec *ts)
774 {
775         __get_realtime_clock_ts(ts);
776 }
777
778 EXPORT_SYMBOL(getnstimeofday);
779
780 /**
781  * do_gettimeofday - Returns the time of day in a timeval
782  * @tv:         pointer to the timeval to be set
783  *
784  * NOTE: Users should be converted to using get_realtime_clock_ts()
785  */
786 void do_gettimeofday(struct timeval *tv)
787 {
788         struct timespec now;
789
790         __get_realtime_clock_ts(&now);
791         tv->tv_sec = now.tv_sec;
792         tv->tv_usec = now.tv_nsec/1000;
793 }
794
795 EXPORT_SYMBOL(do_gettimeofday);
796 /**
797  * do_settimeofday - Sets the time of day
798  * @tv:         pointer to the timespec variable containing the new time
799  *
800  * Sets the time of day to the new time and update NTP and notify hrtimers
801  */
802 int do_settimeofday(struct timespec *tv)
803 {
804         unsigned long flags;
805         time_t wtm_sec, sec = tv->tv_sec;
806         long wtm_nsec, nsec = tv->tv_nsec;
807
808         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
809                 return -EINVAL;
810
811         write_seqlock_irqsave(&xtime_lock, flags);
812
813         nsec -= __get_nsec_offset();
814
815         wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
816         wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
817
818         set_normalized_timespec(&xtime, sec, nsec);
819         set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
820
821         clock->error = 0;
822         ntp_clear();
823
824         write_sequnlock_irqrestore(&xtime_lock, flags);
825
826         /* signal hrtimers about time change */
827         clock_was_set();
828
829         return 0;
830 }
831
832 EXPORT_SYMBOL(do_settimeofday);
833
834 /**
835  * change_clocksource - Swaps clocksources if a new one is available
836  *
837  * Accumulates current time interval and initializes new clocksource
838  */
839 static int change_clocksource(void)
840 {
841         struct clocksource *new;
842         cycle_t now;
843         u64 nsec;
844         new = clocksource_get_next();
845         if (clock != new) {
846                 now = clocksource_read(new);
847                 nsec =  __get_nsec_offset();
848                 timespec_add_ns(&xtime, nsec);
849
850                 clock = new;
851                 clock->cycle_last = now;
852                 printk(KERN_INFO "Time: %s clocksource has been installed.\n",
853                        clock->name);
854                 return 1;
855         } else if (clock->update_callback) {
856                 return clock->update_callback();
857         }
858         return 0;
859 }
860 #else
861 static inline int change_clocksource(void)
862 {
863         return 0;
864 }
865 #endif
866
867 /**
868  * timeofday_is_continuous - check to see if timekeeping is free running
869  */
870 int timekeeping_is_continuous(void)
871 {
872         unsigned long seq;
873         int ret;
874
875         do {
876                 seq = read_seqbegin(&xtime_lock);
877
878                 ret = clock->is_continuous;
879
880         } while (read_seqretry(&xtime_lock, seq));
881
882         return ret;
883 }
884
885 /*
886  * timekeeping_init - Initializes the clocksource and common timekeeping values
887  */
888 void __init timekeeping_init(void)
889 {
890         unsigned long flags;
891
892         write_seqlock_irqsave(&xtime_lock, flags);
893
894         ntp_clear();
895
896         clock = clocksource_get_next();
897         clocksource_calculate_interval(clock, tick_nsec);
898         clock->cycle_last = clocksource_read(clock);
899
900         write_sequnlock_irqrestore(&xtime_lock, flags);
901 }
902
903
904 static int timekeeping_suspended;
905 /**
906  * timekeeping_resume - Resumes the generic timekeeping subsystem.
907  * @dev:        unused
908  *
909  * This is for the generic clocksource timekeeping.
910  * xtime/wall_to_monotonic/jiffies/etc are
911  * still managed by arch specific suspend/resume code.
912  */
913 static int timekeeping_resume(struct sys_device *dev)
914 {
915         unsigned long flags;
916
917         write_seqlock_irqsave(&xtime_lock, flags);
918         /* restart the last cycle value */
919         clock->cycle_last = clocksource_read(clock);
920         clock->error = 0;
921         timekeeping_suspended = 0;
922         write_sequnlock_irqrestore(&xtime_lock, flags);
923         return 0;
924 }
925
926 static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
927 {
928         unsigned long flags;
929
930         write_seqlock_irqsave(&xtime_lock, flags);
931         timekeeping_suspended = 1;
932         write_sequnlock_irqrestore(&xtime_lock, flags);
933         return 0;
934 }
935
936 /* sysfs resume/suspend bits for timekeeping */
937 static struct sysdev_class timekeeping_sysclass = {
938         .resume         = timekeeping_resume,
939         .suspend        = timekeeping_suspend,
940         set_kset_name("timekeeping"),
941 };
942
943 static struct sys_device device_timer = {
944         .id             = 0,
945         .cls            = &timekeeping_sysclass,
946 };
947
948 static int __init timekeeping_init_device(void)
949 {
950         int error = sysdev_class_register(&timekeeping_sysclass);
951         if (!error)
952                 error = sysdev_register(&device_timer);
953         return error;
954 }
955
956 device_initcall(timekeeping_init_device);
957
958 /*
959  * If the error is already larger, we look ahead even further
960  * to compensate for late or lost adjustments.
961  */
962 static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
963                                                  s64 *offset)
964 {
965         s64 tick_error, i;
966         u32 look_ahead, adj;
967         s32 error2, mult;
968
969         /*
970          * Use the current error value to determine how much to look ahead.
971          * The larger the error the slower we adjust for it to avoid problems
972          * with losing too many ticks, otherwise we would overadjust and
973          * produce an even larger error.  The smaller the adjustment the
974          * faster we try to adjust for it, as lost ticks can do less harm
975          * here.  This is tuned so that an error of about 1 msec is adusted
976          * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
977          */
978         error2 = clock->error >> (TICK_LENGTH_SHIFT + 22 - 2 * SHIFT_HZ);
979         error2 = abs(error2);
980         for (look_ahead = 0; error2 > 0; look_ahead++)
981                 error2 >>= 2;
982
983         /*
984          * Now calculate the error in (1 << look_ahead) ticks, but first
985          * remove the single look ahead already included in the error.
986          */
987         tick_error = current_tick_length() >>
988                 (TICK_LENGTH_SHIFT - clock->shift + 1);
989         tick_error -= clock->xtime_interval >> 1;
990         error = ((error - tick_error) >> look_ahead) + tick_error;
991
992         /* Finally calculate the adjustment shift value.  */
993         i = *interval;
994         mult = 1;
995         if (error < 0) {
996                 error = -error;
997                 *interval = -*interval;
998                 *offset = -*offset;
999                 mult = -1;
1000         }
1001         for (adj = 0; error > i; adj++)
1002                 error >>= 1;
1003
1004         *interval <<= adj;
1005         *offset <<= adj;
1006         return mult << adj;
1007 }
1008
1009 /*
1010  * Adjust the multiplier to reduce the error value,
1011  * this is optimized for the most common adjustments of -1,0,1,
1012  * for other values we can do a bit more work.
1013  */
1014 static void clocksource_adjust(struct clocksource *clock, s64 offset)
1015 {
1016         s64 error, interval = clock->cycle_interval;
1017         int adj;
1018
1019         error = clock->error >> (TICK_LENGTH_SHIFT - clock->shift - 1);
1020         if (error > interval) {
1021                 error >>= 2;
1022                 if (likely(error <= interval))
1023                         adj = 1;
1024                 else
1025                         adj = clocksource_bigadjust(error, &interval, &offset);
1026         } else if (error < -interval) {
1027                 error >>= 2;
1028                 if (likely(error >= -interval)) {
1029                         adj = -1;
1030                         interval = -interval;
1031                         offset = -offset;
1032                 } else
1033                         adj = clocksource_bigadjust(error, &interval, &offset);
1034         } else
1035                 return;
1036
1037         clock->mult += adj;
1038         clock->xtime_interval += interval;
1039         clock->xtime_nsec -= offset;
1040         clock->error -= (interval - offset) <<
1041                         (TICK_LENGTH_SHIFT - clock->shift);
1042 }
1043
1044 /**
1045  * update_wall_time - Uses the current clocksource to increment the wall time
1046  *
1047  * Called from the timer interrupt, must hold a write on xtime_lock.
1048  */
1049 static void update_wall_time(void)
1050 {
1051         cycle_t offset;
1052
1053         /* Make sure we're fully resumed: */
1054         if (unlikely(timekeeping_suspended))
1055                 return;
1056
1057 #ifdef CONFIG_GENERIC_TIME
1058         offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
1059 #else
1060         offset = clock->cycle_interval;
1061 #endif
1062         clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift;
1063
1064         /* normally this loop will run just once, however in the
1065          * case of lost or late ticks, it will accumulate correctly.
1066          */
1067         while (offset >= clock->cycle_interval) {
1068                 /* accumulate one interval */
1069                 clock->xtime_nsec += clock->xtime_interval;
1070                 clock->cycle_last += clock->cycle_interval;
1071                 offset -= clock->cycle_interval;
1072
1073                 if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
1074                         clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
1075                         xtime.tv_sec++;
1076                         second_overflow();
1077                 }
1078
1079                 /* interpolator bits */
1080                 time_interpolator_update(clock->xtime_interval
1081                                                 >> clock->shift);
1082
1083                 /* accumulate error between NTP and clock interval */
1084                 clock->error += current_tick_length();
1085                 clock->error -= clock->xtime_interval << (TICK_LENGTH_SHIFT - clock->shift);
1086         }
1087
1088         /* correct the clock when NTP error is too big */
1089         clocksource_adjust(clock, offset);
1090
1091         /* store full nanoseconds into xtime */
1092         xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift;
1093         clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
1094
1095         /* check to see if there is a new clocksource to use */
1096         if (change_clocksource()) {
1097                 clock->error = 0;
1098                 clock->xtime_nsec = 0;
1099                 clocksource_calculate_interval(clock, tick_nsec);
1100         }
1101 }
1102
1103 /*
1104  * Called from the timer interrupt handler to charge one tick to the current 
1105  * process.  user_tick is 1 if the tick is user time, 0 for system.
1106  */
1107 void update_process_times(int user_tick)
1108 {
1109         struct task_struct *p = current;
1110         int cpu = smp_processor_id();
1111
1112         /* Note: this timer irq context must be accounted for as well. */
1113         if (user_tick)
1114                 account_user_time(p, jiffies_to_cputime(1));
1115         else
1116                 account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
1117         run_local_timers();
1118         if (rcu_pending(cpu))
1119                 rcu_check_callbacks(cpu, user_tick);
1120         scheduler_tick();
1121         run_posix_cpu_timers(p);
1122 }
1123
1124 /*
1125  * Nr of active tasks - counted in fixed-point numbers
1126  */
1127 static unsigned long count_active_tasks(void)
1128 {
1129         return nr_active() * FIXED_1;
1130 }
1131
1132 /*
1133  * Hmm.. Changed this, as the GNU make sources (load.c) seems to
1134  * imply that avenrun[] is the standard name for this kind of thing.
1135  * Nothing else seems to be standardized: the fractional size etc
1136  * all seem to differ on different machines.
1137  *
1138  * Requires xtime_lock to access.
1139  */
1140 unsigned long avenrun[3];
1141
1142 EXPORT_SYMBOL(avenrun);
1143
1144 /*
1145  * calc_load - given tick count, update the avenrun load estimates.
1146  * This is called while holding a write_lock on xtime_lock.
1147  */
1148 static inline void calc_load(unsigned long ticks)
1149 {
1150         unsigned long active_tasks; /* fixed-point */
1151         static int count = LOAD_FREQ;
1152
1153         count -= ticks;
1154         if (unlikely(count < 0)) {
1155                 active_tasks = count_active_tasks();
1156                 do {
1157                         CALC_LOAD(avenrun[0], EXP_1, active_tasks);
1158                         CALC_LOAD(avenrun[1], EXP_5, active_tasks);
1159                         CALC_LOAD(avenrun[2], EXP_15, active_tasks);
1160                         count += LOAD_FREQ;
1161                 } while (count < 0);
1162         }
1163 }
1164
1165 /*
1166  * This read-write spinlock protects us from races in SMP while
1167  * playing with xtime and avenrun.
1168  */
1169 #ifndef ARCH_HAVE_XTIME_LOCK
1170 __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
1171
1172 EXPORT_SYMBOL(xtime_lock);
1173 #endif
1174
1175 /*
1176  * This function runs timers and the timer-tq in bottom half context.
1177  */
1178 static void run_timer_softirq(struct softirq_action *h)
1179 {
1180         tvec_base_t *base = __get_cpu_var(tvec_bases);
1181
1182         hrtimer_run_queues();
1183         if (time_after_eq(jiffies, base->timer_jiffies))
1184                 __run_timers(base);
1185 }
1186
1187 /*
1188  * Called by the local, per-CPU timer interrupt on SMP.
1189  */
1190 void run_local_timers(void)
1191 {
1192         raise_softirq(TIMER_SOFTIRQ);
1193         softlockup_tick();
1194 }
1195
1196 /*
1197  * Called by the timer interrupt. xtime_lock must already be taken
1198  * by the timer IRQ!
1199  */
1200 static inline void update_times(unsigned long ticks)
1201 {
1202         update_wall_time();
1203         calc_load(ticks);
1204 }
1205   
1206 /*
1207  * The 64-bit jiffies value is not atomic - you MUST NOT read it
1208  * without sampling the sequence number in xtime_lock.
1209  * jiffies is defined in the linker script...
1210  */
1211
1212 void do_timer(unsigned long ticks)
1213 {
1214         jiffies_64 += ticks;
1215         update_times(ticks);
1216 }
1217
1218 #ifdef __ARCH_WANT_SYS_ALARM
1219
1220 /*
1221  * For backwards compatibility?  This can be done in libc so Alpha
1222  * and all newer ports shouldn't need it.
1223  */
1224 asmlinkage unsigned long sys_alarm(unsigned int seconds)
1225 {
1226         return alarm_setitimer(seconds);
1227 }
1228
1229 #endif
1230
1231
1232 /**
1233  * sys_getpid - return the thread group id of the current process
1234  *
1235  * Note, despite the name, this returns the tgid not the pid.  The tgid and
1236  * the pid are identical unless CLONE_THREAD was specified on clone() in
1237  * which case the tgid is the same in all threads of the same group.
1238  *
1239  * This is SMP safe as current->tgid does not change.
1240  */
1241 asmlinkage long sys_getpid(void)
1242 {
1243         return vx_map_tgid(current->tgid);
1244 }
1245
1246 /*
1247  * Accessing ->parent is not SMP-safe, it could
1248  * change from under us. However, we can use a stale
1249  * value of ->parent under rcu_read_lock(), see
1250  * release_task()->call_rcu(delayed_put_task_struct).
1251  */
1252 asmlinkage long sys_getppid(void)
1253 {
1254         int pid;
1255
1256         rcu_read_lock();
1257         pid = rcu_dereference(current->parent)->tgid;
1258         rcu_read_unlock();
1259         return vx_map_pid(pid);
1260 }
1261
1262 #ifdef __alpha__
1263
1264 /*
1265  * The Alpha uses getxpid, getxuid, and getxgid instead.
1266  */
1267
1268 asmlinkage long do_getxpid(long *ppid)
1269 {
1270         *ppid = sys_getppid();
1271         return sys_getpid();
1272 }
1273
1274 #else /* _alpha_ */
1275
1276 asmlinkage long sys_getuid(void)
1277 {
1278         /* Only we change this so SMP safe */
1279         return current->uid;
1280 }
1281
1282 asmlinkage long sys_geteuid(void)
1283 {
1284         /* Only we change this so SMP safe */
1285         return current->euid;
1286 }
1287
1288 asmlinkage long sys_getgid(void)
1289 {
1290         /* Only we change this so SMP safe */
1291         return current->gid;
1292 }
1293
1294 asmlinkage long sys_getegid(void)
1295 {
1296         /* Only we change this so SMP safe */
1297         return  current->egid;
1298 }
1299
1300 #endif
1301
1302 static void process_timeout(unsigned long __data)
1303 {
1304         wake_up_process((struct task_struct *)__data);
1305 }
1306
1307 /**
1308  * schedule_timeout - sleep until timeout
1309  * @timeout: timeout value in jiffies
1310  *
1311  * Make the current task sleep until @timeout jiffies have
1312  * elapsed. The routine will return immediately unless
1313  * the current task state has been set (see set_current_state()).
1314  *
1315  * You can set the task state as follows -
1316  *
1317  * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1318  * pass before the routine returns. The routine will return 0
1319  *
1320  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1321  * delivered to the current task. In this case the remaining time
1322  * in jiffies will be returned, or 0 if the timer expired in time
1323  *
1324  * The current task state is guaranteed to be TASK_RUNNING when this
1325  * routine returns.
1326  *
1327  * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1328  * the CPU away without a bound on the timeout. In this case the return
1329  * value will be %MAX_SCHEDULE_TIMEOUT.
1330  *
1331  * In all cases the return value is guaranteed to be non-negative.
1332  */
1333 fastcall signed long __sched schedule_timeout(signed long timeout)
1334 {
1335         struct timer_list timer;
1336         unsigned long expire;
1337
1338         switch (timeout)
1339         {
1340         case MAX_SCHEDULE_TIMEOUT:
1341                 /*
1342                  * These two special cases are useful to be comfortable
1343                  * in the caller. Nothing more. We could take
1344                  * MAX_SCHEDULE_TIMEOUT from one of the negative value
1345                  * but I' d like to return a valid offset (>=0) to allow
1346                  * the caller to do everything it want with the retval.
1347                  */
1348                 schedule();
1349                 goto out;
1350         default:
1351                 /*
1352                  * Another bit of PARANOID. Note that the retval will be
1353                  * 0 since no piece of kernel is supposed to do a check
1354                  * for a negative retval of schedule_timeout() (since it
1355                  * should never happens anyway). You just have the printk()
1356                  * that will tell you if something is gone wrong and where.
1357                  */
1358                 if (timeout < 0) {
1359                         printk(KERN_ERR "schedule_timeout: wrong timeout "
1360                                 "value %lx\n", timeout);
1361                         dump_stack();
1362                         current->state = TASK_RUNNING;
1363                         goto out;
1364                 }
1365         }
1366
1367         expire = timeout + jiffies;
1368
1369         setup_timer(&timer, process_timeout, (unsigned long)current);
1370         __mod_timer(&timer, expire);
1371         schedule();
1372         del_singleshot_timer_sync(&timer);
1373
1374         timeout = expire - jiffies;
1375
1376  out:
1377         return timeout < 0 ? 0 : timeout;
1378 }
1379 EXPORT_SYMBOL(schedule_timeout);
1380
1381 /*
1382  * We can use __set_current_state() here because schedule_timeout() calls
1383  * schedule() unconditionally.
1384  */
1385 signed long __sched schedule_timeout_interruptible(signed long timeout)
1386 {
1387         __set_current_state(TASK_INTERRUPTIBLE);
1388         return schedule_timeout(timeout);
1389 }
1390 EXPORT_SYMBOL(schedule_timeout_interruptible);
1391
1392 signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1393 {
1394         __set_current_state(TASK_UNINTERRUPTIBLE);
1395         return schedule_timeout(timeout);
1396 }
1397 EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1398
1399 /* Thread ID - the internal kernel "pid" */
1400 asmlinkage long sys_gettid(void)
1401 {
1402         return current->pid;
1403 }
1404
1405 /**
1406  * sys_sysinfo - fill in sysinfo struct
1407  * @info: pointer to buffer to fill
1408  */ 
1409 asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1410 {
1411         struct sysinfo val;
1412         unsigned long mem_total, sav_total;
1413         unsigned int mem_unit, bitcount;
1414         unsigned long seq;
1415
1416         memset((char *)&val, 0, sizeof(struct sysinfo));
1417
1418         do {
1419                 struct timespec tp;
1420                 seq = read_seqbegin(&xtime_lock);
1421
1422                 /*
1423                  * This is annoying.  The below is the same thing
1424                  * posix_get_clock_monotonic() does, but it wants to
1425                  * take the lock which we want to cover the loads stuff
1426                  * too.
1427                  */
1428
1429                 getnstimeofday(&tp);
1430                 tp.tv_sec += wall_to_monotonic.tv_sec;
1431                 tp.tv_nsec += wall_to_monotonic.tv_nsec;
1432                 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1433                         tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1434                         tp.tv_sec++;
1435                 }
1436                 if (vx_flags(VXF_VIRT_UPTIME, 0))
1437                         vx_vsi_uptime(&tp, NULL);
1438                 val.uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1439
1440                 val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1441                 val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1442                 val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
1443
1444                 val.procs = nr_threads;
1445         } while (read_seqretry(&xtime_lock, seq));
1446
1447         si_meminfo(&val);
1448         si_swapinfo(&val);
1449
1450         /*
1451          * If the sum of all the available memory (i.e. ram + swap)
1452          * is less than can be stored in a 32 bit unsigned long then
1453          * we can be binary compatible with 2.2.x kernels.  If not,
1454          * well, in that case 2.2.x was broken anyways...
1455          *
1456          *  -Erik Andersen <andersee@debian.org>
1457          */
1458
1459         mem_total = val.totalram + val.totalswap;
1460         if (mem_total < val.totalram || mem_total < val.totalswap)
1461                 goto out;
1462         bitcount = 0;
1463         mem_unit = val.mem_unit;
1464         while (mem_unit > 1) {
1465                 bitcount++;
1466                 mem_unit >>= 1;
1467                 sav_total = mem_total;
1468                 mem_total <<= 1;
1469                 if (mem_total < sav_total)
1470                         goto out;
1471         }
1472
1473         /*
1474          * If mem_total did not overflow, multiply all memory values by
1475          * val.mem_unit and set it to 1.  This leaves things compatible
1476          * with 2.2.x, and also retains compatibility with earlier 2.4.x
1477          * kernels...
1478          */
1479
1480         val.mem_unit = 1;
1481         val.totalram <<= bitcount;
1482         val.freeram <<= bitcount;
1483         val.sharedram <<= bitcount;
1484         val.bufferram <<= bitcount;
1485         val.totalswap <<= bitcount;
1486         val.freeswap <<= bitcount;
1487         val.totalhigh <<= bitcount;
1488         val.freehigh <<= bitcount;
1489
1490  out:
1491         if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1492                 return -EFAULT;
1493
1494         return 0;
1495 }
1496
1497 /*
1498  * lockdep: we want to track each per-CPU base as a separate lock-class,
1499  * but timer-bases are kmalloc()-ed, so we need to attach separate
1500  * keys to them:
1501  */
1502 static struct lock_class_key base_lock_keys[NR_CPUS];
1503
1504 static int __devinit init_timers_cpu(int cpu)
1505 {
1506         int j;
1507         tvec_base_t *base;
1508         static char __devinitdata tvec_base_done[NR_CPUS];
1509
1510         if (!tvec_base_done[cpu]) {
1511                 static char boot_done;
1512
1513                 if (boot_done) {
1514                         /*
1515                          * The APs use this path later in boot
1516                          */
1517                         base = kmalloc_node(sizeof(*base), GFP_KERNEL,
1518                                                 cpu_to_node(cpu));
1519                         if (!base)
1520                                 return -ENOMEM;
1521                         memset(base, 0, sizeof(*base));
1522                         per_cpu(tvec_bases, cpu) = base;
1523                 } else {
1524                         /*
1525                          * This is for the boot CPU - we use compile-time
1526                          * static initialisation because per-cpu memory isn't
1527                          * ready yet and because the memory allocators are not
1528                          * initialised either.
1529                          */
1530                         boot_done = 1;
1531                         base = &boot_tvec_bases;
1532                 }
1533                 tvec_base_done[cpu] = 1;
1534         } else {
1535                 base = per_cpu(tvec_bases, cpu);
1536         }
1537
1538         spin_lock_init(&base->lock);
1539         lockdep_set_class(&base->lock, base_lock_keys + cpu);
1540
1541         for (j = 0; j < TVN_SIZE; j++) {
1542                 INIT_LIST_HEAD(base->tv5.vec + j);
1543                 INIT_LIST_HEAD(base->tv4.vec + j);
1544                 INIT_LIST_HEAD(base->tv3.vec + j);
1545                 INIT_LIST_HEAD(base->tv2.vec + j);
1546         }
1547         for (j = 0; j < TVR_SIZE; j++)
1548                 INIT_LIST_HEAD(base->tv1.vec + j);
1549
1550         base->timer_jiffies = jiffies;
1551         return 0;
1552 }
1553
1554 #ifdef CONFIG_HOTPLUG_CPU
1555 static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
1556 {
1557         struct timer_list *timer;
1558
1559         while (!list_empty(head)) {
1560                 timer = list_entry(head->next, struct timer_list, entry);
1561                 detach_timer(timer, 0);
1562                 timer->base = new_base;
1563                 internal_add_timer(new_base, timer);
1564         }
1565 }
1566
1567 static void __devinit migrate_timers(int cpu)
1568 {
1569         tvec_base_t *old_base;
1570         tvec_base_t *new_base;
1571         int i;
1572
1573         BUG_ON(cpu_online(cpu));
1574         old_base = per_cpu(tvec_bases, cpu);
1575         new_base = get_cpu_var(tvec_bases);
1576
1577         local_irq_disable();
1578         spin_lock(&new_base->lock);
1579         spin_lock(&old_base->lock);
1580
1581         BUG_ON(old_base->running_timer);
1582
1583         for (i = 0; i < TVR_SIZE; i++)
1584                 migrate_timer_list(new_base, old_base->tv1.vec + i);
1585         for (i = 0; i < TVN_SIZE; i++) {
1586                 migrate_timer_list(new_base, old_base->tv2.vec + i);
1587                 migrate_timer_list(new_base, old_base->tv3.vec + i);
1588                 migrate_timer_list(new_base, old_base->tv4.vec + i);
1589                 migrate_timer_list(new_base, old_base->tv5.vec + i);
1590         }
1591
1592         spin_unlock(&old_base->lock);
1593         spin_unlock(&new_base->lock);
1594         local_irq_enable();
1595         put_cpu_var(tvec_bases);
1596 }
1597 #endif /* CONFIG_HOTPLUG_CPU */
1598
1599 static int __cpuinit timer_cpu_notify(struct notifier_block *self,
1600                                 unsigned long action, void *hcpu)
1601 {
1602         long cpu = (long)hcpu;
1603         switch(action) {
1604         case CPU_UP_PREPARE:
1605                 if (init_timers_cpu(cpu) < 0)
1606                         return NOTIFY_BAD;
1607                 break;
1608 #ifdef CONFIG_HOTPLUG_CPU
1609         case CPU_DEAD:
1610                 migrate_timers(cpu);
1611                 break;
1612 #endif
1613         default:
1614                 break;
1615         }
1616         return NOTIFY_OK;
1617 }
1618
1619 static struct notifier_block __cpuinitdata timers_nb = {
1620         .notifier_call  = timer_cpu_notify,
1621 };
1622
1623
1624 void __init init_timers(void)
1625 {
1626         int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1627                                 (void *)(long)smp_processor_id());
1628
1629         BUG_ON(err == NOTIFY_BAD);
1630         register_cpu_notifier(&timers_nb);
1631         open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1632 }
1633
1634 #ifdef CONFIG_TIME_INTERPOLATION
1635
1636 struct time_interpolator *time_interpolator __read_mostly;
1637 static struct time_interpolator *time_interpolator_list __read_mostly;
1638 static DEFINE_SPINLOCK(time_interpolator_lock);
1639
1640 static inline u64 time_interpolator_get_cycles(unsigned int src)
1641 {
1642         unsigned long (*x)(void);
1643
1644         switch (src)
1645         {
1646                 case TIME_SOURCE_FUNCTION:
1647                         x = time_interpolator->addr;
1648                         return x();
1649
1650                 case TIME_SOURCE_MMIO64 :
1651                         return readq_relaxed((void __iomem *)time_interpolator->addr);
1652
1653                 case TIME_SOURCE_MMIO32 :
1654                         return readl_relaxed((void __iomem *)time_interpolator->addr);
1655
1656                 default: return get_cycles();
1657         }
1658 }
1659
1660 static inline u64 time_interpolator_get_counter(int writelock)
1661 {
1662         unsigned int src = time_interpolator->source;
1663
1664         if (time_interpolator->jitter)
1665         {
1666                 u64 lcycle;
1667                 u64 now;
1668
1669                 do {
1670                         lcycle = time_interpolator->last_cycle;
1671                         now = time_interpolator_get_cycles(src);
1672                         if (lcycle && time_after(lcycle, now))
1673                                 return lcycle;
1674
1675                         /* When holding the xtime write lock, there's no need
1676                          * to add the overhead of the cmpxchg.  Readers are
1677                          * force to retry until the write lock is released.
1678                          */
1679                         if (writelock) {
1680                                 time_interpolator->last_cycle = now;
1681                                 return now;
1682                         }
1683                         /* Keep track of the last timer value returned. The use of cmpxchg here
1684                          * will cause contention in an SMP environment.
1685                          */
1686                 } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
1687                 return now;
1688         }
1689         else
1690                 return time_interpolator_get_cycles(src);
1691 }
1692
1693 void time_interpolator_reset(void)
1694 {
1695         time_interpolator->offset = 0;
1696         time_interpolator->last_counter = time_interpolator_get_counter(1);
1697 }
1698
1699 #define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
1700
1701 unsigned long time_interpolator_get_offset(void)
1702 {
1703         /* If we do not have a time interpolator set up then just return zero */
1704         if (!time_interpolator)
1705                 return 0;
1706
1707         return time_interpolator->offset +
1708                 GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
1709 }
1710
1711 #define INTERPOLATOR_ADJUST 65536
1712 #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
1713
1714 void time_interpolator_update(long delta_nsec)
1715 {
1716         u64 counter;
1717         unsigned long offset;
1718
1719         /* If there is no time interpolator set up then do nothing */
1720         if (!time_interpolator)
1721                 return;
1722
1723         /*
1724          * The interpolator compensates for late ticks by accumulating the late
1725          * time in time_interpolator->offset. A tick earlier than expected will
1726          * lead to a reset of the offset and a corresponding jump of the clock
1727          * forward. Again this only works if the interpolator clock is running
1728          * slightly slower than the regular clock and the tuning logic insures
1729          * that.
1730          */
1731
1732         counter = time_interpolator_get_counter(1);
1733         offset = time_interpolator->offset +
1734                         GET_TI_NSECS(counter, time_interpolator);
1735
1736         if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
1737                 time_interpolator->offset = offset - delta_nsec;
1738         else {
1739                 time_interpolator->skips++;
1740                 time_interpolator->ns_skipped += delta_nsec - offset;
1741                 time_interpolator->offset = 0;
1742         }
1743         time_interpolator->last_counter = counter;
1744
1745         /* Tuning logic for time interpolator invoked every minute or so.
1746          * Decrease interpolator clock speed if no skips occurred and an offset is carried.
1747          * Increase interpolator clock speed if we skip too much time.
1748          */
1749         if (jiffies % INTERPOLATOR_ADJUST == 0)
1750         {
1751                 if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec)
1752                         time_interpolator->nsec_per_cyc--;
1753                 if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
1754                         time_interpolator->nsec_per_cyc++;
1755                 time_interpolator->skips = 0;
1756                 time_interpolator->ns_skipped = 0;
1757         }
1758 }
1759
1760 static inline int
1761 is_better_time_interpolator(struct time_interpolator *new)
1762 {
1763         if (!time_interpolator)
1764                 return 1;
1765         return new->frequency > 2*time_interpolator->frequency ||
1766             (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
1767 }
1768
1769 void
1770 register_time_interpolator(struct time_interpolator *ti)
1771 {
1772         unsigned long flags;
1773
1774         /* Sanity check */
1775         BUG_ON(ti->frequency == 0 || ti->mask == 0);
1776
1777         ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
1778         spin_lock(&time_interpolator_lock);
1779         write_seqlock_irqsave(&xtime_lock, flags);
1780         if (is_better_time_interpolator(ti)) {
1781                 time_interpolator = ti;
1782                 time_interpolator_reset();
1783         }
1784         write_sequnlock_irqrestore(&xtime_lock, flags);
1785
1786         ti->next = time_interpolator_list;
1787         time_interpolator_list = ti;
1788         spin_unlock(&time_interpolator_lock);
1789 }
1790
1791 void
1792 unregister_time_interpolator(struct time_interpolator *ti)
1793 {
1794         struct time_interpolator *curr, **prev;
1795         unsigned long flags;
1796
1797         spin_lock(&time_interpolator_lock);
1798         prev = &time_interpolator_list;
1799         for (curr = *prev; curr; curr = curr->next) {
1800                 if (curr == ti) {
1801                         *prev = curr->next;
1802                         break;
1803                 }
1804                 prev = &curr->next;
1805         }
1806
1807         write_seqlock_irqsave(&xtime_lock, flags);
1808         if (ti == time_interpolator) {
1809                 /* we lost the best time-interpolator: */
1810                 time_interpolator = NULL;
1811                 /* find the next-best interpolator */
1812                 for (curr = time_interpolator_list; curr; curr = curr->next)
1813                         if (is_better_time_interpolator(curr))
1814                                 time_interpolator = curr;
1815                 time_interpolator_reset();
1816         }
1817         write_sequnlock_irqrestore(&xtime_lock, flags);
1818         spin_unlock(&time_interpolator_lock);
1819 }
1820 #endif /* CONFIG_TIME_INTERPOLATION */
1821
1822 /**
1823  * msleep - sleep safely even with waitqueue interruptions
1824  * @msecs: Time in milliseconds to sleep for
1825  */
1826 void msleep(unsigned int msecs)
1827 {
1828         unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1829
1830         while (timeout)
1831                 timeout = schedule_timeout_uninterruptible(timeout);
1832 }
1833
1834 EXPORT_SYMBOL(msleep);
1835
1836 /**
1837  * msleep_interruptible - sleep waiting for signals
1838  * @msecs: Time in milliseconds to sleep for
1839  */
1840 unsigned long msleep_interruptible(unsigned int msecs)
1841 {
1842         unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1843
1844         while (timeout && !signal_pending(current))
1845                 timeout = schedule_timeout_interruptible(timeout);
1846         return jiffies_to_msecs(timeout);
1847 }
1848
1849 EXPORT_SYMBOL(msleep_interruptible);