MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / kernel / timer.c
bloba66cdba163967c835520202a59f240be535d74ea
1 /*
2 * linux/kernel/timer.c
4 * Kernel internal timers, kernel timekeeping, basic process system calls
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
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
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>
38 #include <asm/uaccess.h>
39 #include <asm/unistd.h>
40 #include <asm/div64.h>
41 #include <asm/timex.h>
42 #include <asm/io.h>
44 u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
46 EXPORT_SYMBOL(jiffies_64);
49 * per-CPU timer vector definitions:
51 #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
52 #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
53 #define TVN_SIZE (1 << TVN_BITS)
54 #define TVR_SIZE (1 << TVR_BITS)
55 #define TVN_MASK (TVN_SIZE - 1)
56 #define TVR_MASK (TVR_SIZE - 1)
58 typedef struct tvec_s {
59 struct list_head vec[TVN_SIZE];
60 } tvec_t;
62 typedef struct tvec_root_s {
63 struct list_head vec[TVR_SIZE];
64 } tvec_root_t;
66 struct tvec_t_base_s {
67 spinlock_t lock;
68 struct timer_list *running_timer;
69 unsigned long timer_jiffies;
70 tvec_root_t tv1;
71 tvec_t tv2;
72 tvec_t tv3;
73 tvec_t tv4;
74 tvec_t tv5;
75 } ____cacheline_aligned_in_smp;
77 typedef struct tvec_t_base_s tvec_base_t;
79 tvec_base_t boot_tvec_bases;
80 EXPORT_SYMBOL(boot_tvec_bases);
81 static DEFINE_PER_CPU(tvec_base_t *, tvec_bases) = &boot_tvec_bases;
83 static inline void set_running_timer(tvec_base_t *base,
84 struct timer_list *timer)
86 #ifdef CONFIG_SMP
87 base->running_timer = timer;
88 #endif
91 static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
93 unsigned long expires = timer->expires;
94 unsigned long idx = expires - base->timer_jiffies;
95 struct list_head *vec;
97 if (idx < TVR_SIZE) {
98 int i = expires & TVR_MASK;
99 vec = base->tv1.vec + i;
100 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
101 int i = (expires >> TVR_BITS) & TVN_MASK;
102 vec = base->tv2.vec + i;
103 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
104 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
105 vec = base->tv3.vec + i;
106 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
107 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
108 vec = base->tv4.vec + i;
109 } else if ((signed long) idx < 0) {
111 * Can happen if you add a timer with expires == jiffies,
112 * or you set a timer to go off in the past
114 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
115 } else {
116 int i;
117 /* If the timeout is larger than 0xffffffff on 64-bit
118 * architectures then we use the maximum timeout:
120 if (idx > 0xffffffffUL) {
121 idx = 0xffffffffUL;
122 expires = idx + base->timer_jiffies;
124 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
125 vec = base->tv5.vec + i;
128 * Timers are FIFO:
130 list_add_tail(&timer->entry, vec);
134 * init_timer - initialize a timer.
135 * @timer: the timer to be initialized
137 * init_timer() must be done to a timer prior calling *any* of the
138 * other timer functions.
140 void fastcall init_timer(struct timer_list *timer)
142 timer->entry.next = NULL;
143 timer->base = __raw_get_cpu_var(tvec_bases);
145 EXPORT_SYMBOL(init_timer);
147 static inline void detach_timer(struct timer_list *timer,
148 int clear_pending)
150 struct list_head *entry = &timer->entry;
152 __list_del(entry->prev, entry->next);
153 if (clear_pending)
154 entry->next = NULL;
155 entry->prev = LIST_POISON2;
159 * We are using hashed locking: holding per_cpu(tvec_bases).lock
160 * means that all timers which are tied to this base via timer->base are
161 * locked, and the base itself is locked too.
163 * So __run_timers/migrate_timers can safely modify all timers which could
164 * be found on ->tvX lists.
166 * When the timer's base is locked, and the timer removed from list, it is
167 * possible to set timer->base = NULL and drop the lock: the timer remains
168 * locked.
170 static tvec_base_t *lock_timer_base(struct timer_list *timer,
171 unsigned long *flags)
172 __acquires(timer->base->lock)
174 tvec_base_t *base;
176 for (;;) {
177 base = timer->base;
178 if (likely(base != NULL)) {
179 spin_lock_irqsave(&base->lock, *flags);
180 if (likely(base == timer->base))
181 return base;
182 /* The timer has migrated to another CPU */
183 spin_unlock_irqrestore(&base->lock, *flags);
185 cpu_relax();
189 int __mod_timer(struct timer_list *timer, unsigned long expires)
191 tvec_base_t *base, *new_base;
192 unsigned long flags;
193 int ret = 0;
195 BUG_ON(!timer->function);
197 base = lock_timer_base(timer, &flags);
199 if (timer_pending(timer)) {
200 detach_timer(timer, 0);
201 ret = 1;
204 new_base = __get_cpu_var(tvec_bases);
206 if (base != new_base) {
208 * We are trying to schedule the timer on the local CPU.
209 * However we can't change timer's base while it is running,
210 * otherwise del_timer_sync() can't detect that the timer's
211 * handler yet has not finished. This also guarantees that
212 * the timer is serialized wrt itself.
214 if (likely(base->running_timer != timer)) {
215 /* See the comment in lock_timer_base() */
216 timer->base = NULL;
217 spin_unlock(&base->lock);
218 base = new_base;
219 spin_lock(&base->lock);
220 timer->base = base;
224 timer->expires = expires;
225 internal_add_timer(base, timer);
226 spin_unlock_irqrestore(&base->lock, flags);
228 return ret;
231 EXPORT_SYMBOL(__mod_timer);
234 * add_timer_on - start a timer on a particular CPU
235 * @timer: the timer to be added
236 * @cpu: the CPU to start it on
238 * This is not very scalable on SMP. Double adds are not possible.
240 void add_timer_on(struct timer_list *timer, int cpu)
242 tvec_base_t *base = per_cpu(tvec_bases, cpu);
243 unsigned long flags;
245 BUG_ON(timer_pending(timer) || !timer->function);
246 spin_lock_irqsave(&base->lock, flags);
247 timer->base = base;
248 internal_add_timer(base, timer);
249 spin_unlock_irqrestore(&base->lock, flags);
254 * mod_timer - modify a timer's timeout
255 * @timer: the timer to be modified
256 * @expires: new timeout in jiffies
258 * mod_timer is a more efficient way to update the expire field of an
259 * active timer (if the timer is inactive it will be activated)
261 * mod_timer(timer, expires) is equivalent to:
263 * del_timer(timer); timer->expires = expires; add_timer(timer);
265 * Note that if there are multiple unserialized concurrent users of the
266 * same timer, then mod_timer() is the only safe way to modify the timeout,
267 * since add_timer() cannot modify an already running timer.
269 * The function returns whether it has modified a pending timer or not.
270 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
271 * active timer returns 1.)
273 int mod_timer(struct timer_list *timer, unsigned long expires)
275 BUG_ON(!timer->function);
278 * This is a common optimization triggered by the
279 * networking code - if the timer is re-modified
280 * to be the same thing then just return:
282 if (timer->expires == expires && timer_pending(timer))
283 return 1;
285 return __mod_timer(timer, expires);
288 EXPORT_SYMBOL(mod_timer);
291 * del_timer - deactive a timer.
292 * @timer: the timer to be deactivated
294 * del_timer() deactivates a timer - this works on both active and inactive
295 * timers.
297 * The function returns whether it has deactivated a pending timer or not.
298 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
299 * active timer returns 1.)
301 int del_timer(struct timer_list *timer)
303 tvec_base_t *base;
304 unsigned long flags;
305 int ret = 0;
307 if (timer_pending(timer)) {
308 base = lock_timer_base(timer, &flags);
309 if (timer_pending(timer)) {
310 detach_timer(timer, 1);
311 ret = 1;
313 spin_unlock_irqrestore(&base->lock, flags);
316 return ret;
319 EXPORT_SYMBOL(del_timer);
321 #ifdef CONFIG_SMP
323 * try_to_del_timer_sync - Try to deactivate a timer
324 * @timer: timer do del
326 * This function tries to deactivate a timer. Upon successful (ret >= 0)
327 * exit the timer is not queued and the handler is not running on any CPU.
329 * It must not be called from interrupt contexts.
331 int try_to_del_timer_sync(struct timer_list *timer)
333 tvec_base_t *base;
334 unsigned long flags;
335 int ret = -1;
337 base = lock_timer_base(timer, &flags);
339 if (base->running_timer == timer)
340 goto out;
342 ret = 0;
343 if (timer_pending(timer)) {
344 detach_timer(timer, 1);
345 ret = 1;
347 out:
348 spin_unlock_irqrestore(&base->lock, flags);
350 return ret;
354 * del_timer_sync - deactivate a timer and wait for the handler to finish.
355 * @timer: the timer to be deactivated
357 * This function only differs from del_timer() on SMP: besides deactivating
358 * the timer it also makes sure the handler has finished executing on other
359 * CPUs.
361 * Synchronization rules: callers must prevent restarting of the timer,
362 * otherwise this function is meaningless. It must not be called from
363 * interrupt contexts. The caller must not hold locks which would prevent
364 * completion of the timer's handler. The timer's handler must not call
365 * add_timer_on(). Upon exit the timer is not queued and the handler is
366 * not running on any CPU.
368 * The function returns whether it has deactivated a pending timer or not.
370 int del_timer_sync(struct timer_list *timer)
372 for (;;) {
373 int ret = try_to_del_timer_sync(timer);
374 if (ret >= 0)
375 return ret;
376 cpu_relax();
380 EXPORT_SYMBOL(del_timer_sync);
381 #endif
383 static int cascade(tvec_base_t *base, tvec_t *tv, int index)
385 /* cascade all the timers from tv up one level */
386 struct timer_list *timer, *tmp;
387 struct list_head tv_list;
389 list_replace_init(tv->vec + index, &tv_list);
392 * We are removing _all_ timers from the list, so we
393 * don't have to detach them individually.
395 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
396 BUG_ON(timer->base != base);
397 internal_add_timer(base, timer);
400 return index;
403 #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
406 * __run_timers - run all expired timers (if any) on this CPU.
407 * @base: the timer vector to be processed.
409 * This function cascades all vectors and executes all expired timer
410 * vectors.
412 static inline void __run_timers(tvec_base_t *base)
414 struct timer_list *timer;
416 spin_lock_irq(&base->lock);
417 while (time_after_eq(jiffies, base->timer_jiffies)) {
418 struct list_head work_list;
419 struct list_head *head = &work_list;
420 int index = base->timer_jiffies & TVR_MASK;
423 * Cascade timers:
425 if (!index &&
426 (!cascade(base, &base->tv2, INDEX(0))) &&
427 (!cascade(base, &base->tv3, INDEX(1))) &&
428 !cascade(base, &base->tv4, INDEX(2)))
429 cascade(base, &base->tv5, INDEX(3));
430 ++base->timer_jiffies;
431 list_replace_init(base->tv1.vec + index, &work_list);
432 while (!list_empty(head)) {
433 void (*fn)(unsigned long);
434 unsigned long data;
436 timer = list_entry(head->next,struct timer_list,entry);
437 fn = timer->function;
438 data = timer->data;
440 set_running_timer(base, timer);
441 detach_timer(timer, 1);
442 spin_unlock_irq(&base->lock);
444 int preempt_count = preempt_count();
445 fn(data);
446 if (preempt_count != preempt_count()) {
447 printk(KERN_WARNING "huh, entered %p "
448 "with preempt_count %08x, exited"
449 " with %08x?\n",
450 fn, preempt_count,
451 preempt_count());
452 BUG();
455 spin_lock_irq(&base->lock);
458 set_running_timer(base, NULL);
459 spin_unlock_irq(&base->lock);
462 #ifdef CONFIG_NO_IDLE_HZ
464 * Find out when the next timer event is due to happen. This
465 * is used on S/390 to stop all activity when a cpus is idle.
466 * This functions needs to be called disabled.
468 unsigned long next_timer_interrupt(void)
470 tvec_base_t *base;
471 struct list_head *list;
472 struct timer_list *nte;
473 unsigned long expires;
474 unsigned long hr_expires = MAX_JIFFY_OFFSET;
475 ktime_t hr_delta;
476 tvec_t *varray[4];
477 int i, j;
479 hr_delta = hrtimer_get_next_event();
480 if (hr_delta.tv64 != KTIME_MAX) {
481 struct timespec tsdelta;
482 tsdelta = ktime_to_timespec(hr_delta);
483 hr_expires = timespec_to_jiffies(&tsdelta);
484 if (hr_expires < 3)
485 return hr_expires + jiffies;
487 hr_expires += jiffies;
489 base = __get_cpu_var(tvec_bases);
490 spin_lock(&base->lock);
491 expires = base->timer_jiffies + (LONG_MAX >> 1);
492 list = NULL;
494 /* Look for timer events in tv1. */
495 j = base->timer_jiffies & TVR_MASK;
496 do {
497 list_for_each_entry(nte, base->tv1.vec + j, entry) {
498 expires = nte->expires;
499 if (j < (base->timer_jiffies & TVR_MASK))
500 list = base->tv2.vec + (INDEX(0));
501 goto found;
503 j = (j + 1) & TVR_MASK;
504 } while (j != (base->timer_jiffies & TVR_MASK));
506 /* Check tv2-tv5. */
507 varray[0] = &base->tv2;
508 varray[1] = &base->tv3;
509 varray[2] = &base->tv4;
510 varray[3] = &base->tv5;
511 for (i = 0; i < 4; i++) {
512 j = INDEX(i);
513 do {
514 if (list_empty(varray[i]->vec + j)) {
515 j = (j + 1) & TVN_MASK;
516 continue;
518 list_for_each_entry(nte, varray[i]->vec + j, entry)
519 if (time_before(nte->expires, expires))
520 expires = nte->expires;
521 if (j < (INDEX(i)) && i < 3)
522 list = varray[i + 1]->vec + (INDEX(i + 1));
523 goto found;
524 } while (j != (INDEX(i)));
526 found:
527 if (list) {
529 * The search wrapped. We need to look at the next list
530 * from next tv element that would cascade into tv element
531 * where we found the timer element.
533 list_for_each_entry(nte, list, entry) {
534 if (time_before(nte->expires, expires))
535 expires = nte->expires;
538 spin_unlock(&base->lock);
541 * It can happen that other CPUs service timer IRQs and increment
542 * jiffies, but we have not yet got a local timer tick to process
543 * the timer wheels. In that case, the expiry time can be before
544 * jiffies, but since the high-resolution timer here is relative to
545 * jiffies, the default expression when high-resolution timers are
546 * not active,
548 * time_before(MAX_JIFFY_OFFSET + jiffies, expires)
550 * would falsely evaluate to true. If that is the case, just
551 * return jiffies so that we can immediately fire the local timer
553 if (time_before(expires, jiffies))
554 return jiffies;
556 if (time_before(hr_expires, expires))
557 return hr_expires;
559 return expires;
561 #endif
563 /******************************************************************/
566 * The current time
567 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
568 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
569 * at zero at system boot time, so wall_to_monotonic will be negative,
570 * however, we will ALWAYS keep the tv_nsec part positive so we can use
571 * the usual normalization.
573 struct timespec xtime __attribute__ ((aligned (16)));
574 struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
576 EXPORT_SYMBOL(xtime);
579 /* XXX - all of this timekeeping code should be later moved to time.c */
580 #include <linux/clocksource.h>
581 static struct clocksource *clock; /* pointer to current clocksource */
583 #ifdef CONFIG_GENERIC_TIME
585 * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook
587 * private function, must hold xtime_lock lock when being
588 * called. Returns the number of nanoseconds since the
589 * last call to update_wall_time() (adjusted by NTP scaling)
591 static inline s64 __get_nsec_offset(void)
593 cycle_t cycle_now, cycle_delta;
594 s64 ns_offset;
596 /* read clocksource: */
597 cycle_now = clocksource_read(clock);
599 /* calculate the delta since the last update_wall_time: */
600 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
602 /* convert to nanoseconds: */
603 ns_offset = cyc2ns(clock, cycle_delta);
605 return ns_offset;
609 * __get_realtime_clock_ts - Returns the time of day in a timespec
610 * @ts: pointer to the timespec to be set
612 * Returns the time of day in a timespec. Used by
613 * do_gettimeofday() and get_realtime_clock_ts().
615 static inline void __get_realtime_clock_ts(struct timespec *ts)
617 unsigned long seq;
618 s64 nsecs;
620 do {
621 seq = read_seqbegin(&xtime_lock);
623 *ts = xtime;
624 nsecs = __get_nsec_offset();
626 } while (read_seqretry(&xtime_lock, seq));
628 timespec_add_ns(ts, nsecs);
632 * getnstimeofday - Returns the time of day in a timespec
633 * @ts: pointer to the timespec to be set
635 * Returns the time of day in a timespec.
637 void getnstimeofday(struct timespec *ts)
639 __get_realtime_clock_ts(ts);
642 EXPORT_SYMBOL(getnstimeofday);
645 * do_gettimeofday - Returns the time of day in a timeval
646 * @tv: pointer to the timeval to be set
648 * NOTE: Users should be converted to using get_realtime_clock_ts()
650 void do_gettimeofday(struct timeval *tv)
652 struct timespec now;
654 __get_realtime_clock_ts(&now);
655 tv->tv_sec = now.tv_sec;
656 tv->tv_usec = now.tv_nsec/1000;
659 EXPORT_SYMBOL(do_gettimeofday);
661 * do_settimeofday - Sets the time of day
662 * @tv: pointer to the timespec variable containing the new time
664 * Sets the time of day to the new time and update NTP and notify hrtimers
666 int do_settimeofday(struct timespec *tv)
668 unsigned long flags;
669 time_t wtm_sec, sec = tv->tv_sec;
670 long wtm_nsec, nsec = tv->tv_nsec;
672 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
673 return -EINVAL;
675 write_seqlock_irqsave(&xtime_lock, flags);
677 nsec -= __get_nsec_offset();
679 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
680 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
682 set_normalized_timespec(&xtime, sec, nsec);
683 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
685 clock->error = 0;
686 ntp_clear();
688 write_sequnlock_irqrestore(&xtime_lock, flags);
690 /* signal hrtimers about time change */
691 clock_was_set();
693 return 0;
696 EXPORT_SYMBOL(do_settimeofday);
699 * change_clocksource - Swaps clocksources if a new one is available
701 * Accumulates current time interval and initializes new clocksource
703 static int change_clocksource(void)
705 struct clocksource *new;
706 cycle_t now;
707 u64 nsec;
708 new = clocksource_get_next();
709 if (clock != new) {
710 now = clocksource_read(new);
711 nsec = __get_nsec_offset();
712 timespec_add_ns(&xtime, nsec);
714 clock = new;
715 clock->cycle_last = now;
716 printk(KERN_INFO "Time: %s clocksource has been installed.\n",
717 clock->name);
718 return 1;
719 } else if (clock->update_callback) {
720 return clock->update_callback();
722 return 0;
724 #else
725 #define change_clocksource() (0)
726 #endif
729 * timeofday_is_continuous - check to see if timekeeping is free running
731 int timekeeping_is_continuous(void)
733 unsigned long seq;
734 int ret;
736 do {
737 seq = read_seqbegin(&xtime_lock);
739 ret = clock->is_continuous;
741 } while (read_seqretry(&xtime_lock, seq));
743 return ret;
747 * timekeeping_init - Initializes the clocksource and common timekeeping values
749 void __init timekeeping_init(void)
751 unsigned long flags;
753 write_seqlock_irqsave(&xtime_lock, flags);
755 ntp_clear();
757 clock = clocksource_get_next();
758 clocksource_calculate_interval(clock, tick_nsec);
759 clock->cycle_last = clocksource_read(clock);
761 write_sequnlock_irqrestore(&xtime_lock, flags);
765 static int timekeeping_suspended;
767 * timekeeping_resume - Resumes the generic timekeeping subsystem.
768 * @dev: unused
770 * This is for the generic clocksource timekeeping.
771 * xtime/wall_to_monotonic/jiffies/etc are
772 * still managed by arch specific suspend/resume code.
774 static int timekeeping_resume(struct sys_device *dev)
776 unsigned long flags;
778 write_seqlock_irqsave(&xtime_lock, flags);
779 /* restart the last cycle value */
780 clock->cycle_last = clocksource_read(clock);
781 clock->error = 0;
782 timekeeping_suspended = 0;
783 write_sequnlock_irqrestore(&xtime_lock, flags);
784 return 0;
787 static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
789 unsigned long flags;
791 write_seqlock_irqsave(&xtime_lock, flags);
792 timekeeping_suspended = 1;
793 write_sequnlock_irqrestore(&xtime_lock, flags);
794 return 0;
797 /* sysfs resume/suspend bits for timekeeping */
798 static struct sysdev_class timekeeping_sysclass = {
799 .resume = timekeeping_resume,
800 .suspend = timekeeping_suspend,
801 set_kset_name("timekeeping"),
804 static struct sys_device device_timer = {
805 .id = 0,
806 .cls = &timekeeping_sysclass,
809 static int __init timekeeping_init_device(void)
811 int error = sysdev_class_register(&timekeeping_sysclass);
812 if (!error)
813 error = sysdev_register(&device_timer);
814 return error;
817 device_initcall(timekeeping_init_device);
820 * If the error is already larger, we look ahead even further
821 * to compensate for late or lost adjustments.
823 static __always_inline int clocksource_bigadjust(s64 error, s64 *interval, s64 *offset)
825 s64 tick_error, i;
826 u32 look_ahead, adj;
827 s32 error2, mult;
830 * Use the current error value to determine how much to look ahead.
831 * The larger the error the slower we adjust for it to avoid problems
832 * with losing too many ticks, otherwise we would overadjust and
833 * produce an even larger error. The smaller the adjustment the
834 * faster we try to adjust for it, as lost ticks can do less harm
835 * here. This is tuned so that an error of about 1 msec is adusted
836 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
838 error2 = clock->error >> (TICK_LENGTH_SHIFT + 22 - 2 * SHIFT_HZ);
839 error2 = abs(error2);
840 for (look_ahead = 0; error2 > 0; look_ahead++)
841 error2 >>= 2;
844 * Now calculate the error in (1 << look_ahead) ticks, but first
845 * remove the single look ahead already included in the error.
847 tick_error = current_tick_length() >> (TICK_LENGTH_SHIFT - clock->shift + 1);
848 tick_error -= clock->xtime_interval >> 1;
849 error = ((error - tick_error) >> look_ahead) + tick_error;
851 /* Finally calculate the adjustment shift value. */
852 i = *interval;
853 mult = 1;
854 if (error < 0) {
855 error = -error;
856 *interval = -*interval;
857 *offset = -*offset;
858 mult = -1;
860 for (adj = 0; error > i; adj++)
861 error >>= 1;
863 *interval <<= adj;
864 *offset <<= adj;
865 return mult << adj;
869 * Adjust the multiplier to reduce the error value,
870 * this is optimized for the most common adjustments of -1,0,1,
871 * for other values we can do a bit more work.
873 static void clocksource_adjust(struct clocksource *clock, s64 offset)
875 s64 error, interval = clock->cycle_interval;
876 int adj;
878 error = clock->error >> (TICK_LENGTH_SHIFT - clock->shift - 1);
879 if (error > interval) {
880 error >>= 2;
881 if (likely(error <= interval))
882 adj = 1;
883 else
884 adj = clocksource_bigadjust(error, &interval, &offset);
885 } else if (error < -interval) {
886 error >>= 2;
887 if (likely(error >= -interval)) {
888 adj = -1;
889 interval = -interval;
890 offset = -offset;
891 } else
892 adj = clocksource_bigadjust(error, &interval, &offset);
893 } else
894 return;
896 clock->mult += adj;
897 clock->xtime_interval += interval;
898 clock->xtime_nsec -= offset;
899 clock->error -= (interval - offset) << (TICK_LENGTH_SHIFT - clock->shift);
903 * update_wall_time - Uses the current clocksource to increment the wall time
905 * Called from the timer interrupt, must hold a write on xtime_lock.
907 static void update_wall_time(void)
909 cycle_t offset;
911 /* Make sure we're fully resumed: */
912 if (unlikely(timekeeping_suspended))
913 return;
915 #ifdef CONFIG_GENERIC_TIME
916 offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
917 #else
918 offset = clock->cycle_interval;
919 #endif
920 clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift;
922 /* normally this loop will run just once, however in the
923 * case of lost or late ticks, it will accumulate correctly.
925 while (offset >= clock->cycle_interval) {
926 /* accumulate one interval */
927 clock->xtime_nsec += clock->xtime_interval;
928 clock->cycle_last += clock->cycle_interval;
929 offset -= clock->cycle_interval;
931 if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
932 clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
933 xtime.tv_sec++;
934 second_overflow();
937 /* interpolator bits */
938 time_interpolator_update(clock->xtime_interval
939 >> clock->shift);
941 /* accumulate error between NTP and clock interval */
942 clock->error += current_tick_length();
943 clock->error -= clock->xtime_interval << (TICK_LENGTH_SHIFT - clock->shift);
946 /* correct the clock when NTP error is too big */
947 clocksource_adjust(clock, offset);
949 /* store full nanoseconds into xtime */
950 xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift;
951 clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
953 /* check to see if there is a new clocksource to use */
954 if (change_clocksource()) {
955 clock->error = 0;
956 clock->xtime_nsec = 0;
957 clocksource_calculate_interval(clock, tick_nsec);
962 * Called from the timer interrupt handler to charge one tick to the current
963 * process. user_tick is 1 if the tick is user time, 0 for system.
965 void update_process_times(int user_tick)
967 struct task_struct *p = current;
968 int cpu = smp_processor_id();
970 /* Note: this timer irq context must be accounted for as well. */
971 if (user_tick)
972 account_user_time(p, jiffies_to_cputime(1));
973 else
974 account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
975 run_local_timers();
976 if (rcu_pending(cpu))
977 rcu_check_callbacks(cpu, user_tick);
978 scheduler_tick();
979 run_posix_cpu_timers(p);
983 * Nr of active tasks - counted in fixed-point numbers
985 static unsigned long count_active_tasks(void)
987 return nr_active() * FIXED_1;
991 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
992 * imply that avenrun[] is the standard name for this kind of thing.
993 * Nothing else seems to be standardized: the fractional size etc
994 * all seem to differ on different machines.
996 * Requires xtime_lock to access.
998 unsigned long avenrun[3];
1000 EXPORT_SYMBOL(avenrun);
1003 * calc_load - given tick count, update the avenrun load estimates.
1004 * This is called while holding a write_lock on xtime_lock.
1006 static inline void calc_load(unsigned long ticks)
1008 unsigned long active_tasks; /* fixed-point */
1009 static int count = LOAD_FREQ;
1011 active_tasks = count_active_tasks();
1012 for (count -= ticks; count < 0; count += LOAD_FREQ) {
1013 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
1014 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
1015 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
1020 * This read-write spinlock protects us from races in SMP while
1021 * playing with xtime and avenrun.
1023 #ifndef ARCH_HAVE_XTIME_LOCK
1024 __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
1026 EXPORT_SYMBOL(xtime_lock);
1027 #endif
1030 * This function runs timers and the timer-tq in bottom half context.
1032 static void run_timer_softirq(struct softirq_action *h)
1034 tvec_base_t *base = __get_cpu_var(tvec_bases);
1036 hrtimer_run_queues();
1037 if (time_after_eq(jiffies, base->timer_jiffies))
1038 __run_timers(base);
1042 * Called by the local, per-CPU timer interrupt on SMP.
1044 void run_local_timers(void)
1046 raise_softirq(TIMER_SOFTIRQ);
1047 softlockup_tick();
1051 * Called by the timer interrupt. xtime_lock must already be taken
1052 * by the timer IRQ!
1054 static inline void update_times(unsigned long ticks)
1056 update_wall_time();
1057 calc_load(ticks);
1061 * The 64-bit jiffies value is not atomic - you MUST NOT read it
1062 * without sampling the sequence number in xtime_lock.
1063 * jiffies is defined in the linker script...
1066 void do_timer(unsigned long ticks)
1068 jiffies_64 += ticks;
1069 #ifdef CONFIG_SNAPDOG
1070 snapdog_service();
1071 #endif
1072 update_times(ticks);
1075 #ifdef __ARCH_WANT_SYS_ALARM
1078 * For backwards compatibility? This can be done in libc so Alpha
1079 * and all newer ports shouldn't need it.
1081 asmlinkage unsigned long sys_alarm(unsigned int seconds)
1083 return alarm_setitimer(seconds);
1086 #endif
1088 #ifndef __alpha__
1091 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
1092 * should be moved into arch/i386 instead?
1096 * sys_getpid - return the thread group id of the current process
1098 * Note, despite the name, this returns the tgid not the pid. The tgid and
1099 * the pid are identical unless CLONE_THREAD was specified on clone() in
1100 * which case the tgid is the same in all threads of the same group.
1102 * This is SMP safe as current->tgid does not change.
1104 asmlinkage long sys_getpid(void)
1106 return current->tgid;
1110 * Accessing ->real_parent is not SMP-safe, it could
1111 * change from under us. However, we can use a stale
1112 * value of ->real_parent under rcu_read_lock(), see
1113 * release_task()->call_rcu(delayed_put_task_struct).
1115 asmlinkage long sys_getppid(void)
1117 int pid;
1119 rcu_read_lock();
1120 pid = rcu_dereference(current->real_parent)->tgid;
1121 rcu_read_unlock();
1123 return pid;
1126 asmlinkage long sys_getuid(void)
1128 /* Only we change this so SMP safe */
1129 return current->uid;
1132 asmlinkage long sys_geteuid(void)
1134 /* Only we change this so SMP safe */
1135 return current->euid;
1138 asmlinkage long sys_getgid(void)
1140 /* Only we change this so SMP safe */
1141 return current->gid;
1144 asmlinkage long sys_getegid(void)
1146 /* Only we change this so SMP safe */
1147 return current->egid;
1150 #endif
1152 static void process_timeout(unsigned long __data)
1154 wake_up_process((struct task_struct *)__data);
1158 * schedule_timeout - sleep until timeout
1159 * @timeout: timeout value in jiffies
1161 * Make the current task sleep until @timeout jiffies have
1162 * elapsed. The routine will return immediately unless
1163 * the current task state has been set (see set_current_state()).
1165 * You can set the task state as follows -
1167 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1168 * pass before the routine returns. The routine will return 0
1170 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1171 * delivered to the current task. In this case the remaining time
1172 * in jiffies will be returned, or 0 if the timer expired in time
1174 * The current task state is guaranteed to be TASK_RUNNING when this
1175 * routine returns.
1177 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1178 * the CPU away without a bound on the timeout. In this case the return
1179 * value will be %MAX_SCHEDULE_TIMEOUT.
1181 * In all cases the return value is guaranteed to be non-negative.
1183 fastcall signed long __sched schedule_timeout(signed long timeout)
1185 struct timer_list timer;
1186 unsigned long expire;
1188 switch (timeout)
1190 case MAX_SCHEDULE_TIMEOUT:
1192 * These two special cases are useful to be comfortable
1193 * in the caller. Nothing more. We could take
1194 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1195 * but I' d like to return a valid offset (>=0) to allow
1196 * the caller to do everything it want with the retval.
1198 schedule();
1199 goto out;
1200 default:
1202 * Another bit of PARANOID. Note that the retval will be
1203 * 0 since no piece of kernel is supposed to do a check
1204 * for a negative retval of schedule_timeout() (since it
1205 * should never happens anyway). You just have the printk()
1206 * that will tell you if something is gone wrong and where.
1208 if (timeout < 0)
1210 printk(KERN_ERR "schedule_timeout: wrong timeout "
1211 "value %lx from %p\n", timeout,
1212 __builtin_return_address(0));
1213 current->state = TASK_RUNNING;
1214 goto out;
1218 expire = timeout + jiffies;
1220 setup_timer(&timer, process_timeout, (unsigned long)current);
1221 __mod_timer(&timer, expire);
1222 schedule();
1223 del_singleshot_timer_sync(&timer);
1225 timeout = expire - jiffies;
1227 out:
1228 return timeout < 0 ? 0 : timeout;
1230 EXPORT_SYMBOL(schedule_timeout);
1233 * We can use __set_current_state() here because schedule_timeout() calls
1234 * schedule() unconditionally.
1236 signed long __sched schedule_timeout_interruptible(signed long timeout)
1238 __set_current_state(TASK_INTERRUPTIBLE);
1239 return schedule_timeout(timeout);
1241 EXPORT_SYMBOL(schedule_timeout_interruptible);
1243 signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1245 __set_current_state(TASK_UNINTERRUPTIBLE);
1246 return schedule_timeout(timeout);
1248 EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1250 /* Thread ID - the internal kernel "pid" */
1251 asmlinkage long sys_gettid(void)
1253 return current->pid;
1257 * sys_sysinfo - fill in sysinfo struct
1258 * @info: pointer to buffer to fill
1260 asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1262 struct sysinfo val;
1263 unsigned long mem_total, sav_total;
1264 unsigned int mem_unit, bitcount;
1265 unsigned long seq;
1267 memset((char *)&val, 0, sizeof(struct sysinfo));
1269 do {
1270 struct timespec tp;
1271 seq = read_seqbegin(&xtime_lock);
1274 * This is annoying. The below is the same thing
1275 * posix_get_clock_monotonic() does, but it wants to
1276 * take the lock which we want to cover the loads stuff
1277 * too.
1280 getnstimeofday(&tp);
1281 tp.tv_sec += wall_to_monotonic.tv_sec;
1282 tp.tv_nsec += wall_to_monotonic.tv_nsec;
1283 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1284 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1285 tp.tv_sec++;
1287 val.uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1289 val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1290 val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1291 val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
1293 val.procs = nr_threads;
1294 } while (read_seqretry(&xtime_lock, seq));
1296 si_meminfo(&val);
1297 si_swapinfo(&val);
1300 * If the sum of all the available memory (i.e. ram + swap)
1301 * is less than can be stored in a 32 bit unsigned long then
1302 * we can be binary compatible with 2.2.x kernels. If not,
1303 * well, in that case 2.2.x was broken anyways...
1305 * -Erik Andersen <andersee@debian.org>
1308 mem_total = val.totalram + val.totalswap;
1309 if (mem_total < val.totalram || mem_total < val.totalswap)
1310 goto out;
1311 bitcount = 0;
1312 mem_unit = val.mem_unit;
1313 while (mem_unit > 1) {
1314 bitcount++;
1315 mem_unit >>= 1;
1316 sav_total = mem_total;
1317 mem_total <<= 1;
1318 if (mem_total < sav_total)
1319 goto out;
1323 * If mem_total did not overflow, multiply all memory values by
1324 * val.mem_unit and set it to 1. This leaves things compatible
1325 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1326 * kernels...
1329 val.mem_unit = 1;
1330 val.totalram <<= bitcount;
1331 val.freeram <<= bitcount;
1332 val.sharedram <<= bitcount;
1333 val.bufferram <<= bitcount;
1334 val.totalswap <<= bitcount;
1335 val.freeswap <<= bitcount;
1336 val.totalhigh <<= bitcount;
1337 val.freehigh <<= bitcount;
1339 out:
1340 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1341 return -EFAULT;
1343 return 0;
1347 * lockdep: we want to track each per-CPU base as a separate lock-class,
1348 * but timer-bases are kmalloc()-ed, so we need to attach separate
1349 * keys to them:
1351 static struct lock_class_key base_lock_keys[NR_CPUS];
1353 static int __devinit init_timers_cpu(int cpu)
1355 int j;
1356 tvec_base_t *base;
1357 static char __devinitdata tvec_base_done[NR_CPUS];
1359 if (!tvec_base_done[cpu]) {
1360 static char boot_done;
1362 if (boot_done) {
1364 * The APs use this path later in boot
1366 base = kmalloc_node(sizeof(*base), GFP_KERNEL,
1367 cpu_to_node(cpu));
1368 if (!base)
1369 return -ENOMEM;
1370 memset(base, 0, sizeof(*base));
1371 per_cpu(tvec_bases, cpu) = base;
1372 } else {
1374 * This is for the boot CPU - we use compile-time
1375 * static initialisation because per-cpu memory isn't
1376 * ready yet and because the memory allocators are not
1377 * initialised either.
1379 boot_done = 1;
1380 base = &boot_tvec_bases;
1382 tvec_base_done[cpu] = 1;
1383 } else {
1384 base = per_cpu(tvec_bases, cpu);
1387 spin_lock_init(&base->lock);
1388 lockdep_set_class(&base->lock, base_lock_keys + cpu);
1390 for (j = 0; j < TVN_SIZE; j++) {
1391 INIT_LIST_HEAD(base->tv5.vec + j);
1392 INIT_LIST_HEAD(base->tv4.vec + j);
1393 INIT_LIST_HEAD(base->tv3.vec + j);
1394 INIT_LIST_HEAD(base->tv2.vec + j);
1396 for (j = 0; j < TVR_SIZE; j++)
1397 INIT_LIST_HEAD(base->tv1.vec + j);
1399 base->timer_jiffies = jiffies;
1400 return 0;
1403 #ifdef CONFIG_HOTPLUG_CPU
1404 static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
1406 struct timer_list *timer;
1408 while (!list_empty(head)) {
1409 timer = list_entry(head->next, struct timer_list, entry);
1410 detach_timer(timer, 0);
1411 timer->base = new_base;
1412 internal_add_timer(new_base, timer);
1416 static void __devinit migrate_timers(int cpu)
1418 tvec_base_t *old_base;
1419 tvec_base_t *new_base;
1420 int i;
1422 BUG_ON(cpu_online(cpu));
1423 old_base = per_cpu(tvec_bases, cpu);
1424 new_base = get_cpu_var(tvec_bases);
1426 local_irq_disable();
1427 spin_lock(&new_base->lock);
1428 spin_lock(&old_base->lock);
1430 BUG_ON(old_base->running_timer);
1432 for (i = 0; i < TVR_SIZE; i++)
1433 migrate_timer_list(new_base, old_base->tv1.vec + i);
1434 for (i = 0; i < TVN_SIZE; i++) {
1435 migrate_timer_list(new_base, old_base->tv2.vec + i);
1436 migrate_timer_list(new_base, old_base->tv3.vec + i);
1437 migrate_timer_list(new_base, old_base->tv4.vec + i);
1438 migrate_timer_list(new_base, old_base->tv5.vec + i);
1441 spin_unlock(&old_base->lock);
1442 spin_unlock(&new_base->lock);
1443 local_irq_enable();
1444 put_cpu_var(tvec_bases);
1446 #endif /* CONFIG_HOTPLUG_CPU */
1448 static int __cpuinit timer_cpu_notify(struct notifier_block *self,
1449 unsigned long action, void *hcpu)
1451 long cpu = (long)hcpu;
1452 switch(action) {
1453 case CPU_UP_PREPARE:
1454 if (init_timers_cpu(cpu) < 0)
1455 return NOTIFY_BAD;
1456 break;
1457 #ifdef CONFIG_HOTPLUG_CPU
1458 case CPU_DEAD:
1459 migrate_timers(cpu);
1460 break;
1461 #endif
1462 default:
1463 break;
1465 return NOTIFY_OK;
1468 static struct notifier_block __cpuinitdata timers_nb = {
1469 .notifier_call = timer_cpu_notify,
1473 void __init init_timers(void)
1475 int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1476 (void *)(long)smp_processor_id());
1478 BUG_ON(err == NOTIFY_BAD);
1479 register_cpu_notifier(&timers_nb);
1480 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1483 #ifdef CONFIG_TIME_INTERPOLATION
1485 struct time_interpolator *time_interpolator __read_mostly;
1486 static struct time_interpolator *time_interpolator_list __read_mostly;
1487 static DEFINE_SPINLOCK(time_interpolator_lock);
1489 static inline u64 time_interpolator_get_cycles(unsigned int src)
1491 unsigned long (*x)(void);
1493 switch (src)
1495 case TIME_SOURCE_FUNCTION:
1496 x = time_interpolator->addr;
1497 return x();
1499 case TIME_SOURCE_MMIO64 :
1500 return readq_relaxed((void __iomem *)time_interpolator->addr);
1502 case TIME_SOURCE_MMIO32 :
1503 return readl_relaxed((void __iomem *)time_interpolator->addr);
1505 default: return get_cycles();
1509 static inline u64 time_interpolator_get_counter(int writelock)
1511 unsigned int src = time_interpolator->source;
1513 if (time_interpolator->jitter)
1515 u64 lcycle;
1516 u64 now;
1518 do {
1519 lcycle = time_interpolator->last_cycle;
1520 now = time_interpolator_get_cycles(src);
1521 if (lcycle && time_after(lcycle, now))
1522 return lcycle;
1524 /* When holding the xtime write lock, there's no need
1525 * to add the overhead of the cmpxchg. Readers are
1526 * force to retry until the write lock is released.
1528 if (writelock) {
1529 time_interpolator->last_cycle = now;
1530 return now;
1532 /* Keep track of the last timer value returned. The use of cmpxchg here
1533 * will cause contention in an SMP environment.
1535 } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
1536 return now;
1538 else
1539 return time_interpolator_get_cycles(src);
1542 void time_interpolator_reset(void)
1544 time_interpolator->offset = 0;
1545 time_interpolator->last_counter = time_interpolator_get_counter(1);
1548 #define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
1550 unsigned long time_interpolator_get_offset(void)
1552 /* If we do not have a time interpolator set up then just return zero */
1553 if (!time_interpolator)
1554 return 0;
1556 return time_interpolator->offset +
1557 GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
1560 #define INTERPOLATOR_ADJUST 65536
1561 #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
1563 void time_interpolator_update(long delta_nsec)
1565 u64 counter;
1566 unsigned long offset;
1568 /* If there is no time interpolator set up then do nothing */
1569 if (!time_interpolator)
1570 return;
1573 * The interpolator compensates for late ticks by accumulating the late
1574 * time in time_interpolator->offset. A tick earlier than expected will
1575 * lead to a reset of the offset and a corresponding jump of the clock
1576 * forward. Again this only works if the interpolator clock is running
1577 * slightly slower than the regular clock and the tuning logic insures
1578 * that.
1581 counter = time_interpolator_get_counter(1);
1582 offset = time_interpolator->offset +
1583 GET_TI_NSECS(counter, time_interpolator);
1585 if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
1586 time_interpolator->offset = offset - delta_nsec;
1587 else {
1588 time_interpolator->skips++;
1589 time_interpolator->ns_skipped += delta_nsec - offset;
1590 time_interpolator->offset = 0;
1592 time_interpolator->last_counter = counter;
1594 /* Tuning logic for time interpolator invoked every minute or so.
1595 * Decrease interpolator clock speed if no skips occurred and an offset is carried.
1596 * Increase interpolator clock speed if we skip too much time.
1598 if (jiffies % INTERPOLATOR_ADJUST == 0)
1600 if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec)
1601 time_interpolator->nsec_per_cyc--;
1602 if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
1603 time_interpolator->nsec_per_cyc++;
1604 time_interpolator->skips = 0;
1605 time_interpolator->ns_skipped = 0;
1609 static inline int
1610 is_better_time_interpolator(struct time_interpolator *new)
1612 if (!time_interpolator)
1613 return 1;
1614 return new->frequency > 2*time_interpolator->frequency ||
1615 (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
1618 void
1619 register_time_interpolator(struct time_interpolator *ti)
1621 unsigned long flags;
1623 /* Sanity check */
1624 BUG_ON(ti->frequency == 0 || ti->mask == 0);
1626 ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
1627 spin_lock(&time_interpolator_lock);
1628 write_seqlock_irqsave(&xtime_lock, flags);
1629 if (is_better_time_interpolator(ti)) {
1630 time_interpolator = ti;
1631 time_interpolator_reset();
1633 write_sequnlock_irqrestore(&xtime_lock, flags);
1635 ti->next = time_interpolator_list;
1636 time_interpolator_list = ti;
1637 spin_unlock(&time_interpolator_lock);
1640 void
1641 unregister_time_interpolator(struct time_interpolator *ti)
1643 struct time_interpolator *curr, **prev;
1644 unsigned long flags;
1646 spin_lock(&time_interpolator_lock);
1647 prev = &time_interpolator_list;
1648 for (curr = *prev; curr; curr = curr->next) {
1649 if (curr == ti) {
1650 *prev = curr->next;
1651 break;
1653 prev = &curr->next;
1656 write_seqlock_irqsave(&xtime_lock, flags);
1657 if (ti == time_interpolator) {
1658 /* we lost the best time-interpolator: */
1659 time_interpolator = NULL;
1660 /* find the next-best interpolator */
1661 for (curr = time_interpolator_list; curr; curr = curr->next)
1662 if (is_better_time_interpolator(curr))
1663 time_interpolator = curr;
1664 time_interpolator_reset();
1666 write_sequnlock_irqrestore(&xtime_lock, flags);
1667 spin_unlock(&time_interpolator_lock);
1669 #endif /* CONFIG_TIME_INTERPOLATION */
1672 * msleep - sleep safely even with waitqueue interruptions
1673 * @msecs: Time in milliseconds to sleep for
1675 void msleep(unsigned int msecs)
1677 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1679 while (timeout)
1680 timeout = schedule_timeout_uninterruptible(timeout);
1683 EXPORT_SYMBOL(msleep);
1686 * msleep_interruptible - sleep waiting for signals
1687 * @msecs: Time in milliseconds to sleep for
1689 unsigned long msleep_interruptible(unsigned int msecs)
1691 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1693 while (timeout && !signal_pending(current))
1694 timeout = schedule_timeout_interruptible(timeout);
1695 return jiffies_to_msecs(timeout);
1698 EXPORT_SYMBOL(msleep_interruptible);