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 #include <linux/mt_sched_mon.h>
54 #define CREATE_TRACE_POINTS
55 #include <trace/events/timer.h>
57 u64 jiffies_64 __cacheline_aligned_in_smp
= INITIAL_JIFFIES
;
59 EXPORT_SYMBOL(jiffies_64
);
62 * per-CPU timer vector definitions:
64 #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
65 #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
66 #define TVN_SIZE (1 << TVN_BITS)
67 #define TVR_SIZE (1 << TVR_BITS)
68 #define TVN_MASK (TVN_SIZE - 1)
69 #define TVR_MASK (TVR_SIZE - 1)
70 #define MAX_TVAL ((unsigned long)((1ULL << (TVR_BITS + 4*TVN_BITS)) - 1))
73 struct list_head vec
[TVN_SIZE
];
77 struct list_head vec
[TVR_SIZE
];
82 struct timer_list
*running_timer
;
83 unsigned long timer_jiffies
;
84 unsigned long next_timer
;
85 unsigned long active_timers
;
91 } ____cacheline_aligned
;
93 struct tvec_base boot_tvec_bases
;
94 EXPORT_SYMBOL(boot_tvec_bases
);
95 static DEFINE_PER_CPU(struct tvec_base
*, tvec_bases
) = &boot_tvec_bases
;
97 /* Functions below help us manage 'deferrable' flag */
98 static inline unsigned int tbase_get_deferrable(struct tvec_base
*base
)
100 return ((unsigned int)(unsigned long)base
& TIMER_DEFERRABLE
);
103 static inline unsigned int tbase_get_irqsafe(struct tvec_base
*base
)
105 return ((unsigned int)(unsigned long)base
& TIMER_IRQSAFE
);
108 static inline struct tvec_base
*tbase_get_base(struct tvec_base
*base
)
110 return ((struct tvec_base
*)((unsigned long)base
& ~TIMER_FLAG_MASK
));
114 timer_set_base(struct timer_list
*timer
, struct tvec_base
*new_base
)
116 unsigned long flags
= (unsigned long)timer
->base
& TIMER_FLAG_MASK
;
118 timer
->base
= (struct tvec_base
*)((unsigned long)(new_base
) | flags
);
121 static unsigned long round_jiffies_common(unsigned long j
, int cpu
,
125 unsigned long original
= j
;
128 * We don't want all cpus firing their timers at once hitting the
129 * same lock or cachelines, so we skew each extra cpu with an extra
130 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
132 * The skew is done by adding 3*cpunr, then round, then subtract this
133 * extra offset again.
140 * If the target jiffie is just after a whole second (which can happen
141 * due to delays of the timer irq, long irq off times etc etc) then
142 * we should round down to the whole second, not up. Use 1/4th second
143 * as cutoff for this rounding as an extreme upper bound for this.
144 * But never round down if @force_up is set.
146 if (rem
< HZ
/4 && !force_up
) /* round down */
151 /* now that we have rounded, subtract the extra skew again */
155 * Make sure j is still in the future. Otherwise return the
158 return time_is_after_jiffies(j
) ? j
: original
;
162 * __round_jiffies - function to round jiffies to a full second
163 * @j: the time in (absolute) jiffies that should be rounded
164 * @cpu: the processor number on which the timeout will happen
166 * __round_jiffies() rounds an absolute time in the future (in jiffies)
167 * up or down to (approximately) full seconds. This is useful for timers
168 * for which the exact time they fire does not matter too much, as long as
169 * they fire approximately every X seconds.
171 * By rounding these timers to whole seconds, all such timers will fire
172 * at the same time, rather than at various times spread out. The goal
173 * of this is to have the CPU wake up less, which saves power.
175 * The exact rounding is skewed for each processor to avoid all
176 * processors firing at the exact same time, which could lead
177 * to lock contention or spurious cache line bouncing.
179 * The return value is the rounded version of the @j parameter.
181 unsigned long __round_jiffies(unsigned long j
, int cpu
)
183 return round_jiffies_common(j
, cpu
, false);
185 EXPORT_SYMBOL_GPL(__round_jiffies
);
188 * __round_jiffies_relative - function to round jiffies to a full second
189 * @j: the time in (relative) jiffies that should be rounded
190 * @cpu: the processor number on which the timeout will happen
192 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
193 * up or down to (approximately) full seconds. This is useful for timers
194 * for which the exact time they fire does not matter too much, as long as
195 * they fire approximately every X seconds.
197 * By rounding these timers to whole seconds, all such timers will fire
198 * at the same time, rather than at various times spread out. The goal
199 * of this is to have the CPU wake up less, which saves power.
201 * The exact rounding is skewed for each processor to avoid all
202 * processors firing at the exact same time, which could lead
203 * to lock contention or spurious cache line bouncing.
205 * The return value is the rounded version of the @j parameter.
207 unsigned long __round_jiffies_relative(unsigned long j
, int cpu
)
209 unsigned long j0
= jiffies
;
211 /* Use j0 because jiffies might change while we run */
212 return round_jiffies_common(j
+ j0
, cpu
, false) - j0
;
214 EXPORT_SYMBOL_GPL(__round_jiffies_relative
);
217 * round_jiffies - function to round jiffies to a full second
218 * @j: the time in (absolute) jiffies that should be rounded
220 * round_jiffies() rounds an absolute time in the future (in jiffies)
221 * up or down to (approximately) full seconds. This is useful for timers
222 * for which the exact time they fire does not matter too much, as long as
223 * they fire approximately every X seconds.
225 * By rounding these timers to whole seconds, all such timers will fire
226 * at the same time, rather than at various times spread out. The goal
227 * of this is to have the CPU wake up less, which saves power.
229 * The return value is the rounded version of the @j parameter.
231 unsigned long round_jiffies(unsigned long j
)
233 return round_jiffies_common(j
, raw_smp_processor_id(), false);
235 EXPORT_SYMBOL_GPL(round_jiffies
);
238 * round_jiffies_relative - function to round jiffies to a full second
239 * @j: the time in (relative) jiffies that should be rounded
241 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
242 * up or down to (approximately) full seconds. This is useful for timers
243 * for which the exact time they fire does not matter too much, as long as
244 * they fire approximately every X seconds.
246 * By rounding these timers to whole seconds, all such timers will fire
247 * at the same time, rather than at various times spread out. The goal
248 * of this is to have the CPU wake up less, which saves power.
250 * The return value is the rounded version of the @j parameter.
252 unsigned long round_jiffies_relative(unsigned long j
)
254 return __round_jiffies_relative(j
, raw_smp_processor_id());
256 EXPORT_SYMBOL_GPL(round_jiffies_relative
);
259 * __round_jiffies_up - function to round jiffies up to a full second
260 * @j: the time in (absolute) jiffies that should be rounded
261 * @cpu: the processor number on which the timeout will happen
263 * This is the same as __round_jiffies() except that it will never
264 * round down. This is useful for timeouts for which the exact time
265 * of firing does not matter too much, as long as they don't fire too
268 unsigned long __round_jiffies_up(unsigned long j
, int cpu
)
270 return round_jiffies_common(j
, cpu
, true);
272 EXPORT_SYMBOL_GPL(__round_jiffies_up
);
275 * __round_jiffies_up_relative - function to round jiffies up to a full second
276 * @j: the time in (relative) jiffies that should be rounded
277 * @cpu: the processor number on which the timeout will happen
279 * This is the same as __round_jiffies_relative() except that it will never
280 * round down. This is useful for timeouts for which the exact time
281 * of firing does not matter too much, as long as they don't fire too
284 unsigned long __round_jiffies_up_relative(unsigned long j
, int cpu
)
286 unsigned long j0
= jiffies
;
288 /* Use j0 because jiffies might change while we run */
289 return round_jiffies_common(j
+ j0
, cpu
, true) - j0
;
291 EXPORT_SYMBOL_GPL(__round_jiffies_up_relative
);
294 * round_jiffies_up - function to round jiffies up to a full second
295 * @j: the time in (absolute) jiffies that should be rounded
297 * This is the same as round_jiffies() except that it will never
298 * round down. This is useful for timeouts for which the exact time
299 * of firing does not matter too much, as long as they don't fire too
302 unsigned long round_jiffies_up(unsigned long j
)
304 return round_jiffies_common(j
, raw_smp_processor_id(), true);
306 EXPORT_SYMBOL_GPL(round_jiffies_up
);
309 * round_jiffies_up_relative - function to round jiffies up to a full second
310 * @j: the time in (relative) jiffies that should be rounded
312 * This is the same as round_jiffies_relative() except that it will never
313 * round down. This is useful for timeouts for which the exact time
314 * of firing does not matter too much, as long as they don't fire too
317 unsigned long round_jiffies_up_relative(unsigned long j
)
319 return __round_jiffies_up_relative(j
, raw_smp_processor_id());
321 EXPORT_SYMBOL_GPL(round_jiffies_up_relative
);
324 * set_timer_slack - set the allowed slack for a timer
325 * @timer: the timer to be modified
326 * @slack_hz: the amount of time (in jiffies) allowed for rounding
328 * Set the amount of time, in jiffies, that a certain timer has
329 * in terms of slack. By setting this value, the timer subsystem
330 * will schedule the actual timer somewhere between
331 * the time mod_timer() asks for, and that time plus the slack.
333 * By setting the slack to -1, a percentage of the delay is used
336 void set_timer_slack(struct timer_list
*timer
, int slack_hz
)
338 timer
->slack
= slack_hz
;
340 EXPORT_SYMBOL_GPL(set_timer_slack
);
343 __internal_add_timer(struct tvec_base
*base
, struct timer_list
*timer
)
345 unsigned long expires
= timer
->expires
;
346 unsigned long idx
= expires
- base
->timer_jiffies
;
347 struct list_head
*vec
;
349 if (idx
< TVR_SIZE
) {
350 int i
= expires
& TVR_MASK
;
351 vec
= base
->tv1
.vec
+ i
;
352 } else if (idx
< 1 << (TVR_BITS
+ TVN_BITS
)) {
353 int i
= (expires
>> TVR_BITS
) & TVN_MASK
;
354 vec
= base
->tv2
.vec
+ i
;
355 } else if (idx
< 1 << (TVR_BITS
+ 2 * TVN_BITS
)) {
356 int i
= (expires
>> (TVR_BITS
+ TVN_BITS
)) & TVN_MASK
;
357 vec
= base
->tv3
.vec
+ i
;
358 } else if (idx
< 1 << (TVR_BITS
+ 3 * TVN_BITS
)) {
359 int i
= (expires
>> (TVR_BITS
+ 2 * TVN_BITS
)) & TVN_MASK
;
360 vec
= base
->tv4
.vec
+ i
;
361 } else if ((signed long) idx
< 0) {
363 * Can happen if you add a timer with expires == jiffies,
364 * or you set a timer to go off in the past
366 vec
= base
->tv1
.vec
+ (base
->timer_jiffies
& TVR_MASK
);
369 /* If the timeout is larger than MAX_TVAL (on 64-bit
370 * architectures or with CONFIG_BASE_SMALL=1) then we
371 * use the maximum timeout.
373 if (idx
> MAX_TVAL
) {
375 expires
= idx
+ base
->timer_jiffies
;
377 i
= (expires
>> (TVR_BITS
+ 3 * TVN_BITS
)) & TVN_MASK
;
378 vec
= base
->tv5
.vec
+ i
;
383 list_add_tail(&timer
->entry
, vec
);
386 static void internal_add_timer(struct tvec_base
*base
, struct timer_list
*timer
)
388 __internal_add_timer(base
, timer
);
390 * Update base->active_timers and base->next_timer
392 if (!tbase_get_deferrable(timer
->base
)) {
393 if (time_before(timer
->expires
, base
->next_timer
))
394 base
->next_timer
= timer
->expires
;
395 base
->active_timers
++;
399 #ifdef CONFIG_TIMER_STATS
400 void __timer_stats_timer_set_start_info(struct timer_list
*timer
, void *addr
)
402 if (timer
->start_site
)
405 timer
->start_site
= addr
;
406 memcpy(timer
->start_comm
, current
->comm
, TASK_COMM_LEN
);
407 timer
->start_pid
= current
->pid
;
410 static void timer_stats_account_timer(struct timer_list
*timer
)
412 unsigned int flag
= 0;
414 if (likely(!timer
->start_site
))
416 if (unlikely(tbase_get_deferrable(timer
->base
)))
417 flag
|= TIMER_STATS_FLAG_DEFERRABLE
;
419 timer_stats_update_stats(timer
, timer
->start_pid
, timer
->start_site
,
420 timer
->function
, timer
->start_comm
, flag
);
424 static void timer_stats_account_timer(struct timer_list
*timer
) {}
427 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
429 static struct debug_obj_descr timer_debug_descr
;
431 static void *timer_debug_hint(void *addr
)
433 return ((struct timer_list
*) addr
)->function
;
437 * fixup_init is called when:
438 * - an active object is initialized
440 static int timer_fixup_init(void *addr
, enum debug_obj_state state
)
442 struct timer_list
*timer
= addr
;
445 case ODEBUG_STATE_ACTIVE
:
446 del_timer_sync(timer
);
447 debug_object_init(timer
, &timer_debug_descr
);
454 /* Stub timer callback for improperly used timers. */
455 static void stub_timer(unsigned long data
)
461 * fixup_activate is called when:
462 * - an active object is activated
463 * - an unknown object is activated (might be a statically initialized object)
465 static int timer_fixup_activate(void *addr
, enum debug_obj_state state
)
467 struct timer_list
*timer
= addr
;
471 case ODEBUG_STATE_NOTAVAILABLE
:
473 * This is not really a fixup. The timer was
474 * statically initialized. We just make sure that it
475 * is tracked in the object tracker.
477 if (timer
->entry
.next
== NULL
&&
478 timer
->entry
.prev
== TIMER_ENTRY_STATIC
) {
479 debug_object_init(timer
, &timer_debug_descr
);
480 debug_object_activate(timer
, &timer_debug_descr
);
483 setup_timer(timer
, stub_timer
, 0);
488 case ODEBUG_STATE_ACTIVE
:
497 * fixup_free is called when:
498 * - an active object is freed
500 static int timer_fixup_free(void *addr
, enum debug_obj_state state
)
502 struct timer_list
*timer
= addr
;
505 case ODEBUG_STATE_ACTIVE
:
506 del_timer_sync(timer
);
507 debug_object_free(timer
, &timer_debug_descr
);
515 * fixup_assert_init is called when:
516 * - an untracked/uninit-ed object is found
518 static int timer_fixup_assert_init(void *addr
, enum debug_obj_state state
)
520 struct timer_list
*timer
= addr
;
523 case ODEBUG_STATE_NOTAVAILABLE
:
524 if (timer
->entry
.prev
== TIMER_ENTRY_STATIC
) {
526 * This is not really a fixup. The timer was
527 * statically initialized. We just make sure that it
528 * is tracked in the object tracker.
530 debug_object_init(timer
, &timer_debug_descr
);
533 setup_timer(timer
, stub_timer
, 0);
541 static struct debug_obj_descr timer_debug_descr
= {
542 .name
= "timer_list",
543 .debug_hint
= timer_debug_hint
,
544 .fixup_init
= timer_fixup_init
,
545 .fixup_activate
= timer_fixup_activate
,
546 .fixup_free
= timer_fixup_free
,
547 .fixup_assert_init
= timer_fixup_assert_init
,
550 static inline void debug_timer_init(struct timer_list
*timer
)
552 debug_object_init(timer
, &timer_debug_descr
);
555 static inline void debug_timer_activate(struct timer_list
*timer
)
557 debug_object_activate(timer
, &timer_debug_descr
);
560 static inline void debug_timer_deactivate(struct timer_list
*timer
)
562 debug_object_deactivate(timer
, &timer_debug_descr
);
565 static inline void debug_timer_free(struct timer_list
*timer
)
567 debug_object_free(timer
, &timer_debug_descr
);
570 static inline void debug_timer_assert_init(struct timer_list
*timer
)
572 debug_object_assert_init(timer
, &timer_debug_descr
);
575 static void do_init_timer(struct timer_list
*timer
, unsigned int flags
,
576 const char *name
, struct lock_class_key
*key
);
578 void init_timer_on_stack_key(struct timer_list
*timer
, unsigned int flags
,
579 const char *name
, struct lock_class_key
*key
)
581 debug_object_init_on_stack(timer
, &timer_debug_descr
);
582 do_init_timer(timer
, flags
, name
, key
);
584 EXPORT_SYMBOL_GPL(init_timer_on_stack_key
);
586 void destroy_timer_on_stack(struct timer_list
*timer
)
588 debug_object_free(timer
, &timer_debug_descr
);
590 EXPORT_SYMBOL_GPL(destroy_timer_on_stack
);
593 static inline void debug_timer_init(struct timer_list
*timer
) { }
594 static inline void debug_timer_activate(struct timer_list
*timer
) { }
595 static inline void debug_timer_deactivate(struct timer_list
*timer
) { }
596 static inline void debug_timer_assert_init(struct timer_list
*timer
) { }
599 static inline void debug_init(struct timer_list
*timer
)
601 debug_timer_init(timer
);
602 trace_timer_init(timer
);
606 debug_activate(struct timer_list
*timer
, unsigned long expires
)
608 debug_timer_activate(timer
);
609 trace_timer_start(timer
, expires
);
612 static inline void debug_deactivate(struct timer_list
*timer
)
614 debug_timer_deactivate(timer
);
615 trace_timer_cancel(timer
);
618 static inline void debug_assert_init(struct timer_list
*timer
)
620 debug_timer_assert_init(timer
);
623 static void do_init_timer(struct timer_list
*timer
, unsigned int flags
,
624 const char *name
, struct lock_class_key
*key
)
626 struct tvec_base
*base
= __raw_get_cpu_var(tvec_bases
);
628 timer
->entry
.next
= NULL
;
629 timer
->base
= (void *)((unsigned long)base
| flags
);
631 #ifdef CONFIG_TIMER_STATS
632 timer
->start_site
= NULL
;
633 timer
->start_pid
= -1;
634 memset(timer
->start_comm
, 0, TASK_COMM_LEN
);
636 lockdep_init_map(&timer
->lockdep_map
, name
, key
, 0);
640 * init_timer_key - initialize a timer
641 * @timer: the timer to be initialized
642 * @flags: timer flags
643 * @name: name of the timer
644 * @key: lockdep class key of the fake lock used for tracking timer
645 * sync lock dependencies
647 * init_timer_key() must be done to a timer prior calling *any* of the
648 * other timer functions.
650 void init_timer_key(struct timer_list
*timer
, unsigned int flags
,
651 const char *name
, struct lock_class_key
*key
)
654 do_init_timer(timer
, flags
, name
, key
);
656 EXPORT_SYMBOL(init_timer_key
);
658 static inline void detach_timer(struct timer_list
*timer
, bool clear_pending
)
660 struct list_head
*entry
= &timer
->entry
;
662 debug_deactivate(timer
);
664 __list_del(entry
->prev
, entry
->next
);
667 entry
->prev
= LIST_POISON2
;
671 detach_expired_timer(struct timer_list
*timer
, struct tvec_base
*base
)
673 detach_timer(timer
, true);
674 if (!tbase_get_deferrable(timer
->base
))
675 base
->active_timers
--;
678 static int detach_if_pending(struct timer_list
*timer
, struct tvec_base
*base
,
681 if (!timer_pending(timer
))
684 detach_timer(timer
, clear_pending
);
685 if (!tbase_get_deferrable(timer
->base
)) {
686 base
->active_timers
--;
687 if (timer
->expires
== base
->next_timer
)
688 base
->next_timer
= base
->timer_jiffies
;
694 * We are using hashed locking: holding per_cpu(tvec_bases).lock
695 * means that all timers which are tied to this base via timer->base are
696 * locked, and the base itself is locked too.
698 * So __run_timers/migrate_timers can safely modify all timers which could
699 * be found on ->tvX lists.
701 * When the timer's base is locked, and the timer removed from list, it is
702 * possible to set timer->base = NULL and drop the lock: the timer remains
705 static struct tvec_base
*lock_timer_base(struct timer_list
*timer
,
706 unsigned long *flags
)
707 __acquires(timer
->base
->lock
)
709 struct tvec_base
*base
;
712 struct tvec_base
*prelock_base
= timer
->base
;
713 base
= tbase_get_base(prelock_base
);
714 if (likely(base
!= NULL
)) {
715 spin_lock_irqsave(&base
->lock
, *flags
);
716 if (likely(prelock_base
== timer
->base
))
718 /* The timer has migrated to another CPU */
719 spin_unlock_irqrestore(&base
->lock
, *flags
);
726 __mod_timer(struct timer_list
*timer
, unsigned long expires
,
727 bool pending_only
, int pinned
)
729 struct tvec_base
*base
, *new_base
;
733 timer_stats_timer_set_start_info(timer
);
734 BUG_ON(!timer
->function
);
736 base
= lock_timer_base(timer
, &flags
);
738 ret
= detach_if_pending(timer
, base
, false);
739 if (!ret
&& pending_only
)
742 debug_activate(timer
, expires
);
744 cpu
= smp_processor_id();
746 #if defined(CONFIG_NO_HZ_COMMON) && defined(CONFIG_SMP)
747 if (!pinned
&& get_sysctl_timer_migration() && idle_cpu(cpu
))
748 cpu
= get_nohz_timer_target();
750 new_base
= per_cpu(tvec_bases
, cpu
);
752 if (base
!= new_base
) {
754 * We are trying to schedule the timer on the local CPU.
755 * However we can't change timer's base while it is running,
756 * otherwise del_timer_sync() can't detect that the timer's
757 * handler yet has not finished. This also guarantees that
758 * the timer is serialized wrt itself.
760 if (likely(base
->running_timer
!= timer
)) {
761 /* See the comment in lock_timer_base() */
762 timer_set_base(timer
, NULL
);
763 spin_unlock(&base
->lock
);
765 spin_lock(&base
->lock
);
766 timer_set_base(timer
, base
);
770 timer
->expires
= expires
;
771 internal_add_timer(base
, timer
);
774 spin_unlock_irqrestore(&base
->lock
, flags
);
780 * mod_timer_pending - modify a pending timer's timeout
781 * @timer: the pending timer to be modified
782 * @expires: new timeout in jiffies
784 * mod_timer_pending() is the same for pending timers as mod_timer(),
785 * but will not re-activate and modify already deleted timers.
787 * It is useful for unserialized use of timers.
789 int mod_timer_pending(struct timer_list
*timer
, unsigned long expires
)
791 return __mod_timer(timer
, expires
, true, TIMER_NOT_PINNED
);
793 EXPORT_SYMBOL(mod_timer_pending
);
796 * Decide where to put the timer while taking the slack into account
799 * 1) calculate the maximum (absolute) time
800 * 2) calculate the highest bit where the expires and new max are different
801 * 3) use this bit to make a mask
802 * 4) use the bitmask to round down the maximum time, so that all last
806 unsigned long apply_slack(struct timer_list
*timer
, unsigned long expires
)
808 unsigned long expires_limit
, mask
;
811 if (timer
->slack
>= 0) {
812 expires_limit
= expires
+ timer
->slack
;
814 long delta
= expires
- jiffies
;
819 expires_limit
= expires
+ delta
/ 256;
821 mask
= expires
^ expires_limit
;
825 bit
= find_last_bit(&mask
, BITS_PER_LONG
);
827 mask
= (1UL << bit
) - 1;
829 expires_limit
= expires_limit
& ~(mask
);
831 return expires_limit
;
835 * mod_timer - modify a timer's timeout
836 * @timer: the timer to be modified
837 * @expires: new timeout in jiffies
839 * mod_timer() is a more efficient way to update the expire field of an
840 * active timer (if the timer is inactive it will be activated)
842 * mod_timer(timer, expires) is equivalent to:
844 * del_timer(timer); timer->expires = expires; add_timer(timer);
846 * Note that if there are multiple unserialized concurrent users of the
847 * same timer, then mod_timer() is the only safe way to modify the timeout,
848 * since add_timer() cannot modify an already running timer.
850 * The function returns whether it has modified a pending timer or not.
851 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
852 * active timer returns 1.)
854 int mod_timer(struct timer_list
*timer
, unsigned long expires
)
856 expires
= apply_slack(timer
, expires
);
859 * This is a common optimization triggered by the
860 * networking code - if the timer is re-modified
861 * to be the same thing then just return:
863 if (timer_pending(timer
) && timer
->expires
== expires
)
866 return __mod_timer(timer
, expires
, false, TIMER_NOT_PINNED
);
868 EXPORT_SYMBOL(mod_timer
);
871 * mod_timer_pinned - modify a timer's timeout
872 * @timer: the timer to be modified
873 * @expires: new timeout in jiffies
875 * mod_timer_pinned() is a way to update the expire field of an
876 * active timer (if the timer is inactive it will be activated)
877 * and to ensure that the timer is scheduled on the current CPU.
879 * Note that this does not prevent the timer from being migrated
880 * when the current CPU goes offline. If this is a problem for
881 * you, use CPU-hotplug notifiers to handle it correctly, for
882 * example, cancelling the timer when the corresponding CPU goes
885 * mod_timer_pinned(timer, expires) is equivalent to:
887 * del_timer(timer); timer->expires = expires; add_timer(timer);
889 int mod_timer_pinned(struct timer_list
*timer
, unsigned long expires
)
891 if (timer
->expires
== expires
&& timer_pending(timer
))
894 return __mod_timer(timer
, expires
, false, TIMER_PINNED
);
896 EXPORT_SYMBOL(mod_timer_pinned
);
899 * add_timer - start a timer
900 * @timer: the timer to be added
902 * The kernel will do a ->function(->data) callback from the
903 * timer interrupt at the ->expires point in the future. The
904 * current time is 'jiffies'.
906 * The timer's ->expires, ->function (and if the handler uses it, ->data)
907 * fields must be set prior calling this function.
909 * Timers with an ->expires field in the past will be executed in the next
912 void add_timer(struct timer_list
*timer
)
914 BUG_ON(timer_pending(timer
));
915 mod_timer(timer
, timer
->expires
);
917 EXPORT_SYMBOL(add_timer
);
920 * add_timer_on - start a timer on a particular CPU
921 * @timer: the timer to be added
922 * @cpu: the CPU to start it on
924 * This is not very scalable on SMP. Double adds are not possible.
926 void add_timer_on(struct timer_list
*timer
, int cpu
)
928 struct tvec_base
*new_base
= per_cpu(tvec_bases
, cpu
);
929 struct tvec_base
*base
;
932 timer_stats_timer_set_start_info(timer
);
933 BUG_ON(timer_pending(timer
) || !timer
->function
);
936 * If @timer was on a different CPU, it should be migrated with the
937 * old base locked to prevent other operations proceeding with the
938 * wrong base locked. See lock_timer_base().
940 base
= lock_timer_base(timer
, &flags
);
941 if (base
!= new_base
) {
942 timer_set_base(timer
, NULL
);
943 spin_unlock(&base
->lock
);
945 spin_lock(&base
->lock
);
946 timer_set_base(timer
, base
);
948 debug_activate(timer
, timer
->expires
);
949 internal_add_timer(base
, timer
);
951 * Check whether the other CPU is in dynticks mode and needs
952 * to be triggered to reevaluate the timer wheel.
953 * We are protected against the other CPU fiddling
954 * with the timer by holding the timer base lock. This also
955 * makes sure that a CPU on the way to stop its tick can not
956 * evaluate the timer wheel.
958 wake_up_nohz_cpu(cpu
);
959 spin_unlock_irqrestore(&base
->lock
, flags
);
961 EXPORT_SYMBOL_GPL(add_timer_on
);
964 * del_timer - deactive a timer.
965 * @timer: the timer to be deactivated
967 * del_timer() deactivates a timer - this works on both active and inactive
970 * The function returns whether it has deactivated a pending timer or not.
971 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
972 * active timer returns 1.)
974 int del_timer(struct timer_list
*timer
)
976 struct tvec_base
*base
;
980 debug_assert_init(timer
);
982 timer_stats_timer_clear_start_info(timer
);
983 if (timer_pending(timer
)) {
984 base
= lock_timer_base(timer
, &flags
);
985 ret
= detach_if_pending(timer
, base
, true);
986 spin_unlock_irqrestore(&base
->lock
, flags
);
991 EXPORT_SYMBOL(del_timer
);
994 * try_to_del_timer_sync - Try to deactivate a timer
995 * @timer: timer do del
997 * This function tries to deactivate a timer. Upon successful (ret >= 0)
998 * exit the timer is not queued and the handler is not running on any CPU.
1000 int try_to_del_timer_sync(struct timer_list
*timer
)
1002 struct tvec_base
*base
;
1003 unsigned long flags
;
1006 debug_assert_init(timer
);
1008 base
= lock_timer_base(timer
, &flags
);
1010 if (base
->running_timer
!= timer
) {
1011 timer_stats_timer_clear_start_info(timer
);
1012 ret
= detach_if_pending(timer
, base
, true);
1014 spin_unlock_irqrestore(&base
->lock
, flags
);
1018 EXPORT_SYMBOL(try_to_del_timer_sync
);
1022 * del_timer_sync - deactivate a timer and wait for the handler to finish.
1023 * @timer: the timer to be deactivated
1025 * This function only differs from del_timer() on SMP: besides deactivating
1026 * the timer it also makes sure the handler has finished executing on other
1029 * Synchronization rules: Callers must prevent restarting of the timer,
1030 * otherwise this function is meaningless. It must not be called from
1031 * interrupt contexts unless the timer is an irqsafe one. The caller must
1032 * not hold locks which would prevent completion of the timer's
1033 * handler. The timer's handler must not call add_timer_on(). Upon exit the
1034 * timer is not queued and the handler is not running on any CPU.
1036 * Note: For !irqsafe timers, you must not hold locks that are held in
1037 * interrupt context while calling this function. Even if the lock has
1038 * nothing to do with the timer in question. Here's why:
1044 * base->running_timer = mytimer;
1045 * spin_lock_irq(somelock);
1047 * spin_lock(somelock);
1048 * del_timer_sync(mytimer);
1049 * while (base->running_timer == mytimer);
1051 * Now del_timer_sync() will never return and never release somelock.
1052 * The interrupt on the other CPU is waiting to grab somelock but
1053 * it has interrupted the softirq that CPU0 is waiting to finish.
1055 * The function returns whether it has deactivated a pending timer or not.
1057 int del_timer_sync(struct timer_list
*timer
)
1059 #ifdef CONFIG_LOCKDEP
1060 unsigned long flags
;
1063 * If lockdep gives a backtrace here, please reference
1064 * the synchronization rules above.
1066 local_irq_save(flags
);
1067 lock_map_acquire(&timer
->lockdep_map
);
1068 lock_map_release(&timer
->lockdep_map
);
1069 local_irq_restore(flags
);
1072 * don't use it in hardirq context, because it
1073 * could lead to deadlock.
1075 WARN_ON(in_irq() && !tbase_get_irqsafe(timer
->base
));
1077 int ret
= try_to_del_timer_sync(timer
);
1083 EXPORT_SYMBOL(del_timer_sync
);
1086 static int cascade(struct tvec_base
*base
, struct tvec
*tv
, int index
)
1088 /* cascade all the timers from tv up one level */
1089 struct timer_list
*timer
, *tmp
;
1090 struct list_head tv_list
;
1092 list_replace_init(tv
->vec
+ index
, &tv_list
);
1095 * We are removing _all_ timers from the list, so we
1096 * don't have to detach them individually.
1098 list_for_each_entry_safe(timer
, tmp
, &tv_list
, entry
) {
1099 BUG_ON(tbase_get_base(timer
->base
) != base
);
1100 /* No accounting, while moving them */
1101 __internal_add_timer(base
, timer
);
1107 static void call_timer_fn(struct timer_list
*timer
, void (*fn
)(unsigned long),
1110 int preempt_count
= preempt_count();
1112 #ifdef CONFIG_LOCKDEP
1114 * It is permissible to free the timer from inside the
1115 * function that is called from it, this we need to take into
1116 * account for lockdep too. To avoid bogus "held lock freed"
1117 * warnings as well as problems when looking into
1118 * timer->lockdep_map, make a copy and use that here.
1120 struct lockdep_map lockdep_map
;
1122 lockdep_copy_map(&lockdep_map
, &timer
->lockdep_map
);
1125 * Couple the lock chain with the lock chain at
1126 * del_timer_sync() by acquiring the lock_map around the fn()
1127 * call here and in del_timer_sync().
1129 lock_map_acquire(&lockdep_map
);
1131 trace_timer_expire_entry(timer
);
1132 mt_trace_sft_start(fn
);
1134 mt_trace_sft_end(fn
);
1135 trace_timer_expire_exit(timer
);
1137 lock_map_release(&lockdep_map
);
1139 if (preempt_count
!= preempt_count()) {
1140 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
1141 fn
, preempt_count
, preempt_count());
1143 * Restore the preempt count. That gives us a decent
1144 * chance to survive and extract information. If the
1145 * callback kept a lock held, bad luck, but not worse
1146 * than the BUG() we had.
1148 preempt_count() = preempt_count
;
1152 #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
1155 * __run_timers - run all expired timers (if any) on this CPU.
1156 * @base: the timer vector to be processed.
1158 * This function cascades all vectors and executes all expired timer
1161 static inline void __run_timers(struct tvec_base
*base
)
1163 struct timer_list
*timer
;
1165 spin_lock_irq(&base
->lock
);
1166 while (time_after_eq(jiffies
, base
->timer_jiffies
)) {
1167 struct list_head work_list
;
1168 struct list_head
*head
= &work_list
;
1169 int index
= base
->timer_jiffies
& TVR_MASK
;
1175 (!cascade(base
, &base
->tv2
, INDEX(0))) &&
1176 (!cascade(base
, &base
->tv3
, INDEX(1))) &&
1177 !cascade(base
, &base
->tv4
, INDEX(2)))
1178 cascade(base
, &base
->tv5
, INDEX(3));
1179 ++base
->timer_jiffies
;
1180 list_replace_init(base
->tv1
.vec
+ index
, &work_list
);
1181 while (!list_empty(head
)) {
1182 void (*fn
)(unsigned long);
1186 timer
= list_first_entry(head
, struct timer_list
,entry
);
1187 fn
= timer
->function
;
1189 irqsafe
= tbase_get_irqsafe(timer
->base
);
1191 timer_stats_account_timer(timer
);
1193 base
->running_timer
= timer
;
1194 detach_expired_timer(timer
, base
);
1197 spin_unlock(&base
->lock
);
1198 call_timer_fn(timer
, fn
, data
);
1199 spin_lock(&base
->lock
);
1201 spin_unlock_irq(&base
->lock
);
1202 call_timer_fn(timer
, fn
, data
);
1203 spin_lock_irq(&base
->lock
);
1207 base
->running_timer
= NULL
;
1208 spin_unlock_irq(&base
->lock
);
1211 #ifdef CONFIG_NO_HZ_COMMON
1213 * Find out when the next timer event is due to happen. This
1214 * is used on S/390 to stop all activity when a CPU is idle.
1215 * This function needs to be called with interrupts disabled.
1217 static unsigned long __next_timer_interrupt(struct tvec_base
*base
)
1219 unsigned long timer_jiffies
= base
->timer_jiffies
;
1220 unsigned long expires
= timer_jiffies
+ NEXT_TIMER_MAX_DELTA
;
1221 int index
, slot
, array
, found
= 0;
1222 struct timer_list
*nte
;
1223 struct tvec
*varray
[4];
1225 /* Look for timer events in tv1. */
1226 index
= slot
= timer_jiffies
& TVR_MASK
;
1228 list_for_each_entry(nte
, base
->tv1
.vec
+ slot
, entry
) {
1229 if (tbase_get_deferrable(nte
->base
))
1233 expires
= nte
->expires
;
1234 /* Look at the cascade bucket(s)? */
1235 if (!index
|| slot
< index
)
1239 slot
= (slot
+ 1) & TVR_MASK
;
1240 } while (slot
!= index
);
1243 /* Calculate the next cascade event */
1245 timer_jiffies
+= TVR_SIZE
- index
;
1246 timer_jiffies
>>= TVR_BITS
;
1248 /* Check tv2-tv5. */
1249 varray
[0] = &base
->tv2
;
1250 varray
[1] = &base
->tv3
;
1251 varray
[2] = &base
->tv4
;
1252 varray
[3] = &base
->tv5
;
1254 for (array
= 0; array
< 4; array
++) {
1255 struct tvec
*varp
= varray
[array
];
1257 index
= slot
= timer_jiffies
& TVN_MASK
;
1259 list_for_each_entry(nte
, varp
->vec
+ slot
, entry
) {
1260 if (tbase_get_deferrable(nte
->base
))
1264 if (time_before(nte
->expires
, expires
))
1265 expires
= nte
->expires
;
1268 * Do we still search for the first timer or are
1269 * we looking up the cascade buckets ?
1272 /* Look at the cascade bucket(s)? */
1273 if (!index
|| slot
< index
)
1277 slot
= (slot
+ 1) & TVN_MASK
;
1278 } while (slot
!= index
);
1281 timer_jiffies
+= TVN_SIZE
- index
;
1282 timer_jiffies
>>= TVN_BITS
;
1288 * Check, if the next hrtimer event is before the next timer wheel
1291 static unsigned long cmp_next_hrtimer_event(unsigned long now
,
1292 unsigned long expires
)
1294 ktime_t hr_delta
= hrtimer_get_next_event();
1295 struct timespec tsdelta
;
1296 unsigned long delta
;
1298 if (hr_delta
.tv64
== KTIME_MAX
)
1302 * Expired timer available, let it expire in the next tick
1304 if (hr_delta
.tv64
<= 0)
1307 tsdelta
= ktime_to_timespec(hr_delta
);
1308 delta
= timespec_to_jiffies(&tsdelta
);
1311 * Limit the delta to the max value, which is checked in
1312 * tick_nohz_stop_sched_tick():
1314 if (delta
> NEXT_TIMER_MAX_DELTA
)
1315 delta
= NEXT_TIMER_MAX_DELTA
;
1318 * Take rounding errors in to account and make sure, that it
1319 * expires in the next tick. Otherwise we go into an endless
1320 * ping pong due to tick_nohz_stop_sched_tick() retriggering
1326 if (time_before(now
, expires
))
1332 * get_next_timer_interrupt - return the jiffy of the next pending timer
1333 * @now: current time (in jiffies)
1335 unsigned long get_next_timer_interrupt(unsigned long now
)
1337 struct tvec_base
*base
= __this_cpu_read(tvec_bases
);
1338 unsigned long expires
= now
+ NEXT_TIMER_MAX_DELTA
;
1341 * Pretend that there is no timer pending if the cpu is offline.
1342 * Possible pending timers will be migrated later to an active cpu.
1344 if (cpu_is_offline(smp_processor_id()))
1347 spin_lock(&base
->lock
);
1348 if (base
->active_timers
) {
1349 if (time_before_eq(base
->next_timer
, base
->timer_jiffies
))
1350 base
->next_timer
= __next_timer_interrupt(base
);
1351 expires
= base
->next_timer
;
1353 spin_unlock(&base
->lock
);
1355 if (time_before_eq(expires
, now
))
1358 return cmp_next_hrtimer_event(now
, expires
);
1363 * Called from the timer interrupt handler to charge one tick to the current
1364 * process. user_tick is 1 if the tick is user time, 0 for system.
1366 void update_process_times(int user_tick
)
1368 struct task_struct
*p
= current
;
1369 int cpu
= smp_processor_id();
1371 /* Note: this timer irq context must be accounted for as well. */
1372 account_process_tick(p
, user_tick
);
1374 rcu_check_callbacks(cpu
, user_tick
);
1375 #ifdef CONFIG_IRQ_WORK
1380 run_posix_cpu_timers(p
);
1384 * This function runs timers and the timer-tq in bottom half context.
1386 static void run_timer_softirq(struct softirq_action
*h
)
1388 struct tvec_base
*base
= __this_cpu_read(tvec_bases
);
1390 hrtimer_run_pending();
1392 if (time_after_eq(jiffies
, base
->timer_jiffies
))
1397 * Called by the local, per-CPU timer interrupt on SMP.
1399 void run_local_timers(void)
1401 hrtimer_run_queues();
1402 raise_softirq(TIMER_SOFTIRQ
);
1405 #ifdef __ARCH_WANT_SYS_ALARM
1408 * For backwards compatibility? This can be done in libc so Alpha
1409 * and all newer ports shouldn't need it.
1411 SYSCALL_DEFINE1(alarm
, unsigned int, seconds
)
1413 return alarm_setitimer(seconds
);
1418 static void process_timeout(unsigned long __data
)
1420 wake_up_process((struct task_struct
*)__data
);
1424 * schedule_timeout - sleep until timeout
1425 * @timeout: timeout value in jiffies
1427 * Make the current task sleep until @timeout jiffies have
1428 * elapsed. The routine will return immediately unless
1429 * the current task state has been set (see set_current_state()).
1431 * You can set the task state as follows -
1433 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1434 * pass before the routine returns. The routine will return 0
1436 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1437 * delivered to the current task. In this case the remaining time
1438 * in jiffies will be returned, or 0 if the timer expired in time
1440 * The current task state is guaranteed to be TASK_RUNNING when this
1443 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1444 * the CPU away without a bound on the timeout. In this case the return
1445 * value will be %MAX_SCHEDULE_TIMEOUT.
1447 * In all cases the return value is guaranteed to be non-negative.
1449 signed long __sched
schedule_timeout(signed long timeout
)
1451 struct timer_list timer
;
1452 unsigned long expire
;
1456 case MAX_SCHEDULE_TIMEOUT
:
1458 * These two special cases are useful to be comfortable
1459 * in the caller. Nothing more. We could take
1460 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1461 * but I' d like to return a valid offset (>=0) to allow
1462 * the caller to do everything it want with the retval.
1468 * Another bit of PARANOID. Note that the retval will be
1469 * 0 since no piece of kernel is supposed to do a check
1470 * for a negative retval of schedule_timeout() (since it
1471 * should never happens anyway). You just have the printk()
1472 * that will tell you if something is gone wrong and where.
1475 printk(KERN_ERR
"schedule_timeout: wrong timeout "
1476 "value %lx\n", timeout
);
1478 current
->state
= TASK_RUNNING
;
1483 expire
= timeout
+ jiffies
;
1485 setup_timer_on_stack(&timer
, process_timeout
, (unsigned long)current
);
1486 __mod_timer(&timer
, expire
, false, TIMER_NOT_PINNED
);
1488 del_singleshot_timer_sync(&timer
);
1490 /* Remove the timer from the object tracker */
1491 destroy_timer_on_stack(&timer
);
1493 timeout
= expire
- jiffies
;
1496 return timeout
< 0 ? 0 : timeout
;
1498 EXPORT_SYMBOL(schedule_timeout
);
1501 * We can use __set_current_state() here because schedule_timeout() calls
1502 * schedule() unconditionally.
1504 signed long __sched
schedule_timeout_interruptible(signed long timeout
)
1506 __set_current_state(TASK_INTERRUPTIBLE
);
1507 return schedule_timeout(timeout
);
1509 EXPORT_SYMBOL(schedule_timeout_interruptible
);
1511 signed long __sched
schedule_timeout_killable(signed long timeout
)
1513 __set_current_state(TASK_KILLABLE
);
1514 return schedule_timeout(timeout
);
1516 EXPORT_SYMBOL(schedule_timeout_killable
);
1518 signed long __sched
schedule_timeout_uninterruptible(signed long timeout
)
1520 __set_current_state(TASK_UNINTERRUPTIBLE
);
1521 return schedule_timeout(timeout
);
1523 EXPORT_SYMBOL(schedule_timeout_uninterruptible
);
1525 static int __cpuinit
init_timers_cpu(int cpu
)
1528 struct tvec_base
*base
;
1529 static char __cpuinitdata tvec_base_done
[NR_CPUS
];
1531 if (!tvec_base_done
[cpu
]) {
1532 static char boot_done
;
1536 * The APs use this path later in boot
1538 base
= kmalloc_node(sizeof(*base
),
1539 GFP_KERNEL
| __GFP_ZERO
,
1544 /* Make sure that tvec_base is 2 byte aligned */
1545 if (tbase_get_deferrable(base
)) {
1550 per_cpu(tvec_bases
, cpu
) = base
;
1553 * This is for the boot CPU - we use compile-time
1554 * static initialisation because per-cpu memory isn't
1555 * ready yet and because the memory allocators are not
1556 * initialised either.
1559 base
= &boot_tvec_bases
;
1561 spin_lock_init(&base
->lock
);
1562 tvec_base_done
[cpu
] = 1;
1564 base
= per_cpu(tvec_bases
, cpu
);
1568 for (j
= 0; j
< TVN_SIZE
; j
++) {
1569 INIT_LIST_HEAD(base
->tv5
.vec
+ j
);
1570 INIT_LIST_HEAD(base
->tv4
.vec
+ j
);
1571 INIT_LIST_HEAD(base
->tv3
.vec
+ j
);
1572 INIT_LIST_HEAD(base
->tv2
.vec
+ j
);
1574 for (j
= 0; j
< TVR_SIZE
; j
++)
1575 INIT_LIST_HEAD(base
->tv1
.vec
+ j
);
1577 base
->timer_jiffies
= jiffies
;
1578 base
->next_timer
= base
->timer_jiffies
;
1579 base
->active_timers
= 0;
1583 #ifdef CONFIG_HOTPLUG_CPU
1584 static void migrate_timer_list(struct tvec_base
*new_base
, struct list_head
*head
)
1586 struct timer_list
*timer
;
1588 while (!list_empty(head
)) {
1589 timer
= list_first_entry(head
, struct timer_list
, entry
);
1590 /* We ignore the accounting on the dying cpu */
1591 detach_timer(timer
, false);
1592 timer_set_base(timer
, new_base
);
1593 internal_add_timer(new_base
, timer
);
1597 static void __cpuinit
migrate_timers(int cpu
)
1599 struct tvec_base
*old_base
;
1600 struct tvec_base
*new_base
;
1603 BUG_ON(cpu_online(cpu
));
1604 old_base
= per_cpu(tvec_bases
, cpu
);
1605 new_base
= get_cpu_var(tvec_bases
);
1607 * The caller is globally serialized and nobody else
1608 * takes two locks at once, deadlock is not possible.
1610 spin_lock_irq(&new_base
->lock
);
1611 spin_lock_nested(&old_base
->lock
, SINGLE_DEPTH_NESTING
);
1613 BUG_ON(old_base
->running_timer
);
1615 for (i
= 0; i
< TVR_SIZE
; i
++)
1616 migrate_timer_list(new_base
, old_base
->tv1
.vec
+ i
);
1617 for (i
= 0; i
< TVN_SIZE
; i
++) {
1618 migrate_timer_list(new_base
, old_base
->tv2
.vec
+ i
);
1619 migrate_timer_list(new_base
, old_base
->tv3
.vec
+ i
);
1620 migrate_timer_list(new_base
, old_base
->tv4
.vec
+ i
);
1621 migrate_timer_list(new_base
, old_base
->tv5
.vec
+ i
);
1624 spin_unlock(&old_base
->lock
);
1625 spin_unlock_irq(&new_base
->lock
);
1626 put_cpu_var(tvec_bases
);
1628 #endif /* CONFIG_HOTPLUG_CPU */
1630 static int __cpuinit
timer_cpu_notify(struct notifier_block
*self
,
1631 unsigned long action
, void *hcpu
)
1633 long cpu
= (long)hcpu
;
1637 case CPU_UP_PREPARE
:
1638 case CPU_UP_PREPARE_FROZEN
:
1639 err
= init_timers_cpu(cpu
);
1641 return notifier_from_errno(err
);
1643 #ifdef CONFIG_HOTPLUG_CPU
1645 case CPU_DEAD_FROZEN
:
1646 migrate_timers(cpu
);
1655 static struct notifier_block __cpuinitdata timers_nb
= {
1656 .notifier_call
= timer_cpu_notify
,
1660 void __init
init_timers(void)
1664 /* ensure there are enough low bits for flags in timer->base pointer */
1665 BUILD_BUG_ON(__alignof__(struct tvec_base
) & TIMER_FLAG_MASK
);
1667 err
= timer_cpu_notify(&timers_nb
, (unsigned long)CPU_UP_PREPARE
,
1668 (void *)(long)smp_processor_id());
1671 BUG_ON(err
!= NOTIFY_OK
);
1672 register_cpu_notifier(&timers_nb
);
1673 open_softirq(TIMER_SOFTIRQ
, run_timer_softirq
);
1677 * msleep - sleep safely even with waitqueue interruptions
1678 * @msecs: Time in milliseconds to sleep for
1680 void msleep(unsigned int msecs
)
1682 unsigned long timeout
= msecs_to_jiffies(msecs
) + 1;
1685 timeout
= schedule_timeout_uninterruptible(timeout
);
1688 EXPORT_SYMBOL(msleep
);
1691 * msleep_interruptible - sleep waiting for signals
1692 * @msecs: Time in milliseconds to sleep for
1694 unsigned long msleep_interruptible(unsigned int msecs
)
1696 unsigned long timeout
= msecs_to_jiffies(msecs
) + 1;
1698 while (timeout
&& !signal_pending(current
))
1699 timeout
= schedule_timeout_interruptible(timeout
);
1700 return jiffies_to_msecs(timeout
);
1703 EXPORT_SYMBOL(msleep_interruptible
);
1705 static int __sched
do_usleep_range(unsigned long min
, unsigned long max
)
1708 unsigned long delta
;
1710 kmin
= ktime_set(0, min
* NSEC_PER_USEC
);
1711 delta
= (max
- min
) * NSEC_PER_USEC
;
1712 return schedule_hrtimeout_range(&kmin
, delta
, HRTIMER_MODE_REL
);
1716 * usleep_range - Drop in replacement for udelay where wakeup is flexible
1717 * @min: Minimum time in usecs to sleep
1718 * @max: Maximum time in usecs to sleep
1720 void usleep_range(unsigned long min
, unsigned long max
)
1722 __set_current_state(TASK_UNINTERRUPTIBLE
);
1723 do_usleep_range(min
, max
);
1725 EXPORT_SYMBOL(usleep_range
);