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
*base
= per_cpu(tvec_bases
, cpu
);
931 timer_stats_timer_set_start_info(timer
);
932 BUG_ON(timer_pending(timer
) || !timer
->function
);
933 spin_lock_irqsave(&base
->lock
, flags
);
934 timer_set_base(timer
, base
);
935 debug_activate(timer
, timer
->expires
);
936 internal_add_timer(base
, timer
);
938 * Check whether the other CPU is in dynticks mode and needs
939 * to be triggered to reevaluate the timer wheel.
940 * We are protected against the other CPU fiddling
941 * with the timer by holding the timer base lock. This also
942 * makes sure that a CPU on the way to stop its tick can not
943 * evaluate the timer wheel.
945 wake_up_nohz_cpu(cpu
);
946 spin_unlock_irqrestore(&base
->lock
, flags
);
948 EXPORT_SYMBOL_GPL(add_timer_on
);
951 * del_timer - deactive a timer.
952 * @timer: the timer to be deactivated
954 * del_timer() deactivates a timer - this works on both active and inactive
957 * The function returns whether it has deactivated a pending timer or not.
958 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
959 * active timer returns 1.)
961 int del_timer(struct timer_list
*timer
)
963 struct tvec_base
*base
;
967 debug_assert_init(timer
);
969 timer_stats_timer_clear_start_info(timer
);
970 if (timer_pending(timer
)) {
971 base
= lock_timer_base(timer
, &flags
);
972 ret
= detach_if_pending(timer
, base
, true);
973 spin_unlock_irqrestore(&base
->lock
, flags
);
978 EXPORT_SYMBOL(del_timer
);
981 * try_to_del_timer_sync - Try to deactivate a timer
982 * @timer: timer do del
984 * This function tries to deactivate a timer. Upon successful (ret >= 0)
985 * exit the timer is not queued and the handler is not running on any CPU.
987 int try_to_del_timer_sync(struct timer_list
*timer
)
989 struct tvec_base
*base
;
993 debug_assert_init(timer
);
995 base
= lock_timer_base(timer
, &flags
);
997 if (base
->running_timer
!= timer
) {
998 timer_stats_timer_clear_start_info(timer
);
999 ret
= detach_if_pending(timer
, base
, true);
1001 spin_unlock_irqrestore(&base
->lock
, flags
);
1005 EXPORT_SYMBOL(try_to_del_timer_sync
);
1009 * del_timer_sync - deactivate a timer and wait for the handler to finish.
1010 * @timer: the timer to be deactivated
1012 * This function only differs from del_timer() on SMP: besides deactivating
1013 * the timer it also makes sure the handler has finished executing on other
1016 * Synchronization rules: Callers must prevent restarting of the timer,
1017 * otherwise this function is meaningless. It must not be called from
1018 * interrupt contexts unless the timer is an irqsafe one. The caller must
1019 * not hold locks which would prevent completion of the timer's
1020 * handler. The timer's handler must not call add_timer_on(). Upon exit the
1021 * timer is not queued and the handler is not running on any CPU.
1023 * Note: For !irqsafe timers, you must not hold locks that are held in
1024 * interrupt context while calling this function. Even if the lock has
1025 * nothing to do with the timer in question. Here's why:
1031 * base->running_timer = mytimer;
1032 * spin_lock_irq(somelock);
1034 * spin_lock(somelock);
1035 * del_timer_sync(mytimer);
1036 * while (base->running_timer == mytimer);
1038 * Now del_timer_sync() will never return and never release somelock.
1039 * The interrupt on the other CPU is waiting to grab somelock but
1040 * it has interrupted the softirq that CPU0 is waiting to finish.
1042 * The function returns whether it has deactivated a pending timer or not.
1044 int del_timer_sync(struct timer_list
*timer
)
1046 #ifdef CONFIG_LOCKDEP
1047 unsigned long flags
;
1050 * If lockdep gives a backtrace here, please reference
1051 * the synchronization rules above.
1053 local_irq_save(flags
);
1054 lock_map_acquire(&timer
->lockdep_map
);
1055 lock_map_release(&timer
->lockdep_map
);
1056 local_irq_restore(flags
);
1059 * don't use it in hardirq context, because it
1060 * could lead to deadlock.
1062 WARN_ON(in_irq() && !tbase_get_irqsafe(timer
->base
));
1064 int ret
= try_to_del_timer_sync(timer
);
1070 EXPORT_SYMBOL(del_timer_sync
);
1073 static int cascade(struct tvec_base
*base
, struct tvec
*tv
, int index
)
1075 /* cascade all the timers from tv up one level */
1076 struct timer_list
*timer
, *tmp
;
1077 struct list_head tv_list
;
1079 list_replace_init(tv
->vec
+ index
, &tv_list
);
1082 * We are removing _all_ timers from the list, so we
1083 * don't have to detach them individually.
1085 list_for_each_entry_safe(timer
, tmp
, &tv_list
, entry
) {
1086 BUG_ON(tbase_get_base(timer
->base
) != base
);
1087 /* No accounting, while moving them */
1088 __internal_add_timer(base
, timer
);
1094 static void call_timer_fn(struct timer_list
*timer
, void (*fn
)(unsigned long),
1097 int preempt_count
= preempt_count();
1099 #ifdef CONFIG_LOCKDEP
1101 * It is permissible to free the timer from inside the
1102 * function that is called from it, this we need to take into
1103 * account for lockdep too. To avoid bogus "held lock freed"
1104 * warnings as well as problems when looking into
1105 * timer->lockdep_map, make a copy and use that here.
1107 struct lockdep_map lockdep_map
;
1109 lockdep_copy_map(&lockdep_map
, &timer
->lockdep_map
);
1112 * Couple the lock chain with the lock chain at
1113 * del_timer_sync() by acquiring the lock_map around the fn()
1114 * call here and in del_timer_sync().
1116 lock_map_acquire(&lockdep_map
);
1118 trace_timer_expire_entry(timer
);
1119 mt_trace_sft_start(fn
);
1121 mt_trace_sft_end(fn
);
1122 trace_timer_expire_exit(timer
);
1124 lock_map_release(&lockdep_map
);
1126 if (preempt_count
!= preempt_count()) {
1127 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
1128 fn
, preempt_count
, preempt_count());
1130 * Restore the preempt count. That gives us a decent
1131 * chance to survive and extract information. If the
1132 * callback kept a lock held, bad luck, but not worse
1133 * than the BUG() we had.
1135 preempt_count() = preempt_count
;
1139 #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
1142 * __run_timers - run all expired timers (if any) on this CPU.
1143 * @base: the timer vector to be processed.
1145 * This function cascades all vectors and executes all expired timer
1148 static inline void __run_timers(struct tvec_base
*base
)
1150 struct timer_list
*timer
;
1152 spin_lock_irq(&base
->lock
);
1153 while (time_after_eq(jiffies
, base
->timer_jiffies
)) {
1154 struct list_head work_list
;
1155 struct list_head
*head
= &work_list
;
1156 int index
= base
->timer_jiffies
& TVR_MASK
;
1162 (!cascade(base
, &base
->tv2
, INDEX(0))) &&
1163 (!cascade(base
, &base
->tv3
, INDEX(1))) &&
1164 !cascade(base
, &base
->tv4
, INDEX(2)))
1165 cascade(base
, &base
->tv5
, INDEX(3));
1166 ++base
->timer_jiffies
;
1167 list_replace_init(base
->tv1
.vec
+ index
, &work_list
);
1168 while (!list_empty(head
)) {
1169 void (*fn
)(unsigned long);
1173 timer
= list_first_entry(head
, struct timer_list
,entry
);
1174 fn
= timer
->function
;
1176 irqsafe
= tbase_get_irqsafe(timer
->base
);
1178 timer_stats_account_timer(timer
);
1180 base
->running_timer
= timer
;
1181 detach_expired_timer(timer
, base
);
1184 spin_unlock(&base
->lock
);
1185 call_timer_fn(timer
, fn
, data
);
1186 spin_lock(&base
->lock
);
1188 spin_unlock_irq(&base
->lock
);
1189 call_timer_fn(timer
, fn
, data
);
1190 spin_lock_irq(&base
->lock
);
1194 base
->running_timer
= NULL
;
1195 spin_unlock_irq(&base
->lock
);
1198 #ifdef CONFIG_NO_HZ_COMMON
1200 * Find out when the next timer event is due to happen. This
1201 * is used on S/390 to stop all activity when a CPU is idle.
1202 * This function needs to be called with interrupts disabled.
1204 static unsigned long __next_timer_interrupt(struct tvec_base
*base
)
1206 unsigned long timer_jiffies
= base
->timer_jiffies
;
1207 unsigned long expires
= timer_jiffies
+ NEXT_TIMER_MAX_DELTA
;
1208 int index
, slot
, array
, found
= 0;
1209 struct timer_list
*nte
;
1210 struct tvec
*varray
[4];
1212 /* Look for timer events in tv1. */
1213 index
= slot
= timer_jiffies
& TVR_MASK
;
1215 list_for_each_entry(nte
, base
->tv1
.vec
+ slot
, entry
) {
1216 if (tbase_get_deferrable(nte
->base
))
1220 expires
= nte
->expires
;
1221 /* Look at the cascade bucket(s)? */
1222 if (!index
|| slot
< index
)
1226 slot
= (slot
+ 1) & TVR_MASK
;
1227 } while (slot
!= index
);
1230 /* Calculate the next cascade event */
1232 timer_jiffies
+= TVR_SIZE
- index
;
1233 timer_jiffies
>>= TVR_BITS
;
1235 /* Check tv2-tv5. */
1236 varray
[0] = &base
->tv2
;
1237 varray
[1] = &base
->tv3
;
1238 varray
[2] = &base
->tv4
;
1239 varray
[3] = &base
->tv5
;
1241 for (array
= 0; array
< 4; array
++) {
1242 struct tvec
*varp
= varray
[array
];
1244 index
= slot
= timer_jiffies
& TVN_MASK
;
1246 list_for_each_entry(nte
, varp
->vec
+ slot
, entry
) {
1247 if (tbase_get_deferrable(nte
->base
))
1251 if (time_before(nte
->expires
, expires
))
1252 expires
= nte
->expires
;
1255 * Do we still search for the first timer or are
1256 * we looking up the cascade buckets ?
1259 /* Look at the cascade bucket(s)? */
1260 if (!index
|| slot
< index
)
1264 slot
= (slot
+ 1) & TVN_MASK
;
1265 } while (slot
!= index
);
1268 timer_jiffies
+= TVN_SIZE
- index
;
1269 timer_jiffies
>>= TVN_BITS
;
1275 * Check, if the next hrtimer event is before the next timer wheel
1278 static unsigned long cmp_next_hrtimer_event(unsigned long now
,
1279 unsigned long expires
)
1281 ktime_t hr_delta
= hrtimer_get_next_event();
1282 struct timespec tsdelta
;
1283 unsigned long delta
;
1285 if (hr_delta
.tv64
== KTIME_MAX
)
1289 * Expired timer available, let it expire in the next tick
1291 if (hr_delta
.tv64
<= 0)
1294 tsdelta
= ktime_to_timespec(hr_delta
);
1295 delta
= timespec_to_jiffies(&tsdelta
);
1298 * Limit the delta to the max value, which is checked in
1299 * tick_nohz_stop_sched_tick():
1301 if (delta
> NEXT_TIMER_MAX_DELTA
)
1302 delta
= NEXT_TIMER_MAX_DELTA
;
1305 * Take rounding errors in to account and make sure, that it
1306 * expires in the next tick. Otherwise we go into an endless
1307 * ping pong due to tick_nohz_stop_sched_tick() retriggering
1313 if (time_before(now
, expires
))
1319 * get_next_timer_interrupt - return the jiffy of the next pending timer
1320 * @now: current time (in jiffies)
1322 unsigned long get_next_timer_interrupt(unsigned long now
)
1324 struct tvec_base
*base
= __this_cpu_read(tvec_bases
);
1325 unsigned long expires
= now
+ NEXT_TIMER_MAX_DELTA
;
1328 * Pretend that there is no timer pending if the cpu is offline.
1329 * Possible pending timers will be migrated later to an active cpu.
1331 if (cpu_is_offline(smp_processor_id()))
1334 spin_lock(&base
->lock
);
1335 if (base
->active_timers
) {
1336 if (time_before_eq(base
->next_timer
, base
->timer_jiffies
))
1337 base
->next_timer
= __next_timer_interrupt(base
);
1338 expires
= base
->next_timer
;
1340 spin_unlock(&base
->lock
);
1342 if (time_before_eq(expires
, now
))
1345 return cmp_next_hrtimer_event(now
, expires
);
1350 * Called from the timer interrupt handler to charge one tick to the current
1351 * process. user_tick is 1 if the tick is user time, 0 for system.
1353 void update_process_times(int user_tick
)
1355 struct task_struct
*p
= current
;
1356 int cpu
= smp_processor_id();
1358 /* Note: this timer irq context must be accounted for as well. */
1359 account_process_tick(p
, user_tick
);
1361 rcu_check_callbacks(cpu
, user_tick
);
1362 #ifdef CONFIG_IRQ_WORK
1367 run_posix_cpu_timers(p
);
1371 * This function runs timers and the timer-tq in bottom half context.
1373 static void run_timer_softirq(struct softirq_action
*h
)
1375 struct tvec_base
*base
= __this_cpu_read(tvec_bases
);
1377 hrtimer_run_pending();
1379 if (time_after_eq(jiffies
, base
->timer_jiffies
))
1384 * Called by the local, per-CPU timer interrupt on SMP.
1386 void run_local_timers(void)
1388 hrtimer_run_queues();
1389 raise_softirq(TIMER_SOFTIRQ
);
1392 #ifdef __ARCH_WANT_SYS_ALARM
1395 * For backwards compatibility? This can be done in libc so Alpha
1396 * and all newer ports shouldn't need it.
1398 SYSCALL_DEFINE1(alarm
, unsigned int, seconds
)
1400 return alarm_setitimer(seconds
);
1405 static void process_timeout(unsigned long __data
)
1407 wake_up_process((struct task_struct
*)__data
);
1411 * schedule_timeout - sleep until timeout
1412 * @timeout: timeout value in jiffies
1414 * Make the current task sleep until @timeout jiffies have
1415 * elapsed. The routine will return immediately unless
1416 * the current task state has been set (see set_current_state()).
1418 * You can set the task state as follows -
1420 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1421 * pass before the routine returns. The routine will return 0
1423 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1424 * delivered to the current task. In this case the remaining time
1425 * in jiffies will be returned, or 0 if the timer expired in time
1427 * The current task state is guaranteed to be TASK_RUNNING when this
1430 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1431 * the CPU away without a bound on the timeout. In this case the return
1432 * value will be %MAX_SCHEDULE_TIMEOUT.
1434 * In all cases the return value is guaranteed to be non-negative.
1436 signed long __sched
schedule_timeout(signed long timeout
)
1438 struct timer_list timer
;
1439 unsigned long expire
;
1443 case MAX_SCHEDULE_TIMEOUT
:
1445 * These two special cases are useful to be comfortable
1446 * in the caller. Nothing more. We could take
1447 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1448 * but I' d like to return a valid offset (>=0) to allow
1449 * the caller to do everything it want with the retval.
1455 * Another bit of PARANOID. Note that the retval will be
1456 * 0 since no piece of kernel is supposed to do a check
1457 * for a negative retval of schedule_timeout() (since it
1458 * should never happens anyway). You just have the printk()
1459 * that will tell you if something is gone wrong and where.
1462 printk(KERN_ERR
"schedule_timeout: wrong timeout "
1463 "value %lx\n", timeout
);
1465 current
->state
= TASK_RUNNING
;
1470 expire
= timeout
+ jiffies
;
1472 setup_timer_on_stack(&timer
, process_timeout
, (unsigned long)current
);
1473 __mod_timer(&timer
, expire
, false, TIMER_NOT_PINNED
);
1475 del_singleshot_timer_sync(&timer
);
1477 /* Remove the timer from the object tracker */
1478 destroy_timer_on_stack(&timer
);
1480 timeout
= expire
- jiffies
;
1483 return timeout
< 0 ? 0 : timeout
;
1485 EXPORT_SYMBOL(schedule_timeout
);
1488 * We can use __set_current_state() here because schedule_timeout() calls
1489 * schedule() unconditionally.
1491 signed long __sched
schedule_timeout_interruptible(signed long timeout
)
1493 __set_current_state(TASK_INTERRUPTIBLE
);
1494 return schedule_timeout(timeout
);
1496 EXPORT_SYMBOL(schedule_timeout_interruptible
);
1498 signed long __sched
schedule_timeout_killable(signed long timeout
)
1500 __set_current_state(TASK_KILLABLE
);
1501 return schedule_timeout(timeout
);
1503 EXPORT_SYMBOL(schedule_timeout_killable
);
1505 signed long __sched
schedule_timeout_uninterruptible(signed long timeout
)
1507 __set_current_state(TASK_UNINTERRUPTIBLE
);
1508 return schedule_timeout(timeout
);
1510 EXPORT_SYMBOL(schedule_timeout_uninterruptible
);
1512 static int __cpuinit
init_timers_cpu(int cpu
)
1515 struct tvec_base
*base
;
1516 static char __cpuinitdata tvec_base_done
[NR_CPUS
];
1518 if (!tvec_base_done
[cpu
]) {
1519 static char boot_done
;
1523 * The APs use this path later in boot
1525 base
= kmalloc_node(sizeof(*base
),
1526 GFP_KERNEL
| __GFP_ZERO
,
1531 /* Make sure that tvec_base is 2 byte aligned */
1532 if (tbase_get_deferrable(base
)) {
1537 per_cpu(tvec_bases
, cpu
) = base
;
1540 * This is for the boot CPU - we use compile-time
1541 * static initialisation because per-cpu memory isn't
1542 * ready yet and because the memory allocators are not
1543 * initialised either.
1546 base
= &boot_tvec_bases
;
1548 spin_lock_init(&base
->lock
);
1549 tvec_base_done
[cpu
] = 1;
1551 base
= per_cpu(tvec_bases
, cpu
);
1555 for (j
= 0; j
< TVN_SIZE
; j
++) {
1556 INIT_LIST_HEAD(base
->tv5
.vec
+ j
);
1557 INIT_LIST_HEAD(base
->tv4
.vec
+ j
);
1558 INIT_LIST_HEAD(base
->tv3
.vec
+ j
);
1559 INIT_LIST_HEAD(base
->tv2
.vec
+ j
);
1561 for (j
= 0; j
< TVR_SIZE
; j
++)
1562 INIT_LIST_HEAD(base
->tv1
.vec
+ j
);
1564 base
->timer_jiffies
= jiffies
;
1565 base
->next_timer
= base
->timer_jiffies
;
1566 base
->active_timers
= 0;
1570 #ifdef CONFIG_HOTPLUG_CPU
1571 static void migrate_timer_list(struct tvec_base
*new_base
, struct list_head
*head
)
1573 struct timer_list
*timer
;
1575 while (!list_empty(head
)) {
1576 timer
= list_first_entry(head
, struct timer_list
, entry
);
1577 /* We ignore the accounting on the dying cpu */
1578 detach_timer(timer
, false);
1579 timer_set_base(timer
, new_base
);
1580 internal_add_timer(new_base
, timer
);
1584 static void __cpuinit
migrate_timers(int cpu
)
1586 struct tvec_base
*old_base
;
1587 struct tvec_base
*new_base
;
1590 BUG_ON(cpu_online(cpu
));
1591 old_base
= per_cpu(tvec_bases
, cpu
);
1592 new_base
= get_cpu_var(tvec_bases
);
1594 * The caller is globally serialized and nobody else
1595 * takes two locks at once, deadlock is not possible.
1597 spin_lock_irq(&new_base
->lock
);
1598 spin_lock_nested(&old_base
->lock
, SINGLE_DEPTH_NESTING
);
1600 BUG_ON(old_base
->running_timer
);
1602 for (i
= 0; i
< TVR_SIZE
; i
++)
1603 migrate_timer_list(new_base
, old_base
->tv1
.vec
+ i
);
1604 for (i
= 0; i
< TVN_SIZE
; i
++) {
1605 migrate_timer_list(new_base
, old_base
->tv2
.vec
+ i
);
1606 migrate_timer_list(new_base
, old_base
->tv3
.vec
+ i
);
1607 migrate_timer_list(new_base
, old_base
->tv4
.vec
+ i
);
1608 migrate_timer_list(new_base
, old_base
->tv5
.vec
+ i
);
1611 spin_unlock(&old_base
->lock
);
1612 spin_unlock_irq(&new_base
->lock
);
1613 put_cpu_var(tvec_bases
);
1615 #endif /* CONFIG_HOTPLUG_CPU */
1617 static int __cpuinit
timer_cpu_notify(struct notifier_block
*self
,
1618 unsigned long action
, void *hcpu
)
1620 long cpu
= (long)hcpu
;
1624 case CPU_UP_PREPARE
:
1625 case CPU_UP_PREPARE_FROZEN
:
1626 err
= init_timers_cpu(cpu
);
1628 return notifier_from_errno(err
);
1630 #ifdef CONFIG_HOTPLUG_CPU
1632 case CPU_DEAD_FROZEN
:
1633 migrate_timers(cpu
);
1642 static struct notifier_block __cpuinitdata timers_nb
= {
1643 .notifier_call
= timer_cpu_notify
,
1647 void __init
init_timers(void)
1651 /* ensure there are enough low bits for flags in timer->base pointer */
1652 BUILD_BUG_ON(__alignof__(struct tvec_base
) & TIMER_FLAG_MASK
);
1654 err
= timer_cpu_notify(&timers_nb
, (unsigned long)CPU_UP_PREPARE
,
1655 (void *)(long)smp_processor_id());
1658 BUG_ON(err
!= NOTIFY_OK
);
1659 register_cpu_notifier(&timers_nb
);
1660 open_softirq(TIMER_SOFTIRQ
, run_timer_softirq
);
1664 * msleep - sleep safely even with waitqueue interruptions
1665 * @msecs: Time in milliseconds to sleep for
1667 void msleep(unsigned int msecs
)
1669 unsigned long timeout
= msecs_to_jiffies(msecs
) + 1;
1672 timeout
= schedule_timeout_uninterruptible(timeout
);
1675 EXPORT_SYMBOL(msleep
);
1678 * msleep_interruptible - sleep waiting for signals
1679 * @msecs: Time in milliseconds to sleep for
1681 unsigned long msleep_interruptible(unsigned int msecs
)
1683 unsigned long timeout
= msecs_to_jiffies(msecs
) + 1;
1685 while (timeout
&& !signal_pending(current
))
1686 timeout
= schedule_timeout_interruptible(timeout
);
1687 return jiffies_to_msecs(timeout
);
1690 EXPORT_SYMBOL(msleep_interruptible
);
1692 static int __sched
do_usleep_range(unsigned long min
, unsigned long max
)
1695 unsigned long delta
;
1697 kmin
= ktime_set(0, min
* NSEC_PER_USEC
);
1698 delta
= (max
- min
) * NSEC_PER_USEC
;
1699 return schedule_hrtimeout_range(&kmin
, delta
, HRTIMER_MODE_REL
);
1703 * usleep_range - Drop in replacement for udelay where wakeup is flexible
1704 * @min: Minimum time in usecs to sleep
1705 * @max: Maximum time in usecs to sleep
1707 void usleep_range(unsigned long min
, unsigned long max
)
1709 __set_current_state(TASK_UNINTERRUPTIBLE
);
1710 do_usleep_range(min
, max
);
1712 EXPORT_SYMBOL(usleep_range
);