Lines Matching full:timer

16  *  2000-10-05  Implemented scalable SMP per-CPU timer handling.
57 #include <trace/events/timer.h>
64 * The timer wheel has LVL_DEPTH array levels. Each level provides an array of
71 * The array level of a newly armed timer depends on the relative expiry
75 * Contrary to the original timer wheel implementation, which aims for 'exact'
77 * the timers into the lower array levels. The previous 'classic' timer wheel
82 * This is an optimization of the original timer wheel implementation for the
83 * majority of the timer wheel use cases: timeouts. The vast majority of
294 * due to delays of the timer irq, long irq off times etc etc) then in round_jiffies_common()
477 static inline unsigned int timer_get_idx(struct timer_list *timer) in timer_get_idx() argument
479 return (timer->flags & TIMER_ARRAYMASK) >> TIMER_ARRAYSHIFT; in timer_get_idx()
482 static inline void timer_set_idx(struct timer_list *timer, unsigned int idx) in timer_set_idx() argument
484 timer->flags = (timer->flags & ~TIMER_ARRAYMASK) | in timer_set_idx()
497 * The timer wheel has to guarantee that a timer does not fire in calc_index()
499 * - Timer is armed at the edge of a tick in calc_index()
548 trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer) in trigger_dyntick_cpu() argument
557 if (timer->flags & TIMER_DEFERRABLE) { in trigger_dyntick_cpu()
565 * timer is not deferrable. If the other CPU is on the way to idle in trigger_dyntick_cpu()
573 * Enqueue the timer into the hash bucket, mark it pending in
574 * the bitmap, store the index in the timer flags then wake up
577 static void enqueue_timer(struct timer_base *base, struct timer_list *timer, in enqueue_timer() argument
581 hlist_add_head(&timer->entry, base->vectors + idx); in enqueue_timer()
583 timer_set_idx(timer, idx); in enqueue_timer()
585 trace_timer_start(timer, timer->expires, timer->flags); in enqueue_timer()
588 * Check whether this is the new first expiring timer. The in enqueue_timer()
589 * effective expiry time of the timer is required here in enqueue_timer()
590 * (bucket_expiry) instead of timer->expires. in enqueue_timer()
599 trigger_dyntick_cpu(base, timer); in enqueue_timer()
603 static void internal_add_timer(struct timer_base *base, struct timer_list *timer) in internal_add_timer() argument
608 idx = calc_wheel_index(timer->expires, base->clk, &bucket_expiry); in internal_add_timer()
609 enqueue_timer(base, timer, idx, bucket_expiry); in internal_add_timer()
623 struct timer_list *timer = addr; in timer_is_static_object() local
625 return (timer->entry.pprev == NULL && in timer_is_static_object()
626 timer->entry.next == TIMER_ENTRY_STATIC); in timer_is_static_object()
635 struct timer_list *timer = addr; in timer_fixup_init() local
639 del_timer_sync(timer); in timer_fixup_init()
640 debug_object_init(timer, &timer_debug_descr); in timer_fixup_init()
647 /* Stub timer callback for improperly used timers. */
660 struct timer_list *timer = addr; in timer_fixup_activate() local
664 timer_setup(timer, stub_timer, 0); in timer_fixup_activate()
681 struct timer_list *timer = addr; in timer_fixup_free() local
685 del_timer_sync(timer); in timer_fixup_free()
686 debug_object_free(timer, &timer_debug_descr); in timer_fixup_free()
699 struct timer_list *timer = addr; in timer_fixup_assert_init() local
703 timer_setup(timer, stub_timer, 0); in timer_fixup_assert_init()
720 static inline void debug_timer_init(struct timer_list *timer) in debug_timer_init() argument
722 debug_object_init(timer, &timer_debug_descr); in debug_timer_init()
725 static inline void debug_timer_activate(struct timer_list *timer) in debug_timer_activate() argument
727 debug_object_activate(timer, &timer_debug_descr); in debug_timer_activate()
730 static inline void debug_timer_deactivate(struct timer_list *timer) in debug_timer_deactivate() argument
732 debug_object_deactivate(timer, &timer_debug_descr); in debug_timer_deactivate()
735 static inline void debug_timer_assert_init(struct timer_list *timer) in debug_timer_assert_init() argument
737 debug_object_assert_init(timer, &timer_debug_descr); in debug_timer_assert_init()
740 static void do_init_timer(struct timer_list *timer,
745 void init_timer_on_stack_key(struct timer_list *timer, in init_timer_on_stack_key() argument
750 debug_object_init_on_stack(timer, &timer_debug_descr); in init_timer_on_stack_key()
751 do_init_timer(timer, func, flags, name, key); in init_timer_on_stack_key()
755 void destroy_timer_on_stack(struct timer_list *timer) in destroy_timer_on_stack() argument
757 debug_object_free(timer, &timer_debug_descr); in destroy_timer_on_stack()
762 static inline void debug_timer_init(struct timer_list *timer) { } in debug_timer_init() argument
763 static inline void debug_timer_activate(struct timer_list *timer) { } in debug_timer_activate() argument
764 static inline void debug_timer_deactivate(struct timer_list *timer) { } in debug_timer_deactivate() argument
765 static inline void debug_timer_assert_init(struct timer_list *timer) { } in debug_timer_assert_init() argument
768 static inline void debug_init(struct timer_list *timer) in debug_init() argument
770 debug_timer_init(timer); in debug_init()
771 trace_timer_init(timer); in debug_init()
774 static inline void debug_deactivate(struct timer_list *timer) in debug_deactivate() argument
776 debug_timer_deactivate(timer); in debug_deactivate()
777 trace_timer_cancel(timer); in debug_deactivate()
780 static inline void debug_assert_init(struct timer_list *timer) in debug_assert_init() argument
782 debug_timer_assert_init(timer); in debug_assert_init()
785 static void do_init_timer(struct timer_list *timer, in do_init_timer() argument
790 timer->entry.pprev = NULL; in do_init_timer()
791 timer->function = func; in do_init_timer()
794 timer->flags = flags | raw_smp_processor_id(); in do_init_timer()
795 lockdep_init_map(&timer->lockdep_map, name, key, 0); in do_init_timer()
799 * init_timer_key - initialize a timer
800 * @timer: the timer to be initialized
801 * @func: timer callback function
802 * @flags: timer flags
803 * @name: name of the timer
804 * @key: lockdep class key of the fake lock used for tracking timer
807 * init_timer_key() must be done to a timer prior calling *any* of the
808 * other timer functions.
810 void init_timer_key(struct timer_list *timer, in init_timer_key() argument
814 debug_init(timer); in init_timer_key()
815 do_init_timer(timer, func, flags, name, key); in init_timer_key()
819 static inline void detach_timer(struct timer_list *timer, bool clear_pending) in detach_timer() argument
821 struct hlist_node *entry = &timer->entry; in detach_timer()
823 debug_deactivate(timer); in detach_timer()
831 static int detach_if_pending(struct timer_list *timer, struct timer_base *base, in detach_if_pending() argument
834 unsigned idx = timer_get_idx(timer); in detach_if_pending()
836 if (!timer_pending(timer)) in detach_if_pending()
839 if (hlist_is_singular_node(&timer->entry, base->vectors + idx)) { in detach_if_pending()
844 detach_timer(timer, clear_pending); in detach_if_pending()
853 * If the timer is deferrable and NO_HZ_COMMON is set then we need in get_timer_cpu_base()
866 * If the timer is deferrable and NO_HZ_COMMON is set then we need in get_timer_this_cpu_base()
924 * When a timer is migrating then the TIMER_MIGRATING flag is set and we need
927 static struct timer_base *lock_timer_base(struct timer_list *timer, in lock_timer_base() argument
929 __acquires(timer->base->lock) in lock_timer_base()
940 tf = READ_ONCE(timer->flags); in lock_timer_base()
945 if (timer->flags == tf) in lock_timer_base()
958 __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int options) in __mod_timer() argument
965 BUG_ON(!timer->function); in __mod_timer()
969 * the timer is re-modified to have the same timeout or ends up in the in __mod_timer()
972 if (!(options & MOD_TIMER_NOTPENDING) && timer_pending(timer)) { in __mod_timer()
976 * timer with this expiry. in __mod_timer()
978 long diff = timer->expires - expires; in __mod_timer()
986 * We lock timer base and calculate the bucket index right in __mod_timer()
987 * here. If the timer ends up in the same bucket, then we in __mod_timer()
991 base = lock_timer_base(timer, &flags); in __mod_timer()
994 if (timer_pending(timer) && (options & MOD_TIMER_REDUCE) && in __mod_timer()
995 time_before_eq(timer->expires, expires)) { in __mod_timer()
1005 * timer. If it matches set the expiry to the new value so a in __mod_timer()
1008 if (idx == timer_get_idx(timer)) { in __mod_timer()
1010 timer->expires = expires; in __mod_timer()
1011 else if (time_after(timer->expires, expires)) in __mod_timer()
1012 timer->expires = expires; in __mod_timer()
1017 base = lock_timer_base(timer, &flags); in __mod_timer()
1021 ret = detach_if_pending(timer, base, false); in __mod_timer()
1025 new_base = get_target_base(base, timer->flags); in __mod_timer()
1029 * We are trying to schedule the timer on the new base. in __mod_timer()
1030 * However we can't change timer's base while it is running, in __mod_timer()
1031 * otherwise del_timer_sync() can't detect that the timer's in __mod_timer()
1033 * timer is serialized wrt itself. in __mod_timer()
1035 if (likely(base->running_timer != timer)) { in __mod_timer()
1037 timer->flags |= TIMER_MIGRATING; in __mod_timer()
1042 WRITE_ONCE(timer->flags, in __mod_timer()
1043 (timer->flags & ~TIMER_BASEMASK) | base->cpu); in __mod_timer()
1048 debug_timer_activate(timer); in __mod_timer()
1050 timer->expires = expires; in __mod_timer()
1058 enqueue_timer(base, timer, idx, bucket_expiry); in __mod_timer()
1060 internal_add_timer(base, timer); in __mod_timer()
1069 * mod_timer_pending - modify a pending timer's timeout
1070 * @timer: the pending timer to be modified
1078 int mod_timer_pending(struct timer_list *timer, unsigned long expires) in mod_timer_pending() argument
1080 return __mod_timer(timer, expires, MOD_TIMER_PENDING_ONLY); in mod_timer_pending()
1085 * mod_timer - modify a timer's timeout
1086 * @timer: the timer to be modified
1090 * active timer (if the timer is inactive it will be activated)
1092 * mod_timer(timer, expires) is equivalent to:
1094 * del_timer(timer); timer->expires = expires; add_timer(timer);
1097 * same timer, then mod_timer() is the only safe way to modify the timeout,
1098 * since add_timer() cannot modify an already running timer.
1100 * The function returns whether it has modified a pending timer or not.
1101 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
1102 * active timer returns 1.)
1104 int mod_timer(struct timer_list *timer, unsigned long expires) in mod_timer() argument
1106 return __mod_timer(timer, expires, 0); in mod_timer()
1111 * timer_reduce - Modify a timer's timeout if it would reduce the timeout
1112 * @timer: The timer to be modified
1116 * modify a running timer if that would reduce the expiration time (it will
1117 * start a timer that isn't running).
1119 int timer_reduce(struct timer_list *timer, unsigned long expires) in timer_reduce() argument
1121 return __mod_timer(timer, expires, MOD_TIMER_REDUCE); in timer_reduce()
1126 * add_timer - start a timer
1127 * @timer: the timer to be added
1129 * The kernel will do a ->function(@timer) callback from the
1130 * timer interrupt at the ->expires point in the future. The
1133 * The timer's ->expires, ->function fields must be set prior calling this
1137 * timer tick.
1139 void add_timer(struct timer_list *timer) in add_timer() argument
1141 BUG_ON(timer_pending(timer)); in add_timer()
1142 __mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING); in add_timer()
1147 * add_timer_on - start a timer on a particular CPU
1148 * @timer: the timer to be added
1153 void add_timer_on(struct timer_list *timer, int cpu) in add_timer_on() argument
1158 BUG_ON(timer_pending(timer) || !timer->function); in add_timer_on()
1160 new_base = get_timer_cpu_base(timer->flags, cpu); in add_timer_on()
1163 * If @timer was on a different CPU, it should be migrated with the in add_timer_on()
1167 base = lock_timer_base(timer, &flags); in add_timer_on()
1169 timer->flags |= TIMER_MIGRATING; in add_timer_on()
1174 WRITE_ONCE(timer->flags, in add_timer_on()
1175 (timer->flags & ~TIMER_BASEMASK) | cpu); in add_timer_on()
1179 debug_timer_activate(timer); in add_timer_on()
1180 internal_add_timer(base, timer); in add_timer_on()
1186 * del_timer - deactivate a timer.
1187 * @timer: the timer to be deactivated
1189 * del_timer() deactivates a timer - this works on both active and inactive
1192 * The function returns whether it has deactivated a pending timer or not.
1193 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
1194 * active timer returns 1.)
1196 int del_timer(struct timer_list *timer) in del_timer() argument
1202 debug_assert_init(timer); in del_timer()
1204 if (timer_pending(timer)) { in del_timer()
1205 base = lock_timer_base(timer, &flags); in del_timer()
1206 ret = detach_if_pending(timer, base, true); in del_timer()
1215 * try_to_del_timer_sync - Try to deactivate a timer
1216 * @timer: timer to delete
1218 * This function tries to deactivate a timer. Upon successful (ret >= 0)
1219 * exit the timer is not queued and the handler is not running on any CPU.
1221 int try_to_del_timer_sync(struct timer_list *timer) in try_to_del_timer_sync() argument
1227 debug_assert_init(timer); in try_to_del_timer_sync()
1229 base = lock_timer_base(timer, &flags); in try_to_del_timer_sync()
1231 if (base->running_timer != timer) in try_to_del_timer_sync()
1232 ret = detach_if_pending(timer, base, true); in try_to_del_timer_sync()
1260 * timer callback to finish. Drop expiry_lock and reaquire it. That allows
1273 * deletion of a timer failed because the timer callback function was
1278 * delete a timer preempted the softirq thread running the timer callback
1281 static void del_timer_wait_running(struct timer_list *timer) in del_timer_wait_running() argument
1285 tf = READ_ONCE(timer->flags); in del_timer_wait_running()
1291 * which is held by the softirq across the timer in del_timer_wait_running()
1293 * expire the next timer. In theory the timer could already in del_timer_wait_running()
1308 static inline void del_timer_wait_running(struct timer_list *timer) { } in del_timer_wait_running() argument
1313 * del_timer_sync - deactivate a timer and wait for the handler to finish.
1314 * @timer: the timer to be deactivated
1317 * the timer it also makes sure the handler has finished executing on other
1320 * Synchronization rules: Callers must prevent restarting of the timer,
1322 * interrupt contexts unless the timer is an irqsafe one. The caller must
1323 * not hold locks which would prevent completion of the timer's
1324 * handler. The timer's handler must not call add_timer_on(). Upon exit the
1325 * timer is not queued and the handler is not running on any CPU.
1329 * nothing to do with the timer in question. Here's why::
1346 * The function returns whether it has deactivated a pending timer or not.
1348 int del_timer_sync(struct timer_list *timer) in del_timer_sync() argument
1360 lock_map_acquire(&timer->lockdep_map); in del_timer_sync()
1361 lock_map_release(&timer->lockdep_map); in del_timer_sync()
1368 WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE)); in del_timer_sync()
1371 ret = try_to_del_timer_sync(timer); in del_timer_sync()
1374 del_timer_wait_running(timer); in del_timer_sync()
1384 static void call_timer_fn(struct timer_list *timer, in call_timer_fn() argument
1392 * It is permissible to free the timer from inside the in call_timer_fn()
1396 * timer->lockdep_map, make a copy and use that here. in call_timer_fn()
1400 lockdep_copy_map(&lockdep_map, &timer->lockdep_map); in call_timer_fn()
1409 trace_timer_expire_entry(timer, baseclk); in call_timer_fn()
1410 fn(timer); in call_timer_fn()
1411 trace_timer_expire_exit(timer); in call_timer_fn()
1416 WARN_ONCE(1, "timer: %pS preempt leak: %08x -> %08x\n", in call_timer_fn()
1438 struct timer_list *timer; in expire_timers() local
1441 timer = hlist_entry(head->first, struct timer_list, entry); in expire_timers()
1443 base->running_timer = timer; in expire_timers()
1444 detach_timer(timer, true); in expire_timers()
1446 fn = timer->function; in expire_timers()
1448 if (timer->flags & TIMER_IRQSAFE) { in expire_timers()
1450 call_timer_fn(timer, fn, baseclk); in expire_timers()
1455 call_timer_fn(timer, fn, baseclk); in expire_timers()
1508 * Search the first expiring timer in the various clock levels. Caller must
1584 * Check, if the next hrtimer event is before the next timer wheel
1599 * If the next timer is already expired, return the tick base in cmp_next_hrtimer_event()
1608 * make sure that this tick really expires the timer to avoid in cmp_next_hrtimer_event()
1617 * get_next_timer_interrupt - return the time (clock mono) of the next timer
1622 * timer or KTIME_MAX if no timer is pending.
1632 * Pretend that there is no timer pending if the cpu is offline. in get_next_timer_interrupt()
1664 * Also the tick is stopped so any added timer must forward in get_next_timer_interrupt()
1678 * timer_clear_idle - Clear the idle state of the timer base
1697 * Called from the timer interrupt handler to charge one tick to the current
1706 /* Note: this timer irq context must be accounted for as well. */ in update_process_times()
1721 * @base: the timer vector to be processed.
1739 * timer at this clk is that all matching timers have been in __run_timers()
1754 * This function runs timers and the timer-tq in bottom half context.
1766 * Called by the local, per-CPU timer interrupt on SMP.
1786 * Since schedule_timeout()'s timer is defined on the stack, it must store
1790 struct timer_list timer; member
1796 struct process_timer *timeout = from_timer(timeout, t, timer); in process_timeout()
1828 * Returns 0 when the timer has expired otherwise the remaining time in
1834 struct process_timer timer; in schedule_timeout() local
1868 timer.task = current; in schedule_timeout()
1869 timer_setup_on_stack(&timer.timer, process_timeout, 0); in schedule_timeout()
1870 __mod_timer(&timer.timer, expire, MOD_TIMER_NOTPENDING); in schedule_timeout()
1872 del_singleshot_timer_sync(&timer.timer); in schedule_timeout()
1874 /* Remove the timer from the object tracker */ in schedule_timeout()
1875 destroy_timer_on_stack(&timer.timer); in schedule_timeout()
1923 struct timer_list *timer; in migrate_timer_list() local
1927 timer = hlist_entry(head->first, struct timer_list, entry); in migrate_timer_list()
1928 detach_timer(timer, false); in migrate_timer_list()
1929 timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu; in migrate_timer_list()
1930 internal_add_timer(new_base, timer); in migrate_timer_list()