blob: b75e7893be14ee80638f18f08486bf129c9018ac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/timer.c
3 *
Stephen Rothwell4a22f162013-04-30 15:27:37 -07004 * Kernel internal timers
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>
Clark Williamscf4aebc22013-02-07 09:46:59 -060042#include <linux/sched/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Stephen Rothwell1a0df592013-04-30 15:27:34 -070044#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <asm/uaccess.h>
47#include <asm/unistd.h>
48#include <asm/div64.h>
49#include <asm/timex.h>
50#include <asm/io.h>
51
Xiao Guangrong2b022e32009-08-10 10:48:59 +080052#define CREATE_TRACE_POINTS
53#include <trace/events/timer.h>
54
Thomas Gleixnerecea8d12005-10-30 15:03:00 -080055u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
56
57EXPORT_SYMBOL(jiffies_64);
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * per-CPU timer vector definitions:
61 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
63#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
64#define TVN_SIZE (1 << TVN_BITS)
65#define TVR_SIZE (1 << TVR_BITS)
66#define TVN_MASK (TVN_SIZE - 1)
67#define TVR_MASK (TVR_SIZE - 1)
Hildner, Christian26cff4e2012-10-08 15:49:03 +020068#define MAX_TVAL ((unsigned long)((1ULL << (TVR_BITS + 4*TVN_BITS)) - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Pavel Macheka6fa8e52008-01-30 13:30:00 +010070struct tvec {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 struct list_head vec[TVN_SIZE];
Pavel Macheka6fa8e52008-01-30 13:30:00 +010072};
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Pavel Macheka6fa8e52008-01-30 13:30:00 +010074struct tvec_root {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct list_head vec[TVR_SIZE];
Pavel Macheka6fa8e52008-01-30 13:30:00 +010076};
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Pavel Macheka6fa8e52008-01-30 13:30:00 +010078struct tvec_base {
Oleg Nesterov3691c512006-03-31 02:30:30 -080079 spinlock_t lock;
80 struct timer_list *running_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 unsigned long timer_jiffies;
Martin Schwidefsky97fd9ed2009-07-21 20:25:05 +020082 unsigned long next_timer;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +000083 unsigned long active_timers;
Pavel Macheka6fa8e52008-01-30 13:30:00 +010084 struct tvec_root tv1;
85 struct tvec tv2;
86 struct tvec tv3;
87 struct tvec tv4;
88 struct tvec tv5;
Venki Pallipadi6e453a62007-05-08 00:27:44 -070089} ____cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Pavel Macheka6fa8e52008-01-30 13:30:00 +010091struct tvec_base boot_tvec_bases;
Oleg Nesterov3691c512006-03-31 02:30:30 -080092EXPORT_SYMBOL(boot_tvec_bases);
Pavel Macheka6fa8e52008-01-30 13:30:00 +010093static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Venki Pallipadi6e453a62007-05-08 00:27:44 -070095/* Functions below help us manage 'deferrable' flag */
Pavel Macheka6fa8e52008-01-30 13:30:00 +010096static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -070097{
Tejun Heoe52b1db2012-08-08 11:10:25 -070098 return ((unsigned int)(unsigned long)base & TIMER_DEFERRABLE);
Venki Pallipadi6e453a62007-05-08 00:27:44 -070099}
100
Tejun Heoc5f66e92012-08-08 11:10:28 -0700101static inline unsigned int tbase_get_irqsafe(struct tvec_base *base)
102{
103 return ((unsigned int)(unsigned long)base & TIMER_IRQSAFE);
104}
105
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100106static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700107{
Tejun Heoe52b1db2012-08-08 11:10:25 -0700108 return ((struct tvec_base *)((unsigned long)base & ~TIMER_FLAG_MASK));
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700109}
110
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700111static inline void
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100112timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700113{
Tejun Heoe52b1db2012-08-08 11:10:25 -0700114 unsigned long flags = (unsigned long)timer->base & TIMER_FLAG_MASK;
115
116 timer->base = (struct tvec_base *)((unsigned long)(new_base) | flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700117}
118
Alan Stern9c133c42008-11-06 08:42:48 +0100119static unsigned long round_jiffies_common(unsigned long j, int cpu,
120 bool force_up)
121{
122 int rem;
123 unsigned long original = j;
124
125 /*
126 * We don't want all cpus firing their timers at once hitting the
127 * same lock or cachelines, so we skew each extra cpu with an extra
128 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
129 * already did this.
130 * The skew is done by adding 3*cpunr, then round, then subtract this
131 * extra offset again.
132 */
133 j += cpu * 3;
134
135 rem = j % HZ;
136
137 /*
138 * If the target jiffie is just after a whole second (which can happen
139 * due to delays of the timer irq, long irq off times etc etc) then
140 * we should round down to the whole second, not up. Use 1/4th second
141 * as cutoff for this rounding as an extreme upper bound for this.
142 * But never round down if @force_up is set.
143 */
144 if (rem < HZ/4 && !force_up) /* round down */
145 j = j - rem;
146 else /* round up */
147 j = j - rem + HZ;
148
149 /* now that we have rounded, subtract the extra skew again */
150 j -= cpu * 3;
151
Bart Van Assche9e04d382013-05-21 20:43:50 +0200152 /*
153 * Make sure j is still in the future. Otherwise return the
154 * unmodified value.
155 */
156 return time_is_after_jiffies(j) ? j : original;
Alan Stern9c133c42008-11-06 08:42:48 +0100157}
158
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800159/**
160 * __round_jiffies - function to round jiffies to a full second
161 * @j: the time in (absolute) jiffies that should be rounded
162 * @cpu: the processor number on which the timeout will happen
163 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800164 * __round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800165 * up or down to (approximately) full seconds. This is useful for timers
166 * for which the exact time they fire does not matter too much, as long as
167 * they fire approximately every X seconds.
168 *
169 * By rounding these timers to whole seconds, all such timers will fire
170 * at the same time, rather than at various times spread out. The goal
171 * of this is to have the CPU wake up less, which saves power.
172 *
173 * The exact rounding is skewed for each processor to avoid all
174 * processors firing at the exact same time, which could lead
175 * to lock contention or spurious cache line bouncing.
176 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800177 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800178 */
179unsigned long __round_jiffies(unsigned long j, int cpu)
180{
Alan Stern9c133c42008-11-06 08:42:48 +0100181 return round_jiffies_common(j, cpu, false);
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800182}
183EXPORT_SYMBOL_GPL(__round_jiffies);
184
185/**
186 * __round_jiffies_relative - function to round jiffies to a full second
187 * @j: the time in (relative) jiffies that should be rounded
188 * @cpu: the processor number on which the timeout will happen
189 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800190 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800191 * up or down to (approximately) full seconds. This is useful for timers
192 * for which the exact time they fire does not matter too much, as long as
193 * they fire approximately every X seconds.
194 *
195 * By rounding these timers to whole seconds, all such timers will fire
196 * at the same time, rather than at various times spread out. The goal
197 * of this is to have the CPU wake up less, which saves power.
198 *
199 * The exact rounding is skewed for each processor to avoid all
200 * processors firing at the exact same time, which could lead
201 * to lock contention or spurious cache line bouncing.
202 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800203 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800204 */
205unsigned long __round_jiffies_relative(unsigned long j, int cpu)
206{
Alan Stern9c133c42008-11-06 08:42:48 +0100207 unsigned long j0 = jiffies;
208
209 /* Use j0 because jiffies might change while we run */
210 return round_jiffies_common(j + j0, cpu, false) - j0;
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800211}
212EXPORT_SYMBOL_GPL(__round_jiffies_relative);
213
214/**
215 * round_jiffies - function to round jiffies to a full second
216 * @j: the time in (absolute) jiffies that should be rounded
217 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800218 * round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800219 * up or down to (approximately) full seconds. This is useful for timers
220 * for which the exact time they fire does not matter too much, as long as
221 * they fire approximately every X seconds.
222 *
223 * By rounding these timers to whole seconds, all such timers will fire
224 * at the same time, rather than at various times spread out. The goal
225 * of this is to have the CPU wake up less, which saves power.
226 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800227 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800228 */
229unsigned long round_jiffies(unsigned long j)
230{
Alan Stern9c133c42008-11-06 08:42:48 +0100231 return round_jiffies_common(j, raw_smp_processor_id(), false);
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800232}
233EXPORT_SYMBOL_GPL(round_jiffies);
234
235/**
236 * round_jiffies_relative - function to round jiffies to a full second
237 * @j: the time in (relative) jiffies that should be rounded
238 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800239 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800240 * up or down to (approximately) full seconds. This is useful for timers
241 * for which the exact time they fire does not matter too much, as long as
242 * they fire approximately every X seconds.
243 *
244 * By rounding these timers to whole seconds, all such timers will fire
245 * at the same time, rather than at various times spread out. The goal
246 * of this is to have the CPU wake up less, which saves power.
247 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800248 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800249 */
250unsigned long round_jiffies_relative(unsigned long j)
251{
252 return __round_jiffies_relative(j, raw_smp_processor_id());
253}
254EXPORT_SYMBOL_GPL(round_jiffies_relative);
255
Alan Stern9c133c42008-11-06 08:42:48 +0100256/**
257 * __round_jiffies_up - function to round jiffies up to a full second
258 * @j: the time in (absolute) jiffies that should be rounded
259 * @cpu: the processor number on which the timeout will happen
260 *
261 * This is the same as __round_jiffies() except that it will never
262 * round down. This is useful for timeouts for which the exact time
263 * of firing does not matter too much, as long as they don't fire too
264 * early.
265 */
266unsigned long __round_jiffies_up(unsigned long j, int cpu)
267{
268 return round_jiffies_common(j, cpu, true);
269}
270EXPORT_SYMBOL_GPL(__round_jiffies_up);
271
272/**
273 * __round_jiffies_up_relative - function to round jiffies up to a full second
274 * @j: the time in (relative) jiffies that should be rounded
275 * @cpu: the processor number on which the timeout will happen
276 *
277 * This is the same as __round_jiffies_relative() except that it will never
278 * round down. This is useful for timeouts for which the exact time
279 * of firing does not matter too much, as long as they don't fire too
280 * early.
281 */
282unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
283{
284 unsigned long j0 = jiffies;
285
286 /* Use j0 because jiffies might change while we run */
287 return round_jiffies_common(j + j0, cpu, true) - j0;
288}
289EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
290
291/**
292 * round_jiffies_up - function to round jiffies up to a full second
293 * @j: the time in (absolute) jiffies that should be rounded
294 *
295 * This is the same as round_jiffies() except that it will never
296 * round down. This is useful for timeouts for which the exact time
297 * of firing does not matter too much, as long as they don't fire too
298 * early.
299 */
300unsigned long round_jiffies_up(unsigned long j)
301{
302 return round_jiffies_common(j, raw_smp_processor_id(), true);
303}
304EXPORT_SYMBOL_GPL(round_jiffies_up);
305
306/**
307 * round_jiffies_up_relative - function to round jiffies up to a full second
308 * @j: the time in (relative) jiffies that should be rounded
309 *
310 * This is the same as round_jiffies_relative() except that it will never
311 * round down. This is useful for timeouts for which the exact time
312 * of firing does not matter too much, as long as they don't fire too
313 * early.
314 */
315unsigned long round_jiffies_up_relative(unsigned long j)
316{
317 return __round_jiffies_up_relative(j, raw_smp_processor_id());
318}
319EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
320
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800321/**
322 * set_timer_slack - set the allowed slack for a timer
Randy Dunlap0caa6212010-08-09 16:32:50 -0700323 * @timer: the timer to be modified
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800324 * @slack_hz: the amount of time (in jiffies) allowed for rounding
325 *
326 * Set the amount of time, in jiffies, that a certain timer has
327 * in terms of slack. By setting this value, the timer subsystem
328 * will schedule the actual timer somewhere between
329 * the time mod_timer() asks for, and that time plus the slack.
330 *
331 * By setting the slack to -1, a percentage of the delay is used
332 * instead.
333 */
334void set_timer_slack(struct timer_list *timer, int slack_hz)
335{
336 timer->slack = slack_hz;
337}
338EXPORT_SYMBOL_GPL(set_timer_slack);
339
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000340static void
341__internal_add_timer(struct tvec_base *base, struct timer_list *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 unsigned long expires = timer->expires;
344 unsigned long idx = expires - base->timer_jiffies;
345 struct list_head *vec;
346
347 if (idx < TVR_SIZE) {
348 int i = expires & TVR_MASK;
349 vec = base->tv1.vec + i;
350 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
351 int i = (expires >> TVR_BITS) & TVN_MASK;
352 vec = base->tv2.vec + i;
353 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
354 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
355 vec = base->tv3.vec + i;
356 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
357 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
358 vec = base->tv4.vec + i;
359 } else if ((signed long) idx < 0) {
360 /*
361 * Can happen if you add a timer with expires == jiffies,
362 * or you set a timer to go off in the past
363 */
364 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
365 } else {
366 int i;
Hildner, Christian26cff4e2012-10-08 15:49:03 +0200367 /* If the timeout is larger than MAX_TVAL (on 64-bit
368 * architectures or with CONFIG_BASE_SMALL=1) then we
369 * use the maximum timeout.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
Hildner, Christian26cff4e2012-10-08 15:49:03 +0200371 if (idx > MAX_TVAL) {
372 idx = MAX_TVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 expires = idx + base->timer_jiffies;
374 }
375 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
376 vec = base->tv5.vec + i;
377 }
378 /*
379 * Timers are FIFO:
380 */
381 list_add_tail(&timer->entry, vec);
382}
383
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000384static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
385{
386 __internal_add_timer(base, timer);
387 /*
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000388 * Update base->active_timers and base->next_timer
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000389 */
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000390 if (!tbase_get_deferrable(timer->base)) {
391 if (time_before(timer->expires, base->next_timer))
392 base->next_timer = timer->expires;
393 base->active_timers++;
394 }
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000395}
396
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800397#ifdef CONFIG_TIMER_STATS
398void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
399{
400 if (timer->start_site)
401 return;
402
403 timer->start_site = addr;
404 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
405 timer->start_pid = current->pid;
406}
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700407
408static void timer_stats_account_timer(struct timer_list *timer)
409{
410 unsigned int flag = 0;
411
Heiko Carstens507e1232009-06-23 17:38:15 +0200412 if (likely(!timer->start_site))
413 return;
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700414 if (unlikely(tbase_get_deferrable(timer->base)))
415 flag |= TIMER_STATS_FLAG_DEFERRABLE;
416
417 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
418 timer->function, timer->start_comm, flag);
419}
420
421#else
422static void timer_stats_account_timer(struct timer_list *timer) {}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800423#endif
424
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700425#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
426
427static struct debug_obj_descr timer_debug_descr;
428
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100429static void *timer_debug_hint(void *addr)
430{
431 return ((struct timer_list *) addr)->function;
432}
433
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700434/*
435 * fixup_init is called when:
436 * - an active object is initialized
437 */
438static int timer_fixup_init(void *addr, enum debug_obj_state state)
439{
440 struct timer_list *timer = addr;
441
442 switch (state) {
443 case ODEBUG_STATE_ACTIVE:
444 del_timer_sync(timer);
445 debug_object_init(timer, &timer_debug_descr);
446 return 1;
447 default:
448 return 0;
449 }
450}
451
Stephen Boydfb16b8c2011-11-07 19:48:26 -0800452/* Stub timer callback for improperly used timers. */
453static void stub_timer(unsigned long data)
454{
455 WARN_ON(1);
456}
457
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700458/*
459 * fixup_activate is called when:
460 * - an active object is activated
461 * - an unknown object is activated (might be a statically initialized object)
462 */
463static int timer_fixup_activate(void *addr, enum debug_obj_state state)
464{
465 struct timer_list *timer = addr;
466
467 switch (state) {
468
469 case ODEBUG_STATE_NOTAVAILABLE:
470 /*
471 * This is not really a fixup. The timer was
472 * statically initialized. We just make sure that it
473 * is tracked in the object tracker.
474 */
475 if (timer->entry.next == NULL &&
476 timer->entry.prev == TIMER_ENTRY_STATIC) {
477 debug_object_init(timer, &timer_debug_descr);
478 debug_object_activate(timer, &timer_debug_descr);
479 return 0;
480 } else {
Stephen Boydfb16b8c2011-11-07 19:48:26 -0800481 setup_timer(timer, stub_timer, 0);
482 return 1;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700483 }
484 return 0;
485
486 case ODEBUG_STATE_ACTIVE:
487 WARN_ON(1);
488
489 default:
490 return 0;
491 }
492}
493
494/*
495 * fixup_free is called when:
496 * - an active object is freed
497 */
498static int timer_fixup_free(void *addr, enum debug_obj_state state)
499{
500 struct timer_list *timer = addr;
501
502 switch (state) {
503 case ODEBUG_STATE_ACTIVE:
504 del_timer_sync(timer);
505 debug_object_free(timer, &timer_debug_descr);
506 return 1;
507 default:
508 return 0;
509 }
510}
511
Christine Chandc4218b2011-11-07 19:48:28 -0800512/*
513 * fixup_assert_init is called when:
514 * - an untracked/uninit-ed object is found
515 */
516static int timer_fixup_assert_init(void *addr, enum debug_obj_state state)
517{
518 struct timer_list *timer = addr;
519
520 switch (state) {
521 case ODEBUG_STATE_NOTAVAILABLE:
522 if (timer->entry.prev == TIMER_ENTRY_STATIC) {
523 /*
524 * This is not really a fixup. The timer was
525 * statically initialized. We just make sure that it
526 * is tracked in the object tracker.
527 */
528 debug_object_init(timer, &timer_debug_descr);
529 return 0;
530 } else {
531 setup_timer(timer, stub_timer, 0);
532 return 1;
533 }
534 default:
535 return 0;
536 }
537}
538
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700539static struct debug_obj_descr timer_debug_descr = {
Christine Chandc4218b2011-11-07 19:48:28 -0800540 .name = "timer_list",
541 .debug_hint = timer_debug_hint,
542 .fixup_init = timer_fixup_init,
543 .fixup_activate = timer_fixup_activate,
544 .fixup_free = timer_fixup_free,
545 .fixup_assert_init = timer_fixup_assert_init,
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700546};
547
548static inline void debug_timer_init(struct timer_list *timer)
549{
550 debug_object_init(timer, &timer_debug_descr);
551}
552
553static inline void debug_timer_activate(struct timer_list *timer)
554{
555 debug_object_activate(timer, &timer_debug_descr);
556}
557
558static inline void debug_timer_deactivate(struct timer_list *timer)
559{
560 debug_object_deactivate(timer, &timer_debug_descr);
561}
562
563static inline void debug_timer_free(struct timer_list *timer)
564{
565 debug_object_free(timer, &timer_debug_descr);
566}
567
Christine Chandc4218b2011-11-07 19:48:28 -0800568static inline void debug_timer_assert_init(struct timer_list *timer)
569{
570 debug_object_assert_init(timer, &timer_debug_descr);
571}
572
Tejun Heofc683992012-08-08 11:10:27 -0700573static void do_init_timer(struct timer_list *timer, unsigned int flags,
574 const char *name, struct lock_class_key *key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700575
Tejun Heofc683992012-08-08 11:10:27 -0700576void init_timer_on_stack_key(struct timer_list *timer, unsigned int flags,
577 const char *name, struct lock_class_key *key)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700578{
579 debug_object_init_on_stack(timer, &timer_debug_descr);
Tejun Heofc683992012-08-08 11:10:27 -0700580 do_init_timer(timer, flags, name, key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700581}
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100582EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700583
584void destroy_timer_on_stack(struct timer_list *timer)
585{
586 debug_object_free(timer, &timer_debug_descr);
587}
588EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
589
590#else
591static inline void debug_timer_init(struct timer_list *timer) { }
592static inline void debug_timer_activate(struct timer_list *timer) { }
593static inline void debug_timer_deactivate(struct timer_list *timer) { }
Christine Chandc4218b2011-11-07 19:48:28 -0800594static inline void debug_timer_assert_init(struct timer_list *timer) { }
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700595#endif
596
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800597static inline void debug_init(struct timer_list *timer)
598{
599 debug_timer_init(timer);
600 trace_timer_init(timer);
601}
602
603static inline void
604debug_activate(struct timer_list *timer, unsigned long expires)
605{
606 debug_timer_activate(timer);
607 trace_timer_start(timer, expires);
608}
609
610static inline void debug_deactivate(struct timer_list *timer)
611{
612 debug_timer_deactivate(timer);
613 trace_timer_cancel(timer);
614}
615
Christine Chandc4218b2011-11-07 19:48:28 -0800616static inline void debug_assert_init(struct timer_list *timer)
617{
618 debug_timer_assert_init(timer);
619}
620
Tejun Heofc683992012-08-08 11:10:27 -0700621static void do_init_timer(struct timer_list *timer, unsigned int flags,
622 const char *name, struct lock_class_key *key)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700623{
Tejun Heofc683992012-08-08 11:10:27 -0700624 struct tvec_base *base = __raw_get_cpu_var(tvec_bases);
625
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700626 timer->entry.next = NULL;
Tejun Heofc683992012-08-08 11:10:27 -0700627 timer->base = (void *)((unsigned long)base | flags);
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800628 timer->slack = -1;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700629#ifdef CONFIG_TIMER_STATS
630 timer->start_site = NULL;
631 timer->start_pid = -1;
632 memset(timer->start_comm, 0, TASK_COMM_LEN);
633#endif
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100634 lockdep_init_map(&timer->lockdep_map, name, key, 0);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700635}
636
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700637/**
Randy Dunlap633fe792009-04-01 17:47:23 -0700638 * init_timer_key - initialize a timer
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700639 * @timer: the timer to be initialized
Tejun Heofc683992012-08-08 11:10:27 -0700640 * @flags: timer flags
Randy Dunlap633fe792009-04-01 17:47:23 -0700641 * @name: name of the timer
642 * @key: lockdep class key of the fake lock used for tracking timer
643 * sync lock dependencies
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700644 *
Randy Dunlap633fe792009-04-01 17:47:23 -0700645 * init_timer_key() must be done to a timer prior calling *any* of the
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700646 * other timer functions.
647 */
Tejun Heofc683992012-08-08 11:10:27 -0700648void init_timer_key(struct timer_list *timer, unsigned int flags,
649 const char *name, struct lock_class_key *key)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700650{
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800651 debug_init(timer);
Tejun Heofc683992012-08-08 11:10:27 -0700652 do_init_timer(timer, flags, name, key);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700653}
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100654EXPORT_SYMBOL(init_timer_key);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700655
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000656static inline void detach_timer(struct timer_list *timer, bool clear_pending)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700657{
658 struct list_head *entry = &timer->entry;
659
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800660 debug_deactivate(timer);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700661
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700662 __list_del(entry->prev, entry->next);
663 if (clear_pending)
664 entry->next = NULL;
665 entry->prev = LIST_POISON2;
666}
667
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000668static inline void
669detach_expired_timer(struct timer_list *timer, struct tvec_base *base)
670{
671 detach_timer(timer, true);
672 if (!tbase_get_deferrable(timer->base))
Tejun Heoe52b1db2012-08-08 11:10:25 -0700673 base->active_timers--;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000674}
675
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000676static int detach_if_pending(struct timer_list *timer, struct tvec_base *base,
677 bool clear_pending)
678{
679 if (!timer_pending(timer))
680 return 0;
681
682 detach_timer(timer, clear_pending);
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000683 if (!tbase_get_deferrable(timer->base)) {
Tejun Heoe52b1db2012-08-08 11:10:25 -0700684 base->active_timers--;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000685 if (timer->expires == base->next_timer)
686 base->next_timer = base->timer_jiffies;
687 }
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000688 return 1;
689}
690
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700691/*
Oleg Nesterov3691c512006-03-31 02:30:30 -0800692 * We are using hashed locking: holding per_cpu(tvec_bases).lock
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700693 * means that all timers which are tied to this base via timer->base are
694 * locked, and the base itself is locked too.
695 *
696 * So __run_timers/migrate_timers can safely modify all timers which could
697 * be found on ->tvX lists.
698 *
699 * When the timer's base is locked, and the timer removed from list, it is
700 * possible to set timer->base = NULL and drop the lock: the timer remains
701 * locked.
702 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100703static struct tvec_base *lock_timer_base(struct timer_list *timer,
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700704 unsigned long *flags)
Josh Triplett89e7e3742006-09-29 01:59:36 -0700705 __acquires(timer->base->lock)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700706{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100707 struct tvec_base *base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700708
709 for (;;) {
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100710 struct tvec_base *prelock_base = timer->base;
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700711 base = tbase_get_base(prelock_base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700712 if (likely(base != NULL)) {
713 spin_lock_irqsave(&base->lock, *flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700714 if (likely(prelock_base == timer->base))
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700715 return base;
716 /* The timer has migrated to another CPU */
717 spin_unlock_irqrestore(&base->lock, *flags);
718 }
719 cpu_relax();
720 }
721}
722
Ingo Molnar74019222009-02-18 12:23:29 +0100723static inline int
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530724__mod_timer(struct timer_list *timer, unsigned long expires,
725 bool pending_only, int pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100727 struct tvec_base *base, *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned long flags;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530729 int ret = 0 , cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800731 timer_stats_timer_set_start_info(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 BUG_ON(!timer->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700734 base = lock_timer_base(timer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000736 ret = detach_if_pending(timer, base, false);
737 if (!ret && pending_only)
738 goto out_unlock;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700739
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800740 debug_activate(timer, expires);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700741
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530742 cpu = smp_processor_id();
743
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200744#if defined(CONFIG_NO_HZ_COMMON) && defined(CONFIG_SMP)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700745 if (!pinned && get_sysctl_timer_migration() && idle_cpu(cpu))
746 cpu = get_nohz_timer_target();
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530747#endif
748 new_base = per_cpu(tvec_bases, cpu);
749
Oleg Nesterov3691c512006-03-31 02:30:30 -0800750 if (base != new_base) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700751 /*
752 * We are trying to schedule the timer on the local CPU.
753 * However we can't change timer's base while it is running,
754 * otherwise del_timer_sync() can't detect that the timer's
755 * handler yet has not finished. This also guarantees that
756 * the timer is serialized wrt itself.
757 */
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800758 if (likely(base->running_timer != timer)) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700759 /* See the comment in lock_timer_base() */
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700760 timer_set_base(timer, NULL);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700761 spin_unlock(&base->lock);
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800762 base = new_base;
763 spin_lock(&base->lock);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700764 timer_set_base(timer, base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700765 }
766 }
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 timer->expires = expires;
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800769 internal_add_timer(base, timer);
Ingo Molnar74019222009-02-18 12:23:29 +0100770
771out_unlock:
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800772 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 return ret;
775}
776
Ingo Molnar74019222009-02-18 12:23:29 +0100777/**
778 * mod_timer_pending - modify a pending timer's timeout
779 * @timer: the pending timer to be modified
780 * @expires: new timeout in jiffies
781 *
782 * mod_timer_pending() is the same for pending timers as mod_timer(),
783 * but will not re-activate and modify already deleted timers.
784 *
785 * It is useful for unserialized use of timers.
786 */
787int mod_timer_pending(struct timer_list *timer, unsigned long expires)
788{
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530789 return __mod_timer(timer, expires, true, TIMER_NOT_PINNED);
Ingo Molnar74019222009-02-18 12:23:29 +0100790}
791EXPORT_SYMBOL(mod_timer_pending);
792
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800793/*
794 * Decide where to put the timer while taking the slack into account
795 *
796 * Algorithm:
797 * 1) calculate the maximum (absolute) time
798 * 2) calculate the highest bit where the expires and new max are different
799 * 3) use this bit to make a mask
800 * 4) use the bitmask to round down the maximum time, so that all last
801 * bits are zeros
802 */
803static inline
804unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
805{
806 unsigned long expires_limit, mask;
807 int bit;
808
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200809 if (timer->slack >= 0) {
Jeff Chuaf00e0472010-05-24 07:16:24 +0800810 expires_limit = expires + timer->slack;
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200811 } else {
Sebastian Andrzej Siewior1c3cc112011-05-21 12:58:28 +0200812 long delta = expires - jiffies;
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800813
Sebastian Andrzej Siewior1c3cc112011-05-21 12:58:28 +0200814 if (delta < 256)
815 return expires;
816
817 expires_limit = expires + delta / 256;
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200818 }
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800819 mask = expires ^ expires_limit;
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800820 if (mask == 0)
821 return expires;
822
823 bit = find_last_bit(&mask, BITS_PER_LONG);
824
825 mask = (1 << bit) - 1;
826
827 expires_limit = expires_limit & ~(mask);
828
829 return expires_limit;
830}
831
Ingo Molnar74019222009-02-18 12:23:29 +0100832/**
833 * mod_timer - modify a timer's timeout
834 * @timer: the timer to be modified
835 * @expires: new timeout in jiffies
836 *
837 * mod_timer() is a more efficient way to update the expire field of an
838 * active timer (if the timer is inactive it will be activated)
839 *
840 * mod_timer(timer, expires) is equivalent to:
841 *
842 * del_timer(timer); timer->expires = expires; add_timer(timer);
843 *
844 * Note that if there are multiple unserialized concurrent users of the
845 * same timer, then mod_timer() is the only safe way to modify the timeout,
846 * since add_timer() cannot modify an already running timer.
847 *
848 * The function returns whether it has modified a pending timer or not.
849 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
850 * active timer returns 1.)
851 */
852int mod_timer(struct timer_list *timer, unsigned long expires)
853{
Sebastian Andrzej Siewior1c3cc112011-05-21 12:58:28 +0200854 expires = apply_slack(timer, expires);
855
Ingo Molnar74019222009-02-18 12:23:29 +0100856 /*
857 * This is a common optimization triggered by the
858 * networking code - if the timer is re-modified
859 * to be the same thing then just return:
860 */
Pavel Roskin48411582009-07-18 16:46:02 -0400861 if (timer_pending(timer) && timer->expires == expires)
Ingo Molnar74019222009-02-18 12:23:29 +0100862 return 1;
863
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530864 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
Ingo Molnar74019222009-02-18 12:23:29 +0100865}
866EXPORT_SYMBOL(mod_timer);
867
868/**
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530869 * mod_timer_pinned - modify a timer's timeout
870 * @timer: the timer to be modified
871 * @expires: new timeout in jiffies
872 *
873 * mod_timer_pinned() is a way to update the expire field of an
874 * active timer (if the timer is inactive it will be activated)
Paul E. McKenney048a0e82012-04-26 10:52:27 -0700875 * and to ensure that the timer is scheduled on the current CPU.
876 *
877 * Note that this does not prevent the timer from being migrated
878 * when the current CPU goes offline. If this is a problem for
879 * you, use CPU-hotplug notifiers to handle it correctly, for
880 * example, cancelling the timer when the corresponding CPU goes
881 * offline.
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530882 *
883 * mod_timer_pinned(timer, expires) is equivalent to:
884 *
885 * del_timer(timer); timer->expires = expires; add_timer(timer);
886 */
887int mod_timer_pinned(struct timer_list *timer, unsigned long expires)
888{
889 if (timer->expires == expires && timer_pending(timer))
890 return 1;
891
892 return __mod_timer(timer, expires, false, TIMER_PINNED);
893}
894EXPORT_SYMBOL(mod_timer_pinned);
895
896/**
Ingo Molnar74019222009-02-18 12:23:29 +0100897 * add_timer - start a timer
898 * @timer: the timer to be added
899 *
900 * The kernel will do a ->function(->data) callback from the
901 * timer interrupt at the ->expires point in the future. The
902 * current time is 'jiffies'.
903 *
904 * The timer's ->expires, ->function (and if the handler uses it, ->data)
905 * fields must be set prior calling this function.
906 *
907 * Timers with an ->expires field in the past will be executed in the next
908 * timer tick.
909 */
910void add_timer(struct timer_list *timer)
911{
912 BUG_ON(timer_pending(timer));
913 mod_timer(timer, timer->expires);
914}
915EXPORT_SYMBOL(add_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700917/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * add_timer_on - start a timer on a particular CPU
919 * @timer: the timer to be added
920 * @cpu: the CPU to start it on
921 *
922 * This is not very scalable on SMP. Double adds are not possible.
923 */
924void add_timer_on(struct timer_list *timer, int cpu)
925{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100926 struct tvec_base *base = per_cpu(tvec_bases, cpu);
Thomas Gleixner68194572007-07-19 01:49:16 -0700927 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700928
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800929 timer_stats_timer_set_start_info(timer);
Thomas Gleixner68194572007-07-19 01:49:16 -0700930 BUG_ON(timer_pending(timer) || !timer->function);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800931 spin_lock_irqsave(&base->lock, flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700932 timer_set_base(timer, base);
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800933 debug_activate(timer, timer->expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 internal_add_timer(base, timer);
Thomas Gleixner06d83082008-03-22 09:20:24 +0100935 /*
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200936 * Check whether the other CPU is in dynticks mode and needs
937 * to be triggered to reevaluate the timer wheel.
938 * We are protected against the other CPU fiddling
Thomas Gleixner06d83082008-03-22 09:20:24 +0100939 * with the timer by holding the timer base lock. This also
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200940 * makes sure that a CPU on the way to stop its tick can not
941 * evaluate the timer wheel.
Viresh Kumar8ba14652014-02-10 17:09:54 +0100942 *
943 * Spare the IPI for deferrable timers on idle targets though.
944 * The next busy ticks will take care of it. Except full dynticks
945 * require special care against races with idle_cpu(), lets deal
946 * with that later.
Thomas Gleixner06d83082008-03-22 09:20:24 +0100947 */
Viresh Kumar8ba14652014-02-10 17:09:54 +0100948 if (!tbase_get_deferrable(timer->base) || tick_nohz_full_cpu(cpu))
949 wake_up_nohz_cpu(cpu);
950
Oleg Nesterov3691c512006-03-31 02:30:30 -0800951 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
Andi Kleena9862e02009-05-19 22:49:07 +0200953EXPORT_SYMBOL_GPL(add_timer_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700955/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 * del_timer - deactive a timer.
957 * @timer: the timer to be deactivated
958 *
959 * del_timer() deactivates a timer - this works on both active and inactive
960 * timers.
961 *
962 * The function returns whether it has deactivated a pending timer or not.
963 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
964 * active timer returns 1.)
965 */
966int del_timer(struct timer_list *timer)
967{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100968 struct tvec_base *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700970 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Christine Chandc4218b2011-11-07 19:48:28 -0800972 debug_assert_init(timer);
973
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800974 timer_stats_timer_clear_start_info(timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700975 if (timer_pending(timer)) {
976 base = lock_timer_base(timer, &flags);
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000977 ret = detach_if_pending(timer, base, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700981 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983EXPORT_SYMBOL(del_timer);
984
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700985/**
986 * try_to_del_timer_sync - Try to deactivate a timer
987 * @timer: timer do del
988 *
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700989 * This function tries to deactivate a timer. Upon successful (ret >= 0)
990 * exit the timer is not queued and the handler is not running on any CPU.
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700991 */
992int try_to_del_timer_sync(struct timer_list *timer)
993{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100994 struct tvec_base *base;
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700995 unsigned long flags;
996 int ret = -1;
997
Christine Chandc4218b2011-11-07 19:48:28 -0800998 debug_assert_init(timer);
999
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001000 base = lock_timer_base(timer, &flags);
1001
Thomas Gleixnerec44bc72012-05-25 22:08:57 +00001002 if (base->running_timer != timer) {
1003 timer_stats_timer_clear_start_info(timer);
1004 ret = detach_if_pending(timer, base, true);
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001005 }
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001006 spin_unlock_irqrestore(&base->lock, flags);
1007
1008 return ret;
1009}
David Howellse19dff12007-04-26 15:46:56 -07001010EXPORT_SYMBOL(try_to_del_timer_sync);
1011
Yong Zhang6f1bc452010-10-20 15:57:31 -07001012#ifdef CONFIG_SMP
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001013/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 * del_timer_sync - deactivate a timer and wait for the handler to finish.
1015 * @timer: the timer to be deactivated
1016 *
1017 * This function only differs from del_timer() on SMP: besides deactivating
1018 * the timer it also makes sure the handler has finished executing on other
1019 * CPUs.
1020 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001021 * Synchronization rules: Callers must prevent restarting of the timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 * otherwise this function is meaningless. It must not be called from
Tejun Heoc5f66e92012-08-08 11:10:28 -07001023 * interrupt contexts unless the timer is an irqsafe one. The caller must
1024 * not hold locks which would prevent completion of the timer's
1025 * handler. The timer's handler must not call add_timer_on(). Upon exit the
1026 * timer is not queued and the handler is not running on any CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 *
Tejun Heoc5f66e92012-08-08 11:10:28 -07001028 * Note: For !irqsafe timers, you must not hold locks that are held in
1029 * interrupt context while calling this function. Even if the lock has
1030 * nothing to do with the timer in question. Here's why:
Steven Rostedt48228f72011-02-08 12:39:54 -05001031 *
1032 * CPU0 CPU1
1033 * ---- ----
1034 * <SOFTIRQ>
1035 * call_timer_fn();
1036 * base->running_timer = mytimer;
1037 * spin_lock_irq(somelock);
1038 * <IRQ>
1039 * spin_lock(somelock);
1040 * del_timer_sync(mytimer);
1041 * while (base->running_timer == mytimer);
1042 *
1043 * Now del_timer_sync() will never return and never release somelock.
1044 * The interrupt on the other CPU is waiting to grab somelock but
1045 * it has interrupted the softirq that CPU0 is waiting to finish.
1046 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * The function returns whether it has deactivated a pending timer or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
1049int del_timer_sync(struct timer_list *timer)
1050{
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001051#ifdef CONFIG_LOCKDEP
Peter Zijlstraf266a512011-02-03 15:09:41 +01001052 unsigned long flags;
1053
Steven Rostedt48228f72011-02-08 12:39:54 -05001054 /*
1055 * If lockdep gives a backtrace here, please reference
1056 * the synchronization rules above.
1057 */
Peter Zijlstra7ff20792011-02-08 15:18:00 +01001058 local_irq_save(flags);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001059 lock_map_acquire(&timer->lockdep_map);
1060 lock_map_release(&timer->lockdep_map);
Peter Zijlstra7ff20792011-02-08 15:18:00 +01001061 local_irq_restore(flags);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001062#endif
Yong Zhang466bd302010-10-20 15:57:33 -07001063 /*
1064 * don't use it in hardirq context, because it
1065 * could lead to deadlock.
1066 */
Tejun Heoc5f66e92012-08-08 11:10:28 -07001067 WARN_ON(in_irq() && !tbase_get_irqsafe(timer->base));
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001068 for (;;) {
1069 int ret = try_to_del_timer_sync(timer);
1070 if (ret >= 0)
1071 return ret;
Andrew Mortona0009652006-07-14 00:24:06 -07001072 cpu_relax();
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075EXPORT_SYMBOL(del_timer_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076#endif
1077
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001078static int cascade(struct tvec_base *base, struct tvec *tv, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 /* cascade all the timers from tv up one level */
Porpoise3439dd82006-06-23 02:05:56 -07001081 struct timer_list *timer, *tmp;
1082 struct list_head tv_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Porpoise3439dd82006-06-23 02:05:56 -07001084 list_replace_init(tv->vec + index, &tv_list);
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /*
Porpoise3439dd82006-06-23 02:05:56 -07001087 * We are removing _all_ timers from the list, so we
1088 * don't have to detach them individually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 */
Porpoise3439dd82006-06-23 02:05:56 -07001090 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001091 BUG_ON(tbase_get_base(timer->base) != base);
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +00001092 /* No accounting, while moving them */
1093 __internal_add_timer(base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 return index;
1097}
1098
Thomas Gleixner576da122010-03-12 21:10:29 +01001099static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
1100 unsigned long data)
1101{
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001102 int count = preempt_count();
Thomas Gleixner576da122010-03-12 21:10:29 +01001103
1104#ifdef CONFIG_LOCKDEP
1105 /*
1106 * It is permissible to free the timer from inside the
1107 * function that is called from it, this we need to take into
1108 * account for lockdep too. To avoid bogus "held lock freed"
1109 * warnings as well as problems when looking into
1110 * timer->lockdep_map, make a copy and use that here.
1111 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001112 struct lockdep_map lockdep_map;
1113
1114 lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
Thomas Gleixner576da122010-03-12 21:10:29 +01001115#endif
1116 /*
1117 * Couple the lock chain with the lock chain at
1118 * del_timer_sync() by acquiring the lock_map around the fn()
1119 * call here and in del_timer_sync().
1120 */
1121 lock_map_acquire(&lockdep_map);
1122
1123 trace_timer_expire_entry(timer);
1124 fn(data);
1125 trace_timer_expire_exit(timer);
1126
1127 lock_map_release(&lockdep_map);
1128
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001129 if (count != preempt_count()) {
Thomas Gleixner802702e2010-03-12 20:13:23 +01001130 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001131 fn, count, preempt_count());
Thomas Gleixner802702e2010-03-12 20:13:23 +01001132 /*
1133 * Restore the preempt count. That gives us a decent
1134 * chance to survive and extract information. If the
1135 * callback kept a lock held, bad luck, but not worse
1136 * than the BUG() we had.
1137 */
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001138 preempt_count_set(count);
Thomas Gleixner576da122010-03-12 21:10:29 +01001139 }
1140}
1141
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001142#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
1143
1144/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 * __run_timers - run all expired timers (if any) on this CPU.
1146 * @base: the timer vector to be processed.
1147 *
1148 * This function cascades all vectors and executes all expired timer
1149 * vectors.
1150 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001151static inline void __run_timers(struct tvec_base *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 struct timer_list *timer;
1154
Oleg Nesterov3691c512006-03-31 02:30:30 -08001155 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 while (time_after_eq(jiffies, base->timer_jiffies)) {
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001157 struct list_head work_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct list_head *head = &work_list;
Thomas Gleixner68194572007-07-19 01:49:16 -07001159 int index = base->timer_jiffies & TVR_MASK;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /*
1162 * Cascade timers:
1163 */
1164 if (!index &&
1165 (!cascade(base, &base->tv2, INDEX(0))) &&
1166 (!cascade(base, &base->tv3, INDEX(1))) &&
1167 !cascade(base, &base->tv4, INDEX(2)))
1168 cascade(base, &base->tv5, INDEX(3));
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001169 ++base->timer_jiffies;
1170 list_replace_init(base->tv1.vec + index, &work_list);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001171 while (!list_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 void (*fn)(unsigned long);
1173 unsigned long data;
Tejun Heoc5f66e92012-08-08 11:10:28 -07001174 bool irqsafe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001176 timer = list_first_entry(head, struct timer_list,entry);
Thomas Gleixner68194572007-07-19 01:49:16 -07001177 fn = timer->function;
1178 data = timer->data;
Tejun Heoc5f66e92012-08-08 11:10:28 -07001179 irqsafe = tbase_get_irqsafe(timer->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001181 timer_stats_account_timer(timer);
1182
Yong Zhang6f1bc452010-10-20 15:57:31 -07001183 base->running_timer = timer;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +00001184 detach_expired_timer(timer, base);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001185
Tejun Heoc5f66e92012-08-08 11:10:28 -07001186 if (irqsafe) {
1187 spin_unlock(&base->lock);
1188 call_timer_fn(timer, fn, data);
1189 spin_lock(&base->lock);
1190 } else {
1191 spin_unlock_irq(&base->lock);
1192 call_timer_fn(timer, fn, data);
1193 spin_lock_irq(&base->lock);
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196 }
Yong Zhang6f1bc452010-10-20 15:57:31 -07001197 base->running_timer = NULL;
Oleg Nesterov3691c512006-03-31 02:30:30 -08001198 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001201#ifdef CONFIG_NO_HZ_COMMON
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202/*
1203 * Find out when the next timer event is due to happen. This
Randy Dunlap90cba642009-08-25 14:35:41 -07001204 * is used on S/390 to stop all activity when a CPU is idle.
1205 * This function needs to be called with interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001207static unsigned long __next_timer_interrupt(struct tvec_base *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001209 unsigned long timer_jiffies = base->timer_jiffies;
Thomas Gleixnereaad0842007-05-29 23:47:39 +02001210 unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001211 int index, slot, array, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 struct timer_list *nte;
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001213 struct tvec *varray[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 /* Look for timer events in tv1. */
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001216 index = slot = timer_jiffies & TVR_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001218 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
Thomas Gleixner68194572007-07-19 01:49:16 -07001219 if (tbase_get_deferrable(nte->base))
1220 continue;
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001221
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001222 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001224 /* Look at the cascade bucket(s)? */
1225 if (!index || slot < index)
1226 goto cascade;
1227 return expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001229 slot = (slot + 1) & TVR_MASK;
1230 } while (slot != index);
1231
1232cascade:
1233 /* Calculate the next cascade event */
1234 if (index)
1235 timer_jiffies += TVR_SIZE - index;
1236 timer_jiffies >>= TVR_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 /* Check tv2-tv5. */
1239 varray[0] = &base->tv2;
1240 varray[1] = &base->tv3;
1241 varray[2] = &base->tv4;
1242 varray[3] = &base->tv5;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001243
1244 for (array = 0; array < 4; array++) {
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001245 struct tvec *varp = varray[array];
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001246
1247 index = slot = timer_jiffies & TVN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001249 list_for_each_entry(nte, varp->vec + slot, entry) {
Jon Huntera0419882009-05-01 13:10:23 -07001250 if (tbase_get_deferrable(nte->base))
1251 continue;
1252
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001253 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (time_before(nte->expires, expires))
1255 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001256 }
1257 /*
1258 * Do we still search for the first timer or are
1259 * we looking up the cascade buckets ?
1260 */
1261 if (found) {
1262 /* Look at the cascade bucket(s)? */
1263 if (!index || slot < index)
1264 break;
1265 return expires;
1266 }
1267 slot = (slot + 1) & TVN_MASK;
1268 } while (slot != index);
1269
1270 if (index)
1271 timer_jiffies += TVN_SIZE - index;
1272 timer_jiffies >>= TVN_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001274 return expires;
1275}
1276
1277/*
1278 * Check, if the next hrtimer event is before the next timer wheel
1279 * event:
1280 */
1281static unsigned long cmp_next_hrtimer_event(unsigned long now,
1282 unsigned long expires)
1283{
1284 ktime_t hr_delta = hrtimer_get_next_event();
1285 struct timespec tsdelta;
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001286 unsigned long delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001287
1288 if (hr_delta.tv64 == KTIME_MAX)
1289 return expires;
1290
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001291 /*
1292 * Expired timer available, let it expire in the next tick
1293 */
1294 if (hr_delta.tv64 <= 0)
1295 return now + 1;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001296
1297 tsdelta = ktime_to_timespec(hr_delta);
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001298 delta = timespec_to_jiffies(&tsdelta);
Thomas Gleixnereaad0842007-05-29 23:47:39 +02001299
1300 /*
1301 * Limit the delta to the max value, which is checked in
1302 * tick_nohz_stop_sched_tick():
1303 */
1304 if (delta > NEXT_TIMER_MAX_DELTA)
1305 delta = NEXT_TIMER_MAX_DELTA;
1306
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001307 /*
1308 * Take rounding errors in to account and make sure, that it
1309 * expires in the next tick. Otherwise we go into an endless
1310 * ping pong due to tick_nohz_stop_sched_tick() retriggering
1311 * the timer softirq
1312 */
1313 if (delta < 1)
1314 delta = 1;
1315 now += delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001316 if (time_before(now, expires))
1317 return now;
1318 return expires;
1319}
1320
1321/**
Li Zefan8dce39c2007-11-05 14:51:10 -08001322 * get_next_timer_interrupt - return the jiffy of the next pending timer
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08001323 * @now: current time (in jiffies)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001324 */
Thomas Gleixnerfd064b92007-02-16 01:27:47 -08001325unsigned long get_next_timer_interrupt(unsigned long now)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001326{
Christoph Lameter74963512010-11-30 14:05:53 -06001327 struct tvec_base *base = __this_cpu_read(tvec_bases);
Thomas Gleixnere40468a2012-05-25 22:08:59 +00001328 unsigned long expires = now + NEXT_TIMER_MAX_DELTA;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001329
Heiko Carstensdbd87b52010-12-01 10:11:09 +01001330 /*
1331 * Pretend that there is no timer pending if the cpu is offline.
1332 * Possible pending timers will be migrated later to an active cpu.
1333 */
1334 if (cpu_is_offline(smp_processor_id()))
Thomas Gleixnere40468a2012-05-25 22:08:59 +00001335 return expires;
1336
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001337 spin_lock(&base->lock);
Thomas Gleixnere40468a2012-05-25 22:08:59 +00001338 if (base->active_timers) {
1339 if (time_before_eq(base->next_timer, base->timer_jiffies))
1340 base->next_timer = __next_timer_interrupt(base);
1341 expires = base->next_timer;
1342 }
Oleg Nesterov3691c512006-03-31 02:30:30 -08001343 spin_unlock(&base->lock);
Tony Lindgren69239742006-03-06 15:42:45 -08001344
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001345 if (time_before_eq(expires, now))
1346 return now;
Zachary Amsden0662b712006-05-20 15:00:24 -07001347
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001348 return cmp_next_hrtimer_event(now, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350#endif
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352/*
Daniel Walker5b4db0c2007-10-18 03:06:11 -07001353 * Called from the timer interrupt handler to charge one tick to the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 * process. user_tick is 1 if the tick is user time, 0 for system.
1355 */
1356void update_process_times(int user_tick)
1357{
1358 struct task_struct *p = current;
1359 int cpu = smp_processor_id();
1360
1361 /* Note: this timer irq context must be accounted for as well. */
Paul Mackerrasfa13a5a2007-11-09 22:39:38 +01001362 account_process_tick(p, user_tick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 run_local_timers();
Paul E. McKenneya1572292009-08-22 13:56:51 -07001364 rcu_check_callbacks(cpu, user_tick);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08001365#ifdef CONFIG_IRQ_WORK
1366 if (in_irq())
1367 irq_work_run();
1368#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 scheduler_tick();
Thomas Gleixner68194572007-07-19 01:49:16 -07001370 run_posix_cpu_timers(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
1373/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 * This function runs timers and the timer-tq in bottom half context.
1375 */
1376static void run_timer_softirq(struct softirq_action *h)
1377{
Christoph Lameter74963512010-11-30 14:05:53 -06001378 struct tvec_base *base = __this_cpu_read(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001380 hrtimer_run_pending();
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (time_after_eq(jiffies, base->timer_jiffies))
1383 __run_timers(base);
1384}
1385
1386/*
1387 * Called by the local, per-CPU timer interrupt on SMP.
1388 */
1389void run_local_timers(void)
1390{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001391 hrtimer_run_queues();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 raise_softirq(TIMER_SOFTIRQ);
1393}
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395#ifdef __ARCH_WANT_SYS_ALARM
1396
1397/*
1398 * For backwards compatibility? This can be done in libc so Alpha
1399 * and all newer ports shouldn't need it.
1400 */
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001401SYSCALL_DEFINE1(alarm, unsigned int, seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -08001403 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
1406#endif
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408static void process_timeout(unsigned long __data)
1409{
Ingo Molnar36c8b582006-07-03 00:25:41 -07001410 wake_up_process((struct task_struct *)__data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
1413/**
1414 * schedule_timeout - sleep until timeout
1415 * @timeout: timeout value in jiffies
1416 *
1417 * Make the current task sleep until @timeout jiffies have
1418 * elapsed. The routine will return immediately unless
1419 * the current task state has been set (see set_current_state()).
1420 *
1421 * You can set the task state as follows -
1422 *
1423 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1424 * pass before the routine returns. The routine will return 0
1425 *
1426 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1427 * delivered to the current task. In this case the remaining time
1428 * in jiffies will be returned, or 0 if the timer expired in time
1429 *
1430 * The current task state is guaranteed to be TASK_RUNNING when this
1431 * routine returns.
1432 *
1433 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1434 * the CPU away without a bound on the timeout. In this case the return
1435 * value will be %MAX_SCHEDULE_TIMEOUT.
1436 *
1437 * In all cases the return value is guaranteed to be non-negative.
1438 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001439signed long __sched schedule_timeout(signed long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 struct timer_list timer;
1442 unsigned long expire;
1443
1444 switch (timeout)
1445 {
1446 case MAX_SCHEDULE_TIMEOUT:
1447 /*
1448 * These two special cases are useful to be comfortable
1449 * in the caller. Nothing more. We could take
1450 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1451 * but I' d like to return a valid offset (>=0) to allow
1452 * the caller to do everything it want with the retval.
1453 */
1454 schedule();
1455 goto out;
1456 default:
1457 /*
1458 * Another bit of PARANOID. Note that the retval will be
1459 * 0 since no piece of kernel is supposed to do a check
1460 * for a negative retval of schedule_timeout() (since it
1461 * should never happens anyway). You just have the printk()
1462 * that will tell you if something is gone wrong and where.
1463 */
Andrew Morton5b149bc2006-12-22 01:10:14 -08001464 if (timeout < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 printk(KERN_ERR "schedule_timeout: wrong timeout "
Andrew Morton5b149bc2006-12-22 01:10:14 -08001466 "value %lx\n", timeout);
1467 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 current->state = TASK_RUNNING;
1469 goto out;
1470 }
1471 }
1472
1473 expire = timeout + jiffies;
1474
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001475 setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
Arun R Bharadwaj597d0272009-04-16 12:13:26 +05301476 __mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 schedule();
1478 del_singleshot_timer_sync(&timer);
1479
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001480 /* Remove the timer from the object tracker */
1481 destroy_timer_on_stack(&timer);
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 timeout = expire - jiffies;
1484
1485 out:
1486 return timeout < 0 ? 0 : timeout;
1487}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488EXPORT_SYMBOL(schedule_timeout);
1489
Andrew Morton8a1c1752005-09-13 01:25:15 -07001490/*
1491 * We can use __set_current_state() here because schedule_timeout() calls
1492 * schedule() unconditionally.
1493 */
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001494signed long __sched schedule_timeout_interruptible(signed long timeout)
1495{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001496 __set_current_state(TASK_INTERRUPTIBLE);
1497 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001498}
1499EXPORT_SYMBOL(schedule_timeout_interruptible);
1500
Matthew Wilcox294d5cc2007-12-06 11:59:46 -05001501signed long __sched schedule_timeout_killable(signed long timeout)
1502{
1503 __set_current_state(TASK_KILLABLE);
1504 return schedule_timeout(timeout);
1505}
1506EXPORT_SYMBOL(schedule_timeout_killable);
1507
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001508signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1509{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001510 __set_current_state(TASK_UNINTERRUPTIBLE);
1511 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001512}
1513EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1514
Paul Gortmaker0db06282013-06-19 14:53:51 -04001515static int init_timers_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 int j;
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001518 struct tvec_base *base;
Paul Gortmaker0db06282013-06-19 14:53:51 -04001519 static char tvec_base_done[NR_CPUS];
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001520
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001521 if (!tvec_base_done[cpu]) {
Jan Beulicha4a61982006-03-24 03:15:54 -08001522 static char boot_done;
1523
Jan Beulicha4a61982006-03-24 03:15:54 -08001524 if (boot_done) {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001525 /*
1526 * The APs use this path later in boot
1527 */
Joe Perchesda554eb2013-11-15 14:15:31 -08001528 base = kzalloc_node(sizeof(*base), GFP_KERNEL,
1529 cpu_to_node(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001530 if (!base)
1531 return -ENOMEM;
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001532
1533 /* Make sure that tvec_base is 2 byte aligned */
1534 if (tbase_get_deferrable(base)) {
1535 WARN_ON(1);
1536 kfree(base);
1537 return -ENOMEM;
1538 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001539 per_cpu(tvec_bases, cpu) = base;
Jan Beulicha4a61982006-03-24 03:15:54 -08001540 } else {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001541 /*
1542 * This is for the boot CPU - we use compile-time
1543 * static initialisation because per-cpu memory isn't
1544 * ready yet and because the memory allocators are not
1545 * initialised either.
1546 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001547 boot_done = 1;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001548 base = &boot_tvec_bases;
Jan Beulicha4a61982006-03-24 03:15:54 -08001549 }
Tirupathi Reddy42a5cf42013-05-14 13:59:02 +05301550 spin_lock_init(&base->lock);
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001551 tvec_base_done[cpu] = 1;
1552 } else {
1553 base = per_cpu(tvec_bases, cpu);
Jan Beulicha4a61982006-03-24 03:15:54 -08001554 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001555
Ingo Molnard730e882006-07-03 00:25:10 -07001556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 for (j = 0; j < TVN_SIZE; j++) {
1558 INIT_LIST_HEAD(base->tv5.vec + j);
1559 INIT_LIST_HEAD(base->tv4.vec + j);
1560 INIT_LIST_HEAD(base->tv3.vec + j);
1561 INIT_LIST_HEAD(base->tv2.vec + j);
1562 }
1563 for (j = 0; j < TVR_SIZE; j++)
1564 INIT_LIST_HEAD(base->tv1.vec + j);
1565
1566 base->timer_jiffies = jiffies;
Martin Schwidefsky97fd9ed2009-07-21 20:25:05 +02001567 base->next_timer = base->timer_jiffies;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +00001568 base->active_timers = 0;
Jan Beulicha4a61982006-03-24 03:15:54 -08001569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
1572#ifdef CONFIG_HOTPLUG_CPU
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001573static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 struct timer_list *timer;
1576
1577 while (!list_empty(head)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001578 timer = list_first_entry(head, struct timer_list, entry);
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +00001579 /* We ignore the accounting on the dying cpu */
Thomas Gleixnerec44bc72012-05-25 22:08:57 +00001580 detach_timer(timer, false);
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001581 timer_set_base(timer, new_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 internal_add_timer(new_base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
Paul Gortmaker0db06282013-06-19 14:53:51 -04001586static void migrate_timers(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001588 struct tvec_base *old_base;
1589 struct tvec_base *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 int i;
1591
1592 BUG_ON(cpu_online(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001593 old_base = per_cpu(tvec_bases, cpu);
1594 new_base = get_cpu_var(tvec_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001595 /*
1596 * The caller is globally serialized and nobody else
1597 * takes two locks at once, deadlock is not possible.
1598 */
1599 spin_lock_irq(&new_base->lock);
Oleg Nesterov0d180402008-04-04 20:54:10 +02001600 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Oleg Nesterov3691c512006-03-31 02:30:30 -08001602 BUG_ON(old_base->running_timer);
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 for (i = 0; i < TVR_SIZE; i++)
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001605 migrate_timer_list(new_base, old_base->tv1.vec + i);
1606 for (i = 0; i < TVN_SIZE; i++) {
1607 migrate_timer_list(new_base, old_base->tv2.vec + i);
1608 migrate_timer_list(new_base, old_base->tv3.vec + i);
1609 migrate_timer_list(new_base, old_base->tv4.vec + i);
1610 migrate_timer_list(new_base, old_base->tv5.vec + i);
1611 }
1612
Oleg Nesterov0d180402008-04-04 20:54:10 +02001613 spin_unlock(&old_base->lock);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001614 spin_unlock_irq(&new_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 put_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617#endif /* CONFIG_HOTPLUG_CPU */
1618
Paul Gortmaker0db06282013-06-19 14:53:51 -04001619static int timer_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 unsigned long action, void *hcpu)
1621{
1622 long cpu = (long)hcpu;
Akinobu Mita80b51842010-05-26 14:43:32 -07001623 int err;
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 switch(action) {
1626 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001627 case CPU_UP_PREPARE_FROZEN:
Akinobu Mita80b51842010-05-26 14:43:32 -07001628 err = init_timers_cpu(cpu);
1629 if (err < 0)
1630 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 break;
1632#ifdef CONFIG_HOTPLUG_CPU
1633 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001634 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 migrate_timers(cpu);
1636 break;
1637#endif
1638 default:
1639 break;
1640 }
1641 return NOTIFY_OK;
1642}
1643
Paul Gortmaker0db06282013-06-19 14:53:51 -04001644static struct notifier_block timers_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 .notifier_call = timer_cpu_notify,
1646};
1647
1648
1649void __init init_timers(void)
1650{
Tejun Heoe52b1db2012-08-08 11:10:25 -07001651 int err;
Akinobu Mita07dccf32006-09-29 02:00:22 -07001652
Tejun Heoe52b1db2012-08-08 11:10:25 -07001653 /* ensure there are enough low bits for flags in timer->base pointer */
1654 BUILD_BUG_ON(__alignof__(struct tvec_base) & TIMER_FLAG_MASK);
1655
1656 err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1657 (void *)(long)smp_processor_id());
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001658 init_timer_stats();
1659
Akinobu Mita9e506f72010-06-04 14:15:04 -07001660 BUG_ON(err != NOTIFY_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 register_cpu_notifier(&timers_nb);
Carlos R. Mafra962cf362008-05-15 11:15:37 -03001662 open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665/**
1666 * msleep - sleep safely even with waitqueue interruptions
1667 * @msecs: Time in milliseconds to sleep for
1668 */
1669void msleep(unsigned int msecs)
1670{
1671 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1672
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001673 while (timeout)
1674 timeout = schedule_timeout_uninterruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
1677EXPORT_SYMBOL(msleep);
1678
1679/**
Domen Puncer96ec3ef2005-06-25 14:58:43 -07001680 * msleep_interruptible - sleep waiting for signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 * @msecs: Time in milliseconds to sleep for
1682 */
1683unsigned long msleep_interruptible(unsigned int msecs)
1684{
1685 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1686
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001687 while (timeout && !signal_pending(current))
1688 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return jiffies_to_msecs(timeout);
1690}
1691
1692EXPORT_SYMBOL(msleep_interruptible);
Patrick Pannuto5e7f5a12010-08-02 15:01:04 -07001693
1694static int __sched do_usleep_range(unsigned long min, unsigned long max)
1695{
1696 ktime_t kmin;
1697 unsigned long delta;
1698
1699 kmin = ktime_set(0, min * NSEC_PER_USEC);
1700 delta = (max - min) * NSEC_PER_USEC;
1701 return schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL);
1702}
1703
1704/**
1705 * usleep_range - Drop in replacement for udelay where wakeup is flexible
1706 * @min: Minimum time in usecs to sleep
1707 * @max: Maximum time in usecs to sleep
1708 */
1709void usleep_range(unsigned long min, unsigned long max)
1710{
1711 __set_current_state(TASK_UNINTERRUPTIBLE);
1712 do_usleep_range(min, max);
1713}
1714EXPORT_SYMBOL(usleep_range);