blob: 99b00a25f88b3efc4c0558c78ece429d3d3300c3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/timer.c
3 *
john stultz85240702007-05-08 00:27:59 -07004 * Kernel internal timers, basic process system calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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/module.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>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070029#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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>
Adrian Bunk97a41e22006-01-08 01:02:17 -080037#include <linux/delay.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080038#include <linux/tick.h>
Ingo Molnar82f67cd2007-02-16 01:28:13 -080039#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/uaccess.h>
42#include <asm/unistd.h>
43#include <asm/div64.h>
44#include <asm/timex.h>
45#include <asm/io.h>
46
Thomas Gleixnerecea8d12005-10-30 15:03:00 -080047u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
48
49EXPORT_SYMBOL(jiffies_64);
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * per-CPU timer vector definitions:
53 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
55#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
56#define TVN_SIZE (1 << TVN_BITS)
57#define TVR_SIZE (1 << TVR_BITS)
58#define TVN_MASK (TVN_SIZE - 1)
59#define TVR_MASK (TVR_SIZE - 1)
60
Pavel Macheka6fa8e52008-01-30 13:30:00 +010061struct tvec {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct list_head vec[TVN_SIZE];
Pavel Macheka6fa8e52008-01-30 13:30:00 +010063};
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Pavel Macheka6fa8e52008-01-30 13:30:00 +010065struct tvec_root {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 struct list_head vec[TVR_SIZE];
Pavel Macheka6fa8e52008-01-30 13:30:00 +010067};
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Pavel Macheka6fa8e52008-01-30 13:30:00 +010069struct tvec_base {
Oleg Nesterov3691c512006-03-31 02:30:30 -080070 spinlock_t lock;
71 struct timer_list *running_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unsigned long timer_jiffies;
Pavel Macheka6fa8e52008-01-30 13:30:00 +010073 struct tvec_root tv1;
74 struct tvec tv2;
75 struct tvec tv3;
76 struct tvec tv4;
77 struct tvec tv5;
Venki Pallipadi6e453a62007-05-08 00:27:44 -070078} ____cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Pavel Macheka6fa8e52008-01-30 13:30:00 +010080struct tvec_base boot_tvec_bases;
Oleg Nesterov3691c512006-03-31 02:30:30 -080081EXPORT_SYMBOL(boot_tvec_bases);
Pavel Macheka6fa8e52008-01-30 13:30:00 +010082static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Venki Pallipadi6e453a62007-05-08 00:27:44 -070084/*
Pavel Macheka6fa8e52008-01-30 13:30:00 +010085 * Note that all tvec_bases are 2 byte aligned and lower bit of
Venki Pallipadi6e453a62007-05-08 00:27:44 -070086 * base in timer_list is guaranteed to be zero. Use the LSB for
87 * the new flag to indicate whether the timer is deferrable
88 */
89#define TBASE_DEFERRABLE_FLAG (0x1)
90
91/* Functions below help us manage 'deferrable' flag */
Pavel Macheka6fa8e52008-01-30 13:30:00 +010092static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -070093{
akpm@linux-foundation.orge9910842007-05-10 03:16:01 -070094 return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);
Venki Pallipadi6e453a62007-05-08 00:27:44 -070095}
96
Pavel Macheka6fa8e52008-01-30 13:30:00 +010097static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -070098{
Pavel Macheka6fa8e52008-01-30 13:30:00 +010099 return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700100}
101
102static inline void timer_set_deferrable(struct timer_list *timer)
103{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100104 timer->base = ((struct tvec_base *)((unsigned long)(timer->base) |
Thomas Gleixner68194572007-07-19 01:49:16 -0700105 TBASE_DEFERRABLE_FLAG));
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700106}
107
108static inline void
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100109timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700110{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100111 timer->base = (struct tvec_base *)((unsigned long)(new_base) |
Thomas Gleixner68194572007-07-19 01:49:16 -0700112 tbase_get_deferrable(timer->base));
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700113}
114
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800115/**
116 * __round_jiffies - function to round jiffies to a full second
117 * @j: the time in (absolute) jiffies that should be rounded
118 * @cpu: the processor number on which the timeout will happen
119 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800120 * __round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800121 * up or down to (approximately) full seconds. This is useful for timers
122 * for which the exact time they fire does not matter too much, as long as
123 * they fire approximately every X seconds.
124 *
125 * By rounding these timers to whole seconds, all such timers will fire
126 * at the same time, rather than at various times spread out. The goal
127 * of this is to have the CPU wake up less, which saves power.
128 *
129 * The exact rounding is skewed for each processor to avoid all
130 * processors firing at the exact same time, which could lead
131 * to lock contention or spurious cache line bouncing.
132 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800133 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800134 */
135unsigned long __round_jiffies(unsigned long j, int cpu)
136{
137 int rem;
138 unsigned long original = j;
139
140 /*
141 * We don't want all cpus firing their timers at once hitting the
142 * same lock or cachelines, so we skew each extra cpu with an extra
143 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
144 * already did this.
145 * The skew is done by adding 3*cpunr, then round, then subtract this
146 * extra offset again.
147 */
148 j += cpu * 3;
149
150 rem = j % HZ;
151
152 /*
153 * If the target jiffie is just after a whole second (which can happen
154 * due to delays of the timer irq, long irq off times etc etc) then
155 * we should round down to the whole second, not up. Use 1/4th second
156 * as cutoff for this rounding as an extreme upper bound for this.
157 */
158 if (rem < HZ/4) /* round down */
159 j = j - rem;
160 else /* round up */
161 j = j - rem + HZ;
162
163 /* now that we have rounded, subtract the extra skew again */
164 j -= cpu * 3;
165
166 if (j <= jiffies) /* rounding ate our timeout entirely; */
167 return original;
168 return j;
169}
170EXPORT_SYMBOL_GPL(__round_jiffies);
171
172/**
173 * __round_jiffies_relative - function to round jiffies to a full second
174 * @j: the time in (relative) jiffies that should be rounded
175 * @cpu: the processor number on which the timeout will happen
176 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800177 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800178 * up or down to (approximately) full seconds. This is useful for timers
179 * for which the exact time they fire does not matter too much, as long as
180 * they fire approximately every X seconds.
181 *
182 * By rounding these timers to whole seconds, all such timers will fire
183 * at the same time, rather than at various times spread out. The goal
184 * of this is to have the CPU wake up less, which saves power.
185 *
186 * The exact rounding is skewed for each processor to avoid all
187 * processors firing at the exact same time, which could lead
188 * to lock contention or spurious cache line bouncing.
189 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800190 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800191 */
192unsigned long __round_jiffies_relative(unsigned long j, int cpu)
193{
194 /*
195 * In theory the following code can skip a jiffy in case jiffies
196 * increments right between the addition and the later subtraction.
197 * However since the entire point of this function is to use approximate
198 * timeouts, it's entirely ok to not handle that.
199 */
200 return __round_jiffies(j + jiffies, cpu) - jiffies;
201}
202EXPORT_SYMBOL_GPL(__round_jiffies_relative);
203
204/**
205 * round_jiffies - function to round jiffies to a full second
206 * @j: the time in (absolute) jiffies that should be rounded
207 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800208 * round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800209 * up or down to (approximately) full seconds. This is useful for timers
210 * for which the exact time they fire does not matter too much, as long as
211 * they fire approximately every X seconds.
212 *
213 * By rounding these timers to whole seconds, all such timers will fire
214 * at the same time, rather than at various times spread out. The goal
215 * of this is to have the CPU wake up less, which saves power.
216 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800217 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800218 */
219unsigned long round_jiffies(unsigned long j)
220{
221 return __round_jiffies(j, raw_smp_processor_id());
222}
223EXPORT_SYMBOL_GPL(round_jiffies);
224
225/**
226 * round_jiffies_relative - function to round jiffies to a full second
227 * @j: the time in (relative) jiffies that should be rounded
228 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800229 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800230 * up or down to (approximately) full seconds. This is useful for timers
231 * for which the exact time they fire does not matter too much, as long as
232 * they fire approximately every X seconds.
233 *
234 * By rounding these timers to whole seconds, all such timers will fire
235 * at the same time, rather than at various times spread out. The goal
236 * of this is to have the CPU wake up less, which saves power.
237 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800238 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800239 */
240unsigned long round_jiffies_relative(unsigned long j)
241{
242 return __round_jiffies_relative(j, raw_smp_processor_id());
243}
244EXPORT_SYMBOL_GPL(round_jiffies_relative);
245
246
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100247static inline void set_running_timer(struct tvec_base *base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct timer_list *timer)
249{
250#ifdef CONFIG_SMP
Oleg Nesterov3691c512006-03-31 02:30:30 -0800251 base->running_timer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#endif
253}
254
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100255static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 unsigned long expires = timer->expires;
258 unsigned long idx = expires - base->timer_jiffies;
259 struct list_head *vec;
260
261 if (idx < TVR_SIZE) {
262 int i = expires & TVR_MASK;
263 vec = base->tv1.vec + i;
264 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
265 int i = (expires >> TVR_BITS) & TVN_MASK;
266 vec = base->tv2.vec + i;
267 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
268 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
269 vec = base->tv3.vec + i;
270 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
271 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
272 vec = base->tv4.vec + i;
273 } else if ((signed long) idx < 0) {
274 /*
275 * Can happen if you add a timer with expires == jiffies,
276 * or you set a timer to go off in the past
277 */
278 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
279 } else {
280 int i;
281 /* If the timeout is larger than 0xffffffff on 64-bit
282 * architectures then we use the maximum timeout:
283 */
284 if (idx > 0xffffffffUL) {
285 idx = 0xffffffffUL;
286 expires = idx + base->timer_jiffies;
287 }
288 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
289 vec = base->tv5.vec + i;
290 }
291 /*
292 * Timers are FIFO:
293 */
294 list_add_tail(&timer->entry, vec);
295}
296
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800297#ifdef CONFIG_TIMER_STATS
298void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
299{
300 if (timer->start_site)
301 return;
302
303 timer->start_site = addr;
304 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
305 timer->start_pid = current->pid;
306}
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700307
308static void timer_stats_account_timer(struct timer_list *timer)
309{
310 unsigned int flag = 0;
311
312 if (unlikely(tbase_get_deferrable(timer->base)))
313 flag |= TIMER_STATS_FLAG_DEFERRABLE;
314
315 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
316 timer->function, timer->start_comm, flag);
317}
318
319#else
320static void timer_stats_account_timer(struct timer_list *timer) {}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800321#endif
322
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700323/**
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700324 * init_timer - initialize a timer.
325 * @timer: the timer to be initialized
326 *
327 * init_timer() must be done to a timer prior calling *any* of the
328 * other timer functions.
329 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800330void init_timer(struct timer_list *timer)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700331{
332 timer->entry.next = NULL;
Paul Mackerrasbfe5d832006-06-25 05:47:14 -0700333 timer->base = __raw_get_cpu_var(tvec_bases);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800334#ifdef CONFIG_TIMER_STATS
335 timer->start_site = NULL;
336 timer->start_pid = -1;
337 memset(timer->start_comm, 0, TASK_COMM_LEN);
338#endif
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700339}
340EXPORT_SYMBOL(init_timer);
341
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800342void init_timer_deferrable(struct timer_list *timer)
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700343{
344 init_timer(timer);
345 timer_set_deferrable(timer);
346}
347EXPORT_SYMBOL(init_timer_deferrable);
348
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700349static inline void detach_timer(struct timer_list *timer,
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800350 int clear_pending)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700351{
352 struct list_head *entry = &timer->entry;
353
354 __list_del(entry->prev, entry->next);
355 if (clear_pending)
356 entry->next = NULL;
357 entry->prev = LIST_POISON2;
358}
359
360/*
Oleg Nesterov3691c512006-03-31 02:30:30 -0800361 * We are using hashed locking: holding per_cpu(tvec_bases).lock
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700362 * means that all timers which are tied to this base via timer->base are
363 * locked, and the base itself is locked too.
364 *
365 * So __run_timers/migrate_timers can safely modify all timers which could
366 * be found on ->tvX lists.
367 *
368 * When the timer's base is locked, and the timer removed from list, it is
369 * possible to set timer->base = NULL and drop the lock: the timer remains
370 * locked.
371 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100372static struct tvec_base *lock_timer_base(struct timer_list *timer,
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700373 unsigned long *flags)
Josh Triplett89e7e3742006-09-29 01:59:36 -0700374 __acquires(timer->base->lock)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700375{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100376 struct tvec_base *base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700377
378 for (;;) {
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100379 struct tvec_base *prelock_base = timer->base;
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700380 base = tbase_get_base(prelock_base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700381 if (likely(base != NULL)) {
382 spin_lock_irqsave(&base->lock, *flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700383 if (likely(prelock_base == timer->base))
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700384 return base;
385 /* The timer has migrated to another CPU */
386 spin_unlock_irqrestore(&base->lock, *flags);
387 }
388 cpu_relax();
389 }
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392int __mod_timer(struct timer_list *timer, unsigned long expires)
393{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100394 struct tvec_base *base, *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 unsigned long flags;
396 int ret = 0;
397
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800398 timer_stats_timer_set_start_info(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 BUG_ON(!timer->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700401 base = lock_timer_base(timer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700403 if (timer_pending(timer)) {
404 detach_timer(timer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 ret = 1;
406 }
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700407
Jan Beulicha4a61982006-03-24 03:15:54 -0800408 new_base = __get_cpu_var(tvec_bases);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700409
Oleg Nesterov3691c512006-03-31 02:30:30 -0800410 if (base != new_base) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700411 /*
412 * We are trying to schedule the timer on the local CPU.
413 * However we can't change timer's base while it is running,
414 * otherwise del_timer_sync() can't detect that the timer's
415 * handler yet has not finished. This also guarantees that
416 * the timer is serialized wrt itself.
417 */
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800418 if (likely(base->running_timer != timer)) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700419 /* See the comment in lock_timer_base() */
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700420 timer_set_base(timer, NULL);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700421 spin_unlock(&base->lock);
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800422 base = new_base;
423 spin_lock(&base->lock);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700424 timer_set_base(timer, base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700425 }
426 }
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 timer->expires = expires;
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800429 internal_add_timer(base, timer);
430 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 return ret;
433}
434
435EXPORT_SYMBOL(__mod_timer);
436
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700437/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * add_timer_on - start a timer on a particular CPU
439 * @timer: the timer to be added
440 * @cpu: the CPU to start it on
441 *
442 * This is not very scalable on SMP. Double adds are not possible.
443 */
444void add_timer_on(struct timer_list *timer, int cpu)
445{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100446 struct tvec_base *base = per_cpu(tvec_bases, cpu);
Thomas Gleixner68194572007-07-19 01:49:16 -0700447 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700448
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800449 timer_stats_timer_set_start_info(timer);
Thomas Gleixner68194572007-07-19 01:49:16 -0700450 BUG_ON(timer_pending(timer) || !timer->function);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800451 spin_lock_irqsave(&base->lock, flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700452 timer_set_base(timer, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 internal_add_timer(base, timer);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800454 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700458/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * mod_timer - modify a timer's timeout
460 * @timer: the timer to be modified
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700461 * @expires: new timeout in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800463 * mod_timer() is a more efficient way to update the expire field of an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * active timer (if the timer is inactive it will be activated)
465 *
466 * mod_timer(timer, expires) is equivalent to:
467 *
468 * del_timer(timer); timer->expires = expires; add_timer(timer);
469 *
470 * Note that if there are multiple unserialized concurrent users of the
471 * same timer, then mod_timer() is the only safe way to modify the timeout,
472 * since add_timer() cannot modify an already running timer.
473 *
474 * The function returns whether it has modified a pending timer or not.
475 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
476 * active timer returns 1.)
477 */
478int mod_timer(struct timer_list *timer, unsigned long expires)
479{
480 BUG_ON(!timer->function);
481
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800482 timer_stats_timer_set_start_info(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /*
484 * This is a common optimization triggered by the
485 * networking code - if the timer is re-modified
486 * to be the same thing then just return:
487 */
488 if (timer->expires == expires && timer_pending(timer))
489 return 1;
490
491 return __mod_timer(timer, expires);
492}
493
494EXPORT_SYMBOL(mod_timer);
495
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700496/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * del_timer - deactive a timer.
498 * @timer: the timer to be deactivated
499 *
500 * del_timer() deactivates a timer - this works on both active and inactive
501 * timers.
502 *
503 * The function returns whether it has deactivated a pending timer or not.
504 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
505 * active timer returns 1.)
506 */
507int del_timer(struct timer_list *timer)
508{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100509 struct tvec_base *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700511 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800513 timer_stats_timer_clear_start_info(timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700514 if (timer_pending(timer)) {
515 base = lock_timer_base(timer, &flags);
516 if (timer_pending(timer)) {
517 detach_timer(timer, 1);
518 ret = 1;
519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700523 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526EXPORT_SYMBOL(del_timer);
527
528#ifdef CONFIG_SMP
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700529/**
530 * try_to_del_timer_sync - Try to deactivate a timer
531 * @timer: timer do del
532 *
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700533 * This function tries to deactivate a timer. Upon successful (ret >= 0)
534 * exit the timer is not queued and the handler is not running on any CPU.
535 *
536 * It must not be called from interrupt contexts.
537 */
538int try_to_del_timer_sync(struct timer_list *timer)
539{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100540 struct tvec_base *base;
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700541 unsigned long flags;
542 int ret = -1;
543
544 base = lock_timer_base(timer, &flags);
545
546 if (base->running_timer == timer)
547 goto out;
548
549 ret = 0;
550 if (timer_pending(timer)) {
551 detach_timer(timer, 1);
552 ret = 1;
553 }
554out:
555 spin_unlock_irqrestore(&base->lock, flags);
556
557 return ret;
558}
559
David Howellse19dff12007-04-26 15:46:56 -0700560EXPORT_SYMBOL(try_to_del_timer_sync);
561
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700562/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * del_timer_sync - deactivate a timer and wait for the handler to finish.
564 * @timer: the timer to be deactivated
565 *
566 * This function only differs from del_timer() on SMP: besides deactivating
567 * the timer it also makes sure the handler has finished executing on other
568 * CPUs.
569 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800570 * Synchronization rules: Callers must prevent restarting of the timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 * otherwise this function is meaningless. It must not be called from
572 * interrupt contexts. The caller must not hold locks which would prevent
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700573 * completion of the timer's handler. The timer's handler must not call
574 * add_timer_on(). Upon exit the timer is not queued and the handler is
575 * not running on any CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 *
577 * The function returns whether it has deactivated a pending timer or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 */
579int del_timer_sync(struct timer_list *timer)
580{
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700581 for (;;) {
582 int ret = try_to_del_timer_sync(timer);
583 if (ret >= 0)
584 return ret;
Andrew Mortona0009652006-07-14 00:24:06 -0700585 cpu_relax();
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589EXPORT_SYMBOL(del_timer_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590#endif
591
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100592static int cascade(struct tvec_base *base, struct tvec *tv, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 /* cascade all the timers from tv up one level */
Porpoise3439dd82006-06-23 02:05:56 -0700595 struct timer_list *timer, *tmp;
596 struct list_head tv_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Porpoise3439dd82006-06-23 02:05:56 -0700598 list_replace_init(tv->vec + index, &tv_list);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /*
Porpoise3439dd82006-06-23 02:05:56 -0700601 * We are removing _all_ timers from the list, so we
602 * don't have to detach them individually.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 */
Porpoise3439dd82006-06-23 02:05:56 -0700604 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700605 BUG_ON(tbase_get_base(timer->base) != base);
Porpoise3439dd82006-06-23 02:05:56 -0700606 internal_add_timer(base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 return index;
610}
611
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700612#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
613
614/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 * __run_timers - run all expired timers (if any) on this CPU.
616 * @base: the timer vector to be processed.
617 *
618 * This function cascades all vectors and executes all expired timer
619 * vectors.
620 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100621static inline void __run_timers(struct tvec_base *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct timer_list *timer;
624
Oleg Nesterov3691c512006-03-31 02:30:30 -0800625 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 while (time_after_eq(jiffies, base->timer_jiffies)) {
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700627 struct list_head work_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 struct list_head *head = &work_list;
Thomas Gleixner68194572007-07-19 01:49:16 -0700629 int index = base->timer_jiffies & TVR_MASK;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /*
632 * Cascade timers:
633 */
634 if (!index &&
635 (!cascade(base, &base->tv2, INDEX(0))) &&
636 (!cascade(base, &base->tv3, INDEX(1))) &&
637 !cascade(base, &base->tv4, INDEX(2)))
638 cascade(base, &base->tv5, INDEX(3));
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700639 ++base->timer_jiffies;
640 list_replace_init(base->tv1.vec + index, &work_list);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700641 while (!list_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 void (*fn)(unsigned long);
643 unsigned long data;
644
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700645 timer = list_first_entry(head, struct timer_list,entry);
Thomas Gleixner68194572007-07-19 01:49:16 -0700646 fn = timer->function;
647 data = timer->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800649 timer_stats_account_timer(timer);
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 set_running_timer(base, timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700652 detach_timer(timer, 1);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800653 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700655 int preempt_count = preempt_count();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 fn(data);
657 if (preempt_count != preempt_count()) {
Pavel Machek4c9dc642008-01-30 13:30:00 +0100658 printk(KERN_ERR "huh, entered %p "
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700659 "with preempt_count %08x, exited"
660 " with %08x?\n",
661 fn, preempt_count,
662 preempt_count());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 BUG();
664 }
665 }
Oleg Nesterov3691c512006-03-31 02:30:30 -0800666 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668 }
669 set_running_timer(base, NULL);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800670 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800673#if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674/*
675 * Find out when the next timer event is due to happen. This
676 * is used on S/390 to stop all activity when a cpus is idle.
677 * This functions needs to be called disabled.
678 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100679static unsigned long __next_timer_interrupt(struct tvec_base *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800681 unsigned long timer_jiffies = base->timer_jiffies;
Thomas Gleixnereaad0842007-05-29 23:47:39 +0200682 unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800683 int index, slot, array, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct timer_list *nte;
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100685 struct tvec *varray[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 /* Look for timer events in tv1. */
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800688 index = slot = timer_jiffies & TVR_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800690 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
Thomas Gleixner68194572007-07-19 01:49:16 -0700691 if (tbase_get_deferrable(nte->base))
692 continue;
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700693
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800694 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800696 /* Look at the cascade bucket(s)? */
697 if (!index || slot < index)
698 goto cascade;
699 return expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800701 slot = (slot + 1) & TVR_MASK;
702 } while (slot != index);
703
704cascade:
705 /* Calculate the next cascade event */
706 if (index)
707 timer_jiffies += TVR_SIZE - index;
708 timer_jiffies >>= TVR_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 /* Check tv2-tv5. */
711 varray[0] = &base->tv2;
712 varray[1] = &base->tv3;
713 varray[2] = &base->tv4;
714 varray[3] = &base->tv5;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800715
716 for (array = 0; array < 4; array++) {
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100717 struct tvec *varp = varray[array];
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800718
719 index = slot = timer_jiffies & TVN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800721 list_for_each_entry(nte, varp->vec + slot, entry) {
722 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (time_before(nte->expires, expires))
724 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800725 }
726 /*
727 * Do we still search for the first timer or are
728 * we looking up the cascade buckets ?
729 */
730 if (found) {
731 /* Look at the cascade bucket(s)? */
732 if (!index || slot < index)
733 break;
734 return expires;
735 }
736 slot = (slot + 1) & TVN_MASK;
737 } while (slot != index);
738
739 if (index)
740 timer_jiffies += TVN_SIZE - index;
741 timer_jiffies >>= TVN_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800743 return expires;
744}
745
746/*
747 * Check, if the next hrtimer event is before the next timer wheel
748 * event:
749 */
750static unsigned long cmp_next_hrtimer_event(unsigned long now,
751 unsigned long expires)
752{
753 ktime_t hr_delta = hrtimer_get_next_event();
754 struct timespec tsdelta;
Thomas Gleixner9501b6c2007-03-25 14:31:17 +0200755 unsigned long delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800756
757 if (hr_delta.tv64 == KTIME_MAX)
758 return expires;
759
Thomas Gleixner9501b6c2007-03-25 14:31:17 +0200760 /*
761 * Expired timer available, let it expire in the next tick
762 */
763 if (hr_delta.tv64 <= 0)
764 return now + 1;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800765
766 tsdelta = ktime_to_timespec(hr_delta);
Thomas Gleixner9501b6c2007-03-25 14:31:17 +0200767 delta = timespec_to_jiffies(&tsdelta);
Thomas Gleixnereaad0842007-05-29 23:47:39 +0200768
769 /*
770 * Limit the delta to the max value, which is checked in
771 * tick_nohz_stop_sched_tick():
772 */
773 if (delta > NEXT_TIMER_MAX_DELTA)
774 delta = NEXT_TIMER_MAX_DELTA;
775
Thomas Gleixner9501b6c2007-03-25 14:31:17 +0200776 /*
777 * Take rounding errors in to account and make sure, that it
778 * expires in the next tick. Otherwise we go into an endless
779 * ping pong due to tick_nohz_stop_sched_tick() retriggering
780 * the timer softirq
781 */
782 if (delta < 1)
783 delta = 1;
784 now += delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800785 if (time_before(now, expires))
786 return now;
787 return expires;
788}
789
790/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800791 * get_next_timer_interrupt - return the jiffy of the next pending timer
Randy Dunlap05fb6bf2007-02-28 20:12:13 -0800792 * @now: current time (in jiffies)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800793 */
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800794unsigned long get_next_timer_interrupt(unsigned long now)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800795{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100796 struct tvec_base *base = __get_cpu_var(tvec_bases);
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800797 unsigned long expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800798
799 spin_lock(&base->lock);
800 expires = __next_timer_interrupt(base);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800801 spin_unlock(&base->lock);
Tony Lindgren69239742006-03-06 15:42:45 -0800802
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800803 if (time_before_eq(expires, now))
804 return now;
Zachary Amsden0662b712006-05-20 15:00:24 -0700805
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800806 return cmp_next_hrtimer_event(now, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800808
809#ifdef CONFIG_NO_IDLE_HZ
810unsigned long next_timer_interrupt(void)
811{
812 return get_next_timer_interrupt(jiffies);
813}
814#endif
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816#endif
817
Paul Mackerrasfa13a5a2007-11-09 22:39:38 +0100818#ifndef CONFIG_VIRT_CPU_ACCOUNTING
819void account_process_tick(struct task_struct *p, int user_tick)
820{
Michael Neuling06b8e872008-02-06 01:36:12 -0800821 cputime_t one_jiffy = jiffies_to_cputime(1);
822
Paul Mackerrasfa13a5a2007-11-09 22:39:38 +0100823 if (user_tick) {
Michael Neuling06b8e872008-02-06 01:36:12 -0800824 account_user_time(p, one_jiffy);
825 account_user_time_scaled(p, cputime_to_scaled(one_jiffy));
Paul Mackerrasfa13a5a2007-11-09 22:39:38 +0100826 } else {
Michael Neuling06b8e872008-02-06 01:36:12 -0800827 account_system_time(p, HARDIRQ_OFFSET, one_jiffy);
828 account_system_time_scaled(p, cputime_to_scaled(one_jiffy));
Paul Mackerrasfa13a5a2007-11-09 22:39:38 +0100829 }
830}
831#endif
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833/*
Daniel Walker5b4db0c2007-10-18 03:06:11 -0700834 * Called from the timer interrupt handler to charge one tick to the current
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 * process. user_tick is 1 if the tick is user time, 0 for system.
836 */
837void update_process_times(int user_tick)
838{
839 struct task_struct *p = current;
840 int cpu = smp_processor_id();
841
842 /* Note: this timer irq context must be accounted for as well. */
Paul Mackerrasfa13a5a2007-11-09 22:39:38 +0100843 account_process_tick(p, user_tick);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 run_local_timers();
845 if (rcu_pending(cpu))
846 rcu_check_callbacks(cpu, user_tick);
847 scheduler_tick();
Thomas Gleixner68194572007-07-19 01:49:16 -0700848 run_posix_cpu_timers(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851/*
852 * Nr of active tasks - counted in fixed-point numbers
853 */
854static unsigned long count_active_tasks(void)
855{
Jack Steinerdb1b1fe2006-03-31 02:31:21 -0800856 return nr_active() * FIXED_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859/*
860 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
861 * imply that avenrun[] is the standard name for this kind of thing.
862 * Nothing else seems to be standardized: the fractional size etc
863 * all seem to differ on different machines.
864 *
865 * Requires xtime_lock to access.
866 */
867unsigned long avenrun[3];
868
869EXPORT_SYMBOL(avenrun);
870
871/*
872 * calc_load - given tick count, update the avenrun load estimates.
873 * This is called while holding a write_lock on xtime_lock.
874 */
875static inline void calc_load(unsigned long ticks)
876{
877 unsigned long active_tasks; /* fixed-point */
878 static int count = LOAD_FREQ;
879
Eric Dumazetcd7175e2006-12-13 00:35:45 -0800880 count -= ticks;
881 if (unlikely(count < 0)) {
882 active_tasks = count_active_tasks();
883 do {
884 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
885 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
886 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
887 count += LOAD_FREQ;
888 } while (count < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890}
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * This function runs timers and the timer-tq in bottom half context.
894 */
895static void run_timer_softirq(struct softirq_action *h)
896{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100897 struct tvec_base *base = __get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100899 hrtimer_run_pending();
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (time_after_eq(jiffies, base->timer_jiffies))
902 __run_timers(base);
903}
904
905/*
906 * Called by the local, per-CPU timer interrupt on SMP.
907 */
908void run_local_timers(void)
909{
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100910 hrtimer_run_queues();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 raise_softirq(TIMER_SOFTIRQ);
Ingo Molnar6687a972006-03-24 03:18:41 -0800912 softlockup_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915/*
916 * Called by the timer interrupt. xtime_lock must already be taken
917 * by the timer IRQ!
918 */
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700919static inline void update_times(unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
john stultzad596172006-06-26 00:25:06 -0700921 update_wall_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 calc_load(ticks);
923}
Thomas Gleixner68194572007-07-19 01:49:16 -0700924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/*
926 * The 64-bit jiffies value is not atomic - you MUST NOT read it
927 * without sampling the sequence number in xtime_lock.
928 * jiffies is defined in the linker script...
929 */
930
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700931void do_timer(unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700933 jiffies_64 += ticks;
934 update_times(ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937#ifdef __ARCH_WANT_SYS_ALARM
938
939/*
940 * For backwards compatibility? This can be done in libc so Alpha
941 * and all newer ports shouldn't need it.
942 */
943asmlinkage unsigned long sys_alarm(unsigned int seconds)
944{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -0800945 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
948#endif
949
950#ifndef __alpha__
951
952/*
953 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
954 * should be moved into arch/i386 instead?
955 */
956
957/**
958 * sys_getpid - return the thread group id of the current process
959 *
960 * Note, despite the name, this returns the tgid not the pid. The tgid and
961 * the pid are identical unless CLONE_THREAD was specified on clone() in
962 * which case the tgid is the same in all threads of the same group.
963 *
964 * This is SMP safe as current->tgid does not change.
965 */
966asmlinkage long sys_getpid(void)
967{
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700968 return task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
971/*
Kirill Korotaev6997a6f2006-08-13 23:24:23 -0700972 * Accessing ->real_parent is not SMP-safe, it could
973 * change from under us. However, we can use a stale
974 * value of ->real_parent under rcu_read_lock(), see
975 * release_task()->call_rcu(delayed_put_task_struct).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 */
977asmlinkage long sys_getppid(void)
978{
979 int pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Kirill Korotaev6997a6f2006-08-13 23:24:23 -0700981 rcu_read_lock();
Pavel Emelyanov6c5f3e72008-02-08 04:19:20 -0800982 pid = task_tgid_vnr(current->real_parent);
Kirill Korotaev6997a6f2006-08-13 23:24:23 -0700983 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return pid;
986}
987
988asmlinkage long sys_getuid(void)
989{
990 /* Only we change this so SMP safe */
991 return current->uid;
992}
993
994asmlinkage long sys_geteuid(void)
995{
996 /* Only we change this so SMP safe */
997 return current->euid;
998}
999
1000asmlinkage long sys_getgid(void)
1001{
1002 /* Only we change this so SMP safe */
1003 return current->gid;
1004}
1005
1006asmlinkage long sys_getegid(void)
1007{
1008 /* Only we change this so SMP safe */
1009 return current->egid;
1010}
1011
1012#endif
1013
1014static void process_timeout(unsigned long __data)
1015{
Ingo Molnar36c8b582006-07-03 00:25:41 -07001016 wake_up_process((struct task_struct *)__data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019/**
1020 * schedule_timeout - sleep until timeout
1021 * @timeout: timeout value in jiffies
1022 *
1023 * Make the current task sleep until @timeout jiffies have
1024 * elapsed. The routine will return immediately unless
1025 * the current task state has been set (see set_current_state()).
1026 *
1027 * You can set the task state as follows -
1028 *
1029 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1030 * pass before the routine returns. The routine will return 0
1031 *
1032 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1033 * delivered to the current task. In this case the remaining time
1034 * in jiffies will be returned, or 0 if the timer expired in time
1035 *
1036 * The current task state is guaranteed to be TASK_RUNNING when this
1037 * routine returns.
1038 *
1039 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1040 * the CPU away without a bound on the timeout. In this case the return
1041 * value will be %MAX_SCHEDULE_TIMEOUT.
1042 *
1043 * In all cases the return value is guaranteed to be non-negative.
1044 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001045signed long __sched schedule_timeout(signed long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct timer_list timer;
1048 unsigned long expire;
1049
1050 switch (timeout)
1051 {
1052 case MAX_SCHEDULE_TIMEOUT:
1053 /*
1054 * These two special cases are useful to be comfortable
1055 * in the caller. Nothing more. We could take
1056 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1057 * but I' d like to return a valid offset (>=0) to allow
1058 * the caller to do everything it want with the retval.
1059 */
1060 schedule();
1061 goto out;
1062 default:
1063 /*
1064 * Another bit of PARANOID. Note that the retval will be
1065 * 0 since no piece of kernel is supposed to do a check
1066 * for a negative retval of schedule_timeout() (since it
1067 * should never happens anyway). You just have the printk()
1068 * that will tell you if something is gone wrong and where.
1069 */
Andrew Morton5b149bc2006-12-22 01:10:14 -08001070 if (timeout < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 printk(KERN_ERR "schedule_timeout: wrong timeout "
Andrew Morton5b149bc2006-12-22 01:10:14 -08001072 "value %lx\n", timeout);
1073 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 current->state = TASK_RUNNING;
1075 goto out;
1076 }
1077 }
1078
1079 expire = timeout + jiffies;
1080
Oleg Nesterova8db2db2005-10-30 15:01:38 -08001081 setup_timer(&timer, process_timeout, (unsigned long)current);
1082 __mod_timer(&timer, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 schedule();
1084 del_singleshot_timer_sync(&timer);
1085
1086 timeout = expire - jiffies;
1087
1088 out:
1089 return timeout < 0 ? 0 : timeout;
1090}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091EXPORT_SYMBOL(schedule_timeout);
1092
Andrew Morton8a1c1752005-09-13 01:25:15 -07001093/*
1094 * We can use __set_current_state() here because schedule_timeout() calls
1095 * schedule() unconditionally.
1096 */
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001097signed long __sched schedule_timeout_interruptible(signed long timeout)
1098{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001099 __set_current_state(TASK_INTERRUPTIBLE);
1100 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001101}
1102EXPORT_SYMBOL(schedule_timeout_interruptible);
1103
Matthew Wilcox294d5cc2007-12-06 11:59:46 -05001104signed long __sched schedule_timeout_killable(signed long timeout)
1105{
1106 __set_current_state(TASK_KILLABLE);
1107 return schedule_timeout(timeout);
1108}
1109EXPORT_SYMBOL(schedule_timeout_killable);
1110
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001111signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1112{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001113 __set_current_state(TASK_UNINTERRUPTIBLE);
1114 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001115}
1116EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118/* Thread ID - the internal kernel "pid" */
1119asmlinkage long sys_gettid(void)
1120{
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001121 return task_pid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001124/**
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001125 * do_sysinfo - fill in sysinfo struct
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001126 * @info: pointer to buffer to fill
Thomas Gleixner68194572007-07-19 01:49:16 -07001127 */
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001128int do_sysinfo(struct sysinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 unsigned long mem_total, sav_total;
1131 unsigned int mem_unit, bitcount;
1132 unsigned long seq;
1133
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001134 memset(info, 0, sizeof(struct sysinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 do {
1137 struct timespec tp;
1138 seq = read_seqbegin(&xtime_lock);
1139
1140 /*
1141 * This is annoying. The below is the same thing
1142 * posix_get_clock_monotonic() does, but it wants to
1143 * take the lock which we want to cover the loads stuff
1144 * too.
1145 */
1146
1147 getnstimeofday(&tp);
1148 tp.tv_sec += wall_to_monotonic.tv_sec;
1149 tp.tv_nsec += wall_to_monotonic.tv_nsec;
Tomas Janousekd6214142007-07-15 23:39:42 -07001150 monotonic_to_bootbased(&tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1152 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1153 tp.tv_sec++;
1154 }
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001155 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001157 info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1158 info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1159 info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001161 info->procs = nr_threads;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 } while (read_seqretry(&xtime_lock, seq));
1163
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001164 si_meminfo(info);
1165 si_swapinfo(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 /*
1168 * If the sum of all the available memory (i.e. ram + swap)
1169 * is less than can be stored in a 32 bit unsigned long then
1170 * we can be binary compatible with 2.2.x kernels. If not,
1171 * well, in that case 2.2.x was broken anyways...
1172 *
1173 * -Erik Andersen <andersee@debian.org>
1174 */
1175
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001176 mem_total = info->totalram + info->totalswap;
1177 if (mem_total < info->totalram || mem_total < info->totalswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 goto out;
1179 bitcount = 0;
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001180 mem_unit = info->mem_unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 while (mem_unit > 1) {
1182 bitcount++;
1183 mem_unit >>= 1;
1184 sav_total = mem_total;
1185 mem_total <<= 1;
1186 if (mem_total < sav_total)
1187 goto out;
1188 }
1189
1190 /*
1191 * If mem_total did not overflow, multiply all memory values by
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001192 * info->mem_unit and set it to 1. This leaves things compatible
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1194 * kernels...
1195 */
1196
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001197 info->mem_unit = 1;
1198 info->totalram <<= bitcount;
1199 info->freeram <<= bitcount;
1200 info->sharedram <<= bitcount;
1201 info->bufferram <<= bitcount;
1202 info->totalswap <<= bitcount;
1203 info->freeswap <<= bitcount;
1204 info->totalhigh <<= bitcount;
1205 info->freehigh <<= bitcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001207out:
1208 return 0;
1209}
1210
1211asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1212{
1213 struct sysinfo val;
1214
1215 do_sysinfo(&val);
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1218 return -EFAULT;
1219
1220 return 0;
1221}
1222
Ingo Molnard730e882006-07-03 00:25:10 -07001223/*
1224 * lockdep: we want to track each per-CPU base as a separate lock-class,
1225 * but timer-bases are kmalloc()-ed, so we need to attach separate
1226 * keys to them:
1227 */
1228static struct lock_class_key base_lock_keys[NR_CPUS];
1229
Adrian Bunkb4be6252007-12-18 18:05:58 +01001230static int __cpuinit init_timers_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 int j;
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001233 struct tvec_base *base;
Adrian Bunkb4be6252007-12-18 18:05:58 +01001234 static char __cpuinitdata tvec_base_done[NR_CPUS];
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001235
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001236 if (!tvec_base_done[cpu]) {
Jan Beulicha4a61982006-03-24 03:15:54 -08001237 static char boot_done;
1238
Jan Beulicha4a61982006-03-24 03:15:54 -08001239 if (boot_done) {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001240 /*
1241 * The APs use this path later in boot
1242 */
Christoph Lameter94f60302007-07-17 04:03:29 -07001243 base = kmalloc_node(sizeof(*base),
1244 GFP_KERNEL | __GFP_ZERO,
Jan Beulicha4a61982006-03-24 03:15:54 -08001245 cpu_to_node(cpu));
1246 if (!base)
1247 return -ENOMEM;
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001248
1249 /* Make sure that tvec_base is 2 byte aligned */
1250 if (tbase_get_deferrable(base)) {
1251 WARN_ON(1);
1252 kfree(base);
1253 return -ENOMEM;
1254 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001255 per_cpu(tvec_bases, cpu) = base;
Jan Beulicha4a61982006-03-24 03:15:54 -08001256 } else {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001257 /*
1258 * This is for the boot CPU - we use compile-time
1259 * static initialisation because per-cpu memory isn't
1260 * ready yet and because the memory allocators are not
1261 * initialised either.
1262 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001263 boot_done = 1;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001264 base = &boot_tvec_bases;
Jan Beulicha4a61982006-03-24 03:15:54 -08001265 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001266 tvec_base_done[cpu] = 1;
1267 } else {
1268 base = per_cpu(tvec_bases, cpu);
Jan Beulicha4a61982006-03-24 03:15:54 -08001269 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001270
Oleg Nesterov3691c512006-03-31 02:30:30 -08001271 spin_lock_init(&base->lock);
Ingo Molnard730e882006-07-03 00:25:10 -07001272 lockdep_set_class(&base->lock, base_lock_keys + cpu);
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 for (j = 0; j < TVN_SIZE; j++) {
1275 INIT_LIST_HEAD(base->tv5.vec + j);
1276 INIT_LIST_HEAD(base->tv4.vec + j);
1277 INIT_LIST_HEAD(base->tv3.vec + j);
1278 INIT_LIST_HEAD(base->tv2.vec + j);
1279 }
1280 for (j = 0; j < TVR_SIZE; j++)
1281 INIT_LIST_HEAD(base->tv1.vec + j);
1282
1283 base->timer_jiffies = jiffies;
Jan Beulicha4a61982006-03-24 03:15:54 -08001284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
1287#ifdef CONFIG_HOTPLUG_CPU
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001288static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 struct timer_list *timer;
1291
1292 while (!list_empty(head)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001293 timer = list_first_entry(head, struct timer_list, entry);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001294 detach_timer(timer, 0);
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001295 timer_set_base(timer, new_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 internal_add_timer(new_base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Randy Dunlap48ccf3d2008-01-21 17:18:25 -08001300static void __cpuinit migrate_timers(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001302 struct tvec_base *old_base;
1303 struct tvec_base *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 int i;
1305
1306 BUG_ON(cpu_online(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001307 old_base = per_cpu(tvec_bases, cpu);
1308 new_base = get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 local_irq_disable();
Heiko Carstense81ce1f2007-03-05 00:30:51 -08001311 double_spin_lock(&new_base->lock, &old_base->lock,
1312 smp_processor_id() < cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Oleg Nesterov3691c512006-03-31 02:30:30 -08001314 BUG_ON(old_base->running_timer);
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 for (i = 0; i < TVR_SIZE; i++)
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001317 migrate_timer_list(new_base, old_base->tv1.vec + i);
1318 for (i = 0; i < TVN_SIZE; i++) {
1319 migrate_timer_list(new_base, old_base->tv2.vec + i);
1320 migrate_timer_list(new_base, old_base->tv3.vec + i);
1321 migrate_timer_list(new_base, old_base->tv4.vec + i);
1322 migrate_timer_list(new_base, old_base->tv5.vec + i);
1323 }
1324
Heiko Carstense81ce1f2007-03-05 00:30:51 -08001325 double_spin_unlock(&new_base->lock, &old_base->lock,
1326 smp_processor_id() < cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 local_irq_enable();
1328 put_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330#endif /* CONFIG_HOTPLUG_CPU */
1331
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001332static int __cpuinit timer_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 unsigned long action, void *hcpu)
1334{
1335 long cpu = (long)hcpu;
1336 switch(action) {
1337 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001338 case CPU_UP_PREPARE_FROZEN:
Jan Beulicha4a61982006-03-24 03:15:54 -08001339 if (init_timers_cpu(cpu) < 0)
1340 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 break;
1342#ifdef CONFIG_HOTPLUG_CPU
1343 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001344 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 migrate_timers(cpu);
1346 break;
1347#endif
1348 default:
1349 break;
1350 }
1351 return NOTIFY_OK;
1352}
1353
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001354static struct notifier_block __cpuinitdata timers_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 .notifier_call = timer_cpu_notify,
1356};
1357
1358
1359void __init init_timers(void)
1360{
Akinobu Mita07dccf32006-09-29 02:00:22 -07001361 int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 (void *)(long)smp_processor_id());
Akinobu Mita07dccf32006-09-29 02:00:22 -07001363
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001364 init_timer_stats();
1365
Akinobu Mita07dccf32006-09-29 02:00:22 -07001366 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 register_cpu_notifier(&timers_nb);
1368 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1369}
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371/**
1372 * msleep - sleep safely even with waitqueue interruptions
1373 * @msecs: Time in milliseconds to sleep for
1374 */
1375void msleep(unsigned int msecs)
1376{
1377 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1378
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001379 while (timeout)
1380 timeout = schedule_timeout_uninterruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
1383EXPORT_SYMBOL(msleep);
1384
1385/**
Domen Puncer96ec3ef2005-06-25 14:58:43 -07001386 * msleep_interruptible - sleep waiting for signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * @msecs: Time in milliseconds to sleep for
1388 */
1389unsigned long msleep_interruptible(unsigned int msecs)
1390{
1391 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1392
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001393 while (timeout && !signal_pending(current))
1394 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return jiffies_to_msecs(timeout);
1396}
1397
1398EXPORT_SYMBOL(msleep_interruptible);