usb: hub: Use correct reset for wedged USB3 devices that are NOTATTACHED
[linux/fpc-iii.git] / kernel / timer.c
blobf8b05a467883c61cd489e9a5a94b398e7d8d5632
1 /*
2 * linux/kernel/timer.c
4 * Kernel internal timers, 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/export.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/delay.h>
38 #include <linux/tick.h>
39 #include <linux/kallsyms.h>
40 #include <linux/irq_work.h>
41 #include <linux/sched.h>
42 #include <linux/slab.h>
44 #include <asm/uaccess.h>
45 #include <asm/unistd.h>
46 #include <asm/div64.h>
47 #include <asm/timex.h>
48 #include <asm/io.h>
50 #define CREATE_TRACE_POINTS
51 #include <trace/events/timer.h>
53 u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
55 EXPORT_SYMBOL(jiffies_64);
58 * per-CPU timer vector definitions:
60 #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
61 #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
62 #define TVN_SIZE (1 << TVN_BITS)
63 #define TVR_SIZE (1 << TVR_BITS)
64 #define TVN_MASK (TVN_SIZE - 1)
65 #define TVR_MASK (TVR_SIZE - 1)
66 #define MAX_TVAL ((unsigned long)((1ULL << (TVR_BITS + 4*TVN_BITS)) - 1))
68 struct tvec {
69 struct list_head vec[TVN_SIZE];
72 struct tvec_root {
73 struct list_head vec[TVR_SIZE];
76 struct tvec_base {
77 spinlock_t lock;
78 struct timer_list *running_timer;
79 unsigned long timer_jiffies;
80 unsigned long next_timer;
81 struct tvec_root tv1;
82 struct tvec tv2;
83 struct tvec tv3;
84 struct tvec tv4;
85 struct tvec tv5;
86 } ____cacheline_aligned;
88 struct tvec_base boot_tvec_bases;
89 EXPORT_SYMBOL(boot_tvec_bases);
90 static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
92 /* Functions below help us manage 'deferrable' flag */
93 static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
95 return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);
98 static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
100 return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
103 static inline void timer_set_deferrable(struct timer_list *timer)
105 timer->base = TBASE_MAKE_DEFERRED(timer->base);
108 static inline void
109 timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
111 timer->base = (struct tvec_base *)((unsigned long)(new_base) |
112 tbase_get_deferrable(timer->base));
115 static unsigned long round_jiffies_common(unsigned long j, int cpu,
116 bool force_up)
118 int rem;
119 unsigned long original = j;
122 * We don't want all cpus firing their timers at once hitting the
123 * same lock or cachelines, so we skew each extra cpu with an extra
124 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
125 * already did this.
126 * The skew is done by adding 3*cpunr, then round, then subtract this
127 * extra offset again.
129 j += cpu * 3;
131 rem = j % HZ;
134 * If the target jiffie is just after a whole second (which can happen
135 * due to delays of the timer irq, long irq off times etc etc) then
136 * we should round down to the whole second, not up. Use 1/4th second
137 * as cutoff for this rounding as an extreme upper bound for this.
138 * But never round down if @force_up is set.
140 if (rem < HZ/4 && !force_up) /* round down */
141 j = j - rem;
142 else /* round up */
143 j = j - rem + HZ;
145 /* now that we have rounded, subtract the extra skew again */
146 j -= cpu * 3;
149 * Make sure j is still in the future. Otherwise return the
150 * unmodified value.
152 return time_is_after_jiffies(j) ? j : original;
156 * __round_jiffies - function to round jiffies to a full second
157 * @j: the time in (absolute) jiffies that should be rounded
158 * @cpu: the processor number on which the timeout will happen
160 * __round_jiffies() rounds an absolute time in the future (in jiffies)
161 * up or down to (approximately) full seconds. This is useful for timers
162 * for which the exact time they fire does not matter too much, as long as
163 * they fire approximately every X seconds.
165 * By rounding these timers to whole seconds, all such timers will fire
166 * at the same time, rather than at various times spread out. The goal
167 * of this is to have the CPU wake up less, which saves power.
169 * The exact rounding is skewed for each processor to avoid all
170 * processors firing at the exact same time, which could lead
171 * to lock contention or spurious cache line bouncing.
173 * The return value is the rounded version of the @j parameter.
175 unsigned long __round_jiffies(unsigned long j, int cpu)
177 return round_jiffies_common(j, cpu, false);
179 EXPORT_SYMBOL_GPL(__round_jiffies);
182 * __round_jiffies_relative - function to round jiffies to a full second
183 * @j: the time in (relative) jiffies that should be rounded
184 * @cpu: the processor number on which the timeout will happen
186 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
187 * up or down to (approximately) full seconds. This is useful for timers
188 * for which the exact time they fire does not matter too much, as long as
189 * they fire approximately every X seconds.
191 * By rounding these timers to whole seconds, all such timers will fire
192 * at the same time, rather than at various times spread out. The goal
193 * of this is to have the CPU wake up less, which saves power.
195 * The exact rounding is skewed for each processor to avoid all
196 * processors firing at the exact same time, which could lead
197 * to lock contention or spurious cache line bouncing.
199 * The return value is the rounded version of the @j parameter.
201 unsigned long __round_jiffies_relative(unsigned long j, int cpu)
203 unsigned long j0 = jiffies;
205 /* Use j0 because jiffies might change while we run */
206 return round_jiffies_common(j + j0, cpu, false) - j0;
208 EXPORT_SYMBOL_GPL(__round_jiffies_relative);
211 * round_jiffies - function to round jiffies to a full second
212 * @j: the time in (absolute) jiffies that should be rounded
214 * round_jiffies() rounds an absolute time in the future (in jiffies)
215 * up or down to (approximately) full seconds. This is useful for timers
216 * for which the exact time they fire does not matter too much, as long as
217 * they fire approximately every X seconds.
219 * By rounding these timers to whole seconds, all such timers will fire
220 * at the same time, rather than at various times spread out. The goal
221 * of this is to have the CPU wake up less, which saves power.
223 * The return value is the rounded version of the @j parameter.
225 unsigned long round_jiffies(unsigned long j)
227 return round_jiffies_common(j, raw_smp_processor_id(), false);
229 EXPORT_SYMBOL_GPL(round_jiffies);
232 * round_jiffies_relative - function to round jiffies to a full second
233 * @j: the time in (relative) jiffies that should be rounded
235 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
236 * up or down to (approximately) full seconds. This is useful for timers
237 * for which the exact time they fire does not matter too much, as long as
238 * they fire approximately every X seconds.
240 * By rounding these timers to whole seconds, all such timers will fire
241 * at the same time, rather than at various times spread out. The goal
242 * of this is to have the CPU wake up less, which saves power.
244 * The return value is the rounded version of the @j parameter.
246 unsigned long round_jiffies_relative(unsigned long j)
248 return __round_jiffies_relative(j, raw_smp_processor_id());
250 EXPORT_SYMBOL_GPL(round_jiffies_relative);
253 * __round_jiffies_up - function to round jiffies up to a full second
254 * @j: the time in (absolute) jiffies that should be rounded
255 * @cpu: the processor number on which the timeout will happen
257 * This is the same as __round_jiffies() except that it will never
258 * round down. This is useful for timeouts for which the exact time
259 * of firing does not matter too much, as long as they don't fire too
260 * early.
262 unsigned long __round_jiffies_up(unsigned long j, int cpu)
264 return round_jiffies_common(j, cpu, true);
266 EXPORT_SYMBOL_GPL(__round_jiffies_up);
269 * __round_jiffies_up_relative - function to round jiffies up to a full second
270 * @j: the time in (relative) jiffies that should be rounded
271 * @cpu: the processor number on which the timeout will happen
273 * This is the same as __round_jiffies_relative() except that it will never
274 * round down. This is useful for timeouts for which the exact time
275 * of firing does not matter too much, as long as they don't fire too
276 * early.
278 unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
280 unsigned long j0 = jiffies;
282 /* Use j0 because jiffies might change while we run */
283 return round_jiffies_common(j + j0, cpu, true) - j0;
285 EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
288 * round_jiffies_up - function to round jiffies up to a full second
289 * @j: the time in (absolute) jiffies that should be rounded
291 * This is the same as round_jiffies() except that it will never
292 * round down. This is useful for timeouts for which the exact time
293 * of firing does not matter too much, as long as they don't fire too
294 * early.
296 unsigned long round_jiffies_up(unsigned long j)
298 return round_jiffies_common(j, raw_smp_processor_id(), true);
300 EXPORT_SYMBOL_GPL(round_jiffies_up);
303 * round_jiffies_up_relative - function to round jiffies up to a full second
304 * @j: the time in (relative) jiffies that should be rounded
306 * This is the same as round_jiffies_relative() except that it will never
307 * round down. This is useful for timeouts for which the exact time
308 * of firing does not matter too much, as long as they don't fire too
309 * early.
311 unsigned long round_jiffies_up_relative(unsigned long j)
313 return __round_jiffies_up_relative(j, raw_smp_processor_id());
315 EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
318 * set_timer_slack - set the allowed slack for a timer
319 * @timer: the timer to be modified
320 * @slack_hz: the amount of time (in jiffies) allowed for rounding
322 * Set the amount of time, in jiffies, that a certain timer has
323 * in terms of slack. By setting this value, the timer subsystem
324 * will schedule the actual timer somewhere between
325 * the time mod_timer() asks for, and that time plus the slack.
327 * By setting the slack to -1, a percentage of the delay is used
328 * instead.
330 void set_timer_slack(struct timer_list *timer, int slack_hz)
332 timer->slack = slack_hz;
334 EXPORT_SYMBOL_GPL(set_timer_slack);
336 static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
338 unsigned long expires = timer->expires;
339 unsigned long idx = expires - base->timer_jiffies;
340 struct list_head *vec;
342 if (idx < TVR_SIZE) {
343 int i = expires & TVR_MASK;
344 vec = base->tv1.vec + i;
345 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
346 int i = (expires >> TVR_BITS) & TVN_MASK;
347 vec = base->tv2.vec + i;
348 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
349 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
350 vec = base->tv3.vec + i;
351 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
352 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
353 vec = base->tv4.vec + i;
354 } else if ((signed long) idx < 0) {
356 * Can happen if you add a timer with expires == jiffies,
357 * or you set a timer to go off in the past
359 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
360 } else {
361 int i;
362 /* If the timeout is larger than MAX_TVAL (on 64-bit
363 * architectures or with CONFIG_BASE_SMALL=1) then we
364 * use the maximum timeout.
366 if (idx > MAX_TVAL) {
367 idx = MAX_TVAL;
368 expires = idx + base->timer_jiffies;
370 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
371 vec = base->tv5.vec + i;
374 * Timers are FIFO:
376 list_add_tail(&timer->entry, vec);
379 #ifdef CONFIG_TIMER_STATS
380 void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
382 if (timer->start_site)
383 return;
385 timer->start_site = addr;
386 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
387 timer->start_pid = current->pid;
390 static void timer_stats_account_timer(struct timer_list *timer)
392 unsigned int flag = 0;
394 if (likely(!timer->start_site))
395 return;
396 if (unlikely(tbase_get_deferrable(timer->base)))
397 flag |= TIMER_STATS_FLAG_DEFERRABLE;
399 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
400 timer->function, timer->start_comm, flag);
403 #else
404 static void timer_stats_account_timer(struct timer_list *timer) {}
405 #endif
407 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
409 static struct debug_obj_descr timer_debug_descr;
411 static void *timer_debug_hint(void *addr)
413 return ((struct timer_list *) addr)->function;
417 * fixup_init is called when:
418 * - an active object is initialized
420 static int timer_fixup_init(void *addr, enum debug_obj_state state)
422 struct timer_list *timer = addr;
424 switch (state) {
425 case ODEBUG_STATE_ACTIVE:
426 del_timer_sync(timer);
427 debug_object_init(timer, &timer_debug_descr);
428 return 1;
429 default:
430 return 0;
435 * fixup_activate is called when:
436 * - an active object is activated
437 * - an unknown object is activated (might be a statically initialized object)
439 static int timer_fixup_activate(void *addr, enum debug_obj_state state)
441 struct timer_list *timer = addr;
443 switch (state) {
445 case ODEBUG_STATE_NOTAVAILABLE:
447 * This is not really a fixup. The timer was
448 * statically initialized. We just make sure that it
449 * is tracked in the object tracker.
451 if (timer->entry.next == NULL &&
452 timer->entry.prev == TIMER_ENTRY_STATIC) {
453 debug_object_init(timer, &timer_debug_descr);
454 debug_object_activate(timer, &timer_debug_descr);
455 return 0;
456 } else {
457 WARN_ON_ONCE(1);
459 return 0;
461 case ODEBUG_STATE_ACTIVE:
462 WARN_ON(1);
464 default:
465 return 0;
470 * fixup_free is called when:
471 * - an active object is freed
473 static int timer_fixup_free(void *addr, enum debug_obj_state state)
475 struct timer_list *timer = addr;
477 switch (state) {
478 case ODEBUG_STATE_ACTIVE:
479 del_timer_sync(timer);
480 debug_object_free(timer, &timer_debug_descr);
481 return 1;
482 default:
483 return 0;
487 static struct debug_obj_descr timer_debug_descr = {
488 .name = "timer_list",
489 .debug_hint = timer_debug_hint,
490 .fixup_init = timer_fixup_init,
491 .fixup_activate = timer_fixup_activate,
492 .fixup_free = timer_fixup_free,
495 static inline void debug_timer_init(struct timer_list *timer)
497 debug_object_init(timer, &timer_debug_descr);
500 static inline void debug_timer_activate(struct timer_list *timer)
502 debug_object_activate(timer, &timer_debug_descr);
505 static inline void debug_timer_deactivate(struct timer_list *timer)
507 debug_object_deactivate(timer, &timer_debug_descr);
510 static inline void debug_timer_free(struct timer_list *timer)
512 debug_object_free(timer, &timer_debug_descr);
515 static void __init_timer(struct timer_list *timer,
516 const char *name,
517 struct lock_class_key *key);
519 void init_timer_on_stack_key(struct timer_list *timer,
520 const char *name,
521 struct lock_class_key *key)
523 debug_object_init_on_stack(timer, &timer_debug_descr);
524 __init_timer(timer, name, key);
526 EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
528 void destroy_timer_on_stack(struct timer_list *timer)
530 debug_object_free(timer, &timer_debug_descr);
532 EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
534 #else
535 static inline void debug_timer_init(struct timer_list *timer) { }
536 static inline void debug_timer_activate(struct timer_list *timer) { }
537 static inline void debug_timer_deactivate(struct timer_list *timer) { }
538 #endif
540 static inline void debug_init(struct timer_list *timer)
542 debug_timer_init(timer);
543 trace_timer_init(timer);
546 static inline void
547 debug_activate(struct timer_list *timer, unsigned long expires)
549 debug_timer_activate(timer);
550 trace_timer_start(timer, expires);
553 static inline void debug_deactivate(struct timer_list *timer)
555 debug_timer_deactivate(timer);
556 trace_timer_cancel(timer);
559 static void __init_timer(struct timer_list *timer,
560 const char *name,
561 struct lock_class_key *key)
563 timer->entry.next = NULL;
564 timer->base = __raw_get_cpu_var(tvec_bases);
565 timer->slack = -1;
566 #ifdef CONFIG_TIMER_STATS
567 timer->start_site = NULL;
568 timer->start_pid = -1;
569 memset(timer->start_comm, 0, TASK_COMM_LEN);
570 #endif
571 lockdep_init_map(&timer->lockdep_map, name, key, 0);
574 void setup_deferrable_timer_on_stack_key(struct timer_list *timer,
575 const char *name,
576 struct lock_class_key *key,
577 void (*function)(unsigned long),
578 unsigned long data)
580 timer->function = function;
581 timer->data = data;
582 init_timer_on_stack_key(timer, name, key);
583 timer_set_deferrable(timer);
585 EXPORT_SYMBOL_GPL(setup_deferrable_timer_on_stack_key);
588 * init_timer_key - initialize a timer
589 * @timer: the timer to be initialized
590 * @name: name of the timer
591 * @key: lockdep class key of the fake lock used for tracking timer
592 * sync lock dependencies
594 * init_timer_key() must be done to a timer prior calling *any* of the
595 * other timer functions.
597 void init_timer_key(struct timer_list *timer,
598 const char *name,
599 struct lock_class_key *key)
601 debug_init(timer);
602 __init_timer(timer, name, key);
604 EXPORT_SYMBOL(init_timer_key);
606 void init_timer_deferrable_key(struct timer_list *timer,
607 const char *name,
608 struct lock_class_key *key)
610 init_timer_key(timer, name, key);
611 timer_set_deferrable(timer);
613 EXPORT_SYMBOL(init_timer_deferrable_key);
615 static inline void detach_timer(struct timer_list *timer,
616 int clear_pending)
618 struct list_head *entry = &timer->entry;
620 debug_deactivate(timer);
622 __list_del(entry->prev, entry->next);
623 if (clear_pending)
624 entry->next = NULL;
625 entry->prev = LIST_POISON2;
629 * We are using hashed locking: holding per_cpu(tvec_bases).lock
630 * means that all timers which are tied to this base via timer->base are
631 * locked, and the base itself is locked too.
633 * So __run_timers/migrate_timers can safely modify all timers which could
634 * be found on ->tvX lists.
636 * When the timer's base is locked, and the timer removed from list, it is
637 * possible to set timer->base = NULL and drop the lock: the timer remains
638 * locked.
640 static struct tvec_base *lock_timer_base(struct timer_list *timer,
641 unsigned long *flags)
642 __acquires(timer->base->lock)
644 struct tvec_base *base;
646 for (;;) {
647 struct tvec_base *prelock_base = timer->base;
648 base = tbase_get_base(prelock_base);
649 if (likely(base != NULL)) {
650 spin_lock_irqsave(&base->lock, *flags);
651 if (likely(prelock_base == timer->base))
652 return base;
653 /* The timer has migrated to another CPU */
654 spin_unlock_irqrestore(&base->lock, *flags);
656 cpu_relax();
660 static inline int
661 __mod_timer(struct timer_list *timer, unsigned long expires,
662 bool pending_only, int pinned)
664 struct tvec_base *base, *new_base;
665 unsigned long flags;
666 int ret = 0 , cpu;
668 timer_stats_timer_set_start_info(timer);
669 BUG_ON(!timer->function);
671 base = lock_timer_base(timer, &flags);
673 if (timer_pending(timer)) {
674 detach_timer(timer, 0);
675 if (timer->expires == base->next_timer &&
676 !tbase_get_deferrable(timer->base))
677 base->next_timer = base->timer_jiffies;
678 ret = 1;
679 } else {
680 if (pending_only)
681 goto out_unlock;
684 debug_activate(timer, expires);
686 cpu = smp_processor_id();
688 #if defined(CONFIG_NO_HZ) && defined(CONFIG_SMP)
689 if (!pinned && get_sysctl_timer_migration() && idle_cpu(cpu))
690 cpu = get_nohz_timer_target();
691 #endif
692 new_base = per_cpu(tvec_bases, cpu);
694 if (base != new_base) {
696 * We are trying to schedule the timer on the local CPU.
697 * However we can't change timer's base while it is running,
698 * otherwise del_timer_sync() can't detect that the timer's
699 * handler yet has not finished. This also guarantees that
700 * the timer is serialized wrt itself.
702 if (likely(base->running_timer != timer)) {
703 /* See the comment in lock_timer_base() */
704 timer_set_base(timer, NULL);
705 spin_unlock(&base->lock);
706 base = new_base;
707 spin_lock(&base->lock);
708 timer_set_base(timer, base);
712 timer->expires = expires;
713 if (time_before(timer->expires, base->next_timer) &&
714 !tbase_get_deferrable(timer->base))
715 base->next_timer = timer->expires;
716 internal_add_timer(base, timer);
718 out_unlock:
719 spin_unlock_irqrestore(&base->lock, flags);
721 return ret;
725 * mod_timer_pending - modify a pending timer's timeout
726 * @timer: the pending timer to be modified
727 * @expires: new timeout in jiffies
729 * mod_timer_pending() is the same for pending timers as mod_timer(),
730 * but will not re-activate and modify already deleted timers.
732 * It is useful for unserialized use of timers.
734 int mod_timer_pending(struct timer_list *timer, unsigned long expires)
736 return __mod_timer(timer, expires, true, TIMER_NOT_PINNED);
738 EXPORT_SYMBOL(mod_timer_pending);
741 * Decide where to put the timer while taking the slack into account
743 * Algorithm:
744 * 1) calculate the maximum (absolute) time
745 * 2) calculate the highest bit where the expires and new max are different
746 * 3) use this bit to make a mask
747 * 4) use the bitmask to round down the maximum time, so that all last
748 * bits are zeros
750 static inline
751 unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
753 unsigned long expires_limit, mask;
754 int bit;
756 if (timer->slack >= 0) {
757 expires_limit = expires + timer->slack;
758 } else {
759 long delta = expires - jiffies;
761 if (delta < 256)
762 return expires;
764 expires_limit = expires + delta / 256;
766 mask = expires ^ expires_limit;
767 if (mask == 0)
768 return expires;
770 bit = find_last_bit(&mask, BITS_PER_LONG);
772 mask = (1 << bit) - 1;
774 expires_limit = expires_limit & ~(mask);
776 return expires_limit;
780 * mod_timer - modify a timer's timeout
781 * @timer: the timer to be modified
782 * @expires: new timeout in jiffies
784 * mod_timer() is a more efficient way to update the expire field of an
785 * active timer (if the timer is inactive it will be activated)
787 * mod_timer(timer, expires) is equivalent to:
789 * del_timer(timer); timer->expires = expires; add_timer(timer);
791 * Note that if there are multiple unserialized concurrent users of the
792 * same timer, then mod_timer() is the only safe way to modify the timeout,
793 * since add_timer() cannot modify an already running timer.
795 * The function returns whether it has modified a pending timer or not.
796 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
797 * active timer returns 1.)
799 int mod_timer(struct timer_list *timer, unsigned long expires)
801 expires = apply_slack(timer, expires);
804 * This is a common optimization triggered by the
805 * networking code - if the timer is re-modified
806 * to be the same thing then just return:
808 if (timer_pending(timer) && timer->expires == expires)
809 return 1;
811 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
813 EXPORT_SYMBOL(mod_timer);
816 * mod_timer_pinned - modify a timer's timeout
817 * @timer: the timer to be modified
818 * @expires: new timeout in jiffies
820 * mod_timer_pinned() is a way to update the expire field of an
821 * active timer (if the timer is inactive it will be activated)
822 * and not allow the timer to be migrated to a different CPU.
824 * mod_timer_pinned(timer, expires) is equivalent to:
826 * del_timer(timer); timer->expires = expires; add_timer(timer);
828 int mod_timer_pinned(struct timer_list *timer, unsigned long expires)
830 if (timer->expires == expires && timer_pending(timer))
831 return 1;
833 return __mod_timer(timer, expires, false, TIMER_PINNED);
835 EXPORT_SYMBOL(mod_timer_pinned);
838 * add_timer - start a timer
839 * @timer: the timer to be added
841 * The kernel will do a ->function(->data) callback from the
842 * timer interrupt at the ->expires point in the future. The
843 * current time is 'jiffies'.
845 * The timer's ->expires, ->function (and if the handler uses it, ->data)
846 * fields must be set prior calling this function.
848 * Timers with an ->expires field in the past will be executed in the next
849 * timer tick.
851 void add_timer(struct timer_list *timer)
853 BUG_ON(timer_pending(timer));
854 mod_timer(timer, timer->expires);
856 EXPORT_SYMBOL(add_timer);
859 * add_timer_on - start a timer on a particular CPU
860 * @timer: the timer to be added
861 * @cpu: the CPU to start it on
863 * This is not very scalable on SMP. Double adds are not possible.
865 void add_timer_on(struct timer_list *timer, int cpu)
867 struct tvec_base *base = per_cpu(tvec_bases, cpu);
868 unsigned long flags;
870 timer_stats_timer_set_start_info(timer);
871 BUG_ON(timer_pending(timer) || !timer->function);
872 spin_lock_irqsave(&base->lock, flags);
873 timer_set_base(timer, base);
874 debug_activate(timer, timer->expires);
875 if (time_before(timer->expires, base->next_timer) &&
876 !tbase_get_deferrable(timer->base))
877 base->next_timer = timer->expires;
878 internal_add_timer(base, timer);
880 * Check whether the other CPU is idle and needs to be
881 * triggered to reevaluate the timer wheel when nohz is
882 * active. We are protected against the other CPU fiddling
883 * with the timer by holding the timer base lock. This also
884 * makes sure that a CPU on the way to idle can not evaluate
885 * the timer wheel.
887 wake_up_idle_cpu(cpu);
888 spin_unlock_irqrestore(&base->lock, flags);
890 EXPORT_SYMBOL_GPL(add_timer_on);
893 * del_timer - deactive a timer.
894 * @timer: the timer to be deactivated
896 * del_timer() deactivates a timer - this works on both active and inactive
897 * timers.
899 * The function returns whether it has deactivated a pending timer or not.
900 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
901 * active timer returns 1.)
903 int del_timer(struct timer_list *timer)
905 struct tvec_base *base;
906 unsigned long flags;
907 int ret = 0;
909 timer_stats_timer_clear_start_info(timer);
910 if (timer_pending(timer)) {
911 base = lock_timer_base(timer, &flags);
912 if (timer_pending(timer)) {
913 detach_timer(timer, 1);
914 if (timer->expires == base->next_timer &&
915 !tbase_get_deferrable(timer->base))
916 base->next_timer = base->timer_jiffies;
917 ret = 1;
919 spin_unlock_irqrestore(&base->lock, flags);
922 return ret;
924 EXPORT_SYMBOL(del_timer);
927 * try_to_del_timer_sync - Try to deactivate a timer
928 * @timer: timer do del
930 * This function tries to deactivate a timer. Upon successful (ret >= 0)
931 * exit the timer is not queued and the handler is not running on any CPU.
933 int try_to_del_timer_sync(struct timer_list *timer)
935 struct tvec_base *base;
936 unsigned long flags;
937 int ret = -1;
939 base = lock_timer_base(timer, &flags);
941 if (base->running_timer == timer)
942 goto out;
944 timer_stats_timer_clear_start_info(timer);
945 ret = 0;
946 if (timer_pending(timer)) {
947 detach_timer(timer, 1);
948 if (timer->expires == base->next_timer &&
949 !tbase_get_deferrable(timer->base))
950 base->next_timer = base->timer_jiffies;
951 ret = 1;
953 out:
954 spin_unlock_irqrestore(&base->lock, flags);
956 return ret;
958 EXPORT_SYMBOL(try_to_del_timer_sync);
960 #ifdef CONFIG_SMP
962 * del_timer_sync - deactivate a timer and wait for the handler to finish.
963 * @timer: the timer to be deactivated
965 * This function only differs from del_timer() on SMP: besides deactivating
966 * the timer it also makes sure the handler has finished executing on other
967 * CPUs.
969 * Synchronization rules: Callers must prevent restarting of the timer,
970 * otherwise this function is meaningless. It must not be called from
971 * interrupt contexts. The caller must not hold locks which would prevent
972 * completion of the timer's handler. The timer's handler must not call
973 * add_timer_on(). Upon exit the timer is not queued and the handler is
974 * not running on any CPU.
976 * Note: You must not hold locks that are held in interrupt context
977 * while calling this function. Even if the lock has nothing to do
978 * with the timer in question. Here's why:
980 * CPU0 CPU1
981 * ---- ----
982 * <SOFTIRQ>
983 * call_timer_fn();
984 * base->running_timer = mytimer;
985 * spin_lock_irq(somelock);
986 * <IRQ>
987 * spin_lock(somelock);
988 * del_timer_sync(mytimer);
989 * while (base->running_timer == mytimer);
991 * Now del_timer_sync() will never return and never release somelock.
992 * The interrupt on the other CPU is waiting to grab somelock but
993 * it has interrupted the softirq that CPU0 is waiting to finish.
995 * The function returns whether it has deactivated a pending timer or not.
997 int del_timer_sync(struct timer_list *timer)
999 #ifdef CONFIG_LOCKDEP
1000 unsigned long flags;
1003 * If lockdep gives a backtrace here, please reference
1004 * the synchronization rules above.
1006 local_irq_save(flags);
1007 lock_map_acquire(&timer->lockdep_map);
1008 lock_map_release(&timer->lockdep_map);
1009 local_irq_restore(flags);
1010 #endif
1012 * don't use it in hardirq context, because it
1013 * could lead to deadlock.
1015 WARN_ON(in_irq());
1016 for (;;) {
1017 int ret = try_to_del_timer_sync(timer);
1018 if (ret >= 0)
1019 return ret;
1020 cpu_relax();
1023 EXPORT_SYMBOL(del_timer_sync);
1024 #endif
1026 static int cascade(struct tvec_base *base, struct tvec *tv, int index)
1028 /* cascade all the timers from tv up one level */
1029 struct timer_list *timer, *tmp;
1030 struct list_head tv_list;
1032 list_replace_init(tv->vec + index, &tv_list);
1035 * We are removing _all_ timers from the list, so we
1036 * don't have to detach them individually.
1038 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
1039 BUG_ON(tbase_get_base(timer->base) != base);
1040 internal_add_timer(base, timer);
1043 return index;
1046 static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
1047 unsigned long data)
1049 int preempt_count = preempt_count();
1051 #ifdef CONFIG_LOCKDEP
1053 * It is permissible to free the timer from inside the
1054 * function that is called from it, this we need to take into
1055 * account for lockdep too. To avoid bogus "held lock freed"
1056 * warnings as well as problems when looking into
1057 * timer->lockdep_map, make a copy and use that here.
1059 struct lockdep_map lockdep_map = timer->lockdep_map;
1060 #endif
1062 * Couple the lock chain with the lock chain at
1063 * del_timer_sync() by acquiring the lock_map around the fn()
1064 * call here and in del_timer_sync().
1066 lock_map_acquire(&lockdep_map);
1068 trace_timer_expire_entry(timer);
1069 fn(data);
1070 trace_timer_expire_exit(timer);
1072 lock_map_release(&lockdep_map);
1074 if (preempt_count != preempt_count()) {
1075 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
1076 fn, preempt_count, preempt_count());
1078 * Restore the preempt count. That gives us a decent
1079 * chance to survive and extract information. If the
1080 * callback kept a lock held, bad luck, but not worse
1081 * than the BUG() we had.
1083 preempt_count() = preempt_count;
1087 #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
1090 * __run_timers - run all expired timers (if any) on this CPU.
1091 * @base: the timer vector to be processed.
1093 * This function cascades all vectors and executes all expired timer
1094 * vectors.
1096 static inline void __run_timers(struct tvec_base *base)
1098 struct timer_list *timer;
1100 spin_lock_irq(&base->lock);
1101 while (time_after_eq(jiffies, base->timer_jiffies)) {
1102 struct list_head work_list;
1103 struct list_head *head = &work_list;
1104 int index = base->timer_jiffies & TVR_MASK;
1107 * Cascade timers:
1109 if (!index &&
1110 (!cascade(base, &base->tv2, INDEX(0))) &&
1111 (!cascade(base, &base->tv3, INDEX(1))) &&
1112 !cascade(base, &base->tv4, INDEX(2)))
1113 cascade(base, &base->tv5, INDEX(3));
1114 ++base->timer_jiffies;
1115 list_replace_init(base->tv1.vec + index, &work_list);
1116 while (!list_empty(head)) {
1117 void (*fn)(unsigned long);
1118 unsigned long data;
1120 timer = list_first_entry(head, struct timer_list,entry);
1121 fn = timer->function;
1122 data = timer->data;
1124 timer_stats_account_timer(timer);
1126 base->running_timer = timer;
1127 detach_timer(timer, 1);
1129 spin_unlock_irq(&base->lock);
1130 call_timer_fn(timer, fn, data);
1131 spin_lock_irq(&base->lock);
1134 base->running_timer = NULL;
1135 spin_unlock_irq(&base->lock);
1138 #ifdef CONFIG_NO_HZ
1140 * Find out when the next timer event is due to happen. This
1141 * is used on S/390 to stop all activity when a CPU is idle.
1142 * This function needs to be called with interrupts disabled.
1144 static unsigned long __next_timer_interrupt(struct tvec_base *base)
1146 unsigned long timer_jiffies = base->timer_jiffies;
1147 unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
1148 int index, slot, array, found = 0;
1149 struct timer_list *nte;
1150 struct tvec *varray[4];
1152 /* Look for timer events in tv1. */
1153 index = slot = timer_jiffies & TVR_MASK;
1154 do {
1155 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
1156 if (tbase_get_deferrable(nte->base))
1157 continue;
1159 found = 1;
1160 expires = nte->expires;
1161 /* Look at the cascade bucket(s)? */
1162 if (!index || slot < index)
1163 goto cascade;
1164 return expires;
1166 slot = (slot + 1) & TVR_MASK;
1167 } while (slot != index);
1169 cascade:
1170 /* Calculate the next cascade event */
1171 if (index)
1172 timer_jiffies += TVR_SIZE - index;
1173 timer_jiffies >>= TVR_BITS;
1175 /* Check tv2-tv5. */
1176 varray[0] = &base->tv2;
1177 varray[1] = &base->tv3;
1178 varray[2] = &base->tv4;
1179 varray[3] = &base->tv5;
1181 for (array = 0; array < 4; array++) {
1182 struct tvec *varp = varray[array];
1184 index = slot = timer_jiffies & TVN_MASK;
1185 do {
1186 list_for_each_entry(nte, varp->vec + slot, entry) {
1187 if (tbase_get_deferrable(nte->base))
1188 continue;
1190 found = 1;
1191 if (time_before(nte->expires, expires))
1192 expires = nte->expires;
1195 * Do we still search for the first timer or are
1196 * we looking up the cascade buckets ?
1198 if (found) {
1199 /* Look at the cascade bucket(s)? */
1200 if (!index || slot < index)
1201 break;
1202 return expires;
1204 slot = (slot + 1) & TVN_MASK;
1205 } while (slot != index);
1207 if (index)
1208 timer_jiffies += TVN_SIZE - index;
1209 timer_jiffies >>= TVN_BITS;
1211 return expires;
1215 * Check, if the next hrtimer event is before the next timer wheel
1216 * event:
1218 static unsigned long cmp_next_hrtimer_event(unsigned long now,
1219 unsigned long expires)
1221 ktime_t hr_delta = hrtimer_get_next_event();
1222 struct timespec tsdelta;
1223 unsigned long delta;
1225 if (hr_delta.tv64 == KTIME_MAX)
1226 return expires;
1229 * Expired timer available, let it expire in the next tick
1231 if (hr_delta.tv64 <= 0)
1232 return now + 1;
1234 tsdelta = ktime_to_timespec(hr_delta);
1235 delta = timespec_to_jiffies(&tsdelta);
1238 * Limit the delta to the max value, which is checked in
1239 * tick_nohz_stop_sched_tick():
1241 if (delta > NEXT_TIMER_MAX_DELTA)
1242 delta = NEXT_TIMER_MAX_DELTA;
1245 * Take rounding errors in to account and make sure, that it
1246 * expires in the next tick. Otherwise we go into an endless
1247 * ping pong due to tick_nohz_stop_sched_tick() retriggering
1248 * the timer softirq
1250 if (delta < 1)
1251 delta = 1;
1252 now += delta;
1253 if (time_before(now, expires))
1254 return now;
1255 return expires;
1259 * get_next_timer_interrupt - return the jiffy of the next pending timer
1260 * @now: current time (in jiffies)
1262 unsigned long get_next_timer_interrupt(unsigned long now)
1264 struct tvec_base *base = __this_cpu_read(tvec_bases);
1265 unsigned long expires;
1268 * Pretend that there is no timer pending if the cpu is offline.
1269 * Possible pending timers will be migrated later to an active cpu.
1271 if (cpu_is_offline(smp_processor_id()))
1272 return now + NEXT_TIMER_MAX_DELTA;
1273 spin_lock(&base->lock);
1274 if (time_before_eq(base->next_timer, base->timer_jiffies))
1275 base->next_timer = __next_timer_interrupt(base);
1276 expires = base->next_timer;
1277 spin_unlock(&base->lock);
1279 if (time_before_eq(expires, now))
1280 return now;
1282 return cmp_next_hrtimer_event(now, expires);
1284 #endif
1287 * Called from the timer interrupt handler to charge one tick to the current
1288 * process. user_tick is 1 if the tick is user time, 0 for system.
1290 void update_process_times(int user_tick)
1292 struct task_struct *p = current;
1293 int cpu = smp_processor_id();
1295 /* Note: this timer irq context must be accounted for as well. */
1296 account_process_tick(p, user_tick);
1297 run_local_timers();
1298 rcu_check_callbacks(cpu, user_tick);
1299 printk_tick();
1300 #ifdef CONFIG_IRQ_WORK
1301 if (in_irq())
1302 irq_work_run();
1303 #endif
1304 scheduler_tick();
1305 run_posix_cpu_timers(p);
1309 * This function runs timers and the timer-tq in bottom half context.
1311 static void run_timer_softirq(struct softirq_action *h)
1313 struct tvec_base *base = __this_cpu_read(tvec_bases);
1315 hrtimer_run_pending();
1317 if (time_after_eq(jiffies, base->timer_jiffies))
1318 __run_timers(base);
1322 * Called by the local, per-CPU timer interrupt on SMP.
1324 void run_local_timers(void)
1326 hrtimer_run_queues();
1327 raise_softirq(TIMER_SOFTIRQ);
1330 #ifdef __ARCH_WANT_SYS_ALARM
1333 * For backwards compatibility? This can be done in libc so Alpha
1334 * and all newer ports shouldn't need it.
1336 SYSCALL_DEFINE1(alarm, unsigned int, seconds)
1338 return alarm_setitimer(seconds);
1341 #endif
1343 #ifndef __alpha__
1346 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
1347 * should be moved into arch/i386 instead?
1351 * sys_getpid - return the thread group id of the current process
1353 * Note, despite the name, this returns the tgid not the pid. The tgid and
1354 * the pid are identical unless CLONE_THREAD was specified on clone() in
1355 * which case the tgid is the same in all threads of the same group.
1357 * This is SMP safe as current->tgid does not change.
1359 SYSCALL_DEFINE0(getpid)
1361 return task_tgid_vnr(current);
1365 * Accessing ->real_parent is not SMP-safe, it could
1366 * change from under us. However, we can use a stale
1367 * value of ->real_parent under rcu_read_lock(), see
1368 * release_task()->call_rcu(delayed_put_task_struct).
1370 SYSCALL_DEFINE0(getppid)
1372 int pid;
1374 rcu_read_lock();
1375 pid = task_tgid_vnr(rcu_dereference(current->real_parent));
1376 rcu_read_unlock();
1378 return pid;
1381 SYSCALL_DEFINE0(getuid)
1383 /* Only we change this so SMP safe */
1384 return current_uid();
1387 SYSCALL_DEFINE0(geteuid)
1389 /* Only we change this so SMP safe */
1390 return current_euid();
1393 SYSCALL_DEFINE0(getgid)
1395 /* Only we change this so SMP safe */
1396 return current_gid();
1399 SYSCALL_DEFINE0(getegid)
1401 /* Only we change this so SMP safe */
1402 return current_egid();
1405 #endif
1407 static void process_timeout(unsigned long __data)
1409 wake_up_process((struct task_struct *)__data);
1413 * schedule_timeout - sleep until timeout
1414 * @timeout: timeout value in jiffies
1416 * Make the current task sleep until @timeout jiffies have
1417 * elapsed. The routine will return immediately unless
1418 * the current task state has been set (see set_current_state()).
1420 * You can set the task state as follows -
1422 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1423 * pass before the routine returns. The routine will return 0
1425 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1426 * delivered to the current task. In this case the remaining time
1427 * in jiffies will be returned, or 0 if the timer expired in time
1429 * The current task state is guaranteed to be TASK_RUNNING when this
1430 * routine returns.
1432 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1433 * the CPU away without a bound on the timeout. In this case the return
1434 * value will be %MAX_SCHEDULE_TIMEOUT.
1436 * In all cases the return value is guaranteed to be non-negative.
1438 signed long __sched schedule_timeout(signed long timeout)
1440 struct timer_list timer;
1441 unsigned long expire;
1443 switch (timeout)
1445 case MAX_SCHEDULE_TIMEOUT:
1447 * These two special cases are useful to be comfortable
1448 * in the caller. Nothing more. We could take
1449 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1450 * but I' d like to return a valid offset (>=0) to allow
1451 * the caller to do everything it want with the retval.
1453 schedule();
1454 goto out;
1455 default:
1457 * Another bit of PARANOID. Note that the retval will be
1458 * 0 since no piece of kernel is supposed to do a check
1459 * for a negative retval of schedule_timeout() (since it
1460 * should never happens anyway). You just have the printk()
1461 * that will tell you if something is gone wrong and where.
1463 if (timeout < 0) {
1464 printk(KERN_ERR "schedule_timeout: wrong timeout "
1465 "value %lx\n", timeout);
1466 dump_stack();
1467 current->state = TASK_RUNNING;
1468 goto out;
1472 expire = timeout + jiffies;
1474 setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
1475 __mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
1476 schedule();
1477 del_singleshot_timer_sync(&timer);
1479 /* Remove the timer from the object tracker */
1480 destroy_timer_on_stack(&timer);
1482 timeout = expire - jiffies;
1484 out:
1485 return timeout < 0 ? 0 : timeout;
1487 EXPORT_SYMBOL(schedule_timeout);
1490 * We can use __set_current_state() here because schedule_timeout() calls
1491 * schedule() unconditionally.
1493 signed long __sched schedule_timeout_interruptible(signed long timeout)
1495 __set_current_state(TASK_INTERRUPTIBLE);
1496 return schedule_timeout(timeout);
1498 EXPORT_SYMBOL(schedule_timeout_interruptible);
1500 signed long __sched schedule_timeout_killable(signed long timeout)
1502 __set_current_state(TASK_KILLABLE);
1503 return schedule_timeout(timeout);
1505 EXPORT_SYMBOL(schedule_timeout_killable);
1507 signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1509 __set_current_state(TASK_UNINTERRUPTIBLE);
1510 return schedule_timeout(timeout);
1512 EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1514 /* Thread ID - the internal kernel "pid" */
1515 SYSCALL_DEFINE0(gettid)
1517 return task_pid_vnr(current);
1521 * do_sysinfo - fill in sysinfo struct
1522 * @info: pointer to buffer to fill
1524 int do_sysinfo(struct sysinfo *info)
1526 unsigned long mem_total, sav_total;
1527 unsigned int mem_unit, bitcount;
1528 struct timespec tp;
1530 memset(info, 0, sizeof(struct sysinfo));
1532 ktime_get_ts(&tp);
1533 monotonic_to_bootbased(&tp);
1534 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1536 get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
1538 info->procs = nr_threads;
1540 si_meminfo(info);
1541 si_swapinfo(info);
1544 * If the sum of all the available memory (i.e. ram + swap)
1545 * is less than can be stored in a 32 bit unsigned long then
1546 * we can be binary compatible with 2.2.x kernels. If not,
1547 * well, in that case 2.2.x was broken anyways...
1549 * -Erik Andersen <andersee@debian.org>
1552 mem_total = info->totalram + info->totalswap;
1553 if (mem_total < info->totalram || mem_total < info->totalswap)
1554 goto out;
1555 bitcount = 0;
1556 mem_unit = info->mem_unit;
1557 while (mem_unit > 1) {
1558 bitcount++;
1559 mem_unit >>= 1;
1560 sav_total = mem_total;
1561 mem_total <<= 1;
1562 if (mem_total < sav_total)
1563 goto out;
1567 * If mem_total did not overflow, multiply all memory values by
1568 * info->mem_unit and set it to 1. This leaves things compatible
1569 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1570 * kernels...
1573 info->mem_unit = 1;
1574 info->totalram <<= bitcount;
1575 info->freeram <<= bitcount;
1576 info->sharedram <<= bitcount;
1577 info->bufferram <<= bitcount;
1578 info->totalswap <<= bitcount;
1579 info->freeswap <<= bitcount;
1580 info->totalhigh <<= bitcount;
1581 info->freehigh <<= bitcount;
1583 out:
1584 return 0;
1587 SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
1589 struct sysinfo val;
1591 do_sysinfo(&val);
1593 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1594 return -EFAULT;
1596 return 0;
1599 static int __cpuinit init_timers_cpu(int cpu)
1601 int j;
1602 struct tvec_base *base;
1603 static char __cpuinitdata tvec_base_done[NR_CPUS];
1605 if (!tvec_base_done[cpu]) {
1606 static char boot_done;
1608 if (boot_done) {
1610 * The APs use this path later in boot
1612 base = kmalloc_node(sizeof(*base),
1613 GFP_KERNEL | __GFP_ZERO,
1614 cpu_to_node(cpu));
1615 if (!base)
1616 return -ENOMEM;
1618 /* Make sure that tvec_base is 2 byte aligned */
1619 if (tbase_get_deferrable(base)) {
1620 WARN_ON(1);
1621 kfree(base);
1622 return -ENOMEM;
1624 per_cpu(tvec_bases, cpu) = base;
1625 } else {
1627 * This is for the boot CPU - we use compile-time
1628 * static initialisation because per-cpu memory isn't
1629 * ready yet and because the memory allocators are not
1630 * initialised either.
1632 boot_done = 1;
1633 base = &boot_tvec_bases;
1635 spin_lock_init(&base->lock);
1636 tvec_base_done[cpu] = 1;
1637 } else {
1638 base = per_cpu(tvec_bases, cpu);
1642 for (j = 0; j < TVN_SIZE; j++) {
1643 INIT_LIST_HEAD(base->tv5.vec + j);
1644 INIT_LIST_HEAD(base->tv4.vec + j);
1645 INIT_LIST_HEAD(base->tv3.vec + j);
1646 INIT_LIST_HEAD(base->tv2.vec + j);
1648 for (j = 0; j < TVR_SIZE; j++)
1649 INIT_LIST_HEAD(base->tv1.vec + j);
1651 base->timer_jiffies = jiffies;
1652 base->next_timer = base->timer_jiffies;
1653 return 0;
1656 #ifdef CONFIG_HOTPLUG_CPU
1657 static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
1659 struct timer_list *timer;
1661 while (!list_empty(head)) {
1662 timer = list_first_entry(head, struct timer_list, entry);
1663 detach_timer(timer, 0);
1664 timer_set_base(timer, new_base);
1665 if (time_before(timer->expires, new_base->next_timer) &&
1666 !tbase_get_deferrable(timer->base))
1667 new_base->next_timer = timer->expires;
1668 internal_add_timer(new_base, timer);
1672 static void __cpuinit migrate_timers(int cpu)
1674 struct tvec_base *old_base;
1675 struct tvec_base *new_base;
1676 int i;
1678 BUG_ON(cpu_online(cpu));
1679 old_base = per_cpu(tvec_bases, cpu);
1680 new_base = get_cpu_var(tvec_bases);
1682 * The caller is globally serialized and nobody else
1683 * takes two locks at once, deadlock is not possible.
1685 spin_lock_irq(&new_base->lock);
1686 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1688 BUG_ON(old_base->running_timer);
1690 for (i = 0; i < TVR_SIZE; i++)
1691 migrate_timer_list(new_base, old_base->tv1.vec + i);
1692 for (i = 0; i < TVN_SIZE; i++) {
1693 migrate_timer_list(new_base, old_base->tv2.vec + i);
1694 migrate_timer_list(new_base, old_base->tv3.vec + i);
1695 migrate_timer_list(new_base, old_base->tv4.vec + i);
1696 migrate_timer_list(new_base, old_base->tv5.vec + i);
1699 spin_unlock(&old_base->lock);
1700 spin_unlock_irq(&new_base->lock);
1701 put_cpu_var(tvec_bases);
1703 #endif /* CONFIG_HOTPLUG_CPU */
1705 static int __cpuinit timer_cpu_notify(struct notifier_block *self,
1706 unsigned long action, void *hcpu)
1708 long cpu = (long)hcpu;
1709 int err;
1711 switch(action) {
1712 case CPU_UP_PREPARE:
1713 case CPU_UP_PREPARE_FROZEN:
1714 err = init_timers_cpu(cpu);
1715 if (err < 0)
1716 return notifier_from_errno(err);
1717 break;
1718 #ifdef CONFIG_HOTPLUG_CPU
1719 case CPU_DEAD:
1720 case CPU_DEAD_FROZEN:
1721 migrate_timers(cpu);
1722 break;
1723 #endif
1724 default:
1725 break;
1727 return NOTIFY_OK;
1730 static struct notifier_block __cpuinitdata timers_nb = {
1731 .notifier_call = timer_cpu_notify,
1735 void __init init_timers(void)
1737 int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1738 (void *)(long)smp_processor_id());
1740 init_timer_stats();
1742 BUG_ON(err != NOTIFY_OK);
1743 register_cpu_notifier(&timers_nb);
1744 open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
1748 * msleep - sleep safely even with waitqueue interruptions
1749 * @msecs: Time in milliseconds to sleep for
1751 void msleep(unsigned int msecs)
1753 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1755 while (timeout)
1756 timeout = schedule_timeout_uninterruptible(timeout);
1759 EXPORT_SYMBOL(msleep);
1762 * msleep_interruptible - sleep waiting for signals
1763 * @msecs: Time in milliseconds to sleep for
1765 unsigned long msleep_interruptible(unsigned int msecs)
1767 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1769 while (timeout && !signal_pending(current))
1770 timeout = schedule_timeout_interruptible(timeout);
1771 return jiffies_to_msecs(timeout);
1774 EXPORT_SYMBOL(msleep_interruptible);
1776 static int __sched do_usleep_range(unsigned long min, unsigned long max)
1778 ktime_t kmin;
1779 unsigned long delta;
1781 kmin = ktime_set(0, min * NSEC_PER_USEC);
1782 delta = (max - min) * NSEC_PER_USEC;
1783 return schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL);
1787 * usleep_range - Drop in replacement for udelay where wakeup is flexible
1788 * @min: Minimum time in usecs to sleep
1789 * @max: Maximum time in usecs to sleep
1791 void usleep_range(unsigned long min, unsigned long max)
1793 __set_current_state(TASK_UNINTERRUPTIBLE);
1794 do_usleep_range(min, max);
1796 EXPORT_SYMBOL(usleep_range);