aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/time/timer.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/time/timer.c')
-rw-r--r--kernel/time/timer.c1736
1 files changed, 1736 insertions, 0 deletions
diff --git a/kernel/time/timer.c b/kernel/time/timer.c
new file mode 100644
index 000000000000..aca5dfe2fa3d
--- /dev/null
+++ b/kernel/time/timer.c
@@ -0,0 +1,1736 @@
1/*
2 * linux/kernel/timer.c
3 *
4 * Kernel internal timers
5 *
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 *
8 * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
9 *
10 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
11 * "A Kernel Model for Precision Timekeeping" by Dave Mills
12 * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
13 * serialize accesses to xtime/lost_ticks).
14 * Copyright (C) 1998 Andrea Arcangeli
15 * 1999-03-10 Improved NTP compatibility by Ulrich Windl
16 * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love
17 * 2000-10-05 Implemented scalable SMP per-CPU timer handling.
18 * Copyright (C) 2000, 2001, 2002 Ingo Molnar
19 * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
20 */
21
22#include <linux/kernel_stat.h>
23#include <linux/export.h>
24#include <linux/interrupt.h>
25#include <linux/percpu.h>
26#include <linux/init.h>
27#include <linux/mm.h>
28#include <linux/swap.h>
29#include <linux/pid_namespace.h>
30#include <linux/notifier.h>
31#include <linux/thread_info.h>
32#include <linux/time.h>
33#include <linux/jiffies.h>
34#include <linux/posix-timers.h>
35#include <linux/cpu.h>
36#include <linux/syscalls.h>
37#include <linux/delay.h>
38#include <linux/tick.h>
39#include <linux/kallsyms.h>
40#include <linux/irq_work.h>
41#include <linux/sched.h>
42#include <linux/sched/sysctl.h>
43#include <linux/slab.h>
44#include <linux/compat.h>
45
46#include <asm/uaccess.h>
47#include <asm/unistd.h>
48#include <asm/div64.h>
49#include <asm/timex.h>
50#include <asm/io.h>
51
52#define CREATE_TRACE_POINTS
53#include <trace/events/timer.h>
54
55__visible u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
56
57EXPORT_SYMBOL(jiffies_64);
58
59/*
60 * per-CPU timer vector definitions:
61 */
62#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
63#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
64#define TVN_SIZE (1 << TVN_BITS)
65#define TVR_SIZE (1 << TVR_BITS)
66#define TVN_MASK (TVN_SIZE - 1)
67#define TVR_MASK (TVR_SIZE - 1)
68#define MAX_TVAL ((unsigned long)((1ULL << (TVR_BITS + 4*TVN_BITS)) - 1))
69
70struct tvec {
71 struct list_head vec[TVN_SIZE];
72};
73
74struct tvec_root {
75 struct list_head vec[TVR_SIZE];
76};
77
78struct tvec_base {
79 spinlock_t lock;
80 struct timer_list *running_timer;
81 unsigned long timer_jiffies;
82 unsigned long next_timer;
83 unsigned long active_timers;
84 unsigned long all_timers;
85 int cpu;
86 struct tvec_root tv1;
87 struct tvec tv2;
88 struct tvec tv3;
89 struct tvec tv4;
90 struct tvec tv5;
91} ____cacheline_aligned;
92
93struct tvec_base boot_tvec_bases;
94EXPORT_SYMBOL(boot_tvec_bases);
95static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
96
97/* Functions below help us manage 'deferrable' flag */
98static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
99{
100 return ((unsigned int)(unsigned long)base & TIMER_DEFERRABLE);
101}
102
103static inline unsigned int tbase_get_irqsafe(struct tvec_base *base)
104{
105 return ((unsigned int)(unsigned long)base & TIMER_IRQSAFE);
106}
107
108static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
109{
110 return ((struct tvec_base *)((unsigned long)base & ~TIMER_FLAG_MASK));
111}
112
113static inline void
114timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
115{
116 unsigned long flags = (unsigned long)timer->base & TIMER_FLAG_MASK;
117
118 timer->base = (struct tvec_base *)((unsigned long)(new_base) | flags);
119}
120
121static unsigned long round_jiffies_common(unsigned long j, int cpu,
122 bool force_up)
123{
124 int rem;
125 unsigned long original = j;
126
127 /*
128 * We don't want all cpus firing their timers at once hitting the
129 * same lock or cachelines, so we skew each extra cpu with an extra
130 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
131 * already did this.
132 * The skew is done by adding 3*cpunr, then round, then subtract this
133 * extra offset again.
134 */
135 j += cpu * 3;
136
137 rem = j % HZ;
138
139 /*
140 * If the target jiffie is just after a whole second (which can happen
141 * due to delays of the timer irq, long irq off times etc etc) then
142 * we should round down to the whole second, not up. Use 1/4th second
143 * as cutoff for this rounding as an extreme upper bound for this.
144 * But never round down if @force_up is set.
145 */
146 if (rem < HZ/4 && !force_up) /* round down */
147 j = j - rem;
148 else /* round up */
149 j = j - rem + HZ;
150
151 /* now that we have rounded, subtract the extra skew again */
152 j -= cpu * 3;
153
154 /*
155 * Make sure j is still in the future. Otherwise return the
156 * unmodified value.
157 */
158 return time_is_after_jiffies(j) ? j : original;
159}
160
161/**
162 * __round_jiffies - function to round jiffies to a full second
163 * @j: the time in (absolute) jiffies that should be rounded
164 * @cpu: the processor number on which the timeout will happen
165 *
166 * __round_jiffies() rounds an absolute time in the future (in jiffies)
167 * up or down to (approximately) full seconds. This is useful for timers
168 * for which the exact time they fire does not matter too much, as long as
169 * they fire approximately every X seconds.
170 *
171 * By rounding these timers to whole seconds, all such timers will fire
172 * at the same time, rather than at various times spread out. The goal
173 * of this is to have the CPU wake up less, which saves power.
174 *
175 * The exact rounding is skewed for each processor to avoid all
176 * processors firing at the exact same time, which could lead
177 * to lock contention or spurious cache line bouncing.
178 *
179 * The return value is the rounded version of the @j parameter.
180 */
181unsigned long __round_jiffies(unsigned long j, int cpu)
182{
183 return round_jiffies_common(j, cpu, false);
184}
185EXPORT_SYMBOL_GPL(__round_jiffies);
186
187/**
188 * __round_jiffies_relative - function to round jiffies to a full second
189 * @j: the time in (relative) jiffies that should be rounded
190 * @cpu: the processor number on which the timeout will happen
191 *
192 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
193 * up or down to (approximately) full seconds. This is useful for timers
194 * for which the exact time they fire does not matter too much, as long as
195 * they fire approximately every X seconds.
196 *
197 * By rounding these timers to whole seconds, all such timers will fire
198 * at the same time, rather than at various times spread out. The goal
199 * of this is to have the CPU wake up less, which saves power.
200 *
201 * The exact rounding is skewed for each processor to avoid all
202 * processors firing at the exact same time, which could lead
203 * to lock contention or spurious cache line bouncing.
204 *
205 * The return value is the rounded version of the @j parameter.
206 */
207unsigned long __round_jiffies_relative(unsigned long j, int cpu)
208{
209 unsigned long j0 = jiffies;
210
211 /* Use j0 because jiffies might change while we run */
212 return round_jiffies_common(j + j0, cpu, false) - j0;
213}
214EXPORT_SYMBOL_GPL(__round_jiffies_relative);
215
216/**
217 * round_jiffies - function to round jiffies to a full second
218 * @j: the time in (absolute) jiffies that should be rounded
219 *
220 * round_jiffies() rounds an absolute time in the future (in jiffies)
221 * up or down to (approximately) full seconds. This is useful for timers
222 * for which the exact time they fire does not matter too much, as long as
223 * they fire ap