Lines Matching full:timer

16  *  2000-10-05  Implemented scalable SMP per-CPU timer handling.
58 #include <trace/events/timer.h>
65 * The timer wheel has LVL_DEPTH array levels. Each level provides an array of
72 * The array level of a newly armed timer depends on the relative expiry
76 * Contrary to the original timer wheel implementation, which aims for 'exact'
78 * the timers into the lower array levels. The previous 'classic' timer wheel
83 * This is an optimization of the original timer wheel implementation for the
84 * majority of the timer wheel use cases: timeouts. The vast majority of
318 * due to delays of the timer irq, long irq off times etc etc) then in round_jiffies_common()
501 static inline unsigned int timer_get_idx(struct timer_list *timer) in timer_get_idx() argument
503 return (timer->flags & TIMER_ARRAYMASK) >> TIMER_ARRAYSHIFT; in timer_get_idx()
506 static inline void timer_set_idx(struct timer_list *timer, unsigned int idx) in timer_set_idx() argument
508 timer->flags = (timer->flags & ~TIMER_ARRAYMASK) | in timer_set_idx()
521 * The timer wheel has to guarantee that a timer does not fire in calc_index()
523 * - Timer is armed at the edge of a tick in calc_index()
572 trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer) in trigger_dyntick_cpu() argument
581 if (timer->flags & TIMER_DEFERRABLE) { in trigger_dyntick_cpu()
589 * timer is not deferrable. If the other CPU is on the way to idle in trigger_dyntick_cpu()
597 * Enqueue the timer into the hash bucket, mark it pending in
598 * the bitmap, store the index in the timer flags then wake up
601 static void enqueue_timer(struct timer_base *base, struct timer_list *timer, in enqueue_timer() argument
605 hlist_add_head(&timer->entry, base->vectors + idx); in enqueue_timer()
607 timer_set_idx(timer, idx); in enqueue_timer()
609 trace_timer_start(timer, timer->expires, timer->flags); in enqueue_timer()
612 * Check whether this is the new first expiring timer. The in enqueue_timer()
613 * effective expiry time of the timer is required here in enqueue_timer()
614 * (bucket_expiry) instead of timer->expires. in enqueue_timer()
624 trigger_dyntick_cpu(base, timer); in enqueue_timer()
628 static void internal_add_timer(struct timer_base *base, struct timer_list *timer) in internal_add_timer() argument
633 idx = calc_wheel_index(timer->expires, base->clk, &bucket_expiry); in internal_add_timer()
634 enqueue_timer(base, timer, idx, bucket_expiry); in internal_add_timer()
655 struct delayed_work, timer, work.func),
657 struct kthread_delayed_work, timer, work.func),
662 struct timer_list *timer = addr; in timer_debug_hint() local
666 if (timer_hints[i].function == timer->function) { in timer_debug_hint()
673 return timer->function; in timer_debug_hint()
678 struct timer_list *timer = addr; in timer_is_static_object() local
680 return (timer->entry.pprev == NULL && in timer_is_static_object()
681 timer->entry.next == TIMER_ENTRY_STATIC); in timer_is_static_object()
690 struct timer_list *timer = addr; in timer_fixup_init() local
694 del_timer_sync(timer); in timer_fixup_init()
695 debug_object_init(timer, &timer_debug_descr); in timer_fixup_init()
702 /* Stub timer callback for improperly used timers. */
715 struct timer_list *timer = addr; in timer_fixup_activate() local
719 timer_setup(timer, stub_timer, 0); in timer_fixup_activate()
736 struct timer_list *timer = addr; in timer_fixup_free() local
740 del_timer_sync(timer); in timer_fixup_free()
741 debug_object_free(timer, &timer_debug_descr); in timer_fixup_free()
754 struct timer_list *timer = addr; in timer_fixup_assert_init() local
758 timer_setup(timer, stub_timer, 0); in timer_fixup_assert_init()
775 static inline void debug_timer_init(struct timer_list *timer) in debug_timer_init() argument
777 debug_object_init(timer, &timer_debug_descr); in debug_timer_init()
780 static inline void debug_timer_activate(struct timer_list *timer) in debug_timer_activate() argument
782 debug_object_activate(timer, &timer_debug_descr); in debug_timer_activate()
785 static inline void debug_timer_deactivate(struct timer_list *timer) in debug_timer_deactivate() argument
787 debug_object_deactivate(timer, &timer_debug_descr); in debug_timer_deactivate()
790 static inline void debug_timer_assert_init(struct timer_list *timer) in debug_timer_assert_init() argument
792 debug_object_assert_init(timer, &timer_debug_descr); in debug_timer_assert_init()
795 static void do_init_timer(struct timer_list *timer,
800 void init_timer_on_stack_key(struct timer_list *timer, in init_timer_on_stack_key() argument
805 debug_object_init_on_stack(timer, &timer_debug_descr); in init_timer_on_stack_key()
806 do_init_timer(timer, func, flags, name, key); in init_timer_on_stack_key()
810 void destroy_timer_on_stack(struct timer_list *timer) in destroy_timer_on_stack() argument
812 debug_object_free(timer, &timer_debug_descr); in destroy_timer_on_stack()
817 static inline void debug_timer_init(struct timer_list *timer) { } in debug_timer_init() argument
818 static inline void debug_timer_activate(struct timer_list *timer) { } in debug_timer_activate() argument
819 static inline void debug_timer_deactivate(struct timer_list *timer) { } in debug_timer_deactivate() argument
820 static inline void debug_timer_assert_init(struct timer_list *timer) { } in debug_timer_assert_init() argument
823 static inline void debug_init(struct timer_list *timer) in debug_init() argument
825 debug_timer_init(timer); in debug_init()
826 trace_timer_init(timer); in debug_init()
829 static inline void debug_deactivate(struct timer_list *timer) in debug_deactivate() argument
831 debug_timer_deactivate(timer); in debug_deactivate()
832 trace_timer_cancel(timer); in debug_deactivate()
835 static inline void debug_assert_init(struct timer_list *timer) in debug_assert_init() argument
837 debug_timer_assert_init(timer); in debug_assert_init()
840 static void do_init_timer(struct timer_list *timer, in do_init_timer() argument
845 timer->entry.pprev = NULL; in do_init_timer()
846 timer->function = func; in do_init_timer()
849 timer->flags = flags | raw_smp_processor_id(); in do_init_timer()
850 lockdep_init_map(&timer->lockdep_map, name, key, 0); in do_init_timer()
854 * init_timer_key - initialize a timer
855 * @timer: the timer to be initialized
856 * @func: timer callback function
857 * @flags: timer flags
858 * @name: name of the timer
859 * @key: lockdep class key of the fake lock used for tracking timer
862 * init_timer_key() must be done to a timer prior calling *any* of the
863 * other timer functions.
865 void init_timer_key(struct timer_list *timer, in init_timer_key() argument
869 debug_init(timer); in init_timer_key()
870 do_init_timer(timer, func, flags, name, key); in init_timer_key()
874 static inline void detach_timer(struct timer_list *timer, bool clear_pending) in detach_timer() argument
876 struct hlist_node *entry = &timer->entry; in detach_timer()
878 debug_deactivate(timer); in detach_timer()
886 static int detach_if_pending(struct timer_list *timer, struct timer_base *base, in detach_if_pending() argument
889 unsigned idx = timer_get_idx(timer); in detach_if_pending()
891 if (!timer_pending(timer)) in detach_if_pending()
894 if (hlist_is_singular_node(&timer->entry, base->vectors + idx)) { in detach_if_pending()
899 detach_timer(timer, clear_pending); in detach_if_pending()
908 * If the timer is deferrable and NO_HZ_COMMON is set then we need in get_timer_cpu_base()
921 * If the timer is deferrable and NO_HZ_COMMON is set then we need in get_timer_this_cpu_base()
979 * When a timer is migrating then the TIMER_MIGRATING flag is set and we need
982 static struct timer_base *lock_timer_base(struct timer_list *timer, in lock_timer_base() argument
984 __acquires(timer->base->lock) in lock_timer_base()
995 tf = READ_ONCE(timer->flags); in lock_timer_base()
1000 if (timer->flags == tf) in lock_timer_base()
1013 __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int options) in __mod_timer() argument
1020 BUG_ON(!timer->function); in __mod_timer()
1024 * the timer is re-modified to have the same timeout or ends up in the in __mod_timer()
1027 if (!(options & MOD_TIMER_NOTPENDING) && timer_pending(timer)) { in __mod_timer()
1031 * timer with this expiry. in __mod_timer()
1033 long diff = timer->expires - expires; in __mod_timer()
1041 * We lock timer base and calculate the bucket index right in __mod_timer()
1042 * here. If the timer ends up in the same bucket, then we in __mod_timer()
1046 base = lock_timer_base(timer, &flags); in __mod_timer()
1049 if (timer_pending(timer) && (options & MOD_TIMER_REDUCE) && in __mod_timer()
1050 time_before_eq(timer->expires, expires)) { in __mod_timer()
1060 * timer. If it matches set the expiry to the new value so a in __mod_timer()
1063 if (idx == timer_get_idx(timer)) { in __mod_timer()
1065 timer->expires = expires; in __mod_timer()
1066 else if (time_after(timer->expires, expires)) in __mod_timer()
1067 timer->expires = expires; in __mod_timer()
1072 base = lock_timer_base(timer, &flags); in __mod_timer()
1076 ret = detach_if_pending(timer, base, false); in __mod_timer()
1080 new_base = get_target_base(base, timer->flags); in __mod_timer()
1084 * We are trying to schedule the timer on the new base. in __mod_timer()
1085 * However we can't change timer's base while it is running, in __mod_timer()
1086 * otherwise del_timer_sync() can't detect that the timer's in __mod_timer()
1088 * timer is serialized wrt itself. in __mod_timer()
1090 if (likely(base->running_timer != timer)) { in __mod_timer()
1092 timer->flags |= TIMER_MIGRATING; in __mod_timer()
1097 WRITE_ONCE(timer->flags, in __mod_timer()
1098 (timer->flags & ~TIMER_BASEMASK) | base->cpu); in __mod_timer()
1103 debug_timer_activate(timer); in __mod_timer()
1105 timer->expires = expires; in __mod_timer()
1113 enqueue_timer(base, timer, idx, bucket_expiry); in __mod_timer()
1115 internal_add_timer(base, timer); in __mod_timer()
1124 * mod_timer_pending - modify a pending timer's timeout
1125 * @timer: the pending timer to be modified
1133 int mod_timer_pending(struct timer_list *timer, unsigned long expires) in mod_timer_pending() argument
1135 return __mod_timer(timer, expires, MOD_TIMER_PENDING_ONLY); in mod_timer_pending()
1140 * mod_timer - modify a timer's timeout
1141 * @timer: the timer to be modified
1145 * active timer (if the timer is inactive it will be activated)
1147 * mod_timer(timer, expires) is equivalent to:
1149 * del_timer(timer); timer->expires = expires; add_timer(timer);
1152 * same timer, then mod_timer() is the only safe way to modify the timeout,
1153 * since add_timer() cannot modify an already running timer.
1155 * The function returns whether it has modified a pending timer or not.
1156 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
1157 * active timer returns 1.)
1159 int mod_timer(struct timer_list *timer, unsigned long expires) in mod_timer() argument
1161 return __mod_timer(timer, expires, 0); in mod_timer()
1166 * timer_reduce - Modify a timer's timeout if it would reduce the timeout
1167 * @timer: The timer to be modified
1171 * modify a running timer if that would reduce the expiration time (it will
1172 * start a timer that isn't running).
1174 int timer_reduce(struct timer_list *timer, unsigned long expires) in timer_reduce() argument
1176 return __mod_timer(timer, expires, MOD_TIMER_REDUCE); in timer_reduce()
1181 * add_timer - start a timer
1182 * @timer: the timer to be added
1184 * The kernel will do a ->function(@timer) callback from the
1185 * timer interrupt at the ->expires point in the future. The
1188 * The timer's ->expires, ->function fields must be set prior calling this
1192 * timer tick.
1194 void add_timer(struct timer_list *timer) in add_timer() argument
1196 BUG_ON(timer_pending(timer)); in add_timer()
1197 __mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING); in add_timer()
1202 * add_timer_on - start a timer on a particular CPU
1203 * @timer: the timer to be added
1208 void add_timer_on(struct timer_list *timer, int cpu) in add_timer_on() argument
1213 BUG_ON(timer_pending(timer) || !timer->function); in add_timer_on()
1215 new_base = get_timer_cpu_base(timer->flags, cpu); in add_timer_on()
1218 * If @timer was on a different CPU, it should be migrated with the in add_timer_on()
1222 base = lock_timer_base(timer, &flags); in add_timer_on()
1224 timer->flags |= TIMER_MIGRATING; in add_timer_on()
1229 WRITE_ONCE(timer->flags, in add_timer_on()
1230 (timer->flags & ~TIMER_BASEMASK) | cpu); in add_timer_on()
1234 debug_timer_activate(timer); in add_timer_on()
1235 internal_add_timer(base, timer); in add_timer_on()
1241 * del_timer - deactivate a timer.
1242 * @timer: the timer to be deactivated
1244 * del_timer() deactivates a timer - this works on both active and inactive
1247 * The function returns whether it has deactivated a pending timer or not.
1248 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
1249 * active timer returns 1.)
1251 int del_timer(struct timer_list *timer) in del_timer() argument
1257 debug_assert_init(timer); in del_timer()
1259 if (timer_pending(timer)) { in del_timer()
1260 base = lock_timer_base(timer, &flags); in del_timer()
1261 ret = detach_if_pending(timer, base, true); in del_timer()
1270 * try_to_del_timer_sync - Try to deactivate a timer
1271 * @timer: timer to delete
1273 * This function tries to deactivate a timer. Upon successful (ret >= 0)
1274 * exit the timer is not queued and the handler is not running on any CPU.
1276 int try_to_del_timer_sync(struct timer_list *timer) in try_to_del_timer_sync() argument
1282 debug_assert_init(timer); in try_to_del_timer_sync()
1284 base = lock_timer_base(timer, &flags); in try_to_del_timer_sync()
1286 if (base->running_timer != timer) in try_to_del_timer_sync()
1287 ret = detach_if_pending(timer, base, true); in try_to_del_timer_sync()
1315 * timer callback to finish. Drop expiry_lock and reacquire it. That allows
1330 * deletion of a timer failed because the timer callback function was
1335 * delete a timer preempted the softirq thread running the timer callback
1338 static void del_timer_wait_running(struct timer_list *timer) in del_timer_wait_running() argument
1342 tf = READ_ONCE(timer->flags); in del_timer_wait_running()
1348 * which is held by the softirq across the timer in del_timer_wait_running()
1350 * expire the next timer. In theory the timer could already in del_timer_wait_running()
1365 static inline void del_timer_wait_running(struct timer_list *timer) { } in del_timer_wait_running() argument
1370 * del_timer_sync - deactivate a timer and wait for the handler to finish.
1371 * @timer: the timer to be deactivated
1374 * the timer it also makes sure the handler has finished executing on other
1377 * Synchronization rules: Callers must prevent restarting of the timer,
1379 * interrupt contexts unless the timer is an irqsafe one. The caller must
1380 * not hold locks which would prevent completion of the timer's
1381 * handler. The timer's handler must not call add_timer_on(). Upon exit the
1382 * timer is not queued and the handler is not running on any CPU.
1386 * nothing to do with the timer in question. Here's why::
1403 * The function returns whether it has deactivated a pending timer or not.
1405 int del_timer_sync(struct timer_list *timer) in del_timer_sync() argument
1417 lock_map_acquire(&timer->lockdep_map); in del_timer_sync()
1418 lock_map_release(&timer->lockdep_map); in del_timer_sync()
1425 WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE)); in del_timer_sync()
1431 if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(timer->flags & TIMER_IRQSAFE)) in del_timer_sync()
1435 ret = try_to_del_timer_sync(timer); in del_timer_sync()
1438 del_timer_wait_running(timer); in del_timer_sync()
1448 static void call_timer_fn(struct timer_list *timer, in call_timer_fn() argument
1456 * It is permissible to free the timer from inside the in call_timer_fn()
1460 * timer->lockdep_map, make a copy and use that here. in call_timer_fn()
1464 lockdep_copy_map(&lockdep_map, &timer->lockdep_map); in call_timer_fn()
1473 trace_timer_expire_entry(timer, baseclk); in call_timer_fn()
1474 fn(timer); in call_timer_fn()
1475 trace_timer_expire_exit(timer); in call_timer_fn()
1480 WARN_ONCE(1, "timer: %pS preempt leak: %08x -> %08x\n", in call_timer_fn()
1502 struct timer_list *timer; in expire_timers() local
1505 timer = hlist_entry(head->first, struct timer_list, entry); in expire_timers()
1507 base->running_timer = timer; in expire_timers()
1508 detach_timer(timer, true); in expire_timers()
1510 fn = timer->function; in expire_timers()
1512 if (timer->flags & TIMER_IRQSAFE) { in expire_timers()
1514 call_timer_fn(timer, fn, baseclk); in expire_timers()
1519 call_timer_fn(timer, fn, baseclk); in expire_timers()
1572 * Search the first expiring timer in the various clock levels. Caller must
1649 * Check, if the next hrtimer event is before the next timer wheel
1664 * If the next timer is already expired, return the tick base in cmp_next_hrtimer_event()
1673 * make sure that this tick really expires the timer to avoid in cmp_next_hrtimer_event()
1682 * get_next_timer_interrupt - return the time (clock mono) of the next timer
1687 * timer or KTIME_MAX if no timer is pending.
1696 * Pretend that there is no timer pending if the cpu is offline. in get_next_timer_interrupt()
1727 * Also the tick is stopped so any added timer must forward in get_next_timer_interrupt()
1741 * timer_clear_idle - Clear the idle state of the timer base
1761 * @base: the timer vector to be processed.
1779 * timer at this clk are that all matching timers have been in __run_timers()
1780 * dequeued or no timer has been queued since in __run_timers()
1797 * This function runs timers and the timer-tq in bottom half context.
1809 * Called by the local, per-CPU timer interrupt on SMP.
1829 * Called from the timer interrupt handler to charge one tick to the current
1836 /* Note: this timer irq context must be accounted for as well. */ in update_process_times()
1850 * Since schedule_timeout()'s timer is defined on the stack, it must store
1854 struct timer_list timer; member
1860 struct process_timer *timeout = from_timer(timeout, t, timer); in process_timeout()
1892 * Returns 0 when the timer has expired otherwise the remaining time in
1898 struct process_timer timer; in schedule_timeout() local
1932 timer.task = current; in schedule_timeout()
1933 timer_setup_on_stack(&timer.timer, process_timeout, 0); in schedule_timeout()
1934 __mod_timer(&timer.timer, expire, MOD_TIMER_NOTPENDING); in schedule_timeout()
1936 del_singleshot_timer_sync(&timer.timer); in schedule_timeout()
1938 /* Remove the timer from the object tracker */ in schedule_timeout()
1939 destroy_timer_on_stack(&timer.timer); in schedule_timeout()
1987 struct timer_list *timer; in migrate_timer_list() local
1991 timer = hlist_entry(head->first, struct timer_list, entry); in migrate_timer_list()
1992 detach_timer(timer, false); in migrate_timer_list()
1993 timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu; in migrate_timer_list()
1994 internal_add_timer(new_base, timer); in migrate_timer_list()