Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  *  linux/kernel/timer.c
  3  *
  4  *  Kernel internal timers, 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/pid_namespace.h>
 30 #include <linux/notifier.h>
 31 #include <linux/thread_info.h>
 32 #include <linux/time.h>
 33 #include <linux/jiffies.h>
 34 #include <linux/posix-timers.h>
 35 #include <linux/cpu.h>
 36 #include <linux/syscalls.h>
 37 #include <linux/kallsyms.h>
 38 #include <linux/delay.h>
 39 #include <linux/tick.h>
 40 #include <linux/kallsyms.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 struct tvec {
 63         struct list_head vec[TVN_SIZE];
 64 };
 65 
 66 struct tvec_root {
 67         struct list_head vec[TVR_SIZE];
 68 };
 69 
 70 struct tvec_base {
 71         spinlock_t lock;
 72         struct timer_list *running_timer;
 73         wait_queue_head_t wait_for_running_timer;
 74         unsigned long timer_jiffies;
 75         struct tvec_root tv1;
 76         struct tvec tv2;
 77         struct tvec tv3;
 78         struct tvec tv4;
 79         struct tvec tv5;
 80 } ____cacheline_aligned;
 81 
 82 struct tvec_base boot_tvec_bases;
 83 EXPORT_SYMBOL(boot_tvec_bases);
 84 static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
 85 
 86 /*
 87  * Note that all tvec_bases are 2 byte aligned and lower bit of
 88  * base in timer_list is guaranteed to be zero. Use the LSB for
 89  * the new flag to indicate whether the timer is deferrable
 90  */
 91 #define TBASE_DEFERRABLE_FLAG           (0x1)
 92 
 93 /* Functions below help us manage 'deferrable' flag */
 94 static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
 95 {
 96         return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);
 97 }
 98 
 99 static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
