xref: /linux-6.15/kernel/time/timer.c (revision 3ed769bd)
15cee9645SThomas Gleixner /*
25cee9645SThomas Gleixner  *  linux/kernel/timer.c
35cee9645SThomas Gleixner  *
45cee9645SThomas Gleixner  *  Kernel internal timers
55cee9645SThomas Gleixner  *
65cee9645SThomas Gleixner  *  Copyright (C) 1991, 1992  Linus Torvalds
75cee9645SThomas Gleixner  *
85cee9645SThomas Gleixner  *  1997-01-28  Modified by Finn Arne Gangstad to make timers scale better.
95cee9645SThomas Gleixner  *
105cee9645SThomas Gleixner  *  1997-09-10  Updated NTP code according to technical memorandum Jan '96
115cee9645SThomas Gleixner  *              "A Kernel Model for Precision Timekeeping" by Dave Mills
125cee9645SThomas Gleixner  *  1998-12-24  Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
135cee9645SThomas Gleixner  *              serialize accesses to xtime/lost_ticks).
145cee9645SThomas Gleixner  *                              Copyright (C) 1998  Andrea Arcangeli
155cee9645SThomas Gleixner  *  1999-03-10  Improved NTP compatibility by Ulrich Windl
165cee9645SThomas Gleixner  *  2002-05-31	Move sys_sysinfo here and make its locking sane, Robert Love
175cee9645SThomas Gleixner  *  2000-10-05  Implemented scalable SMP per-CPU timer handling.
185cee9645SThomas Gleixner  *                              Copyright (C) 2000, 2001, 2002  Ingo Molnar
195cee9645SThomas Gleixner  *              Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
205cee9645SThomas Gleixner  */
215cee9645SThomas Gleixner 
225cee9645SThomas Gleixner #include <linux/kernel_stat.h>
235cee9645SThomas Gleixner #include <linux/export.h>
245cee9645SThomas Gleixner #include <linux/interrupt.h>
255cee9645SThomas Gleixner #include <linux/percpu.h>
265cee9645SThomas Gleixner #include <linux/init.h>
275cee9645SThomas Gleixner #include <linux/mm.h>
285cee9645SThomas Gleixner #include <linux/swap.h>
295cee9645SThomas Gleixner #include <linux/pid_namespace.h>
305cee9645SThomas Gleixner #include <linux/notifier.h>
315cee9645SThomas Gleixner #include <linux/thread_info.h>
325cee9645SThomas Gleixner #include <linux/time.h>
335cee9645SThomas Gleixner #include <linux/jiffies.h>
345cee9645SThomas Gleixner #include <linux/posix-timers.h>
355cee9645SThomas Gleixner #include <linux/cpu.h>
365cee9645SThomas Gleixner #include <linux/syscalls.h>
375cee9645SThomas Gleixner #include <linux/delay.h>
385cee9645SThomas Gleixner #include <linux/tick.h>
395cee9645SThomas Gleixner #include <linux/kallsyms.h>
405cee9645SThomas Gleixner #include <linux/irq_work.h>
415cee9645SThomas Gleixner #include <linux/sched.h>
425cee9645SThomas Gleixner #include <linux/sched/sysctl.h>
435cee9645SThomas Gleixner #include <linux/slab.h>
445cee9645SThomas Gleixner #include <linux/compat.h>
455cee9645SThomas Gleixner 
465cee9645SThomas Gleixner #include <asm/uaccess.h>
475cee9645SThomas Gleixner #include <asm/unistd.h>
485cee9645SThomas Gleixner #include <asm/div64.h>
495cee9645SThomas Gleixner #include <asm/timex.h>
505cee9645SThomas Gleixner #include <asm/io.h>
515cee9645SThomas Gleixner 
52c1ad348bSThomas Gleixner #include "tick-internal.h"
53c1ad348bSThomas Gleixner 
545cee9645SThomas Gleixner #define CREATE_TRACE_POINTS
555cee9645SThomas Gleixner #include <trace/events/timer.h>
565cee9645SThomas Gleixner 
575cee9645SThomas Gleixner __visible u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
585cee9645SThomas Gleixner 
595cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_64);
605cee9645SThomas Gleixner 
615cee9645SThomas Gleixner /*
625cee9645SThomas Gleixner  * per-CPU timer vector definitions:
635cee9645SThomas Gleixner  */
645cee9645SThomas Gleixner #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
655cee9645SThomas Gleixner #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
665cee9645SThomas Gleixner #define TVN_SIZE (1 << TVN_BITS)
675cee9645SThomas Gleixner #define TVR_SIZE (1 << TVR_BITS)
685cee9645SThomas Gleixner #define TVN_MASK (TVN_SIZE - 1)
695cee9645SThomas Gleixner #define TVR_MASK (TVR_SIZE - 1)
705cee9645SThomas Gleixner #define MAX_TVAL ((unsigned long)((1ULL << (TVR_BITS + 4*TVN_BITS)) - 1))
715cee9645SThomas Gleixner 
725cee9645SThomas Gleixner struct tvec {
731dabbcecSThomas Gleixner 	struct hlist_head vec[TVN_SIZE];
745cee9645SThomas Gleixner };
755cee9645SThomas Gleixner 
765cee9645SThomas Gleixner struct tvec_root {
771dabbcecSThomas Gleixner 	struct hlist_head vec[TVR_SIZE];
785cee9645SThomas Gleixner };
795cee9645SThomas Gleixner 
805cee9645SThomas Gleixner struct tvec_base {
815cee9645SThomas Gleixner 	spinlock_t lock;
825cee9645SThomas Gleixner 	struct timer_list *running_timer;
835cee9645SThomas Gleixner 	unsigned long timer_jiffies;
845cee9645SThomas Gleixner 	unsigned long next_timer;
855cee9645SThomas Gleixner 	unsigned long active_timers;
865cee9645SThomas Gleixner 	unsigned long all_timers;
87d6f93829SViresh Kumar 	int cpu;
88bc7a34b8SThomas Gleixner 	bool migration_enabled;
89683be13aSThomas Gleixner 	bool nohz_active;
905cee9645SThomas Gleixner 	struct tvec_root tv1;
915cee9645SThomas Gleixner 	struct tvec tv2;
925cee9645SThomas Gleixner 	struct tvec tv3;
935cee9645SThomas Gleixner 	struct tvec tv4;
945cee9645SThomas Gleixner 	struct tvec tv5;
955cee9645SThomas Gleixner } ____cacheline_aligned;
965cee9645SThomas Gleixner 
97b337a938SPeter Zijlstra 
980eeda71bSThomas Gleixner static DEFINE_PER_CPU(struct tvec_base, tvec_bases);
995cee9645SThomas Gleixner 
100bc7a34b8SThomas Gleixner #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
101bc7a34b8SThomas Gleixner unsigned int sysctl_timer_migration = 1;
102bc7a34b8SThomas Gleixner 
103683be13aSThomas Gleixner void timers_update_migration(bool update_nohz)
104bc7a34b8SThomas Gleixner {
105bc7a34b8SThomas Gleixner 	bool on = sysctl_timer_migration && tick_nohz_active;
106bc7a34b8SThomas Gleixner 	unsigned int cpu;
107bc7a34b8SThomas Gleixner 
108bc7a34b8SThomas Gleixner 	/* Avoid the loop, if nothing to update */
109bc7a34b8SThomas Gleixner 	if (this_cpu_read(tvec_bases.migration_enabled) == on)
110bc7a34b8SThomas Gleixner 		return;
111bc7a34b8SThomas Gleixner 
112bc7a34b8SThomas Gleixner 	for_each_possible_cpu(cpu) {
113bc7a34b8SThomas Gleixner 		per_cpu(tvec_bases.migration_enabled, cpu) = on;
114bc7a34b8SThomas Gleixner 		per_cpu(hrtimer_bases.migration_enabled, cpu) = on;
115683be13aSThomas Gleixner 		if (!update_nohz)
116683be13aSThomas Gleixner 			continue;
117683be13aSThomas Gleixner 		per_cpu(tvec_bases.nohz_active, cpu) = true;
118683be13aSThomas Gleixner 		per_cpu(hrtimer_bases.nohz_active, cpu) = true;
119bc7a34b8SThomas Gleixner 	}
120bc7a34b8SThomas Gleixner }
121bc7a34b8SThomas Gleixner 
122bc7a34b8SThomas Gleixner int timer_migration_handler(struct ctl_table *table, int write,
123bc7a34b8SThomas Gleixner 			    void __user *buffer, size_t *lenp,
124bc7a34b8SThomas Gleixner 			    loff_t *ppos)
125bc7a34b8SThomas Gleixner {
126bc7a34b8SThomas Gleixner 	static DEFINE_MUTEX(mutex);
127bc7a34b8SThomas Gleixner 	int ret;
128bc7a34b8SThomas Gleixner 
129bc7a34b8SThomas Gleixner 	mutex_lock(&mutex);
130bc7a34b8SThomas Gleixner 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
131bc7a34b8SThomas Gleixner 	if (!ret && write)
132683be13aSThomas Gleixner 		timers_update_migration(false);
133bc7a34b8SThomas Gleixner 	mutex_unlock(&mutex);
134bc7a34b8SThomas Gleixner 	return ret;
135bc7a34b8SThomas Gleixner }
136bc7a34b8SThomas Gleixner 
137bc7a34b8SThomas Gleixner static inline struct tvec_base *get_target_base(struct tvec_base *base,
138bc7a34b8SThomas Gleixner 						int pinned)
139bc7a34b8SThomas Gleixner {
140bc7a34b8SThomas Gleixner 	if (pinned || !base->migration_enabled)
141bc7a34b8SThomas Gleixner 		return this_cpu_ptr(&tvec_bases);
142bc7a34b8SThomas Gleixner 	return per_cpu_ptr(&tvec_bases, get_nohz_timer_target());
143bc7a34b8SThomas Gleixner }
144bc7a34b8SThomas Gleixner #else
145bc7a34b8SThomas Gleixner static inline struct tvec_base *get_target_base(struct tvec_base *base,
146bc7a34b8SThomas Gleixner 						int pinned)
147bc7a34b8SThomas Gleixner {
148bc7a34b8SThomas Gleixner 	return this_cpu_ptr(&tvec_bases);
149bc7a34b8SThomas Gleixner }
150bc7a34b8SThomas Gleixner #endif
151bc7a34b8SThomas Gleixner 
1525cee9645SThomas Gleixner static unsigned long round_jiffies_common(unsigned long j, int cpu,
1535cee9645SThomas Gleixner 		bool force_up)
1545cee9645SThomas Gleixner {
1555cee9645SThomas Gleixner 	int rem;
1565cee9645SThomas Gleixner 	unsigned long original = j;
1575cee9645SThomas Gleixner 
1585cee9645SThomas Gleixner 	/*
1595cee9645SThomas Gleixner 	 * We don't want all cpus firing their timers at once hitting the
1605cee9645SThomas Gleixner 	 * same lock or cachelines, so we skew each extra cpu with an extra
1615cee9645SThomas Gleixner 	 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
1625cee9645SThomas Gleixner 	 * already did this.
1635cee9645SThomas Gleixner 	 * The skew is done by adding 3*cpunr, then round, then subtract this
1645cee9645SThomas Gleixner 	 * extra offset again.
1655cee9645SThomas Gleixner 	 */
1665cee9645SThomas Gleixner 	j += cpu * 3;
1675cee9645SThomas Gleixner 
1685cee9645SThomas Gleixner 	rem = j % HZ;
1695cee9645SThomas Gleixner 
1705cee9645SThomas Gleixner 	/*
1715cee9645SThomas Gleixner 	 * If the target jiffie is just after a whole second (which can happen
1725cee9645SThomas Gleixner 	 * due to delays of the timer irq, long irq off times etc etc) then
1735cee9645SThomas Gleixner 	 * we should round down to the whole second, not up. Use 1/4th second
1745cee9645SThomas Gleixner 	 * as cutoff for this rounding as an extreme upper bound for this.
1755cee9645SThomas Gleixner 	 * But never round down if @force_up is set.
1765cee9645SThomas Gleixner 	 */
1775cee9645SThomas Gleixner 	if (rem < HZ/4 && !force_up) /* round down */
1785cee9645SThomas Gleixner 		j = j - rem;
1795cee9645SThomas Gleixner 	else /* round up */
1805cee9645SThomas Gleixner 		j = j - rem + HZ;
1815cee9645SThomas Gleixner 
1825cee9645SThomas Gleixner 	/* now that we have rounded, subtract the extra skew again */
1835cee9645SThomas Gleixner 	j -= cpu * 3;
1845cee9645SThomas Gleixner 
1855cee9645SThomas Gleixner 	/*
1865cee9645SThomas Gleixner 	 * Make sure j is still in the future. Otherwise return the
1875cee9645SThomas Gleixner 	 * unmodified value.
1885cee9645SThomas Gleixner 	 */
1895cee9645SThomas Gleixner 	return time_is_after_jiffies(j) ? j : original;
1905cee9645SThomas Gleixner }
1915cee9645SThomas Gleixner 
1925cee9645SThomas Gleixner /**
1935cee9645SThomas Gleixner  * __round_jiffies - function to round jiffies to a full second
1945cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
1955cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
1965cee9645SThomas Gleixner  *
1975cee9645SThomas Gleixner  * __round_jiffies() rounds an absolute time in the future (in jiffies)
1985cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
1995cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
2005cee9645SThomas Gleixner  * they fire approximately every X seconds.
2015cee9645SThomas Gleixner  *
2025cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
2035cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
2045cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
2055cee9645SThomas Gleixner  *
2065cee9645SThomas Gleixner  * The exact rounding is skewed for each processor to avoid all
2075cee9645SThomas Gleixner  * processors firing at the exact same time, which could lead
2085cee9645SThomas Gleixner  * to lock contention or spurious cache line bouncing.
2095cee9645SThomas Gleixner  *
2105cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
2115cee9645SThomas Gleixner  */
2125cee9645SThomas Gleixner unsigned long __round_jiffies(unsigned long j, int cpu)
2135cee9645SThomas Gleixner {
2145cee9645SThomas Gleixner 	return round_jiffies_common(j, cpu, false);
2155cee9645SThomas Gleixner }
2165cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies);
2175cee9645SThomas Gleixner 
2185cee9645SThomas Gleixner /**
2195cee9645SThomas Gleixner  * __round_jiffies_relative - function to round jiffies to a full second
2205cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
2215cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
2225cee9645SThomas Gleixner  *
2235cee9645SThomas Gleixner  * __round_jiffies_relative() rounds a time delta  in the future (in jiffies)
2245cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
2255cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
2265cee9645SThomas Gleixner  * they fire approximately every X seconds.
2275cee9645SThomas Gleixner  *
2285cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
2295cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
2305cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
2315cee9645SThomas Gleixner  *
2325cee9645SThomas Gleixner  * The exact rounding is skewed for each processor to avoid all
2335cee9645SThomas Gleixner  * processors firing at the exact same time, which could lead
2345cee9645SThomas Gleixner  * to lock contention or spurious cache line bouncing.
2355cee9645SThomas Gleixner  *
2365cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
2375cee9645SThomas Gleixner  */
2385cee9645SThomas Gleixner unsigned long __round_jiffies_relative(unsigned long j, int cpu)
2395cee9645SThomas Gleixner {
2405cee9645SThomas Gleixner 	unsigned long j0 = jiffies;
2415cee9645SThomas Gleixner 
2425cee9645SThomas Gleixner 	/* Use j0 because jiffies might change while we run */
2435cee9645SThomas Gleixner 	return round_jiffies_common(j + j0, cpu, false) - j0;
2445cee9645SThomas Gleixner }
2455cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_relative);
2465cee9645SThomas Gleixner 
2475cee9645SThomas Gleixner /**
2485cee9645SThomas Gleixner  * round_jiffies - function to round jiffies to a full second
2495cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
2505cee9645SThomas Gleixner  *
2515cee9645SThomas Gleixner  * round_jiffies() rounds an absolute time in the future (in jiffies)
2525cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
2535cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
2545cee9645SThomas Gleixner  * they fire approximately every X seconds.
2555cee9645SThomas Gleixner  *
2565cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
2575cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
2585cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
2595cee9645SThomas Gleixner  *
2605cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
2615cee9645SThomas Gleixner  */
2625cee9645SThomas Gleixner unsigned long round_jiffies(unsigned long j)
2635cee9645SThomas Gleixner {
2645cee9645SThomas Gleixner 	return round_jiffies_common(j, raw_smp_processor_id(), false);
2655cee9645SThomas Gleixner }
2665cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies);
2675cee9645SThomas Gleixner 
2685cee9645SThomas Gleixner /**
2695cee9645SThomas Gleixner  * round_jiffies_relative - function to round jiffies to a full second
2705cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
2715cee9645SThomas Gleixner  *
2725cee9645SThomas Gleixner  * round_jiffies_relative() rounds a time delta  in the future (in jiffies)
2735cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
2745cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
2755cee9645SThomas Gleixner  * they fire approximately every X seconds.
2765cee9645SThomas Gleixner  *
2775cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
2785cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
2795cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
2805cee9645SThomas Gleixner  *
2815cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
2825cee9645SThomas Gleixner  */
2835cee9645SThomas Gleixner unsigned long round_jiffies_relative(unsigned long j)
2845cee9645SThomas Gleixner {
2855cee9645SThomas Gleixner 	return __round_jiffies_relative(j, raw_smp_processor_id());
2865cee9645SThomas Gleixner }
2875cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_relative);
2885cee9645SThomas Gleixner 
2895cee9645SThomas Gleixner /**
2905cee9645SThomas Gleixner  * __round_jiffies_up - function to round jiffies up to a full second
2915cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
2925cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
2935cee9645SThomas Gleixner  *
2945cee9645SThomas Gleixner  * This is the same as __round_jiffies() except that it will never
2955cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
2965cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
2975cee9645SThomas Gleixner  * early.
2985cee9645SThomas Gleixner  */
2995cee9645SThomas Gleixner unsigned long __round_jiffies_up(unsigned long j, int cpu)
3005cee9645SThomas Gleixner {
3015cee9645SThomas Gleixner 	return round_jiffies_common(j, cpu, true);
3025cee9645SThomas Gleixner }
3035cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_up);
3045cee9645SThomas Gleixner 
3055cee9645SThomas Gleixner /**
3065cee9645SThomas Gleixner  * __round_jiffies_up_relative - function to round jiffies up to a full second
3075cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
3085cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
3095cee9645SThomas Gleixner  *
3105cee9645SThomas Gleixner  * This is the same as __round_jiffies_relative() except that it will never
3115cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
3125cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
3135cee9645SThomas Gleixner  * early.
3145cee9645SThomas Gleixner  */
3155cee9645SThomas Gleixner unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
3165cee9645SThomas Gleixner {
3175cee9645SThomas Gleixner 	unsigned long j0 = jiffies;
3185cee9645SThomas Gleixner 
3195cee9645SThomas Gleixner 	/* Use j0 because jiffies might change while we run */
3205cee9645SThomas Gleixner 	return round_jiffies_common(j + j0, cpu, true) - j0;
3215cee9645SThomas Gleixner }
3225cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
3235cee9645SThomas Gleixner 
3245cee9645SThomas Gleixner /**
3255cee9645SThomas Gleixner  * round_jiffies_up - function to round jiffies up to a full second
3265cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
3275cee9645SThomas Gleixner  *
3285cee9645SThomas Gleixner  * This is the same as round_jiffies() except that it will never
3295cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
3305cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
3315cee9645SThomas Gleixner  * early.
3325cee9645SThomas Gleixner  */
3335cee9645SThomas Gleixner unsigned long round_jiffies_up(unsigned long j)
3345cee9645SThomas Gleixner {
3355cee9645SThomas Gleixner 	return round_jiffies_common(j, raw_smp_processor_id(), true);
3365cee9645SThomas Gleixner }
3375cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_up);
3385cee9645SThomas Gleixner 
3395cee9645SThomas Gleixner /**
3405cee9645SThomas Gleixner  * round_jiffies_up_relative - function to round jiffies up to a full second
3415cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
3425cee9645SThomas Gleixner  *
3435cee9645SThomas Gleixner  * This is the same as round_jiffies_relative() except that it will never
3445cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
3455cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
3465cee9645SThomas Gleixner  * early.
3475cee9645SThomas Gleixner  */
3485cee9645SThomas Gleixner unsigned long round_jiffies_up_relative(unsigned long j)
3495cee9645SThomas Gleixner {
3505cee9645SThomas Gleixner 	return __round_jiffies_up_relative(j, raw_smp_processor_id());
3515cee9645SThomas Gleixner }
3525cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
3535cee9645SThomas Gleixner 
3545cee9645SThomas Gleixner /**
3555cee9645SThomas Gleixner  * set_timer_slack - set the allowed slack for a timer
3565cee9645SThomas Gleixner  * @timer: the timer to be modified
3575cee9645SThomas Gleixner  * @slack_hz: the amount of time (in jiffies) allowed for rounding
3585cee9645SThomas Gleixner  *
3595cee9645SThomas Gleixner  * Set the amount of time, in jiffies, that a certain timer has
3605cee9645SThomas Gleixner  * in terms of slack. By setting this value, the timer subsystem
3615cee9645SThomas Gleixner  * will schedule the actual timer somewhere between
3625cee9645SThomas Gleixner  * the time mod_timer() asks for, and that time plus the slack.
3635cee9645SThomas Gleixner  *
3645cee9645SThomas Gleixner  * By setting the slack to -1, a percentage of the delay is used
3655cee9645SThomas Gleixner  * instead.
3665cee9645SThomas Gleixner  */
3675cee9645SThomas Gleixner void set_timer_slack(struct timer_list *timer, int slack_hz)
3685cee9645SThomas Gleixner {
3695cee9645SThomas Gleixner 	timer->slack = slack_hz;
3705cee9645SThomas Gleixner }
3715cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(set_timer_slack);
3725cee9645SThomas Gleixner 
3735cee9645SThomas Gleixner static void
3745cee9645SThomas Gleixner __internal_add_timer(struct tvec_base *base, struct timer_list *timer)
3755cee9645SThomas Gleixner {
3765cee9645SThomas Gleixner 	unsigned long expires = timer->expires;
3775cee9645SThomas Gleixner 	unsigned long idx = expires - base->timer_jiffies;
3781dabbcecSThomas Gleixner 	struct hlist_head *vec;
3795cee9645SThomas Gleixner 
3805cee9645SThomas Gleixner 	if (idx < TVR_SIZE) {
3815cee9645SThomas Gleixner 		int i = expires & TVR_MASK;
3825cee9645SThomas Gleixner 		vec = base->tv1.vec + i;
3835cee9645SThomas Gleixner 	} else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
3845cee9645SThomas Gleixner 		int i = (expires >> TVR_BITS) & TVN_MASK;
3855cee9645SThomas Gleixner 		vec = base->tv2.vec + i;
3865cee9645SThomas Gleixner 	} else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
3875cee9645SThomas Gleixner 		int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
3885cee9645SThomas Gleixner 		vec = base->tv3.vec + i;
3895cee9645SThomas Gleixner 	} else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
3905cee9645SThomas Gleixner 		int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
3915cee9645SThomas Gleixner 		vec = base->tv4.vec + i;
3925cee9645SThomas Gleixner 	} else if ((signed long) idx < 0) {
3935cee9645SThomas Gleixner 		/*
3945cee9645SThomas Gleixner 		 * Can happen if you add a timer with expires == jiffies,
3955cee9645SThomas Gleixner 		 * or you set a timer to go off in the past
3965cee9645SThomas Gleixner 		 */
3975cee9645SThomas Gleixner 		vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
3985cee9645SThomas Gleixner 	} else {
3995cee9645SThomas Gleixner 		int i;
4005cee9645SThomas Gleixner 		/* If the timeout is larger than MAX_TVAL (on 64-bit
4015cee9645SThomas Gleixner 		 * architectures or with CONFIG_BASE_SMALL=1) then we
4025cee9645SThomas Gleixner 		 * use the maximum timeout.
4035cee9645SThomas Gleixner 		 */
4045cee9645SThomas Gleixner 		if (idx > MAX_TVAL) {
4055cee9645SThomas Gleixner 			idx = MAX_TVAL;
4065cee9645SThomas Gleixner 			expires = idx + base->timer_jiffies;
4075cee9645SThomas Gleixner 		}
4085cee9645SThomas Gleixner 		i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
4095cee9645SThomas Gleixner 		vec = base->tv5.vec + i;
4105cee9645SThomas Gleixner 	}
4111bd04bf6SThomas Gleixner 
4121dabbcecSThomas Gleixner 	hlist_add_head(&timer->entry, vec);
4135cee9645SThomas Gleixner }
4145cee9645SThomas Gleixner 
4155cee9645SThomas Gleixner static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
4165cee9645SThomas Gleixner {
4173bb475a3SThomas Gleixner 	/* Advance base->jiffies, if the base is empty */
4183bb475a3SThomas Gleixner 	if (!base->all_timers++)
4193bb475a3SThomas Gleixner 		base->timer_jiffies = jiffies;
4203bb475a3SThomas Gleixner 
4215cee9645SThomas Gleixner 	__internal_add_timer(base, timer);
4225cee9645SThomas Gleixner 	/*
4235cee9645SThomas Gleixner 	 * Update base->active_timers and base->next_timer
4245cee9645SThomas Gleixner 	 */
4250eeda71bSThomas Gleixner 	if (!(timer->flags & TIMER_DEFERRABLE)) {
4265cee9645SThomas Gleixner 		if (!base->active_timers++ ||
4275cee9645SThomas Gleixner 		    time_before(timer->expires, base->next_timer))
4285cee9645SThomas Gleixner 			base->next_timer = timer->expires;
4295cee9645SThomas Gleixner 	}
4309f6d9baaSViresh Kumar 
4319f6d9baaSViresh Kumar 	/*
4329f6d9baaSViresh Kumar 	 * Check whether the other CPU is in dynticks mode and needs
4339f6d9baaSViresh Kumar 	 * to be triggered to reevaluate the timer wheel.
4349f6d9baaSViresh Kumar 	 * We are protected against the other CPU fiddling
4359f6d9baaSViresh Kumar 	 * with the timer by holding the timer base lock. This also
4369f6d9baaSViresh Kumar 	 * makes sure that a CPU on the way to stop its tick can not
4379f6d9baaSViresh Kumar 	 * evaluate the timer wheel.
4389f6d9baaSViresh Kumar 	 *
4399f6d9baaSViresh Kumar 	 * Spare the IPI for deferrable timers on idle targets though.
4409f6d9baaSViresh Kumar 	 * The next busy ticks will take care of it. Except full dynticks
4419f6d9baaSViresh Kumar 	 * require special care against races with idle_cpu(), lets deal
4429f6d9baaSViresh Kumar 	 * with that later.
4439f6d9baaSViresh Kumar 	 */
444683be13aSThomas Gleixner 	if (base->nohz_active) {
445683be13aSThomas Gleixner 		if (!(timer->flags & TIMER_DEFERRABLE) ||
446683be13aSThomas Gleixner 		    tick_nohz_full_cpu(base->cpu))
4479f6d9baaSViresh Kumar 			wake_up_nohz_cpu(base->cpu);
4485cee9645SThomas Gleixner 	}
449683be13aSThomas Gleixner }
4505cee9645SThomas Gleixner 
4515cee9645SThomas Gleixner #ifdef CONFIG_TIMER_STATS
4525cee9645SThomas Gleixner void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
4535cee9645SThomas Gleixner {
4545cee9645SThomas Gleixner 	if (timer->start_site)
4555cee9645SThomas Gleixner 		return;
4565cee9645SThomas Gleixner 
4575cee9645SThomas Gleixner 	timer->start_site = addr;
4585cee9645SThomas Gleixner 	memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
4595cee9645SThomas Gleixner 	timer->start_pid = current->pid;
4605cee9645SThomas Gleixner }
4615cee9645SThomas Gleixner 
4625cee9645SThomas Gleixner static void timer_stats_account_timer(struct timer_list *timer)
4635cee9645SThomas Gleixner {
464*3ed769bdSDmitry Vyukov 	void *site;
465*3ed769bdSDmitry Vyukov 
466*3ed769bdSDmitry Vyukov 	/*
467*3ed769bdSDmitry Vyukov 	 * start_site can be concurrently reset by
468*3ed769bdSDmitry Vyukov 	 * timer_stats_timer_clear_start_info()
469*3ed769bdSDmitry Vyukov 	 */
470*3ed769bdSDmitry Vyukov 	site = READ_ONCE(timer->start_site);
471*3ed769bdSDmitry Vyukov 	if (likely(!site))
4725cee9645SThomas Gleixner 		return;
4735cee9645SThomas Gleixner 
474*3ed769bdSDmitry Vyukov 	timer_stats_update_stats(timer, timer->start_pid, site,
475c74441a1SThomas Gleixner 				 timer->function, timer->start_comm,
476c74441a1SThomas Gleixner 				 timer->flags);
4775cee9645SThomas Gleixner }
4785cee9645SThomas Gleixner 
4795cee9645SThomas Gleixner #else
4805cee9645SThomas Gleixner static void timer_stats_account_timer(struct timer_list *timer) {}
4815cee9645SThomas Gleixner #endif
4825cee9645SThomas Gleixner 
4835cee9645SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
4845cee9645SThomas Gleixner 
4855cee9645SThomas Gleixner static struct debug_obj_descr timer_debug_descr;
4865cee9645SThomas Gleixner 
4875cee9645SThomas Gleixner static void *timer_debug_hint(void *addr)
4885cee9645SThomas Gleixner {
4895cee9645SThomas Gleixner 	return ((struct timer_list *) addr)->function;
4905cee9645SThomas Gleixner }
4915cee9645SThomas Gleixner 
4925cee9645SThomas Gleixner /*
4935cee9645SThomas Gleixner  * fixup_init is called when:
4945cee9645SThomas Gleixner  * - an active object is initialized
4955cee9645SThomas Gleixner  */
4965cee9645SThomas Gleixner static int timer_fixup_init(void *addr, enum debug_obj_state state)
4975cee9645SThomas Gleixner {
4985cee9645SThomas Gleixner 	struct timer_list *timer = addr;
4995cee9645SThomas Gleixner 
5005cee9645SThomas Gleixner 	switch (state) {
5015cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
5025cee9645SThomas Gleixner 		del_timer_sync(timer);
5035cee9645SThomas Gleixner 		debug_object_init(timer, &timer_debug_descr);
5045cee9645SThomas Gleixner 		return 1;
5055cee9645SThomas Gleixner 	default:
5065cee9645SThomas Gleixner 		return 0;
5075cee9645SThomas Gleixner 	}
5085cee9645SThomas Gleixner }
5095cee9645SThomas Gleixner 
5105cee9645SThomas Gleixner /* Stub timer callback for improperly used timers. */
5115cee9645SThomas Gleixner static void stub_timer(unsigned long data)
5125cee9645SThomas Gleixner {
5135cee9645SThomas Gleixner 	WARN_ON(1);
5145cee9645SThomas Gleixner }
5155cee9645SThomas Gleixner 
5165cee9645SThomas Gleixner /*
5175cee9645SThomas Gleixner  * fixup_activate is called when:
5185cee9645SThomas Gleixner  * - an active object is activated
5195cee9645SThomas Gleixner  * - an unknown object is activated (might be a statically initialized object)
5205cee9645SThomas Gleixner  */
5215cee9645SThomas Gleixner static int timer_fixup_activate(void *addr, enum debug_obj_state state)
5225cee9645SThomas Gleixner {
5235cee9645SThomas Gleixner 	struct timer_list *timer = addr;
5245cee9645SThomas Gleixner 
5255cee9645SThomas Gleixner 	switch (state) {
5265cee9645SThomas Gleixner 
5275cee9645SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
5285cee9645SThomas Gleixner 		/*
5295cee9645SThomas Gleixner 		 * This is not really a fixup. The timer was
5305cee9645SThomas Gleixner 		 * statically initialized. We just make sure that it
5315cee9645SThomas Gleixner 		 * is tracked in the object tracker.
5325cee9645SThomas Gleixner 		 */
5331dabbcecSThomas Gleixner 		if (timer->entry.pprev == NULL &&
5341dabbcecSThomas Gleixner 		    timer->entry.next == TIMER_ENTRY_STATIC) {
5355cee9645SThomas Gleixner 			debug_object_init(timer, &timer_debug_descr);
5365cee9645SThomas Gleixner 			debug_object_activate(timer, &timer_debug_descr);
5375cee9645SThomas Gleixner 			return 0;
5385cee9645SThomas Gleixner 		} else {
5395cee9645SThomas Gleixner 			setup_timer(timer, stub_timer, 0);
5405cee9645SThomas Gleixner 			return 1;
5415cee9645SThomas Gleixner 		}
5425cee9645SThomas Gleixner 		return 0;
5435cee9645SThomas Gleixner 
5445cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
5455cee9645SThomas Gleixner 		WARN_ON(1);
5465cee9645SThomas Gleixner 
5475cee9645SThomas Gleixner 	default:
5485cee9645SThomas Gleixner 		return 0;
5495cee9645SThomas Gleixner 	}
5505cee9645SThomas Gleixner }
5515cee9645SThomas Gleixner 
5525cee9645SThomas Gleixner /*
5535cee9645SThomas Gleixner  * fixup_free is called when:
5545cee9645SThomas Gleixner  * - an active object is freed
5555cee9645SThomas Gleixner  */
5565cee9645SThomas Gleixner static int timer_fixup_free(void *addr, enum debug_obj_state state)
5575cee9645SThomas Gleixner {
5585cee9645SThomas Gleixner 	struct timer_list *timer = addr;
5595cee9645SThomas Gleixner 
5605cee9645SThomas Gleixner 	switch (state) {
5615cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
5625cee9645SThomas Gleixner 		del_timer_sync(timer);
5635cee9645SThomas Gleixner 		debug_object_free(timer, &timer_debug_descr);
5645cee9645SThomas Gleixner 		return 1;
5655cee9645SThomas Gleixner 	default:
5665cee9645SThomas Gleixner 		return 0;
5675cee9645SThomas Gleixner 	}
5685cee9645SThomas Gleixner }
5695cee9645SThomas Gleixner 
5705cee9645SThomas Gleixner /*
5715cee9645SThomas Gleixner  * fixup_assert_init is called when:
5725cee9645SThomas Gleixner  * - an untracked/uninit-ed object is found
5735cee9645SThomas Gleixner  */
5745cee9645SThomas Gleixner static int timer_fixup_assert_init(void *addr, enum debug_obj_state state)
5755cee9645SThomas Gleixner {
5765cee9645SThomas Gleixner 	struct timer_list *timer = addr;
5775cee9645SThomas Gleixner 
5785cee9645SThomas Gleixner 	switch (state) {
5795cee9645SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
5801dabbcecSThomas Gleixner 		if (timer->entry.next == TIMER_ENTRY_STATIC) {
5815cee9645SThomas Gleixner 			/*
5825cee9645SThomas Gleixner 			 * This is not really a fixup. The timer was
5835cee9645SThomas Gleixner 			 * statically initialized. We just make sure that it
5845cee9645SThomas Gleixner 			 * is tracked in the object tracker.
5855cee9645SThomas Gleixner 			 */
5865cee9645SThomas Gleixner 			debug_object_init(timer, &timer_debug_descr);
5875cee9645SThomas Gleixner 			return 0;
5885cee9645SThomas Gleixner 		} else {
5895cee9645SThomas Gleixner 			setup_timer(timer, stub_timer, 0);
5905cee9645SThomas Gleixner 			return 1;
5915cee9645SThomas Gleixner 		}
5925cee9645SThomas Gleixner 	default:
5935cee9645SThomas Gleixner 		return 0;
5945cee9645SThomas Gleixner 	}
5955cee9645SThomas Gleixner }
5965cee9645SThomas Gleixner 
5975cee9645SThomas Gleixner static struct debug_obj_descr timer_debug_descr = {
5985cee9645SThomas Gleixner 	.name			= "timer_list",
5995cee9645SThomas Gleixner 	.debug_hint		= timer_debug_hint,
6005cee9645SThomas Gleixner 	.fixup_init		= timer_fixup_init,
6015cee9645SThomas Gleixner 	.fixup_activate		= timer_fixup_activate,
6025cee9645SThomas Gleixner 	.fixup_free		= timer_fixup_free,
6035cee9645SThomas Gleixner 	.fixup_assert_init	= timer_fixup_assert_init,
6045cee9645SThomas Gleixner };
6055cee9645SThomas Gleixner 
6065cee9645SThomas Gleixner static inline void debug_timer_init(struct timer_list *timer)
6075cee9645SThomas Gleixner {
6085cee9645SThomas Gleixner 	debug_object_init(timer, &timer_debug_descr);
6095cee9645SThomas Gleixner }
6105cee9645SThomas Gleixner 
6115cee9645SThomas Gleixner static inline void debug_timer_activate(struct timer_list *timer)
6125cee9645SThomas Gleixner {
6135cee9645SThomas Gleixner 	debug_object_activate(timer, &timer_debug_descr);
6145cee9645SThomas Gleixner }
6155cee9645SThomas Gleixner 
6165cee9645SThomas Gleixner static inline void debug_timer_deactivate(struct timer_list *timer)
6175cee9645SThomas Gleixner {
6185cee9645SThomas Gleixner 	debug_object_deactivate(timer, &timer_debug_descr);
6195cee9645SThomas Gleixner }
6205cee9645SThomas Gleixner 
6215cee9645SThomas Gleixner static inline void debug_timer_free(struct timer_list *timer)
6225cee9645SThomas Gleixner {
6235cee9645SThomas Gleixner 	debug_object_free(timer, &timer_debug_descr);
6245cee9645SThomas Gleixner }
6255cee9645SThomas Gleixner 
6265cee9645SThomas Gleixner static inline void debug_timer_assert_init(struct timer_list *timer)
6275cee9645SThomas Gleixner {
6285cee9645SThomas Gleixner 	debug_object_assert_init(timer, &timer_debug_descr);
6295cee9645SThomas Gleixner }
6305cee9645SThomas Gleixner 
6315cee9645SThomas Gleixner static void do_init_timer(struct timer_list *timer, unsigned int flags,
6325cee9645SThomas Gleixner 			  const char *name, struct lock_class_key *key);
6335cee9645SThomas Gleixner 
6345cee9645SThomas Gleixner void init_timer_on_stack_key(struct timer_list *timer, unsigned int flags,
6355cee9645SThomas Gleixner 			     const char *name, struct lock_class_key *key)
6365cee9645SThomas Gleixner {
6375cee9645SThomas Gleixner 	debug_object_init_on_stack(timer, &timer_debug_descr);
6385cee9645SThomas Gleixner 	do_init_timer(timer, flags, name, key);
6395cee9645SThomas Gleixner }
6405cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
6415cee9645SThomas Gleixner 
6425cee9645SThomas Gleixner void destroy_timer_on_stack(struct timer_list *timer)
6435cee9645SThomas Gleixner {
6445cee9645SThomas Gleixner 	debug_object_free(timer, &timer_debug_descr);
6455cee9645SThomas Gleixner }
6465cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
6475cee9645SThomas Gleixner 
6485cee9645SThomas Gleixner #else
6495cee9645SThomas Gleixner static inline void debug_timer_init(struct timer_list *timer) { }
6505cee9645SThomas Gleixner static inline void debug_timer_activate(struct timer_list *timer) { }
6515cee9645SThomas Gleixner static inline void debug_timer_deactivate(struct timer_list *timer) { }
6525cee9645SThomas Gleixner static inline void debug_timer_assert_init(struct timer_list *timer) { }
6535cee9645SThomas Gleixner #endif
6545cee9645SThomas Gleixner 
6555cee9645SThomas Gleixner static inline void debug_init(struct timer_list *timer)
6565cee9645SThomas Gleixner {
6575cee9645SThomas Gleixner 	debug_timer_init(timer);
6585cee9645SThomas Gleixner 	trace_timer_init(timer);
6595cee9645SThomas Gleixner }
6605cee9645SThomas Gleixner 
6615cee9645SThomas Gleixner static inline void
6625cee9645SThomas Gleixner debug_activate(struct timer_list *timer, unsigned long expires)
6635cee9645SThomas Gleixner {
6645cee9645SThomas Gleixner 	debug_timer_activate(timer);
6650eeda71bSThomas Gleixner 	trace_timer_start(timer, expires, timer->flags);
6665cee9645SThomas Gleixner }
6675cee9645SThomas Gleixner 
6685cee9645SThomas Gleixner static inline void debug_deactivate(struct timer_list *timer)
6695cee9645SThomas Gleixner {
6705cee9645SThomas Gleixner 	debug_timer_deactivate(timer);
6715cee9645SThomas Gleixner 	trace_timer_cancel(timer);
6725cee9645SThomas Gleixner }
6735cee9645SThomas Gleixner 
6745cee9645SThomas Gleixner static inline void debug_assert_init(struct timer_list *timer)
6755cee9645SThomas Gleixner {
6765cee9645SThomas Gleixner 	debug_timer_assert_init(timer);
6775cee9645SThomas Gleixner }
6785cee9645SThomas Gleixner 
6795cee9645SThomas Gleixner static void do_init_timer(struct timer_list *timer, unsigned int flags,
6805cee9645SThomas Gleixner 			  const char *name, struct lock_class_key *key)
6815cee9645SThomas Gleixner {
6821dabbcecSThomas Gleixner 	timer->entry.pprev = NULL;
6830eeda71bSThomas Gleixner 	timer->flags = flags | raw_smp_processor_id();
6845cee9645SThomas Gleixner 	timer->slack = -1;
6855cee9645SThomas Gleixner #ifdef CONFIG_TIMER_STATS
6865cee9645SThomas Gleixner 	timer->start_site = NULL;
6875cee9645SThomas Gleixner 	timer->start_pid = -1;
6885cee9645SThomas Gleixner 	memset(timer->start_comm, 0, TASK_COMM_LEN);
6895cee9645SThomas Gleixner #endif
6905cee9645SThomas Gleixner 	lockdep_init_map(&timer->lockdep_map, name, key, 0);
6915cee9645SThomas Gleixner }
6925cee9645SThomas Gleixner 
6935cee9645SThomas Gleixner /**
6945cee9645SThomas Gleixner  * init_timer_key - initialize a timer
6955cee9645SThomas Gleixner  * @timer: the timer to be initialized
6965cee9645SThomas Gleixner  * @flags: timer flags
6975cee9645SThomas Gleixner  * @name: name of the timer
6985cee9645SThomas Gleixner  * @key: lockdep class key of the fake lock used for tracking timer
6995cee9645SThomas Gleixner  *       sync lock dependencies
7005cee9645SThomas Gleixner  *
7015cee9645SThomas Gleixner  * init_timer_key() must be done to a timer prior calling *any* of the
7025cee9645SThomas Gleixner  * other timer functions.
7035cee9645SThomas Gleixner  */
7045cee9645SThomas Gleixner void init_timer_key(struct timer_list *timer, unsigned int flags,
7055cee9645SThomas Gleixner 		    const char *name, struct lock_class_key *key)
7065cee9645SThomas Gleixner {
7075cee9645SThomas Gleixner 	debug_init(timer);
7085cee9645SThomas Gleixner 	do_init_timer(timer, flags, name, key);
7095cee9645SThomas Gleixner }
7105cee9645SThomas Gleixner EXPORT_SYMBOL(init_timer_key);
7115cee9645SThomas Gleixner 
7125cee9645SThomas Gleixner static inline void detach_timer(struct timer_list *timer, bool clear_pending)
7135cee9645SThomas Gleixner {
7141dabbcecSThomas Gleixner 	struct hlist_node *entry = &timer->entry;
7155cee9645SThomas Gleixner 
7165cee9645SThomas Gleixner 	debug_deactivate(timer);
7175cee9645SThomas Gleixner 
7181dabbcecSThomas Gleixner 	__hlist_del(entry);
7195cee9645SThomas Gleixner 	if (clear_pending)
7201dabbcecSThomas Gleixner 		entry->pprev = NULL;
7211dabbcecSThomas Gleixner 	entry->next = LIST_POISON2;
7225cee9645SThomas Gleixner }
7235cee9645SThomas Gleixner 
7245cee9645SThomas Gleixner static inline void
7255cee9645SThomas Gleixner detach_expired_timer(struct timer_list *timer, struct tvec_base *base)
7265cee9645SThomas Gleixner {
7275cee9645SThomas Gleixner 	detach_timer(timer, true);
7280eeda71bSThomas Gleixner 	if (!(timer->flags & TIMER_DEFERRABLE))
7295cee9645SThomas Gleixner 		base->active_timers--;
7305cee9645SThomas Gleixner 	base->all_timers--;
7315cee9645SThomas Gleixner }
7325cee9645SThomas Gleixner 
7335cee9645SThomas Gleixner static int detach_if_pending(struct timer_list *timer, struct tvec_base *base,
7345cee9645SThomas Gleixner 			     bool clear_pending)
7355cee9645SThomas Gleixner {
7365cee9645SThomas Gleixner 	if (!timer_pending(timer))
7375cee9645SThomas Gleixner 		return 0;
7385cee9645SThomas Gleixner 
7395cee9645SThomas Gleixner 	detach_timer(timer, clear_pending);
7400eeda71bSThomas Gleixner 	if (!(timer->flags & TIMER_DEFERRABLE)) {
7415cee9645SThomas Gleixner 		base->active_timers--;
7425cee9645SThomas Gleixner 		if (timer->expires == base->next_timer)
7435cee9645SThomas Gleixner 			base->next_timer = base->timer_jiffies;
7445cee9645SThomas Gleixner 	}
7453bb475a3SThomas Gleixner 	/* If this was the last timer, advance base->jiffies */
7463bb475a3SThomas Gleixner 	if (!--base->all_timers)
7473bb475a3SThomas Gleixner 		base->timer_jiffies = jiffies;
7485cee9645SThomas Gleixner 	return 1;
7495cee9645SThomas Gleixner }
7505cee9645SThomas Gleixner 
7515cee9645SThomas Gleixner /*
7525cee9645SThomas Gleixner  * We are using hashed locking: holding per_cpu(tvec_bases).lock
7535cee9645SThomas Gleixner  * means that all timers which are tied to this base via timer->base are
7545cee9645SThomas Gleixner  * locked, and the base itself is locked too.
7555cee9645SThomas Gleixner  *
7565cee9645SThomas Gleixner  * So __run_timers/migrate_timers can safely modify all timers which could
7575cee9645SThomas Gleixner  * be found on ->tvX lists.
7585cee9645SThomas Gleixner  *
7590eeda71bSThomas Gleixner  * When the timer's base is locked and removed from the list, the
7600eeda71bSThomas Gleixner  * TIMER_MIGRATING flag is set, FIXME
7615cee9645SThomas Gleixner  */
7625cee9645SThomas Gleixner static struct tvec_base *lock_timer_base(struct timer_list *timer,
7635cee9645SThomas Gleixner 					unsigned long *flags)
7645cee9645SThomas Gleixner 	__acquires(timer->base->lock)
7655cee9645SThomas Gleixner {
7660eeda71bSThomas Gleixner 	for (;;) {
7670eeda71bSThomas Gleixner 		u32 tf = timer->flags;
7685cee9645SThomas Gleixner 		struct tvec_base *base;
7695cee9645SThomas Gleixner 
7700eeda71bSThomas Gleixner 		if (!(tf & TIMER_MIGRATING)) {
7710eeda71bSThomas Gleixner 			base = per_cpu_ptr(&tvec_bases, tf & TIMER_CPUMASK);
7725cee9645SThomas Gleixner 			spin_lock_irqsave(&base->lock, *flags);
7730eeda71bSThomas Gleixner 			if (timer->flags == tf)
7745cee9645SThomas Gleixner 				return base;
7755cee9645SThomas Gleixner 			spin_unlock_irqrestore(&base->lock, *flags);
7765cee9645SThomas Gleixner 		}
7775cee9645SThomas Gleixner 		cpu_relax();
7785cee9645SThomas Gleixner 	}
7795cee9645SThomas Gleixner }
7805cee9645SThomas Gleixner 
7815cee9645SThomas Gleixner static inline int
7825cee9645SThomas Gleixner __mod_timer(struct timer_list *timer, unsigned long expires,
7835cee9645SThomas Gleixner 	    bool pending_only, int pinned)
7845cee9645SThomas Gleixner {
7855cee9645SThomas Gleixner 	struct tvec_base *base, *new_base;
7865cee9645SThomas Gleixner 	unsigned long flags;
787bc7a34b8SThomas Gleixner 	int ret = 0;
7885cee9645SThomas Gleixner 
7895cee9645SThomas Gleixner 	timer_stats_timer_set_start_info(timer);
7905cee9645SThomas Gleixner 	BUG_ON(!timer->function);
7915cee9645SThomas Gleixner 
7925cee9645SThomas Gleixner 	base = lock_timer_base(timer, &flags);
7935cee9645SThomas Gleixner 
7945cee9645SThomas Gleixner 	ret = detach_if_pending(timer, base, false);
7955cee9645SThomas Gleixner 	if (!ret && pending_only)
7965cee9645SThomas Gleixner 		goto out_unlock;
7975cee9645SThomas Gleixner 
7985cee9645SThomas Gleixner 	debug_activate(timer, expires);
7995cee9645SThomas Gleixner 
800bc7a34b8SThomas Gleixner 	new_base = get_target_base(base, pinned);
8015cee9645SThomas Gleixner 
8025cee9645SThomas Gleixner 	if (base != new_base) {
8035cee9645SThomas Gleixner 		/*
8045cee9645SThomas Gleixner 		 * We are trying to schedule the timer on the local CPU.
8055cee9645SThomas Gleixner 		 * However we can't change timer's base while it is running,
8065cee9645SThomas Gleixner 		 * otherwise del_timer_sync() can't detect that the timer's
8075cee9645SThomas Gleixner 		 * handler yet has not finished. This also guarantees that
8085cee9645SThomas Gleixner 		 * the timer is serialized wrt itself.
8095cee9645SThomas Gleixner 		 */
8105cee9645SThomas Gleixner 		if (likely(base->running_timer != timer)) {
8115cee9645SThomas Gleixner 			/* See the comment in lock_timer_base() */
8120eeda71bSThomas Gleixner 			timer->flags |= TIMER_MIGRATING;
8130eeda71bSThomas Gleixner 
8145cee9645SThomas Gleixner 			spin_unlock(&base->lock);
8155cee9645SThomas Gleixner 			base = new_base;
8165cee9645SThomas Gleixner 			spin_lock(&base->lock);
817d0023a14SEric Dumazet 			WRITE_ONCE(timer->flags,
818d0023a14SEric Dumazet 				   (timer->flags & ~TIMER_BASEMASK) | base->cpu);
8195cee9645SThomas Gleixner 		}
8205cee9645SThomas Gleixner 	}
8215cee9645SThomas Gleixner 
8225cee9645SThomas Gleixner 	timer->expires = expires;
8235cee9645SThomas Gleixner 	internal_add_timer(base, timer);
8245cee9645SThomas Gleixner 
8255cee9645SThomas Gleixner out_unlock:
8265cee9645SThomas Gleixner 	spin_unlock_irqrestore(&base->lock, flags);
8275cee9645SThomas Gleixner 
8285cee9645SThomas Gleixner 	return ret;
8295cee9645SThomas Gleixner }
8305cee9645SThomas Gleixner 
8315cee9645SThomas Gleixner /**
8325cee9645SThomas Gleixner  * mod_timer_pending - modify a pending timer's timeout
8335cee9645SThomas Gleixner  * @timer: the pending timer to be modified
8345cee9645SThomas Gleixner  * @expires: new timeout in jiffies
8355cee9645SThomas Gleixner  *
8365cee9645SThomas Gleixner  * mod_timer_pending() is the same for pending timers as mod_timer(),
8375cee9645SThomas Gleixner  * but will not re-activate and modify already deleted timers.
8385cee9645SThomas Gleixner  *
8395cee9645SThomas Gleixner  * It is useful for unserialized use of timers.
8405cee9645SThomas Gleixner  */
8415cee9645SThomas Gleixner int mod_timer_pending(struct timer_list *timer, unsigned long expires)
8425cee9645SThomas Gleixner {
8435cee9645SThomas Gleixner 	return __mod_timer(timer, expires, true, TIMER_NOT_PINNED);
8445cee9645SThomas Gleixner }
8455cee9645SThomas Gleixner EXPORT_SYMBOL(mod_timer_pending);
8465cee9645SThomas Gleixner 
8475cee9645SThomas Gleixner /*
8485cee9645SThomas Gleixner  * Decide where to put the timer while taking the slack into account
8495cee9645SThomas Gleixner  *
8505cee9645SThomas Gleixner  * Algorithm:
8515cee9645SThomas Gleixner  *   1) calculate the maximum (absolute) time
8525cee9645SThomas Gleixner  *   2) calculate the highest bit where the expires and new max are different
8535cee9645SThomas Gleixner  *   3) use this bit to make a mask
8545cee9645SThomas Gleixner  *   4) use the bitmask to round down the maximum time, so that all last
8555cee9645SThomas Gleixner  *      bits are zeros
8565cee9645SThomas Gleixner  */
8575cee9645SThomas Gleixner static inline
8585cee9645SThomas Gleixner unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
8595cee9645SThomas Gleixner {
8605cee9645SThomas Gleixner 	unsigned long expires_limit, mask;
8615cee9645SThomas Gleixner 	int bit;
8625cee9645SThomas Gleixner 
8635cee9645SThomas Gleixner 	if (timer->slack >= 0) {
8645cee9645SThomas Gleixner 		expires_limit = expires + timer->slack;
8655cee9645SThomas Gleixner 	} else {
8665cee9645SThomas Gleixner 		long delta = expires - jiffies;
8675cee9645SThomas Gleixner 
8685cee9645SThomas Gleixner 		if (delta < 256)
8695cee9645SThomas Gleixner 			return expires;
8705cee9645SThomas Gleixner 
8715cee9645SThomas Gleixner 		expires_limit = expires + delta / 256;
8725cee9645SThomas Gleixner 	}
8735cee9645SThomas Gleixner 	mask = expires ^ expires_limit;
8745cee9645SThomas Gleixner 	if (mask == 0)
8755cee9645SThomas Gleixner 		return expires;
8765cee9645SThomas Gleixner 
8775cee9645SThomas Gleixner 	bit = find_last_bit(&mask, BITS_PER_LONG);
8785cee9645SThomas Gleixner 
8795cee9645SThomas Gleixner 	mask = (1UL << bit) - 1;
8805cee9645SThomas Gleixner 
8815cee9645SThomas Gleixner 	expires_limit = expires_limit & ~(mask);
8825cee9645SThomas Gleixner 
8835cee9645SThomas Gleixner 	return expires_limit;
8845cee9645SThomas Gleixner }
8855cee9645SThomas Gleixner 
8865cee9645SThomas Gleixner /**
8875cee9645SThomas Gleixner  * mod_timer - modify a timer's timeout
8885cee9645SThomas Gleixner  * @timer: the timer to be modified
8895cee9645SThomas Gleixner  * @expires: new timeout in jiffies
8905cee9645SThomas Gleixner  *
8915cee9645SThomas Gleixner  * mod_timer() is a more efficient way to update the expire field of an
8925cee9645SThomas Gleixner  * active timer (if the timer is inactive it will be activated)
8935cee9645SThomas Gleixner  *
8945cee9645SThomas Gleixner  * mod_timer(timer, expires) is equivalent to:
8955cee9645SThomas Gleixner  *
8965cee9645SThomas Gleixner  *     del_timer(timer); timer->expires = expires; add_timer(timer);
8975cee9645SThomas Gleixner  *
8985cee9645SThomas Gleixner  * Note that if there are multiple unserialized concurrent users of the
8995cee9645SThomas Gleixner  * same timer, then mod_timer() is the only safe way to modify the timeout,
9005cee9645SThomas Gleixner  * since add_timer() cannot modify an already running timer.
9015cee9645SThomas Gleixner  *
9025cee9645SThomas Gleixner  * The function returns whether it has modified a pending timer or not.
9035cee9645SThomas Gleixner  * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
9045cee9645SThomas Gleixner  * active timer returns 1.)
9055cee9645SThomas Gleixner  */
9065cee9645SThomas Gleixner int mod_timer(struct timer_list *timer, unsigned long expires)
9075cee9645SThomas Gleixner {
9085cee9645SThomas Gleixner 	expires = apply_slack(timer, expires);
9095cee9645SThomas Gleixner 
9105cee9645SThomas Gleixner 	/*
9115cee9645SThomas Gleixner 	 * This is a common optimization triggered by the
9125cee9645SThomas Gleixner 	 * networking code - if the timer is re-modified
9135cee9645SThomas Gleixner 	 * to be the same thing then just return:
9145cee9645SThomas Gleixner 	 */
9155cee9645SThomas Gleixner 	if (timer_pending(timer) && timer->expires == expires)
9165cee9645SThomas Gleixner 		return 1;
9175cee9645SThomas Gleixner 
9185cee9645SThomas Gleixner 	return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
9195cee9645SThomas Gleixner }
9205cee9645SThomas Gleixner EXPORT_SYMBOL(mod_timer);
9215cee9645SThomas Gleixner 
9225cee9645SThomas Gleixner /**
9235cee9645SThomas Gleixner  * mod_timer_pinned - modify a timer's timeout
9245cee9645SThomas Gleixner  * @timer: the timer to be modified
9255cee9645SThomas Gleixner  * @expires: new timeout in jiffies
9265cee9645SThomas Gleixner  *
9275cee9645SThomas Gleixner  * mod_timer_pinned() is a way to update the expire field of an
9285cee9645SThomas Gleixner  * active timer (if the timer is inactive it will be activated)
9295cee9645SThomas Gleixner  * and to ensure that the timer is scheduled on the current CPU.
9305cee9645SThomas Gleixner  *
9315cee9645SThomas Gleixner  * Note that this does not prevent the timer from being migrated
9325cee9645SThomas Gleixner  * when the current CPU goes offline.  If this is a problem for
9335cee9645SThomas Gleixner  * you, use CPU-hotplug notifiers to handle it correctly, for
9345cee9645SThomas Gleixner  * example, cancelling the timer when the corresponding CPU goes
9355cee9645SThomas Gleixner  * offline.
9365cee9645SThomas Gleixner  *
9375cee9645SThomas Gleixner  * mod_timer_pinned(timer, expires) is equivalent to:
9385cee9645SThomas Gleixner  *
9395cee9645SThomas Gleixner  *     del_timer(timer); timer->expires = expires; add_timer(timer);
9405cee9645SThomas Gleixner  */
9415cee9645SThomas Gleixner int mod_timer_pinned(struct timer_list *timer, unsigned long expires)
9425cee9645SThomas Gleixner {
9435cee9645SThomas Gleixner 	if (timer->expires == expires && timer_pending(timer))
9445cee9645SThomas Gleixner 		return 1;
9455cee9645SThomas Gleixner 
9465cee9645SThomas Gleixner 	return __mod_timer(timer, expires, false, TIMER_PINNED);
9475cee9645SThomas Gleixner }
9485cee9645SThomas Gleixner EXPORT_SYMBOL(mod_timer_pinned);
9495cee9645SThomas Gleixner 
9505cee9645SThomas Gleixner /**
9515cee9645SThomas Gleixner  * add_timer - start a timer
9525cee9645SThomas Gleixner  * @timer: the timer to be added
9535cee9645SThomas Gleixner  *
9545cee9645SThomas Gleixner  * The kernel will do a ->function(->data) callback from the
9555cee9645SThomas Gleixner  * timer interrupt at the ->expires point in the future. The
9565cee9645SThomas Gleixner  * current time is 'jiffies'.
9575cee9645SThomas Gleixner  *
9585cee9645SThomas Gleixner  * The timer's ->expires, ->function (and if the handler uses it, ->data)
9595cee9645SThomas Gleixner  * fields must be set prior calling this function.
9605cee9645SThomas Gleixner  *
9615cee9645SThomas Gleixner  * Timers with an ->expires field in the past will be executed in the next
9625cee9645SThomas Gleixner  * timer tick.
9635cee9645SThomas Gleixner  */
9645cee9645SThomas Gleixner void add_timer(struct timer_list *timer)
9655cee9645SThomas Gleixner {
9665cee9645SThomas Gleixner 	BUG_ON(timer_pending(timer));
9675cee9645SThomas Gleixner 	mod_timer(timer, timer->expires);
9685cee9645SThomas Gleixner }
9695cee9645SThomas Gleixner EXPORT_SYMBOL(add_timer);
9705cee9645SThomas Gleixner 
9715cee9645SThomas Gleixner /**
9725cee9645SThomas Gleixner  * add_timer_on - start a timer on a particular CPU
9735cee9645SThomas Gleixner  * @timer: the timer to be added
9745cee9645SThomas Gleixner  * @cpu: the CPU to start it on
9755cee9645SThomas Gleixner  *
9765cee9645SThomas Gleixner  * This is not very scalable on SMP. Double adds are not possible.
9775cee9645SThomas Gleixner  */
9785cee9645SThomas Gleixner void add_timer_on(struct timer_list *timer, int cpu)
9795cee9645SThomas Gleixner {
9800eeda71bSThomas Gleixner 	struct tvec_base *base = per_cpu_ptr(&tvec_bases, cpu);
9815cee9645SThomas Gleixner 	unsigned long flags;
9825cee9645SThomas Gleixner 
9835cee9645SThomas Gleixner 	timer_stats_timer_set_start_info(timer);
9845cee9645SThomas Gleixner 	BUG_ON(timer_pending(timer) || !timer->function);
9855cee9645SThomas Gleixner 	spin_lock_irqsave(&base->lock, flags);
9860eeda71bSThomas Gleixner 	timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu;
9875cee9645SThomas Gleixner 	debug_activate(timer, timer->expires);
9885cee9645SThomas Gleixner 	internal_add_timer(base, timer);
9895cee9645SThomas Gleixner 	spin_unlock_irqrestore(&base->lock, flags);
9905cee9645SThomas Gleixner }
9915cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(add_timer_on);
9925cee9645SThomas Gleixner 
9935cee9645SThomas Gleixner /**
9945cee9645SThomas Gleixner  * del_timer - deactive a timer.
9955cee9645SThomas Gleixner  * @timer: the timer to be deactivated
9965cee9645SThomas Gleixner  *
9975cee9645SThomas Gleixner  * del_timer() deactivates a timer - this works on both active and inactive
9985cee9645SThomas Gleixner  * timers.
9995cee9645SThomas Gleixner  *
10005cee9645SThomas Gleixner  * The function returns whether it has deactivated a pending timer or not.
10015cee9645SThomas Gleixner  * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
10025cee9645SThomas Gleixner  * active timer returns 1.)
10035cee9645SThomas Gleixner  */
10045cee9645SThomas Gleixner int del_timer(struct timer_list *timer)
10055cee9645SThomas Gleixner {
10065cee9645SThomas Gleixner 	struct tvec_base *base;
10075cee9645SThomas Gleixner 	unsigned long flags;
10085cee9645SThomas Gleixner 	int ret = 0;
10095cee9645SThomas Gleixner 
10105cee9645SThomas Gleixner 	debug_assert_init(timer);
10115cee9645SThomas Gleixner 
10125cee9645SThomas Gleixner 	timer_stats_timer_clear_start_info(timer);
10135cee9645SThomas Gleixner 	if (timer_pending(timer)) {
10145cee9645SThomas Gleixner 		base = lock_timer_base(timer, &flags);
10155cee9645SThomas Gleixner 		ret = detach_if_pending(timer, base, true);
10165cee9645SThomas Gleixner 		spin_unlock_irqrestore(&base->lock, flags);
10175cee9645SThomas Gleixner 	}
10185cee9645SThomas Gleixner 
10195cee9645SThomas Gleixner 	return ret;
10205cee9645SThomas Gleixner }
10215cee9645SThomas Gleixner EXPORT_SYMBOL(del_timer);
10225cee9645SThomas Gleixner 
10235cee9645SThomas Gleixner /**
10245cee9645SThomas Gleixner  * try_to_del_timer_sync - Try to deactivate a timer
10255cee9645SThomas Gleixner  * @timer: timer do del
10265cee9645SThomas Gleixner  *
10275cee9645SThomas Gleixner  * This function tries to deactivate a timer. Upon successful (ret >= 0)
10285cee9645SThomas Gleixner  * exit the timer is not queued and the handler is not running on any CPU.
10295cee9645SThomas Gleixner  */
10305cee9645SThomas Gleixner int try_to_del_timer_sync(struct timer_list *timer)
10315cee9645SThomas Gleixner {
10325cee9645SThomas Gleixner 	struct tvec_base *base;
10335cee9645SThomas Gleixner 	unsigned long flags;
10345cee9645SThomas Gleixner 	int ret = -1;
10355cee9645SThomas Gleixner 
10365cee9645SThomas Gleixner 	debug_assert_init(timer);
10375cee9645SThomas Gleixner 
10385cee9645SThomas Gleixner 	base = lock_timer_base(timer, &flags);
10395cee9645SThomas Gleixner 
10405cee9645SThomas Gleixner 	if (base->running_timer != timer) {
10415cee9645SThomas Gleixner 		timer_stats_timer_clear_start_info(timer);
10425cee9645SThomas Gleixner 		ret = detach_if_pending(timer, base, true);
10435cee9645SThomas Gleixner 	}
10445cee9645SThomas Gleixner 	spin_unlock_irqrestore(&base->lock, flags);
10455cee9645SThomas Gleixner 
10465cee9645SThomas Gleixner 	return ret;
10475cee9645SThomas Gleixner }
10485cee9645SThomas Gleixner EXPORT_SYMBOL(try_to_del_timer_sync);
10495cee9645SThomas Gleixner 
10505cee9645SThomas Gleixner #ifdef CONFIG_SMP
10515cee9645SThomas Gleixner /**
10525cee9645SThomas Gleixner  * del_timer_sync - deactivate a timer and wait for the handler to finish.
10535cee9645SThomas Gleixner  * @timer: the timer to be deactivated
10545cee9645SThomas Gleixner  *
10555cee9645SThomas Gleixner  * This function only differs from del_timer() on SMP: besides deactivating
10565cee9645SThomas Gleixner  * the timer it also makes sure the handler has finished executing on other
10575cee9645SThomas Gleixner  * CPUs.
10585cee9645SThomas Gleixner  *
10595cee9645SThomas Gleixner  * Synchronization rules: Callers must prevent restarting of the timer,
10605cee9645SThomas Gleixner  * otherwise this function is meaningless. It must not be called from
10615cee9645SThomas Gleixner  * interrupt contexts unless the timer is an irqsafe one. The caller must
10625cee9645SThomas Gleixner  * not hold locks which would prevent completion of the timer's
10635cee9645SThomas Gleixner  * handler. The timer's handler must not call add_timer_on(). Upon exit the
10645cee9645SThomas Gleixner  * timer is not queued and the handler is not running on any CPU.
10655cee9645SThomas Gleixner  *
10665cee9645SThomas Gleixner  * Note: For !irqsafe timers, you must not hold locks that are held in
10675cee9645SThomas Gleixner  *   interrupt context while calling this function. Even if the lock has
10685cee9645SThomas Gleixner  *   nothing to do with the timer in question.  Here's why:
10695cee9645SThomas Gleixner  *
10705cee9645SThomas Gleixner  *    CPU0                             CPU1
10715cee9645SThomas Gleixner  *    ----                             ----
10725cee9645SThomas Gleixner  *                                   <SOFTIRQ>
10735cee9645SThomas Gleixner  *                                   call_timer_fn();
10745cee9645SThomas Gleixner  *                                     base->running_timer = mytimer;
10755cee9645SThomas Gleixner  *  spin_lock_irq(somelock);
10765cee9645SThomas Gleixner  *                                     <IRQ>
10775cee9645SThomas Gleixner  *                                        spin_lock(somelock);
10785cee9645SThomas Gleixner  *  del_timer_sync(mytimer);
10795cee9645SThomas Gleixner  *   while (base->running_timer == mytimer);
10805cee9645SThomas Gleixner  *
10815cee9645SThomas Gleixner  * Now del_timer_sync() will never return and never release somelock.
10825cee9645SThomas Gleixner  * The interrupt on the other CPU is waiting to grab somelock but
10835cee9645SThomas Gleixner  * it has interrupted the softirq that CPU0 is waiting to finish.
10845cee9645SThomas Gleixner  *
10855cee9645SThomas Gleixner  * The function returns whether it has deactivated a pending timer or not.
10865cee9645SThomas Gleixner  */
10875cee9645SThomas Gleixner int del_timer_sync(struct timer_list *timer)
10885cee9645SThomas Gleixner {
10895cee9645SThomas Gleixner #ifdef CONFIG_LOCKDEP
10905cee9645SThomas Gleixner 	unsigned long flags;
10915cee9645SThomas Gleixner 
10925cee9645SThomas Gleixner 	/*
10935cee9645SThomas Gleixner 	 * If lockdep gives a backtrace here, please reference
10945cee9645SThomas Gleixner 	 * the synchronization rules above.
10955cee9645SThomas Gleixner 	 */
10965cee9645SThomas Gleixner 	local_irq_save(flags);
10975cee9645SThomas Gleixner 	lock_map_acquire(&timer->lockdep_map);
10985cee9645SThomas Gleixner 	lock_map_release(&timer->lockdep_map);
10995cee9645SThomas Gleixner 	local_irq_restore(flags);
11005cee9645SThomas Gleixner #endif
11015cee9645SThomas Gleixner 	/*
11025cee9645SThomas Gleixner 	 * don't use it in hardirq context, because it
11035cee9645SThomas Gleixner 	 * could lead to deadlock.
11045cee9645SThomas Gleixner 	 */
11050eeda71bSThomas Gleixner 	WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE));
11065cee9645SThomas Gleixner 	for (;;) {
11075cee9645SThomas Gleixner 		int ret = try_to_del_timer_sync(timer);
11085cee9645SThomas Gleixner 		if (ret >= 0)
11095cee9645SThomas Gleixner 			return ret;
11105cee9645SThomas Gleixner 		cpu_relax();
11115cee9645SThomas Gleixner 	}
11125cee9645SThomas Gleixner }
11135cee9645SThomas Gleixner EXPORT_SYMBOL(del_timer_sync);
11145cee9645SThomas Gleixner #endif
11155cee9645SThomas Gleixner 
11165cee9645SThomas Gleixner static int cascade(struct tvec_base *base, struct tvec *tv, int index)
11175cee9645SThomas Gleixner {
11185cee9645SThomas Gleixner 	/* cascade all the timers from tv up one level */
11191dabbcecSThomas Gleixner 	struct timer_list *timer;
11201dabbcecSThomas Gleixner 	struct hlist_node *tmp;
11211dabbcecSThomas Gleixner 	struct hlist_head tv_list;
11225cee9645SThomas Gleixner 
11231dabbcecSThomas Gleixner 	hlist_move_list(tv->vec + index, &tv_list);
11245cee9645SThomas Gleixner 
11255cee9645SThomas Gleixner 	/*
11265cee9645SThomas Gleixner 	 * We are removing _all_ timers from the list, so we
11275cee9645SThomas Gleixner 	 * don't have to detach them individually.
11285cee9645SThomas Gleixner 	 */
11291dabbcecSThomas Gleixner 	hlist_for_each_entry_safe(timer, tmp, &tv_list, entry) {
11305cee9645SThomas Gleixner 		/* No accounting, while moving them */
11315cee9645SThomas Gleixner 		__internal_add_timer(base, timer);
11325cee9645SThomas Gleixner 	}
11335cee9645SThomas Gleixner 
11345cee9645SThomas Gleixner 	return index;
11355cee9645SThomas Gleixner }
11365cee9645SThomas Gleixner 
11375cee9645SThomas Gleixner static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
11385cee9645SThomas Gleixner 			  unsigned long data)
11395cee9645SThomas Gleixner {
11405cee9645SThomas Gleixner 	int count = preempt_count();
11415cee9645SThomas Gleixner 
11425cee9645SThomas Gleixner #ifdef CONFIG_LOCKDEP
11435cee9645SThomas Gleixner 	/*
11445cee9645SThomas Gleixner 	 * It is permissible to free the timer from inside the
11455cee9645SThomas Gleixner 	 * function that is called from it, this we need to take into
11465cee9645SThomas Gleixner 	 * account for lockdep too. To avoid bogus "held lock freed"
11475cee9645SThomas Gleixner 	 * warnings as well as problems when looking into
11485cee9645SThomas Gleixner 	 * timer->lockdep_map, make a copy and use that here.
11495cee9645SThomas Gleixner 	 */
11505cee9645SThomas Gleixner 	struct lockdep_map lockdep_map;
11515cee9645SThomas Gleixner 
11525cee9645SThomas Gleixner 	lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
11535cee9645SThomas Gleixner #endif
11545cee9645SThomas Gleixner 	/*
11555cee9645SThomas Gleixner 	 * Couple the lock chain with the lock chain at
11565cee9645SThomas Gleixner 	 * del_timer_sync() by acquiring the lock_map around the fn()
11575cee9645SThomas Gleixner 	 * call here and in del_timer_sync().
11585cee9645SThomas Gleixner 	 */
11595cee9645SThomas Gleixner 	lock_map_acquire(&lockdep_map);
11605cee9645SThomas Gleixner 
11615cee9645SThomas Gleixner 	trace_timer_expire_entry(timer);
11625cee9645SThomas Gleixner 	fn(data);
11635cee9645SThomas Gleixner 	trace_timer_expire_exit(timer);
11645cee9645SThomas Gleixner 
11655cee9645SThomas Gleixner 	lock_map_release(&lockdep_map);
11665cee9645SThomas Gleixner 
11675cee9645SThomas Gleixner 	if (count != preempt_count()) {
11685cee9645SThomas Gleixner 		WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
11695cee9645SThomas Gleixner 			  fn, count, preempt_count());
11705cee9645SThomas Gleixner 		/*
11715cee9645SThomas Gleixner 		 * Restore the preempt count. That gives us a decent
11725cee9645SThomas Gleixner 		 * chance to survive and extract information. If the
11735cee9645SThomas Gleixner 		 * callback kept a lock held, bad luck, but not worse
11745cee9645SThomas Gleixner 		 * than the BUG() we had.
11755cee9645SThomas Gleixner 		 */
11765cee9645SThomas Gleixner 		preempt_count_set(count);
11775cee9645SThomas Gleixner 	}
11785cee9645SThomas Gleixner }
11795cee9645SThomas Gleixner 
11805cee9645SThomas Gleixner #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
11815cee9645SThomas Gleixner 
11825cee9645SThomas Gleixner /**
11835cee9645SThomas Gleixner  * __run_timers - run all expired timers (if any) on this CPU.
11845cee9645SThomas Gleixner  * @base: the timer vector to be processed.
11855cee9645SThomas Gleixner  *
11865cee9645SThomas Gleixner  * This function cascades all vectors and executes all expired timer
11875cee9645SThomas Gleixner  * vectors.
11885cee9645SThomas Gleixner  */
11895cee9645SThomas Gleixner static inline void __run_timers(struct tvec_base *base)
11905cee9645SThomas Gleixner {
11915cee9645SThomas Gleixner 	struct timer_list *timer;
11925cee9645SThomas Gleixner 
11935cee9645SThomas Gleixner 	spin_lock_irq(&base->lock);
11943bb475a3SThomas Gleixner 
11955cee9645SThomas Gleixner 	while (time_after_eq(jiffies, base->timer_jiffies)) {
11961dabbcecSThomas Gleixner 		struct hlist_head work_list;
11971dabbcecSThomas Gleixner 		struct hlist_head *head = &work_list;
11983bb475a3SThomas Gleixner 		int index;
11993bb475a3SThomas Gleixner 
12003bb475a3SThomas Gleixner 		if (!base->all_timers) {
12013bb475a3SThomas Gleixner 			base->timer_jiffies = jiffies;
12023bb475a3SThomas Gleixner 			break;
12033bb475a3SThomas Gleixner 		}
12043bb475a3SThomas Gleixner 
12053bb475a3SThomas Gleixner 		index = base->timer_jiffies & TVR_MASK;
12065cee9645SThomas Gleixner 
12075cee9645SThomas Gleixner 		/*
12085cee9645SThomas Gleixner 		 * Cascade timers:
12095cee9645SThomas Gleixner 		 */
12105cee9645SThomas Gleixner 		if (!index &&
12115cee9645SThomas Gleixner 			(!cascade(base, &base->tv2, INDEX(0))) &&
12125cee9645SThomas Gleixner 				(!cascade(base, &base->tv3, INDEX(1))) &&
12135cee9645SThomas Gleixner 					!cascade(base, &base->tv4, INDEX(2)))
12145cee9645SThomas Gleixner 			cascade(base, &base->tv5, INDEX(3));
12155cee9645SThomas Gleixner 		++base->timer_jiffies;
12161dabbcecSThomas Gleixner 		hlist_move_list(base->tv1.vec + index, head);
12171dabbcecSThomas Gleixner 		while (!hlist_empty(head)) {
12185cee9645SThomas Gleixner 			void (*fn)(unsigned long);
12195cee9645SThomas Gleixner 			unsigned long data;
12205cee9645SThomas Gleixner 			bool irqsafe;
12215cee9645SThomas Gleixner 
12221dabbcecSThomas Gleixner 			timer = hlist_entry(head->first, struct timer_list, entry);
12235cee9645SThomas Gleixner 			fn = timer->function;
12245cee9645SThomas Gleixner 			data = timer->data;
12250eeda71bSThomas Gleixner 			irqsafe = timer->flags & TIMER_IRQSAFE;
12265cee9645SThomas Gleixner 
12275cee9645SThomas Gleixner 			timer_stats_account_timer(timer);
12285cee9645SThomas Gleixner 
12295cee9645SThomas Gleixner 			base->running_timer = timer;
12305cee9645SThomas Gleixner 			detach_expired_timer(timer, base);
12315cee9645SThomas Gleixner 
12325cee9645SThomas Gleixner 			if (irqsafe) {
12335cee9645SThomas Gleixner 				spin_unlock(&base->lock);
12345cee9645SThomas Gleixner 				call_timer_fn(timer, fn, data);
12355cee9645SThomas Gleixner 				spin_lock(&base->lock);
12365cee9645SThomas Gleixner 			} else {
12375cee9645SThomas Gleixner 				spin_unlock_irq(&base->lock);
12385cee9645SThomas Gleixner 				call_timer_fn(timer, fn, data);
12395cee9645SThomas Gleixner 				spin_lock_irq(&base->lock);
12405cee9645SThomas Gleixner 			}
12415cee9645SThomas Gleixner 		}
12425cee9645SThomas Gleixner 	}
12435cee9645SThomas Gleixner 	base->running_timer = NULL;
12445cee9645SThomas Gleixner 	spin_unlock_irq(&base->lock);
12455cee9645SThomas Gleixner }
12465cee9645SThomas Gleixner 
12475cee9645SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
12485cee9645SThomas Gleixner /*
12495cee9645SThomas Gleixner  * Find out when the next timer event is due to happen. This
12505cee9645SThomas Gleixner  * is used on S/390 to stop all activity when a CPU is idle.
12515cee9645SThomas Gleixner  * This function needs to be called with interrupts disabled.
12525cee9645SThomas Gleixner  */
12535cee9645SThomas Gleixner static unsigned long __next_timer_interrupt(struct tvec_base *base)
12545cee9645SThomas Gleixner {
12555cee9645SThomas Gleixner 	unsigned long timer_jiffies = base->timer_jiffies;
12565cee9645SThomas Gleixner 	unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
12575cee9645SThomas Gleixner 	int index, slot, array, found = 0;
12585cee9645SThomas Gleixner 	struct timer_list *nte;
12595cee9645SThomas Gleixner 	struct tvec *varray[4];
12605cee9645SThomas Gleixner 
12615cee9645SThomas Gleixner 	/* Look for timer events in tv1. */
12625cee9645SThomas Gleixner 	index = slot = timer_jiffies & TVR_MASK;
12635cee9645SThomas Gleixner 	do {
12641dabbcecSThomas Gleixner 		hlist_for_each_entry(nte, base->tv1.vec + slot, entry) {
12650eeda71bSThomas Gleixner 			if (nte->flags & TIMER_DEFERRABLE)
12665cee9645SThomas Gleixner 				continue;
12675cee9645SThomas Gleixner 
12685cee9645SThomas Gleixner 			found = 1;
12695cee9645SThomas Gleixner 			expires = nte->expires;
12705cee9645SThomas Gleixner 			/* Look at the cascade bucket(s)? */
12715cee9645SThomas Gleixner 			if (!index || slot < index)
12725cee9645SThomas Gleixner 				goto cascade;
12735cee9645SThomas Gleixner 			return expires;
12745cee9645SThomas Gleixner 		}
12755cee9645SThomas Gleixner 		slot = (slot + 1) & TVR_MASK;
12765cee9645SThomas Gleixner 	} while (slot != index);
12775cee9645SThomas Gleixner 
12785cee9645SThomas Gleixner cascade:
12795cee9645SThomas Gleixner 	/* Calculate the next cascade event */
12805cee9645SThomas Gleixner 	if (index)
12815cee9645SThomas Gleixner 		timer_jiffies += TVR_SIZE - index;
12825cee9645SThomas Gleixner 	timer_jiffies >>= TVR_BITS;
12835cee9645SThomas Gleixner 
12845cee9645SThomas Gleixner 	/* Check tv2-tv5. */
12855cee9645SThomas Gleixner 	varray[0] = &base->tv2;
12865cee9645SThomas Gleixner 	varray[1] = &base->tv3;
12875cee9645SThomas Gleixner 	varray[2] = &base->tv4;
12885cee9645SThomas Gleixner 	varray[3] = &base->tv5;
12895cee9645SThomas Gleixner 
12905cee9645SThomas Gleixner 	for (array = 0; array < 4; array++) {
12915cee9645SThomas Gleixner 		struct tvec *varp = varray[array];
12925cee9645SThomas Gleixner 
12935cee9645SThomas Gleixner 		index = slot = timer_jiffies & TVN_MASK;
12945cee9645SThomas Gleixner 		do {
12951dabbcecSThomas Gleixner 			hlist_for_each_entry(nte, varp->vec + slot, entry) {
12960eeda71bSThomas Gleixner 				if (nte->flags & TIMER_DEFERRABLE)
12975cee9645SThomas Gleixner 					continue;
12985cee9645SThomas Gleixner 
12995cee9645SThomas Gleixner 				found = 1;
13005cee9645SThomas Gleixner 				if (time_before(nte->expires, expires))
13015cee9645SThomas Gleixner 					expires = nte->expires;
13025cee9645SThomas Gleixner 			}
13035cee9645SThomas Gleixner 			/*
13045cee9645SThomas Gleixner 			 * Do we still search for the first timer or are
13055cee9645SThomas Gleixner 			 * we looking up the cascade buckets ?
13065cee9645SThomas Gleixner 			 */
13075cee9645SThomas Gleixner 			if (found) {
13085cee9645SThomas Gleixner 				/* Look at the cascade bucket(s)? */
13095cee9645SThomas Gleixner 				if (!index || slot < index)
13105cee9645SThomas Gleixner 					break;
13115cee9645SThomas Gleixner 				return expires;
13125cee9645SThomas Gleixner 			}
13135cee9645SThomas Gleixner 			slot = (slot + 1) & TVN_MASK;
13145cee9645SThomas Gleixner 		} while (slot != index);
13155cee9645SThomas Gleixner 
13165cee9645SThomas Gleixner 		if (index)
13175cee9645SThomas Gleixner 			timer_jiffies += TVN_SIZE - index;
13185cee9645SThomas Gleixner 		timer_jiffies >>= TVN_BITS;
13195cee9645SThomas Gleixner 	}
13205cee9645SThomas Gleixner 	return expires;
13215cee9645SThomas Gleixner }
13225cee9645SThomas Gleixner 
13235cee9645SThomas Gleixner /*
13245cee9645SThomas Gleixner  * Check, if the next hrtimer event is before the next timer wheel
13255cee9645SThomas Gleixner  * event:
13265cee9645SThomas Gleixner  */
1327c1ad348bSThomas Gleixner static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
13285cee9645SThomas Gleixner {
1329c1ad348bSThomas Gleixner 	u64 nextevt = hrtimer_get_next_event();
13305cee9645SThomas Gleixner 
1331c1ad348bSThomas Gleixner 	/*
1332c1ad348bSThomas Gleixner 	 * If high resolution timers are enabled
1333c1ad348bSThomas Gleixner 	 * hrtimer_get_next_event() returns KTIME_MAX.
1334c1ad348bSThomas Gleixner 	 */
1335c1ad348bSThomas Gleixner 	if (expires <= nextevt)
13365cee9645SThomas Gleixner 		return expires;
13375cee9645SThomas Gleixner 
13385cee9645SThomas Gleixner 	/*
1339c1ad348bSThomas Gleixner 	 * If the next timer is already expired, return the tick base
1340c1ad348bSThomas Gleixner 	 * time so the tick is fired immediately.
13415cee9645SThomas Gleixner 	 */
1342c1ad348bSThomas Gleixner 	if (nextevt <= basem)
1343c1ad348bSThomas Gleixner 		return basem;
13445cee9645SThomas Gleixner 
13455cee9645SThomas Gleixner 	/*
1346c1ad348bSThomas Gleixner 	 * Round up to the next jiffie. High resolution timers are
1347c1ad348bSThomas Gleixner 	 * off, so the hrtimers are expired in the tick and we need to
1348c1ad348bSThomas Gleixner 	 * make sure that this tick really expires the timer to avoid
1349c1ad348bSThomas Gleixner 	 * a ping pong of the nohz stop code.
1350c1ad348bSThomas Gleixner 	 *
1351c1ad348bSThomas Gleixner 	 * Use DIV_ROUND_UP_ULL to prevent gcc calling __divdi3
13525cee9645SThomas Gleixner 	 */
1353c1ad348bSThomas Gleixner 	return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC;
13545cee9645SThomas Gleixner }
13555cee9645SThomas Gleixner 
13565cee9645SThomas Gleixner /**
1357c1ad348bSThomas Gleixner  * get_next_timer_interrupt - return the time (clock mono) of the next timer
1358c1ad348bSThomas Gleixner  * @basej:	base time jiffies
1359c1ad348bSThomas Gleixner  * @basem:	base time clock monotonic
1360c1ad348bSThomas Gleixner  *
1361c1ad348bSThomas Gleixner  * Returns the tick aligned clock monotonic time of the next pending
1362c1ad348bSThomas Gleixner  * timer or KTIME_MAX if no timer is pending.
13635cee9645SThomas Gleixner  */
1364c1ad348bSThomas Gleixner u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
13655cee9645SThomas Gleixner {
13660eeda71bSThomas Gleixner 	struct tvec_base *base = this_cpu_ptr(&tvec_bases);
1367c1ad348bSThomas Gleixner 	u64 expires = KTIME_MAX;
1368c1ad348bSThomas Gleixner 	unsigned long nextevt;
13695cee9645SThomas Gleixner 
13705cee9645SThomas Gleixner 	/*
13715cee9645SThomas Gleixner 	 * Pretend that there is no timer pending if the cpu is offline.
13725cee9645SThomas Gleixner 	 * Possible pending timers will be migrated later to an active cpu.
13735cee9645SThomas Gleixner 	 */
13745cee9645SThomas Gleixner 	if (cpu_is_offline(smp_processor_id()))
13755cee9645SThomas Gleixner 		return expires;
13765cee9645SThomas Gleixner 
13775cee9645SThomas Gleixner 	spin_lock(&base->lock);
13785cee9645SThomas Gleixner 	if (base->active_timers) {
13795cee9645SThomas Gleixner 		if (time_before_eq(base->next_timer, base->timer_jiffies))
13805cee9645SThomas Gleixner 			base->next_timer = __next_timer_interrupt(base);
1381c1ad348bSThomas Gleixner 		nextevt = base->next_timer;
1382c1ad348bSThomas Gleixner 		if (time_before_eq(nextevt, basej))
1383c1ad348bSThomas Gleixner 			expires = basem;
1384c1ad348bSThomas Gleixner 		else
1385c1ad348bSThomas Gleixner 			expires = basem + (nextevt - basej) * TICK_NSEC;
13865cee9645SThomas Gleixner 	}
13875cee9645SThomas Gleixner 	spin_unlock(&base->lock);
13885cee9645SThomas Gleixner 
1389c1ad348bSThomas Gleixner 	return cmp_next_hrtimer_event(basem, expires);
13905cee9645SThomas Gleixner }
13915cee9645SThomas Gleixner #endif
13925cee9645SThomas Gleixner 
13935cee9645SThomas Gleixner /*
13945cee9645SThomas Gleixner  * Called from the timer interrupt handler to charge one tick to the current
13955cee9645SThomas Gleixner  * process.  user_tick is 1 if the tick is user time, 0 for system.
13965cee9645SThomas Gleixner  */
13975cee9645SThomas Gleixner void update_process_times(int user_tick)
13985cee9645SThomas Gleixner {
13995cee9645SThomas Gleixner 	struct task_struct *p = current;
14005cee9645SThomas Gleixner 
14015cee9645SThomas Gleixner 	/* Note: this timer irq context must be accounted for as well. */
14025cee9645SThomas Gleixner 	account_process_tick(p, user_tick);
14035cee9645SThomas Gleixner 	run_local_timers();
1404c3377c2dSPaul E. McKenney 	rcu_check_callbacks(user_tick);
14055cee9645SThomas Gleixner #ifdef CONFIG_IRQ_WORK
14065cee9645SThomas Gleixner 	if (in_irq())
140776a33061SFrederic Weisbecker 		irq_work_tick();
14085cee9645SThomas Gleixner #endif
14095cee9645SThomas Gleixner 	scheduler_tick();
14105cee9645SThomas Gleixner 	run_posix_cpu_timers(p);
14115cee9645SThomas Gleixner }
14125cee9645SThomas Gleixner 
14135cee9645SThomas Gleixner /*
14145cee9645SThomas Gleixner  * This function runs timers and the timer-tq in bottom half context.
14155cee9645SThomas Gleixner  */
14165cee9645SThomas Gleixner static void run_timer_softirq(struct softirq_action *h)
14175cee9645SThomas Gleixner {
14180eeda71bSThomas Gleixner 	struct tvec_base *base = this_cpu_ptr(&tvec_bases);
14195cee9645SThomas Gleixner 
14205cee9645SThomas Gleixner 	if (time_after_eq(jiffies, base->timer_jiffies))
14215cee9645SThomas Gleixner 		__run_timers(base);
14225cee9645SThomas Gleixner }
14235cee9645SThomas Gleixner 
14245cee9645SThomas Gleixner /*
14255cee9645SThomas Gleixner  * Called by the local, per-CPU timer interrupt on SMP.
14265cee9645SThomas Gleixner  */
14275cee9645SThomas Gleixner void run_local_timers(void)
14285cee9645SThomas Gleixner {
14295cee9645SThomas Gleixner 	hrtimer_run_queues();
14305cee9645SThomas Gleixner 	raise_softirq(TIMER_SOFTIRQ);
14315cee9645SThomas Gleixner }
14325cee9645SThomas Gleixner 
14335cee9645SThomas Gleixner #ifdef __ARCH_WANT_SYS_ALARM
14345cee9645SThomas Gleixner 
14355cee9645SThomas Gleixner /*
14365cee9645SThomas Gleixner  * For backwards compatibility?  This can be done in libc so Alpha
14375cee9645SThomas Gleixner  * and all newer ports shouldn't need it.
14385cee9645SThomas Gleixner  */
14395cee9645SThomas Gleixner SYSCALL_DEFINE1(alarm, unsigned int, seconds)
14405cee9645SThomas Gleixner {
14415cee9645SThomas Gleixner 	return alarm_setitimer(seconds);
14425cee9645SThomas Gleixner }
14435cee9645SThomas Gleixner 
14445cee9645SThomas Gleixner #endif
14455cee9645SThomas Gleixner 
14465cee9645SThomas Gleixner static void process_timeout(unsigned long __data)
14475cee9645SThomas Gleixner {
14485cee9645SThomas Gleixner 	wake_up_process((struct task_struct *)__data);
14495cee9645SThomas Gleixner }
14505cee9645SThomas Gleixner 
14515cee9645SThomas Gleixner /**
14525cee9645SThomas Gleixner  * schedule_timeout - sleep until timeout
14535cee9645SThomas Gleixner  * @timeout: timeout value in jiffies
14545cee9645SThomas Gleixner  *
14555cee9645SThomas Gleixner  * Make the current task sleep until @timeout jiffies have
14565cee9645SThomas Gleixner  * elapsed. The routine will return immediately unless
14575cee9645SThomas Gleixner  * the current task state has been set (see set_current_state()).
14585cee9645SThomas Gleixner  *
14595cee9645SThomas Gleixner  * You can set the task state as follows -
14605cee9645SThomas Gleixner  *
14615cee9645SThomas Gleixner  * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
14625cee9645SThomas Gleixner  * pass before the routine returns. The routine will return 0
14635cee9645SThomas Gleixner  *
14645cee9645SThomas Gleixner  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
14655cee9645SThomas Gleixner  * delivered to the current task. In this case the remaining time
14665cee9645SThomas Gleixner  * in jiffies will be returned, or 0 if the timer expired in time
14675cee9645SThomas Gleixner  *
14685cee9645SThomas Gleixner  * The current task state is guaranteed to be TASK_RUNNING when this
14695cee9645SThomas Gleixner  * routine returns.
14705cee9645SThomas Gleixner  *
14715cee9645SThomas Gleixner  * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
14725cee9645SThomas Gleixner  * the CPU away without a bound on the timeout. In this case the return
14735cee9645SThomas Gleixner  * value will be %MAX_SCHEDULE_TIMEOUT.
14745cee9645SThomas Gleixner  *
14755cee9645SThomas Gleixner  * In all cases the return value is guaranteed to be non-negative.
14765cee9645SThomas Gleixner  */
14775cee9645SThomas Gleixner signed long __sched schedule_timeout(signed long timeout)
14785cee9645SThomas Gleixner {
14795cee9645SThomas Gleixner 	struct timer_list timer;
14805cee9645SThomas Gleixner 	unsigned long expire;
14815cee9645SThomas Gleixner 
14825cee9645SThomas Gleixner 	switch (timeout)
14835cee9645SThomas Gleixner 	{
14845cee9645SThomas Gleixner 	case MAX_SCHEDULE_TIMEOUT:
14855cee9645SThomas Gleixner 		/*
14865cee9645SThomas Gleixner 		 * These two special cases are useful to be comfortable
14875cee9645SThomas Gleixner 		 * in the caller. Nothing more. We could take
14885cee9645SThomas Gleixner 		 * MAX_SCHEDULE_TIMEOUT from one of the negative value
14895cee9645SThomas Gleixner 		 * but I' d like to return a valid offset (>=0) to allow
14905cee9645SThomas Gleixner 		 * the caller to do everything it want with the retval.
14915cee9645SThomas Gleixner 		 */
14925cee9645SThomas Gleixner 		schedule();
14935cee9645SThomas Gleixner 		goto out;
14945cee9645SThomas Gleixner 	default:
14955cee9645SThomas Gleixner 		/*
14965cee9645SThomas Gleixner 		 * Another bit of PARANOID. Note that the retval will be
14975cee9645SThomas Gleixner 		 * 0 since no piece of kernel is supposed to do a check
14985cee9645SThomas Gleixner 		 * for a negative retval of schedule_timeout() (since it
14995cee9645SThomas Gleixner 		 * should never happens anyway). You just have the printk()
15005cee9645SThomas Gleixner 		 * that will tell you if something is gone wrong and where.
15015cee9645SThomas Gleixner 		 */
15025cee9645SThomas Gleixner 		if (timeout < 0) {
15035cee9645SThomas Gleixner 			printk(KERN_ERR "schedule_timeout: wrong timeout "
15045cee9645SThomas Gleixner 				"value %lx\n", timeout);
15055cee9645SThomas Gleixner 			dump_stack();
15065cee9645SThomas Gleixner 			current->state = TASK_RUNNING;
15075cee9645SThomas Gleixner 			goto out;
15085cee9645SThomas Gleixner 		}
15095cee9645SThomas Gleixner 	}
15105cee9645SThomas Gleixner 
15115cee9645SThomas Gleixner 	expire = timeout + jiffies;
15125cee9645SThomas Gleixner 
15135cee9645SThomas Gleixner 	setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
15145cee9645SThomas Gleixner 	__mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
15155cee9645SThomas Gleixner 	schedule();
15165cee9645SThomas Gleixner 	del_singleshot_timer_sync(&timer);
15175cee9645SThomas Gleixner 
15185cee9645SThomas Gleixner 	/* Remove the timer from the object tracker */
15195cee9645SThomas Gleixner 	destroy_timer_on_stack(&timer);
15205cee9645SThomas Gleixner 
15215cee9645SThomas Gleixner 	timeout = expire - jiffies;
15225cee9645SThomas Gleixner 
15235cee9645SThomas Gleixner  out:
15245cee9645SThomas Gleixner 	return timeout < 0 ? 0 : timeout;
15255cee9645SThomas Gleixner }
15265cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout);
15275cee9645SThomas Gleixner 
15285cee9645SThomas Gleixner /*
15295cee9645SThomas Gleixner  * We can use __set_current_state() here because schedule_timeout() calls
15305cee9645SThomas Gleixner  * schedule() unconditionally.
15315cee9645SThomas Gleixner  */
15325cee9645SThomas Gleixner signed long __sched schedule_timeout_interruptible(signed long timeout)
15335cee9645SThomas Gleixner {
15345cee9645SThomas Gleixner 	__set_current_state(TASK_INTERRUPTIBLE);
15355cee9645SThomas Gleixner 	return schedule_timeout(timeout);
15365cee9645SThomas Gleixner }
15375cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_interruptible);
15385cee9645SThomas Gleixner 
15395cee9645SThomas Gleixner signed long __sched schedule_timeout_killable(signed long timeout)
15405cee9645SThomas Gleixner {
15415cee9645SThomas Gleixner 	__set_current_state(TASK_KILLABLE);
15425cee9645SThomas Gleixner 	return schedule_timeout(timeout);
15435cee9645SThomas Gleixner }
15445cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_killable);
15455cee9645SThomas Gleixner 
15465cee9645SThomas Gleixner signed long __sched schedule_timeout_uninterruptible(signed long timeout)
15475cee9645SThomas Gleixner {
15485cee9645SThomas Gleixner 	__set_current_state(TASK_UNINTERRUPTIBLE);
15495cee9645SThomas Gleixner 	return schedule_timeout(timeout);
15505cee9645SThomas Gleixner }
15515cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_uninterruptible);
15525cee9645SThomas Gleixner 
15535cee9645SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
15541dabbcecSThomas Gleixner static void migrate_timer_list(struct tvec_base *new_base, struct hlist_head *head)
15555cee9645SThomas Gleixner {
15565cee9645SThomas Gleixner 	struct timer_list *timer;
15570eeda71bSThomas Gleixner 	int cpu = new_base->cpu;
15585cee9645SThomas Gleixner 
15591dabbcecSThomas Gleixner 	while (!hlist_empty(head)) {
15601dabbcecSThomas Gleixner 		timer = hlist_entry(head->first, struct timer_list, entry);
15615cee9645SThomas Gleixner 		/* We ignore the accounting on the dying cpu */
15625cee9645SThomas Gleixner 		detach_timer(timer, false);
15630eeda71bSThomas Gleixner 		timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu;
15645cee9645SThomas Gleixner 		internal_add_timer(new_base, timer);
15655cee9645SThomas Gleixner 	}
15665cee9645SThomas Gleixner }
15675cee9645SThomas Gleixner 
15685cee9645SThomas Gleixner static void migrate_timers(int cpu)
15695cee9645SThomas Gleixner {
15705cee9645SThomas Gleixner 	struct tvec_base *old_base;
15715cee9645SThomas Gleixner 	struct tvec_base *new_base;
15725cee9645SThomas Gleixner 	int i;
15735cee9645SThomas Gleixner 
15745cee9645SThomas Gleixner 	BUG_ON(cpu_online(cpu));
15750eeda71bSThomas Gleixner 	old_base = per_cpu_ptr(&tvec_bases, cpu);
157624bfcb10SThomas Gleixner 	new_base = get_cpu_ptr(&tvec_bases);
15775cee9645SThomas Gleixner 	/*
15785cee9645SThomas Gleixner 	 * The caller is globally serialized and nobody else
15795cee9645SThomas Gleixner 	 * takes two locks at once, deadlock is not possible.
15805cee9645SThomas Gleixner 	 */
15815cee9645SThomas Gleixner 	spin_lock_irq(&new_base->lock);
15825cee9645SThomas Gleixner 	spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
15835cee9645SThomas Gleixner 
15845cee9645SThomas Gleixner 	BUG_ON(old_base->running_timer);
15855cee9645SThomas Gleixner 
15865cee9645SThomas Gleixner 	for (i = 0; i < TVR_SIZE; i++)
15875cee9645SThomas Gleixner 		migrate_timer_list(new_base, old_base->tv1.vec + i);
15885cee9645SThomas Gleixner 	for (i = 0; i < TVN_SIZE; i++) {
15895cee9645SThomas Gleixner 		migrate_timer_list(new_base, old_base->tv2.vec + i);
15905cee9645SThomas Gleixner 		migrate_timer_list(new_base, old_base->tv3.vec + i);
15915cee9645SThomas Gleixner 		migrate_timer_list(new_base, old_base->tv4.vec + i);
15925cee9645SThomas Gleixner 		migrate_timer_list(new_base, old_base->tv5.vec + i);
15935cee9645SThomas Gleixner 	}
15945cee9645SThomas Gleixner 
15958def9060SViresh Kumar 	old_base->active_timers = 0;
15968def9060SViresh Kumar 	old_base->all_timers = 0;
15978def9060SViresh Kumar 
15985cee9645SThomas Gleixner 	spin_unlock(&old_base->lock);
15995cee9645SThomas Gleixner 	spin_unlock_irq(&new_base->lock);
160024bfcb10SThomas Gleixner 	put_cpu_ptr(&tvec_bases);
16015cee9645SThomas Gleixner }
16025cee9645SThomas Gleixner 
16035cee9645SThomas Gleixner static int timer_cpu_notify(struct notifier_block *self,
16045cee9645SThomas Gleixner 				unsigned long action, void *hcpu)
16055cee9645SThomas Gleixner {
16068def9060SViresh Kumar 	switch (action) {
16075cee9645SThomas Gleixner 	case CPU_DEAD:
16085cee9645SThomas Gleixner 	case CPU_DEAD_FROZEN:
16098def9060SViresh Kumar 		migrate_timers((long)hcpu);
16105cee9645SThomas Gleixner 		break;
16115cee9645SThomas Gleixner 	default:
16125cee9645SThomas Gleixner 		break;
16135cee9645SThomas Gleixner 	}
16143650b57fSPeter Zijlstra 
16155cee9645SThomas Gleixner 	return NOTIFY_OK;
16165cee9645SThomas Gleixner }
16175cee9645SThomas Gleixner 
16183650b57fSPeter Zijlstra static inline void timer_register_cpu_notifier(void)
16193650b57fSPeter Zijlstra {
16203650b57fSPeter Zijlstra 	cpu_notifier(timer_cpu_notify, 0);
16213650b57fSPeter Zijlstra }
16223650b57fSPeter Zijlstra #else
16233650b57fSPeter Zijlstra static inline void timer_register_cpu_notifier(void) { }
16243650b57fSPeter Zijlstra #endif /* CONFIG_HOTPLUG_CPU */
16255cee9645SThomas Gleixner 
16260eeda71bSThomas Gleixner static void __init init_timer_cpu(int cpu)
16278def9060SViresh Kumar {
16280eeda71bSThomas Gleixner 	struct tvec_base *base = per_cpu_ptr(&tvec_bases, cpu);
16293650b57fSPeter Zijlstra 
16308def9060SViresh Kumar 	base->cpu = cpu;
16318def9060SViresh Kumar 	spin_lock_init(&base->lock);
16328def9060SViresh Kumar 
16338def9060SViresh Kumar 	base->timer_jiffies = jiffies;
16348def9060SViresh Kumar 	base->next_timer = base->timer_jiffies;
16358def9060SViresh Kumar }
16368def9060SViresh Kumar 
16378def9060SViresh Kumar static void __init init_timer_cpus(void)
16388def9060SViresh Kumar {
16398def9060SViresh Kumar 	int cpu;
16408def9060SViresh Kumar 
16410eeda71bSThomas Gleixner 	for_each_possible_cpu(cpu)
16420eeda71bSThomas Gleixner 		init_timer_cpu(cpu);
16438def9060SViresh Kumar }
16445cee9645SThomas Gleixner 
16455cee9645SThomas Gleixner void __init init_timers(void)
16465cee9645SThomas Gleixner {
16478def9060SViresh Kumar 	init_timer_cpus();
16485cee9645SThomas Gleixner 	init_timer_stats();
16493650b57fSPeter Zijlstra 	timer_register_cpu_notifier();
16505cee9645SThomas Gleixner 	open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
16515cee9645SThomas Gleixner }
16525cee9645SThomas Gleixner 
16535cee9645SThomas Gleixner /**
16545cee9645SThomas Gleixner  * msleep - sleep safely even with waitqueue interruptions
16555cee9645SThomas Gleixner  * @msecs: Time in milliseconds to sleep for
16565cee9645SThomas Gleixner  */
16575cee9645SThomas Gleixner void msleep(unsigned int msecs)
16585cee9645SThomas Gleixner {
16595cee9645SThomas Gleixner 	unsigned long timeout = msecs_to_jiffies(msecs) + 1;
16605cee9645SThomas Gleixner 
16615cee9645SThomas Gleixner 	while (timeout)
16625cee9645SThomas Gleixner 		timeout = schedule_timeout_uninterruptible(timeout);
16635cee9645SThomas Gleixner }
16645cee9645SThomas Gleixner 
16655cee9645SThomas Gleixner EXPORT_SYMBOL(msleep);
16665cee9645SThomas Gleixner 
16675cee9645SThomas Gleixner /**
16685cee9645SThomas Gleixner  * msleep_interruptible - sleep waiting for signals
16695cee9645SThomas Gleixner  * @msecs: Time in milliseconds to sleep for
16705cee9645SThomas Gleixner  */
16715cee9645SThomas Gleixner unsigned long msleep_interruptible(unsigned int msecs)
16725cee9645SThomas Gleixner {
16735cee9645SThomas Gleixner 	unsigned long timeout = msecs_to_jiffies(msecs) + 1;
16745cee9645SThomas Gleixner 
16755cee9645SThomas Gleixner 	while (timeout && !signal_pending(current))
16765cee9645SThomas Gleixner 		timeout = schedule_timeout_interruptible(timeout);
16775cee9645SThomas Gleixner 	return jiffies_to_msecs(timeout);
16785cee9645SThomas Gleixner }
16795cee9645SThomas Gleixner 
16805cee9645SThomas Gleixner EXPORT_SYMBOL(msleep_interruptible);
16815cee9645SThomas Gleixner 
16826deba083SThomas Gleixner static void __sched do_usleep_range(unsigned long min, unsigned long max)
16835cee9645SThomas Gleixner {
16845cee9645SThomas Gleixner 	ktime_t kmin;
16855cee9645SThomas Gleixner 	unsigned long delta;
16865cee9645SThomas Gleixner 
16875cee9645SThomas Gleixner 	kmin = ktime_set(0, min * NSEC_PER_USEC);
16885cee9645SThomas Gleixner 	delta = (max - min) * NSEC_PER_USEC;
16896deba083SThomas Gleixner 	schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL);
16905cee9645SThomas Gleixner }
16915cee9645SThomas Gleixner 
16925cee9645SThomas Gleixner /**
16935cee9645SThomas Gleixner  * usleep_range - Drop in replacement for udelay where wakeup is flexible
16945cee9645SThomas Gleixner  * @min: Minimum time in usecs to sleep
16955cee9645SThomas Gleixner  * @max: Maximum time in usecs to sleep
16965cee9645SThomas Gleixner  */
16972ad5d327SThomas Gleixner void __sched usleep_range(unsigned long min, unsigned long max)
16985cee9645SThomas Gleixner {
16995cee9645SThomas Gleixner 	__set_current_state(TASK_UNINTERRUPTIBLE);
17005cee9645SThomas Gleixner 	do_usleep_range(min, max);
17015cee9645SThomas Gleixner }
17025cee9645SThomas Gleixner EXPORT_SYMBOL(usleep_range);
1703