blob: ef5ec26dd4968cf5f8bfe705128ff925ec4d9d6d [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>
Paul Gortmaker9984de12011-05-23 14:51:41 -040023#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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>
Peter Zijlstrae360adb2010-10-14 14:01:34 +080040#include <linux/irq_work.h>
Arun R Bharadwajeea08f32009-04-16 12:16:41 +053041#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <asm/uaccess.h>
45#include <asm/unistd.h>
46#include <asm/div64.h>
47#include <asm/timex.h>
48#include <asm/io.h>
49
Xiao Guangrong2b022e32009-08-10 10:48:59 +080050#define CREATE_TRACE_POINTS
51#include <trace/events/timer.h>
52
Thomas Gleixnerecea8d12005-10-30 15:03:00 -080053u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
54
55EXPORT_SYMBOL(jiffies_64);
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * per-CPU timer vector definitions:
59 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
61#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
62#define TVN_SIZE (1 << TVN_BITS)
63#define TVR_SIZE (1 << TVR_BITS)
64#define TVN_MASK (TVN_SIZE - 1)
65#define TVR_MASK (TVR_SIZE - 1)
Devin Kim62720dc2012-10-29 16:46:33 -070066#define MAX_TVAL ((unsigned long)((1ULL << (TVR_BITS + 4*TVN_BITS)) - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Pavel Macheka6fa8e52008-01-30 13:30:00 +010068struct tvec {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct list_head vec[TVN_SIZE];
Pavel Macheka6fa8e52008-01-30 13:30:00 +010070};
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Pavel Macheka6fa8e52008-01-30 13:30:00 +010072struct tvec_root {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct list_head vec[TVR_SIZE];
Pavel Macheka6fa8e52008-01-30 13:30:00 +010074};
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Pavel Macheka6fa8e52008-01-30 13:30:00 +010076struct tvec_base {
Oleg Nesterov3691c512006-03-31 02:30:30 -080077 spinlock_t lock;
78 struct timer_list *running_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 unsigned long timer_jiffies;
Martin Schwidefsky97fd9ed2009-07-21 20:25:05 +020080 unsigned long next_timer;
Pavel Macheka6fa8e52008-01-30 13:30:00 +010081 struct tvec_root tv1;
82 struct tvec tv2;
83 struct tvec tv3;
84 struct tvec tv4;
85 struct tvec tv5;
Venki Pallipadi6e453a62007-05-08 00:27:44 -070086} ____cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Pavel Macheka6fa8e52008-01-30 13:30:00 +010088struct tvec_base boot_tvec_bases;
Oleg Nesterov3691c512006-03-31 02:30:30 -080089EXPORT_SYMBOL(boot_tvec_bases);
Pavel Macheka6fa8e52008-01-30 13:30:00 +010090static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Venki Pallipadi6e453a62007-05-08 00:27:44 -070092/* Functions below help us manage 'deferrable' flag */
Pavel Macheka6fa8e52008-01-30 13:30:00 +010093static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -070094{
akpm@linux-foundation.orge9910842007-05-10 03:16:01 -070095 return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);
Venki Pallipadi6e453a62007-05-08 00:27:44 -070096}
97
Pavel Macheka6fa8e52008-01-30 13:30:00 +010098static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -070099{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100100 return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700101}
102
103static inline void timer_set_deferrable(struct timer_list *timer)
104{
Phil Carmodydd6414b2010-10-20 15:57:33 -0700105 timer->base = TBASE_MAKE_DEFERRED(timer->base);
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
Alan Stern9c133c42008-11-06 08:42:48 +0100115static unsigned long round_jiffies_common(unsigned long j, int cpu,
116 bool force_up)
117{
118 int rem;
119 unsigned long original = j;
120
121 /*
122 * We don't want all cpus firing their timers at once hitting the
123 * same lock or cachelines, so we skew each extra cpu with an extra
124 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
125 * already did this.
126 * The skew is done by adding 3*cpunr, then round, then subtract this
127 * extra offset again.
128 */
129 j += cpu * 3;
130
131 rem = j % HZ;
132
133 /*
134 * If the target jiffie is just after a whole second (which can happen
135 * due to delays of the timer irq, long irq off times etc etc) then
136 * we should round down to the whole second, not up. Use 1/4th second
137 * as cutoff for this rounding as an extreme upper bound for this.
138 * But never round down if @force_up is set.
139 */
140 if (rem < HZ/4 && !force_up) /* round down */
141 j = j - rem;
142 else /* round up */
143 j = j - rem + HZ;
144
145 /* now that we have rounded, subtract the extra skew again */
146 j -= cpu * 3;
147
148 if (j <= jiffies) /* rounding ate our timeout entirely; */
149 return original;
150 return j;
151}
152
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800153/**
154 * __round_jiffies - function to round jiffies to a full second
155 * @j: the time in (absolute) jiffies that should be rounded
156 * @cpu: the processor number on which the timeout will happen
157 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800158 * __round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800159 * up or down to (approximately) full seconds. This is useful for timers
160 * for which the exact time they fire does not matter too much, as long as
161 * they fire approximately every X seconds.
162 *
163 * By rounding these timers to whole seconds, all such timers will fire
164 * at the same time, rather than at various times spread out. The goal
165 * of this is to have the CPU wake up less, which saves power.
166 *
167 * The exact rounding is skewed for each processor to avoid all
168 * processors firing at the exact same time, which could lead
169 * to lock contention or spurious cache line bouncing.
170 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800171 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800172 */
173unsigned long __round_jiffies(unsigned long j, int cpu)
174{
Alan Stern9c133c42008-11-06 08:42:48 +0100175 return round_jiffies_common(j, cpu, false);
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800176}
177EXPORT_SYMBOL_GPL(__round_jiffies);
178
179/**
180 * __round_jiffies_relative - function to round jiffies to a full second
181 * @j: the time in (relative) jiffies that should be rounded
182 * @cpu: the processor number on which the timeout will happen
183 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800184 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800185 * up or down to (approximately) full seconds. This is useful for timers
186 * for which the exact time they fire does not matter too much, as long as
187 * they fire approximately every X seconds.
188 *
189 * By rounding these timers to whole seconds, all such timers will fire
190 * at the same time, rather than at various times spread out. The goal
191 * of this is to have the CPU wake up less, which saves power.
192 *
193 * The exact rounding is skewed for each processor to avoid all
194 * processors firing at the exact same time, which could lead
195 * to lock contention or spurious cache line bouncing.
196 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800197 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800198 */
199unsigned long __round_jiffies_relative(unsigned long j, int cpu)
200{
Alan Stern9c133c42008-11-06 08:42:48 +0100201 unsigned long j0 = jiffies;
202
203 /* Use j0 because jiffies might change while we run */
204 return round_jiffies_common(j + j0, cpu, false) - j0;
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800205}
206EXPORT_SYMBOL_GPL(__round_jiffies_relative);
207
208/**
209 * round_jiffies - function to round jiffies to a full second
210 * @j: the time in (absolute) jiffies that should be rounded
211 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800212 * round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800213 * up or down to (approximately) full seconds. This is useful for timers
214 * for which the exact time they fire does not matter too much, as long as
215 * they fire approximately every X seconds.
216 *
217 * By rounding these timers to whole seconds, all such timers will fire
218 * at the same time, rather than at various times spread out. The goal
219 * of this is to have the CPU wake up less, which saves power.
220 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800221 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800222 */
223unsigned long round_jiffies(unsigned long j)
224{
Alan Stern9c133c42008-11-06 08:42:48 +0100225 return round_jiffies_common(j, raw_smp_processor_id(), false);
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800226}
227EXPORT_SYMBOL_GPL(round_jiffies);
228
229/**
230 * round_jiffies_relative - function to round jiffies to a full second
231 * @j: the time in (relative) jiffies that should be rounded
232 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800233 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800234 * up or down to (approximately) full seconds. This is useful for timers
235 * for which the exact time they fire does not matter too much, as long as
236 * they fire approximately every X seconds.
237 *
238 * By rounding these timers to whole seconds, all such timers will fire
239 * at the same time, rather than at various times spread out. The goal
240 * of this is to have the CPU wake up less, which saves power.
241 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800242 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800243 */
244unsigned long round_jiffies_relative(unsigned long j)
245{
246 return __round_jiffies_relative(j, raw_smp_processor_id());
247}
248EXPORT_SYMBOL_GPL(round_jiffies_relative);
249
Alan Stern9c133c42008-11-06 08:42:48 +0100250/**
251 * __round_jiffies_up - function to round jiffies up to a full second
252 * @j: the time in (absolute) jiffies that should be rounded
253 * @cpu: the processor number on which the timeout will happen
254 *
255 * This is the same as __round_jiffies() except that it will never
256 * round down. This is useful for timeouts for which the exact time
257 * of firing does not matter too much, as long as they don't fire too
258 * early.
259 */
260unsigned long __round_jiffies_up(unsigned long j, int cpu)
261{
262 return round_jiffies_common(j, cpu, true);
263}
264EXPORT_SYMBOL_GPL(__round_jiffies_up);
265
266/**
267 * __round_jiffies_up_relative - function to round jiffies up to a full second
268 * @j: the time in (relative) jiffies that should be rounded
269 * @cpu: the processor number on which the timeout will happen
270 *
271 * This is the same as __round_jiffies_relative() except that it will never
272 * round down. This is useful for timeouts for which the exact time
273 * of firing does not matter too much, as long as they don't fire too
274 * early.
275 */
276unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
277{
278 unsigned long j0 = jiffies;
279
280 /* Use j0 because jiffies might change while we run */
281 return round_jiffies_common(j + j0, cpu, true) - j0;
282}
283EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
284
285/**
286 * round_jiffies_up - function to round jiffies up to a full second
287 * @j: the time in (absolute) jiffies that should be rounded
288 *
289 * This is the same as round_jiffies() except that it will never
290 * round down. This is useful for timeouts for which the exact time
291 * of firing does not matter too much, as long as they don't fire too
292 * early.
293 */
294unsigned long round_jiffies_up(unsigned long j)
295{
296 return round_jiffies_common(j, raw_smp_processor_id(), true);
297}
298EXPORT_SYMBOL_GPL(round_jiffies_up);
299
300/**
301 * round_jiffies_up_relative - function to round jiffies up to a full second
302 * @j: the time in (relative) jiffies that should be rounded
303 *
304 * This is the same as round_jiffies_relative() except that it will never
305 * round down. This is useful for timeouts for which the exact time
306 * of firing does not matter too much, as long as they don't fire too
307 * early.
308 */
309unsigned long round_jiffies_up_relative(unsigned long j)
310{
311 return __round_jiffies_up_relative(j, raw_smp_processor_id());
312}
313EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
314
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800315/**
316 * set_timer_slack - set the allowed slack for a timer
Randy Dunlap0caa6212010-08-09 16:32:50 -0700317 * @timer: the timer to be modified
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800318 * @slack_hz: the amount of time (in jiffies) allowed for rounding
319 *
320 * Set the amount of time, in jiffies, that a certain timer has
321 * in terms of slack. By setting this value, the timer subsystem
322 * will schedule the actual timer somewhere between
323 * the time mod_timer() asks for, and that time plus the slack.
324 *
325 * By setting the slack to -1, a percentage of the delay is used
326 * instead.
327 */
328void set_timer_slack(struct timer_list *timer, int slack_hz)
329{
330 timer->slack = slack_hz;
331}
332EXPORT_SYMBOL_GPL(set_timer_slack);
333
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100334static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 unsigned long expires = timer->expires;
337 unsigned long idx = expires - base->timer_jiffies;
338 struct list_head *vec;
339
340 if (idx < TVR_SIZE) {
341 int i = expires & TVR_MASK;
342 vec = base->tv1.vec + i;
343 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
344 int i = (expires >> TVR_BITS) & TVN_MASK;
345 vec = base->tv2.vec + i;
346 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
347 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
348 vec = base->tv3.vec + i;
349 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
350 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
351 vec = base->tv4.vec + i;
352 } else if ((signed long) idx < 0) {
353 /*
354 * Can happen if you add a timer with expires == jiffies,
355 * or you set a timer to go off in the past
356 */
357 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
358 } else {
359 int i;
Devin Kim62720dc2012-10-29 16:46:33 -0700360 /* If the timeout is larger than MAX_TVAL (on 64-bit
361 * architectures or with CONFIG_BASE_SMALL=1) then we
362 * use the maximum timeout.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
Devin Kim62720dc2012-10-29 16:46:33 -0700364 if (idx > MAX_TVAL) {
365 idx = MAX_TVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 expires = idx + base->timer_jiffies;
367 }
368 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
369 vec = base->tv5.vec + i;
370 }
371 /*
372 * Timers are FIFO:
373 */
374 list_add_tail(&timer->entry, vec);
375}
376
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800377#ifdef CONFIG_TIMER_STATS
378void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
379{
380 if (timer->start_site)
381 return;
382
383 timer->start_site = addr;
384 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
385 timer->start_pid = current->pid;
386}
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700387
388static void timer_stats_account_timer(struct timer_list *timer)
389{
390 unsigned int flag = 0;
391
Heiko Carstens507e1232009-06-23 17:38:15 +0200392 if (likely(!timer->start_site))
393 return;
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700394 if (unlikely(tbase_get_deferrable(timer->base)))
395 flag |= TIMER_STATS_FLAG_DEFERRABLE;
396
397 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
398 timer->function, timer->start_comm, flag);
399}
400
401#else
402static void timer_stats_account_timer(struct timer_list *timer) {}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800403#endif
404
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700405#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
406
407static struct debug_obj_descr timer_debug_descr;
408
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100409static void *timer_debug_hint(void *addr)
410{
411 return ((struct timer_list *) addr)->function;
412}
413
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700414/*
415 * fixup_init is called when:
416 * - an active object is initialized
417 */
418static int timer_fixup_init(void *addr, enum debug_obj_state state)
419{
420 struct timer_list *timer = addr;
421
422 switch (state) {
423 case ODEBUG_STATE_ACTIVE:
424 del_timer_sync(timer);
425 debug_object_init(timer, &timer_debug_descr);
426 return 1;
427 default:
428 return 0;
429 }
430}
431
Stephen Boydfb16b8c2011-11-07 19:48:26 -0800432/* Stub timer callback for improperly used timers. */
433static void stub_timer(unsigned long data)
434{
435 WARN_ON(1);
436}
437
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700438/*
439 * fixup_activate is called when:
440 * - an active object is activated
441 * - an unknown object is activated (might be a statically initialized object)
442 */
443static int timer_fixup_activate(void *addr, enum debug_obj_state state)
444{
445 struct timer_list *timer = addr;
446
447 switch (state) {
448
449 case ODEBUG_STATE_NOTAVAILABLE:
450 /*
451 * This is not really a fixup. The timer was
452 * statically initialized. We just make sure that it
453 * is tracked in the object tracker.
454 */
455 if (timer->entry.next == NULL &&
456 timer->entry.prev == TIMER_ENTRY_STATIC) {
457 debug_object_init(timer, &timer_debug_descr);
458 debug_object_activate(timer, &timer_debug_descr);
459 return 0;
460 } else {
Stephen Boydfb16b8c2011-11-07 19:48:26 -0800461 setup_timer(timer, stub_timer, 0);
462 return 1;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700463 }
464 return 0;
465
466 case ODEBUG_STATE_ACTIVE:
467 WARN_ON(1);
468
469 default:
470 return 0;
471 }
472}
473
474/*
475 * fixup_free is called when:
476 * - an active object is freed
477 */
478static int timer_fixup_free(void *addr, enum debug_obj_state state)
479{
480 struct timer_list *timer = addr;
481
482 switch (state) {
483 case ODEBUG_STATE_ACTIVE:
484 del_timer_sync(timer);
485 debug_object_free(timer, &timer_debug_descr);
486 return 1;
487 default:
488 return 0;
489 }
490}
491
Christine Chandc4218b2011-11-07 19:48:28 -0800492/*
493 * fixup_assert_init is called when:
494 * - an untracked/uninit-ed object is found
495 */
496static int timer_fixup_assert_init(void *addr, enum debug_obj_state state)
497{
498 struct timer_list *timer = addr;
499
500 switch (state) {
501 case ODEBUG_STATE_NOTAVAILABLE:
502 if (timer->entry.prev == TIMER_ENTRY_STATIC) {
503 /*
504 * This is not really a fixup. The timer was
505 * statically initialized. We just make sure that it
506 * is tracked in the object tracker.
507 */
508 debug_object_init(timer, &timer_debug_descr);
509 return 0;
510 } else {
511 setup_timer(timer, stub_timer, 0);
512 return 1;
513 }
514 default:
515 return 0;
516 }
517}
518
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700519static struct debug_obj_descr timer_debug_descr = {
Christine Chandc4218b2011-11-07 19:48:28 -0800520 .name = "timer_list",
521 .debug_hint = timer_debug_hint,
522 .fixup_init = timer_fixup_init,
523 .fixup_activate = timer_fixup_activate,
524 .fixup_free = timer_fixup_free,
525 .fixup_assert_init = timer_fixup_assert_init,
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700526};
527
528static inline void debug_timer_init(struct timer_list *timer)
529{
530 debug_object_init(timer, &timer_debug_descr);
531}
532
533static inline void debug_timer_activate(struct timer_list *timer)
534{
535 debug_object_activate(timer, &timer_debug_descr);
536}
537
538static inline void debug_timer_deactivate(struct timer_list *timer)
539{
540 debug_object_deactivate(timer, &timer_debug_descr);
541}
542
543static inline void debug_timer_free(struct timer_list *timer)
544{
545 debug_object_free(timer, &timer_debug_descr);
546}
547
Christine Chandc4218b2011-11-07 19:48:28 -0800548static inline void debug_timer_assert_init(struct timer_list *timer)
549{
550 debug_object_assert_init(timer, &timer_debug_descr);
551}
552
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100553static void __init_timer(struct timer_list *timer,
554 const char *name,
555 struct lock_class_key *key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700556
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100557void init_timer_on_stack_key(struct timer_list *timer,
558 const char *name,
559 struct lock_class_key *key)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700560{
561 debug_object_init_on_stack(timer, &timer_debug_descr);
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100562 __init_timer(timer, name, key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700563}
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100564EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700565
566void destroy_timer_on_stack(struct timer_list *timer)
567{
568 debug_object_free(timer, &timer_debug_descr);
569}
570EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
571
572#else
573static inline void debug_timer_init(struct timer_list *timer) { }
574static inline void debug_timer_activate(struct timer_list *timer) { }
575static inline void debug_timer_deactivate(struct timer_list *timer) { }
Christine Chandc4218b2011-11-07 19:48:28 -0800576static inline void debug_timer_assert_init(struct timer_list *timer) { }
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700577#endif
578
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800579static inline void debug_init(struct timer_list *timer)
580{
581 debug_timer_init(timer);
582 trace_timer_init(timer);
583}
584
585static inline void
586debug_activate(struct timer_list *timer, unsigned long expires)
587{
588 debug_timer_activate(timer);
Badhri Jagan Sridharana2cd6ea2012-07-18 15:46:01 -0700589 trace_timer_start(timer, expires,
590 tbase_get_deferrable(timer->base) > 0 ? 'y' : 'n');
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800591}
592
593static inline void debug_deactivate(struct timer_list *timer)
594{
595 debug_timer_deactivate(timer);
596 trace_timer_cancel(timer);
597}
598
Christine Chandc4218b2011-11-07 19:48:28 -0800599static inline void debug_assert_init(struct timer_list *timer)
600{
601 debug_timer_assert_init(timer);
602}
603
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100604static void __init_timer(struct timer_list *timer,
605 const char *name,
606 struct lock_class_key *key)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700607{
608 timer->entry.next = NULL;
609 timer->base = __raw_get_cpu_var(tvec_bases);
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800610 timer->slack = -1;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700611#ifdef CONFIG_TIMER_STATS
612 timer->start_site = NULL;
613 timer->start_pid = -1;
614 memset(timer->start_comm, 0, TASK_COMM_LEN);
615#endif
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100616 lockdep_init_map(&timer->lockdep_map, name, key, 0);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700617}
618
Jesse Barnes8cadd2832010-05-10 14:26:20 -0700619void setup_deferrable_timer_on_stack_key(struct timer_list *timer,
620 const char *name,
621 struct lock_class_key *key,
622 void (*function)(unsigned long),
623 unsigned long data)
624{
625 timer->function = function;
626 timer->data = data;
627 init_timer_on_stack_key(timer, name, key);
628 timer_set_deferrable(timer);
629}
630EXPORT_SYMBOL_GPL(setup_deferrable_timer_on_stack_key);
631
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700632/**
Randy Dunlap633fe792009-04-01 17:47:23 -0700633 * init_timer_key - initialize a timer
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700634 * @timer: the timer to be initialized
Randy Dunlap633fe792009-04-01 17:47:23 -0700635 * @name: name of the timer
636 * @key: lockdep class key of the fake lock used for tracking timer
637 * sync lock dependencies
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700638 *
Randy Dunlap633fe792009-04-01 17:47:23 -0700639 * init_timer_key() must be done to a timer prior calling *any* of the
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700640 * other timer functions.
641 */
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100642void init_timer_key(struct timer_list *timer,
643 const char *name,
644 struct lock_class_key *key)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700645{
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800646 debug_init(timer);
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100647 __init_timer(timer, name, key);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700648}
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100649EXPORT_SYMBOL(init_timer_key);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700650
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100651void init_timer_deferrable_key(struct timer_list *timer,
652 const char *name,
653 struct lock_class_key *key)
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700654{
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100655 init_timer_key(timer, name, key);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700656 timer_set_deferrable(timer);
657}
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100658EXPORT_SYMBOL(init_timer_deferrable_key);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700659
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700660static inline void detach_timer(struct timer_list *timer,
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800661 int clear_pending)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700662{
663 struct list_head *entry = &timer->entry;
664
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800665 debug_deactivate(timer);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700666
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700667 __list_del(entry->prev, entry->next);
668 if (clear_pending)
669 entry->next = NULL;
670 entry->prev = LIST_POISON2;
671}
672
673/*
Oleg Nesterov3691c512006-03-31 02:30:30 -0800674 * We are using hashed locking: holding per_cpu(tvec_bases).lock
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700675 * means that all timers which are tied to this base via timer->base are
676 * locked, and the base itself is locked too.
677 *
678 * So __run_timers/migrate_timers can safely modify all timers which could
679 * be found on ->tvX lists.
680 *
681 * When the timer's base is locked, and the timer removed from list, it is
682 * possible to set timer->base = NULL and drop the lock: the timer remains
683 * locked.
684 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100685static struct tvec_base *lock_timer_base(struct timer_list *timer,
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700686 unsigned long *flags)
Josh Triplett89e7e3742006-09-29 01:59:36 -0700687 __acquires(timer->base->lock)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700688{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100689 struct tvec_base *base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700690
691 for (;;) {
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100692 struct tvec_base *prelock_base = timer->base;
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700693 base = tbase_get_base(prelock_base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700694 if (likely(base != NULL)) {
695 spin_lock_irqsave(&base->lock, *flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700696 if (likely(prelock_base == timer->base))
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700697 return base;
698 /* The timer has migrated to another CPU */
699 spin_unlock_irqrestore(&base->lock, *flags);
700 }
701 cpu_relax();
702 }
703}
704
Ingo Molnar74019222009-02-18 12:23:29 +0100705static inline int
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530706__mod_timer(struct timer_list *timer, unsigned long expires,
707 bool pending_only, int pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100709 struct tvec_base *base, *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 unsigned long flags;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530711 int ret = 0 , cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800713 timer_stats_timer_set_start_info(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 BUG_ON(!timer->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700716 base = lock_timer_base(timer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700718 if (timer_pending(timer)) {
719 detach_timer(timer, 0);
Martin Schwidefsky97fd9ed2009-07-21 20:25:05 +0200720 if (timer->expires == base->next_timer &&
721 !tbase_get_deferrable(timer->base))
722 base->next_timer = base->timer_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 ret = 1;
Ingo Molnar74019222009-02-18 12:23:29 +0100724 } else {
725 if (pending_only)
726 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700728
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800729 debug_activate(timer, expires);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700730
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530731 cpu = smp_processor_id();
732
733#if defined(CONFIG_NO_HZ) && defined(CONFIG_SMP)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700734 if (!pinned && get_sysctl_timer_migration() && idle_cpu(cpu))
735 cpu = get_nohz_timer_target();
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530736#endif
737 new_base = per_cpu(tvec_bases, cpu);
738
Oleg Nesterov3691c512006-03-31 02:30:30 -0800739 if (base != new_base) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700740 /*
741 * We are trying to schedule the timer on the local CPU.
742 * However we can't change timer's base while it is running,
743 * otherwise del_timer_sync() can't detect that the timer's
744 * handler yet has not finished. This also guarantees that
745 * the timer is serialized wrt itself.
746 */
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800747 if (likely(base->running_timer != timer)) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700748 /* See the comment in lock_timer_base() */
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700749 timer_set_base(timer, NULL);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700750 spin_unlock(&base->lock);
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800751 base = new_base;
752 spin_lock(&base->lock);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700753 timer_set_base(timer, base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700754 }
755 }
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 timer->expires = expires;
Martin Schwidefsky97fd9ed2009-07-21 20:25:05 +0200758 if (time_before(timer->expires, base->next_timer) &&
759 !tbase_get_deferrable(timer->base))
760 base->next_timer = timer->expires;
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800761 internal_add_timer(base, timer);
Ingo Molnar74019222009-02-18 12:23:29 +0100762
763out_unlock:
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800764 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 return ret;
767}
768
Ingo Molnar74019222009-02-18 12:23:29 +0100769/**
770 * mod_timer_pending - modify a pending timer's timeout
771 * @timer: the pending timer to be modified
772 * @expires: new timeout in jiffies
773 *
774 * mod_timer_pending() is the same for pending timers as mod_timer(),
775 * but will not re-activate and modify already deleted timers.
776 *
777 * It is useful for unserialized use of timers.
778 */
779int mod_timer_pending(struct timer_list *timer, unsigned long expires)
780{
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530781 return __mod_timer(timer, expires, true, TIMER_NOT_PINNED);
Ingo Molnar74019222009-02-18 12:23:29 +0100782}
783EXPORT_SYMBOL(mod_timer_pending);
784
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800785/*
786 * Decide where to put the timer while taking the slack into account
787 *
788 * Algorithm:
789 * 1) calculate the maximum (absolute) time
790 * 2) calculate the highest bit where the expires and new max are different
791 * 3) use this bit to make a mask
792 * 4) use the bitmask to round down the maximum time, so that all last
793 * bits are zeros
794 */
795static inline
796unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
797{
798 unsigned long expires_limit, mask;
799 int bit;
800
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200801 if (timer->slack >= 0) {
Jeff Chuaf00e0472010-05-24 07:16:24 +0800802 expires_limit = expires + timer->slack;
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200803 } else {
Sebastian Andrzej Siewior1c3cc1162011-05-21 12:58:28 +0200804 long delta = expires - jiffies;
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800805
Sebastian Andrzej Siewior1c3cc1162011-05-21 12:58:28 +0200806 if (delta < 256)
807 return expires;
808
809 expires_limit = expires + delta / 256;
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200810 }
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800811 mask = expires ^ expires_limit;
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800812 if (mask == 0)
813 return expires;
814
815 bit = find_last_bit(&mask, BITS_PER_LONG);
816
817 mask = (1 << bit) - 1;
818
819 expires_limit = expires_limit & ~(mask);
820
821 return expires_limit;
822}
823
Ingo Molnar74019222009-02-18 12:23:29 +0100824/**
825 * mod_timer - modify a timer's timeout
826 * @timer: the timer to be modified
827 * @expires: new timeout in jiffies
828 *
829 * mod_timer() is a more efficient way to update the expire field of an
830 * active timer (if the timer is inactive it will be activated)
831 *
832 * mod_timer(timer, expires) is equivalent to:
833 *
834 * del_timer(timer); timer->expires = expires; add_timer(timer);
835 *
836 * Note that if there are multiple unserialized concurrent users of the
837 * same timer, then mod_timer() is the only safe way to modify the timeout,
838 * since add_timer() cannot modify an already running timer.
839 *
840 * The function returns whether it has modified a pending timer or not.
841 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
842 * active timer returns 1.)
843 */
844int mod_timer(struct timer_list *timer, unsigned long expires)
845{
Sebastian Andrzej Siewior1c3cc1162011-05-21 12:58:28 +0200846 expires = apply_slack(timer, expires);
847
Ingo Molnar74019222009-02-18 12:23:29 +0100848 /*
849 * This is a common optimization triggered by the
850 * networking code - if the timer is re-modified
851 * to be the same thing then just return:
852 */
Pavel Roskin48411582009-07-18 16:46:02 -0400853 if (timer_pending(timer) && timer->expires == expires)
Ingo Molnar74019222009-02-18 12:23:29 +0100854 return 1;
855
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530856 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
Ingo Molnar74019222009-02-18 12:23:29 +0100857}
858EXPORT_SYMBOL(mod_timer);
859
860/**
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530861 * mod_timer_pinned - modify a timer's timeout
862 * @timer: the timer to be modified
863 * @expires: new timeout in jiffies
864 *
865 * mod_timer_pinned() is a way to update the expire field of an
866 * active timer (if the timer is inactive it will be activated)
867 * and not allow the timer to be migrated to a different CPU.
868 *
869 * mod_timer_pinned(timer, expires) is equivalent to:
870 *
871 * del_timer(timer); timer->expires = expires; add_timer(timer);
872 */
873int mod_timer_pinned(struct timer_list *timer, unsigned long expires)
874{
875 if (timer->expires == expires && timer_pending(timer))
876 return 1;
877
878 return __mod_timer(timer, expires, false, TIMER_PINNED);
879}
880EXPORT_SYMBOL(mod_timer_pinned);
881
882/**
Ingo Molnar74019222009-02-18 12:23:29 +0100883 * add_timer - start a timer
884 * @timer: the timer to be added
885 *
886 * The kernel will do a ->function(->data) callback from the
887 * timer interrupt at the ->expires point in the future. The
888 * current time is 'jiffies'.
889 *
890 * The timer's ->expires, ->function (and if the handler uses it, ->data)
891 * fields must be set prior calling this function.
892 *
893 * Timers with an ->expires field in the past will be executed in the next
894 * timer tick.
895 */
896void add_timer(struct timer_list *timer)
897{
898 BUG_ON(timer_pending(timer));
899 mod_timer(timer, timer->expires);
900}
901EXPORT_SYMBOL(add_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700903/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 * add_timer_on - start a timer on a particular CPU
905 * @timer: the timer to be added
906 * @cpu: the CPU to start it on
907 *
908 * This is not very scalable on SMP. Double adds are not possible.
909 */
910void add_timer_on(struct timer_list *timer, int cpu)
911{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100912 struct tvec_base *base = per_cpu(tvec_bases, cpu);
Thomas Gleixner68194572007-07-19 01:49:16 -0700913 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700914
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800915 timer_stats_timer_set_start_info(timer);
Thomas Gleixner68194572007-07-19 01:49:16 -0700916 BUG_ON(timer_pending(timer) || !timer->function);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800917 spin_lock_irqsave(&base->lock, flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700918 timer_set_base(timer, base);
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800919 debug_activate(timer, timer->expires);
Martin Schwidefsky97fd9ed2009-07-21 20:25:05 +0200920 if (time_before(timer->expires, base->next_timer) &&
921 !tbase_get_deferrable(timer->base))
922 base->next_timer = timer->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 internal_add_timer(base, timer);
Thomas Gleixner06d83082008-03-22 09:20:24 +0100924 /*
925 * Check whether the other CPU is idle and needs to be
926 * triggered to reevaluate the timer wheel when nohz is
927 * active. We are protected against the other CPU fiddling
928 * with the timer by holding the timer base lock. This also
929 * makes sure that a CPU on the way to idle can not evaluate
930 * the timer wheel.
931 */
932 wake_up_idle_cpu(cpu);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800933 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
Andi Kleena9862e02009-05-19 22:49:07 +0200935EXPORT_SYMBOL_GPL(add_timer_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700937/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 * del_timer - deactive a timer.
939 * @timer: the timer to be deactivated
940 *
941 * del_timer() deactivates a timer - this works on both active and inactive
942 * timers.
943 *
944 * The function returns whether it has deactivated a pending timer or not.
945 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
946 * active timer returns 1.)
947 */
948int del_timer(struct timer_list *timer)
949{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100950 struct tvec_base *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700952 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Christine Chandc4218b2011-11-07 19:48:28 -0800954 debug_assert_init(timer);
955
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800956 timer_stats_timer_clear_start_info(timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700957 if (timer_pending(timer)) {
958 base = lock_timer_base(timer, &flags);
959 if (timer_pending(timer)) {
960 detach_timer(timer, 1);
Martin Schwidefsky97fd9ed2009-07-21 20:25:05 +0200961 if (timer->expires == base->next_timer &&
962 !tbase_get_deferrable(timer->base))
963 base->next_timer = base->timer_jiffies;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700964 ret = 1;
965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700969 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971EXPORT_SYMBOL(del_timer);
972
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700973/**
974 * try_to_del_timer_sync - Try to deactivate a timer
975 * @timer: timer do del
976 *
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700977 * This function tries to deactivate a timer. Upon successful (ret >= 0)
978 * exit the timer is not queued and the handler is not running on any CPU.
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700979 */
980int try_to_del_timer_sync(struct timer_list *timer)
981{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100982 struct tvec_base *base;
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700983 unsigned long flags;
984 int ret = -1;
985
Christine Chandc4218b2011-11-07 19:48:28 -0800986 debug_assert_init(timer);
987
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700988 base = lock_timer_base(timer, &flags);
989
990 if (base->running_timer == timer)
991 goto out;
992
Andrew Morton829b6c12010-03-11 14:04:30 -0800993 timer_stats_timer_clear_start_info(timer);
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700994 ret = 0;
995 if (timer_pending(timer)) {
996 detach_timer(timer, 1);
Martin Schwidefsky97fd9ed2009-07-21 20:25:05 +0200997 if (timer->expires == base->next_timer &&
998 !tbase_get_deferrable(timer->base))
999 base->next_timer = base->timer_jiffies;
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001000 ret = 1;
1001 }
1002out:
1003 spin_unlock_irqrestore(&base->lock, flags);
1004
1005 return ret;
1006}
David Howellse19dff12007-04-26 15:46:56 -07001007EXPORT_SYMBOL(try_to_del_timer_sync);
1008
Yong Zhang6f1bc452010-10-20 15:57:31 -07001009#ifdef CONFIG_SMP
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001010/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 * del_timer_sync - deactivate a timer and wait for the handler to finish.
1012 * @timer: the timer to be deactivated
1013 *
1014 * This function only differs from del_timer() on SMP: besides deactivating
1015 * the timer it also makes sure the handler has finished executing on other
1016 * CPUs.
1017 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001018 * Synchronization rules: Callers must prevent restarting of the timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 * otherwise this function is meaningless. It must not be called from
Peter Zijlstra7ff20792011-02-08 15:18:00 +01001020 * interrupt contexts. The caller must not hold locks which would prevent
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001021 * completion of the timer's handler. The timer's handler must not call
1022 * add_timer_on(). Upon exit the timer is not queued and the handler is
1023 * not running on any CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 *
Steven Rostedt48228f72011-02-08 12:39:54 -05001025 * Note: You must not hold locks that are held in interrupt context
1026 * while calling this function. Even if the lock has nothing to do
1027 * with the timer in question. Here's why:
1028 *
1029 * CPU0 CPU1
1030 * ---- ----
1031 * <SOFTIRQ>
1032 * call_timer_fn();
1033 * base->running_timer = mytimer;
1034 * spin_lock_irq(somelock);
1035 * <IRQ>
1036 * spin_lock(somelock);
1037 * del_timer_sync(mytimer);
1038 * while (base->running_timer == mytimer);
1039 *
1040 * Now del_timer_sync() will never return and never release somelock.
1041 * The interrupt on the other CPU is waiting to grab somelock but
1042 * it has interrupted the softirq that CPU0 is waiting to finish.
1043 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 * The function returns whether it has deactivated a pending timer or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 */
1046int del_timer_sync(struct timer_list *timer)
1047{
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001048#ifdef CONFIG_LOCKDEP
Peter Zijlstraf266a512011-02-03 15:09:41 +01001049 unsigned long flags;
1050
Steven Rostedt48228f72011-02-08 12:39:54 -05001051 /*
1052 * If lockdep gives a backtrace here, please reference
1053 * the synchronization rules above.
1054 */
Peter Zijlstra7ff20792011-02-08 15:18:00 +01001055 local_irq_save(flags);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001056 lock_map_acquire(&timer->lockdep_map);
1057 lock_map_release(&timer->lockdep_map);
Peter Zijlstra7ff20792011-02-08 15:18:00 +01001058 local_irq_restore(flags);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001059#endif
Yong Zhang466bd302010-10-20 15:57:33 -07001060 /*
1061 * don't use it in hardirq context, because it
1062 * could lead to deadlock.
1063 */
1064 WARN_ON(in_irq());
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001065 for (;;) {
1066 int ret = try_to_del_timer_sync(timer);
1067 if (ret >= 0)
1068 return ret;
Andrew Mortona0009652006-07-14 00:24:06 -07001069 cpu_relax();
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072EXPORT_SYMBOL(del_timer_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073#endif
1074
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001075static int cascade(struct tvec_base *base, struct tvec *tv, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 /* cascade all the timers from tv up one level */
Porpoise3439dd82006-06-23 02:05:56 -07001078 struct timer_list *timer, *tmp;
1079 struct list_head tv_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Porpoise3439dd82006-06-23 02:05:56 -07001081 list_replace_init(tv->vec + index, &tv_list);
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /*
Porpoise3439dd82006-06-23 02:05:56 -07001084 * We are removing _all_ timers from the list, so we
1085 * don't have to detach them individually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
Porpoise3439dd82006-06-23 02:05:56 -07001087 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001088 BUG_ON(tbase_get_base(timer->base) != base);
Porpoise3439dd82006-06-23 02:05:56 -07001089 internal_add_timer(base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 return index;
1093}
1094
Thomas Gleixner576da122010-03-12 21:10:29 +01001095static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
1096 unsigned long data)
1097{
1098 int preempt_count = preempt_count();
1099
1100#ifdef CONFIG_LOCKDEP
1101 /*
1102 * It is permissible to free the timer from inside the
1103 * function that is called from it, this we need to take into
1104 * account for lockdep too. To avoid bogus "held lock freed"
1105 * warnings as well as problems when looking into
1106 * timer->lockdep_map, make a copy and use that here.
1107 */
1108 struct lockdep_map lockdep_map = timer->lockdep_map;
1109#endif
1110 /*
1111 * Couple the lock chain with the lock chain at
1112 * del_timer_sync() by acquiring the lock_map around the fn()
1113 * call here and in del_timer_sync().
1114 */
1115 lock_map_acquire(&lockdep_map);
1116
1117 trace_timer_expire_entry(timer);
1118 fn(data);
1119 trace_timer_expire_exit(timer);
1120
1121 lock_map_release(&lockdep_map);
1122
1123 if (preempt_count != preempt_count()) {
Thomas Gleixner802702e2010-03-12 20:13:23 +01001124 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
1125 fn, preempt_count, preempt_count());
1126 /*
1127 * Restore the preempt count. That gives us a decent
1128 * chance to survive and extract information. If the
1129 * callback kept a lock held, bad luck, but not worse
1130 * than the BUG() we had.
1131 */
1132 preempt_count() = preempt_count;
Thomas Gleixner576da122010-03-12 21:10:29 +01001133 }
1134}
1135
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001136#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
1137
1138/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 * __run_timers - run all expired timers (if any) on this CPU.
1140 * @base: the timer vector to be processed.
1141 *
1142 * This function cascades all vectors and executes all expired timer
1143 * vectors.
1144 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001145static inline void __run_timers(struct tvec_base *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 struct timer_list *timer;
1148
Oleg Nesterov3691c512006-03-31 02:30:30 -08001149 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 while (time_after_eq(jiffies, base->timer_jiffies)) {
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001151 struct list_head work_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct list_head *head = &work_list;
Thomas Gleixner68194572007-07-19 01:49:16 -07001153 int index = base->timer_jiffies & TVR_MASK;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /*
1156 * Cascade timers:
1157 */
1158 if (!index &&
1159 (!cascade(base, &base->tv2, INDEX(0))) &&
1160 (!cascade(base, &base->tv3, INDEX(1))) &&
1161 !cascade(base, &base->tv4, INDEX(2)))
1162 cascade(base, &base->tv5, INDEX(3));
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001163 ++base->timer_jiffies;
1164 list_replace_init(base->tv1.vec + index, &work_list);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001165 while (!list_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 void (*fn)(unsigned long);
1167 unsigned long data;
1168
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001169 timer = list_first_entry(head, struct timer_list,entry);
Thomas Gleixner68194572007-07-19 01:49:16 -07001170 fn = timer->function;
1171 data = timer->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001173 timer_stats_account_timer(timer);
1174
Yong Zhang6f1bc452010-10-20 15:57:31 -07001175 base->running_timer = timer;
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001176 detach_timer(timer, 1);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001177
Oleg Nesterov3691c512006-03-31 02:30:30 -08001178 spin_unlock_irq(&base->lock);
Thomas Gleixner576da122010-03-12 21:10:29 +01001179 call_timer_fn(timer, fn, data);
Oleg Nesterov3691c512006-03-31 02:30:30 -08001180 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 }
Yong Zhang6f1bc452010-10-20 15:57:31 -07001183 base->running_timer = NULL;
Oleg Nesterov3691c512006-03-31 02:30:30 -08001184 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Russell Kingee9c5782008-04-20 13:59:33 +01001187#ifdef CONFIG_NO_HZ
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188/*
1189 * Find out when the next timer event is due to happen. This
Randy Dunlap90cba642009-08-25 14:35:41 -07001190 * is used on S/390 to stop all activity when a CPU is idle.
1191 * This function needs to be called with interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001193static unsigned long __next_timer_interrupt(struct tvec_base *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001195 unsigned long timer_jiffies = base->timer_jiffies;
Thomas Gleixnereaad0842007-05-29 23:47:39 +02001196 unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001197 int index, slot, array, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 struct timer_list *nte;
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001199 struct tvec *varray[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 /* Look for timer events in tv1. */
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001202 index = slot = timer_jiffies & TVR_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001204 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
Thomas Gleixner68194572007-07-19 01:49:16 -07001205 if (tbase_get_deferrable(nte->base))
1206 continue;
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001207
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001208 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001210 /* Look at the cascade bucket(s)? */
1211 if (!index || slot < index)
1212 goto cascade;
1213 return expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001215 slot = (slot + 1) & TVR_MASK;
1216 } while (slot != index);
1217
1218cascade:
1219 /* Calculate the next cascade event */
1220 if (index)
1221 timer_jiffies += TVR_SIZE - index;
1222 timer_jiffies >>= TVR_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 /* Check tv2-tv5. */
1225 varray[0] = &base->tv2;
1226 varray[1] = &base->tv3;
1227 varray[2] = &base->tv4;
1228 varray[3] = &base->tv5;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001229
1230 for (array = 0; array < 4; array++) {
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001231 struct tvec *varp = varray[array];
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001232
1233 index = slot = timer_jiffies & TVN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001235 list_for_each_entry(nte, varp->vec + slot, entry) {
Jon Huntera04198882009-05-01 13:10:23 -07001236 if (tbase_get_deferrable(nte->base))
1237 continue;
1238
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001239 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (time_before(nte->expires, expires))
1241 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001242 }
1243 /*
1244 * Do we still search for the first timer or are
1245 * we looking up the cascade buckets ?
1246 */
1247 if (found) {
1248 /* Look at the cascade bucket(s)? */
1249 if (!index || slot < index)
1250 break;
1251 return expires;
1252 }
1253 slot = (slot + 1) & TVN_MASK;
1254 } while (slot != index);
1255
1256 if (index)
1257 timer_jiffies += TVN_SIZE - index;
1258 timer_jiffies >>= TVN_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001260 return expires;
1261}
1262
1263/*
1264 * Check, if the next hrtimer event is before the next timer wheel
1265 * event:
1266 */
1267static unsigned long cmp_next_hrtimer_event(unsigned long now,
1268 unsigned long expires)
1269{
1270 ktime_t hr_delta = hrtimer_get_next_event();
1271 struct timespec tsdelta;
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001272 unsigned long delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001273
1274 if (hr_delta.tv64 == KTIME_MAX)
1275 return expires;
1276
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001277 /*
1278 * Expired timer available, let it expire in the next tick
1279 */
1280 if (hr_delta.tv64 <= 0)
1281 return now + 1;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001282
1283 tsdelta = ktime_to_timespec(hr_delta);
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001284 delta = timespec_to_jiffies(&tsdelta);
Thomas Gleixnereaad0842007-05-29 23:47:39 +02001285
1286 /*
1287 * Limit the delta to the max value, which is checked in
1288 * tick_nohz_stop_sched_tick():
1289 */
1290 if (delta > NEXT_TIMER_MAX_DELTA)
1291 delta = NEXT_TIMER_MAX_DELTA;
1292
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001293 /*
1294 * Take rounding errors in to account and make sure, that it
1295 * expires in the next tick. Otherwise we go into an endless
1296 * ping pong due to tick_nohz_stop_sched_tick() retriggering
1297 * the timer softirq
1298 */
1299 if (delta < 1)
1300 delta = 1;
1301 now += delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001302 if (time_before(now, expires))
1303 return now;
1304 return expires;
1305}
1306
1307/**
Li Zefan8dce39c2007-11-05 14:51:10 -08001308 * get_next_timer_interrupt - return the jiffy of the next pending timer
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08001309 * @now: current time (in jiffies)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001310 */
Thomas Gleixnerfd064b92007-02-16 01:27:47 -08001311unsigned long get_next_timer_interrupt(unsigned long now)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001312{
Christoph Lameter74963512010-11-30 14:05:53 -06001313 struct tvec_base *base = __this_cpu_read(tvec_bases);
Thomas Gleixnerfd064b92007-02-16 01:27:47 -08001314 unsigned long expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001315
Heiko Carstensdbd87b52010-12-01 10:11:09 +01001316 /*
1317 * Pretend that there is no timer pending if the cpu is offline.
1318 * Possible pending timers will be migrated later to an active cpu.
1319 */
1320 if (cpu_is_offline(smp_processor_id()))
1321 return now + NEXT_TIMER_MAX_DELTA;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001322 spin_lock(&base->lock);
Martin Schwidefsky97fd9ed2009-07-21 20:25:05 +02001323 if (time_before_eq(base->next_timer, base->timer_jiffies))
1324 base->next_timer = __next_timer_interrupt(base);
1325 expires = base->next_timer;
Oleg Nesterov3691c512006-03-31 02:30:30 -08001326 spin_unlock(&base->lock);
Tony Lindgren69239742006-03-06 15:42:45 -08001327
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001328 if (time_before_eq(expires, now))
1329 return now;
Zachary Amsden0662b712006-05-20 15:00:24 -07001330
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001331 return cmp_next_hrtimer_event(now, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333#endif
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335/*
Daniel Walker5b4db0c2007-10-18 03:06:11 -07001336 * Called from the timer interrupt handler to charge one tick to the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * process. user_tick is 1 if the tick is user time, 0 for system.
1338 */
1339void update_process_times(int user_tick)
1340{
1341 struct task_struct *p = current;
1342 int cpu = smp_processor_id();
1343
1344 /* Note: this timer irq context must be accounted for as well. */
Paul Mackerrasfa13a5a2007-11-09 22:39:38 +01001345 account_process_tick(p, user_tick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 run_local_timers();
Paul E. McKenneya1572292009-08-22 13:56:51 -07001347 rcu_check_callbacks(cpu, user_tick);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001348 printk_tick();
Peter Zijlstrae360adb2010-10-14 14:01:34 +08001349#ifdef CONFIG_IRQ_WORK
1350 if (in_irq())
1351 irq_work_run();
1352#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 scheduler_tick();
Thomas Gleixner68194572007-07-19 01:49:16 -07001354 run_posix_cpu_timers(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
1357/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 * This function runs timers and the timer-tq in bottom half context.
1359 */
1360static void run_timer_softirq(struct softirq_action *h)
1361{
Christoph Lameter74963512010-11-30 14:05:53 -06001362 struct tvec_base *base = __this_cpu_read(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001364 hrtimer_run_pending();
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (time_after_eq(jiffies, base->timer_jiffies))
1367 __run_timers(base);
1368}
1369
1370/*
1371 * Called by the local, per-CPU timer interrupt on SMP.
1372 */
1373void run_local_timers(void)
1374{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001375 hrtimer_run_queues();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 raise_softirq(TIMER_SOFTIRQ);
1377}
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379#ifdef __ARCH_WANT_SYS_ALARM
1380
1381/*
1382 * For backwards compatibility? This can be done in libc so Alpha
1383 * and all newer ports shouldn't need it.
1384 */
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001385SYSCALL_DEFINE1(alarm, unsigned int, seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -08001387 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
1390#endif
1391
1392#ifndef __alpha__
1393
1394/*
1395 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
1396 * should be moved into arch/i386 instead?
1397 */
1398
1399/**
1400 * sys_getpid - return the thread group id of the current process
1401 *
1402 * Note, despite the name, this returns the tgid not the pid. The tgid and
1403 * the pid are identical unless CLONE_THREAD was specified on clone() in
1404 * which case the tgid is the same in all threads of the same group.
1405 *
1406 * This is SMP safe as current->tgid does not change.
1407 */
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001408SYSCALL_DEFINE0(getpid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001410 return task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
1413/*
Kirill Korotaev6997a6f2006-08-13 23:24:23 -07001414 * Accessing ->real_parent is not SMP-safe, it could
1415 * change from under us. However, we can use a stale
1416 * value of ->real_parent under rcu_read_lock(), see
1417 * release_task()->call_rcu(delayed_put_task_struct).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001419SYSCALL_DEFINE0(getppid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 int pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Kirill Korotaev6997a6f2006-08-13 23:24:23 -07001423 rcu_read_lock();
Mandeep Singh Baines031af1652011-12-08 14:34:44 -08001424 pid = task_tgid_vnr(rcu_dereference(current->real_parent));
Kirill Korotaev6997a6f2006-08-13 23:24:23 -07001425 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return pid;
1428}
1429
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001430SYSCALL_DEFINE0(getuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 /* Only we change this so SMP safe */
David Howells76aac0e2008-11-14 10:39:12 +11001433 return current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001436SYSCALL_DEFINE0(geteuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 /* Only we change this so SMP safe */
David Howells76aac0e2008-11-14 10:39:12 +11001439 return current_euid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001442SYSCALL_DEFINE0(getgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 /* Only we change this so SMP safe */
David Howells76aac0e2008-11-14 10:39:12 +11001445 return current_gid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Heiko Carstensdbf040d2009-01-14 14:14:04 +01001448SYSCALL_DEFINE0(getegid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 /* Only we change this so SMP safe */
David Howells76aac0e2008-11-14 10:39:12 +11001451 return current_egid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
1454#endif
1455
1456static void process_timeout(unsigned long __data)
1457{
Ingo Molnar36c8b582006-07-03 00:25:41 -07001458 wake_up_process((struct task_struct *)__data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461/**
1462 * schedule_timeout - sleep until timeout
1463 * @timeout: timeout value in jiffies
1464 *
1465 * Make the current task sleep until @timeout jiffies have
1466 * elapsed. The routine will return immediately unless
1467 * the current task state has been set (see set_current_state()).
1468 *
1469 * You can set the task state as follows -
1470 *
1471 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1472 * pass before the routine returns. The routine will return 0
1473 *
1474 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1475 * delivered to the current task. In this case the remaining time
1476 * in jiffies will be returned, or 0 if the timer expired in time
1477 *
1478 * The current task state is guaranteed to be TASK_RUNNING when this
1479 * routine returns.
1480 *
1481 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1482 * the CPU away without a bound on the timeout. In this case the return
1483 * value will be %MAX_SCHEDULE_TIMEOUT.
1484 *
1485 * In all cases the return value is guaranteed to be non-negative.
1486 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001487signed long __sched schedule_timeout(signed long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 struct timer_list timer;
1490 unsigned long expire;
1491
1492 switch (timeout)
1493 {
1494 case MAX_SCHEDULE_TIMEOUT:
1495 /*
1496 * These two special cases are useful to be comfortable
1497 * in the caller. Nothing more. We could take
1498 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1499 * but I' d like to return a valid offset (>=0) to allow
1500 * the caller to do everything it want with the retval.
1501 */
1502 schedule();
1503 goto out;
1504 default:
1505 /*
1506 * Another bit of PARANOID. Note that the retval will be
1507 * 0 since no piece of kernel is supposed to do a check
1508 * for a negative retval of schedule_timeout() (since it
1509 * should never happens anyway). You just have the printk()
1510 * that will tell you if something is gone wrong and where.
1511 */
Andrew Morton5b149bc2006-12-22 01:10:14 -08001512 if (timeout < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 printk(KERN_ERR "schedule_timeout: wrong timeout "
Andrew Morton5b149bc2006-12-22 01:10:14 -08001514 "value %lx\n", timeout);
1515 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 current->state = TASK_RUNNING;
1517 goto out;
1518 }
1519 }
1520
1521 expire = timeout + jiffies;
1522
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001523 setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
Arun R Bharadwaj597d0272009-04-16 12:13:26 +05301524 __mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 schedule();
1526 del_singleshot_timer_sync(&timer);
1527
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001528 /* Remove the timer from the object tracker */
1529 destroy_timer_on_stack(&timer);
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 timeout = expire - jiffies;
1532
1533 out:
1534 return timeout < 0 ? 0 : timeout;
1535}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536EXPORT_SYMBOL(schedule_timeout);
1537
Andrew Morton8a1c1752005-09-13 01:25:15 -07001538/*
1539 * We can use __set_current_state() here because schedule_timeout() calls
1540 * schedule() unconditionally.
1541 */
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001542signed long __sched schedule_timeout_interruptible(signed long timeout)
1543{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001544 __set_current_state(TASK_INTERRUPTIBLE);
1545 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001546}
1547EXPORT_SYMBOL(schedule_timeout_interruptible);
1548
Matthew Wilcox294d5cc2007-12-06 11:59:46 -05001549signed long __sched schedule_timeout_killable(signed long timeout)
1550{
1551 __set_current_state(TASK_KILLABLE);
1552 return schedule_timeout(timeout);
1553}
1554EXPORT_SYMBOL(schedule_timeout_killable);
1555
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001556signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1557{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001558 __set_current_state(TASK_UNINTERRUPTIBLE);
1559 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001560}
1561EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563/* Thread ID - the internal kernel "pid" */
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001564SYSCALL_DEFINE0(gettid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001566 return task_pid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001569/**
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001570 * do_sysinfo - fill in sysinfo struct
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001571 * @info: pointer to buffer to fill
Thomas Gleixner68194572007-07-19 01:49:16 -07001572 */
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001573int do_sysinfo(struct sysinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 unsigned long mem_total, sav_total;
1576 unsigned int mem_unit, bitcount;
Thomas Gleixner2d024942009-05-02 20:08:52 +02001577 struct timespec tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001579 memset(info, 0, sizeof(struct sysinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Thomas Gleixner2d024942009-05-02 20:08:52 +02001581 ktime_get_ts(&tp);
1582 monotonic_to_bootbased(&tp);
1583 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Thomas Gleixner2d024942009-05-02 20:08:52 +02001585 get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Thomas Gleixner2d024942009-05-02 20:08:52 +02001587 info->procs = nr_threads;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001589 si_meminfo(info);
1590 si_swapinfo(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /*
1593 * If the sum of all the available memory (i.e. ram + swap)
1594 * is less than can be stored in a 32 bit unsigned long then
1595 * we can be binary compatible with 2.2.x kernels. If not,
1596 * well, in that case 2.2.x was broken anyways...
1597 *
1598 * -Erik Andersen <andersee@debian.org>
1599 */
1600
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001601 mem_total = info->totalram + info->totalswap;
1602 if (mem_total < info->totalram || mem_total < info->totalswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 goto out;
1604 bitcount = 0;
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001605 mem_unit = info->mem_unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 while (mem_unit > 1) {
1607 bitcount++;
1608 mem_unit >>= 1;
1609 sav_total = mem_total;
1610 mem_total <<= 1;
1611 if (mem_total < sav_total)
1612 goto out;
1613 }
1614
1615 /*
1616 * If mem_total did not overflow, multiply all memory values by
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001617 * info->mem_unit and set it to 1. This leaves things compatible
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1619 * kernels...
1620 */
1621
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001622 info->mem_unit = 1;
1623 info->totalram <<= bitcount;
1624 info->freeram <<= bitcount;
1625 info->sharedram <<= bitcount;
1626 info->bufferram <<= bitcount;
1627 info->totalswap <<= bitcount;
1628 info->freeswap <<= bitcount;
1629 info->totalhigh <<= bitcount;
1630 info->freehigh <<= bitcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001632out:
1633 return 0;
1634}
1635
Heiko Carstens1e7bfb22009-01-14 14:14:29 +01001636SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001637{
1638 struct sysinfo val;
1639
1640 do_sysinfo(&val);
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1643 return -EFAULT;
1644
1645 return 0;
1646}
1647
Adrian Bunkb4be6252007-12-18 18:05:58 +01001648static int __cpuinit init_timers_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
1650 int j;
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001651 struct tvec_base *base;
Adrian Bunkb4be6252007-12-18 18:05:58 +01001652 static char __cpuinitdata tvec_base_done[NR_CPUS];
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001653
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001654 if (!tvec_base_done[cpu]) {
Jan Beulicha4a61982006-03-24 03:15:54 -08001655 static char boot_done;
1656
Jan Beulicha4a61982006-03-24 03:15:54 -08001657 if (boot_done) {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001658 /*
1659 * The APs use this path later in boot
1660 */
Christoph Lameter94f60302007-07-17 04:03:29 -07001661 base = kmalloc_node(sizeof(*base),
1662 GFP_KERNEL | __GFP_ZERO,
Jan Beulicha4a61982006-03-24 03:15:54 -08001663 cpu_to_node(cpu));
1664 if (!base)
1665 return -ENOMEM;
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001666
1667 /* Make sure that tvec_base is 2 byte aligned */
1668 if (tbase_get_deferrable(base)) {
1669 WARN_ON(1);
1670 kfree(base);
1671 return -ENOMEM;
1672 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001673 per_cpu(tvec_bases, cpu) = base;
Jan Beulicha4a61982006-03-24 03:15:54 -08001674 } else {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001675 /*
1676 * This is for the boot CPU - we use compile-time
1677 * static initialisation because per-cpu memory isn't
1678 * ready yet and because the memory allocators are not
1679 * initialised either.
1680 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001681 boot_done = 1;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001682 base = &boot_tvec_bases;
Jan Beulicha4a61982006-03-24 03:15:54 -08001683 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001684 tvec_base_done[cpu] = 1;
1685 } else {
1686 base = per_cpu(tvec_bases, cpu);
Jan Beulicha4a61982006-03-24 03:15:54 -08001687 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001688
Oleg Nesterov3691c512006-03-31 02:30:30 -08001689 spin_lock_init(&base->lock);
Ingo Molnard730e882006-07-03 00:25:10 -07001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 for (j = 0; j < TVN_SIZE; j++) {
1692 INIT_LIST_HEAD(base->tv5.vec + j);
1693 INIT_LIST_HEAD(base->tv4.vec + j);
1694 INIT_LIST_HEAD(base->tv3.vec + j);
1695 INIT_LIST_HEAD(base->tv2.vec + j);
1696 }
1697 for (j = 0; j < TVR_SIZE; j++)
1698 INIT_LIST_HEAD(base->tv1.vec + j);
1699
1700 base->timer_jiffies = jiffies;
Martin Schwidefsky97fd9ed2009-07-21 20:25:05 +02001701 base->next_timer = base->timer_jiffies;
Jan Beulicha4a61982006-03-24 03:15:54 -08001702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
1705#ifdef CONFIG_HOTPLUG_CPU
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001706static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
1708 struct timer_list *timer;
1709
1710 while (!list_empty(head)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001711 timer = list_first_entry(head, struct timer_list, entry);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001712 detach_timer(timer, 0);
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001713 timer_set_base(timer, new_base);
Martin Schwidefsky97fd9ed2009-07-21 20:25:05 +02001714 if (time_before(timer->expires, new_base->next_timer) &&
1715 !tbase_get_deferrable(timer->base))
1716 new_base->next_timer = timer->expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 internal_add_timer(new_base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
Randy Dunlap48ccf3d2008-01-21 17:18:25 -08001721static void __cpuinit migrate_timers(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001723 struct tvec_base *old_base;
1724 struct tvec_base *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 int i;
1726
1727 BUG_ON(cpu_online(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001728 old_base = per_cpu(tvec_bases, cpu);
1729 new_base = get_cpu_var(tvec_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001730 /*
1731 * The caller is globally serialized and nobody else
1732 * takes two locks at once, deadlock is not possible.
1733 */
1734 spin_lock_irq(&new_base->lock);
Oleg Nesterov0d180402008-04-04 20:54:10 +02001735 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Oleg Nesterov3691c512006-03-31 02:30:30 -08001737 BUG_ON(old_base->running_timer);
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 for (i = 0; i < TVR_SIZE; i++)
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001740 migrate_timer_list(new_base, old_base->tv1.vec + i);
1741 for (i = 0; i < TVN_SIZE; i++) {
1742 migrate_timer_list(new_base, old_base->tv2.vec + i);
1743 migrate_timer_list(new_base, old_base->tv3.vec + i);
1744 migrate_timer_list(new_base, old_base->tv4.vec + i);
1745 migrate_timer_list(new_base, old_base->tv5.vec + i);
1746 }
1747
Oleg Nesterov0d180402008-04-04 20:54:10 +02001748 spin_unlock(&old_base->lock);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001749 spin_unlock_irq(&new_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 put_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
1752#endif /* CONFIG_HOTPLUG_CPU */
1753
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001754static int __cpuinit timer_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 unsigned long action, void *hcpu)
1756{
1757 long cpu = (long)hcpu;
Akinobu Mita80b51842010-05-26 14:43:32 -07001758 int err;
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 switch(action) {
1761 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001762 case CPU_UP_PREPARE_FROZEN:
Akinobu Mita80b51842010-05-26 14:43:32 -07001763 err = init_timers_cpu(cpu);
1764 if (err < 0)
1765 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 break;
1767#ifdef CONFIG_HOTPLUG_CPU
1768 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001769 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 migrate_timers(cpu);
1771 break;
1772#endif
1773 default:
1774 break;
1775 }
1776 return NOTIFY_OK;
1777}
1778
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001779static struct notifier_block __cpuinitdata timers_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 .notifier_call = timer_cpu_notify,
1781};
1782
1783
1784void __init init_timers(void)
1785{
Akinobu Mita07dccf32006-09-29 02:00:22 -07001786 int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 (void *)(long)smp_processor_id());
Akinobu Mita07dccf32006-09-29 02:00:22 -07001788
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001789 init_timer_stats();
1790
Akinobu Mita9e506f72010-06-04 14:15:04 -07001791 BUG_ON(err != NOTIFY_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 register_cpu_notifier(&timers_nb);
Carlos R. Mafra962cf362008-05-15 11:15:37 -03001793 open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796/**
1797 * msleep - sleep safely even with waitqueue interruptions
1798 * @msecs: Time in milliseconds to sleep for
1799 */
1800void msleep(unsigned int msecs)
1801{
1802 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1803
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001804 while (timeout)
1805 timeout = schedule_timeout_uninterruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
1808EXPORT_SYMBOL(msleep);
1809
1810/**
Domen Puncer96ec3ef2005-06-25 14:58:43 -07001811 * msleep_interruptible - sleep waiting for signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 * @msecs: Time in milliseconds to sleep for
1813 */
1814unsigned long msleep_interruptible(unsigned int msecs)
1815{
1816 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1817
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001818 while (timeout && !signal_pending(current))
1819 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return jiffies_to_msecs(timeout);
1821}
1822
1823EXPORT_SYMBOL(msleep_interruptible);
Patrick Pannuto5e7f5a12010-08-02 15:01:04 -07001824
1825static int __sched do_usleep_range(unsigned long min, unsigned long max)
1826{
1827 ktime_t kmin;
1828 unsigned long delta;
1829
1830 kmin = ktime_set(0, min * NSEC_PER_USEC);
1831 delta = (max - min) * NSEC_PER_USEC;
1832 return schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL);
1833}
1834
1835/**
1836 * usleep_range - Drop in replacement for udelay where wakeup is flexible
1837 * @min: Minimum time in usecs to sleep
1838 * @max: Maximum time in usecs to sleep
1839 */
1840void usleep_range(unsigned long min, unsigned long max)
1841{
1842 __set_current_state(TASK_UNINTERRUPTIBLE);
1843 do_usleep_range(min, max);
1844}
1845EXPORT_SYMBOL(usleep_range);