100 {
101         return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
102 }
103 
104 static inline void timer_set_deferrable(struct timer_list *timer)
105 {
106         timer->base = ((struct tvec_base *)((unsigned long)(timer->base) |
107                                        TBASE_DEFERRABLE_FLAG));
108 }
109 
110 static inline void
111 timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
112 {
113         timer->base = (struct tvec_base *)((unsigned long)(new_base) |
114                                       tbase_get_deferrable(timer->base));
115 }
116 
117 /**
118  * __round_jiffies - function to round jiffies to a full second
119  * @j: the time in (absolute) jiffies that should be rounded
120  * @cpu: the processor number on which the timeout will happen
121  *
122  * __round_jiffies() rounds an absolute time in the future (in jiffies)
123  * up or down to (approximately) full seconds. This is useful for timers
124  * for which the exact time they fire does not matter too much, as long as
125  * they fire approximately every X seconds.
126  *
127  * By rounding these timers to whole seconds, all such timers will fire
128  * at the same time, rather than at various times spread out. The goal
129  * of this is to have the CPU wake up less, which saves power.
130  *
131  * The exact rounding is skewed for each processor to avoid all
132  * processors firing at the exact same time, which could lead
133  * to lock contention or spurious cache line bouncing.
134  *
135  * The return value is the rounded version of the @j parameter.
136  */
137 unsigned long __round_jiffies(unsigned long j, int cpu)
138 {
139         int rem;
140         unsigned long original = j;
141 
142         /*
143          * We don't want all cpus firing their timers at once hitting the
144          * same lock or cachelines, so we skew each extra cpu with an extra
145          * 3 jiffies. This 3 jiffies came originally from the mm/ code which
146          * already did this.
147          * The skew is done by adding 3*cpunr, then round, then subtract this
148          * extra offset again.
149          */
150         j += cpu * 3;
151 
152         rem = j % HZ;
153 
154         /*
155          * If the target jiffie is just after a whole second (which can happen
156          * due to delays of the timer irq, long irq off times etc etc) then
157          * we should round down to the whole second, not up. Use 1/4th second
158          * as cutoff for this rounding as an extreme upper bound for this.
159          */
160         if (rem < HZ/4) /* round down */
161                 j = j - rem;
162         else /* round up */
163                 j = j - rem + HZ;
164 
165         /* now that we have rounded, subtract the extra skew again */
166         j -= cpu * 3;
167 
168         if (j <= jiffies) /* rounding ate our timeout entirely; */
169                 return original;
170         return j;
171 }
172 EXPORT_SYMBOL_GPL(__round_jiffies);
173 
174 /**
175  * __round_jiffies_relative - function to round jiffies to a full second
176  * @j: the time in (relative) jiffies that should be rounded
177  * @cpu: the processor number on which the timeout will happen
178  *
179  * __round_jiffies_relative() rounds a time delta  in the future (in jiffies)
180  * up or down to (approximately) full seconds. This is useful for timers
181  * for which the exact time they fire does not matter too much, as long as
182  * they fire approximately every X seconds.
183  *
184  * By rounding these timers to whole seconds, all such timers will fire
185  * at the same time, rather than at various times spread out. The goal
186  * of this is to have the CPU wake up less, which saves power.
187  *
188  * The exact rounding is skewed for each processor to avoid all
189  * processors firing at the exact same time, which could lead
190  * to lock contention or spurious cache line bouncing.
191  *
192  * The return value is the rounded version of the @j parameter.
193  */
194 unsigned long __round_jiffies_relative(unsigned long j, int cpu)
195 {
196         /*
197          * In theory the following code can skip a jiffy in case jiffies
198          * increments right between the addition and the later subtraction.
199          * However since the entire point of this function is to use approximate
200          * timeouts, it's entirely ok to not handle that.
201          */
202         return  __round_jiffies(j + jiffies, cpu) - jiffies;
203 }
204 EXPORT_SYMBOL_GPL(__round_jiffies_relative);
205 
206 /**
207  * round_jiffies - function to round jiffies to a full second
208  * @j: the time in (absolute) jiffies that should be rounded
209  *
210  * round_jiffies() rounds an absolute time in the future (in jiffies)
211  * up or down to (approximately) full seconds. This is useful for timers
212  * for which the exact time they fire does not matter too much, as long as
213  * they fire approximately every X seconds.
214  *
215  * By rounding these timers to whole seconds, all such timers will fire
216  * at the same time, rather than at various times spread out. The goal
217  * of this is to have the CPU wake up less, which saves power.
218  *
219  * The return value is the rounded version of the @j parameter.
220  */
221 unsigned long round_jiffies(unsigned long j)
222 {
223         return __round_jiffies(j, raw_smp_processor_id());
224 }
225 EXPORT_SYMBOL_GPL(round_jiffies);
226 
227 /**
228  * round_jiffies_relative - function to round jiffies to a full second
229  * @j: the time in (relative) jiffies that should be rounded
230  *
231  * round_jiffies_relative() rounds a time delta  in the future (in jiffies)
232  * up or down to (approximately) full seconds. This is useful for timers
233  * for which the exact time they fire does not matter too much, as long as
234  * they fire approximately every X seconds.
235  *
236  * By rounding these timers to whole seconds, all such timers will fire
237  * at the same time, rather than at various times spread out. The goal
238  * of this is to have the CPU wake up less, which saves power.
239  *
240  * The return value is the rounded version of the @j parameter.
241  */
242 unsigned long round_jiffies_relative(unsigned long j)
243 {
244         return __round_jiffies_relative(j, raw_smp_processor_id());
245 }
246 EXPORT_SYMBOL_GPL(round_jiffies_relative);
247 
248 
249 static inline void set_running_timer(struct tvec_base *base,
250                                         struct timer_list *timer)
251 {
252         base->running_timer = timer;
253 }
254 
255 static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
256 {
257         unsigned long expires = timer->expires;
258         unsigned long idx = expires - base->timer_jiffies;
259         struct list_head *vec;
260 
261         if (idx < TVR_SIZE) {
262                 int i = expires & TVR_MASK;
263                 vec = base->tv1.vec + i;
264         } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
265                 int i = (expires >> TVR_BITS) & TVN_MASK;
266                 vec = base->tv2.vec + i;
267         } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
268                 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
269                 vec = base->tv3.vec + i;
270         } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
271                 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
272                 vec = base->tv4.vec + i;
273         } else if ((signed long) idx < 0) {
274                 /*
275                  * Can happen if you add a timer with expires == jiffies,
276                  * or you set a timer to go off in the past
277                  */
278                 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
279         } else {
280                 int i;
281                 /* If the timeout is larger than 0xffffffff on 64-bit
282                  * architectures then we use the maximum timeout:
283                  */
284                 if (idx > 0xffffffffUL) {
285                         idx = 0xffffffffUL;
286                         expires = idx + base->timer_jiffies;
287                 }
288                 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
289                 vec = base->tv5.vec + i;
290         }
291         /*
292          * Timers are FIFO:
293          */
294         list_add_tail(&timer->entry, vec);
295 }
296 
297 #ifdef CONFIG_TIMER_STATS
298 void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
299 {
300         if (timer->start_site)
301                 return;
302 
303         timer->start_site = addr;
304         memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
305         timer->start_pid = current->pid;
306 }
307 
308 static void timer_stats_account_timer(struct timer_list *timer)
309 {
310         unsigned int flag = 0;
311 
312         if (unlikely(tbase_get_deferrable(timer->base)))
313                 flag |= TIMER_STATS_FLAG_DEFERRABLE;
314 
315         timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
316                                  timer->function, timer->start_comm, flag);
317 }
318 
319 #else
320 static void timer_stats_account_timer(struct timer_list *timer) {}
321 #endif
322 
323 /**
324  * init_timer - initialize a timer.
325  * @timer: the timer to be initialized
326  *
327  * init_timer() must be done to a timer prior calling *any* of the
328  * other timer functions.
329  */
330 void init_timer(struct timer_list *timer)
331 {
332         timer->entry.next = NULL;
333         timer->base = __raw_get_cpu_var(tvec_bases);
334 #ifdef CONFIG_TIMER_STATS
335         timer->start_site = NULL;
336         timer->start_pid = -1;
337         memset(timer->start_comm, 0, TASK_COMM_LEN);
338 #endif
339 }
340 EXPORT_SYMBOL(init_timer);
341 
342 void init_timer_deferrable(struct timer_list *timer)
343 {
344         init_timer(timer);
345         timer_set_deferrable(timer);
346 }
347 EXPORT_SYMBOL(init_timer_deferrable);
348 
349 static inline void detach_timer(struct timer_list *timer,
350                                 int clear_pending)
351 {
352         struct list_head *entry = &timer->entry;
353 
354         __list_del(entry->prev, entry->next);
355         if (clear_pending)
356                 entry->next = NULL;
357         entry->prev = LIST_POISON2;
358 }
359 
360 /*
361  * We are using hashed locking: holding per_cpu(tvec_bases).lock
362  * means that all timers which are tied to this base via timer->base are
363  * locked, and the base itself is locked too.
364  *
365  * So __run_timers/migrate_timers can safely modify all timers which could
366  * be found on ->tvX lists.
367  *
368  * When the timer's base is locked, and the timer removed from list, it is
369  * possible to set timer->base = NULL and drop the lock: the timer remains
370  * locked.
371  */
372 static struct tvec_base *lock_timer_base(struct timer_list *timer,
373                                         unsigned long *flags)
374         __acquires(timer->base->lock)
375 {
376         struct tvec_base *base;
377 
378         for (;;) {
379                 struct tvec_base *prelock_base = timer->base;
380                 base = tbase_get_base(prelock_base);
381                 if (likely(base != NULL)) {
382                         spin_lock_irqsave(&base->lock, *flags);
383                         if (likely(prelock_base == timer->base))
384                                 return base;
385                         /* The timer has migrated to another CPU */
386                         spin_unlock_irqrestore(&base->lock, *flags);
387                 }
388                 cpu_relax();
389         }
390 }
391 
392 int __mod_timer(struct timer_list *timer, unsigned long expires)
393 {
394         struct tvec_base *base, *new_base;
395         unsigned long flags;
396         int ret = 0, cpu;
397 
398         timer_stats_timer_set_start_info(timer);
399         BUG_ON(!timer->function);
400 
401         base = lock_timer_base(timer, &flags);
402 
403         if (timer_pending(timer)) {
404                 detach_timer(timer, 0);
405                 ret = 1;
406         }
407 
408         cpu = raw_smp_processor_id();
409         new_base = per_cpu(tvec_bases, cpu);
410 
411         if (base != new_base) {
412                 /*
413                  * We are trying to schedule the timer on the local CPU.
414                  * However we can't change timer's base while it is running,
415                  * otherwise del_timer_sync() can't detect that the timer's
416                  * handler yet has not finished. This also guarantees that
417                  * the timer is serialized wrt itself.
418                  */
419                 if (likely(base->running_timer != timer)) {
420                         /* See the comment in lock_timer_base() */
421                         timer_set_base(timer, NULL);
422                         spin_unlock(&base->lock);
423                         base = new_base;
424                         spin_lock(&base->lock);
425                         timer_set_base(timer, base);
426                 }
427         }
428 
429         timer->expires = expires;
430         internal_add_timer(base, timer);
431         spin_unlock_irqrestore(&base->lock, flags);
432 
433         return ret;
434 }
435 
436 EXPORT_SYMBOL(__mod_timer);
437 
438 /**
439  * add_timer_on - start a timer on a particular CPU
440  * @timer: the timer to be added
441  * @cpu: the CPU to start it on
442  *
443  * This is not very scalable on SMP. Double adds are not possible.
444  */
445 void add_timer_on(struct timer_list *timer, int cpu)
446 {
447         struct tvec_base *base = per_cpu(tvec_bases, cpu);
448         unsigned long flags;
449 
450         timer_stats_timer_set_start_info(timer);
451         BUG_ON(timer_pending(timer) || !timer->function);
452         spin_lock_irqsave(&base->lock, flags);
453         timer_set_base(timer, base);
454         internal_add_timer(base, timer);
455         /*
456          * Check whether the other CPU is idle and needs to be
457          * triggered to reevaluate the timer wheel when nohz is
458          * active. We are protected against the other CPU fiddling
459          * with the timer by holding the timer base lock. This also
460          * makes sure that a CPU on the way to idle can not evaluate
461          * the timer wheel.
462          */
463         wake_up_idle_cpu(cpu);
464         spin_unlock_irqrestore(&base->lock, flags);
465 }
466 
467 /*
468  * Wait for a running timer
469  */
470 void wait_for_running_timer(struct timer_list *timer)
471 {
472         struct tvec_base *base = timer->base;
473 
474         if (base->running_timer == timer)
475                 wait_event(base->wait_for_running_timer,
476                            base->running_timer != timer);
477 }
478 
479 /**
480  * mod_timer - modify a timer's timeout
481  * @timer: the timer to be modified
482  * @expires: new timeout in jiffies
483  *
484  * mod_timer() is a more efficient way to update the expire field of an
485  * active timer (if the timer is inactive it will be activated)
486  *
487  * mod_timer(timer, expires) is equivalent to:
488  *
489  *     del_timer(timer); timer->expires = expires; add_timer(timer);
490  *
491  * Note that if there are multiple unserialized concurrent users of the
492  * same timer, then mod_timer() is the only safe way to modify the timeout,
493  * since add_timer() cannot modify an already running timer.
494  *
495  * The function returns whether it has modified a pending timer or not.
496  * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
497  * active timer returns 1.)
498  */
499 int mod_timer(struct timer_list *timer, unsigned long expires)
500 {
501         BUG_ON(!timer->function);
502 
503         timer_stats_timer_set_start_info(timer);
504         /*
505          * This is a common optimization triggered by the
506          * networking code - if the timer is re-modified
507          * to be the same thing then just return:
508          */
509         if (timer->expires == expires && timer_pending(timer))
510                 return 1;
511 
512         return __mod_timer(timer, expires);
513 }
514 
515 EXPORT_SYMBOL(mod_timer);
516 
517 /**
518  * del_timer - deactive a timer.
519  * @timer: the timer to be deactivated
520  *
521  * del_timer() deactivates a timer - this works on both active and inactive
522  * timers.
523  *
524  * The function returns whether it has deactivated a pending timer or not.
525  * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
526  * active timer returns 1.)
527  */
528 int del_timer(struct timer_list *timer)
529 {
530         struct tvec_base *base;
531         unsigned long flags;
532         int ret = 0;
533 
534         timer_stats_timer_clear_start_info(timer);
535         if (timer_pending(timer)) {
536                 base = lock_timer_base(timer, &flags);
537                 if (timer_pending(timer)) {
538                         detach_timer(timer, 1);
539                         ret = 1;
540                 }
541                 spin_unlock_irqrestore(&base->lock, flags);
542         }
543 
544         return ret;
545 }
546 
547 EXPORT_SYMBOL(del_timer);
548 
549 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_SOFTIRQS)
550 /*
551  * This function checks whether a timer is active and not running on any
552  * CPU. Upon successful (ret >= 0) exit the timer is not queued and the
553  * handler is not running on any CPU.
554  *
555  * It must not be called from interrupt contexts.
556  */
557 int timer_pending_sync(struct timer_list *timer)
558 {
559         struct tvec_base *base;
560         unsigned long flags;
561         int ret = -1;
562 
563         base = lock_timer_base(timer, &flags);
564 
565         if (base->running_timer == timer)
566                 goto out;
567 
568         ret = 0;
569         if (timer_pending(timer))
570                 ret = 1;
571 out:
572         spin_unlock_irqrestore(&base->lock, flags);
573 
574         return ret;
575 }
576 
577 
578 /**
579  * try_to_del_timer_sync - Try to deactivate a timer
580  * @timer: timer do del
581  *
582  * This function tries to deactivate a timer. Upon successful (ret >= 0)
583  * exit the timer is not queued and the handler is not running on any CPU.
584  *
585  * It must not be called from interrupt contexts.
586  */
587 int try_to_del_timer_sync(struct timer_list *timer)
588 {
589         struct tvec_base *base;
590         unsigned long flags;
591         int ret = -1;
592 
593         base = lock_timer_base(timer, &flags);
594 
595         if (base->running_timer == timer)
596                 goto out;
597 
598         ret = 0;
599         if (timer_pending(timer)) {
600                 detach_timer(timer, 1);
601                 ret = 1;
602         }
603 out:
604         spin_unlock_irqrestore(&base->lock, flags);
605 
606         return ret;
607 }
608 
609 EXPORT_SYMBOL(try_to_del_timer_sync);
610 
611 /**
612  * del_timer_sync - deactivate a timer and wait for the handler to finish.
613  * @timer: the timer to be deactivated
614  *
615  * This function only differs from del_timer() on SMP: besides deactivating
616  * the timer it also makes sure the handler has finished executing on other
617  * CPUs.
618  *
619  * Synchronization rules: Callers must prevent restarting of the timer,
620  * otherwise this function is meaningless. It must not be called from
621  * interrupt contexts. The caller must not hold locks which would prevent
622  * completion of the timer's handler. The timer's handler must not call
623  * add_timer_on(). Upon exit the timer is not queued and the handler is
624  * not running on any CPU.
625  *
626  * The function returns whether it has deactivated a pending timer or not.
627  */
628 int del_timer_sync(struct timer_list *timer)
629 {
630         for (;;) {
631                 int ret = try_to_del_timer_sync(timer);
632                 if (ret >= 0)
633                         return ret;
634                 wait_for_running_timer(timer);
635         }
636 }
637 
638 EXPORT_SYMBOL(del_timer_sync);
639 #endif
640 
641 static int cascade(struct tvec_base *base, struct tvec *tv, int index)
642 {
643         /* cascade all the timers from tv up one level */
644         struct timer_list *timer, *tmp;
645         struct list_head tv_list;
646 
647         list_replace_init(tv->vec + index, &tv_list);
648 
649         /*
650          * We are removing _all_ timers from the list, so we
651          * don't have to detach them individually.
652          */
653         list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
654                 BUG_ON(tbase_get_base(timer->base) != base);
655                 internal_add_timer(base, timer);
656         }
657 
658         return index;
659 }
660 
661 #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
662 
663 /**
664  * __run_timers - run all expired timers (if any) on this CPU.
665  * @base: the timer vector to be processed.
666  *
667  * This function cascades all vectors and executes all expired timer
668  * vectors.
669  */
670 static inline void __run_timers(struct tvec_base *base)
671 {
672         struct timer_list *timer;
673 
674         spin_lock_irq(&base->lock);
675         while (time_after_eq(jiffies, base->timer_jiffies)) {
676                 struct list_head work_list;
677                 struct list_head *head = &work_list;
678                 int index = base->timer_jiffies & TVR_MASK;
679 
680                 if (softirq_need_resched()) {
681                         spin_unlock_irq(&base->lock);
682                         wake_up(&base->wait_for_running_timer);
683                         cond_resched_softirq_context();
684                         cpu_relax();
685                         spin_lock_irq(&base->lock);
686                         /*
687                          * We can simply continue after preemption, nobody
688                          * else can touch timer_jiffies so 'index' is still
689                          * valid. Any new jiffy will be taken care of in
690                          * subsequent loops:
691                          */
692                 }
693 
694                 /*
695                  * Cascade timers:
696                  */
697                 if (!index &&
698                         (!cascade(base, &base->tv2, INDEX(0))) &&
699                                 (!cascade(base, &base->tv3, INDEX(1))) &&
700                                         !cascade(base, &base->tv4, INDEX(2)))
701                         cascade(base, &base->tv5, INDEX(3));
702                 ++base->timer_jiffies;
703                 list_replace_init(base->tv1.vec + index, &work_list);
704                 while (!list_empty(head)) {
705                         void (*fn)(unsigned long);
706                         unsigned long data;
707 
708                         timer = list_first_entry(head, struct timer_list,entry);
709                         fn = timer->function;
710                         data = timer->data;
711 
712                         timer_stats_account_timer(timer);
713 
714                         set_running_timer(base, timer);
715                         detach_timer(timer, 1);
716                         spin_unlock_irq(&base->lock);
717                         {
718                                 int preempt_count = preempt_count();
719                                 fn(data);
720                                 if (preempt_count != preempt_count()) {
721                                         print_symbol("BUG: unbalanced timer-handler preempt count in %s!\n", (unsigned long) fn);
722                                         printk("entered with %08x, exited with %08x.\n", preempt_count, preempt_count());
723                                         preempt_count() = preempt_count;
724                                 }
725                         }
726                         set_running_timer(base, NULL);
727                         cond_resched_softirq_context();
728                         spin_lock_irq(&base->lock);
729                 }
730         }
731         wake_up(&base->wait_for_running_timer);
732         spin_unlock_irq(&base->lock);
733 }
734 
735 #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
736 /*
737  * Find out when the next timer event is due to happen. This
738  * is used on S/390 to stop all activity when a cpus is idle.
739  * This functions needs to be called disabled.
740  */
741 static unsigned long __next_timer_interrupt(struct tvec_base *base)
742 {
743         unsigned long timer_jiffies = base->timer_jiffies;
744         unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
745         int index, slot, array, found = 0;
746         struct timer_list *nte;
747         struct tvec *varray[4];
748 
749         /* Look for timer events in tv1. */
750         index = slot = timer_jiffies & TVR_MASK;
751         do {
752                 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
753                         if (tbase_get_deferrable(nte->base))
754                                 continue;
755 
756                         found = 1;
757                         expires = nte->expires;
758                         /* Look at the cascade bucket(s)? */
759                         if (!index || slot < index)
760                                 goto cascade;
761                         return expires;
762                 }
763                 slot = (slot + 1) & TVR_MASK;
764         } while (slot != index);
765 
766 cascade:
767         /* Calculate the next cascade event */
768         if (index)
769                 timer_jiffies += TVR_SIZE - index;
770         timer_jiffies >>= TVR_BITS;
771 
772         /* Check tv2-tv5. */
773         varray[0] = &base->tv2;
774         varray[1] = &base->tv3;
775         varray[2] = &base->tv4;
776         varray[3] = &base->tv5;
777 
778         for (array = 0; array < 4; array++) {
779                 struct tvec *varp = varray[array];
780 
781                 index = slot = timer_jiffies & TVN_MASK;
782                 do {
783                         list_for_each_entry(nte, varp->vec + slot, entry) {
784                                 found = 1;
785                                 if (time_before(nte->expires, expires))
786                                         expires = nte->expires;
787                         }
788                         /*
789                          * Do we still search for the first timer or are
790                          * we looking up the cascade buckets ?
791                          */
792                         if (found) {
793                                 /* Look at the cascade bucket(s)? */
794                                 if (!index || slot < index)
795                                         break;
796                                 return expires;
797                         }
798                         slot = (slot + 1) & TVN_MASK;
799                 } while (slot != index);
800 
801                 if (index)
802                         timer_jiffies += TVN_SIZE - index;
803                 timer_jiffies >>= TVN_BITS;
804         }
805         return expires;
806 }
807 
808 /*
809  * Check, if the next hrtimer event is before the next timer wheel
810  * event:
811  */
812 static unsigned long cmp_next_hrtimer_event(unsigned long now,
813                                             unsigned long expires)
814 {
815         ktime_t hr_delta = hrtimer_get_next_event();
816         struct timespec tsdelta;
817         unsigned long delta;
818 
819         if (hr_delta.tv64 == KTIME_MAX)
820                 return expires;
821 
822         /*
823          * Expired timer available, let it expire in the next tick
824          */
825         if (hr_delta.tv64 <= 0)
826                 return now + 1;
827 
828         tsdelta = ktime_to_timespec(hr_delta);
829         delta = timespec_to_jiffies(&tsdelta);
830 
831         /*
832          * Limit the delta to the max value, which is checked in
833          * tick_nohz_stop_sched_tick():
834          */
835         if (delta > NEXT_TIMER_MAX_DELTA)
836                 delta = NEXT_TIMER_MAX_DELTA;
837 
838         /*
839          * Take rounding errors in to account and make sure, that it
840          * expires in the next tick. Otherwise we go into an endless
841          * ping pong due to tick_nohz_stop_sched_tick() retriggering
842          * the timer softirq
843          */
844         if (delta < 1)
845                 delta = 1;
846         now += delta;
847         if (time_before(now, expires))
848                 return now;
849         return expires;
850 }
851 
852 /**
853  * get_next_timer_interrupt - return the jiffy of the next pending timer
854  * @now: current time (in jiffies)
855  */
856 unsigned long get_next_timer_interrupt(unsigned long now)
857 {
858         struct tvec_base *base = __get_cpu_var(tvec_bases);
859         unsigned long expires;
860 
861 #ifdef CONFIG_PREEMPT_RT
862         /*
863          * On PREEMPT_RT we cannot sleep here. If the trylock does not
864          * succeed then we return the worst-case 'expires in 1 tick'
865          * value:
866          */
867         if (spin_trylock(&base->lock)) {
868                 expires = __next_timer_interrupt(base);
869                 spin_unlock(&base->lock);
870         } else
871                 expires = now + 1;
872 #else
873         spin_lock(&base->lock);
874         expires = __next_timer_interrupt(base);
875         spin_unlock(&base->lock);
876 #endif
877 
878         if (time_before_eq(expires, now))
879                 return now;
880 
881         return cmp_next_hrtimer_event(now, expires);
882 }
883 
884 #ifdef CONFIG_NO_IDLE_HZ
885 unsigned long next_timer_interrupt(void)
886 {
887         return get_next_timer_interrupt(jiffies);
888 }
889 #endif
890 
891 #endif
892 
893 #ifndef CONFIG_VIRT_CPU_ACCOUNTING
894 void account_process_tick(struct task_struct *p, int user_tick)
895 {
896         cputime_t one_jiffy = jiffies_to_cputime(1);
897 
898         if (user_tick) {
899                 account_user_time(p, one_jiffy);
900                 account_user_time_scaled(p, cputime_to_scaled(one_jiffy));
901         } else {
902                 account_system_time(p, HARDIRQ_OFFSET, one_jiffy);
903                 account_system_time_scaled(p, cputime_to_scaled(one_jiffy));
904         }
905 }
906 #endif
907 
908 /*
909  * Called from the timer interrupt handler to charge one tick to the current
910  * process.  user_tick is 1 if the tick is user time, 0 for system.
911  */
912 void update_process_times(int user_tick)
913 {
914         struct task_struct *p = current;
915         int cpu = smp_processor_id();
916 
917         /* Note: this timer irq context must be accounted for as well. */
918         account_process_tick(p, user_tick);
919         scheduler_tick();
920         run_local_timers();
921         if (rcu_pending(cpu))
922                 rcu_check_callbacks(cpu, user_tick);
923         run_posix_cpu_timers(p);
924 }
925 
926 /*
927  * Nr of active tasks - counted in fixed-point numbers
928  */
929 static unsigned long count_active_tasks(void)
930 {
931         /*
932          * On PREEMPT_RT, we are running in the timer softirq thread,
933          * so consider 1 less running tasks:
934          */
935 #ifdef CONFIG_PREEMPT_RT
936         return (nr_active() - 1) * FIXED_1;
937 #else
938         return nr_active() * FIXED_1;
939 #endif
940 }
941 
942 #ifdef CONFIG_PREEMPT_RT
943 /*
944  * Nr of active tasks - counted in fixed-point numbers
945  */
946 static unsigned long count_active_rt_tasks(void)
947 {
948         extern unsigned long rt_nr_running(void);
949         extern unsigned long rt_nr_uninterruptible(void);
950 
951         return (rt_nr_running() + rt_nr_uninterruptible()) * FIXED_1;
952 }
953 #endif
954 
955 /*
956  * Hmm.. Changed this, as the GNU make sources (load.c) seems to
957  * imply that avenrun[] is the standard name for this kind of thing.
958  * Nothing else seems to be standardized: the fractional size etc
959  * all seem to differ on different machines.
960  *
961  * Requires xtime_lock to access.
962  */
963 unsigned long avenrun[3];
964 
965 EXPORT_SYMBOL(avenrun);
966 
967 unsigned long avenrun_rt[3];
968 
969 /*
970  * calc_load - given tick count, update the avenrun load estimates.
971  * This is called while holding a write_lock on xtime_lock.
972  */
973 static inline void calc_load(unsigned long ticks)
974 {
975         unsigned long active_tasks; /* fixed-point */
976         static int count = LOAD_FREQ;
977 #ifdef CONFIG_PREEMPT_RT
978         unsigned long active_rt_tasks; /* fixed-point */
979 #endif
980 
981         count -= ticks;
982         if (unlikely(count < 0)) {
983                 active_tasks = count_active_tasks();
984 #ifdef CONFIG_PREEMPT_RT
985                 active_rt_tasks = count_active_rt_tasks();
986 #endif
987                 do {
988                         CALC_LOAD(avenrun[0], EXP_1, active_tasks);
989                         CALC_LOAD(avenrun[1], EXP_5, active_tasks);
990                         CALC_LOAD(avenrun[2], EXP_15, active_tasks);
991 #ifdef CONFIG_PREEMPT_RT
992                         CALC_LOAD(avenrun_rt[0], EXP_1, active_rt_tasks);
993                         CALC_LOAD(avenrun_rt[1], EXP_5, active_rt_tasks);
994                         CALC_LOAD(avenrun_rt[2], EXP_15, active_rt_tasks);
995 #endif
996                         count += LOAD_FREQ;
997 
998                 } while (count < 0);
999         }
1000 }
1001 
1002 /*
1003  * Called by the local, per-CPU timer interrupt on SMP.
1004  */
1005 void run_local_timers(void)
1006 {
1007         hrtimer_run_queues();
1008         raise_softirq(TIMER_SOFTIRQ);
1009         softlockup_tick();
1010 }
1011 
1012 /*
1013  * Time of day handling:
1014  */
1015 static inline void update_times(void)
1016 {
1017         static unsigned long last_tick = INITIAL_JIFFIES;
1018         unsigned long ticks, flags;
1019 
1020         /*
1021          * Dont take the xtime_lock from every CPU in
1022          * every tick - only when needed:
1023          */
1024         if (jiffies == last_tick)
1025                 return;
1026 
1027         write_seqlock_irqsave(&xtime_lock, flags);
1028         ticks = jiffies - last_tick;
1029         if (ticks) {
1030                 last_tick += ticks;
1031                 calc_load(ticks);
1032         }
1033         write_sequnlock_irqrestore(&xtime_lock, flags);
1034 }
1035 
1036 
1037 /*
1038  * This function runs timers and the timer-tq in bottom half context.
1039  */
1040 static void run_timer_softirq(struct softirq_action *h)
1041 {
1042         struct tvec_base *base = per_cpu(tvec_bases, raw_smp_processor_id());
1043 
1044         update_times();
1045         hrtimer_run_pending();
1046 
1047         if (time_after_eq(jiffies, base->timer_jiffies))
1048                 __run_timers(base);
1049 }
1050 
1051 /*
1052  * The 64-bit jiffies value is not atomic - you MUST NOT read it
1053  * without sampling the sequence number in xtime_lock.
1054  * jiffies is defined in the linker script...
1055  */
1056 
1057 void do_timer(unsigned long ticks)
1058 {
1059         jiffies_64 += ticks;
1060         update_wall_time();
1061 }
1062 
1063 #ifdef __ARCH_WANT_SYS_ALARM
1064 
1065 /*
1066  * For backwards compatibility?  This can be done in libc so Alpha
1067  * and all newer ports shouldn't need it.
1068  */
1069 asmlinkage unsigned long sys_alarm(unsigned int seconds)
1070 {
1071         return alarm_setitimer(seconds);
1072 }
1073 
1074 #endif
1075 
1076 #ifndef __alpha__
1077 
1078 /*
1079  * The Alpha uses getxpid, getxuid, and getxgid instead.  Maybe this
1080  * should be moved into arch/i386 instead?
1081  */
1082 
1083 /**
1084  * sys_getpid - return the thread group id of the current process
1085  *
1086  * Note, despite the name, this returns the tgid not the pid.  The tgid and
1087  * the pid are identical unless CLONE_THREAD was specified on clone() in
1088  * which case the tgid is the same in all threads of the same group.
1089  *
1090  * This is SMP safe as current->tgid does not change.
1091  */
1092 asmlinkage long sys_getpid(void)
1093 {
1094         return task_tgid_vnr(current);
1095 }
1096 
1097 /*
1098  * Accessing ->real_parent is not SMP-safe, it could
1099  * change from under us. However, we can use a stale
1100  * value of ->real_parent under rcu_read_lock(), see
1101  * release_task()->call_rcu(delayed_put_task_struct).
1102  */
1103 asmlinkage long sys_getppid(void)
1104 {
1105         int pid;
1106 
1107         rcu_read_lock();
1108         pid = task_tgid_vnr(current->real_parent);
1109         rcu_read_unlock();
1110 
1111         return pid;
1112 }
1113 
1114 asmlinkage long sys_getuid(void)
1115 {
1116         /* Only we change this so SMP safe */
1117         return current->uid;
1118 }
1119 
1120 asmlinkage long sys_geteuid(void)
1121 {
1122         /* Only we change this so SMP safe */
1123         return current->euid;
1124 }
1125 
1126 asmlinkage long sys_getgid(void)
1127 {
1128         /* Only we change this so SMP safe */
1129         return current->gid;
1130 }
1131 
1132 asmlinkage long sys_getegid(void)
1133 {
1134         /* Only we change this so SMP safe */
1135         return  current->egid;
1136 }
1137 
1138 #endif
1139 
1140 static void process_timeout(unsigned long __data)
1141 {
1142         wake_up_process((struct task_struct *)__data);
1143 }
1144 
1145 /**
1146  * schedule_timeout - sleep until timeout
1147  * @timeout: timeout value in jiffies
1148  *
1149  * Make the current task sleep until @timeout jiffies have
1150  * elapsed. The routine will return immediately unless
1151  * the current task state has been set (see set_current_state()).
1152  *
1153  * You can set the task state as follows -
1154  *
1155  * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1156  * pass before the routine returns. The routine will return 0
1157  *
1158  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1159  * delivered to the current task. In this case the remaining time
1160  * in jiffies will be returned, or 0 if the timer expired in time
1161  *
1162  * The current task state is guaranteed to be TASK_RUNNING when this
1163  * routine returns.
1164  *
1165  * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1166  * the CPU away without a bound on the timeout. In this case the return
1167  * value will be %MAX_SCHEDULE_TIMEOUT.
1168  *
1169  * In all cases the return value is guaranteed to be non-negative.
1170  */
1171 signed long __sched schedule_timeout(signed long timeout)
1172 {
1173         struct timer_list timer;
1174         unsigned long expire;
1175 
1176         switch (timeout)
1177         {
1178         case MAX_SCHEDULE_TIMEOUT:
1179                 /*
1180                  * These two special cases are useful to be comfortable
1181                  * in the caller. Nothing more. We could take
1182                  * MAX_SCHEDULE_TIMEOUT from one of the negative value
1183                  * but I' d like to return a valid offset (>=0) to allow
1184                  * the caller to do everything it want with the retval.
1185                  */
1186                 schedule();
1187                 goto out;
1188         default:
1189                 /*
1190                  * Another bit of PARANOID. Note that the retval will be
1191                  * 0 since no piece of kernel is supposed to do a check
1192                  * for a negative retval of schedule_timeout() (since it
1193                  * should never happens anyway). You just have the printk()
1194                  * that will tell you if something is gone wrong and where.
1195                  */
1196                 if (timeout < 0) {
1197                         printk(KERN_ERR "schedule_timeout: wrong timeout "
1198                                 "value %lx\n", timeout);
1199                         dump_stack();
1200                         current->state = TASK_RUNNING;
1201                         goto out;
1202                 }
1203         }
1204 
1205         expire = timeout + jiffies;
1206 
1207         setup_timer(&timer, process_timeout, (unsigned long)current);
1208         __mod_timer(&timer, expire);
1209         schedule();
1210         del_singleshot_timer_sync(&timer);
1211 
1212         timeout = expire - jiffies;
1213 
1214  out:
1215         return timeout < 0 ? 0 : timeout;
1216 }
1217 EXPORT_SYMBOL(schedule_timeout);
1218 
1219 /*
1220  * We can use __set_current_state() here because schedule_timeout() calls
1221  * schedule() unconditionally.
1222  */
1223 signed long __sched schedule_timeout_interruptible(signed long timeout)
1224 {
1225         __set_current_state(TASK_INTERRUPTIBLE);
1226         return schedule_timeout(timeout);
1227 }
1228 EXPORT_SYMBOL(schedule_timeout_interruptible);
1229 
1230 signed long __sched schedule_timeout_killable(signed long timeout)
1231 {
1232         __set_current_state(TASK_KILLABLE);
1233         return schedule_timeout(timeout);
1234 }
1235 EXPORT_SYMBOL(schedule_timeout_killable);
1236 
1237 signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1238 {
1239         __set_current_state(TASK_UNINTERRUPTIBLE);
1240         return schedule_timeout(timeout);
1241 }
1242 EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1243 
1244 /* Thread ID - the internal kernel "pid" */
1245 asmlinkage long sys_gettid(void)
1246 {
1247         return task_pid_vnr(current);
1248 }
1249 
1250 /**
1251  * do_sysinfo - fill in sysinfo struct
1252  * @info: pointer to buffer to fill
1253  */
1254 int do_sysinfo(struct sysinfo *info)
1255 {
1256         unsigned long mem_total, sav_total;
1257         unsigned int mem_unit, bitcount;
1258         unsigned long seq;
1259 
1260         memset(info, 0, sizeof(struct sysinfo));
1261 
1262         do {
1263                 struct timespec tp;
1264                 seq = read_seqbegin(&xtime_lock);
1265 
1266                 /*
1267                  * This is annoying.  The below is the same thing
1268                  * posix_get_clock_monotonic() does, but it wants to
1269                  * take the lock which we want to cover the loads stuff
1270                  * too.
1271                  */
1272 
1273                 getnstimeofday(&tp);
1274                 tp.tv_sec += wall_to_monotonic.tv_sec;
1275                 tp.tv_nsec += wall_to_monotonic.tv_nsec;
1276                 monotonic_to_bootbased(&tp);
1277                 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1278                         tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1279                         tp.tv_sec++;
1280                 }
1281                 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1282 
1283                 info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1284                 info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1285                 info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
1286 
1287                 info->procs = nr_threads;
1288         } while (read_seqretry(&xtime_lock, seq));
1289 
1290         si_meminfo(info);
1291         si_swapinfo(info);
1292 
1293         /*
1294          * If the sum of all the available memory (i.e. ram + swap)
1295          * is less than can be stored in a 32 bit unsigned long then
1296          * we can be binary compatible with 2.2.x kernels.  If not,
1297          * well, in that case 2.2.x was broken anyways...
1298          *
1299          *  -Erik Andersen <andersee@debian.org>
1300          */
1301 
1302         mem_total = info->totalram + info->totalswap;
1303         if (mem_total < info->totalram || mem_total < info->totalswap)
1304                 goto out;
1305         bitcount = 0;
1306         mem_unit = info->mem_unit;
1307         while (mem_unit > 1) {
1308                 bitcount++;
1309                 mem_unit >>= 1;
1310                 sav_total = mem_total;
1311                 mem_total <<= 1;
1312                 if (mem_total < sav_total)
1313                         goto out;
1314         }
1315 
1316         /*
1317          * If mem_total did not overflow, multiply all memory values by
1318          * info->mem_unit and set it to 1.  This leaves things compatible
1319          * with 2.2.x, and also retains compatibility with earlier 2.4.x
1320          * kernels...
1321          */
1322 
1323         info->mem_unit = 1;
1324         info->totalram <<= bitcount;
1325         info->freeram <<= bitcount;
1326         info->sharedram <<= bitcount;
1327         info->bufferram <<= bitcount;
1328         info->totalswap <<= bitcount;
1329         info->freeswap <<= bitcount;
1330         info->totalhigh <<= bitcount;
1331         info->freehigh <<= bitcount;
1332 
1333 out:
1334         return 0;
1335 }
1336 
1337 asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1338 {
1339         struct sysinfo val;
1340 
1341         do_sysinfo(&val);
1342 
1343         if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1344                 return -EFAULT;
1345 
1346         return 0;
1347 }
1348 
1349 /*
1350  * lockdep: we want to track each per-CPU base as a separate lock-class,
1351  * but timer-bases are kmalloc()-ed, so we need to attach separate
1352  * keys to them:
1353  */
1354 static struct lock_class_key base_lock_keys[NR_CPUS];
1355 
1356 static int __cpuinit init_timers_cpu(int cpu)
1357 {
1358         int j;
1359         struct tvec_base *base;
1360         static char __cpuinitdata tvec_base_done[NR_CPUS];
1361 
1362         if (!tvec_base_done[cpu]) {
1363                 static char boot_done;
1364 
1365                 if (boot_done) {
1366                         /*
1367                          * The APs use this path later in boot
1368                          */
1369                         base = kmalloc_node(sizeof(*base),
1370                                                 GFP_KERNEL | __GFP_ZERO,
1371                                                 cpu_to_node(cpu));
1372                         if (!base)
1373                                 return -ENOMEM;
1374 
1375                         /* Make sure that tvec_base is 2 byte aligned */
1376                         if (tbase_get_deferrable(base)) {
1377                                 WARN_ON(1);
1378                                 kfree(base);
1379                                 return -ENOMEM;
1380                         }
1381                         per_cpu(tvec_bases, cpu) = base;
1382                 } else {
1383                         /*
1384                          * This is for the boot CPU - we use compile-time
1385                          * static initialisation because per-cpu memory isn't
1386                          * ready yet and because the memory allocators are not
1387                          * initialised either.
1388                          */
1389                         boot_done = 1;
1390                         base = &boot_tvec_bases;
1391                 }
1392                 tvec_base_done[cpu] = 1;
1393         } else {
1394                 base = per_cpu(tvec_bases, cpu);
1395         }
1396 
1397         spin_lock_init(&base->lock);
1398         lockdep_set_class(&base->lock, base_lock_keys + cpu);
1399         init_waitqueue_head(&base->wait_for_running_timer);
1400 
1401         for (j = 0; j < TVN_SIZE; j++) {
1402                 INIT_LIST_HEAD(base->tv5.vec + j);
1403                 INIT_LIST_HEAD(base->tv4.vec + j);
1404                 INIT_LIST_HEAD(base->tv3.vec + j);
1405                 INIT_LIST_HEAD(base->tv2.vec + j);
1406         }
1407         for (j = 0; j < TVR_SIZE; j++)
1408                 INIT_LIST_HEAD(base->tv1.vec + j);
1409 
1410         base->timer_jiffies = jiffies;
1411         return 0;
1412 }
1413 
1414 #ifdef CONFIG_HOTPLUG_CPU
1415 static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
1416 {
1417         struct timer_list *timer;
1418 
1419         while (!list_empty(head)) {
1420                 timer = list_first_entry(head, struct timer_list, entry);
1421                 detach_timer(timer, 0);
1422                 timer_set_base(timer, new_base);
1423                 internal_add_timer(new_base, timer);
1424         }
1425 }
1426 
1427 static void __cpuinit migrate_timers(int cpu)
1428 {
1429         struct tvec_base *old_base;
1430         struct tvec_base *new_base;
1431         int i;
1432 
1433         BUG_ON(cpu_online(cpu));
1434         old_base = per_cpu(tvec_bases, cpu);
1435         new_base = get_cpu_var(tvec_bases);
1436 
1437         local_irq_disable_nort();
1438         double_spin_lock(&new_base->lock, &old_base->lock,
1439                          smp_processor_id() < cpu);
1440 
1441         BUG_ON(old_base->running_timer);
1442 
1443         for (i = 0; i < TVR_SIZE; i++)
1444                 migrate_timer_list(new_base, old_base->tv1.vec + i);
1445         for (i = 0; i < TVN_SIZE; i++) {
1446                 migrate_timer_list(new_base, old_base->tv2.vec + i);
1447                 migrate_timer_list(new_base, old_base->tv3.vec + i);
1448                 migrate_timer_list(new_base, old_base->tv4.vec + i);
1449                 migrate_timer_list(new_base, old_base->tv5.vec + i);
1450         }
1451 
1452         double_spin_unlock(&new_base->lock, &old_base->lock,
1453                            smp_processor_id() < cpu);
1454         local_irq_enable_nort();
1455         put_cpu_var(tvec_bases);
1456 }
1457 #endif /* CONFIG_HOTPLUG_CPU */
1458 
1459 static int __cpuinit timer_cpu_notify(struct notifier_block *self,
1460                                 unsigned long action, void *hcpu)
1461 {
1462         long cpu = (long)hcpu;
1463         switch(action) {
1464         case CPU_UP_PREPARE:
1465         case CPU_UP_PREPARE_FROZEN:
1466                 if (init_timers_cpu(cpu) < 0)
1467                         return NOTIFY_BAD;
1468                 break;
1469 #ifdef CONFIG_HOTPLUG_CPU
1470         case CPU_DEAD:
1471         case CPU_DEAD_FROZEN:
1472                 migrate_timers(cpu);
1473                 break;
1474 #endif
1475         default:
1476                 break;
1477         }
1478         return NOTIFY_OK;
1479 }
1480 
1481 static struct notifier_block __cpuinitdata timers_nb = {
1482         .notifier_call  = timer_cpu_notify,
1483 };
1484 
1485 
1486 void __init init_timers(void)
1487 {
1488         int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1489                                 (void *)(long)smp_processor_id());
1490 
1491         init_timer_stats();
1492 
1493         BUG_ON(err == NOTIFY_BAD);
1494         register_cpu_notifier(&timers_nb);
1495         open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1496 }
1497 
1498 /**
1499  * msleep - sleep safely even with waitqueue interruptions
1500  * @msecs: Time in milliseconds to sleep for
1501  */
1502 void msleep(unsigned int msecs)
1503 {
1504         unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1505 
1506         while (timeout)
1507                 timeout = schedule_timeout_uninterruptible(timeout);
1508 }
1509 
1510 EXPORT_SYMBOL(msleep);
1511 
1512 /**
1513  * msleep_interruptible - sleep waiting for signals
1514  * @msecs: Time in milliseconds to sleep for
1515  */
1516 unsigned long msleep_interruptible(unsigned int msecs)
1517 {
1518         unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1519 
1520         while (timeout && !signal_pending(current))
1521                 timeout = schedule_timeout_interruptible(timeout);
1522         return jiffies_to_msecs(timeout);
1523 }
1524 
1525 EXPORT_SYMBOL(msleep_interruptible);
1526 
  This page was automatically generated by the LXR engine.