4 * Kernel internal timers
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>
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/sched/sysctl.h>
43 #include <linux/slab.h>
44 #include <linux/compat.h>
46 #include <asm/uaccess.h>
47 #include <asm/unistd.h>
48 #include <asm/div64.h>
49 #include <asm/timex.h>
52 #define CREATE_TRACE_POINTS
53 #include <trace/events/timer.h>
55 u64 jiffies_64 __cacheline_aligned_in_smp
= INITIAL_JIFFIES
;
57 EXPORT_SYMBOL(jiffies_64
);
60 * per-CPU timer vector definitions:
62 #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
63 #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
64 #define TVN_SIZE (1 << TVN_BITS)
65 #define TVR_SIZE (1 << TVR_BITS)
66 #define TVN_MASK (TVN_SIZE - 1)
67 #define TVR_MASK (TVR_SIZE - 1)
68 #define MAX_TVAL ((unsigned long)((1ULL << (TVR_BITS + 4*TVN_BITS)) - 1))
71 struct list_head vec
[TVN_SIZE
];
75 struct list_head vec
[TVR_SIZE
];
80 struct timer_list
*running_timer
;
81 unsigned long timer_jiffies
;
82 unsigned long next_timer
;
83 unsigned long active_timers
;
89 } ____cacheline_aligned
;
91 struct tvec_base boot_tvec_bases
;
92 EXPORT_SYMBOL(boot_tvec_bases
);
93 static DEFINE_PER_CPU(struct tvec_base
*, tvec_bases
) = &boot_tvec_bases
;
95 /* Functions below help us manage 'deferrable' flag */
96 static inline unsigned int tbase_get_deferrable(struct tvec_base
*base
)
98 return ((unsigned int)(unsigned long)base
& TIMER_DEFERRABLE
);
101 static inline unsigned int tbase_get_irqsafe(struct tvec_base
*base
)
103 return ((unsigned int)(unsigned long)base
& TIMER_IRQSAFE
);
106 static inline struct tvec_base
*tbase_get_base(struct tvec_base
*base
)
108 return ((struct tvec_base
*)((unsigned long)base
& ~TIMER_FLAG_MASK
));
112 timer_set_base(struct timer_list
*timer
, struct tvec_base
*new_base
)
114 unsigned long flags
= (unsigned long)timer
->base
& TIMER_FLAG_MASK
;
116 timer
->base
= (struct tvec_base
*)((unsigned long)(new_base
) | flags
);
119 static unsigned long round_jiffies_common(unsigned long j
, int cpu
,
123 unsigned long original
= j
;
126 * We don't want all cpus firing their timers at once hitting the
127 * same lock or cachelines, so we skew each extra cpu with an extra
128 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
130 * The skew is done by adding 3*cpunr, then round, then subtract this
131 * extra offset again.
138 * If the target jiffie is just after a whole second (which can happen
139 * due to delays of the timer irq, long irq off times etc etc) then
140 * we should round down to the whole second, not up. Use 1/4th second
141 * as cutoff for this rounding as an extreme upper bound for this.
142 * But never round down if @force_up is set.
144 if (rem
< HZ
/4 && !force_up
) /* round down */
149 /* now that we have rounded, subtract the extra skew again */
153 * Make sure j is still in the future. Otherwise return the
156 return time_is_after_jiffies(j
) ? j
: original
;
160 * __round_jiffies - function to round jiffies to a full second
161 * @j: the time in (absolute) jiffies that should be rounded
162 * @cpu: the processor number on which the timeout will happen
164 * __round_jiffies() rounds an absolute time in the future (in jiffies)
165 * up or down to (approximately) full seconds. This is useful for timers
166 * for which the exact time they fire does not matter too much, as long as
167 * they fire approximately every X seconds.
169 * By rounding these timers to whole seconds, all such timers will fire
170 * at the same time, rather than at various times spread out. The goal
171 * of this is to have the CPU wake up less, which saves power.
173 * The exact rounding is skewed for each processor to avoid all
174 * processors firing at the exact same time, which could lead
175 * to lock contention or spurious cache line bouncing.
177 * The return value is the rounded version of the @j parameter.
179 unsigned long __round_jiffies(unsigned long j
, int cpu
)
181 return round_jiffies_common(j
, cpu
, false);
183 EXPORT_SYMBOL_GPL(__round_jiffies
);
186 * __round_jiffies_relative - function to round jiffies to a full second
187 * @j: the time in (relative) jiffies that should be rounded
188 * @cpu: the processor number on which the timeout will happen
190 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
191 * up or down to (approximately) full seconds. This is useful for timers
192 * for which the exact time they fire does not matter too much, as long as
193 * they fire approximately every X seconds.
195 * By rounding these timers to whole seconds, all such timers will fire
196 * at the same time, rather than at various times spread out. The goal
197 * of this is to have the CPU wake up less, which saves power.
199 * The exact rounding is skewed for each processor to avoid all
200 * processors firing at the exact same time, which could lead
201 * to lock contention or spurious cache line bouncing.
203 * The return value is the rounded version of the @j parameter.
205 unsigned long __round_jiffies_relative(unsigned long j
, int cpu
)
207 unsigned long j0
= jiffies
;
209 /* Use j0 because jiffies might change while we run */
210 return round_jiffies_common(j
+ j0
, cpu
, false) - j0
;
212 EXPORT_SYMBOL_GPL(__round_jiffies_relative
);
215 * round_jiffies - function to round jiffies to a full second
216 * @j: the time in (absolute) jiffies that should be rounded
218 * round_jiffies() rounds an absolute time in the future (in jiffies)
219 * up or down to (approximately) full seconds. This is useful for timers
220 * for which the exact time they fire does not matter too much, as long as
221 * they fire approximately every X seconds.
223 * By rounding these timers to whole seconds, all such timers will fire
224 * at the same time, rather than at various times spread out. The goal
225 * of this is to have the CPU wake up less, which saves power.
227 * The return value is the rounded version of the @j parameter.
229 unsigned long round_jiffies(unsigned long j
)
231 return round_jiffies_common(j
, raw_smp_processor_id(), false);
233 EXPORT_SYMBOL_GPL(round_jiffies
);
236 * round_jiffies_relative - function to round jiffies to a full second
237 * @j: the time in (relative) jiffies that should be rounded
239 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
240 * up or down to (approximately) full seconds. This is useful for timers
241 * for which the exact time they fire does not matter too much, as long as
242 * they fire approximately every X seconds.
244 * By rounding these timers to whole seconds, all such timers will fire
245 * at the same time, rather than at various times spread out. The goal
246 * of this is to have the CPU wake up less, which saves power.
248 * The return value is the rounded version of the @j parameter.
250 unsigned long round_jiffies_relative(unsigned long j
)
252 return __round_jiffies_relative(j
, raw_smp_processor_id());
254 EXPORT_SYMBOL_GPL(round_jiffies_relative
);
257 * __round_jiffies_up - function to round jiffies up to a full second
258 * @j: the time in (absolute) jiffies that should be rounded
259 * @cpu: the processor number on which the timeout will happen
261 * This is the same as __round_jiffies() except that it will never
262 * round down. This is useful for timeouts for which the exact time
263 * of firing does not matter too much, as long as they don't fire too
266 unsigned long __round_jiffies_up(unsigned long j
, int cpu
)
268 return round_jiffies_common(j
, cpu
, true);
270 EXPORT_SYMBOL_GPL(__round_jiffies_up
);
273 * __round_jiffies_up_relative - function to round jiffies up to a full second
274 * @j: the time in (relative) jiffies that should be rounded
275 * @cpu: the processor number on which the timeout will happen
277 * This is the same as __round_jiffies_relative() except that it will never
278 * round down. This is useful for timeouts for which the exact time
279 * of firing does not matter too much, as long as they don't fire too
282 unsigned long __round_jiffies_up_relative(unsigned long j
, int cpu
)
284 unsigned long j0
= jiffies
;
286 /* Use j0 because jiffies might change while we run */
287 return round_jiffies_common(j
+ j0
, cpu
, true) - j0
;
289 EXPORT_SYMBOL_GPL(__round_jiffies_up_relative
);
292 * round_jiffies_up - function to round jiffies up to a full second
293 * @j: the time in (absolute) jiffies that should be rounded
295 * This is the same as round_jiffies() except that it will never
296 * round down. This is useful for timeouts for which the exact time
297 * of firing does not matter too much, as long as they don't fire too
300 unsigned long round_jiffies_up(unsigned long j
)
302 return round_jiffies_common(j
, raw_smp_processor_id(), true);
304 EXPORT_SYMBOL_GPL(round_jiffies_up
);
307 * round_jiffies_up_relative - function to round jiffies up to a full second
308 * @j: the time in (relative) jiffies that should be rounded
310 * This is the same as round_jiffies_relative() except that it will never
311 * round down. This is useful for timeouts for which the exact time
312 * of firing does not matter too much, as long as they don't fire too
315 unsigned long round_jiffies_up_relative(unsigned long j
)
317 return __round_jiffies_up_relative(j
, raw_smp_processor_id());
319 EXPORT_SYMBOL_GPL(round_jiffies_up_relative
);
322 * set_timer_slack - set the allowed slack for a timer
323 * @timer: the timer to be modified
324 * @slack_hz: the amount of time (in jiffies) allowed for rounding
326 * Set the amount of time, in jiffies, that a certain timer has
327 * in terms of slack. By setting this value, the timer subsystem
328 * will schedule the actual timer somewhere between
329 * the time mod_timer() asks for, and that time plus the slack.
331 * By setting the slack to -1, a percentage of the delay is used
334 void set_timer_slack(struct timer_list
*timer
, int slack_hz
)
336 timer
->slack
= slack_hz
;
338 EXPORT_SYMBOL_GPL(set_timer_slack
);
341 __internal_add_timer(struct tvec_base
*base
, struct timer_list
*timer
)
343 unsigned long expires
= timer
->expires
;
344 unsigned long idx
= expires
- base
->timer_jiffies
;
345 struct list_head
*vec
;
347 if (idx
< TVR_SIZE
) {
348 int i
= expires
& TVR_MASK
;
349 vec
= base
->tv1
.vec
+ i
;
350 } else if (idx
< 1 << (TVR_BITS
+ TVN_BITS
)) {
351 int i
= (expires
>> TVR_BITS
) & TVN_MASK
;
352 vec
= base
->tv2
.vec
+ i
;
353 } else if (idx
< 1 << (TVR_BITS
+ 2 * TVN_BITS
)) {
354 int i
= (expires
>> (TVR_BITS
+ TVN_BITS
)) & TVN_MASK
;
355 vec
= base
->tv3
.vec
+ i
;
356 } else if (idx
< 1 << (TVR_BITS
+ 3 * TVN_BITS
)) {
357 int i
= (expires
>> (TVR_BITS
+ 2 * TVN_BITS
)) & TVN_MASK
;
358 vec
= base
->tv4
.vec
+ i
;
359 } else if ((signed long) idx
< 0) {
361 * Can happen if you add a timer with expires == jiffies,
362 * or you set a timer to go off in the past
364 vec
= base
->tv1
.vec
+ (base
->timer_jiffies
& TVR_MASK
);
367 /* If the timeout is larger than MAX_TVAL (on 64-bit
368 * architectures or with CONFIG_BASE_SMALL=1) then we
369 * use the maximum timeout.
371 if (idx
> MAX_TVAL
) {
373 expires
= idx
+ base
->timer_jiffies
;
375 i
= (expires
>> (TVR_BITS
+ 3 * TVN_BITS
)) & TVN_MASK
;
376 vec
= base
->tv5
.vec
+ i
;
381 list_add_tail(&timer
->entry
, vec
);
384 static void internal_add_timer(struct tvec_base
*base
, struct timer_list
*timer
)
386 __internal_add_timer(base
, timer
);
388 * Update base->active_timers and base->next_timer
390 if (!tbase_get_deferrable(timer
->base
)) {
391 if (time_before(timer
->expires
, base
->next_timer
))
392 base
->next_timer
= timer
->expires
;
393 base
->active_timers
++;
397 #ifdef CONFIG_TIMER_STATS
398 void __timer_stats_timer_set_start_info(struct timer_list
*timer
, void *addr
)
400 if (timer
->start_site
)
403 timer
->start_site
= addr
;
404 memcpy(timer
->start_comm
, current
->comm
, TASK_COMM_LEN
);
405 timer
->start_pid
= current
->pid
;
408 static void timer_stats_account_timer(struct timer_list
*timer
)
410 unsigned int flag
= 0;
412 if (likely(!timer
->start_site
))
414 if (unlikely(tbase_get_deferrable(timer
->base
)))
415 flag
|= TIMER_STATS_FLAG_DEFERRABLE
;
417 timer_stats_update_stats(timer
, timer
->start_pid
, timer
->start_site
,
418 timer
->function
, timer
->start_comm
, flag
);
422 static void timer_stats_account_timer(struct timer_list
*timer
) {}
425 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
427 static struct debug_obj_descr timer_debug_descr
;
429 static void *timer_debug_hint(void *addr
)
431 return ((struct timer_list
*) addr
)->function
;
435 * fixup_init is called when:
436 * - an active object is initialized
438 static int timer_fixup_init(void *addr
, enum debug_obj_state state
)
440 struct timer_list
*timer
= addr
;
443 case ODEBUG_STATE_ACTIVE
:
444 del_timer_sync(timer
);
445 debug_object_init(timer
, &timer_debug_descr
);
452 /* Stub timer callback for improperly used timers. */
453 static void stub_timer(unsigned long data
)
459 * fixup_activate is called when:
460 * - an active object is activated
461 * - an unknown object is activated (might be a statically initialized object)
463 static int timer_fixup_activate(void *addr
, enum debug_obj_state state
)
465 struct timer_list
*timer
= addr
;
469 case ODEBUG_STATE_NOTAVAILABLE
:
471 * This is not really a fixup. The timer was
472 * statically initialized. We just make sure that it
473 * is tracked in the object tracker.
475 if (timer
->entry
.next
== NULL
&&
476 timer
->entry
.prev
== TIMER_ENTRY_STATIC
) {
477 debug_object_init(timer
, &timer_debug_descr
);
478 debug_object_activate(timer
, &timer_debug_descr
);
481 setup_timer(timer
, stub_timer
, 0);
486 case ODEBUG_STATE_ACTIVE
:
495 * fixup_free is called when:
496 * - an active object is freed
498 static int timer_fixup_free(void *addr
, enum debug_obj_state state
)
500 struct timer_list
*timer
= addr
;
503 case ODEBUG_STATE_ACTIVE
:
504 del_timer_sync(timer
);
505 debug_object_free(timer
, &timer_debug_descr
);
513 * fixup_assert_init is called when:
514 * - an untracked/uninit-ed object is found
516 static int timer_fixup_assert_init(void *addr
, enum debug_obj_state state
)
518 struct timer_list
*timer
= addr
;
521 case ODEBUG_STATE_NOTAVAILABLE
:
522 if (timer
->entry
.prev
== TIMER_ENTRY_STATIC
) {
524 * This is not really a fixup. The timer was
525 * statically initialized. We just make sure that it
526 * is tracked in the object tracker.
528 debug_object_init(timer
, &timer_debug_descr
);
531 setup_timer(timer
, stub_timer
, 0);
539 static struct debug_obj_descr timer_debug_descr
= {
540 .name
= "timer_list",
541 .debug_hint
= timer_debug_hint
,
542 .fixup_init
= timer_fixup_init
,
543 .fixup_activate
= timer_fixup_activate
,
544 .fixup_free
= timer_fixup_free
,
545 .fixup_assert_init
= timer_fixup_assert_init
,
548 static inline void debug_timer_init(struct timer_list
*timer
)
550 debug_object_init(timer
, &timer_debug_descr
);
553 static inline void debug_timer_activate(struct timer_list
*timer
)
555 debug_object_activate(timer
, &timer_debug_descr
);
558 static inline void debug_timer_deactivate(struct timer_list
*timer
)
560 debug_object_deactivate(timer
, &timer_debug_descr
);
563 static inline void debug_timer_free(struct timer_list
*timer
)
565 debug_object_free(timer
, &timer_debug_descr
);
568 static inline void debug_timer_assert_init(struct timer_list
*timer
)
570 debug_object_assert_init(timer
, &timer_debug_descr
);
573 static void do_init_timer(struct timer_list
*timer
, unsigned int flags
,
574 const char *name
, struct lock_class_key
*key
);
576 void init_timer_on_stack_key(struct timer_list
*timer
, unsigned int flags
,
577 const char *name
, struct lock_class_key
*key
)
579 debug_object_init_on_stack(timer
, &timer_debug_descr
);
580 do_init_timer(timer
, flags
, name
, key
);
582 EXPORT_SYMBOL_GPL(init_timer_on_stack_key
);
584 void destroy_timer_on_stack(struct timer_list
*timer
)
586 debug_object_free(timer
, &timer_debug_descr
);
588 EXPORT_SYMBOL_GPL(destroy_timer_on_stack
);
591 static inline void debug_timer_init(struct timer_list
*timer
) { }
592 static inline void debug_timer_activate(struct timer_list
*timer
) { }
593 static inline void debug_timer_deactivate(struct timer_list
*timer
) { }
594 static inline void debug_timer_assert_init(struct timer_list
*timer
) { }
597 static inline void debug_init(struct timer_list
*timer
)
599 debug_timer_init(timer
);
600 trace_timer_init(timer
);
604 debug_activate(struct timer_list
*timer
, unsigned long expires
)
606 debug_timer_activate(timer
);
607 trace_timer_start(timer
, expires
);
610 static inline void debug_deactivate(struct timer_list
*timer
)
612 debug_timer_deactivate(timer
);
613 trace_timer_cancel(timer
);
616 static inline void debug_assert_init(struct timer_list
*timer
)
618 debug_timer_assert_init(timer
);
621 static void do_init_timer(struct timer_list
*timer
, unsigned int flags
,
622 const char *name
, struct lock_class_key
*key
)
624 struct tvec_base
*base
= __raw_get_cpu_var(tvec_bases
);
626 timer
->entry
.next
= NULL
;
627 timer
->base
= (void *)((unsigned long)base
| flags
);
629 #ifdef CONFIG_TIMER_STATS
630 timer
->start_site
= NULL
;
631 timer
->start_pid
= -1;
632 memset(timer
->start_comm
, 0, TASK_COMM_LEN
);
634 lockdep_init_map(&timer
->lockdep_map
, name
, key
, 0);
638 * init_timer_key - initialize a timer
639 * @timer: the timer to be initialized
640 * @flags: timer flags
641 * @name: name of the timer
642 * @key: lockdep class key of the fake lock used for tracking timer
643 * sync lock dependencies
645 * init_timer_key() must be done to a timer prior calling *any* of the
646 * other timer functions.
648 void init_timer_key(struct timer_list
*timer
, unsigned int flags
,
649 const char *name
, struct lock_class_key
*key
)
652 do_init_timer(timer
, flags
, name
, key
);
654 EXPORT_SYMBOL(init_timer_key
);
656 static inline void detach_timer(struct timer_list
*timer
, bool clear_pending
)
658 struct list_head
*entry
= &timer
->entry
;
660 debug_deactivate(timer
);
662 __list_del(entry
->prev
, entry
->next
);
665 entry
->prev
= LIST_POISON2
;
669 detach_expired_timer(struct timer_list
*timer
, struct tvec_base
*base
)
671 detach_timer(timer
, true);
672 if (!tbase_get_deferrable(timer
->base
))
673 base
->active_timers
--;
676 static int detach_if_pending(struct timer_list
*timer
, struct tvec_base
*base
,
679 if (!timer_pending(timer
))
682 detach_timer(timer
, clear_pending
);
683 if (!tbase_get_deferrable(timer
->base
)) {
684 base
->active_timers
--;
685 if (timer
->expires
== base
->next_timer
)
686 base
->next_timer
= base
->timer_jiffies
;
692 * We are using hashed locking: holding per_cpu(tvec_bases).lock
693 * means that all timers which are tied to this base via timer->base are
694 * locked, and the base itself is locked too.
696 * So __run_timers/migrate_timers can safely modify all timers which could
697 * be found on ->tvX lists.
699 * When the timer's base is locked, and the timer removed from list, it is
700 * possible to set timer->base = NULL and drop the lock: the timer remains
703 static struct tvec_base
*lock_timer_base(struct timer_list
*timer
,
704 unsigned long *flags
)
705 __acquires(timer
->base
->lock
)
707 struct tvec_base
*base
;
710 struct tvec_base
*prelock_base
= timer
->base
;
711 base
= tbase_get_base(prelock_base
);
712 if (likely(base
!= NULL
)) {
713 spin_lock_irqsave(&base
->lock
, *flags
);
714 if (likely(prelock_base
== timer
->base
))
716 /* The timer has migrated to another CPU */
717 spin_unlock_irqrestore(&base
->lock
, *flags
);
724 __mod_timer(struct timer_list
*timer
, unsigned long expires
,
725 bool pending_only
, int pinned
)
727 struct tvec_base
*base
, *new_base
;
731 timer_stats_timer_set_start_info(timer
);
732 BUG_ON(!timer
->function
);
734 base
= lock_timer_base(timer
, &flags
);
736 ret
= detach_if_pending(timer
, base
, false);
737 if (!ret
&& pending_only
)
740 debug_activate(timer
, expires
);
742 cpu
= smp_processor_id();
744 #if defined(CONFIG_NO_HZ_COMMON) && defined(CONFIG_SMP)
745 if (!pinned
&& get_sysctl_timer_migration() && idle_cpu(cpu
))
746 cpu
= get_nohz_timer_target();
748 new_base
= per_cpu(tvec_bases
, cpu
);
750 if (base
!= new_base
) {
752 * We are trying to schedule the timer on the local CPU.
753 * However we can't change timer's base while it is running,
754 * otherwise del_timer_sync() can't detect that the timer's
755 * handler yet has not finished. This also guarantees that
756 * the timer is serialized wrt itself.
758 if (likely(base
->running_timer
!= timer
)) {
759 /* See the comment in lock_timer_base() */
760 timer_set_base(timer
, NULL
);
761 spin_unlock(&base
->lock
);
763 spin_lock(&base
->lock
);
764 timer_set_base(timer
, base
);
768 timer
->expires
= expires
;
769 internal_add_timer(base
, timer
);
772 spin_unlock_irqrestore(&base
->lock
, flags
);
778 * mod_timer_pending - modify a pending timer's timeout
779 * @timer: the pending timer to be modified
780 * @expires: new timeout in jiffies
782 * mod_timer_pending() is the same for pending timers as mod_timer(),
783 * but will not re-activate and modify already deleted timers.
785 * It is useful for unserialized use of timers.
787 int mod_timer_pending(struct timer_list
*timer
, unsigned long expires
)
789 return __mod_timer(timer
, expires
, true, TIMER_NOT_PINNED
);
791 EXPORT_SYMBOL(mod_timer_pending
);
794 * Decide where to put the timer while taking the slack into account
797 * 1) calculate the maximum (absolute) time
798 * 2) calculate the highest bit where the expires and new max are different
799 * 3) use this bit to make a mask
800 * 4) use the bitmask to round down the maximum time, so that all last
804 unsigned long apply_slack(struct timer_list
*timer
, unsigned long expires
)
806 unsigned long expires_limit
, mask
;
809 if (timer
->slack
>= 0) {
810 expires_limit
= expires
+ timer
->slack
;
812 long delta
= expires
- jiffies
;
817 expires_limit
= expires
+ delta
/ 256;
819 mask
= expires
^ expires_limit
;
823 bit
= find_last_bit(&mask
, BITS_PER_LONG
);
825 mask
= (1UL << bit
) - 1;
827 expires_limit
= expires_limit
& ~(mask
);
829 return expires_limit
;
833 * mod_timer - modify a timer's timeout
834 * @timer: the timer to be modified
835 * @expires: new timeout in jiffies
837 * mod_timer() is a more efficient way to update the expire field of an
838 * active timer (if the timer is inactive it will be activated)
840 * mod_timer(timer, expires) is equivalent to:
842 * del_timer(timer); timer->expires = expires; add_timer(timer);
844 * Note that if there are multiple unserialized concurrent users of the
845 * same timer, then mod_timer() is the only safe way to modify the timeout,
846 * since add_timer() cannot modify an already running timer.
848 * The function returns whether it has modified a pending timer or not.
849 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
850 * active timer returns 1.)
852 int mod_timer(struct timer_list
*timer
, unsigned long expires
)
854 expires
= apply_slack(timer
, expires
);
857 * This is a common optimization triggered by the
858 * networking code - if the timer is re-modified
859 * to be the same thing then just return:
861 if (timer_pending(timer
) && timer
->expires
== expires
)
864 return __mod_timer(timer
, expires
, false, TIMER_NOT_PINNED
);
866 EXPORT_SYMBOL(mod_timer
);
869 * mod_timer_pinned - modify a timer's timeout
870 * @timer: the timer to be modified
871 * @expires: new timeout in jiffies
873 * mod_timer_pinned() is a way to update the expire field of an
874 * active timer (if the timer is inactive it will be activated)
875 * and to ensure that the timer is scheduled on the current CPU.
877 * Note that this does not prevent the timer from being migrated
878 * when the current CPU goes offline. If this is a problem for
879 * you, use CPU-hotplug notifiers to handle it correctly, for
880 * example, cancelling the timer when the corresponding CPU goes
883 * mod_timer_pinned(timer, expires) is equivalent to:
885 * del_timer(timer); timer->expires = expires; add_timer(timer);
887 int mod_timer_pinned(struct timer_list
*timer
, unsigned long expires
)
889 if (timer
->expires
== expires
&& timer_pending(timer
))
892 return __mod_timer(timer
, expires
, false, TIMER_PINNED
);
894 EXPORT_SYMBOL(mod_timer_pinned
);
897 * add_timer - start a timer
898 * @timer: the timer to be added
900 * The kernel will do a ->function(->data) callback from the
901 * timer interrupt at the ->expires point in the future. The
902 * current time is 'jiffies'.
904 * The timer's ->expires, ->function (and if the handler uses it, ->data)
905 * fields must be set prior calling this function.
907 * Timers with an ->expires field in the past will be executed in the next
910 void add_timer(struct timer_list
*timer
)
912 BUG_ON(timer_pending(timer
));
913 mod_timer(timer
, timer
->expires
);
915 EXPORT_SYMBOL(add_timer
);
918 * add_timer_on - start a timer on a particular CPU
919 * @timer: the timer to be added
920 * @cpu: the CPU to start it on
922 * This is not very scalable on SMP. Double adds are not possible.
924 void add_timer_on(struct timer_list
*timer
, int cpu
)
926 struct tvec_base
*new_base
= per_cpu(tvec_bases
, cpu
);
927 struct tvec_base
*base
;
930 timer_stats_timer_set_start_info(timer
);
931 BUG_ON(timer_pending(timer
) || !timer
->function
);
934 * If @timer was on a different CPU, it should be migrated with the
935 * old base locked to prevent other operations proceeding with the
936 * wrong base locked. See lock_timer_base().
938 base
= lock_timer_base(timer
, &flags
);
939 if (base
!= new_base
) {
940 timer_set_base(timer
, NULL
);
941 spin_unlock(&base
->lock
);
943 spin_lock(&base
->lock
);
944 timer_set_base(timer
, base
);
946 debug_activate(timer
, timer
->expires
);
947 internal_add_timer(base
, timer
);
949 * Check whether the other CPU is in dynticks mode and needs
950 * to be triggered to reevaluate the timer wheel.
951 * We are protected against the other CPU fiddling
952 * with the timer by holding the timer base lock. This also
953 * makes sure that a CPU on the way to stop its tick can not
954 * evaluate the timer wheel.
956 wake_up_nohz_cpu(cpu
);
957 spin_unlock_irqrestore(&base
->lock
, flags
);
959 EXPORT_SYMBOL_GPL(add_timer_on
);
962 * del_timer - deactive a timer.
963 * @timer: the timer to be deactivated
965 * del_timer() deactivates a timer - this works on both active and inactive
968 * The function returns whether it has deactivated a pending timer or not.
969 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
970 * active timer returns 1.)
972 int del_timer(struct timer_list
*timer
)
974 struct tvec_base
*base
;
978 debug_assert_init(timer
);
980 timer_stats_timer_clear_start_info(timer
);
981 if (timer_pending(timer
)) {
982 base
= lock_timer_base(timer
, &flags
);
983 ret
= detach_if_pending(timer
, base
, true);
984 spin_unlock_irqrestore(&base
->lock
, flags
);
989 EXPORT_SYMBOL(del_timer
);
992 * try_to_del_timer_sync - Try to deactivate a timer
993 * @timer: timer do del
995 * This function tries to deactivate a timer. Upon successful (ret >= 0)
996 * exit the timer is not queued and the handler is not running on any CPU.
998 int try_to_del_timer_sync(struct timer_list
*timer
)
1000 struct tvec_base
*base
;
1001 unsigned long flags
;
1004 debug_assert_init(timer
);
1006 base
= lock_timer_base(timer
, &flags
);
1008 if (base
->running_timer
!= timer
) {
1009 timer_stats_timer_clear_start_info(timer
);
1010 ret
= detach_if_pending(timer
, base
, true);
1012 spin_unlock_irqrestore(&base
->lock
, flags
);
1016 EXPORT_SYMBOL(try_to_del_timer_sync
);
1020 * del_timer_sync - deactivate a timer and wait for the handler to finish.
1021 * @timer: the timer to be deactivated
1023 * This function only differs from del_timer() on SMP: besides deactivating
1024 * the timer it also makes sure the handler has finished executing on other
1027 * Synchronization rules: Callers must prevent restarting of the timer,
1028 * otherwise this function is meaningless. It must not be called from
1029 * interrupt contexts unless the timer is an irqsafe one. The caller must
1030 * not hold locks which would prevent completion of the timer's
1031 * handler. The timer's handler must not call add_timer_on(). Upon exit the
1032 * timer is not queued and the handler is not running on any CPU.
1034 * Note: For !irqsafe timers, you must not hold locks that are held in
1035 * interrupt context while calling this function. Even if the lock has
1036 * nothing to do with the timer in question. Here's why:
1042 * base->running_timer = mytimer;
1043 * spin_lock_irq(somelock);
1045 * spin_lock(somelock);
1046 * del_timer_sync(mytimer);
1047 * while (base->running_timer == mytimer);
1049 * Now del_timer_sync() will never return and never release somelock.
1050 * The interrupt on the other CPU is waiting to grab somelock but
1051 * it has interrupted the softirq that CPU0 is waiting to finish.
1053 * The function returns whether it has deactivated a pending timer or not.
1055 int del_timer_sync(struct timer_list
*timer
)
1057 #ifdef CONFIG_LOCKDEP
1058 unsigned long flags
;
1061 * If lockdep gives a backtrace here, please reference
1062 * the synchronization rules above.
1064 local_irq_save(flags
);
1065 lock_map_acquire(&timer
->lockdep_map
);
1066 lock_map_release(&timer
->lockdep_map
);
1067 local_irq_restore(flags
);
1070 * don't use it in hardirq context, because it
1071 * could lead to deadlock.
1073 WARN_ON(in_irq() && !tbase_get_irqsafe(timer
->base
));
1075 int ret
= try_to_del_timer_sync(timer
);
1081 EXPORT_SYMBOL(del_timer_sync
);
1084 static int cascade(struct tvec_base
*base
, struct tvec
*tv
, int index
)
1086 /* cascade all the timers from tv up one level */
1087 struct timer_list
*timer
, *tmp
;
1088 struct list_head tv_list
;
1090 list_replace_init(tv
->vec
+ index
, &tv_list
);
1093 * We are removing _all_ timers from the list, so we
1094 * don't have to detach them individually.
1096 list_for_each_entry_safe(timer
, tmp
, &tv_list
, entry
) {
1097 BUG_ON(tbase_get_base(timer
->base
) != base
);
1098 /* No accounting, while moving them */
1099 __internal_add_timer(base
, timer
);
1105 static void call_timer_fn(struct timer_list
*timer
, void (*fn
)(unsigned long),
1108 int preempt_count
= preempt_count();
1110 #ifdef CONFIG_LOCKDEP
1112 * It is permissible to free the timer from inside the
1113 * function that is called from it, this we need to take into
1114 * account for lockdep too. To avoid bogus "held lock freed"
1115 * warnings as well as problems when looking into
1116 * timer->lockdep_map, make a copy and use that here.
1118 struct lockdep_map lockdep_map
;
1120 lockdep_copy_map(&lockdep_map
, &timer
->lockdep_map
);
1123 * Couple the lock chain with the lock chain at
1124 * del_timer_sync() by acquiring the lock_map around the fn()
1125 * call here and in del_timer_sync().
1127 lock_map_acquire(&lockdep_map
);
1129 trace_timer_expire_entry(timer
);
1131 trace_timer_expire_exit(timer
);
1133 lock_map_release(&lockdep_map
);
1135 if (preempt_count
!= preempt_count()) {
1136 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
1137 fn
, preempt_count
, preempt_count());
1139 * Restore the preempt count. That gives us a decent
1140 * chance to survive and extract information. If the
1141 * callback kept a lock held, bad luck, but not worse
1142 * than the BUG() we had.
1144 preempt_count() = preempt_count
;
1148 #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
1151 * __run_timers - run all expired timers (if any) on this CPU.
1152 * @base: the timer vector to be processed.
1154 * This function cascades all vectors and executes all expired timer
1157 static inline void __run_timers(struct tvec_base
*base
)
1159 struct timer_list
*timer
;
1161 spin_lock_irq(&base
->lock
);
1162 while (time_after_eq(jiffies
, base
->timer_jiffies
)) {
1163 struct list_head work_list
;
1164 struct list_head
*head
= &work_list
;
1165 int index
= base
->timer_jiffies
& TVR_MASK
;
1171 (!cascade(base
, &base
->tv2
, INDEX(0))) &&
1172 (!cascade(base
, &base
->tv3
, INDEX(1))) &&
1173 !cascade(base
, &base
->tv4
, INDEX(2)))
1174 cascade(base
, &base
->tv5
, INDEX(3));
1175 ++base
->timer_jiffies
;
1176 list_replace_init(base
->tv1
.vec
+ index
, &work_list
);
1177 while (!list_empty(head
)) {
1178 void (*fn
)(unsigned long);
1182 timer
= list_first_entry(head
, struct timer_list
,entry
);
1183 fn
= timer
->function
;
1185 irqsafe
= tbase_get_irqsafe(timer
->base
);
1187 timer_stats_account_timer(timer
);
1189 base
->running_timer
= timer
;
1190 detach_expired_timer(timer
, base
);
1193 spin_unlock(&base
->lock
);
1194 call_timer_fn(timer
, fn
, data
);
1195 spin_lock(&base
->lock
);
1197 spin_unlock_irq(&base
->lock
);
1198 call_timer_fn(timer
, fn
, data
);
1199 spin_lock_irq(&base
->lock
);
1203 base
->running_timer
= NULL
;
1204 spin_unlock_irq(&base
->lock
);
1207 #ifdef CONFIG_NO_HZ_COMMON
1209 * Find out when the next timer event is due to happen. This
1210 * is used on S/390 to stop all activity when a CPU is idle.
1211 * This function needs to be called with interrupts disabled.
1213 static unsigned long __next_timer_interrupt(struct tvec_base
*base
)
1215 unsigned long timer_jiffies
= base
->timer_jiffies
;
1216 unsigned long expires
= timer_jiffies
+ NEXT_TIMER_MAX_DELTA
;
1217 int index
, slot
, array
, found
= 0;
1218 struct timer_list
*nte
;
1219 struct tvec
*varray
[4];
1221 /* Look for timer events in tv1. */
1222 index
= slot
= timer_jiffies
& TVR_MASK
;
1224 list_for_each_entry(nte
, base
->tv1
.vec
+ slot
, entry
) {
1225 if (tbase_get_deferrable(nte
->base
))
1229 expires
= nte
->expires
;
1230 /* Look at the cascade bucket(s)? */
1231 if (!index
|| slot
< index
)
1235 slot
= (slot
+ 1) & TVR_MASK
;
1236 } while (slot
!= index
);
1239 /* Calculate the next cascade event */
1241 timer_jiffies
+= TVR_SIZE
- index
;
1242 timer_jiffies
>>= TVR_BITS
;
1244 /* Check tv2-tv5. */
1245 varray
[0] = &base
->tv2
;
1246 varray
[1] = &base
->tv3
;
1247 varray
[2] = &base
->tv4
;
1248 varray
[3] = &base
->tv5
;
1250 for (array
= 0; array
< 4; array
++) {
1251 struct tvec
*varp
= varray
[array
];
1253 index
= slot
= timer_jiffies
& TVN_MASK
;
1255 list_for_each_entry(nte
, varp
->vec
+ slot
, entry
) {
1256 if (tbase_get_deferrable(nte
->base
))
1260 if (time_before(nte
->expires
, expires
))
1261 expires
= nte
->expires
;
1264 * Do we still search for the first timer or are
1265 * we looking up the cascade buckets ?
1268 /* Look at the cascade bucket(s)? */
1269 if (!index
|| slot
< index
)
1273 slot
= (slot
+ 1) & TVN_MASK
;
1274 } while (slot
!= index
);
1277 timer_jiffies
+= TVN_SIZE
- index
;
1278 timer_jiffies
>>= TVN_BITS
;
1284 * Check, if the next hrtimer event is before the next timer wheel
1287 static unsigned long cmp_next_hrtimer_event(unsigned long now
,
1288 unsigned long expires
)
1290 ktime_t hr_delta
= hrtimer_get_next_event();
1291 struct timespec tsdelta
;
1292 unsigned long delta
;
1294 if (hr_delta
.tv64
== KTIME_MAX
)
1298 * Expired timer available, let it expire in the next tick
1300 if (hr_delta
.tv64
<= 0)
1303 tsdelta
= ktime_to_timespec(hr_delta
);
1304 delta
= timespec_to_jiffies(&tsdelta
);
1307 * Limit the delta to the max value, which is checked in
1308 * tick_nohz_stop_sched_tick():
1310 if (delta
> NEXT_TIMER_MAX_DELTA
)
1311 delta
= NEXT_TIMER_MAX_DELTA
;
1314 * Take rounding errors in to account and make sure, that it
1315 * expires in the next tick. Otherwise we go into an endless
1316 * ping pong due to tick_nohz_stop_sched_tick() retriggering
1322 if (time_before(now
, expires
))
1328 * get_next_timer_interrupt - return the jiffy of the next pending timer
1329 * @now: current time (in jiffies)
1331 unsigned long get_next_timer_interrupt(unsigned long now
)
1333 struct tvec_base
*base
= __this_cpu_read(tvec_bases
);
1334 unsigned long expires
= now
+ NEXT_TIMER_MAX_DELTA
;
1337 * Pretend that there is no timer pending if the cpu is offline.
1338 * Possible pending timers will be migrated later to an active cpu.
1340 if (cpu_is_offline(smp_processor_id()))
1343 spin_lock(&base
->lock
);
1344 if (base
->active_timers
) {
1345 if (time_before_eq(base
->next_timer
, base
->timer_jiffies
))
1346 base
->next_timer
= __next_timer_interrupt(base
);
1347 expires
= base
->next_timer
;
1349 spin_unlock(&base
->lock
);
1351 if (time_before_eq(expires
, now
))
1354 return cmp_next_hrtimer_event(now
, expires
);
1359 * Called from the timer interrupt handler to charge one tick to the current
1360 * process. user_tick is 1 if the tick is user time, 0 for system.
1362 void update_process_times(int user_tick
)
1364 struct task_struct
*p
= current
;
1365 int cpu
= smp_processor_id();
1367 /* Note: this timer irq context must be accounted for as well. */
1368 account_process_tick(p
, user_tick
);
1370 rcu_check_callbacks(cpu
, user_tick
);
1371 #ifdef CONFIG_IRQ_WORK
1376 run_posix_cpu_timers(p
);
1380 * This function runs timers and the timer-tq in bottom half context.
1382 static void run_timer_softirq(struct softirq_action
*h
)
1384 struct tvec_base
*base
= __this_cpu_read(tvec_bases
);
1386 hrtimer_run_pending();
1388 if (time_after_eq(jiffies
, base
->timer_jiffies
))
1393 * Called by the local, per-CPU timer interrupt on SMP.
1395 void run_local_timers(void)
1397 hrtimer_run_queues();
1398 raise_softirq(TIMER_SOFTIRQ
);
1401 #ifdef __ARCH_WANT_SYS_ALARM
1404 * For backwards compatibility? This can be done in libc so Alpha
1405 * and all newer ports shouldn't need it.
1407 SYSCALL_DEFINE1(alarm
, unsigned int, seconds
)
1409 return alarm_setitimer(seconds
);
1414 static void process_timeout(unsigned long __data
)
1416 wake_up_process((struct task_struct
*)__data
);
1420 * schedule_timeout - sleep until timeout
1421 * @timeout: timeout value in jiffies
1423 * Make the current task sleep until @timeout jiffies have
1424 * elapsed. The routine will return immediately unless
1425 * the current task state has been set (see set_current_state()).
1427 * You can set the task state as follows -
1429 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1430 * pass before the routine returns. The routine will return 0
1432 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1433 * delivered to the current task. In this case the remaining time
1434 * in jiffies will be returned, or 0 if the timer expired in time
1436 * The current task state is guaranteed to be TASK_RUNNING when this
1439 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1440 * the CPU away without a bound on the timeout. In this case the return
1441 * value will be %MAX_SCHEDULE_TIMEOUT.
1443 * In all cases the return value is guaranteed to be non-negative.
1445 signed long __sched
schedule_timeout(signed long timeout
)
1447 struct timer_list timer
;
1448 unsigned long expire
;
1452 case MAX_SCHEDULE_TIMEOUT
:
1454 * These two special cases are useful to be comfortable
1455 * in the caller. Nothing more. We could take
1456 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1457 * but I' d like to return a valid offset (>=0) to allow
1458 * the caller to do everything it want with the retval.
1464 * Another bit of PARANOID. Note that the retval will be
1465 * 0 since no piece of kernel is supposed to do a check
1466 * for a negative retval of schedule_timeout() (since it
1467 * should never happens anyway). You just have the printk()
1468 * that will tell you if something is gone wrong and where.
1471 printk(KERN_ERR
"schedule_timeout: wrong timeout "
1472 "value %lx\n", timeout
);
1474 current
->state
= TASK_RUNNING
;
1479 expire
= timeout
+ jiffies
;
1481 setup_timer_on_stack(&timer
, process_timeout
, (unsigned long)current
);
1482 __mod_timer(&timer
, expire
, false, TIMER_NOT_PINNED
);
1484 del_singleshot_timer_sync(&timer
);
1486 /* Remove the timer from the object tracker */
1487 destroy_timer_on_stack(&timer
);
1489 timeout
= expire
- jiffies
;
1492 return timeout
< 0 ? 0 : timeout
;
1494 EXPORT_SYMBOL(schedule_timeout
);
1497 * We can use __set_current_state() here because schedule_timeout() calls
1498 * schedule() unconditionally.
1500 signed long __sched
schedule_timeout_interruptible(signed long timeout
)
1502 __set_current_state(TASK_INTERRUPTIBLE
);
1503 return schedule_timeout(timeout
);
1505 EXPORT_SYMBOL(schedule_timeout_interruptible
);
1507 signed long __sched
schedule_timeout_killable(signed long timeout
)
1509 __set_current_state(TASK_KILLABLE
);
1510 return schedule_timeout(timeout
);
1512 EXPORT_SYMBOL(schedule_timeout_killable
);
1514 signed long __sched
schedule_timeout_uninterruptible(signed long timeout
)
1516 __set_current_state(TASK_UNINTERRUPTIBLE
);
1517 return schedule_timeout(timeout
);
1519 EXPORT_SYMBOL(schedule_timeout_uninterruptible
);
1521 static int __cpuinit
init_timers_cpu(int cpu
)
1524 struct tvec_base
*base
;
1525 static char __cpuinitdata tvec_base_done
[NR_CPUS
];
1527 if (!tvec_base_done
[cpu
]) {
1528 static char boot_done
;
1532 * The APs use this path later in boot
1534 base
= kmalloc_node(sizeof(*base
),
1535 GFP_KERNEL
| __GFP_ZERO
,
1540 /* Make sure that tvec_base is 2 byte aligned */
1541 if (tbase_get_deferrable(base
)) {
1546 per_cpu(tvec_bases
, cpu
) = base
;
1549 * This is for the boot CPU - we use compile-time
1550 * static initialisation because per-cpu memory isn't
1551 * ready yet and because the memory allocators are not
1552 * initialised either.
1555 base
= &boot_tvec_bases
;
1557 spin_lock_init(&base
->lock
);
1558 tvec_base_done
[cpu
] = 1;
1560 base
= per_cpu(tvec_bases
, cpu
);
1564 for (j
= 0; j
< TVN_SIZE
; j
++) {
1565 INIT_LIST_HEAD(base
->tv5
.vec
+ j
);
1566 INIT_LIST_HEAD(base
->tv4
.vec
+ j
);
1567 INIT_LIST_HEAD(base
->tv3
.vec
+ j
);
1568 INIT_LIST_HEAD(base
->tv2
.vec
+ j
);
1570 for (j
= 0; j
< TVR_SIZE
; j
++)
1571 INIT_LIST_HEAD(base
->tv1
.vec
+ j
);
1573 base
->timer_jiffies
= jiffies
;
1574 base
->next_timer
= base
->timer_jiffies
;
1575 base
->active_timers
= 0;
1579 #ifdef CONFIG_HOTPLUG_CPU
1580 static void migrate_timer_list(struct tvec_base
*new_base
, struct list_head
*head
)
1582 struct timer_list
*timer
;
1584 while (!list_empty(head
)) {
1585 timer
= list_first_entry(head
, struct timer_list
, entry
);
1586 /* We ignore the accounting on the dying cpu */
1587 detach_timer(timer
, false);
1588 timer_set_base(timer
, new_base
);
1589 internal_add_timer(new_base
, timer
);
1593 static void __cpuinit
migrate_timers(int cpu
)
1595 struct tvec_base
*old_base
;
1596 struct tvec_base
*new_base
;
1599 BUG_ON(cpu_online(cpu
));
1600 old_base
= per_cpu(tvec_bases
, cpu
);
1601 new_base
= get_cpu_var(tvec_bases
);
1603 * The caller is globally serialized and nobody else
1604 * takes two locks at once, deadlock is not possible.
1606 spin_lock_irq(&new_base
->lock
);
1607 spin_lock_nested(&old_base
->lock
, SINGLE_DEPTH_NESTING
);
1609 BUG_ON(old_base
->running_timer
);
1611 for (i
= 0; i
< TVR_SIZE
; i
++)
1612 migrate_timer_list(new_base
, old_base
->tv1
.vec
+ i
);
1613 for (i
= 0; i
< TVN_SIZE
; i
++) {
1614 migrate_timer_list(new_base
, old_base
->tv2
.vec
+ i
);
1615 migrate_timer_list(new_base
, old_base
->tv3
.vec
+ i
);
1616 migrate_timer_list(new_base
, old_base
->tv4
.vec
+ i
);
1617 migrate_timer_list(new_base
, old_base
->tv5
.vec
+ i
);
1620 spin_unlock(&old_base
->lock
);
1621 spin_unlock_irq(&new_base
->lock
);
1622 put_cpu_var(tvec_bases
);
1624 #endif /* CONFIG_HOTPLUG_CPU */
1626 static int __cpuinit
timer_cpu_notify(struct notifier_block
*self
,
1627 unsigned long action
, void *hcpu
)
1629 long cpu
= (long)hcpu
;
1633 case CPU_UP_PREPARE
:
1634 case CPU_UP_PREPARE_FROZEN
:
1635 err
= init_timers_cpu(cpu
);
1637 return notifier_from_errno(err
);
1639 #ifdef CONFIG_HOTPLUG_CPU
1641 case CPU_DEAD_FROZEN
:
1642 migrate_timers(cpu
);
1651 static struct notifier_block __cpuinitdata timers_nb
= {
1652 .notifier_call
= timer_cpu_notify
,
1656 void __init
init_timers(void)
1660 /* ensure there are enough low bits for flags in timer->base pointer */
1661 BUILD_BUG_ON(__alignof__(struct tvec_base
) & TIMER_FLAG_MASK
);
1663 err
= timer_cpu_notify(&timers_nb
, (unsigned long)CPU_UP_PREPARE
,
1664 (void *)(long)smp_processor_id());
1667 BUG_ON(err
!= NOTIFY_OK
);
1668 register_cpu_notifier(&timers_nb
);
1669 open_softirq(TIMER_SOFTIRQ
, run_timer_softirq
);
1673 * msleep - sleep safely even with waitqueue interruptions
1674 * @msecs: Time in milliseconds to sleep for
1676 void msleep(unsigned int msecs
)
1678 unsigned long timeout
= msecs_to_jiffies(msecs
) + 1;
1681 timeout
= schedule_timeout_uninterruptible(timeout
);
1684 EXPORT_SYMBOL(msleep
);
1687 * msleep_interruptible - sleep waiting for signals
1688 * @msecs: Time in milliseconds to sleep for
1690 unsigned long msleep_interruptible(unsigned int msecs
)
1692 unsigned long timeout
= msecs_to_jiffies(msecs
) + 1;
1694 while (timeout
&& !signal_pending(current
))
1695 timeout
= schedule_timeout_interruptible(timeout
);
1696 return jiffies_to_msecs(timeout
);
1699 EXPORT_SYMBOL(msleep_interruptible
);
1701 static int __sched
do_usleep_range(unsigned long min
, unsigned long max
)
1704 unsigned long delta
;
1706 kmin
= ktime_set(0, min
* NSEC_PER_USEC
);
1707 delta
= (max
- min
) * NSEC_PER_USEC
;
1708 return schedule_hrtimeout_range(&kmin
, delta
, HRTIMER_MODE_REL
);
1712 * usleep_range - Drop in replacement for udelay where wakeup is flexible
1713 * @min: Minimum time in usecs to sleep
1714 * @max: Maximum time in usecs to sleep
1716 void usleep_range(unsigned long min
, unsigned long max
)
1718 __set_current_state(TASK_UNINTERRUPTIBLE
);
1719 do_usleep_range(min
, max
);
1721 EXPORT_SYMBOL(usleep_range
);