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 "tick-internal.h"
54 #define CREATE_TRACE_POINTS
55 #include <trace/events/timer.h>
57 __visible 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 hlist_head vec[TVN_SIZE];
77 struct hlist_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;
86 unsigned long all_timers;
93 } ____cacheline_aligned;
96 static DEFINE_PER_CPU(struct tvec_base, tvec_bases);
98 static unsigned long round_jiffies_common(unsigned long j, int cpu,
102 unsigned long original = j;
105 * We don't want all cpus firing their timers at once hitting the
106 * same lock or cachelines, so we skew each extra cpu with an extra
107 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
109 * The skew is done by adding 3*cpunr, then round, then subtract this
110 * extra offset again.
117 * If the target jiffie is just after a whole second (which can happen
118 * due to delays of the timer irq, long irq off times etc etc) then
119 * we should round down to the whole second, not up. Use 1/4th second
120 * as cutoff for this rounding as an extreme upper bound for this.
121 * But never round down if @force_up is set.
123 if (rem < HZ/4 && !force_up) /* round down */
128 /* now that we have rounded, subtract the extra skew again */
132 * Make sure j is still in the future. Otherwise return the
135 return time_is_after_jiffies(j) ? j : original;
139 * __round_jiffies - function to round jiffies to a full second
140 * @j: the time in (absolute) jiffies that should be rounded
141 * @cpu: the processor number on which the timeout will happen
143 * __round_jiffies() rounds an absolute time in the future (in jiffies)
144 * up or down to (approximately) full seconds. This is useful for timers
145 * for which the exact time they fire does not matter too much, as long as
146 * they fire approximately every X seconds.
148 * By rounding these timers to whole seconds, all such timers will fire
149 * at the same time, rather than at various times spread out. The goal
150 * of this is to have the CPU wake up less, which saves power.
152 * The exact rounding is skewed for each processor to avoid all
153 * processors firing at the exact same time, which could lead
154 * to lock contention or spurious cache line bouncing.
156 * The return value is the rounded version of the @j parameter.
158 unsigned long __round_jiffies(unsigned long j, int cpu)
160 return round_jiffies_common(j, cpu, false);
162 EXPORT_SYMBOL_GPL(__round_jiffies);
165 * __round_jiffies_relative - function to round jiffies to a full second
166 * @j: the time in (relative) jiffies that should be rounded
167 * @cpu: the processor number on which the timeout will happen
169 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
170 * up or down to (approximately) full seconds. This is useful for timers
171 * for which the exact time they fire does not matter too much, as long as
172 * they fire approximately every X seconds.
174 * By rounding these timers to whole seconds, all such timers will fire
175 * at the same time, rather than at various times spread out. The goal
176 * of this is to have the CPU wake up less, which saves power.
178 * The exact rounding is skewed for each processor to avoid all
179 * processors firing at the exact same time, which could lead
180 * to lock contention or spurious cache line bouncing.
182 * The return value is the rounded version of the @j parameter.
184 unsigned long __round_jiffies_relative(unsigned long j, int cpu)
186 unsigned long j0 = jiffies;
188 /* Use j0 because jiffies might change while we run */
189 return round_jiffies_common(j + j0, cpu, false) - j0;
191 EXPORT_SYMBOL_GPL(__round_jiffies_relative);
194 * round_jiffies - function to round jiffies to a full second
195 * @j: the time in (absolute) jiffies that should be rounded
197 * round_jiffies() rounds an absolute time in the future (in jiffies)
198 * up or down to (approximately) full seconds. This is useful for timers
199 * for which the exact time they fire does not matter too much, as long as
200 * they fire approximately every X seconds.
202 * By rounding these timers to whole seconds, all such timers will fire
203 * at the same time, rather than at various times spread out. The goal
204 * of this is to have the CPU wake up less, which saves power.
206 * The return value is the rounded version of the @j parameter.
208 unsigned long round_jiffies(unsigned long j)
210 return round_jiffies_common(j, raw_smp_processor_id(), false);
212 EXPORT_SYMBOL_GPL(round_jiffies);
215 * round_jiffies_relative - function to round jiffies to a full second
216 * @j: the time in (relative) jiffies that should be rounded
218 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
219 * up or down to (approximately) full seconds. This is useful for timers
220 * for which the exact time they fire does not matter too much, as long as
221 * they fire approximately every X seconds.
223 * By rounding these timers to whole seconds, all such timers will fire
224 * at the same time, rather than at various times spread out. The goal
225 * of this is to have the CPU wake up less, which saves power.
227 * The return value is the rounded version of the @j parameter.
229 unsigned long round_jiffies_relative(unsigned long j)
231 return __round_jiffies_relative(j, raw_smp_processor_id());
233 EXPORT_SYMBOL_GPL(round_jiffies_relative);
236 * __round_jiffies_up - function to round jiffies up to a full second
237 * @j: the time in (absolute) jiffies that should be rounded
238 * @cpu: the processor number on which the timeout will happen
240 * This is the same as __round_jiffies() except that it will never
241 * round down. This is useful for timeouts for which the exact time
242 * of firing does not matter too much, as long as they don't fire too
245 unsigned long __round_jiffies_up(unsigned long j, int cpu)
247 return round_jiffies_common(j, cpu, true);
249 EXPORT_SYMBOL_GPL(__round_jiffies_up);
252 * __round_jiffies_up_relative - function to round jiffies up to a full second
253 * @j: the time in (relative) jiffies that should be rounded
254 * @cpu: the processor number on which the timeout will happen
256 * This is the same as __round_jiffies_relative() except that it will never
257 * round down. This is useful for timeouts for which the exact time
258 * of firing does not matter too much, as long as they don't fire too
261 unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
263 unsigned long j0 = jiffies;
265 /* Use j0 because jiffies might change while we run */
266 return round_jiffies_common(j + j0, cpu, true) - j0;
268 EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
271 * round_jiffies_up - function to round jiffies up to a full second
272 * @j: the time in (absolute) jiffies that should be rounded
274 * This is the same as round_jiffies() except that it will never
275 * round down. This is useful for timeouts for which the exact time
276 * of firing does not matter too much, as long as they don't fire too
279 unsigned long round_jiffies_up(unsigned long j)
281 return round_jiffies_common(j, raw_smp_processor_id(), true);
283 EXPORT_SYMBOL_GPL(round_jiffies_up);
286 * round_jiffies_up_relative - function to round jiffies up to a full second
287 * @j: the time in (relative) jiffies that should be rounded
289 * This is the same as round_jiffies_relative() except that it will never
290 * round down. This is useful for timeouts for which the exact time
291 * of firing does not matter too much, as long as they don't fire too
294 unsigned long round_jiffies_up_relative(unsigned long j)
296 return __round_jiffies_up_relative(j, raw_smp_processor_id());
298 EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
301 * set_timer_slack - set the allowed slack for a timer
302 * @timer: the timer to be modified
303 * @slack_hz: the amount of time (in jiffies) allowed for rounding
305 * Set the amount of time, in jiffies, that a certain timer has
306 * in terms of slack. By setting this value, the timer subsystem
307 * will schedule the actual timer somewhere between
308 * the time mod_timer() asks for, and that time plus the slack.
310 * By setting the slack to -1, a percentage of the delay is used
313 void set_timer_slack(struct timer_list *timer, int slack_hz)
315 timer->slack = slack_hz;
317 EXPORT_SYMBOL_GPL(set_timer_slack);
320 __internal_add_timer(struct tvec_base *base, struct timer_list *timer)
322 unsigned long expires = timer->expires;
323 unsigned long idx = expires - base->timer_jiffies;
324 struct hlist_head *vec;
326 if (idx < TVR_SIZE) {
327 int i = expires & TVR_MASK;
328 vec = base->tv1.vec + i;
329 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
330 int i = (expires >> TVR_BITS) & TVN_MASK;
331 vec = base->tv2.vec + i;
332 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
333 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
334 vec = base->tv3.vec + i;
335 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
336 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
337 vec = base->tv4.vec + i;
338 } else if ((signed long) idx < 0) {
340 * Can happen if you add a timer with expires == jiffies,
341 * or you set a timer to go off in the past
343 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
346 /* If the timeout is larger than MAX_TVAL (on 64-bit
347 * architectures or with CONFIG_BASE_SMALL=1) then we
348 * use the maximum timeout.
350 if (idx > MAX_TVAL) {
352 expires = idx + base->timer_jiffies;
354 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
355 vec = base->tv5.vec + i;
358 hlist_add_head(&timer->entry, vec);
361 static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
363 /* Advance base->jiffies, if the base is empty */
364 if (!base->all_timers++)
365 base->timer_jiffies = jiffies;
367 __internal_add_timer(base, timer);
369 * Update base->active_timers and base->next_timer
371 if (!(timer->flags & TIMER_DEFERRABLE)) {
372 if (!base->active_timers++ ||
373 time_before(timer->expires, base->next_timer))
374 base->next_timer = timer->expires;
378 * Check whether the other CPU is in dynticks mode and needs
379 * to be triggered to reevaluate the timer wheel.
380 * We are protected against the other CPU fiddling
381 * with the timer by holding the timer base lock. This also
382 * makes sure that a CPU on the way to stop its tick can not
383 * evaluate the timer wheel.
385 * Spare the IPI for deferrable timers on idle targets though.
386 * The next busy ticks will take care of it. Except full dynticks
387 * require special care against races with idle_cpu(), lets deal
390 if (!(timer->flags & TIMER_DEFERRABLE) || tick_nohz_full_cpu(base->cpu))
391 wake_up_nohz_cpu(base->cpu);
394 #ifdef CONFIG_TIMER_STATS
395 void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
397 if (timer->start_site)
400 timer->start_site = addr;
401 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
402 timer->start_pid = current->pid;
405 static void timer_stats_account_timer(struct timer_list *timer)
407 if (likely(!timer->start_site))
410 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
411 timer->function, timer->start_comm,
416 static void timer_stats_account_timer(struct timer_list *timer) {}
419 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
421 static struct debug_obj_descr timer_debug_descr;
423 static void *timer_debug_hint(void *addr)
425 return ((struct timer_list *) addr)->function;
429 * fixup_init is called when:
430 * - an active object is initialized
432 static int timer_fixup_init(void *addr, enum debug_obj_state state)
434 struct timer_list *timer = addr;
437 case ODEBUG_STATE_ACTIVE:
438 del_timer_sync(timer);
439 debug_object_init(timer, &timer_debug_descr);
446 /* Stub timer callback for improperly used timers. */
447 static void stub_timer(unsigned long data)
453 * fixup_activate is called when:
454 * - an active object is activated
455 * - an unknown object is activated (might be a statically initialized object)
457 static int timer_fixup_activate(void *addr, enum debug_obj_state state)
459 struct timer_list *timer = addr;
463 case ODEBUG_STATE_NOTAVAILABLE:
465 * This is not really a fixup. The timer was
466 * statically initialized. We just make sure that it
467 * is tracked in the object tracker.
469 if (timer->entry.pprev == NULL &&
470 timer->entry.next == TIMER_ENTRY_STATIC) {
471 debug_object_init(timer, &timer_debug_descr);
472 debug_object_activate(timer, &timer_debug_descr);
475 setup_timer(timer, stub_timer, 0);
480 case ODEBUG_STATE_ACTIVE:
489 * fixup_free is called when:
490 * - an active object is freed
492 static int timer_fixup_free(void *addr, enum debug_obj_state state)
494 struct timer_list *timer = addr;
497 case ODEBUG_STATE_ACTIVE:
498 del_timer_sync(timer);
499 debug_object_free(timer, &timer_debug_descr);
507 * fixup_assert_init is called when:
508 * - an untracked/uninit-ed object is found
510 static int timer_fixup_assert_init(void *addr, enum debug_obj_state state)
512 struct timer_list *timer = addr;
515 case ODEBUG_STATE_NOTAVAILABLE:
516 if (timer->entry.next == TIMER_ENTRY_STATIC) {
518 * This is not really a fixup. The timer was
519 * statically initialized. We just make sure that it
520 * is tracked in the object tracker.
522 debug_object_init(timer, &timer_debug_descr);
525 setup_timer(timer, stub_timer, 0);
533 static struct debug_obj_descr timer_debug_descr = {
534 .name = "timer_list",
535 .debug_hint = timer_debug_hint,
536 .fixup_init = timer_fixup_init,
537 .fixup_activate = timer_fixup_activate,
538 .fixup_free = timer_fixup_free,
539 .fixup_assert_init = timer_fixup_assert_init,
542 static inline void debug_timer_init(struct timer_list *timer)
544 debug_object_init(timer, &timer_debug_descr);
547 static inline void debug_timer_activate(struct timer_list *timer)
549 debug_object_activate(timer, &timer_debug_descr);
552 static inline void debug_timer_deactivate(struct timer_list *timer)
554 debug_object_deactivate(timer, &timer_debug_descr);
557 static inline void debug_timer_free(struct timer_list *timer)
559 debug_object_free(timer, &timer_debug_descr);
562 static inline void debug_timer_assert_init(struct timer_list *timer)
564 debug_object_assert_init(timer, &timer_debug_descr);
567 static void do_init_timer(struct timer_list *timer, unsigned int flags,
568 const char *name, struct lock_class_key *key);
570 void init_timer_on_stack_key(struct timer_list *timer, unsigned int flags,
571 const char *name, struct lock_class_key *key)
573 debug_object_init_on_stack(timer, &timer_debug_descr);
574 do_init_timer(timer, flags, name, key);
576 EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
578 void destroy_timer_on_stack(struct timer_list *timer)
580 debug_object_free(timer, &timer_debug_descr);
582 EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
585 static inline void debug_timer_init(struct timer_list *timer) { }
586 static inline void debug_timer_activate(struct timer_list *timer) { }
587 static inline void debug_timer_deactivate(struct timer_list *timer) { }
588 static inline void debug_timer_assert_init(struct timer_list *timer) { }
591 static inline void debug_init(struct timer_list *timer)
593 debug_timer_init(timer);
594 trace_timer_init(timer);
598 debug_activate(struct timer_list *timer, unsigned long expires)
600 debug_timer_activate(timer);
601 trace_timer_start(timer, expires, timer->flags);
604 static inline void debug_deactivate(struct timer_list *timer)
606 debug_timer_deactivate(timer);
607 trace_timer_cancel(timer);
610 static inline void debug_assert_init(struct timer_list *timer)
612 debug_timer_assert_init(timer);
615 static void do_init_timer(struct timer_list *timer, unsigned int flags,
616 const char *name, struct lock_class_key *key)
618 timer->entry.pprev = NULL;
619 timer->flags = flags | raw_smp_processor_id();
621 #ifdef CONFIG_TIMER_STATS
622 timer->start_site = NULL;
623 timer->start_pid = -1;
624 memset(timer->start_comm, 0, TASK_COMM_LEN);
626 lockdep_init_map(&timer->lockdep_map, name, key, 0);
630 * init_timer_key - initialize a timer
631 * @timer: the timer to be initialized
632 * @flags: timer flags
633 * @name: name of the timer
634 * @key: lockdep class key of the fake lock used for tracking timer
635 * sync lock dependencies
637 * init_timer_key() must be done to a timer prior calling *any* of the
638 * other timer functions.
640 void init_timer_key(struct timer_list *timer, unsigned int flags,
641 const char *name, struct lock_class_key *key)
644 do_init_timer(timer, flags, name, key);
646 EXPORT_SYMBOL(init_timer_key);
648 static inline void detach_timer(struct timer_list *timer, bool clear_pending)
650 struct hlist_node *entry = &timer->entry;
652 debug_deactivate(timer);
657 entry->next = LIST_POISON2;
661 detach_expired_timer(struct timer_list *timer, struct tvec_base *base)
663 detach_timer(timer, true);
664 if (!(timer->flags & TIMER_DEFERRABLE))
665 base->active_timers--;
669 static int detach_if_pending(struct timer_list *timer, struct tvec_base *base,
672 if (!timer_pending(timer))
675 detach_timer(timer, clear_pending);
676 if (!(timer->flags & TIMER_DEFERRABLE)) {
677 base->active_timers--;
678 if (timer->expires == base->next_timer)
679 base->next_timer = base->timer_jiffies;
681 /* If this was the last timer, advance base->jiffies */
682 if (!--base->all_timers)
683 base->timer_jiffies = jiffies;
688 * We are using hashed locking: holding per_cpu(tvec_bases).lock
689 * means that all timers which are tied to this base via timer->base are
690 * locked, and the base itself is locked too.
692 * So __run_timers/migrate_timers can safely modify all timers which could
693 * be found on ->tvX lists.
695 * When the timer's base is locked and removed from the list, the
696 * TIMER_MIGRATING flag is set, FIXME
698 static struct tvec_base *lock_timer_base(struct timer_list *timer,
699 unsigned long *flags)
700 __acquires(timer->base->lock)
703 u32 tf = timer->flags;
704 struct tvec_base *base;
706 if (!(tf & TIMER_MIGRATING)) {
707 base = per_cpu_ptr(&tvec_bases, tf & TIMER_CPUMASK);
708 spin_lock_irqsave(&base->lock, *flags);
709 if (timer->flags == tf)
711 spin_unlock_irqrestore(&base->lock, *flags);
718 __mod_timer(struct timer_list *timer, unsigned long expires,
719 bool pending_only, int pinned)
721 struct tvec_base *base, *new_base;
725 timer_stats_timer_set_start_info(timer);
726 BUG_ON(!timer->function);
728 base = lock_timer_base(timer, &flags);
730 ret = detach_if_pending(timer, base, false);
731 if (!ret && pending_only)
734 debug_activate(timer, expires);
736 cpu = get_nohz_timer_target(pinned);
737 new_base = per_cpu_ptr(&tvec_bases, cpu);
739 if (base != new_base) {
741 * We are trying to schedule the timer on the local CPU.
742 * However we can't change timer's base while it is running,
743 * otherwise del_timer_sync() can't detect that the timer's
744 * handler yet has not finished. This also guarantees that
745 * the timer is serialized wrt itself.
747 if (likely(base->running_timer != timer)) {
748 /* See the comment in lock_timer_base() */
749 timer->flags |= TIMER_MIGRATING;
751 spin_unlock(&base->lock);
753 spin_lock(&base->lock);
754 timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu;
758 timer->expires = expires;
759 internal_add_timer(base, timer);
762 spin_unlock_irqrestore(&base->lock, flags);
768 * mod_timer_pending - modify a pending timer's timeout
769 * @timer: the pending timer to be modified
770 * @expires: new timeout in jiffies
772 * mod_timer_pending() is the same for pending timers as mod_timer(),
773 * but will not re-activate and modify already deleted timers.
775 * It is useful for unserialized use of timers.
777 int mod_timer_pending(struct timer_list *timer, unsigned long expires)
779 return __mod_timer(timer, expires, true, TIMER_NOT_PINNED);
781 EXPORT_SYMBOL(mod_timer_pending);
784 * Decide where to put the timer while taking the slack into account
787 * 1) calculate the maximum (absolute) time
788 * 2) calculate the highest bit where the expires and new max are different
789 * 3) use this bit to make a mask
790 * 4) use the bitmask to round down the maximum time, so that all last
794 unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
796 unsigned long expires_limit, mask;
799 if (timer->slack >= 0) {
800 expires_limit = expires + timer->slack;
802 long delta = expires - jiffies;
807 expires_limit = expires + delta / 256;
809 mask = expires ^ expires_limit;
813 bit = find_last_bit(&mask, BITS_PER_LONG);
815 mask = (1UL << bit) - 1;
817 expires_limit = expires_limit & ~(mask);
819 return expires_limit;
823 * mod_timer - modify a timer's timeout
824 * @timer: the timer to be modified
825 * @expires: new timeout in jiffies
827 * mod_timer() is a more efficient way to update the expire field of an
828 * active timer (if the timer is inactive it will be activated)
830 * mod_timer(timer, expires) is equivalent to:
832 * del_timer(timer); timer->expires = expires; add_timer(timer);
834 * Note that if there are multiple unserialized concurrent users of the
835 * same timer, then mod_timer() is the only safe way to modify the timeout,
836 * since add_timer() cannot modify an already running timer.
838 * The function returns whether it has modified a pending timer or not.
839 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
840 * active timer returns 1.)
842 int mod_timer(struct timer_list *timer, unsigned long expires)
844 expires = apply_slack(timer, expires);
847 * This is a common optimization triggered by the
848 * networking code - if the timer is re-modified
849 * to be the same thing then just return:
851 if (timer_pending(timer) && timer->expires == expires)
854 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
856 EXPORT_SYMBOL(mod_timer);
859 * mod_timer_pinned - modify a timer's timeout
860 * @timer: the timer to be modified
861 * @expires: new timeout in jiffies
863 * mod_timer_pinned() is a way to update the expire field of an
864 * active timer (if the timer is inactive it will be activated)
865 * and to ensure that the timer is scheduled on the current CPU.
867 * Note that this does not prevent the timer from being migrated
868 * when the current CPU goes offline. If this is a problem for
869 * you, use CPU-hotplug notifiers to handle it correctly, for
870 * example, cancelling the timer when the corresponding CPU goes
873 * mod_timer_pinned(timer, expires) is equivalent to:
875 * del_timer(timer); timer->expires = expires; add_timer(timer);
877 int mod_timer_pinned(struct timer_list *timer, unsigned long expires)
879 if (timer->expires == expires && timer_pending(timer))
882 return __mod_timer(timer, expires, false, TIMER_PINNED);
884 EXPORT_SYMBOL(mod_timer_pinned);
887 * add_timer - start a timer
888 * @timer: the timer to be added
890 * The kernel will do a ->function(->data) callback from the
891 * timer interrupt at the ->expires point in the future. The
892 * current time is 'jiffies'.
894 * The timer's ->expires, ->function (and if the handler uses it, ->data)
895 * fields must be set prior calling this function.
897 * Timers with an ->expires field in the past will be executed in the next
900 void add_timer(struct timer_list *timer)
902 BUG_ON(timer_pending(timer));
903 mod_timer(timer, timer->expires);
905 EXPORT_SYMBOL(add_timer);
908 * add_timer_on - start a timer on a particular CPU
909 * @timer: the timer to be added
910 * @cpu: the CPU to start it on
912 * This is not very scalable on SMP. Double adds are not possible.
914 void add_timer_on(struct timer_list *timer, int cpu)
916 struct tvec_base *base = per_cpu_ptr(&tvec_bases, cpu);
919 timer_stats_timer_set_start_info(timer);
920 BUG_ON(timer_pending(timer) || !timer->function);
921 spin_lock_irqsave(&base->lock, flags);
922 timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu;
923 debug_activate(timer, timer->expires);
924 internal_add_timer(base, timer);
925 spin_unlock_irqrestore(&base->lock, flags);
927 EXPORT_SYMBOL_GPL(add_timer_on);
930 * del_timer - deactive a timer.
931 * @timer: the timer to be deactivated
933 * del_timer() deactivates a timer - this works on both active and inactive
936 * The function returns whether it has deactivated a pending timer or not.
937 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
938 * active timer returns 1.)
940 int del_timer(struct timer_list *timer)
942 struct tvec_base *base;
946 debug_assert_init(timer);
948 timer_stats_timer_clear_start_info(timer);
949 if (timer_pending(timer)) {
950 base = lock_timer_base(timer, &flags);
951 ret = detach_if_pending(timer, base, true);
952 spin_unlock_irqrestore(&base->lock, flags);
957 EXPORT_SYMBOL(del_timer);
960 * try_to_del_timer_sync - Try to deactivate a timer
961 * @timer: timer do del
963 * This function tries to deactivate a timer. Upon successful (ret >= 0)
964 * exit the timer is not queued and the handler is not running on any CPU.
966 int try_to_del_timer_sync(struct timer_list *timer)
968 struct tvec_base *base;
972 debug_assert_init(timer);
974 base = lock_timer_base(timer, &flags);
976 if (base->running_timer != timer) {
977 timer_stats_timer_clear_start_info(timer);
978 ret = detach_if_pending(timer, base, true);
980 spin_unlock_irqrestore(&base->lock, flags);
984 EXPORT_SYMBOL(try_to_del_timer_sync);
988 * del_timer_sync - deactivate a timer and wait for the handler to finish.
989 * @timer: the timer to be deactivated
991 * This function only differs from del_timer() on SMP: besides deactivating
992 * the timer it also makes sure the handler has finished executing on other
995 * Synchronization rules: Callers must prevent restarting of the timer,
996 * otherwise this function is meaningless. It must not be called from
997 * interrupt contexts unless the timer is an irqsafe one. The caller must
998 * not hold locks which would prevent completion of the timer's
999 * handler. The timer's handler must not call add_timer_on(). Upon exit the
1000 * timer is not queued and the handler is not running on any CPU.
1002 * Note: For !irqsafe timers, you must not hold locks that are held in
1003 * interrupt context while calling this function. Even if the lock has
1004 * nothing to do with the timer in question. Here's why:
1010 * base->running_timer = mytimer;
1011 * spin_lock_irq(somelock);
1013 * spin_lock(somelock);
1014 * del_timer_sync(mytimer);
1015 * while (base->running_timer == mytimer);
1017 * Now del_timer_sync() will never return and never release somelock.
1018 * The interrupt on the other CPU is waiting to grab somelock but
1019 * it has interrupted the softirq that CPU0 is waiting to finish.
1021 * The function returns whether it has deactivated a pending timer or not.
1023 int del_timer_sync(struct timer_list *timer)
1025 #ifdef CONFIG_LOCKDEP
1026 unsigned long flags;
1029 * If lockdep gives a backtrace here, please reference
1030 * the synchronization rules above.
1032 local_irq_save(flags);
1033 lock_map_acquire(&timer->lockdep_map);
1034 lock_map_release(&timer->lockdep_map);
1035 local_irq_restore(flags);
1038 * don't use it in hardirq context, because it
1039 * could lead to deadlock.
1041 WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE));
1043 int ret = try_to_del_timer_sync(timer);
1049 EXPORT_SYMBOL(del_timer_sync);
1052 static int cascade(struct tvec_base *base, struct tvec *tv, int index)
1054 /* cascade all the timers from tv up one level */
1055 struct timer_list *timer;
1056 struct hlist_node *tmp;
1057 struct hlist_head tv_list;
1059 hlist_move_list(tv->vec + index, &tv_list);
1062 * We are removing _all_ timers from the list, so we
1063 * don't have to detach them individually.
1065 hlist_for_each_entry_safe(timer, tmp, &tv_list, entry) {
1066 /* No accounting, while moving them */
1067 __internal_add_timer(base, timer);
1073 static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
1076 int count = preempt_count();
1078 #ifdef CONFIG_LOCKDEP
1080 * It is permissible to free the timer from inside the
1081 * function that is called from it, this we need to take into
1082 * account for lockdep too. To avoid bogus "held lock freed"
1083 * warnings as well as problems when looking into
1084 * timer->lockdep_map, make a copy and use that here.
1086 struct lockdep_map lockdep_map;
1088 lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
1091 * Couple the lock chain with the lock chain at
1092 * del_timer_sync() by acquiring the lock_map around the fn()
1093 * call here and in del_timer_sync().
1095 lock_map_acquire(&lockdep_map);
1097 trace_timer_expire_entry(timer);
1099 trace_timer_expire_exit(timer);
1101 lock_map_release(&lockdep_map);
1103 if (count != preempt_count()) {
1104 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
1105 fn, count, preempt_count());
1107 * Restore the preempt count. That gives us a decent
1108 * chance to survive and extract information. If the
1109 * callback kept a lock held, bad luck, but not worse
1110 * than the BUG() we had.
1112 preempt_count_set(count);
1116 #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
1119 * __run_timers - run all expired timers (if any) on this CPU.
1120 * @base: the timer vector to be processed.
1122 * This function cascades all vectors and executes all expired timer
1125 static inline void __run_timers(struct tvec_base *base)
1127 struct timer_list *timer;
1129 spin_lock_irq(&base->lock);
1131 while (time_after_eq(jiffies, base->timer_jiffies)) {
1132 struct hlist_head work_list;
1133 struct hlist_head *head = &work_list;
1136 if (!base->all_timers) {
1137 base->timer_jiffies = jiffies;
1141 index = base->timer_jiffies & TVR_MASK;
1147 (!cascade(base, &base->tv2, INDEX(0))) &&
1148 (!cascade(base, &base->tv3, INDEX(1))) &&
1149 !cascade(base, &base->tv4, INDEX(2)))
1150 cascade(base, &base->tv5, INDEX(3));
1151 ++base->timer_jiffies;
1152 hlist_move_list(base->tv1.vec + index, head);
1153 while (!hlist_empty(head)) {
1154 void (*fn)(unsigned long);
1158 timer = hlist_entry(head->first, struct timer_list, entry);
1159 fn = timer->function;
1161 irqsafe = timer->flags & TIMER_IRQSAFE;
1163 timer_stats_account_timer(timer);
1165 base->running_timer = timer;
1166 detach_expired_timer(timer, base);
1169 spin_unlock(&base->lock);
1170 call_timer_fn(timer, fn, data);
1171 spin_lock(&base->lock);
1173 spin_unlock_irq(&base->lock);
1174 call_timer_fn(timer, fn, data);
1175 spin_lock_irq(&base->lock);
1179 base->running_timer = NULL;
1180 spin_unlock_irq(&base->lock);
1183 #ifdef CONFIG_NO_HZ_COMMON
1185 * Find out when the next timer event is due to happen. This
1186 * is used on S/390 to stop all activity when a CPU is idle.
1187 * This function needs to be called with interrupts disabled.
1189 static unsigned long __next_timer_interrupt(struct tvec_base *base)
1191 unsigned long timer_jiffies = base->timer_jiffies;
1192 unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
1193 int index, slot, array, found = 0;
1194 struct timer_list *nte;
1195 struct tvec *varray[4];
1197 /* Look for timer events in tv1. */
1198 index = slot = timer_jiffies & TVR_MASK;
1200 hlist_for_each_entry(nte, base->tv1.vec + slot, entry) {
1201 if (nte->flags & TIMER_DEFERRABLE)
1205 expires = nte->expires;
1206 /* Look at the cascade bucket(s)? */
1207 if (!index || slot < index)
1211 slot = (slot + 1) & TVR_MASK;
1212 } while (slot != index);
1215 /* Calculate the next cascade event */
1217 timer_jiffies += TVR_SIZE - index;
1218 timer_jiffies >>= TVR_BITS;
1220 /* Check tv2-tv5. */
1221 varray[0] = &base->tv2;
1222 varray[1] = &base->tv3;
1223 varray[2] = &base->tv4;
1224 varray[3] = &base->tv5;
1226 for (array = 0; array < 4; array++) {
1227 struct tvec *varp = varray[array];
1229 index = slot = timer_jiffies & TVN_MASK;
1231 hlist_for_each_entry(nte, varp->vec + slot, entry) {
1232 if (nte->flags & TIMER_DEFERRABLE)
1236 if (time_before(nte->expires, expires))
1237 expires = nte->expires;
1240 * Do we still search for the first timer or are
1241 * we looking up the cascade buckets ?
1244 /* Look at the cascade bucket(s)? */
1245 if (!index || slot < index)
1249 slot = (slot + 1) & TVN_MASK;
1250 } while (slot != index);
1253 timer_jiffies += TVN_SIZE - index;
1254 timer_jiffies >>= TVN_BITS;
1260 * Check, if the next hrtimer event is before the next timer wheel
1263 static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
1265 u64 nextevt = hrtimer_get_next_event();
1268 * If high resolution timers are enabled
1269 * hrtimer_get_next_event() returns KTIME_MAX.
1271 if (expires <= nextevt)
1275 * If the next timer is already expired, return the tick base
1276 * time so the tick is fired immediately.
1278 if (nextevt <= basem)
1282 * Round up to the next jiffie. High resolution timers are
1283 * off, so the hrtimers are expired in the tick and we need to
1284 * make sure that this tick really expires the timer to avoid
1285 * a ping pong of the nohz stop code.
1287 * Use DIV_ROUND_UP_ULL to prevent gcc calling __divdi3
1289 return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC;
1293 * get_next_timer_interrupt - return the time (clock mono) of the next timer
1294 * @basej: base time jiffies
1295 * @basem: base time clock monotonic
1297 * Returns the tick aligned clock monotonic time of the next pending
1298 * timer or KTIME_MAX if no timer is pending.
1300 u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
1302 struct tvec_base *base = this_cpu_ptr(&tvec_bases);
1303 u64 expires = KTIME_MAX;
1304 unsigned long nextevt;
1307 * Pretend that there is no timer pending if the cpu is offline.
1308 * Possible pending timers will be migrated later to an active cpu.
1310 if (cpu_is_offline(smp_processor_id()))
1313 spin_lock(&base->lock);
1314 if (base->active_timers) {
1315 if (time_before_eq(base->next_timer, base->timer_jiffies))
1316 base->next_timer = __next_timer_interrupt(base);
1317 nextevt = base->next_timer;
1318 if (time_before_eq(nextevt, basej))
1321 expires = basem + (nextevt - basej) * TICK_NSEC;
1323 spin_unlock(&base->lock);
1325 return cmp_next_hrtimer_event(basem, expires);
1330 * Called from the timer interrupt handler to charge one tick to the current
1331 * process. user_tick is 1 if the tick is user time, 0 for system.
1333 void update_process_times(int user_tick)
1335 struct task_struct *p = current;
1337 /* Note: this timer irq context must be accounted for as well. */
1338 account_process_tick(p, user_tick);
1340 rcu_check_callbacks(user_tick);
1341 #ifdef CONFIG_IRQ_WORK
1346 run_posix_cpu_timers(p);
1350 * This function runs timers and the timer-tq in bottom half context.
1352 static void run_timer_softirq(struct softirq_action *h)
1354 struct tvec_base *base = this_cpu_ptr(&tvec_bases);
1356 if (time_after_eq(jiffies, base->timer_jiffies))
1361 * Called by the local, per-CPU timer interrupt on SMP.
1363 void run_local_timers(void)
1365 hrtimer_run_queues();
1366 raise_softirq(TIMER_SOFTIRQ);
1369 #ifdef __ARCH_WANT_SYS_ALARM
1372 * For backwards compatibility? This can be done in libc so Alpha
1373 * and all newer ports shouldn't need it.
1375 SYSCALL_DEFINE1(alarm, unsigned int, seconds)
1377 return alarm_setitimer(seconds);
1382 static void process_timeout(unsigned long __data)
1384 wake_up_process((struct task_struct *)__data);
1388 * schedule_timeout - sleep until timeout
1389 * @timeout: timeout value in jiffies
1391 * Make the current task sleep until @timeout jiffies have
1392 * elapsed. The routine will return immediately unless
1393 * the current task state has been set (see set_current_state()).
1395 * You can set the task state as follows -
1397 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1398 * pass before the routine returns. The routine will return 0
1400 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1401 * delivered to the current task. In this case the remaining time
1402 * in jiffies will be returned, or 0 if the timer expired in time
1404 * The current task state is guaranteed to be TASK_RUNNING when this
1407 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1408 * the CPU away without a bound on the timeout. In this case the return
1409 * value will be %MAX_SCHEDULE_TIMEOUT.
1411 * In all cases the return value is guaranteed to be non-negative.
1413 signed long __sched schedule_timeout(signed long timeout)
1415 struct timer_list timer;
1416 unsigned long expire;
1420 case MAX_SCHEDULE_TIMEOUT:
1422 * These two special cases are useful to be comfortable
1423 * in the caller. Nothing more. We could take
1424 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1425 * but I' d like to return a valid offset (>=0) to allow
1426 * the caller to do everything it want with the retval.
1432 * Another bit of PARANOID. Note that the retval will be
1433 * 0 since no piece of kernel is supposed to do a check
1434 * for a negative retval of schedule_timeout() (since it
1435 * should never happens anyway). You just have the printk()
1436 * that will tell you if something is gone wrong and where.
1439 printk(KERN_ERR "schedule_timeout: wrong timeout "
1440 "value %lx\n", timeout);
1442 current->state = TASK_RUNNING;
1447 expire = timeout + jiffies;
1449 setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
1450 __mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
1452 del_singleshot_timer_sync(&timer);
1454 /* Remove the timer from the object tracker */
1455 destroy_timer_on_stack(&timer);
1457 timeout = expire - jiffies;
1460 return timeout < 0 ? 0 : timeout;
1462 EXPORT_SYMBOL(schedule_timeout);
1465 * We can use __set_current_state() here because schedule_timeout() calls
1466 * schedule() unconditionally.
1468 signed long __sched schedule_timeout_interruptible(signed long timeout)
1470 __set_current_state(TASK_INTERRUPTIBLE);
1471 return schedule_timeout(timeout);
1473 EXPORT_SYMBOL(schedule_timeout_interruptible);
1475 signed long __sched schedule_timeout_killable(signed long timeout)
1477 __set_current_state(TASK_KILLABLE);
1478 return schedule_timeout(timeout);
1480 EXPORT_SYMBOL(schedule_timeout_killable);
1482 signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1484 __set_current_state(TASK_UNINTERRUPTIBLE);
1485 return schedule_timeout(timeout);
1487 EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1489 #ifdef CONFIG_HOTPLUG_CPU
1490 static void migrate_timer_list(struct tvec_base *new_base, struct hlist_head *head)
1492 struct timer_list *timer;
1493 int cpu = new_base->cpu;
1495 while (!hlist_empty(head)) {
1496 timer = hlist_entry(head->first, struct timer_list, entry);
1497 /* We ignore the accounting on the dying cpu */
1498 detach_timer(timer, false);
1499 timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu;
1500 internal_add_timer(new_base, timer);
1504 static void migrate_timers(int cpu)
1506 struct tvec_base *old_base;
1507 struct tvec_base *new_base;
1510 BUG_ON(cpu_online(cpu));
1511 old_base = per_cpu_ptr(&tvec_bases, cpu);
1512 new_base = this_cpu_ptr(&tvec_bases);
1514 * The caller is globally serialized and nobody else
1515 * takes two locks at once, deadlock is not possible.
1517 spin_lock_irq(&new_base->lock);
1518 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1520 BUG_ON(old_base->running_timer);
1522 for (i = 0; i < TVR_SIZE; i++)
1523 migrate_timer_list(new_base, old_base->tv1.vec + i);
1524 for (i = 0; i < TVN_SIZE; i++) {
1525 migrate_timer_list(new_base, old_base->tv2.vec + i);
1526 migrate_timer_list(new_base, old_base->tv3.vec + i);
1527 migrate_timer_list(new_base, old_base->tv4.vec + i);
1528 migrate_timer_list(new_base, old_base->tv5.vec + i);
1531 old_base->active_timers = 0;
1532 old_base->all_timers = 0;
1534 spin_unlock(&old_base->lock);
1535 spin_unlock_irq(&new_base->lock);
1538 static int timer_cpu_notify(struct notifier_block *self,
1539 unsigned long action, void *hcpu)
1543 case CPU_DEAD_FROZEN:
1544 migrate_timers((long)hcpu);
1553 static inline void timer_register_cpu_notifier(void)
1555 cpu_notifier(timer_cpu_notify, 0);
1558 static inline void timer_register_cpu_notifier(void) { }
1559 #endif /* CONFIG_HOTPLUG_CPU */
1561 static void __init init_timer_cpu(int cpu)
1563 struct tvec_base *base = per_cpu_ptr(&tvec_bases, cpu);
1566 spin_lock_init(&base->lock);
1568 base->timer_jiffies = jiffies;
1569 base->next_timer = base->timer_jiffies;
1572 static void __init init_timer_cpus(void)
1576 for_each_possible_cpu(cpu)
1577 init_timer_cpu(cpu);
1580 void __init init_timers(void)
1584 timer_register_cpu_notifier();
1585 open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
1589 * msleep - sleep safely even with waitqueue interruptions
1590 * @msecs: Time in milliseconds to sleep for
1592 void msleep(unsigned int msecs)
1594 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1597 timeout = schedule_timeout_uninterruptible(timeout);
1600 EXPORT_SYMBOL(msleep);
1603 * msleep_interruptible - sleep waiting for signals
1604 * @msecs: Time in milliseconds to sleep for
1606 unsigned long msleep_interruptible(unsigned int msecs)
1608 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1610 while (timeout && !signal_pending(current))
1611 timeout = schedule_timeout_interruptible(timeout);
1612 return jiffies_to_msecs(timeout);
1615 EXPORT_SYMBOL(msleep_interruptible);
1617 static void __sched do_usleep_range(unsigned long min, unsigned long max)
1620 unsigned long delta;
1622 kmin = ktime_set(0, min * NSEC_PER_USEC);
1623 delta = (max - min) * NSEC_PER_USEC;
1624 schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL);
1628 * usleep_range - Drop in replacement for udelay where wakeup is flexible
1629 * @min: Minimum time in usecs to sleep
1630 * @max: Maximum time in usecs to sleep
1632 void __sched usleep_range(unsigned long min, unsigned long max)
1634 __set_current_state(TASK_UNINTERRUPTIBLE);
1635 do_usleep_range(min, max);
1637 EXPORT_SYMBOL(usleep_range);