blob: 0c638cf3d9d21ce066bc0873431fd6d11063cc21 [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;
Paul E. McKenneyfff42152014-01-14 20:20:43 -080084 unsigned long all_timers;
Pavel Macheka6fa8e52008-01-30 13:30:00 +010085 struct tvec_root tv1;
86 struct tvec tv2;
87 struct tvec tv3;
88 struct tvec tv4;
89 struct tvec tv5;
Venki Pallipadi6e453a62007-05-08 00:27:44 -070090} ____cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Pavel Macheka6fa8e52008-01-30 13:30:00 +010092struct tvec_base boot_tvec_bases;
Oleg Nesterov3691c512006-03-31 02:30:30 -080093EXPORT_SYMBOL(boot_tvec_bases);
Pavel Macheka6fa8e52008-01-30 13:30:00 +010094static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Venki Pallipadi6e453a62007-05-08 00:27:44 -070096/* Functions below help us manage 'deferrable' flag */
Pavel Macheka6fa8e52008-01-30 13:30:00 +010097static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -070098{
Tejun Heoe52b1db2012-08-08 11:10:25 -070099 return ((unsigned int)(unsigned long)base & TIMER_DEFERRABLE);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700100}
101
Tejun Heoc5f66e92012-08-08 11:10:28 -0700102static inline unsigned int tbase_get_irqsafe(struct tvec_base *base)
103{
104 return ((unsigned int)(unsigned long)base & TIMER_IRQSAFE);
105}
106
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100107static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700108{
Tejun Heoe52b1db2012-08-08 11:10:25 -0700109 return ((struct tvec_base *)((unsigned long)base & ~TIMER_FLAG_MASK));
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700110}
111
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700112static inline void
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100113timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700114{
Tejun Heoe52b1db2012-08-08 11:10:25 -0700115 unsigned long flags = (unsigned long)timer->base & TIMER_FLAG_MASK;
116
117 timer->base = (struct tvec_base *)((unsigned long)(new_base) | flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700118}
119
Alan Stern9c133c42008-11-06 08:42:48 +0100120static unsigned long round_jiffies_common(unsigned long j, int cpu,
121 bool force_up)
122{
123 int rem;
124 unsigned long original = j;
125
126 /*
127 * We don't want all cpus firing their timers at once hitting the
128 * same lock or cachelines, so we skew each extra cpu with an extra
129 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
130 * already did this.
131 * The skew is done by adding 3*cpunr, then round, then subtract this
132 * extra offset again.
133 */
134 j += cpu * 3;
135
136 rem = j % HZ;
137
138 /*
139 * If the target jiffie is just after a whole second (which can happen
140 * due to delays of the timer irq, long irq off times etc etc) then
141 * we should round down to the whole second, not up. Use 1/4th second
142 * as cutoff for this rounding as an extreme upper bound for this.
143 * But never round down if @force_up is set.
144 */
145 if (rem < HZ/4 && !force_up) /* round down */
146 j = j - rem;
147 else /* round up */
148 j = j - rem + HZ;
149
150 /* now that we have rounded, subtract the extra skew again */
151 j -= cpu * 3;
152
Bart Van Assche9e04d382013-05-21 20:43:50 +0200153 /*
154 * Make sure j is still in the future. Otherwise return the
155 * unmodified value.
156 */
157 return time_is_after_jiffies(j) ? j : original;
Alan Stern9c133c42008-11-06 08:42:48 +0100158}
159
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800160/**
161 * __round_jiffies - function to round jiffies to a full second
162 * @j: the time in (absolute) jiffies that should be rounded
163 * @cpu: the processor number on which the timeout will happen
164 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800165 * __round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800166 * up or down to (approximately) full seconds. This is useful for timers
167 * for which the exact time they fire does not matter too much, as long as
168 * they fire approximately every X seconds.
169 *
170 * By rounding these timers to whole seconds, all such timers will fire
171 * at the same time, rather than at various times spread out. The goal
172 * of this is to have the CPU wake up less, which saves power.
173 *
174 * The exact rounding is skewed for each processor to avoid all
175 * processors firing at the exact same time, which could lead
176 * to lock contention or spurious cache line bouncing.
177 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800178 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800179 */
180unsigned long __round_jiffies(unsigned long j, int cpu)
181{
Alan Stern9c133c42008-11-06 08:42:48 +0100182 return round_jiffies_common(j, cpu, false);
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800183}
184EXPORT_SYMBOL_GPL(__round_jiffies);
185
186/**
187 * __round_jiffies_relative - function to round jiffies to a full second
188 * @j: the time in (relative) jiffies that should be rounded
189 * @cpu: the processor number on which the timeout will happen
190 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800191 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800192 * up or down to (approximately) full seconds. This is useful for timers
193 * for which the exact time they fire does not matter too much, as long as
194 * they fire approximately every X seconds.
195 *
196 * By rounding these timers to whole seconds, all such timers will fire
197 * at the same time, rather than at various times spread out. The goal
198 * of this is to have the CPU wake up less, which saves power.
199 *
200 * The exact rounding is skewed for each processor to avoid all
201 * processors firing at the exact same time, which could lead
202 * to lock contention or spurious cache line bouncing.
203 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800204 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800205 */
206unsigned long __round_jiffies_relative(unsigned long j, int cpu)
207{
Alan Stern9c133c42008-11-06 08:42:48 +0100208 unsigned long j0 = jiffies;
209
210 /* Use j0 because jiffies might change while we run */
211 return round_jiffies_common(j + j0, cpu, false) - j0;
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800212}
213EXPORT_SYMBOL_GPL(__round_jiffies_relative);
214
215/**
216 * round_jiffies - function to round jiffies to a full second
217 * @j: the time in (absolute) jiffies that should be rounded
218 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800219 * round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800220 * up or down to (approximately) full seconds. This is useful for timers
221 * for which the exact time they fire does not matter too much, as long as
222 * they fire approximately every X seconds.
223 *
224 * By rounding these timers to whole seconds, all such timers will fire
225 * at the same time, rather than at various times spread out. The goal
226 * of this is to have the CPU wake up less, which saves power.
227 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800228 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800229 */
230unsigned long round_jiffies(unsigned long j)
231{
Alan Stern9c133c42008-11-06 08:42:48 +0100232 return round_jiffies_common(j, raw_smp_processor_id(), false);
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800233}
234EXPORT_SYMBOL_GPL(round_jiffies);
235
236/**
237 * round_jiffies_relative - function to round jiffies to a full second
238 * @j: the time in (relative) jiffies that should be rounded
239 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800240 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800241 * up or down to (approximately) full seconds. This is useful for timers
242 * for which the exact time they fire does not matter too much, as long as
243 * they fire approximately every X seconds.
244 *
245 * By rounding these timers to whole seconds, all such timers will fire
246 * at the same time, rather than at various times spread out. The goal
247 * of this is to have the CPU wake up less, which saves power.
248 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800249 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800250 */
251unsigned long round_jiffies_relative(unsigned long j)
252{
253 return __round_jiffies_relative(j, raw_smp_processor_id());
254}
255EXPORT_SYMBOL_GPL(round_jiffies_relative);
256
Alan Stern9c133c42008-11-06 08:42:48 +0100257/**
258 * __round_jiffies_up - function to round jiffies up to a full second
259 * @j: the time in (absolute) jiffies that should be rounded
260 * @cpu: the processor number on which the timeout will happen
261 *
262 * This is the same as __round_jiffies() except that it will never
263 * round down. This is useful for timeouts for which the exact time
264 * of firing does not matter too much, as long as they don't fire too
265 * early.
266 */
267unsigned long __round_jiffies_up(unsigned long j, int cpu)
268{
269 return round_jiffies_common(j, cpu, true);
270}
271EXPORT_SYMBOL_GPL(__round_jiffies_up);
272
273/**
274 * __round_jiffies_up_relative - function to round jiffies up to a full second
275 * @j: the time in (relative) jiffies that should be rounded
276 * @cpu: the processor number on which the timeout will happen
277 *
278 * This is the same as __round_jiffies_relative() except that it will never
279 * round down. This is useful for timeouts for which the exact time
280 * of firing does not matter too much, as long as they don't fire too
281 * early.
282 */
283unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
284{
285 unsigned long j0 = jiffies;
286
287 /* Use j0 because jiffies might change while we run */
288 return round_jiffies_common(j + j0, cpu, true) - j0;
289}
290EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
291
292/**
293 * round_jiffies_up - function to round jiffies up to a full second
294 * @j: the time in (absolute) jiffies that should be rounded
295 *
296 * This is the same as round_jiffies() except that it will never
297 * round down. This is useful for timeouts for which the exact time
298 * of firing does not matter too much, as long as they don't fire too
299 * early.
300 */
301unsigned long round_jiffies_up(unsigned long j)
302{
303 return round_jiffies_common(j, raw_smp_processor_id(), true);
304}
305EXPORT_SYMBOL_GPL(round_jiffies_up);
306
307/**
308 * round_jiffies_up_relative - function to round jiffies up to a full second
309 * @j: the time in (relative) jiffies that should be rounded
310 *
311 * This is the same as round_jiffies_relative() except that it will never
312 * round down. This is useful for timeouts for which the exact time
313 * of firing does not matter too much, as long as they don't fire too
314 * early.
315 */
316unsigned long round_jiffies_up_relative(unsigned long j)
317{
318 return __round_jiffies_up_relative(j, raw_smp_processor_id());
319}
320EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
321
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800322/**
323 * set_timer_slack - set the allowed slack for a timer
Randy Dunlap0caa6212010-08-09 16:32:50 -0700324 * @timer: the timer to be modified
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800325 * @slack_hz: the amount of time (in jiffies) allowed for rounding
326 *
327 * Set the amount of time, in jiffies, that a certain timer has
328 * in terms of slack. By setting this value, the timer subsystem
329 * will schedule the actual timer somewhere between
330 * the time mod_timer() asks for, and that time plus the slack.
331 *
332 * By setting the slack to -1, a percentage of the delay is used
333 * instead.
334 */
335void set_timer_slack(struct timer_list *timer, int slack_hz)
336{
337 timer->slack = slack_hz;
338}
339EXPORT_SYMBOL_GPL(set_timer_slack);
340
Paul E. McKenneyd550e812013-12-16 05:57:10 -0800341/*
342 * If the list is empty, catch up ->timer_jiffies to the current time.
343 * The caller must hold the tvec_base lock. Returns true if the list
344 * was empty and therefore ->timer_jiffies was updated.
345 */
346static bool catchup_timer_jiffies(struct tvec_base *base)
347{
348 if (!base->all_timers) {
349 base->timer_jiffies = jiffies;
350 return true;
351 }
352 return false;
353}
354
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000355static void
356__internal_add_timer(struct tvec_base *base, struct timer_list *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 unsigned long expires = timer->expires;
359 unsigned long idx = expires - base->timer_jiffies;
360 struct list_head *vec;
361
362 if (idx < TVR_SIZE) {
363 int i = expires & TVR_MASK;
364 vec = base->tv1.vec + i;
365 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
366 int i = (expires >> TVR_BITS) & TVN_MASK;
367 vec = base->tv2.vec + i;
368 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
369 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
370 vec = base->tv3.vec + i;
371 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
372 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
373 vec = base->tv4.vec + i;
374 } else if ((signed long) idx < 0) {
375 /*
376 * Can happen if you add a timer with expires == jiffies,
377 * or you set a timer to go off in the past
378 */
379 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
380 } else {
381 int i;
Hildner, Christian26cff4e2012-10-08 15:49:03 +0200382 /* If the timeout is larger than MAX_TVAL (on 64-bit
383 * architectures or with CONFIG_BASE_SMALL=1) then we
384 * use the maximum timeout.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 */
Hildner, Christian26cff4e2012-10-08 15:49:03 +0200386 if (idx > MAX_TVAL) {
387 idx = MAX_TVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 expires = idx + base->timer_jiffies;
389 }
390 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
391 vec = base->tv5.vec + i;
392 }
393 /*
394 * Timers are FIFO:
395 */
396 list_add_tail(&timer->entry, vec);
397}
398
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000399static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
400{
Paul E. McKenney18d8cb642013-12-16 10:41:50 -0800401 (void)catchup_timer_jiffies(base);
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000402 __internal_add_timer(base, timer);
403 /*
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000404 * Update base->active_timers and base->next_timer
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000405 */
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000406 if (!tbase_get_deferrable(timer->base)) {
407 if (time_before(timer->expires, base->next_timer))
408 base->next_timer = timer->expires;
409 base->active_timers++;
410 }
Paul E. McKenneyfff42152014-01-14 20:20:43 -0800411 base->all_timers++;
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000412}
413
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800414#ifdef CONFIG_TIMER_STATS
415void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
416{
417 if (timer->start_site)
418 return;
419
420 timer->start_site = addr;
421 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
422 timer->start_pid = current->pid;
423}
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700424
425static void timer_stats_account_timer(struct timer_list *timer)
426{
427 unsigned int flag = 0;
428
Heiko Carstens507e1232009-06-23 17:38:15 +0200429 if (likely(!timer->start_site))
430 return;
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700431 if (unlikely(tbase_get_deferrable(timer->base)))
432 flag |= TIMER_STATS_FLAG_DEFERRABLE;
433
434 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
435 timer->function, timer->start_comm, flag);
436}
437
438#else
439static void timer_stats_account_timer(struct timer_list *timer) {}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800440#endif
441
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700442#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
443
444static struct debug_obj_descr timer_debug_descr;
445
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100446static void *timer_debug_hint(void *addr)
447{
448 return ((struct timer_list *) addr)->function;
449}
450
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700451/*
452 * fixup_init is called when:
453 * - an active object is initialized
454 */
455static int timer_fixup_init(void *addr, enum debug_obj_state state)
456{
457 struct timer_list *timer = addr;
458
459 switch (state) {
460 case ODEBUG_STATE_ACTIVE:
461 del_timer_sync(timer);
462 debug_object_init(timer, &timer_debug_descr);
463 return 1;
464 default:
465 return 0;
466 }
467}
468
Stephen Boydfb16b8c2011-11-07 19:48:26 -0800469/* Stub timer callback for improperly used timers. */
470static void stub_timer(unsigned long data)
471{
472 WARN_ON(1);
473}
474
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700475/*
476 * fixup_activate is called when:
477 * - an active object is activated
478 * - an unknown object is activated (might be a statically initialized object)
479 */
480static int timer_fixup_activate(void *addr, enum debug_obj_state state)
481{
482 struct timer_list *timer = addr;
483
484 switch (state) {
485
486 case ODEBUG_STATE_NOTAVAILABLE:
487 /*
488 * This is not really a fixup. The timer was
489 * statically initialized. We just make sure that it
490 * is tracked in the object tracker.
491 */
492 if (timer->entry.next == NULL &&
493 timer->entry.prev == TIMER_ENTRY_STATIC) {
494 debug_object_init(timer, &timer_debug_descr);
495 debug_object_activate(timer, &timer_debug_descr);
496 return 0;
497 } else {
Stephen Boydfb16b8c2011-11-07 19:48:26 -0800498 setup_timer(timer, stub_timer, 0);
499 return 1;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700500 }
501 return 0;
502
503 case ODEBUG_STATE_ACTIVE:
504 WARN_ON(1);
505
506 default:
507 return 0;
508 }
509}
510
511/*
512 * fixup_free is called when:
513 * - an active object is freed
514 */
515static int timer_fixup_free(void *addr, enum debug_obj_state state)
516{
517 struct timer_list *timer = addr;
518
519 switch (state) {
520 case ODEBUG_STATE_ACTIVE:
521 del_timer_sync(timer);
522 debug_object_free(timer, &timer_debug_descr);
523 return 1;
524 default:
525 return 0;
526 }
527}
528
Christine Chandc4218b2011-11-07 19:48:28 -0800529/*
530 * fixup_assert_init is called when:
531 * - an untracked/uninit-ed object is found
532 */
533static int timer_fixup_assert_init(void *addr, enum debug_obj_state state)
534{
535 struct timer_list *timer = addr;
536
537 switch (state) {
538 case ODEBUG_STATE_NOTAVAILABLE:
539 if (timer->entry.prev == TIMER_ENTRY_STATIC) {
540 /*
541 * This is not really a fixup. The timer was
542 * statically initialized. We just make sure that it
543 * is tracked in the object tracker.
544 */
545 debug_object_init(timer, &timer_debug_descr);
546 return 0;
547 } else {
548 setup_timer(timer, stub_timer, 0);
549 return 1;
550 }
551 default:
552 return 0;
553 }
554}
555
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700556static struct debug_obj_descr timer_debug_descr = {
Christine Chandc4218b2011-11-07 19:48:28 -0800557 .name = "timer_list",
558 .debug_hint = timer_debug_hint,
559 .fixup_init = timer_fixup_init,
560 .fixup_activate = timer_fixup_activate,
561 .fixup_free = timer_fixup_free,
562 .fixup_assert_init = timer_fixup_assert_init,
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700563};
564
565static inline void debug_timer_init(struct timer_list *timer)
566{
567 debug_object_init(timer, &timer_debug_descr);
568}
569
570static inline void debug_timer_activate(struct timer_list *timer)
571{
572 debug_object_activate(timer, &timer_debug_descr);
573}
574
575static inline void debug_timer_deactivate(struct timer_list *timer)
576{
577 debug_object_deactivate(timer, &timer_debug_descr);
578}
579
580static inline void debug_timer_free(struct timer_list *timer)
581{
582 debug_object_free(timer, &timer_debug_descr);
583}
584
Christine Chandc4218b2011-11-07 19:48:28 -0800585static inline void debug_timer_assert_init(struct timer_list *timer)
586{
587 debug_object_assert_init(timer, &timer_debug_descr);
588}
589
Tejun Heofc683992012-08-08 11:10:27 -0700590static void do_init_timer(struct timer_list *timer, unsigned int flags,
591 const char *name, struct lock_class_key *key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700592
Tejun Heofc683992012-08-08 11:10:27 -0700593void init_timer_on_stack_key(struct timer_list *timer, unsigned int flags,
594 const char *name, struct lock_class_key *key)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700595{
596 debug_object_init_on_stack(timer, &timer_debug_descr);
Tejun Heofc683992012-08-08 11:10:27 -0700597 do_init_timer(timer, flags, name, key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700598}
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100599EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700600
601void destroy_timer_on_stack(struct timer_list *timer)
602{
603 debug_object_free(timer, &timer_debug_descr);
604}
605EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
606
607#else
608static inline void debug_timer_init(struct timer_list *timer) { }
609static inline void debug_timer_activate(struct timer_list *timer) { }
610static inline void debug_timer_deactivate(struct timer_list *timer) { }
Christine Chandc4218b2011-11-07 19:48:28 -0800611static inline void debug_timer_assert_init(struct timer_list *timer) { }
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700612#endif
613
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800614static inline void debug_init(struct timer_list *timer)
615{
616 debug_timer_init(timer);
617 trace_timer_init(timer);
618}
619
620static inline void
621debug_activate(struct timer_list *timer, unsigned long expires)
622{
623 debug_timer_activate(timer);
624 trace_timer_start(timer, expires);
625}
626
627static inline void debug_deactivate(struct timer_list *timer)
628{
629 debug_timer_deactivate(timer);
630 trace_timer_cancel(timer);
631}
632
Christine Chandc4218b2011-11-07 19:48:28 -0800633static inline void debug_assert_init(struct timer_list *timer)
634{
635 debug_timer_assert_init(timer);
636}
637
Tejun Heofc683992012-08-08 11:10:27 -0700638static void do_init_timer(struct timer_list *timer, unsigned int flags,
639 const char *name, struct lock_class_key *key)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700640{
Tejun Heofc683992012-08-08 11:10:27 -0700641 struct tvec_base *base = __raw_get_cpu_var(tvec_bases);
642
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700643 timer->entry.next = NULL;
Tejun Heofc683992012-08-08 11:10:27 -0700644 timer->base = (void *)((unsigned long)base | flags);
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800645 timer->slack = -1;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700646#ifdef CONFIG_TIMER_STATS
647 timer->start_site = NULL;
648 timer->start_pid = -1;
649 memset(timer->start_comm, 0, TASK_COMM_LEN);
650#endif
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100651 lockdep_init_map(&timer->lockdep_map, name, key, 0);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700652}
653
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700654/**
Randy Dunlap633fe792009-04-01 17:47:23 -0700655 * init_timer_key - initialize a timer
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700656 * @timer: the timer to be initialized
Tejun Heofc683992012-08-08 11:10:27 -0700657 * @flags: timer flags
Randy Dunlap633fe792009-04-01 17:47:23 -0700658 * @name: name of the timer
659 * @key: lockdep class key of the fake lock used for tracking timer
660 * sync lock dependencies
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700661 *
Randy Dunlap633fe792009-04-01 17:47:23 -0700662 * init_timer_key() must be done to a timer prior calling *any* of the
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700663 * other timer functions.
664 */
Tejun Heofc683992012-08-08 11:10:27 -0700665void init_timer_key(struct timer_list *timer, unsigned int flags,
666 const char *name, struct lock_class_key *key)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700667{
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800668 debug_init(timer);
Tejun Heofc683992012-08-08 11:10:27 -0700669 do_init_timer(timer, flags, name, key);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700670}
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100671EXPORT_SYMBOL(init_timer_key);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700672
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000673static inline void detach_timer(struct timer_list *timer, bool clear_pending)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700674{
675 struct list_head *entry = &timer->entry;
676
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800677 debug_deactivate(timer);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700678
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700679 __list_del(entry->prev, entry->next);
680 if (clear_pending)
681 entry->next = NULL;
682 entry->prev = LIST_POISON2;
683}
684
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000685static inline void
686detach_expired_timer(struct timer_list *timer, struct tvec_base *base)
687{
688 detach_timer(timer, true);
689 if (!tbase_get_deferrable(timer->base))
Tejun Heoe52b1db2012-08-08 11:10:25 -0700690 base->active_timers--;
Paul E. McKenneyfff42152014-01-14 20:20:43 -0800691 base->all_timers--;
Paul E. McKenney16d937f2013-12-16 10:32:01 -0800692 (void)catchup_timer_jiffies(base);
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000693}
694
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000695static int detach_if_pending(struct timer_list *timer, struct tvec_base *base,
696 bool clear_pending)
697{
698 if (!timer_pending(timer))
699 return 0;
700
701 detach_timer(timer, clear_pending);
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000702 if (!tbase_get_deferrable(timer->base)) {
Tejun Heoe52b1db2012-08-08 11:10:25 -0700703 base->active_timers--;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000704 if (timer->expires == base->next_timer)
705 base->next_timer = base->timer_jiffies;
706 }
Paul E. McKenneyfff42152014-01-14 20:20:43 -0800707 base->all_timers--;
Paul E. McKenney16d937f2013-12-16 10:32:01 -0800708 (void)catchup_timer_jiffies(base);
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000709 return 1;
710}
711
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700712/*
Oleg Nesterov3691c512006-03-31 02:30:30 -0800713 * We are using hashed locking: holding per_cpu(tvec_bases).lock
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700714 * means that all timers which are tied to this base via timer->base are
715 * locked, and the base itself is locked too.
716 *
717 * So __run_timers/migrate_timers can safely modify all timers which could
718 * be found on ->tvX lists.
719 *
720 * When the timer's base is locked, and the timer removed from list, it is
721 * possible to set timer->base = NULL and drop the lock: the timer remains
722 * locked.
723 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100724static struct tvec_base *lock_timer_base(struct timer_list *timer,
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700725 unsigned long *flags)
Josh Triplett89e7e3742006-09-29 01:59:36 -0700726 __acquires(timer->base->lock)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700727{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100728 struct tvec_base *base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700729
730 for (;;) {
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100731 struct tvec_base *prelock_base = timer->base;
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700732 base = tbase_get_base(prelock_base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700733 if (likely(base != NULL)) {
734 spin_lock_irqsave(&base->lock, *flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700735 if (likely(prelock_base == timer->base))
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700736 return base;
737 /* The timer has migrated to another CPU */
738 spin_unlock_irqrestore(&base->lock, *flags);
739 }
740 cpu_relax();
741 }
742}
743
Ingo Molnar74019222009-02-18 12:23:29 +0100744static inline int
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530745__mod_timer(struct timer_list *timer, unsigned long expires,
746 bool pending_only, int pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100748 struct tvec_base *base, *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 unsigned long flags;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530750 int ret = 0 , cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800752 timer_stats_timer_set_start_info(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 BUG_ON(!timer->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700755 base = lock_timer_base(timer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000757 ret = detach_if_pending(timer, base, false);
758 if (!ret && pending_only)
759 goto out_unlock;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700760
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800761 debug_activate(timer, expires);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700762
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530763 cpu = smp_processor_id();
764
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200765#if defined(CONFIG_NO_HZ_COMMON) && defined(CONFIG_SMP)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700766 if (!pinned && get_sysctl_timer_migration() && idle_cpu(cpu))
767 cpu = get_nohz_timer_target();
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530768#endif
769 new_base = per_cpu(tvec_bases, cpu);
770
Oleg Nesterov3691c512006-03-31 02:30:30 -0800771 if (base != new_base) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700772 /*
773 * We are trying to schedule the timer on the local CPU.
774 * However we can't change timer's base while it is running,
775 * otherwise del_timer_sync() can't detect that the timer's
776 * handler yet has not finished. This also guarantees that
777 * the timer is serialized wrt itself.
778 */
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800779 if (likely(base->running_timer != timer)) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700780 /* See the comment in lock_timer_base() */
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700781 timer_set_base(timer, NULL);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700782 spin_unlock(&base->lock);
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800783 base = new_base;
784 spin_lock(&base->lock);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700785 timer_set_base(timer, base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700786 }
787 }
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 timer->expires = expires;
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800790 internal_add_timer(base, timer);
Ingo Molnar74019222009-02-18 12:23:29 +0100791
792out_unlock:
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800793 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 return ret;
796}
797
Ingo Molnar74019222009-02-18 12:23:29 +0100798/**
799 * mod_timer_pending - modify a pending timer's timeout
800 * @timer: the pending timer to be modified
801 * @expires: new timeout in jiffies
802 *
803 * mod_timer_pending() is the same for pending timers as mod_timer(),
804 * but will not re-activate and modify already deleted timers.
805 *
806 * It is useful for unserialized use of timers.
807 */
808int mod_timer_pending(struct timer_list *timer, unsigned long expires)
809{
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530810 return __mod_timer(timer, expires, true, TIMER_NOT_PINNED);
Ingo Molnar74019222009-02-18 12:23:29 +0100811}
812EXPORT_SYMBOL(mod_timer_pending);
813
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800814/*
815 * Decide where to put the timer while taking the slack into account
816 *
817 * Algorithm:
818 * 1) calculate the maximum (absolute) time
819 * 2) calculate the highest bit where the expires and new max are different
820 * 3) use this bit to make a mask
821 * 4) use the bitmask to round down the maximum time, so that all last
822 * bits are zeros
823 */
824static inline
825unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
826{
827 unsigned long expires_limit, mask;
828 int bit;
829
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200830 if (timer->slack >= 0) {
Jeff Chuaf00e0472010-05-24 07:16:24 +0800831 expires_limit = expires + timer->slack;
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200832 } else {
Sebastian Andrzej Siewior1c3cc112011-05-21 12:58:28 +0200833 long delta = expires - jiffies;
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800834
Sebastian Andrzej Siewior1c3cc112011-05-21 12:58:28 +0200835 if (delta < 256)
836 return expires;
837
838 expires_limit = expires + delta / 256;
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200839 }
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800840 mask = expires ^ expires_limit;
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800841 if (mask == 0)
842 return expires;
843
844 bit = find_last_bit(&mask, BITS_PER_LONG);
845
846 mask = (1 << bit) - 1;
847
848 expires_limit = expires_limit & ~(mask);
849
850 return expires_limit;
851}
852
Ingo Molnar74019222009-02-18 12:23:29 +0100853/**
854 * mod_timer - modify a timer's timeout
855 * @timer: the timer to be modified
856 * @expires: new timeout in jiffies
857 *
858 * mod_timer() is a more efficient way to update the expire field of an
859 * active timer (if the timer is inactive it will be activated)
860 *
861 * mod_timer(timer, expires) is equivalent to:
862 *
863 * del_timer(timer); timer->expires = expires; add_timer(timer);
864 *
865 * Note that if there are multiple unserialized concurrent users of the
866 * same timer, then mod_timer() is the only safe way to modify the timeout,
867 * since add_timer() cannot modify an already running timer.
868 *
869 * The function returns whether it has modified a pending timer or not.
870 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
871 * active timer returns 1.)
872 */
873int mod_timer(struct timer_list *timer, unsigned long expires)
874{
Sebastian Andrzej Siewior1c3cc112011-05-21 12:58:28 +0200875 expires = apply_slack(timer, expires);
876
Ingo Molnar74019222009-02-18 12:23:29 +0100877 /*
878 * This is a common optimization triggered by the
879 * networking code - if the timer is re-modified
880 * to be the same thing then just return:
881 */
Pavel Roskin48411582009-07-18 16:46:02 -0400882 if (timer_pending(timer) && timer->expires == expires)
Ingo Molnar74019222009-02-18 12:23:29 +0100883 return 1;
884
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530885 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
Ingo Molnar74019222009-02-18 12:23:29 +0100886}
887EXPORT_SYMBOL(mod_timer);
888
889/**
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530890 * mod_timer_pinned - modify a timer's timeout
891 * @timer: the timer to be modified
892 * @expires: new timeout in jiffies
893 *
894 * mod_timer_pinned() is a way to update the expire field of an
895 * active timer (if the timer is inactive it will be activated)
Paul E. McKenney048a0e82012-04-26 10:52:27 -0700896 * and to ensure that the timer is scheduled on the current CPU.
897 *
898 * Note that this does not prevent the timer from being migrated
899 * when the current CPU goes offline. If this is a problem for
900 * you, use CPU-hotplug notifiers to handle it correctly, for
901 * example, cancelling the timer when the corresponding CPU goes
902 * offline.
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530903 *
904 * mod_timer_pinned(timer, expires) is equivalent to:
905 *
906 * del_timer(timer); timer->expires = expires; add_timer(timer);
907 */
908int mod_timer_pinned(struct timer_list *timer, unsigned long expires)
909{
910 if (timer->expires == expires && timer_pending(timer))
911 return 1;
912
913 return __mod_timer(timer, expires, false, TIMER_PINNED);
914}
915EXPORT_SYMBOL(mod_timer_pinned);
916
917/**
Ingo Molnar74019222009-02-18 12:23:29 +0100918 * add_timer - start a timer
919 * @timer: the timer to be added
920 *
921 * The kernel will do a ->function(->data) callback from the
922 * timer interrupt at the ->expires point in the future. The
923 * current time is 'jiffies'.
924 *
925 * The timer's ->expires, ->function (and if the handler uses it, ->data)
926 * fields must be set prior calling this function.
927 *
928 * Timers with an ->expires field in the past will be executed in the next
929 * timer tick.
930 */
931void add_timer(struct timer_list *timer)
932{
933 BUG_ON(timer_pending(timer));
934 mod_timer(timer, timer->expires);
935}
936EXPORT_SYMBOL(add_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700938/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 * add_timer_on - start a timer on a particular CPU
940 * @timer: the timer to be added
941 * @cpu: the CPU to start it on
942 *
943 * This is not very scalable on SMP. Double adds are not possible.
944 */
945void add_timer_on(struct timer_list *timer, int cpu)
946{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100947 struct tvec_base *base = per_cpu(tvec_bases, cpu);
Thomas Gleixner68194572007-07-19 01:49:16 -0700948 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700949
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800950 timer_stats_timer_set_start_info(timer);
Thomas Gleixner68194572007-07-19 01:49:16 -0700951 BUG_ON(timer_pending(timer) || !timer->function);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800952 spin_lock_irqsave(&base->lock, flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700953 timer_set_base(timer, base);
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800954 debug_activate(timer, timer->expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 internal_add_timer(base, timer);
Thomas Gleixner06d83082008-03-22 09:20:24 +0100956 /*
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200957 * Check whether the other CPU is in dynticks mode and needs
958 * to be triggered to reevaluate the timer wheel.
959 * We are protected against the other CPU fiddling
Thomas Gleixner06d83082008-03-22 09:20:24 +0100960 * with the timer by holding the timer base lock. This also
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200961 * makes sure that a CPU on the way to stop its tick can not
962 * evaluate the timer wheel.
Thomas Gleixner06d83082008-03-22 09:20:24 +0100963 */
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200964 wake_up_nohz_cpu(cpu);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800965 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
Andi Kleena9862e02009-05-19 22:49:07 +0200967EXPORT_SYMBOL_GPL(add_timer_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700969/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 * del_timer - deactive a timer.
971 * @timer: the timer to be deactivated
972 *
973 * del_timer() deactivates a timer - this works on both active and inactive
974 * timers.
975 *
976 * The function returns whether it has deactivated a pending timer or not.
977 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
978 * active timer returns 1.)
979 */
980int del_timer(struct timer_list *timer)
981{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100982 struct tvec_base *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700984 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Christine Chandc4218b2011-11-07 19:48:28 -0800986 debug_assert_init(timer);
987
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800988 timer_stats_timer_clear_start_info(timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700989 if (timer_pending(timer)) {
990 base = lock_timer_base(timer, &flags);
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000991 ret = detach_if_pending(timer, base, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700995 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997EXPORT_SYMBOL(del_timer);
998
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700999/**
1000 * try_to_del_timer_sync - Try to deactivate a timer
1001 * @timer: timer do del
1002 *
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001003 * This function tries to deactivate a timer. Upon successful (ret >= 0)
1004 * exit the timer is not queued and the handler is not running on any CPU.
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001005 */
1006int try_to_del_timer_sync(struct timer_list *timer)
1007{
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001008 struct tvec_base *base;
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001009 unsigned long flags;
1010 int ret = -1;
1011
Christine Chandc4218b2011-11-07 19:48:28 -08001012 debug_assert_init(timer);
1013
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001014 base = lock_timer_base(timer, &flags);
1015
Thomas Gleixnerec44bc72012-05-25 22:08:57 +00001016 if (base->running_timer != timer) {
1017 timer_stats_timer_clear_start_info(timer);
1018 ret = detach_if_pending(timer, base, true);
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001019 }
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001020 spin_unlock_irqrestore(&base->lock, flags);
1021
1022 return ret;
1023}
David Howellse19dff12007-04-26 15:46:56 -07001024EXPORT_SYMBOL(try_to_del_timer_sync);
1025
Yong Zhang6f1bc452010-10-20 15:57:31 -07001026#ifdef CONFIG_SMP
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001027/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 * del_timer_sync - deactivate a timer and wait for the handler to finish.
1029 * @timer: the timer to be deactivated
1030 *
1031 * This function only differs from del_timer() on SMP: besides deactivating
1032 * the timer it also makes sure the handler has finished executing on other
1033 * CPUs.
1034 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001035 * Synchronization rules: Callers must prevent restarting of the timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 * otherwise this function is meaningless. It must not be called from
Tejun Heoc5f66e92012-08-08 11:10:28 -07001037 * interrupt contexts unless the timer is an irqsafe one. The caller must
1038 * not hold locks which would prevent completion of the timer's
1039 * handler. The timer's handler must not call add_timer_on(). Upon exit the
1040 * timer is not queued and the handler is not running on any CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 *
Tejun Heoc5f66e92012-08-08 11:10:28 -07001042 * Note: For !irqsafe timers, you must not hold locks that are held in
1043 * interrupt context while calling this function. Even if the lock has
1044 * nothing to do with the timer in question. Here's why:
Steven Rostedt48228f72011-02-08 12:39:54 -05001045 *
1046 * CPU0 CPU1
1047 * ---- ----
1048 * <SOFTIRQ>
1049 * call_timer_fn();
1050 * base->running_timer = mytimer;
1051 * spin_lock_irq(somelock);
1052 * <IRQ>
1053 * spin_lock(somelock);
1054 * del_timer_sync(mytimer);
1055 * while (base->running_timer == mytimer);
1056 *
1057 * Now del_timer_sync() will never return and never release somelock.
1058 * The interrupt on the other CPU is waiting to grab somelock but
1059 * it has interrupted the softirq that CPU0 is waiting to finish.
1060 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 * The function returns whether it has deactivated a pending timer or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
1063int del_timer_sync(struct timer_list *timer)
1064{
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001065#ifdef CONFIG_LOCKDEP
Peter Zijlstraf266a512011-02-03 15:09:41 +01001066 unsigned long flags;
1067
Steven Rostedt48228f72011-02-08 12:39:54 -05001068 /*
1069 * If lockdep gives a backtrace here, please reference
1070 * the synchronization rules above.
1071 */
Peter Zijlstra7ff20792011-02-08 15:18:00 +01001072 local_irq_save(flags);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001073 lock_map_acquire(&timer->lockdep_map);
1074 lock_map_release(&timer->lockdep_map);
Peter Zijlstra7ff20792011-02-08 15:18:00 +01001075 local_irq_restore(flags);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001076#endif
Yong Zhang466bd302010-10-20 15:57:33 -07001077 /*
1078 * don't use it in hardirq context, because it
1079 * could lead to deadlock.
1080 */
Tejun Heoc5f66e92012-08-08 11:10:28 -07001081 WARN_ON(in_irq() && !tbase_get_irqsafe(timer->base));
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001082 for (;;) {
1083 int ret = try_to_del_timer_sync(timer);
1084 if (ret >= 0)
1085 return ret;
Andrew Mortona0009652006-07-14 00:24:06 -07001086 cpu_relax();
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089EXPORT_SYMBOL(del_timer_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090#endif
1091
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001092static int cascade(struct tvec_base *base, struct tvec *tv, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 /* cascade all the timers from tv up one level */
Porpoise3439dd82006-06-23 02:05:56 -07001095 struct timer_list *timer, *tmp;
1096 struct list_head tv_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Porpoise3439dd82006-06-23 02:05:56 -07001098 list_replace_init(tv->vec + index, &tv_list);
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /*
Porpoise3439dd82006-06-23 02:05:56 -07001101 * We are removing _all_ timers from the list, so we
1102 * don't have to detach them individually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 */
Porpoise3439dd82006-06-23 02:05:56 -07001104 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001105 BUG_ON(tbase_get_base(timer->base) != base);
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +00001106 /* No accounting, while moving them */
1107 __internal_add_timer(base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 return index;
1111}
1112
Thomas Gleixner576da122010-03-12 21:10:29 +01001113static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
1114 unsigned long data)
1115{
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001116 int count = preempt_count();
Thomas Gleixner576da122010-03-12 21:10:29 +01001117
1118#ifdef CONFIG_LOCKDEP
1119 /*
1120 * It is permissible to free the timer from inside the
1121 * function that is called from it, this we need to take into
1122 * account for lockdep too. To avoid bogus "held lock freed"
1123 * warnings as well as problems when looking into
1124 * timer->lockdep_map, make a copy and use that here.
1125 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001126 struct lockdep_map lockdep_map;
1127
1128 lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
Thomas Gleixner576da122010-03-12 21:10:29 +01001129#endif
1130 /*
1131 * Couple the lock chain with the lock chain at
1132 * del_timer_sync() by acquiring the lock_map around the fn()
1133 * call here and in del_timer_sync().
1134 */
1135 lock_map_acquire(&lockdep_map);
1136
1137 trace_timer_expire_entry(timer);
1138 fn(data);
1139 trace_timer_expire_exit(timer);
1140
1141 lock_map_release(&lockdep_map);
1142
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001143 if (count != preempt_count()) {
Thomas Gleixner802702e2010-03-12 20:13:23 +01001144 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001145 fn, count, preempt_count());
Thomas Gleixner802702e2010-03-12 20:13:23 +01001146 /*
1147 * Restore the preempt count. That gives us a decent
1148 * chance to survive and extract information. If the
1149 * callback kept a lock held, bad luck, but not worse
1150 * than the BUG() we had.
1151 */
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001152 preempt_count_set(count);
Thomas Gleixner576da122010-03-12 21:10:29 +01001153 }
1154}
1155
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001156#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
1157
1158/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * __run_timers - run all expired timers (if any) on this CPU.
1160 * @base: the timer vector to be processed.
1161 *
1162 * This function cascades all vectors and executes all expired timer
1163 * vectors.
1164 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001165static inline void __run_timers(struct tvec_base *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 struct timer_list *timer;
1168
Oleg Nesterov3691c512006-03-31 02:30:30 -08001169 spin_lock_irq(&base->lock);
Paul E. McKenneyd550e812013-12-16 05:57:10 -08001170 if (catchup_timer_jiffies(base)) {
1171 spin_unlock_irq(&base->lock);
1172 return;
1173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 while (time_after_eq(jiffies, base->timer_jiffies)) {
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001175 struct list_head work_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 struct list_head *head = &work_list;
Thomas Gleixner68194572007-07-19 01:49:16 -07001177 int index = base->timer_jiffies & TVR_MASK;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /*
1180 * Cascade timers:
1181 */
1182 if (!index &&
1183 (!cascade(base, &base->tv2, INDEX(0))) &&
1184 (!cascade(base, &base->tv3, INDEX(1))) &&
1185 !cascade(base, &base->tv4, INDEX(2)))
1186 cascade(base, &base->tv5, INDEX(3));
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001187 ++base->timer_jiffies;
1188 list_replace_init(base->tv1.vec + index, &work_list);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001189 while (!list_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 void (*fn)(unsigned long);
1191 unsigned long data;
Tejun Heoc5f66e92012-08-08 11:10:28 -07001192 bool irqsafe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001194 timer = list_first_entry(head, struct timer_list,entry);
Thomas Gleixner68194572007-07-19 01:49:16 -07001195 fn = timer->function;
1196 data = timer->data;
Tejun Heoc5f66e92012-08-08 11:10:28 -07001197 irqsafe = tbase_get_irqsafe(timer->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001199 timer_stats_account_timer(timer);
1200
Yong Zhang6f1bc452010-10-20 15:57:31 -07001201 base->running_timer = timer;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +00001202 detach_expired_timer(timer, base);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001203
Tejun Heoc5f66e92012-08-08 11:10:28 -07001204 if (irqsafe) {
1205 spin_unlock(&base->lock);
1206 call_timer_fn(timer, fn, data);
1207 spin_lock(&base->lock);
1208 } else {
1209 spin_unlock_irq(&base->lock);
1210 call_timer_fn(timer, fn, data);
1211 spin_lock_irq(&base->lock);
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214 }
Yong Zhang6f1bc452010-10-20 15:57:31 -07001215 base->running_timer = NULL;
Oleg Nesterov3691c512006-03-31 02:30:30 -08001216 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001219#ifdef CONFIG_NO_HZ_COMMON
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220/*
1221 * Find out when the next timer event is due to happen. This
Randy Dunlap90cba642009-08-25 14:35:41 -07001222 * is used on S/390 to stop all activity when a CPU is idle.
1223 * This function needs to be called with interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001225static unsigned long __next_timer_interrupt(struct tvec_base *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001227 unsigned long timer_jiffies = base->timer_jiffies;
Thomas Gleixnereaad0842007-05-29 23:47:39 +02001228 unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001229 int index, slot, array, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 struct timer_list *nte;
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001231 struct tvec *varray[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 /* Look for timer events in tv1. */
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001234 index = slot = timer_jiffies & TVR_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001236 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
Thomas Gleixner68194572007-07-19 01:49:16 -07001237 if (tbase_get_deferrable(nte->base))
1238 continue;
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001239
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001240 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001242 /* Look at the cascade bucket(s)? */
1243 if (!index || slot < index)
1244 goto cascade;
1245 return expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001247 slot = (slot + 1) & TVR_MASK;
1248 } while (slot != index);
1249
1250cascade:
1251 /* Calculate the next cascade event */
1252 if (index)
1253 timer_jiffies += TVR_SIZE - index;
1254 timer_jiffies >>= TVR_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 /* Check tv2-tv5. */
1257 varray[0] = &base->tv2;
1258 varray[1] = &base->tv3;
1259 varray[2] = &base->tv4;
1260 varray[3] = &base->tv5;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001261
1262 for (array = 0; array < 4; array++) {
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001263 struct tvec *varp = varray[array];
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001264
1265 index = slot = timer_jiffies & TVN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001267 list_for_each_entry(nte, varp->vec + slot, entry) {
Jon Huntera0419882009-05-01 13:10:23 -07001268 if (tbase_get_deferrable(nte->base))
1269 continue;
1270
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001271 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (time_before(nte->expires, expires))
1273 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001274 }
1275 /*
1276 * Do we still search for the first timer or are
1277 * we looking up the cascade buckets ?
1278 */
1279 if (found) {
1280 /* Look at the cascade bucket(s)? */
1281 if (!index || slot < index)
1282 break;
1283 return expires;
1284 }
1285 slot = (slot + 1) & TVN_MASK;
1286 } while (slot != index);
1287
1288 if (index)
1289 timer_jiffies += TVN_SIZE - index;
1290 timer_jiffies >>= TVN_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001292 return expires;
1293}
1294
1295/*
1296 * Check, if the next hrtimer event is before the next timer wheel
1297 * event:
1298 */
1299static unsigned long cmp_next_hrtimer_event(unsigned long now,
1300 unsigned long expires)
1301{
1302 ktime_t hr_delta = hrtimer_get_next_event();
1303 struct timespec tsdelta;
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001304 unsigned long delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001305
1306 if (hr_delta.tv64 == KTIME_MAX)
1307 return expires;
1308
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001309 /*
1310 * Expired timer available, let it expire in the next tick
1311 */
1312 if (hr_delta.tv64 <= 0)
1313 return now + 1;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001314
1315 tsdelta = ktime_to_timespec(hr_delta);
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001316 delta = timespec_to_jiffies(&tsdelta);
Thomas Gleixnereaad0842007-05-29 23:47:39 +02001317
1318 /*
1319 * Limit the delta to the max value, which is checked in
1320 * tick_nohz_stop_sched_tick():
1321 */
1322 if (delta > NEXT_TIMER_MAX_DELTA)
1323 delta = NEXT_TIMER_MAX_DELTA;
1324
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001325 /*
1326 * Take rounding errors in to account and make sure, that it
1327 * expires in the next tick. Otherwise we go into an endless
1328 * ping pong due to tick_nohz_stop_sched_tick() retriggering
1329 * the timer softirq
1330 */
1331 if (delta < 1)
1332 delta = 1;
1333 now += delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001334 if (time_before(now, expires))
1335 return now;
1336 return expires;
1337}
1338
1339/**
Li Zefan8dce39c2007-11-05 14:51:10 -08001340 * get_next_timer_interrupt - return the jiffy of the next pending timer
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08001341 * @now: current time (in jiffies)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001342 */
Thomas Gleixnerfd064b92007-02-16 01:27:47 -08001343unsigned long get_next_timer_interrupt(unsigned long now)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001344{
Christoph Lameter74963512010-11-30 14:05:53 -06001345 struct tvec_base *base = __this_cpu_read(tvec_bases);
Thomas Gleixnere40468a2012-05-25 22:08:59 +00001346 unsigned long expires = now + NEXT_TIMER_MAX_DELTA;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001347
Heiko Carstensdbd87b52010-12-01 10:11:09 +01001348 /*
1349 * Pretend that there is no timer pending if the cpu is offline.
1350 * Possible pending timers will be migrated later to an active cpu.
1351 */
1352 if (cpu_is_offline(smp_processor_id()))
Thomas Gleixnere40468a2012-05-25 22:08:59 +00001353 return expires;
1354
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001355 spin_lock(&base->lock);
Thomas Gleixnere40468a2012-05-25 22:08:59 +00001356 if (base->active_timers) {
1357 if (time_before_eq(base->next_timer, base->timer_jiffies))
1358 base->next_timer = __next_timer_interrupt(base);
1359 expires = base->next_timer;
1360 }
Oleg Nesterov3691c512006-03-31 02:30:30 -08001361 spin_unlock(&base->lock);
Tony Lindgren69239742006-03-06 15:42:45 -08001362
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001363 if (time_before_eq(expires, now))
1364 return now;
Zachary Amsden0662b712006-05-20 15:00:24 -07001365
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001366 return cmp_next_hrtimer_event(now, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368#endif
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370/*
Daniel Walker5b4db0c2007-10-18 03:06:11 -07001371 * Called from the timer interrupt handler to charge one tick to the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 * process. user_tick is 1 if the tick is user time, 0 for system.
1373 */
1374void update_process_times(int user_tick)
1375{
1376 struct task_struct *p = current;
1377 int cpu = smp_processor_id();
1378
1379 /* Note: this timer irq context must be accounted for as well. */
Paul Mackerrasfa13a5a2007-11-09 22:39:38 +01001380 account_process_tick(p, user_tick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 run_local_timers();
Paul E. McKenneya1572292009-08-22 13:56:51 -07001382 rcu_check_callbacks(cpu, user_tick);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08001383#ifdef CONFIG_IRQ_WORK
1384 if (in_irq())
1385 irq_work_run();
1386#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 scheduler_tick();
Thomas Gleixner68194572007-07-19 01:49:16 -07001388 run_posix_cpu_timers(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
1391/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * This function runs timers and the timer-tq in bottom half context.
1393 */
1394static void run_timer_softirq(struct softirq_action *h)
1395{
Christoph Lameter74963512010-11-30 14:05:53 -06001396 struct tvec_base *base = __this_cpu_read(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001398 hrtimer_run_pending();
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (time_after_eq(jiffies, base->timer_jiffies))
1401 __run_timers(base);
1402}
1403
1404/*
1405 * Called by the local, per-CPU timer interrupt on SMP.
1406 */
1407void run_local_timers(void)
1408{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001409 hrtimer_run_queues();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 raise_softirq(TIMER_SOFTIRQ);
1411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413#ifdef __ARCH_WANT_SYS_ALARM
1414
1415/*
1416 * For backwards compatibility? This can be done in libc so Alpha
1417 * and all newer ports shouldn't need it.
1418 */
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001419SYSCALL_DEFINE1(alarm, unsigned int, seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -08001421 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
1424#endif
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426static void process_timeout(unsigned long __data)
1427{
Ingo Molnar36c8b582006-07-03 00:25:41 -07001428 wake_up_process((struct task_struct *)__data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
1431/**
1432 * schedule_timeout - sleep until timeout
1433 * @timeout: timeout value in jiffies
1434 *
1435 * Make the current task sleep until @timeout jiffies have
1436 * elapsed. The routine will return immediately unless
1437 * the current task state has been set (see set_current_state()).
1438 *
1439 * You can set the task state as follows -
1440 *
1441 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1442 * pass before the routine returns. The routine will return 0
1443 *
1444 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1445 * delivered to the current task. In this case the remaining time
1446 * in jiffies will be returned, or 0 if the timer expired in time
1447 *
1448 * The current task state is guaranteed to be TASK_RUNNING when this
1449 * routine returns.
1450 *
1451 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1452 * the CPU away without a bound on the timeout. In this case the return
1453 * value will be %MAX_SCHEDULE_TIMEOUT.
1454 *
1455 * In all cases the return value is guaranteed to be non-negative.
1456 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001457signed long __sched schedule_timeout(signed long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 struct timer_list timer;
1460 unsigned long expire;
1461
1462 switch (timeout)
1463 {
1464 case MAX_SCHEDULE_TIMEOUT:
1465 /*
1466 * These two special cases are useful to be comfortable
1467 * in the caller. Nothing more. We could take
1468 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1469 * but I' d like to return a valid offset (>=0) to allow
1470 * the caller to do everything it want with the retval.
1471 */
1472 schedule();
1473 goto out;
1474 default:
1475 /*
1476 * Another bit of PARANOID. Note that the retval will be
1477 * 0 since no piece of kernel is supposed to do a check
1478 * for a negative retval of schedule_timeout() (since it
1479 * should never happens anyway). You just have the printk()
1480 * that will tell you if something is gone wrong and where.
1481 */
Andrew Morton5b149bc2006-12-22 01:10:14 -08001482 if (timeout < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 printk(KERN_ERR "schedule_timeout: wrong timeout "
Andrew Morton5b149bc2006-12-22 01:10:14 -08001484 "value %lx\n", timeout);
1485 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 current->state = TASK_RUNNING;
1487 goto out;
1488 }
1489 }
1490
1491 expire = timeout + jiffies;
1492
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001493 setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
Arun R Bharadwaj597d0272009-04-16 12:13:26 +05301494 __mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 schedule();
1496 del_singleshot_timer_sync(&timer);
1497
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001498 /* Remove the timer from the object tracker */
1499 destroy_timer_on_stack(&timer);
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 timeout = expire - jiffies;
1502
1503 out:
1504 return timeout < 0 ? 0 : timeout;
1505}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506EXPORT_SYMBOL(schedule_timeout);
1507
Andrew Morton8a1c1752005-09-13 01:25:15 -07001508/*
1509 * We can use __set_current_state() here because schedule_timeout() calls
1510 * schedule() unconditionally.
1511 */
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001512signed long __sched schedule_timeout_interruptible(signed long timeout)
1513{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001514 __set_current_state(TASK_INTERRUPTIBLE);
1515 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001516}
1517EXPORT_SYMBOL(schedule_timeout_interruptible);
1518
Matthew Wilcox294d5cc2007-12-06 11:59:46 -05001519signed long __sched schedule_timeout_killable(signed long timeout)
1520{
1521 __set_current_state(TASK_KILLABLE);
1522 return schedule_timeout(timeout);
1523}
1524EXPORT_SYMBOL(schedule_timeout_killable);
1525
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001526signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1527{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001528 __set_current_state(TASK_UNINTERRUPTIBLE);
1529 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001530}
1531EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1532
Paul Gortmaker0db06282013-06-19 14:53:51 -04001533static int init_timers_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 int j;
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001536 struct tvec_base *base;
Paul Gortmaker0db06282013-06-19 14:53:51 -04001537 static char tvec_base_done[NR_CPUS];
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001538
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001539 if (!tvec_base_done[cpu]) {
Jan Beulicha4a61982006-03-24 03:15:54 -08001540 static char boot_done;
1541
Jan Beulicha4a61982006-03-24 03:15:54 -08001542 if (boot_done) {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001543 /*
1544 * The APs use this path later in boot
1545 */
Joe Perchesda554eb2013-11-15 14:15:31 -08001546 base = kzalloc_node(sizeof(*base), GFP_KERNEL,
1547 cpu_to_node(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001548 if (!base)
1549 return -ENOMEM;
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001550
1551 /* Make sure that tvec_base is 2 byte aligned */
1552 if (tbase_get_deferrable(base)) {
1553 WARN_ON(1);
1554 kfree(base);
1555 return -ENOMEM;
1556 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001557 per_cpu(tvec_bases, cpu) = base;
Jan Beulicha4a61982006-03-24 03:15:54 -08001558 } else {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001559 /*
1560 * This is for the boot CPU - we use compile-time
1561 * static initialisation because per-cpu memory isn't
1562 * ready yet and because the memory allocators are not
1563 * initialised either.
1564 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001565 boot_done = 1;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001566 base = &boot_tvec_bases;
Jan Beulicha4a61982006-03-24 03:15:54 -08001567 }
Tirupathi Reddy42a5cf462013-05-14 13:59:02 +05301568 spin_lock_init(&base->lock);
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001569 tvec_base_done[cpu] = 1;
1570 } else {
1571 base = per_cpu(tvec_bases, cpu);
Jan Beulicha4a61982006-03-24 03:15:54 -08001572 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001573
Ingo Molnard730e882006-07-03 00:25:10 -07001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 for (j = 0; j < TVN_SIZE; j++) {
1576 INIT_LIST_HEAD(base->tv5.vec + j);
1577 INIT_LIST_HEAD(base->tv4.vec + j);
1578 INIT_LIST_HEAD(base->tv3.vec + j);
1579 INIT_LIST_HEAD(base->tv2.vec + j);
1580 }
1581 for (j = 0; j < TVR_SIZE; j++)
1582 INIT_LIST_HEAD(base->tv1.vec + j);
1583
1584 base->timer_jiffies = jiffies;
Martin Schwidefsky97fd9ed2009-07-21 20:25:05 +02001585 base->next_timer = base->timer_jiffies;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +00001586 base->active_timers = 0;
Paul E. McKenneyfff42152014-01-14 20:20:43 -08001587 base->all_timers = 0;
Jan Beulicha4a61982006-03-24 03:15:54 -08001588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
1591#ifdef CONFIG_HOTPLUG_CPU
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001592static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 struct timer_list *timer;
1595
1596 while (!list_empty(head)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001597 timer = list_first_entry(head, struct timer_list, entry);
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +00001598 /* We ignore the accounting on the dying cpu */
Thomas Gleixnerec44bc72012-05-25 22:08:57 +00001599 detach_timer(timer, false);
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001600 timer_set_base(timer, new_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 internal_add_timer(new_base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
Paul Gortmaker0db06282013-06-19 14:53:51 -04001605static void migrate_timers(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001607 struct tvec_base *old_base;
1608 struct tvec_base *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 int i;
1610
1611 BUG_ON(cpu_online(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001612 old_base = per_cpu(tvec_bases, cpu);
1613 new_base = get_cpu_var(tvec_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001614 /*
1615 * The caller is globally serialized and nobody else
1616 * takes two locks at once, deadlock is not possible.
1617 */
1618 spin_lock_irq(&new_base->lock);
Oleg Nesterov0d180402008-04-04 20:54:10 +02001619 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Oleg Nesterov3691c512006-03-31 02:30:30 -08001621 BUG_ON(old_base->running_timer);
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 for (i = 0; i < TVR_SIZE; i++)
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001624 migrate_timer_list(new_base, old_base->tv1.vec + i);
1625 for (i = 0; i < TVN_SIZE; i++) {
1626 migrate_timer_list(new_base, old_base->tv2.vec + i);
1627 migrate_timer_list(new_base, old_base->tv3.vec + i);
1628 migrate_timer_list(new_base, old_base->tv4.vec + i);
1629 migrate_timer_list(new_base, old_base->tv5.vec + i);
1630 }
1631
Oleg Nesterov0d180402008-04-04 20:54:10 +02001632 spin_unlock(&old_base->lock);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001633 spin_unlock_irq(&new_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 put_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636#endif /* CONFIG_HOTPLUG_CPU */
1637
Paul Gortmaker0db06282013-06-19 14:53:51 -04001638static int timer_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 unsigned long action, void *hcpu)
1640{
1641 long cpu = (long)hcpu;
Akinobu Mita80b51842010-05-26 14:43:32 -07001642 int err;
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 switch(action) {
1645 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001646 case CPU_UP_PREPARE_FROZEN:
Akinobu Mita80b51842010-05-26 14:43:32 -07001647 err = init_timers_cpu(cpu);
1648 if (err < 0)
1649 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 break;
1651#ifdef CONFIG_HOTPLUG_CPU
1652 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001653 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 migrate_timers(cpu);
1655 break;
1656#endif
1657 default:
1658 break;
1659 }
1660 return NOTIFY_OK;
1661}
1662
Paul Gortmaker0db06282013-06-19 14:53:51 -04001663static struct notifier_block timers_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 .notifier_call = timer_cpu_notify,
1665};
1666
1667
1668void __init init_timers(void)
1669{
Tejun Heoe52b1db2012-08-08 11:10:25 -07001670 int err;
Akinobu Mita07dccf32006-09-29 02:00:22 -07001671
Tejun Heoe52b1db2012-08-08 11:10:25 -07001672 /* ensure there are enough low bits for flags in timer->base pointer */
1673 BUILD_BUG_ON(__alignof__(struct tvec_base) & TIMER_FLAG_MASK);
1674
1675 err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1676 (void *)(long)smp_processor_id());
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001677 init_timer_stats();
1678
Akinobu Mita9e506f72010-06-04 14:15:04 -07001679 BUG_ON(err != NOTIFY_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 register_cpu_notifier(&timers_nb);
Carlos R. Mafra962cf362008-05-15 11:15:37 -03001681 open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684/**
1685 * msleep - sleep safely even with waitqueue interruptions
1686 * @msecs: Time in milliseconds to sleep for
1687 */
1688void msleep(unsigned int msecs)
1689{
1690 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1691
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001692 while (timeout)
1693 timeout = schedule_timeout_uninterruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
1696EXPORT_SYMBOL(msleep);
1697
1698/**
Domen Puncer96ec3ef2005-06-25 14:58:43 -07001699 * msleep_interruptible - sleep waiting for signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 * @msecs: Time in milliseconds to sleep for
1701 */
1702unsigned long msleep_interruptible(unsigned int msecs)
1703{
1704 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1705
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001706 while (timeout && !signal_pending(current))
1707 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return jiffies_to_msecs(timeout);
1709}
1710
1711EXPORT_SYMBOL(msleep_interruptible);
Patrick Pannuto5e7f5a12010-08-02 15:01:04 -07001712
1713static int __sched do_usleep_range(unsigned long min, unsigned long max)
1714{
1715 ktime_t kmin;
1716 unsigned long delta;
1717
1718 kmin = ktime_set(0, min * NSEC_PER_USEC);
1719 delta = (max - min) * NSEC_PER_USEC;
1720 return schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL);
1721}
1722
1723/**
1724 * usleep_range - Drop in replacement for udelay where wakeup is flexible
1725 * @min: Minimum time in usecs to sleep
1726 * @max: Maximum time in usecs to sleep
1727 */
1728void usleep_range(unsigned long min, unsigned long max)
1729{
1730 __set_current_state(TASK_UNINTERRUPTIBLE);
1731 do_usleep_range(min, max);
1732}
1733EXPORT_SYMBOL(usleep_range);