blob: 2ece3aa5069cade64b8c4982e920a45bea5ba232 [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
Andi Kleen40747ff2014-02-08 08:51:59 +010055__visible u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
Thomas Gleixnerecea8d12005-10-30 15:03:00 -080056
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;
Viresh Kumard6f93822014-06-22 01:29:13 +020085 int cpu;
Pavel Macheka6fa8e52008-01-30 13:30:00 +010086 struct tvec_root tv1;
87 struct tvec tv2;
88 struct tvec tv3;
89 struct tvec tv4;
90 struct tvec tv5;
Venki Pallipadi6e453a62007-05-08 00:27:44 -070091} ____cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Peter Zijlstrab337a9382015-03-31 20:49:00 +053093/*
94 * __TIMER_INITIALIZER() needs to set ->base to a valid pointer (because we've
95 * made NULL special, hint: lock_timer_base()) and we cannot get a compile time
96 * pointer to per-cpu entries because we don't know where we'll map the section,
97 * even for the boot cpu.
98 *
99 * And so we use boot_tvec_bases for boot CPU and per-cpu __tvec_bases for the
100 * rest of them.
101 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100102struct tvec_base boot_tvec_bases;
Oleg Nesterov3691c512006-03-31 02:30:30 -0800103EXPORT_SYMBOL(boot_tvec_bases);
Peter Zijlstrab337a9382015-03-31 20:49:00 +0530104
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100105static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700107/* Functions below help us manage 'deferrable' flag */
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100108static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700109{
Tejun Heoe52b1db2012-08-08 11:10:25 -0700110 return ((unsigned int)(unsigned long)base & TIMER_DEFERRABLE);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700111}
112
Tejun Heoc5f66e92012-08-08 11:10:28 -0700113static inline unsigned int tbase_get_irqsafe(struct tvec_base *base)
114{
115 return ((unsigned int)(unsigned long)base & TIMER_IRQSAFE);
116}
117
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100118static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700119{
Tejun Heoe52b1db2012-08-08 11:10:25 -0700120 return ((struct tvec_base *)((unsigned long)base & ~TIMER_FLAG_MASK));
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700121}
122
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700123static inline void
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100124timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700125{
Tejun Heoe52b1db2012-08-08 11:10:25 -0700126 unsigned long flags = (unsigned long)timer->base & TIMER_FLAG_MASK;
127
128 timer->base = (struct tvec_base *)((unsigned long)(new_base) | flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700129}
130
Alan Stern9c133c42008-11-06 08:42:48 +0100131static unsigned long round_jiffies_common(unsigned long j, int cpu,
132 bool force_up)
133{
134 int rem;
135 unsigned long original = j;
136
137 /*
138 * We don't want all cpus firing their timers at once hitting the
139 * same lock or cachelines, so we skew each extra cpu with an extra
140 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
141 * already did this.
142 * The skew is done by adding 3*cpunr, then round, then subtract this
143 * extra offset again.
144 */
145 j += cpu * 3;
146
147 rem = j % HZ;
148
149 /*
150 * If the target jiffie is just after a whole second (which can happen
151 * due to delays of the timer irq, long irq off times etc etc) then
152 * we should round down to the whole second, not up. Use 1/4th second
153 * as cutoff for this rounding as an extreme upper bound for this.
154 * But never round down if @force_up is set.
155 */
156 if (rem < HZ/4 && !force_up) /* round down */
157 j = j - rem;
158 else /* round up */
159 j = j - rem + HZ;
160
161 /* now that we have rounded, subtract the extra skew again */
162 j -= cpu * 3;
163
Bart Van Assche9e04d382013-05-21 20:43:50 +0200164 /*
165 * Make sure j is still in the future. Otherwise return the
166 * unmodified value.
167 */
168 return time_is_after_jiffies(j) ? j : original;
Alan Stern9c133c42008-11-06 08:42:48 +0100169}
170
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800171/**
172 * __round_jiffies - function to round jiffies to a full second
173 * @j: the time in (absolute) jiffies that should be rounded
174 * @cpu: the processor number on which the timeout will happen
175 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800176 * __round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800177 * up or down to (approximately) full seconds. This is useful for timers
178 * for which the exact time they fire does not matter too much, as long as
179 * they fire approximately every X seconds.
180 *
181 * By rounding these timers to whole seconds, all such timers will fire
182 * at the same time, rather than at various times spread out. The goal
183 * of this is to have the CPU wake up less, which saves power.
184 *
185 * The exact rounding is skewed for each processor to avoid all
186 * processors firing at the exact same time, which could lead
187 * to lock contention or spurious cache line bouncing.
188 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800189 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800190 */
191unsigned long __round_jiffies(unsigned long j, int cpu)
192{
Alan Stern9c133c42008-11-06 08:42:48 +0100193 return round_jiffies_common(j, cpu, false);
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800194}
195EXPORT_SYMBOL_GPL(__round_jiffies);
196
197/**
198 * __round_jiffies_relative - function to round jiffies to a full second
199 * @j: the time in (relative) jiffies that should be rounded
200 * @cpu: the processor number on which the timeout will happen
201 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800202 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800203 * up or down to (approximately) full seconds. This is useful for timers
204 * for which the exact time they fire does not matter too much, as long as
205 * they fire approximately every X seconds.
206 *
207 * By rounding these timers to whole seconds, all such timers will fire
208 * at the same time, rather than at various times spread out. The goal
209 * of this is to have the CPU wake up less, which saves power.
210 *
211 * The exact rounding is skewed for each processor to avoid all
212 * processors firing at the exact same time, which could lead
213 * to lock contention or spurious cache line bouncing.
214 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800215 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800216 */
217unsigned long __round_jiffies_relative(unsigned long j, int cpu)
218{
Alan Stern9c133c42008-11-06 08:42:48 +0100219 unsigned long j0 = jiffies;
220
221 /* Use j0 because jiffies might change while we run */
222 return round_jiffies_common(j + j0, cpu, false) - j0;
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800223}
224EXPORT_SYMBOL_GPL(__round_jiffies_relative);
225
226/**
227 * round_jiffies - function to round jiffies to a full second
228 * @j: the time in (absolute) jiffies that should be rounded
229 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800230 * round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800231 * up or down to (approximately) full seconds. This is useful for timers
232 * for which the exact time they fire does not matter too much, as long as
233 * they fire approximately every X seconds.
234 *
235 * By rounding these timers to whole seconds, all such timers will fire
236 * at the same time, rather than at various times spread out. The goal
237 * of this is to have the CPU wake up less, which saves power.
238 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800239 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800240 */
241unsigned long round_jiffies(unsigned long j)
242{
Alan Stern9c133c42008-11-06 08:42:48 +0100243 return round_jiffies_common(j, raw_smp_processor_id(), false);
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800244}
245EXPORT_SYMBOL_GPL(round_jiffies);
246
247/**
248 * round_jiffies_relative - function to round jiffies to a full second
249 * @j: the time in (relative) jiffies that should be rounded
250 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800251 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800252 * up or down to (approximately) full seconds. This is useful for timers
253 * for which the exact time they fire does not matter too much, as long as
254 * they fire approximately every X seconds.
255 *
256 * By rounding these timers to whole seconds, all such timers will fire
257 * at the same time, rather than at various times spread out. The goal
258 * of this is to have the CPU wake up less, which saves power.
259 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800260 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800261 */
262unsigned long round_jiffies_relative(unsigned long j)
263{
264 return __round_jiffies_relative(j, raw_smp_processor_id());
265}
266EXPORT_SYMBOL_GPL(round_jiffies_relative);
267
Alan Stern9c133c42008-11-06 08:42:48 +0100268/**
269 * __round_jiffies_up - function to round jiffies up to a full second
270 * @j: the time in (absolute) jiffies that should be rounded
271 * @cpu: the processor number on which the timeout will happen
272 *
273 * This is the same as __round_jiffies() except that it will never
274 * round down. This is useful for timeouts for which the exact time
275 * of firing does not matter too much, as long as they don't fire too
276 * early.
277 */
278unsigned long __round_jiffies_up(unsigned long j, int cpu)
279{
280 return round_jiffies_common(j, cpu, true);
281}
282EXPORT_SYMBOL_GPL(__round_jiffies_up);
283
284/**
285 * __round_jiffies_up_relative - function to round jiffies up to a full second
286 * @j: the time in (relative) jiffies that should be rounded
287 * @cpu: the processor number on which the timeout will happen
288 *
289 * This is the same as __round_jiffies_relative() except that it will never
290 * round down. This is useful for timeouts for which the exact time
291 * of firing does not matter too much, as long as they don't fire too
292 * early.
293 */
294unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
295{
296 unsigned long j0 = jiffies;
297
298 /* Use j0 because jiffies might change while we run */
299 return round_jiffies_common(j + j0, cpu, true) - j0;
300}
301EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
302
303/**
304 * round_jiffies_up - function to round jiffies up to a full second
305 * @j: the time in (absolute) jiffies that should be rounded
306 *
307 * This is the same as round_jiffies() except that it will never
308 * round down. This is useful for timeouts for which the exact time
309 * of firing does not matter too much, as long as they don't fire too
310 * early.
311 */
312unsigned long round_jiffies_up(unsigned long j)
313{
314 return round_jiffies_common(j, raw_smp_processor_id(), true);
315}
316EXPORT_SYMBOL_GPL(round_jiffies_up);
317
318/**
319 * round_jiffies_up_relative - function to round jiffies up to a full second
320 * @j: the time in (relative) jiffies that should be rounded
321 *
322 * This is the same as round_jiffies_relative() except that it will never
323 * round down. This is useful for timeouts for which the exact time
324 * of firing does not matter too much, as long as they don't fire too
325 * early.
326 */
327unsigned long round_jiffies_up_relative(unsigned long j)
328{
329 return __round_jiffies_up_relative(j, raw_smp_processor_id());
330}
331EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
332
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800333/**
334 * set_timer_slack - set the allowed slack for a timer
Randy Dunlap0caa6212010-08-09 16:32:50 -0700335 * @timer: the timer to be modified
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800336 * @slack_hz: the amount of time (in jiffies) allowed for rounding
337 *
338 * Set the amount of time, in jiffies, that a certain timer has
339 * in terms of slack. By setting this value, the timer subsystem
340 * will schedule the actual timer somewhere between
341 * the time mod_timer() asks for, and that time plus the slack.
342 *
343 * By setting the slack to -1, a percentage of the delay is used
344 * instead.
345 */
346void set_timer_slack(struct timer_list *timer, int slack_hz)
347{
348 timer->slack = slack_hz;
349}
350EXPORT_SYMBOL_GPL(set_timer_slack);
351
Paul E. McKenneyd550e812013-12-16 05:57:10 -0800352/*
353 * If the list is empty, catch up ->timer_jiffies to the current time.
354 * The caller must hold the tvec_base lock. Returns true if the list
355 * was empty and therefore ->timer_jiffies was updated.
356 */
357static bool catchup_timer_jiffies(struct tvec_base *base)
358{
359 if (!base->all_timers) {
360 base->timer_jiffies = jiffies;
361 return true;
362 }
363 return false;
364}
365
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000366static void
367__internal_add_timer(struct tvec_base *base, struct timer_list *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 unsigned long expires = timer->expires;
370 unsigned long idx = expires - base->timer_jiffies;
371 struct list_head *vec;
372
373 if (idx < TVR_SIZE) {
374 int i = expires & TVR_MASK;
375 vec = base->tv1.vec + i;
376 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
377 int i = (expires >> TVR_BITS) & TVN_MASK;
378 vec = base->tv2.vec + i;
379 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
380 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
381 vec = base->tv3.vec + i;
382 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
383 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
384 vec = base->tv4.vec + i;
385 } else if ((signed long) idx < 0) {
386 /*
387 * Can happen if you add a timer with expires == jiffies,
388 * or you set a timer to go off in the past
389 */
390 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
391 } else {
392 int i;
Hildner, Christian26cff4e2012-10-08 15:49:03 +0200393 /* If the timeout is larger than MAX_TVAL (on 64-bit
394 * architectures or with CONFIG_BASE_SMALL=1) then we
395 * use the maximum timeout.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
Hildner, Christian26cff4e2012-10-08 15:49:03 +0200397 if (idx > MAX_TVAL) {
398 idx = MAX_TVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 expires = idx + base->timer_jiffies;
400 }
401 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
402 vec = base->tv5.vec + i;
403 }
404 /*
405 * Timers are FIFO:
406 */
407 list_add_tail(&timer->entry, vec);
408}
409
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000410static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
411{
Paul E. McKenney18d8cb62013-12-16 10:41:50 -0800412 (void)catchup_timer_jiffies(base);
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000413 __internal_add_timer(base, timer);
414 /*
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000415 * Update base->active_timers and base->next_timer
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000416 */
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000417 if (!tbase_get_deferrable(timer->base)) {
Oleg Nesterovaea369b2014-01-15 16:19:27 -0800418 if (!base->active_timers++ ||
419 time_before(timer->expires, base->next_timer))
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000420 base->next_timer = timer->expires;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000421 }
Paul E. McKenneyfff42152014-01-14 20:20:43 -0800422 base->all_timers++;
Viresh Kumar9f6d9ba2014-06-22 01:29:14 +0200423
424 /*
425 * Check whether the other CPU is in dynticks mode and needs
426 * to be triggered to reevaluate the timer wheel.
427 * We are protected against the other CPU fiddling
428 * with the timer by holding the timer base lock. This also
429 * makes sure that a CPU on the way to stop its tick can not
430 * evaluate the timer wheel.
431 *
432 * Spare the IPI for deferrable timers on idle targets though.
433 * The next busy ticks will take care of it. Except full dynticks
434 * require special care against races with idle_cpu(), lets deal
435 * with that later.
436 */
437 if (!tbase_get_deferrable(base) || tick_nohz_full_cpu(base->cpu))
438 wake_up_nohz_cpu(base->cpu);
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000439}
440
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800441#ifdef CONFIG_TIMER_STATS
442void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
443{
444 if (timer->start_site)
445 return;
446
447 timer->start_site = addr;
448 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
449 timer->start_pid = current->pid;
450}
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700451
452static void timer_stats_account_timer(struct timer_list *timer)
453{
454 unsigned int flag = 0;
455
Heiko Carstens507e1232009-06-23 17:38:15 +0200456 if (likely(!timer->start_site))
457 return;
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700458 if (unlikely(tbase_get_deferrable(timer->base)))
459 flag |= TIMER_STATS_FLAG_DEFERRABLE;
460
461 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
462 timer->function, timer->start_comm, flag);
463}
464
465#else
466static void timer_stats_account_timer(struct timer_list *timer) {}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800467#endif
468
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700469#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
470
471static struct debug_obj_descr timer_debug_descr;
472
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100473static void *timer_debug_hint(void *addr)
474{
475 return ((struct timer_list *) addr)->function;
476}
477
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700478/*
479 * fixup_init is called when:
480 * - an active object is initialized
481 */
482static int timer_fixup_init(void *addr, enum debug_obj_state state)
483{
484 struct timer_list *timer = addr;
485
486 switch (state) {
487 case ODEBUG_STATE_ACTIVE:
488 del_timer_sync(timer);
489 debug_object_init(timer, &timer_debug_descr);
490 return 1;
491 default:
492 return 0;
493 }
494}
495
Stephen Boydfb16b8c2011-11-07 19:48:26 -0800496/* Stub timer callback for improperly used timers. */
497static void stub_timer(unsigned long data)
498{
499 WARN_ON(1);
500}
501
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700502/*
503 * fixup_activate is called when:
504 * - an active object is activated
505 * - an unknown object is activated (might be a statically initialized object)
506 */
507static int timer_fixup_activate(void *addr, enum debug_obj_state state)
508{
509 struct timer_list *timer = addr;
510
511 switch (state) {
512
513 case ODEBUG_STATE_NOTAVAILABLE:
514 /*
515 * This is not really a fixup. The timer was
516 * statically initialized. We just make sure that it
517 * is tracked in the object tracker.
518 */
519 if (timer->entry.next == NULL &&
520 timer->entry.prev == TIMER_ENTRY_STATIC) {
521 debug_object_init(timer, &timer_debug_descr);
522 debug_object_activate(timer, &timer_debug_descr);
523 return 0;
524 } else {
Stephen Boydfb16b8c2011-11-07 19:48:26 -0800525 setup_timer(timer, stub_timer, 0);
526 return 1;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700527 }
528 return 0;
529
530 case ODEBUG_STATE_ACTIVE:
531 WARN_ON(1);
532
533 default:
534 return 0;
535 }
536}
537
538/*
539 * fixup_free is called when:
540 * - an active object is freed
541 */
542static int timer_fixup_free(void *addr, enum debug_obj_state state)
543{
544 struct timer_list *timer = addr;
545
546 switch (state) {
547 case ODEBUG_STATE_ACTIVE:
548 del_timer_sync(timer);
549 debug_object_free(timer, &timer_debug_descr);
550 return 1;
551 default:
552 return 0;
553 }
554}
555
Christine Chandc4218b2011-11-07 19:48:28 -0800556/*
557 * fixup_assert_init is called when:
558 * - an untracked/uninit-ed object is found
559 */
560static int timer_fixup_assert_init(void *addr, enum debug_obj_state state)
561{
562 struct timer_list *timer = addr;
563
564 switch (state) {
565 case ODEBUG_STATE_NOTAVAILABLE:
566 if (timer->entry.prev == TIMER_ENTRY_STATIC) {
567 /*
568 * This is not really a fixup. The timer was
569 * statically initialized. We just make sure that it
570 * is tracked in the object tracker.
571 */
572 debug_object_init(timer, &timer_debug_descr);
573 return 0;
574 } else {
575 setup_timer(timer, stub_timer, 0);
576 return 1;
577 }
578 default:
579 return 0;
580 }
581}
582
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700583static struct debug_obj_descr timer_debug_descr = {
Christine Chandc4218b2011-11-07 19:48:28 -0800584 .name = "timer_list",
585 .debug_hint = timer_debug_hint,
586 .fixup_init = timer_fixup_init,
587 .fixup_activate = timer_fixup_activate,
588 .fixup_free = timer_fixup_free,
589 .fixup_assert_init = timer_fixup_assert_init,
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700590};
591
592static inline void debug_timer_init(struct timer_list *timer)
593{
594 debug_object_init(timer, &timer_debug_descr);
595}
596
597static inline void debug_timer_activate(struct timer_list *timer)
598{
599 debug_object_activate(timer, &timer_debug_descr);
600}
601
602static inline void debug_timer_deactivate(struct timer_list *timer)
603{
604 debug_object_deactivate(timer, &timer_debug_descr);
605}
606
607static inline void debug_timer_free(struct timer_list *timer)
608{
609 debug_object_free(timer, &timer_debug_descr);
610}
611
Christine Chandc4218b2011-11-07 19:48:28 -0800612static inline void debug_timer_assert_init(struct timer_list *timer)
613{
614 debug_object_assert_init(timer, &timer_debug_descr);
615}
616
Tejun Heofc683992012-08-08 11:10:27 -0700617static void do_init_timer(struct timer_list *timer, unsigned int flags,
618 const char *name, struct lock_class_key *key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700619
Tejun Heofc683992012-08-08 11:10:27 -0700620void init_timer_on_stack_key(struct timer_list *timer, unsigned int flags,
621 const char *name, struct lock_class_key *key)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700622{
623 debug_object_init_on_stack(timer, &timer_debug_descr);
Tejun Heofc683992012-08-08 11:10:27 -0700624 do_init_timer(timer, flags, name, key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700625}
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100626EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700627
628void destroy_timer_on_stack(struct timer_list *timer)
629{
630 debug_object_free(timer, &timer_debug_descr);
631}
632EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
633
634#else
635static inline void debug_timer_init(struct timer_list *timer) { }
636static inline void debug_timer_activate(struct timer_list *timer) { }
637static inline void debug_timer_deactivate(struct timer_list *timer) { }
Christine Chandc4218b2011-11-07 19:48:28 -0800638static inline void debug_timer_assert_init(struct timer_list *timer) { }
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700639#endif
640
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800641static inline void debug_init(struct timer_list *timer)
642{
643 debug_timer_init(timer);
644 trace_timer_init(timer);
645}
646
647static inline void
648debug_activate(struct timer_list *timer, unsigned long expires)
649{
650 debug_timer_activate(timer);
651 trace_timer_start(timer, expires);
652}
653
654static inline void debug_deactivate(struct timer_list *timer)
655{
656 debug_timer_deactivate(timer);
657 trace_timer_cancel(timer);
658}
659
Christine Chandc4218b2011-11-07 19:48:28 -0800660static inline void debug_assert_init(struct timer_list *timer)
661{
662 debug_timer_assert_init(timer);
663}
664
Tejun Heofc683992012-08-08 11:10:27 -0700665static void do_init_timer(struct timer_list *timer, unsigned int flags,
666 const char *name, struct lock_class_key *key)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700667{
Christoph Lameter22127e92014-08-17 12:30:25 -0500668 struct tvec_base *base = raw_cpu_read(tvec_bases);
Tejun Heofc683992012-08-08 11:10:27 -0700669
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700670 timer->entry.next = NULL;
Tejun Heofc683992012-08-08 11:10:27 -0700671 timer->base = (void *)((unsigned long)base | flags);
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800672 timer->slack = -1;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700673#ifdef CONFIG_TIMER_STATS
674 timer->start_site = NULL;
675 timer->start_pid = -1;
676 memset(timer->start_comm, 0, TASK_COMM_LEN);
677#endif
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100678 lockdep_init_map(&timer->lockdep_map, name, key, 0);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700679}
680
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700681/**
Randy Dunlap633fe792009-04-01 17:47:23 -0700682 * init_timer_key - initialize a timer
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700683 * @timer: the timer to be initialized
Tejun Heofc683992012-08-08 11:10:27 -0700684 * @flags: timer flags
Randy Dunlap633fe792009-04-01 17:47:23 -0700685 * @name: name of the timer
686 * @key: lockdep class key of the fake lock used for tracking timer
687 * sync lock dependencies
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700688 *
Randy Dunlap633fe792009-04-01 17:47:23 -0700689 * init_timer_key() must be done to a timer prior calling *any* of the
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700690 * other timer functions.
691 */
Tejun Heofc683992012-08-08 11:10:27 -0700692void init_timer_key(struct timer_list *timer, unsigned int flags,
693 const char *name, struct lock_class_key *key)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700694{
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800695 debug_init(timer);
Tejun Heofc683992012-08-08 11:10:27 -0700696 do_init_timer(timer, flags, name, key);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700697}
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100698EXPORT_SYMBOL(init_timer_key);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700699
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000700static inline void detach_timer(struct timer_list *timer, bool clear_pending)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700701{
702 struct list_head *entry = &timer->entry;
703
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800704 debug_deactivate(timer);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700705
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700706 __list_del(entry->prev, entry->next);
707 if (clear_pending)
708 entry->next = NULL;
709 entry->prev = LIST_POISON2;
710}
711
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000712static inline void
713detach_expired_timer(struct timer_list *timer, struct tvec_base *base)
714{
715 detach_timer(timer, true);
716 if (!tbase_get_deferrable(timer->base))
Tejun Heoe52b1db2012-08-08 11:10:25 -0700717 base->active_timers--;
Paul E. McKenneyfff42152014-01-14 20:20:43 -0800718 base->all_timers--;
Paul E. McKenney16d937f2013-12-16 10:32:01 -0800719 (void)catchup_timer_jiffies(base);
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000720}
721
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000722static int detach_if_pending(struct timer_list *timer, struct tvec_base *base,
723 bool clear_pending)
724{
725 if (!timer_pending(timer))
726 return 0;
727
728 detach_timer(timer, clear_pending);
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000729 if (!tbase_get_deferrable(timer->base)) {
Tejun Heoe52b1db2012-08-08 11:10:25 -0700730 base->active_timers--;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000731 if (timer->expires == base->next_timer)
732 base->next_timer = base->timer_jiffies;
733 }
Paul E. McKenneyfff42152014-01-14 20:20:43 -0800734 base->all_timers--;
Paul E. McKenney16d937f2013-12-16 10:32:01 -0800735 (void)catchup_timer_jiffies(base);
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000736 return 1;
737}
738
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700739/*
Oleg Nesterov3691c512006-03-31 02:30:30 -0800740 * We are using hashed locking: holding per_cpu(tvec_bases).lock
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700741 * means that all timers which are tied to this base via timer->base are
742 * locked, and the base itself is locked too.
743 *
744 * So __run_timers/migrate_timers can safely modify all timers which could
745 * be found on ->tvX lists.
746 *
747 * When the timer's base is locked, and the timer removed from list, it is
748 * possible to set timer->base = NULL and drop the lock: the timer remains
749 * locked.
750 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100751static struct tvec_base *lock_timer_base(struct timer_list *timer,
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700752 unsigned long *flags)
Josh Triplett89e7e3742006-09-29 01:59:36 -0700753 __acquires(timer->base->lock)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700754{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100755 struct tvec_base *base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700756
757 for (;;) {
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100758 struct tvec_base *prelock_base = timer->base;
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700759 base = tbase_get_base(prelock_base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700760 if (likely(base != NULL)) {
761 spin_lock_irqsave(&base->lock, *flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700762 if (likely(prelock_base == timer->base))
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700763 return base;
764 /* The timer has migrated to another CPU */
765 spin_unlock_irqrestore(&base->lock, *flags);
766 }
767 cpu_relax();
768 }
769}
770
Ingo Molnar74019222009-02-18 12:23:29 +0100771static inline int
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530772__mod_timer(struct timer_list *timer, unsigned long expires,
773 bool pending_only, int pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100775 struct tvec_base *base, *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 unsigned long flags;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530777 int ret = 0 , cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800779 timer_stats_timer_set_start_info(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 BUG_ON(!timer->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700782 base = lock_timer_base(timer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000784 ret = detach_if_pending(timer, base, false);
785 if (!ret && pending_only)
786 goto out_unlock;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700787
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800788 debug_activate(timer, expires);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700789
Viresh Kumar6201b4d2014-03-18 16:26:07 +0530790 cpu = get_nohz_timer_target(pinned);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530791 new_base = per_cpu(tvec_bases, cpu);
792
Oleg Nesterov3691c512006-03-31 02:30:30 -0800793 if (base != new_base) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700794 /*
795 * We are trying to schedule the timer on the local CPU.
796 * However we can't change timer's base while it is running,
797 * otherwise del_timer_sync() can't detect that the timer's
798 * handler yet has not finished. This also guarantees that
799 * the timer is serialized wrt itself.
800 */
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800801 if (likely(base->running_timer != timer)) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700802 /* See the comment in lock_timer_base() */
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700803 timer_set_base(timer, NULL);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700804 spin_unlock(&base->lock);
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800805 base = new_base;
806 spin_lock(&base->lock);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700807 timer_set_base(timer, base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700808 }
809 }
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 timer->expires = expires;
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800812 internal_add_timer(base, timer);
Ingo Molnar74019222009-02-18 12:23:29 +0100813
814out_unlock:
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800815 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 return ret;
818}
819
Ingo Molnar74019222009-02-18 12:23:29 +0100820/**
821 * mod_timer_pending - modify a pending timer's timeout
822 * @timer: the pending timer to be modified
823 * @expires: new timeout in jiffies
824 *
825 * mod_timer_pending() is the same for pending timers as mod_timer(),
826 * but will not re-activate and modify already deleted timers.
827 *
828 * It is useful for unserialized use of timers.
829 */
830int mod_timer_pending(struct timer_list *timer, unsigned long expires)
831{
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530832 return __mod_timer(timer, expires, true, TIMER_NOT_PINNED);
Ingo Molnar74019222009-02-18 12:23:29 +0100833}
834EXPORT_SYMBOL(mod_timer_pending);
835
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800836/*
837 * Decide where to put the timer while taking the slack into account
838 *
839 * Algorithm:
840 * 1) calculate the maximum (absolute) time
841 * 2) calculate the highest bit where the expires and new max are different
842 * 3) use this bit to make a mask
843 * 4) use the bitmask to round down the maximum time, so that all last
844 * bits are zeros
845 */
846static inline
847unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
848{
849 unsigned long expires_limit, mask;
850 int bit;
851
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200852 if (timer->slack >= 0) {
Jeff Chuaf00e0472010-05-24 07:16:24 +0800853 expires_limit = expires + timer->slack;
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200854 } else {
Sebastian Andrzej Siewior1c3cc112011-05-21 12:58:28 +0200855 long delta = expires - jiffies;
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800856
Sebastian Andrzej Siewior1c3cc112011-05-21 12:58:28 +0200857 if (delta < 256)
858 return expires;
859
860 expires_limit = expires + delta / 256;
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200861 }
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800862 mask = expires ^ expires_limit;
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800863 if (mask == 0)
864 return expires;
865
866 bit = find_last_bit(&mask, BITS_PER_LONG);
867
Jiri Bohac98a01e72014-04-18 17:23:11 +0200868 mask = (1UL << bit) - 1;
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800869
870 expires_limit = expires_limit & ~(mask);
871
872 return expires_limit;
873}
874
Ingo Molnar74019222009-02-18 12:23:29 +0100875/**
876 * mod_timer - modify a timer's timeout
877 * @timer: the timer to be modified
878 * @expires: new timeout in jiffies
879 *
880 * mod_timer() is a more efficient way to update the expire field of an
881 * active timer (if the timer is inactive it will be activated)
882 *
883 * mod_timer(timer, expires) is equivalent to:
884 *
885 * del_timer(timer); timer->expires = expires; add_timer(timer);
886 *
887 * Note that if there are multiple unserialized concurrent users of the
888 * same timer, then mod_timer() is the only safe way to modify the timeout,
889 * since add_timer() cannot modify an already running timer.
890 *
891 * The function returns whether it has modified a pending timer or not.
892 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
893 * active timer returns 1.)
894 */
895int mod_timer(struct timer_list *timer, unsigned long expires)
896{
Sebastian Andrzej Siewior1c3cc112011-05-21 12:58:28 +0200897 expires = apply_slack(timer, expires);
898
Ingo Molnar74019222009-02-18 12:23:29 +0100899 /*
900 * This is a common optimization triggered by the
901 * networking code - if the timer is re-modified
902 * to be the same thing then just return:
903 */
Pavel Roskin48411582009-07-18 16:46:02 -0400904 if (timer_pending(timer) && timer->expires == expires)
Ingo Molnar74019222009-02-18 12:23:29 +0100905 return 1;
906
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530907 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
Ingo Molnar74019222009-02-18 12:23:29 +0100908}
909EXPORT_SYMBOL(mod_timer);
910
911/**
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530912 * mod_timer_pinned - modify a timer's timeout
913 * @timer: the timer to be modified
914 * @expires: new timeout in jiffies
915 *
916 * mod_timer_pinned() is a way to update the expire field of an
917 * active timer (if the timer is inactive it will be activated)
Paul E. McKenney048a0e82012-04-26 10:52:27 -0700918 * and to ensure that the timer is scheduled on the current CPU.
919 *
920 * Note that this does not prevent the timer from being migrated
921 * when the current CPU goes offline. If this is a problem for
922 * you, use CPU-hotplug notifiers to handle it correctly, for
923 * example, cancelling the timer when the corresponding CPU goes
924 * offline.
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530925 *
926 * mod_timer_pinned(timer, expires) is equivalent to:
927 *
928 * del_timer(timer); timer->expires = expires; add_timer(timer);
929 */
930int mod_timer_pinned(struct timer_list *timer, unsigned long expires)
931{
932 if (timer->expires == expires && timer_pending(timer))
933 return 1;
934
935 return __mod_timer(timer, expires, false, TIMER_PINNED);
936}
937EXPORT_SYMBOL(mod_timer_pinned);
938
939/**
Ingo Molnar74019222009-02-18 12:23:29 +0100940 * add_timer - start a timer
941 * @timer: the timer to be added
942 *
943 * The kernel will do a ->function(->data) callback from the
944 * timer interrupt at the ->expires point in the future. The
945 * current time is 'jiffies'.
946 *
947 * The timer's ->expires, ->function (and if the handler uses it, ->data)
948 * fields must be set prior calling this function.
949 *
950 * Timers with an ->expires field in the past will be executed in the next
951 * timer tick.
952 */
953void add_timer(struct timer_list *timer)
954{
955 BUG_ON(timer_pending(timer));
956 mod_timer(timer, timer->expires);
957}
958EXPORT_SYMBOL(add_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700960/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * add_timer_on - start a timer on a particular CPU
962 * @timer: the timer to be added
963 * @cpu: the CPU to start it on
964 *
965 * This is not very scalable on SMP. Double adds are not possible.
966 */
967void add_timer_on(struct timer_list *timer, int cpu)
968{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100969 struct tvec_base *base = per_cpu(tvec_bases, cpu);
Thomas Gleixner68194572007-07-19 01:49:16 -0700970 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700971
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800972 timer_stats_timer_set_start_info(timer);
Thomas Gleixner68194572007-07-19 01:49:16 -0700973 BUG_ON(timer_pending(timer) || !timer->function);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800974 spin_lock_irqsave(&base->lock, flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700975 timer_set_base(timer, base);
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800976 debug_activate(timer, timer->expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 internal_add_timer(base, timer);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800978 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
Andi Kleena9862e02009-05-19 22:49:07 +0200980EXPORT_SYMBOL_GPL(add_timer_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700982/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * del_timer - deactive a timer.
984 * @timer: the timer to be deactivated
985 *
986 * del_timer() deactivates a timer - this works on both active and inactive
987 * timers.
988 *
989 * The function returns whether it has deactivated a pending timer or not.
990 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
991 * active timer returns 1.)
992 */
993int del_timer(struct timer_list *timer)
994{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100995 struct tvec_base *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700997 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Christine Chandc4218b2011-11-07 19:48:28 -0800999 debug_assert_init(timer);
1000
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001001 timer_stats_timer_clear_start_info(timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001002 if (timer_pending(timer)) {
1003 base = lock_timer_base(timer, &flags);
Thomas Gleixnerec44bc72012-05-25 22:08:57 +00001004 ret = detach_if_pending(timer, base, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001008 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010EXPORT_SYMBOL(del_timer);
1011
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001012/**
1013 * try_to_del_timer_sync - Try to deactivate a timer
1014 * @timer: timer do del
1015 *
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001016 * This function tries to deactivate a timer. Upon successful (ret >= 0)
1017 * exit the timer is not queued and the handler is not running on any CPU.
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001018 */
1019int try_to_del_timer_sync(struct timer_list *timer)
1020{
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001021 struct tvec_base *base;
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001022 unsigned long flags;
1023 int ret = -1;
1024
Christine Chandc4218b2011-11-07 19:48:28 -08001025 debug_assert_init(timer);
1026
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001027 base = lock_timer_base(timer, &flags);
1028
Thomas Gleixnerec44bc72012-05-25 22:08:57 +00001029 if (base->running_timer != timer) {
1030 timer_stats_timer_clear_start_info(timer);
1031 ret = detach_if_pending(timer, base, true);
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001032 }
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001033 spin_unlock_irqrestore(&base->lock, flags);
1034
1035 return ret;
1036}
David Howellse19dff12007-04-26 15:46:56 -07001037EXPORT_SYMBOL(try_to_del_timer_sync);
1038
Yong Zhang6f1bc452010-10-20 15:57:31 -07001039#ifdef CONFIG_SMP
Peter Zijlstra3650b572015-03-31 20:49:02 +05301040static DEFINE_PER_CPU(struct tvec_base, __tvec_bases);
1041
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001042/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 * del_timer_sync - deactivate a timer and wait for the handler to finish.
1044 * @timer: the timer to be deactivated
1045 *
1046 * This function only differs from del_timer() on SMP: besides deactivating
1047 * the timer it also makes sure the handler has finished executing on other
1048 * CPUs.
1049 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001050 * Synchronization rules: Callers must prevent restarting of the timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 * otherwise this function is meaningless. It must not be called from
Tejun Heoc5f66e92012-08-08 11:10:28 -07001052 * interrupt contexts unless the timer is an irqsafe one. The caller must
1053 * not hold locks which would prevent completion of the timer's
1054 * handler. The timer's handler must not call add_timer_on(). Upon exit the
1055 * timer is not queued and the handler is not running on any CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 *
Tejun Heoc5f66e92012-08-08 11:10:28 -07001057 * Note: For !irqsafe timers, you must not hold locks that are held in
1058 * interrupt context while calling this function. Even if the lock has
1059 * nothing to do with the timer in question. Here's why:
Steven Rostedt48228f72011-02-08 12:39:54 -05001060 *
1061 * CPU0 CPU1
1062 * ---- ----
1063 * <SOFTIRQ>
1064 * call_timer_fn();
1065 * base->running_timer = mytimer;
1066 * spin_lock_irq(somelock);
1067 * <IRQ>
1068 * spin_lock(somelock);
1069 * del_timer_sync(mytimer);
1070 * while (base->running_timer == mytimer);
1071 *
1072 * Now del_timer_sync() will never return and never release somelock.
1073 * The interrupt on the other CPU is waiting to grab somelock but
1074 * it has interrupted the softirq that CPU0 is waiting to finish.
1075 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * The function returns whether it has deactivated a pending timer or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 */
1078int del_timer_sync(struct timer_list *timer)
1079{
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001080#ifdef CONFIG_LOCKDEP
Peter Zijlstraf266a512011-02-03 15:09:41 +01001081 unsigned long flags;
1082
Steven Rostedt48228f72011-02-08 12:39:54 -05001083 /*
1084 * If lockdep gives a backtrace here, please reference
1085 * the synchronization rules above.
1086 */
Peter Zijlstra7ff20792011-02-08 15:18:00 +01001087 local_irq_save(flags);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001088 lock_map_acquire(&timer->lockdep_map);
1089 lock_map_release(&timer->lockdep_map);
Peter Zijlstra7ff20792011-02-08 15:18:00 +01001090 local_irq_restore(flags);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001091#endif
Yong Zhang466bd302010-10-20 15:57:33 -07001092 /*
1093 * don't use it in hardirq context, because it
1094 * could lead to deadlock.
1095 */
Tejun Heoc5f66e92012-08-08 11:10:28 -07001096 WARN_ON(in_irq() && !tbase_get_irqsafe(timer->base));
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001097 for (;;) {
1098 int ret = try_to_del_timer_sync(timer);
1099 if (ret >= 0)
1100 return ret;
Andrew Mortona0009652006-07-14 00:24:06 -07001101 cpu_relax();
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104EXPORT_SYMBOL(del_timer_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105#endif
1106
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001107static int cascade(struct tvec_base *base, struct tvec *tv, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 /* cascade all the timers from tv up one level */
Porpoise3439dd82006-06-23 02:05:56 -07001110 struct timer_list *timer, *tmp;
1111 struct list_head tv_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Porpoise3439dd82006-06-23 02:05:56 -07001113 list_replace_init(tv->vec + index, &tv_list);
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 /*
Porpoise3439dd82006-06-23 02:05:56 -07001116 * We are removing _all_ timers from the list, so we
1117 * don't have to detach them individually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
Porpoise3439dd82006-06-23 02:05:56 -07001119 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001120 BUG_ON(tbase_get_base(timer->base) != base);
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +00001121 /* No accounting, while moving them */
1122 __internal_add_timer(base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 return index;
1126}
1127
Thomas Gleixner576da122010-03-12 21:10:29 +01001128static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
1129 unsigned long data)
1130{
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001131 int count = preempt_count();
Thomas Gleixner576da122010-03-12 21:10:29 +01001132
1133#ifdef CONFIG_LOCKDEP
1134 /*
1135 * It is permissible to free the timer from inside the
1136 * function that is called from it, this we need to take into
1137 * account for lockdep too. To avoid bogus "held lock freed"
1138 * warnings as well as problems when looking into
1139 * timer->lockdep_map, make a copy and use that here.
1140 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001141 struct lockdep_map lockdep_map;
1142
1143 lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
Thomas Gleixner576da122010-03-12 21:10:29 +01001144#endif
1145 /*
1146 * Couple the lock chain with the lock chain at
1147 * del_timer_sync() by acquiring the lock_map around the fn()
1148 * call here and in del_timer_sync().
1149 */
1150 lock_map_acquire(&lockdep_map);
1151
1152 trace_timer_expire_entry(timer);
1153 fn(data);
1154 trace_timer_expire_exit(timer);
1155
1156 lock_map_release(&lockdep_map);
1157
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001158 if (count != preempt_count()) {
Thomas Gleixner802702e2010-03-12 20:13:23 +01001159 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001160 fn, count, preempt_count());
Thomas Gleixner802702e2010-03-12 20:13:23 +01001161 /*
1162 * Restore the preempt count. That gives us a decent
1163 * chance to survive and extract information. If the
1164 * callback kept a lock held, bad luck, but not worse
1165 * than the BUG() we had.
1166 */
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001167 preempt_count_set(count);
Thomas Gleixner576da122010-03-12 21:10:29 +01001168 }
1169}
1170
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001171#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
1172
1173/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 * __run_timers - run all expired timers (if any) on this CPU.
1175 * @base: the timer vector to be processed.
1176 *
1177 * This function cascades all vectors and executes all expired timer
1178 * vectors.
1179 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001180static inline void __run_timers(struct tvec_base *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 struct timer_list *timer;
1183
Oleg Nesterov3691c512006-03-31 02:30:30 -08001184 spin_lock_irq(&base->lock);
Paul E. McKenneyd550e812013-12-16 05:57:10 -08001185 if (catchup_timer_jiffies(base)) {
1186 spin_unlock_irq(&base->lock);
1187 return;
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 while (time_after_eq(jiffies, base->timer_jiffies)) {
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001190 struct list_head work_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 struct list_head *head = &work_list;
Thomas Gleixner68194572007-07-19 01:49:16 -07001192 int index = base->timer_jiffies & TVR_MASK;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /*
1195 * Cascade timers:
1196 */
1197 if (!index &&
1198 (!cascade(base, &base->tv2, INDEX(0))) &&
1199 (!cascade(base, &base->tv3, INDEX(1))) &&
1200 !cascade(base, &base->tv4, INDEX(2)))
1201 cascade(base, &base->tv5, INDEX(3));
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001202 ++base->timer_jiffies;
Viresh Kumarc41eba72014-03-18 13:23:15 +05301203 list_replace_init(base->tv1.vec + index, head);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001204 while (!list_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 void (*fn)(unsigned long);
1206 unsigned long data;
Tejun Heoc5f66e92012-08-08 11:10:28 -07001207 bool irqsafe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001209 timer = list_first_entry(head, struct timer_list,entry);
Thomas Gleixner68194572007-07-19 01:49:16 -07001210 fn = timer->function;
1211 data = timer->data;
Tejun Heoc5f66e92012-08-08 11:10:28 -07001212 irqsafe = tbase_get_irqsafe(timer->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001214 timer_stats_account_timer(timer);
1215
Yong Zhang6f1bc452010-10-20 15:57:31 -07001216 base->running_timer = timer;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +00001217 detach_expired_timer(timer, base);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001218
Tejun Heoc5f66e92012-08-08 11:10:28 -07001219 if (irqsafe) {
1220 spin_unlock(&base->lock);
1221 call_timer_fn(timer, fn, data);
1222 spin_lock(&base->lock);
1223 } else {
1224 spin_unlock_irq(&base->lock);
1225 call_timer_fn(timer, fn, data);
1226 spin_lock_irq(&base->lock);
1227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229 }
Yong Zhang6f1bc452010-10-20 15:57:31 -07001230 base->running_timer = NULL;
Oleg Nesterov3691c512006-03-31 02:30:30 -08001231 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001234#ifdef CONFIG_NO_HZ_COMMON
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235/*
1236 * Find out when the next timer event is due to happen. This
Randy Dunlap90cba642009-08-25 14:35:41 -07001237 * is used on S/390 to stop all activity when a CPU is idle.
1238 * This function needs to be called with interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001240static unsigned long __next_timer_interrupt(struct tvec_base *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001242 unsigned long timer_jiffies = base->timer_jiffies;
Thomas Gleixnereaad0842007-05-29 23:47:39 +02001243 unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001244 int index, slot, array, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 struct timer_list *nte;
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001246 struct tvec *varray[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 /* Look for timer events in tv1. */
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001249 index = slot = timer_jiffies & TVR_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001251 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
Thomas Gleixner68194572007-07-19 01:49:16 -07001252 if (tbase_get_deferrable(nte->base))
1253 continue;
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001254
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001255 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001257 /* Look at the cascade bucket(s)? */
1258 if (!index || slot < index)
1259 goto cascade;
1260 return expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001262 slot = (slot + 1) & TVR_MASK;
1263 } while (slot != index);
1264
1265cascade:
1266 /* Calculate the next cascade event */
1267 if (index)
1268 timer_jiffies += TVR_SIZE - index;
1269 timer_jiffies >>= TVR_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* Check tv2-tv5. */
1272 varray[0] = &base->tv2;
1273 varray[1] = &base->tv3;
1274 varray[2] = &base->tv4;
1275 varray[3] = &base->tv5;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001276
1277 for (array = 0; array < 4; array++) {
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001278 struct tvec *varp = varray[array];
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001279
1280 index = slot = timer_jiffies & TVN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001282 list_for_each_entry(nte, varp->vec + slot, entry) {
Jon Huntera0419882009-05-01 13:10:23 -07001283 if (tbase_get_deferrable(nte->base))
1284 continue;
1285
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001286 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (time_before(nte->expires, expires))
1288 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001289 }
1290 /*
1291 * Do we still search for the first timer or are
1292 * we looking up the cascade buckets ?
1293 */
1294 if (found) {
1295 /* Look at the cascade bucket(s)? */
1296 if (!index || slot < index)
1297 break;
1298 return expires;
1299 }
1300 slot = (slot + 1) & TVN_MASK;
1301 } while (slot != index);
1302
1303 if (index)
1304 timer_jiffies += TVN_SIZE - index;
1305 timer_jiffies >>= TVN_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001307 return expires;
1308}
1309
1310/*
1311 * Check, if the next hrtimer event is before the next timer wheel
1312 * event:
1313 */
1314static unsigned long cmp_next_hrtimer_event(unsigned long now,
1315 unsigned long expires)
1316{
1317 ktime_t hr_delta = hrtimer_get_next_event();
1318 struct timespec tsdelta;
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001319 unsigned long delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001320
1321 if (hr_delta.tv64 == KTIME_MAX)
1322 return expires;
1323
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001324 /*
1325 * Expired timer available, let it expire in the next tick
1326 */
1327 if (hr_delta.tv64 <= 0)
1328 return now + 1;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001329
1330 tsdelta = ktime_to_timespec(hr_delta);
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001331 delta = timespec_to_jiffies(&tsdelta);
Thomas Gleixnereaad0842007-05-29 23:47:39 +02001332
1333 /*
1334 * Limit the delta to the max value, which is checked in
1335 * tick_nohz_stop_sched_tick():
1336 */
1337 if (delta > NEXT_TIMER_MAX_DELTA)
1338 delta = NEXT_TIMER_MAX_DELTA;
1339
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001340 /*
1341 * Take rounding errors in to account and make sure, that it
1342 * expires in the next tick. Otherwise we go into an endless
1343 * ping pong due to tick_nohz_stop_sched_tick() retriggering
1344 * the timer softirq
1345 */
1346 if (delta < 1)
1347 delta = 1;
1348 now += delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001349 if (time_before(now, expires))
1350 return now;
1351 return expires;
1352}
1353
1354/**
Li Zefan8dce39c2007-11-05 14:51:10 -08001355 * get_next_timer_interrupt - return the jiffy of the next pending timer
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08001356 * @now: current time (in jiffies)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001357 */
Thomas Gleixnerfd064b92007-02-16 01:27:47 -08001358unsigned long get_next_timer_interrupt(unsigned long now)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001359{
Christoph Lameter74963512010-11-30 14:05:53 -06001360 struct tvec_base *base = __this_cpu_read(tvec_bases);
Thomas Gleixnere40468a2012-05-25 22:08:59 +00001361 unsigned long expires = now + NEXT_TIMER_MAX_DELTA;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001362
Heiko Carstensdbd87b52010-12-01 10:11:09 +01001363 /*
1364 * Pretend that there is no timer pending if the cpu is offline.
1365 * Possible pending timers will be migrated later to an active cpu.
1366 */
1367 if (cpu_is_offline(smp_processor_id()))
Thomas Gleixnere40468a2012-05-25 22:08:59 +00001368 return expires;
1369
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001370 spin_lock(&base->lock);
Thomas Gleixnere40468a2012-05-25 22:08:59 +00001371 if (base->active_timers) {
1372 if (time_before_eq(base->next_timer, base->timer_jiffies))
1373 base->next_timer = __next_timer_interrupt(base);
1374 expires = base->next_timer;
1375 }
Oleg Nesterov3691c512006-03-31 02:30:30 -08001376 spin_unlock(&base->lock);
Tony Lindgren69239742006-03-06 15:42:45 -08001377
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001378 if (time_before_eq(expires, now))
1379 return now;
Zachary Amsden0662b712006-05-20 15:00:24 -07001380
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001381 return cmp_next_hrtimer_event(now, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383#endif
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385/*
Daniel Walker5b4db0c2007-10-18 03:06:11 -07001386 * Called from the timer interrupt handler to charge one tick to the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * process. user_tick is 1 if the tick is user time, 0 for system.
1388 */
1389void update_process_times(int user_tick)
1390{
1391 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 /* Note: this timer irq context must be accounted for as well. */
Paul Mackerrasfa13a5a2007-11-09 22:39:38 +01001394 account_process_tick(p, user_tick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 run_local_timers();
Paul E. McKenneyc3377c2d2014-10-21 07:53:02 -07001396 rcu_check_callbacks(user_tick);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08001397#ifdef CONFIG_IRQ_WORK
1398 if (in_irq())
Frederic Weisbecker76a33062014-08-16 18:37:19 +02001399 irq_work_tick();
Peter Zijlstrae360adb2010-10-14 14:01:34 +08001400#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 scheduler_tick();
Thomas Gleixner68194572007-07-19 01:49:16 -07001402 run_posix_cpu_timers(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
1405/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 * This function runs timers and the timer-tq in bottom half context.
1407 */
1408static void run_timer_softirq(struct softirq_action *h)
1409{
Christoph Lameter74963512010-11-30 14:05:53 -06001410 struct tvec_base *base = __this_cpu_read(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001412 hrtimer_run_pending();
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (time_after_eq(jiffies, base->timer_jiffies))
1415 __run_timers(base);
1416}
1417
1418/*
1419 * Called by the local, per-CPU timer interrupt on SMP.
1420 */
1421void run_local_timers(void)
1422{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001423 hrtimer_run_queues();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 raise_softirq(TIMER_SOFTIRQ);
1425}
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427#ifdef __ARCH_WANT_SYS_ALARM
1428
1429/*
1430 * For backwards compatibility? This can be done in libc so Alpha
1431 * and all newer ports shouldn't need it.
1432 */
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001433SYSCALL_DEFINE1(alarm, unsigned int, seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -08001435 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
1438#endif
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440static void process_timeout(unsigned long __data)
1441{
Ingo Molnar36c8b582006-07-03 00:25:41 -07001442 wake_up_process((struct task_struct *)__data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
1445/**
1446 * schedule_timeout - sleep until timeout
1447 * @timeout: timeout value in jiffies
1448 *
1449 * Make the current task sleep until @timeout jiffies have
1450 * elapsed. The routine will return immediately unless
1451 * the current task state has been set (see set_current_state()).
1452 *
1453 * You can set the task state as follows -
1454 *
1455 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1456 * pass before the routine returns. The routine will return 0
1457 *
1458 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1459 * delivered to the current task. In this case the remaining time
1460 * in jiffies will be returned, or 0 if the timer expired in time
1461 *
1462 * The current task state is guaranteed to be TASK_RUNNING when this
1463 * routine returns.
1464 *
1465 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1466 * the CPU away without a bound on the timeout. In this case the return
1467 * value will be %MAX_SCHEDULE_TIMEOUT.
1468 *
1469 * In all cases the return value is guaranteed to be non-negative.
1470 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001471signed long __sched schedule_timeout(signed long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473 struct timer_list timer;
1474 unsigned long expire;
1475
1476 switch (timeout)
1477 {
1478 case MAX_SCHEDULE_TIMEOUT:
1479 /*
1480 * These two special cases are useful to be comfortable
1481 * in the caller. Nothing more. We could take
1482 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1483 * but I' d like to return a valid offset (>=0) to allow
1484 * the caller to do everything it want with the retval.
1485 */
1486 schedule();
1487 goto out;
1488 default:
1489 /*
1490 * Another bit of PARANOID. Note that the retval will be
1491 * 0 since no piece of kernel is supposed to do a check
1492 * for a negative retval of schedule_timeout() (since it
1493 * should never happens anyway). You just have the printk()
1494 * that will tell you if something is gone wrong and where.
1495 */
Andrew Morton5b149bc2006-12-22 01:10:14 -08001496 if (timeout < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 printk(KERN_ERR "schedule_timeout: wrong timeout "
Andrew Morton5b149bc2006-12-22 01:10:14 -08001498 "value %lx\n", timeout);
1499 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 current->state = TASK_RUNNING;
1501 goto out;
1502 }
1503 }
1504
1505 expire = timeout + jiffies;
1506
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001507 setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
Arun R Bharadwaj597d0272009-04-16 12:13:26 +05301508 __mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 schedule();
1510 del_singleshot_timer_sync(&timer);
1511
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001512 /* Remove the timer from the object tracker */
1513 destroy_timer_on_stack(&timer);
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 timeout = expire - jiffies;
1516
1517 out:
1518 return timeout < 0 ? 0 : timeout;
1519}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520EXPORT_SYMBOL(schedule_timeout);
1521
Andrew Morton8a1c1752005-09-13 01:25:15 -07001522/*
1523 * We can use __set_current_state() here because schedule_timeout() calls
1524 * schedule() unconditionally.
1525 */
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001526signed long __sched schedule_timeout_interruptible(signed long timeout)
1527{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001528 __set_current_state(TASK_INTERRUPTIBLE);
1529 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001530}
1531EXPORT_SYMBOL(schedule_timeout_interruptible);
1532
Matthew Wilcox294d5cc2007-12-06 11:59:46 -05001533signed long __sched schedule_timeout_killable(signed long timeout)
1534{
1535 __set_current_state(TASK_KILLABLE);
1536 return schedule_timeout(timeout);
1537}
1538EXPORT_SYMBOL(schedule_timeout_killable);
1539
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001540signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1541{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001542 __set_current_state(TASK_UNINTERRUPTIBLE);
1543 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001544}
1545EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547#ifdef CONFIG_HOTPLUG_CPU
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001548static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 struct timer_list *timer;
1551
1552 while (!list_empty(head)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001553 timer = list_first_entry(head, struct timer_list, entry);
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +00001554 /* We ignore the accounting on the dying cpu */
Thomas Gleixnerec44bc72012-05-25 22:08:57 +00001555 detach_timer(timer, false);
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001556 timer_set_base(timer, new_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 internal_add_timer(new_base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Paul Gortmaker0db06282013-06-19 14:53:51 -04001561static void migrate_timers(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001563 struct tvec_base *old_base;
1564 struct tvec_base *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 int i;
1566
1567 BUG_ON(cpu_online(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001568 old_base = per_cpu(tvec_bases, cpu);
1569 new_base = get_cpu_var(tvec_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001570 /*
1571 * The caller is globally serialized and nobody else
1572 * takes two locks at once, deadlock is not possible.
1573 */
1574 spin_lock_irq(&new_base->lock);
Oleg Nesterov0d180402008-04-04 20:54:10 +02001575 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Oleg Nesterov3691c512006-03-31 02:30:30 -08001577 BUG_ON(old_base->running_timer);
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 for (i = 0; i < TVR_SIZE; i++)
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001580 migrate_timer_list(new_base, old_base->tv1.vec + i);
1581 for (i = 0; i < TVN_SIZE; i++) {
1582 migrate_timer_list(new_base, old_base->tv2.vec + i);
1583 migrate_timer_list(new_base, old_base->tv3.vec + i);
1584 migrate_timer_list(new_base, old_base->tv4.vec + i);
1585 migrate_timer_list(new_base, old_base->tv5.vec + i);
1586 }
1587
Viresh Kumar8def9062015-03-31 20:49:01 +05301588 old_base->active_timers = 0;
1589 old_base->all_timers = 0;
1590
Oleg Nesterov0d180402008-04-04 20:54:10 +02001591 spin_unlock(&old_base->lock);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001592 spin_unlock_irq(&new_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 put_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Paul Gortmaker0db06282013-06-19 14:53:51 -04001596static int timer_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 unsigned long action, void *hcpu)
1598{
Viresh Kumar8def9062015-03-31 20:49:01 +05301599 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001601 case CPU_DEAD_FROZEN:
Viresh Kumar8def9062015-03-31 20:49:01 +05301602 migrate_timers((long)hcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 default:
1605 break;
1606 }
Peter Zijlstra3650b572015-03-31 20:49:02 +05301607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return NOTIFY_OK;
1609}
1610
Peter Zijlstra3650b572015-03-31 20:49:02 +05301611static inline void timer_register_cpu_notifier(void)
1612{
1613 cpu_notifier(timer_cpu_notify, 0);
1614}
1615#else
1616static inline void timer_register_cpu_notifier(void) { }
1617#endif /* CONFIG_HOTPLUG_CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Viresh Kumar8def9062015-03-31 20:49:01 +05301619static void __init init_timer_cpu(struct tvec_base *base, int cpu)
1620{
1621 int j;
1622
Peter Zijlstra3650b572015-03-31 20:49:02 +05301623 BUG_ON(base != tbase_get_base(base));
1624
Viresh Kumar8def9062015-03-31 20:49:01 +05301625 base->cpu = cpu;
1626 per_cpu(tvec_bases, cpu) = base;
1627 spin_lock_init(&base->lock);
1628
1629 for (j = 0; j < TVN_SIZE; j++) {
1630 INIT_LIST_HEAD(base->tv5.vec + j);
1631 INIT_LIST_HEAD(base->tv4.vec + j);
1632 INIT_LIST_HEAD(base->tv3.vec + j);
1633 INIT_LIST_HEAD(base->tv2.vec + j);
1634 }
1635 for (j = 0; j < TVR_SIZE; j++)
1636 INIT_LIST_HEAD(base->tv1.vec + j);
1637
1638 base->timer_jiffies = jiffies;
1639 base->next_timer = base->timer_jiffies;
1640}
1641
1642static void __init init_timer_cpus(void)
1643{
1644 struct tvec_base *base;
1645 int local_cpu = smp_processor_id();
1646 int cpu;
1647
1648 for_each_possible_cpu(cpu) {
1649 if (cpu == local_cpu)
1650 base = &boot_tvec_bases;
Peter Zijlstra3650b572015-03-31 20:49:02 +05301651#ifdef CONFIG_SMP
Viresh Kumar8def9062015-03-31 20:49:01 +05301652 else
1653 base = per_cpu_ptr(&__tvec_bases, cpu);
Peter Zijlstra3650b572015-03-31 20:49:02 +05301654#endif
Viresh Kumar8def9062015-03-31 20:49:01 +05301655
1656 init_timer_cpu(base, cpu);
1657 }
1658}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660void __init init_timers(void)
1661{
Tejun Heoe52b1db2012-08-08 11:10:25 -07001662 /* ensure there are enough low bits for flags in timer->base pointer */
1663 BUILD_BUG_ON(__alignof__(struct tvec_base) & TIMER_FLAG_MASK);
1664
Viresh Kumar8def9062015-03-31 20:49:01 +05301665 init_timer_cpus();
Viresh Kumarc24a4a32014-02-28 14:15:21 +05301666 init_timer_stats();
Peter Zijlstra3650b572015-03-31 20:49:02 +05301667 timer_register_cpu_notifier();
Carlos R. Mafra962cf362008-05-15 11:15:37 -03001668 open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671/**
1672 * msleep - sleep safely even with waitqueue interruptions
1673 * @msecs: Time in milliseconds to sleep for
1674 */
1675void msleep(unsigned int msecs)
1676{
1677 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1678
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001679 while (timeout)
1680 timeout = schedule_timeout_uninterruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
1683EXPORT_SYMBOL(msleep);
1684
1685/**
Domen Puncer96ec3ef2005-06-25 14:58:43 -07001686 * msleep_interruptible - sleep waiting for signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 * @msecs: Time in milliseconds to sleep for
1688 */
1689unsigned long msleep_interruptible(unsigned int msecs)
1690{
1691 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1692
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001693 while (timeout && !signal_pending(current))
1694 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return jiffies_to_msecs(timeout);
1696}
1697
1698EXPORT_SYMBOL(msleep_interruptible);
Patrick Pannuto5e7f5a12010-08-02 15:01:04 -07001699
1700static int __sched do_usleep_range(unsigned long min, unsigned long max)
1701{
1702 ktime_t kmin;
1703 unsigned long delta;
1704
1705 kmin = ktime_set(0, min * NSEC_PER_USEC);
1706 delta = (max - min) * NSEC_PER_USEC;
1707 return schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL);
1708}
1709
1710/**
1711 * usleep_range - Drop in replacement for udelay where wakeup is flexible
1712 * @min: Minimum time in usecs to sleep
1713 * @max: Maximum time in usecs to sleep
1714 */
1715void usleep_range(unsigned long min, unsigned long max)
1716{
1717 __set_current_state(TASK_UNINTERRUPTIBLE);
1718 do_usleep_range(min, max);
1719}
1720EXPORT_SYMBOL(usleep_range);