blob: e212df24ad3f6299c39413f68cb76ab44d4af67b [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
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +000052#include "tick-internal.h"
53
Xiao Guangrong2b022e32009-08-10 10:48:59 +080054#define CREATE_TRACE_POINTS
55#include <trace/events/timer.h>
56
Andi Kleen40747ff2014-02-08 08:51:59 +010057__visible u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
Thomas Gleixnerecea8d12005-10-30 15:03:00 -080058
59EXPORT_SYMBOL(jiffies_64);
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * per-CPU timer vector definitions:
63 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
65#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
66#define TVN_SIZE (1 << TVN_BITS)
67#define TVR_SIZE (1 << TVR_BITS)
68#define TVN_MASK (TVN_SIZE - 1)
69#define TVR_MASK (TVR_SIZE - 1)
Hildner, Christian26cff4e2012-10-08 15:49:03 +020070#define MAX_TVAL ((unsigned long)((1ULL << (TVR_BITS + 4*TVN_BITS)) - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Pavel Macheka6fa8e52008-01-30 13:30:00 +010072struct tvec {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct list_head vec[TVN_SIZE];
Pavel Macheka6fa8e52008-01-30 13:30:00 +010074};
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Pavel Macheka6fa8e52008-01-30 13:30:00 +010076struct tvec_root {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct list_head vec[TVR_SIZE];
Pavel Macheka6fa8e52008-01-30 13:30:00 +010078};
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Pavel Macheka6fa8e52008-01-30 13:30:00 +010080struct tvec_base {
Oleg Nesterov3691c512006-03-31 02:30:30 -080081 spinlock_t lock;
82 struct timer_list *running_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned long timer_jiffies;
Martin Schwidefsky97fd9ed2009-07-21 20:25:05 +020084 unsigned long next_timer;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +000085 unsigned long active_timers;
Paul E. McKenneyfff42152014-01-14 20:20:43 -080086 unsigned long all_timers;
Viresh Kumard6f93822014-06-22 01:29:13 +020087 int cpu;
Pavel Macheka6fa8e52008-01-30 13:30:00 +010088 struct tvec_root tv1;
89 struct tvec tv2;
90 struct tvec tv3;
91 struct tvec tv4;
92 struct tvec tv5;
Venki Pallipadi6e453a62007-05-08 00:27:44 -070093} ____cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Peter Zijlstrab337a9382015-03-31 20:49:00 +053095/*
96 * __TIMER_INITIALIZER() needs to set ->base to a valid pointer (because we've
97 * made NULL special, hint: lock_timer_base()) and we cannot get a compile time
98 * pointer to per-cpu entries because we don't know where we'll map the section,
99 * even for the boot cpu.
100 *
101 * And so we use boot_tvec_bases for boot CPU and per-cpu __tvec_bases for the
102 * rest of them.
103 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100104struct tvec_base boot_tvec_bases;
Oleg Nesterov3691c512006-03-31 02:30:30 -0800105EXPORT_SYMBOL(boot_tvec_bases);
Peter Zijlstrab337a9382015-03-31 20:49:00 +0530106
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100107static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700109/* Functions below help us manage 'deferrable' flag */
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100110static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700111{
Tejun Heoe52b1db2012-08-08 11:10:25 -0700112 return ((unsigned int)(unsigned long)base & TIMER_DEFERRABLE);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700113}
114
Tejun Heoc5f66e92012-08-08 11:10:28 -0700115static inline unsigned int tbase_get_irqsafe(struct tvec_base *base)
116{
117 return ((unsigned int)(unsigned long)base & TIMER_IRQSAFE);
118}
119
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100120static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700121{
Tejun Heoe52b1db2012-08-08 11:10:25 -0700122 return ((struct tvec_base *)((unsigned long)base & ~TIMER_FLAG_MASK));
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700123}
124
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700125static inline void
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100126timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700127{
Tejun Heoe52b1db2012-08-08 11:10:25 -0700128 unsigned long flags = (unsigned long)timer->base & TIMER_FLAG_MASK;
129
130 timer->base = (struct tvec_base *)((unsigned long)(new_base) | flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700131}
132
Alan Stern9c133c42008-11-06 08:42:48 +0100133static unsigned long round_jiffies_common(unsigned long j, int cpu,
134 bool force_up)
135{
136 int rem;
137 unsigned long original = j;
138
139 /*
140 * We don't want all cpus firing their timers at once hitting the
141 * same lock or cachelines, so we skew each extra cpu with an extra
142 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
143 * already did this.
144 * The skew is done by adding 3*cpunr, then round, then subtract this
145 * extra offset again.
146 */
147 j += cpu * 3;
148
149 rem = j % HZ;
150
151 /*
152 * If the target jiffie is just after a whole second (which can happen
153 * due to delays of the timer irq, long irq off times etc etc) then
154 * we should round down to the whole second, not up. Use 1/4th second
155 * as cutoff for this rounding as an extreme upper bound for this.
156 * But never round down if @force_up is set.
157 */
158 if (rem < HZ/4 && !force_up) /* round down */
159 j = j - rem;
160 else /* round up */
161 j = j - rem + HZ;
162
163 /* now that we have rounded, subtract the extra skew again */
164 j -= cpu * 3;
165
Bart Van Assche9e04d382013-05-21 20:43:50 +0200166 /*
167 * Make sure j is still in the future. Otherwise return the
168 * unmodified value.
169 */
170 return time_is_after_jiffies(j) ? j : original;
Alan Stern9c133c42008-11-06 08:42:48 +0100171}
172
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800173/**
174 * __round_jiffies - function to round jiffies to a full second
175 * @j: the time in (absolute) jiffies that should be rounded
176 * @cpu: the processor number on which the timeout will happen
177 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800178 * __round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800179 * up or down to (approximately) full seconds. This is useful for timers
180 * for which the exact time they fire does not matter too much, as long as
181 * they fire approximately every X seconds.
182 *
183 * By rounding these timers to whole seconds, all such timers will fire
184 * at the same time, rather than at various times spread out. The goal
185 * of this is to have the CPU wake up less, which saves power.
186 *
187 * The exact rounding is skewed for each processor to avoid all
188 * processors firing at the exact same time, which could lead
189 * to lock contention or spurious cache line bouncing.
190 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800191 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800192 */
193unsigned long __round_jiffies(unsigned long j, int cpu)
194{
Alan Stern9c133c42008-11-06 08:42:48 +0100195 return round_jiffies_common(j, cpu, false);
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800196}
197EXPORT_SYMBOL_GPL(__round_jiffies);
198
199/**
200 * __round_jiffies_relative - function to round jiffies to a full second
201 * @j: the time in (relative) jiffies that should be rounded
202 * @cpu: the processor number on which the timeout will happen
203 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800204 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800205 * up or down to (approximately) full seconds. This is useful for timers
206 * for which the exact time they fire does not matter too much, as long as
207 * they fire approximately every X seconds.
208 *
209 * By rounding these timers to whole seconds, all such timers will fire
210 * at the same time, rather than at various times spread out. The goal
211 * of this is to have the CPU wake up less, which saves power.
212 *
213 * The exact rounding is skewed for each processor to avoid all
214 * processors firing at the exact same time, which could lead
215 * to lock contention or spurious cache line bouncing.
216 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800217 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800218 */
219unsigned long __round_jiffies_relative(unsigned long j, int cpu)
220{
Alan Stern9c133c42008-11-06 08:42:48 +0100221 unsigned long j0 = jiffies;
222
223 /* Use j0 because jiffies might change while we run */
224 return round_jiffies_common(j + j0, cpu, false) - j0;
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800225}
226EXPORT_SYMBOL_GPL(__round_jiffies_relative);
227
228/**
229 * round_jiffies - function to round jiffies to a full second
230 * @j: the time in (absolute) jiffies that should be rounded
231 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800232 * round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800233 * up or down to (approximately) full seconds. This is useful for timers
234 * for which the exact time they fire does not matter too much, as long as
235 * they fire approximately every X seconds.
236 *
237 * By rounding these timers to whole seconds, all such timers will fire
238 * at the same time, rather than at various times spread out. The goal
239 * of this is to have the CPU wake up less, which saves power.
240 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800241 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800242 */
243unsigned long round_jiffies(unsigned long j)
244{
Alan Stern9c133c42008-11-06 08:42:48 +0100245 return round_jiffies_common(j, raw_smp_processor_id(), false);
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800246}
247EXPORT_SYMBOL_GPL(round_jiffies);
248
249/**
250 * round_jiffies_relative - function to round jiffies to a full second
251 * @j: the time in (relative) jiffies that should be rounded
252 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800253 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800254 * up or down to (approximately) full seconds. This is useful for timers
255 * for which the exact time they fire does not matter too much, as long as
256 * they fire approximately every X seconds.
257 *
258 * By rounding these timers to whole seconds, all such timers will fire
259 * at the same time, rather than at various times spread out. The goal
260 * of this is to have the CPU wake up less, which saves power.
261 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800262 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800263 */
264unsigned long round_jiffies_relative(unsigned long j)
265{
266 return __round_jiffies_relative(j, raw_smp_processor_id());
267}
268EXPORT_SYMBOL_GPL(round_jiffies_relative);
269
Alan Stern9c133c42008-11-06 08:42:48 +0100270/**
271 * __round_jiffies_up - function to round jiffies up to a full second
272 * @j: the time in (absolute) jiffies that should be rounded
273 * @cpu: the processor number on which the timeout will happen
274 *
275 * This is the same as __round_jiffies() except that it will never
276 * round down. This is useful for timeouts for which the exact time
277 * of firing does not matter too much, as long as they don't fire too
278 * early.
279 */
280unsigned long __round_jiffies_up(unsigned long j, int cpu)
281{
282 return round_jiffies_common(j, cpu, true);
283}
284EXPORT_SYMBOL_GPL(__round_jiffies_up);
285
286/**
287 * __round_jiffies_up_relative - function to round jiffies up to a full second
288 * @j: the time in (relative) jiffies that should be rounded
289 * @cpu: the processor number on which the timeout will happen
290 *
291 * This is the same as __round_jiffies_relative() except that it will never
292 * round down. This is useful for timeouts for which the exact time
293 * of firing does not matter too much, as long as they don't fire too
294 * early.
295 */
296unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
297{
298 unsigned long j0 = jiffies;
299
300 /* Use j0 because jiffies might change while we run */
301 return round_jiffies_common(j + j0, cpu, true) - j0;
302}
303EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
304
305/**
306 * round_jiffies_up - function to round jiffies up to a full second
307 * @j: the time in (absolute) jiffies that should be rounded
308 *
309 * This is the same as round_jiffies() except that it will never
310 * round down. This is useful for timeouts for which the exact time
311 * of firing does not matter too much, as long as they don't fire too
312 * early.
313 */
314unsigned long round_jiffies_up(unsigned long j)
315{
316 return round_jiffies_common(j, raw_smp_processor_id(), true);
317}
318EXPORT_SYMBOL_GPL(round_jiffies_up);
319
320/**
321 * round_jiffies_up_relative - function to round jiffies up to a full second
322 * @j: the time in (relative) jiffies that should be rounded
323 *
324 * This is the same as round_jiffies_relative() except that it will never
325 * round down. This is useful for timeouts for which the exact time
326 * of firing does not matter too much, as long as they don't fire too
327 * early.
328 */
329unsigned long round_jiffies_up_relative(unsigned long j)
330{
331 return __round_jiffies_up_relative(j, raw_smp_processor_id());
332}
333EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
334
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800335/**
336 * set_timer_slack - set the allowed slack for a timer
Randy Dunlap0caa6212010-08-09 16:32:50 -0700337 * @timer: the timer to be modified
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800338 * @slack_hz: the amount of time (in jiffies) allowed for rounding
339 *
340 * Set the amount of time, in jiffies, that a certain timer has
341 * in terms of slack. By setting this value, the timer subsystem
342 * will schedule the actual timer somewhere between
343 * the time mod_timer() asks for, and that time plus the slack.
344 *
345 * By setting the slack to -1, a percentage of the delay is used
346 * instead.
347 */
348void set_timer_slack(struct timer_list *timer, int slack_hz)
349{
350 timer->slack = slack_hz;
351}
352EXPORT_SYMBOL_GPL(set_timer_slack);
353
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000354static void
355__internal_add_timer(struct tvec_base *base, struct timer_list *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 unsigned long expires = timer->expires;
358 unsigned long idx = expires - base->timer_jiffies;
359 struct list_head *vec;
360
361 if (idx < TVR_SIZE) {
362 int i = expires & TVR_MASK;
363 vec = base->tv1.vec + i;
364 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
365 int i = (expires >> TVR_BITS) & TVN_MASK;
366 vec = base->tv2.vec + i;
367 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
368 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
369 vec = base->tv3.vec + i;
370 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
371 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
372 vec = base->tv4.vec + i;
373 } else if ((signed long) idx < 0) {
374 /*
375 * Can happen if you add a timer with expires == jiffies,
376 * or you set a timer to go off in the past
377 */
378 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
379 } else {
380 int i;
Hildner, Christian26cff4e2012-10-08 15:49:03 +0200381 /* If the timeout is larger than MAX_TVAL (on 64-bit
382 * architectures or with CONFIG_BASE_SMALL=1) then we
383 * use the maximum timeout.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 */
Hildner, Christian26cff4e2012-10-08 15:49:03 +0200385 if (idx > MAX_TVAL) {
386 idx = MAX_TVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 expires = idx + base->timer_jiffies;
388 }
389 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
390 vec = base->tv5.vec + i;
391 }
Thomas Gleixner1bd04bf2015-05-26 22:50:26 +0000392
393 list_add(&timer->entry, vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000396static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
397{
Thomas Gleixner3bb475a2015-05-26 22:50:24 +0000398 /* Advance base->jiffies, if the base is empty */
399 if (!base->all_timers++)
400 base->timer_jiffies = jiffies;
401
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)) {
Oleg Nesterovaea369b2014-01-15 16:19:27 -0800407 if (!base->active_timers++ ||
408 time_before(timer->expires, base->next_timer))
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000409 base->next_timer = timer->expires;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000410 }
Viresh Kumar9f6d9ba2014-06-22 01:29:14 +0200411
412 /*
413 * Check whether the other CPU is in dynticks mode and needs
414 * to be triggered to reevaluate the timer wheel.
415 * We are protected against the other CPU fiddling
416 * with the timer by holding the timer base lock. This also
417 * makes sure that a CPU on the way to stop its tick can not
418 * evaluate the timer wheel.
419 *
420 * Spare the IPI for deferrable timers on idle targets though.
421 * The next busy ticks will take care of it. Except full dynticks
422 * require special care against races with idle_cpu(), lets deal
423 * with that later.
424 */
Joonwoo Park781978e2015-04-27 19:21:49 -0700425 if (!tbase_get_deferrable(timer->base) || tick_nohz_full_cpu(base->cpu))
Viresh Kumar9f6d9ba2014-06-22 01:29:14 +0200426 wake_up_nohz_cpu(base->cpu);
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000427}
428
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800429#ifdef CONFIG_TIMER_STATS
430void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
431{
432 if (timer->start_site)
433 return;
434
435 timer->start_site = addr;
436 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
437 timer->start_pid = current->pid;
438}
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700439
440static void timer_stats_account_timer(struct timer_list *timer)
441{
442 unsigned int flag = 0;
443
Heiko Carstens507e1232009-06-23 17:38:15 +0200444 if (likely(!timer->start_site))
445 return;
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700446 if (unlikely(tbase_get_deferrable(timer->base)))
447 flag |= TIMER_STATS_FLAG_DEFERRABLE;
448
449 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
450 timer->function, timer->start_comm, flag);
451}
452
453#else
454static void timer_stats_account_timer(struct timer_list *timer) {}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800455#endif
456
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700457#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
458
459static struct debug_obj_descr timer_debug_descr;
460
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100461static void *timer_debug_hint(void *addr)
462{
463 return ((struct timer_list *) addr)->function;
464}
465
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700466/*
467 * fixup_init is called when:
468 * - an active object is initialized
469 */
470static int timer_fixup_init(void *addr, enum debug_obj_state state)
471{
472 struct timer_list *timer = addr;
473
474 switch (state) {
475 case ODEBUG_STATE_ACTIVE:
476 del_timer_sync(timer);
477 debug_object_init(timer, &timer_debug_descr);
478 return 1;
479 default:
480 return 0;
481 }
482}
483
Stephen Boydfb16b8c2011-11-07 19:48:26 -0800484/* Stub timer callback for improperly used timers. */
485static void stub_timer(unsigned long data)
486{
487 WARN_ON(1);
488}
489
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700490/*
491 * fixup_activate is called when:
492 * - an active object is activated
493 * - an unknown object is activated (might be a statically initialized object)
494 */
495static int timer_fixup_activate(void *addr, enum debug_obj_state state)
496{
497 struct timer_list *timer = addr;
498
499 switch (state) {
500
501 case ODEBUG_STATE_NOTAVAILABLE:
502 /*
503 * This is not really a fixup. The timer was
504 * statically initialized. We just make sure that it
505 * is tracked in the object tracker.
506 */
507 if (timer->entry.next == NULL &&
508 timer->entry.prev == TIMER_ENTRY_STATIC) {
509 debug_object_init(timer, &timer_debug_descr);
510 debug_object_activate(timer, &timer_debug_descr);
511 return 0;
512 } else {
Stephen Boydfb16b8c2011-11-07 19:48:26 -0800513 setup_timer(timer, stub_timer, 0);
514 return 1;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700515 }
516 return 0;
517
518 case ODEBUG_STATE_ACTIVE:
519 WARN_ON(1);
520
521 default:
522 return 0;
523 }
524}
525
526/*
527 * fixup_free is called when:
528 * - an active object is freed
529 */
530static int timer_fixup_free(void *addr, enum debug_obj_state state)
531{
532 struct timer_list *timer = addr;
533
534 switch (state) {
535 case ODEBUG_STATE_ACTIVE:
536 del_timer_sync(timer);
537 debug_object_free(timer, &timer_debug_descr);
538 return 1;
539 default:
540 return 0;
541 }
542}
543
Christine Chandc4218b2011-11-07 19:48:28 -0800544/*
545 * fixup_assert_init is called when:
546 * - an untracked/uninit-ed object is found
547 */
548static int timer_fixup_assert_init(void *addr, enum debug_obj_state state)
549{
550 struct timer_list *timer = addr;
551
552 switch (state) {
553 case ODEBUG_STATE_NOTAVAILABLE:
554 if (timer->entry.prev == TIMER_ENTRY_STATIC) {
555 /*
556 * This is not really a fixup. The timer was
557 * statically initialized. We just make sure that it
558 * is tracked in the object tracker.
559 */
560 debug_object_init(timer, &timer_debug_descr);
561 return 0;
562 } else {
563 setup_timer(timer, stub_timer, 0);
564 return 1;
565 }
566 default:
567 return 0;
568 }
569}
570
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700571static struct debug_obj_descr timer_debug_descr = {
Christine Chandc4218b2011-11-07 19:48:28 -0800572 .name = "timer_list",
573 .debug_hint = timer_debug_hint,
574 .fixup_init = timer_fixup_init,
575 .fixup_activate = timer_fixup_activate,
576 .fixup_free = timer_fixup_free,
577 .fixup_assert_init = timer_fixup_assert_init,
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700578};
579
580static inline void debug_timer_init(struct timer_list *timer)
581{
582 debug_object_init(timer, &timer_debug_descr);
583}
584
585static inline void debug_timer_activate(struct timer_list *timer)
586{
587 debug_object_activate(timer, &timer_debug_descr);
588}
589
590static inline void debug_timer_deactivate(struct timer_list *timer)
591{
592 debug_object_deactivate(timer, &timer_debug_descr);
593}
594
595static inline void debug_timer_free(struct timer_list *timer)
596{
597 debug_object_free(timer, &timer_debug_descr);
598}
599
Christine Chandc4218b2011-11-07 19:48:28 -0800600static inline void debug_timer_assert_init(struct timer_list *timer)
601{
602 debug_object_assert_init(timer, &timer_debug_descr);
603}
604
Tejun Heofc683992012-08-08 11:10:27 -0700605static void do_init_timer(struct timer_list *timer, unsigned int flags,
606 const char *name, struct lock_class_key *key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700607
Tejun Heofc683992012-08-08 11:10:27 -0700608void init_timer_on_stack_key(struct timer_list *timer, unsigned int flags,
609 const char *name, struct lock_class_key *key)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700610{
611 debug_object_init_on_stack(timer, &timer_debug_descr);
Tejun Heofc683992012-08-08 11:10:27 -0700612 do_init_timer(timer, flags, name, key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700613}
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100614EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700615
616void destroy_timer_on_stack(struct timer_list *timer)
617{
618 debug_object_free(timer, &timer_debug_descr);
619}
620EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
621
622#else
623static inline void debug_timer_init(struct timer_list *timer) { }
624static inline void debug_timer_activate(struct timer_list *timer) { }
625static inline void debug_timer_deactivate(struct timer_list *timer) { }
Christine Chandc4218b2011-11-07 19:48:28 -0800626static inline void debug_timer_assert_init(struct timer_list *timer) { }
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700627#endif
628
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800629static inline void debug_init(struct timer_list *timer)
630{
631 debug_timer_init(timer);
632 trace_timer_init(timer);
633}
634
635static inline void
636debug_activate(struct timer_list *timer, unsigned long expires)
637{
638 debug_timer_activate(timer);
Badhri Jagan Sridharan4e413e82015-05-07 16:20:34 -0700639 trace_timer_start(timer, expires, tbase_get_deferrable(timer->base));
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800640}
641
642static inline void debug_deactivate(struct timer_list *timer)
643{
644 debug_timer_deactivate(timer);
645 trace_timer_cancel(timer);
646}
647
Christine Chandc4218b2011-11-07 19:48:28 -0800648static inline void debug_assert_init(struct timer_list *timer)
649{
650 debug_timer_assert_init(timer);
651}
652
Tejun Heofc683992012-08-08 11:10:27 -0700653static void do_init_timer(struct timer_list *timer, unsigned int flags,
654 const char *name, struct lock_class_key *key)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700655{
Christoph Lameter22127e92014-08-17 12:30:25 -0500656 struct tvec_base *base = raw_cpu_read(tvec_bases);
Tejun Heofc683992012-08-08 11:10:27 -0700657
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700658 timer->entry.next = NULL;
Tejun Heofc683992012-08-08 11:10:27 -0700659 timer->base = (void *)((unsigned long)base | flags);
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800660 timer->slack = -1;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700661#ifdef CONFIG_TIMER_STATS
662 timer->start_site = NULL;
663 timer->start_pid = -1;
664 memset(timer->start_comm, 0, TASK_COMM_LEN);
665#endif
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100666 lockdep_init_map(&timer->lockdep_map, name, key, 0);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700667}
668
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700669/**
Randy Dunlap633fe792009-04-01 17:47:23 -0700670 * init_timer_key - initialize a timer
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700671 * @timer: the timer to be initialized
Tejun Heofc683992012-08-08 11:10:27 -0700672 * @flags: timer flags
Randy Dunlap633fe792009-04-01 17:47:23 -0700673 * @name: name of the timer
674 * @key: lockdep class key of the fake lock used for tracking timer
675 * sync lock dependencies
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700676 *
Randy Dunlap633fe792009-04-01 17:47:23 -0700677 * init_timer_key() must be done to a timer prior calling *any* of the
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700678 * other timer functions.
679 */
Tejun Heofc683992012-08-08 11:10:27 -0700680void init_timer_key(struct timer_list *timer, unsigned int flags,
681 const char *name, struct lock_class_key *key)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700682{
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800683 debug_init(timer);
Tejun Heofc683992012-08-08 11:10:27 -0700684 do_init_timer(timer, flags, name, key);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700685}
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100686EXPORT_SYMBOL(init_timer_key);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700687
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000688static inline void detach_timer(struct timer_list *timer, bool clear_pending)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700689{
690 struct list_head *entry = &timer->entry;
691
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800692 debug_deactivate(timer);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700693
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700694 __list_del(entry->prev, entry->next);
695 if (clear_pending)
696 entry->next = NULL;
697 entry->prev = LIST_POISON2;
698}
699
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000700static inline void
701detach_expired_timer(struct timer_list *timer, struct tvec_base *base)
702{
703 detach_timer(timer, true);
704 if (!tbase_get_deferrable(timer->base))
Tejun Heoe52b1db2012-08-08 11:10:25 -0700705 base->active_timers--;
Paul E. McKenneyfff42152014-01-14 20:20:43 -0800706 base->all_timers--;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000707}
708
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000709static int detach_if_pending(struct timer_list *timer, struct tvec_base *base,
710 bool clear_pending)
711{
712 if (!timer_pending(timer))
713 return 0;
714
715 detach_timer(timer, clear_pending);
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000716 if (!tbase_get_deferrable(timer->base)) {
Tejun Heoe52b1db2012-08-08 11:10:25 -0700717 base->active_timers--;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +0000718 if (timer->expires == base->next_timer)
719 base->next_timer = base->timer_jiffies;
720 }
Thomas Gleixner3bb475a2015-05-26 22:50:24 +0000721 /* If this was the last timer, advance base->jiffies */
722 if (!--base->all_timers)
723 base->timer_jiffies = jiffies;
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000724 return 1;
725}
726
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700727/*
Oleg Nesterov3691c512006-03-31 02:30:30 -0800728 * We are using hashed locking: holding per_cpu(tvec_bases).lock
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700729 * means that all timers which are tied to this base via timer->base are
730 * locked, and the base itself is locked too.
731 *
732 * So __run_timers/migrate_timers can safely modify all timers which could
733 * be found on ->tvX lists.
734 *
735 * When the timer's base is locked, and the timer removed from list, it is
736 * possible to set timer->base = NULL and drop the lock: the timer remains
737 * locked.
738 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100739static struct tvec_base *lock_timer_base(struct timer_list *timer,
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700740 unsigned long *flags)
Josh Triplett89e7e3742006-09-29 01:59:36 -0700741 __acquires(timer->base->lock)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700742{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100743 struct tvec_base *base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700744
745 for (;;) {
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100746 struct tvec_base *prelock_base = timer->base;
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700747 base = tbase_get_base(prelock_base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700748 if (likely(base != NULL)) {
749 spin_lock_irqsave(&base->lock, *flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700750 if (likely(prelock_base == timer->base))
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700751 return base;
752 /* The timer has migrated to another CPU */
753 spin_unlock_irqrestore(&base->lock, *flags);
754 }
755 cpu_relax();
756 }
757}
758
Ingo Molnar74019222009-02-18 12:23:29 +0100759static inline int
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530760__mod_timer(struct timer_list *timer, unsigned long expires,
761 bool pending_only, int pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100763 struct tvec_base *base, *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 unsigned long flags;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530765 int ret = 0 , cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800767 timer_stats_timer_set_start_info(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 BUG_ON(!timer->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700770 base = lock_timer_base(timer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000772 ret = detach_if_pending(timer, base, false);
773 if (!ret && pending_only)
774 goto out_unlock;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700775
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800776 debug_activate(timer, expires);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700777
Viresh Kumar6201b4d2014-03-18 16:26:07 +0530778 cpu = get_nohz_timer_target(pinned);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530779 new_base = per_cpu(tvec_bases, cpu);
780
Oleg Nesterov3691c512006-03-31 02:30:30 -0800781 if (base != new_base) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700782 /*
783 * We are trying to schedule the timer on the local CPU.
784 * However we can't change timer's base while it is running,
785 * otherwise del_timer_sync() can't detect that the timer's
786 * handler yet has not finished. This also guarantees that
787 * the timer is serialized wrt itself.
788 */
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800789 if (likely(base->running_timer != timer)) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700790 /* See the comment in lock_timer_base() */
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700791 timer_set_base(timer, NULL);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700792 spin_unlock(&base->lock);
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800793 base = new_base;
794 spin_lock(&base->lock);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700795 timer_set_base(timer, base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700796 }
797 }
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 timer->expires = expires;
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800800 internal_add_timer(base, timer);
Ingo Molnar74019222009-02-18 12:23:29 +0100801
802out_unlock:
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800803 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 return ret;
806}
807
Ingo Molnar74019222009-02-18 12:23:29 +0100808/**
809 * mod_timer_pending - modify a pending timer's timeout
810 * @timer: the pending timer to be modified
811 * @expires: new timeout in jiffies
812 *
813 * mod_timer_pending() is the same for pending timers as mod_timer(),
814 * but will not re-activate and modify already deleted timers.
815 *
816 * It is useful for unserialized use of timers.
817 */
818int mod_timer_pending(struct timer_list *timer, unsigned long expires)
819{
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530820 return __mod_timer(timer, expires, true, TIMER_NOT_PINNED);
Ingo Molnar74019222009-02-18 12:23:29 +0100821}
822EXPORT_SYMBOL(mod_timer_pending);
823
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800824/*
825 * Decide where to put the timer while taking the slack into account
826 *
827 * Algorithm:
828 * 1) calculate the maximum (absolute) time
829 * 2) calculate the highest bit where the expires and new max are different
830 * 3) use this bit to make a mask
831 * 4) use the bitmask to round down the maximum time, so that all last
832 * bits are zeros
833 */
834static inline
835unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
836{
837 unsigned long expires_limit, mask;
838 int bit;
839
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200840 if (timer->slack >= 0) {
Jeff Chuaf00e0472010-05-24 07:16:24 +0800841 expires_limit = expires + timer->slack;
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200842 } else {
Sebastian Andrzej Siewior1c3cc112011-05-21 12:58:28 +0200843 long delta = expires - jiffies;
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800844
Sebastian Andrzej Siewior1c3cc112011-05-21 12:58:28 +0200845 if (delta < 256)
846 return expires;
847
848 expires_limit = expires + delta / 256;
Thomas Gleixner8e63d772010-05-25 20:43:30 +0200849 }
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800850 mask = expires ^ expires_limit;
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800851 if (mask == 0)
852 return expires;
853
854 bit = find_last_bit(&mask, BITS_PER_LONG);
855
Jiri Bohac98a01e72014-04-18 17:23:11 +0200856 mask = (1UL << bit) - 1;
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800857
858 expires_limit = expires_limit & ~(mask);
859
860 return expires_limit;
861}
862
Ingo Molnar74019222009-02-18 12:23:29 +0100863/**
864 * mod_timer - modify a timer's timeout
865 * @timer: the timer to be modified
866 * @expires: new timeout in jiffies
867 *
868 * mod_timer() is a more efficient way to update the expire field of an
869 * active timer (if the timer is inactive it will be activated)
870 *
871 * mod_timer(timer, expires) is equivalent to:
872 *
873 * del_timer(timer); timer->expires = expires; add_timer(timer);
874 *
875 * Note that if there are multiple unserialized concurrent users of the
876 * same timer, then mod_timer() is the only safe way to modify the timeout,
877 * since add_timer() cannot modify an already running timer.
878 *
879 * The function returns whether it has modified a pending timer or not.
880 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
881 * active timer returns 1.)
882 */
883int mod_timer(struct timer_list *timer, unsigned long expires)
884{
Sebastian Andrzej Siewior1c3cc112011-05-21 12:58:28 +0200885 expires = apply_slack(timer, expires);
886
Ingo Molnar74019222009-02-18 12:23:29 +0100887 /*
888 * This is a common optimization triggered by the
889 * networking code - if the timer is re-modified
890 * to be the same thing then just return:
891 */
Pavel Roskin48411582009-07-18 16:46:02 -0400892 if (timer_pending(timer) && timer->expires == expires)
Ingo Molnar74019222009-02-18 12:23:29 +0100893 return 1;
894
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530895 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
Ingo Molnar74019222009-02-18 12:23:29 +0100896}
897EXPORT_SYMBOL(mod_timer);
898
899/**
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530900 * mod_timer_pinned - modify a timer's timeout
901 * @timer: the timer to be modified
902 * @expires: new timeout in jiffies
903 *
904 * mod_timer_pinned() is a way to update the expire field of an
905 * active timer (if the timer is inactive it will be activated)
Paul E. McKenney048a0e82012-04-26 10:52:27 -0700906 * and to ensure that the timer is scheduled on the current CPU.
907 *
908 * Note that this does not prevent the timer from being migrated
909 * when the current CPU goes offline. If this is a problem for
910 * you, use CPU-hotplug notifiers to handle it correctly, for
911 * example, cancelling the timer when the corresponding CPU goes
912 * offline.
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530913 *
914 * mod_timer_pinned(timer, expires) is equivalent to:
915 *
916 * del_timer(timer); timer->expires = expires; add_timer(timer);
917 */
918int mod_timer_pinned(struct timer_list *timer, unsigned long expires)
919{
920 if (timer->expires == expires && timer_pending(timer))
921 return 1;
922
923 return __mod_timer(timer, expires, false, TIMER_PINNED);
924}
925EXPORT_SYMBOL(mod_timer_pinned);
926
927/**
Ingo Molnar74019222009-02-18 12:23:29 +0100928 * add_timer - start a timer
929 * @timer: the timer to be added
930 *
931 * The kernel will do a ->function(->data) callback from the
932 * timer interrupt at the ->expires point in the future. The
933 * current time is 'jiffies'.
934 *
935 * The timer's ->expires, ->function (and if the handler uses it, ->data)
936 * fields must be set prior calling this function.
937 *
938 * Timers with an ->expires field in the past will be executed in the next
939 * timer tick.
940 */
941void add_timer(struct timer_list *timer)
942{
943 BUG_ON(timer_pending(timer));
944 mod_timer(timer, timer->expires);
945}
946EXPORT_SYMBOL(add_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700948/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * add_timer_on - start a timer on a particular CPU
950 * @timer: the timer to be added
951 * @cpu: the CPU to start it on
952 *
953 * This is not very scalable on SMP. Double adds are not possible.
954 */
955void add_timer_on(struct timer_list *timer, int cpu)
956{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100957 struct tvec_base *base = per_cpu(tvec_bases, cpu);
Thomas Gleixner68194572007-07-19 01:49:16 -0700958 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700959
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800960 timer_stats_timer_set_start_info(timer);
Thomas Gleixner68194572007-07-19 01:49:16 -0700961 BUG_ON(timer_pending(timer) || !timer->function);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800962 spin_lock_irqsave(&base->lock, flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700963 timer_set_base(timer, base);
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800964 debug_activate(timer, timer->expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 internal_add_timer(base, timer);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800966 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
Andi Kleena9862e02009-05-19 22:49:07 +0200968EXPORT_SYMBOL_GPL(add_timer_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700970/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 * del_timer - deactive a timer.
972 * @timer: the timer to be deactivated
973 *
974 * del_timer() deactivates a timer - this works on both active and inactive
975 * timers.
976 *
977 * The function returns whether it has deactivated a pending timer or not.
978 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
979 * active timer returns 1.)
980 */
981int del_timer(struct timer_list *timer)
982{
Pavel Macheka6fa8e52008-01-30 13:30:00 +0100983 struct tvec_base *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700985 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Christine Chandc4218b2011-11-07 19:48:28 -0800987 debug_assert_init(timer);
988
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800989 timer_stats_timer_clear_start_info(timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700990 if (timer_pending(timer)) {
991 base = lock_timer_base(timer, &flags);
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000992 ret = detach_if_pending(timer, base, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700996 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998EXPORT_SYMBOL(del_timer);
999
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001000/**
1001 * try_to_del_timer_sync - Try to deactivate a timer
1002 * @timer: timer do del
1003 *
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001004 * This function tries to deactivate a timer. Upon successful (ret >= 0)
1005 * exit the timer is not queued and the handler is not running on any CPU.
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001006 */
1007int try_to_del_timer_sync(struct timer_list *timer)
1008{
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001009 struct tvec_base *base;
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001010 unsigned long flags;
1011 int ret = -1;
1012
Christine Chandc4218b2011-11-07 19:48:28 -08001013 debug_assert_init(timer);
1014
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001015 base = lock_timer_base(timer, &flags);
1016
Thomas Gleixnerec44bc72012-05-25 22:08:57 +00001017 if (base->running_timer != timer) {
1018 timer_stats_timer_clear_start_info(timer);
1019 ret = detach_if_pending(timer, base, true);
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001020 }
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001021 spin_unlock_irqrestore(&base->lock, flags);
1022
1023 return ret;
1024}
David Howellse19dff12007-04-26 15:46:56 -07001025EXPORT_SYMBOL(try_to_del_timer_sync);
1026
Yong Zhang6f1bc452010-10-20 15:57:31 -07001027#ifdef CONFIG_SMP
Peter Zijlstra3650b572015-03-31 20:49:02 +05301028static DEFINE_PER_CPU(struct tvec_base, __tvec_bases);
1029
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001030/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 * del_timer_sync - deactivate a timer and wait for the handler to finish.
1032 * @timer: the timer to be deactivated
1033 *
1034 * This function only differs from del_timer() on SMP: besides deactivating
1035 * the timer it also makes sure the handler has finished executing on other
1036 * CPUs.
1037 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001038 * Synchronization rules: Callers must prevent restarting of the timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 * otherwise this function is meaningless. It must not be called from
Tejun Heoc5f66e92012-08-08 11:10:28 -07001040 * interrupt contexts unless the timer is an irqsafe one. The caller must
1041 * not hold locks which would prevent completion of the timer's
1042 * handler. The timer's handler must not call add_timer_on(). Upon exit the
1043 * timer is not queued and the handler is not running on any CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 *
Tejun Heoc5f66e92012-08-08 11:10:28 -07001045 * Note: For !irqsafe timers, you must not hold locks that are held in
1046 * interrupt context while calling this function. Even if the lock has
1047 * nothing to do with the timer in question. Here's why:
Steven Rostedt48228f72011-02-08 12:39:54 -05001048 *
1049 * CPU0 CPU1
1050 * ---- ----
1051 * <SOFTIRQ>
1052 * call_timer_fn();
1053 * base->running_timer = mytimer;
1054 * spin_lock_irq(somelock);
1055 * <IRQ>
1056 * spin_lock(somelock);
1057 * del_timer_sync(mytimer);
1058 * while (base->running_timer == mytimer);
1059 *
1060 * Now del_timer_sync() will never return and never release somelock.
1061 * The interrupt on the other CPU is waiting to grab somelock but
1062 * it has interrupted the softirq that CPU0 is waiting to finish.
1063 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 * The function returns whether it has deactivated a pending timer or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 */
1066int del_timer_sync(struct timer_list *timer)
1067{
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001068#ifdef CONFIG_LOCKDEP
Peter Zijlstraf266a512011-02-03 15:09:41 +01001069 unsigned long flags;
1070
Steven Rostedt48228f72011-02-08 12:39:54 -05001071 /*
1072 * If lockdep gives a backtrace here, please reference
1073 * the synchronization rules above.
1074 */
Peter Zijlstra7ff20792011-02-08 15:18:00 +01001075 local_irq_save(flags);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001076 lock_map_acquire(&timer->lockdep_map);
1077 lock_map_release(&timer->lockdep_map);
Peter Zijlstra7ff20792011-02-08 15:18:00 +01001078 local_irq_restore(flags);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001079#endif
Yong Zhang466bd302010-10-20 15:57:33 -07001080 /*
1081 * don't use it in hardirq context, because it
1082 * could lead to deadlock.
1083 */
Tejun Heoc5f66e92012-08-08 11:10:28 -07001084 WARN_ON(in_irq() && !tbase_get_irqsafe(timer->base));
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001085 for (;;) {
1086 int ret = try_to_del_timer_sync(timer);
1087 if (ret >= 0)
1088 return ret;
Andrew Mortona0009652006-07-14 00:24:06 -07001089 cpu_relax();
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092EXPORT_SYMBOL(del_timer_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093#endif
1094
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001095static int cascade(struct tvec_base *base, struct tvec *tv, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 /* cascade all the timers from tv up one level */
Porpoise3439dd82006-06-23 02:05:56 -07001098 struct timer_list *timer, *tmp;
1099 struct list_head tv_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Porpoise3439dd82006-06-23 02:05:56 -07001101 list_replace_init(tv->vec + index, &tv_list);
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 /*
Porpoise3439dd82006-06-23 02:05:56 -07001104 * We are removing _all_ timers from the list, so we
1105 * don't have to detach them individually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 */
Porpoise3439dd82006-06-23 02:05:56 -07001107 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001108 BUG_ON(tbase_get_base(timer->base) != base);
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +00001109 /* No accounting, while moving them */
1110 __internal_add_timer(base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 return index;
1114}
1115
Thomas Gleixner576da122010-03-12 21:10:29 +01001116static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
1117 unsigned long data)
1118{
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001119 int count = preempt_count();
Thomas Gleixner576da122010-03-12 21:10:29 +01001120
1121#ifdef CONFIG_LOCKDEP
1122 /*
1123 * It is permissible to free the timer from inside the
1124 * function that is called from it, this we need to take into
1125 * account for lockdep too. To avoid bogus "held lock freed"
1126 * warnings as well as problems when looking into
1127 * timer->lockdep_map, make a copy and use that here.
1128 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001129 struct lockdep_map lockdep_map;
1130
1131 lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
Thomas Gleixner576da122010-03-12 21:10:29 +01001132#endif
1133 /*
1134 * Couple the lock chain with the lock chain at
1135 * del_timer_sync() by acquiring the lock_map around the fn()
1136 * call here and in del_timer_sync().
1137 */
1138 lock_map_acquire(&lockdep_map);
1139
1140 trace_timer_expire_entry(timer);
1141 fn(data);
1142 trace_timer_expire_exit(timer);
1143
1144 lock_map_release(&lockdep_map);
1145
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001146 if (count != preempt_count()) {
Thomas Gleixner802702e2010-03-12 20:13:23 +01001147 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001148 fn, count, preempt_count());
Thomas Gleixner802702e2010-03-12 20:13:23 +01001149 /*
1150 * Restore the preempt count. That gives us a decent
1151 * chance to survive and extract information. If the
1152 * callback kept a lock held, bad luck, but not worse
1153 * than the BUG() we had.
1154 */
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001155 preempt_count_set(count);
Thomas Gleixner576da122010-03-12 21:10:29 +01001156 }
1157}
1158
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001159#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
1160
1161/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 * __run_timers - run all expired timers (if any) on this CPU.
1163 * @base: the timer vector to be processed.
1164 *
1165 * This function cascades all vectors and executes all expired timer
1166 * vectors.
1167 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001168static inline void __run_timers(struct tvec_base *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 struct timer_list *timer;
1171
Oleg Nesterov3691c512006-03-31 02:30:30 -08001172 spin_lock_irq(&base->lock);
Thomas Gleixner3bb475a2015-05-26 22:50:24 +00001173
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 Gleixner3bb475a2015-05-26 22:50:24 +00001177 int index;
1178
1179 if (!base->all_timers) {
1180 base->timer_jiffies = jiffies;
1181 break;
1182 }
1183
1184 index = base->timer_jiffies & TVR_MASK;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /*
1187 * Cascade timers:
1188 */
1189 if (!index &&
1190 (!cascade(base, &base->tv2, INDEX(0))) &&
1191 (!cascade(base, &base->tv3, INDEX(1))) &&
1192 !cascade(base, &base->tv4, INDEX(2)))
1193 cascade(base, &base->tv5, INDEX(3));
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001194 ++base->timer_jiffies;
Viresh Kumarc41eba72014-03-18 13:23:15 +05301195 list_replace_init(base->tv1.vec + index, head);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001196 while (!list_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 void (*fn)(unsigned long);
1198 unsigned long data;
Tejun Heoc5f66e92012-08-08 11:10:28 -07001199 bool irqsafe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001201 timer = list_first_entry(head, struct timer_list,entry);
Thomas Gleixner68194572007-07-19 01:49:16 -07001202 fn = timer->function;
1203 data = timer->data;
Tejun Heoc5f66e92012-08-08 11:10:28 -07001204 irqsafe = tbase_get_irqsafe(timer->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001206 timer_stats_account_timer(timer);
1207
Yong Zhang6f1bc452010-10-20 15:57:31 -07001208 base->running_timer = timer;
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +00001209 detach_expired_timer(timer, base);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001210
Tejun Heoc5f66e92012-08-08 11:10:28 -07001211 if (irqsafe) {
1212 spin_unlock(&base->lock);
1213 call_timer_fn(timer, fn, data);
1214 spin_lock(&base->lock);
1215 } else {
1216 spin_unlock_irq(&base->lock);
1217 call_timer_fn(timer, fn, data);
1218 spin_lock_irq(&base->lock);
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221 }
Yong Zhang6f1bc452010-10-20 15:57:31 -07001222 base->running_timer = NULL;
Oleg Nesterov3691c512006-03-31 02:30:30 -08001223 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001226#ifdef CONFIG_NO_HZ_COMMON
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227/*
1228 * Find out when the next timer event is due to happen. This
Randy Dunlap90cba642009-08-25 14:35:41 -07001229 * is used on S/390 to stop all activity when a CPU is idle.
1230 * This function needs to be called with interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001232static unsigned long __next_timer_interrupt(struct tvec_base *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001234 unsigned long timer_jiffies = base->timer_jiffies;
Thomas Gleixnereaad0842007-05-29 23:47:39 +02001235 unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001236 int index, slot, array, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 struct timer_list *nte;
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001238 struct tvec *varray[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /* Look for timer events in tv1. */
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001241 index = slot = timer_jiffies & TVR_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001243 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
Thomas Gleixner68194572007-07-19 01:49:16 -07001244 if (tbase_get_deferrable(nte->base))
1245 continue;
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001246
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001247 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001249 /* Look at the cascade bucket(s)? */
1250 if (!index || slot < index)
1251 goto cascade;
1252 return expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001254 slot = (slot + 1) & TVR_MASK;
1255 } while (slot != index);
1256
1257cascade:
1258 /* Calculate the next cascade event */
1259 if (index)
1260 timer_jiffies += TVR_SIZE - index;
1261 timer_jiffies >>= TVR_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 /* Check tv2-tv5. */
1264 varray[0] = &base->tv2;
1265 varray[1] = &base->tv3;
1266 varray[2] = &base->tv4;
1267 varray[3] = &base->tv5;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001268
1269 for (array = 0; array < 4; array++) {
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001270 struct tvec *varp = varray[array];
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001271
1272 index = slot = timer_jiffies & TVN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001274 list_for_each_entry(nte, varp->vec + slot, entry) {
Jon Huntera0419882009-05-01 13:10:23 -07001275 if (tbase_get_deferrable(nte->base))
1276 continue;
1277
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001278 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (time_before(nte->expires, expires))
1280 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001281 }
1282 /*
1283 * Do we still search for the first timer or are
1284 * we looking up the cascade buckets ?
1285 */
1286 if (found) {
1287 /* Look at the cascade bucket(s)? */
1288 if (!index || slot < index)
1289 break;
1290 return expires;
1291 }
1292 slot = (slot + 1) & TVN_MASK;
1293 } while (slot != index);
1294
1295 if (index)
1296 timer_jiffies += TVN_SIZE - index;
1297 timer_jiffies >>= TVN_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001299 return expires;
1300}
1301
1302/*
1303 * Check, if the next hrtimer event is before the next timer wheel
1304 * event:
1305 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001306static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001307{
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001308 u64 nextevt = hrtimer_get_next_event();
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001309
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001310 /*
1311 * If high resolution timers are enabled
1312 * hrtimer_get_next_event() returns KTIME_MAX.
1313 */
1314 if (expires <= nextevt)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001315 return expires;
1316
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001317 /*
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001318 * If the next timer is already expired, return the tick base
1319 * time so the tick is fired immediately.
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001320 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001321 if (nextevt <= basem)
1322 return basem;
Thomas Gleixnereaad0842007-05-29 23:47:39 +02001323
1324 /*
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001325 * Round up to the next jiffie. High resolution timers are
1326 * off, so the hrtimers are expired in the tick and we need to
1327 * make sure that this tick really expires the timer to avoid
1328 * a ping pong of the nohz stop code.
1329 *
1330 * Use DIV_ROUND_UP_ULL to prevent gcc calling __divdi3
Thomas Gleixnereaad0842007-05-29 23:47:39 +02001331 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001332 return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001333}
1334
1335/**
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001336 * get_next_timer_interrupt - return the time (clock mono) of the next timer
1337 * @basej: base time jiffies
1338 * @basem: base time clock monotonic
1339 *
1340 * Returns the tick aligned clock monotonic time of the next pending
1341 * timer or KTIME_MAX if no timer is pending.
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001342 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001343u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
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 Gleixnerc1ad3482015-04-14 21:08:58 +00001346 u64 expires = KTIME_MAX;
1347 unsigned long nextevt;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001348
Heiko Carstensdbd87b52010-12-01 10:11:09 +01001349 /*
1350 * Pretend that there is no timer pending if the cpu is offline.
1351 * Possible pending timers will be migrated later to an active cpu.
1352 */
1353 if (cpu_is_offline(smp_processor_id()))
Thomas Gleixnere40468a2012-05-25 22:08:59 +00001354 return expires;
1355
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001356 spin_lock(&base->lock);
Thomas Gleixnere40468a2012-05-25 22:08:59 +00001357 if (base->active_timers) {
1358 if (time_before_eq(base->next_timer, base->timer_jiffies))
1359 base->next_timer = __next_timer_interrupt(base);
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001360 nextevt = base->next_timer;
1361 if (time_before_eq(nextevt, basej))
1362 expires = basem;
1363 else
1364 expires = basem + (nextevt - basej) * TICK_NSEC;
Thomas Gleixnere40468a2012-05-25 22:08:59 +00001365 }
Oleg Nesterov3691c512006-03-31 02:30:30 -08001366 spin_unlock(&base->lock);
Tony Lindgren69239742006-03-06 15:42:45 -08001367
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001368 return cmp_next_hrtimer_event(basem, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370#endif
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372/*
Daniel Walker5b4db0c2007-10-18 03:06:11 -07001373 * Called from the timer interrupt handler to charge one tick to the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 * process. user_tick is 1 if the tick is user time, 0 for system.
1375 */
1376void update_process_times(int user_tick)
1377{
1378 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /* Note: this timer irq context must be accounted for as well. */
Paul Mackerrasfa13a5a2007-11-09 22:39:38 +01001381 account_process_tick(p, user_tick);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 run_local_timers();
Paul E. McKenneyc3377c2d2014-10-21 07:53:02 -07001383 rcu_check_callbacks(user_tick);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08001384#ifdef CONFIG_IRQ_WORK
1385 if (in_irq())
Frederic Weisbecker76a33062014-08-16 18:37:19 +02001386 irq_work_tick();
Peter Zijlstrae360adb2010-10-14 14:01:34 +08001387#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 scheduler_tick();
Thomas Gleixner68194572007-07-19 01:49:16 -07001389 run_posix_cpu_timers(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
1392/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 * This function runs timers and the timer-tq in bottom half context.
1394 */
1395static void run_timer_softirq(struct softirq_action *h)
1396{
Christoph Lameter74963512010-11-30 14:05:53 -06001397 struct tvec_base *base = __this_cpu_read(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 if (time_after_eq(jiffies, base->timer_jiffies))
1400 __run_timers(base);
1401}
1402
1403/*
1404 * Called by the local, per-CPU timer interrupt on SMP.
1405 */
1406void run_local_timers(void)
1407{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001408 hrtimer_run_queues();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 raise_softirq(TIMER_SOFTIRQ);
1410}
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412#ifdef __ARCH_WANT_SYS_ALARM
1413
1414/*
1415 * For backwards compatibility? This can be done in libc so Alpha
1416 * and all newer ports shouldn't need it.
1417 */
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001418SYSCALL_DEFINE1(alarm, unsigned int, seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -08001420 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
1423#endif
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425static void process_timeout(unsigned long __data)
1426{
Ingo Molnar36c8b582006-07-03 00:25:41 -07001427 wake_up_process((struct task_struct *)__data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
1430/**
1431 * schedule_timeout - sleep until timeout
1432 * @timeout: timeout value in jiffies
1433 *
1434 * Make the current task sleep until @timeout jiffies have
1435 * elapsed. The routine will return immediately unless
1436 * the current task state has been set (see set_current_state()).
1437 *
1438 * You can set the task state as follows -
1439 *
1440 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1441 * pass before the routine returns. The routine will return 0
1442 *
1443 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1444 * delivered to the current task. In this case the remaining time
1445 * in jiffies will be returned, or 0 if the timer expired in time
1446 *
1447 * The current task state is guaranteed to be TASK_RUNNING when this
1448 * routine returns.
1449 *
1450 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1451 * the CPU away without a bound on the timeout. In this case the return
1452 * value will be %MAX_SCHEDULE_TIMEOUT.
1453 *
1454 * In all cases the return value is guaranteed to be non-negative.
1455 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001456signed long __sched schedule_timeout(signed long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 struct timer_list timer;
1459 unsigned long expire;
1460
1461 switch (timeout)
1462 {
1463 case MAX_SCHEDULE_TIMEOUT:
1464 /*
1465 * These two special cases are useful to be comfortable
1466 * in the caller. Nothing more. We could take
1467 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1468 * but I' d like to return a valid offset (>=0) to allow
1469 * the caller to do everything it want with the retval.
1470 */
1471 schedule();
1472 goto out;
1473 default:
1474 /*
1475 * Another bit of PARANOID. Note that the retval will be
1476 * 0 since no piece of kernel is supposed to do a check
1477 * for a negative retval of schedule_timeout() (since it
1478 * should never happens anyway). You just have the printk()
1479 * that will tell you if something is gone wrong and where.
1480 */
Andrew Morton5b149bc2006-12-22 01:10:14 -08001481 if (timeout < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 printk(KERN_ERR "schedule_timeout: wrong timeout "
Andrew Morton5b149bc2006-12-22 01:10:14 -08001483 "value %lx\n", timeout);
1484 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 current->state = TASK_RUNNING;
1486 goto out;
1487 }
1488 }
1489
1490 expire = timeout + jiffies;
1491
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001492 setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
Arun R Bharadwaj597d0272009-04-16 12:13:26 +05301493 __mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 schedule();
1495 del_singleshot_timer_sync(&timer);
1496
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001497 /* Remove the timer from the object tracker */
1498 destroy_timer_on_stack(&timer);
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 timeout = expire - jiffies;
1501
1502 out:
1503 return timeout < 0 ? 0 : timeout;
1504}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505EXPORT_SYMBOL(schedule_timeout);
1506
Andrew Morton8a1c1752005-09-13 01:25:15 -07001507/*
1508 * We can use __set_current_state() here because schedule_timeout() calls
1509 * schedule() unconditionally.
1510 */
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001511signed long __sched schedule_timeout_interruptible(signed long timeout)
1512{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001513 __set_current_state(TASK_INTERRUPTIBLE);
1514 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001515}
1516EXPORT_SYMBOL(schedule_timeout_interruptible);
1517
Matthew Wilcox294d5cc2007-12-06 11:59:46 -05001518signed long __sched schedule_timeout_killable(signed long timeout)
1519{
1520 __set_current_state(TASK_KILLABLE);
1521 return schedule_timeout(timeout);
1522}
1523EXPORT_SYMBOL(schedule_timeout_killable);
1524
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001525signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1526{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001527 __set_current_state(TASK_UNINTERRUPTIBLE);
1528 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001529}
1530EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532#ifdef CONFIG_HOTPLUG_CPU
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001533static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 struct timer_list *timer;
1536
1537 while (!list_empty(head)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001538 timer = list_first_entry(head, struct timer_list, entry);
Thomas Gleixner99d5f3a2012-05-25 22:08:58 +00001539 /* We ignore the accounting on the dying cpu */
Thomas Gleixnerec44bc72012-05-25 22:08:57 +00001540 detach_timer(timer, false);
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001541 timer_set_base(timer, new_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 internal_add_timer(new_base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
Paul Gortmaker0db06282013-06-19 14:53:51 -04001546static void migrate_timers(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Pavel Macheka6fa8e52008-01-30 13:30:00 +01001548 struct tvec_base *old_base;
1549 struct tvec_base *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 int i;
1551
1552 BUG_ON(cpu_online(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001553 old_base = per_cpu(tvec_bases, cpu);
1554 new_base = get_cpu_var(tvec_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001555 /*
1556 * The caller is globally serialized and nobody else
1557 * takes two locks at once, deadlock is not possible.
1558 */
1559 spin_lock_irq(&new_base->lock);
Oleg Nesterov0d180402008-04-04 20:54:10 +02001560 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Oleg Nesterov3691c512006-03-31 02:30:30 -08001562 BUG_ON(old_base->running_timer);
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 for (i = 0; i < TVR_SIZE; i++)
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001565 migrate_timer_list(new_base, old_base->tv1.vec + i);
1566 for (i = 0; i < TVN_SIZE; i++) {
1567 migrate_timer_list(new_base, old_base->tv2.vec + i);
1568 migrate_timer_list(new_base, old_base->tv3.vec + i);
1569 migrate_timer_list(new_base, old_base->tv4.vec + i);
1570 migrate_timer_list(new_base, old_base->tv5.vec + i);
1571 }
1572
Viresh Kumar8def9062015-03-31 20:49:01 +05301573 old_base->active_timers = 0;
1574 old_base->all_timers = 0;
1575
Oleg Nesterov0d180402008-04-04 20:54:10 +02001576 spin_unlock(&old_base->lock);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001577 spin_unlock_irq(&new_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 put_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Paul Gortmaker0db06282013-06-19 14:53:51 -04001581static int timer_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 unsigned long action, void *hcpu)
1583{
Viresh Kumar8def9062015-03-31 20:49:01 +05301584 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001586 case CPU_DEAD_FROZEN:
Viresh Kumar8def9062015-03-31 20:49:01 +05301587 migrate_timers((long)hcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 default:
1590 break;
1591 }
Peter Zijlstra3650b572015-03-31 20:49:02 +05301592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return NOTIFY_OK;
1594}
1595
Peter Zijlstra3650b572015-03-31 20:49:02 +05301596static inline void timer_register_cpu_notifier(void)
1597{
1598 cpu_notifier(timer_cpu_notify, 0);
1599}
1600#else
1601static inline void timer_register_cpu_notifier(void) { }
1602#endif /* CONFIG_HOTPLUG_CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Viresh Kumar8def9062015-03-31 20:49:01 +05301604static void __init init_timer_cpu(struct tvec_base *base, int cpu)
1605{
1606 int j;
1607
Peter Zijlstra3650b572015-03-31 20:49:02 +05301608 BUG_ON(base != tbase_get_base(base));
1609
Viresh Kumar8def9062015-03-31 20:49:01 +05301610 base->cpu = cpu;
1611 per_cpu(tvec_bases, cpu) = base;
1612 spin_lock_init(&base->lock);
1613
1614 for (j = 0; j < TVN_SIZE; j++) {
1615 INIT_LIST_HEAD(base->tv5.vec + j);
1616 INIT_LIST_HEAD(base->tv4.vec + j);
1617 INIT_LIST_HEAD(base->tv3.vec + j);
1618 INIT_LIST_HEAD(base->tv2.vec + j);
1619 }
1620 for (j = 0; j < TVR_SIZE; j++)
1621 INIT_LIST_HEAD(base->tv1.vec + j);
1622
1623 base->timer_jiffies = jiffies;
1624 base->next_timer = base->timer_jiffies;
1625}
1626
1627static void __init init_timer_cpus(void)
1628{
1629 struct tvec_base *base;
1630 int local_cpu = smp_processor_id();
1631 int cpu;
1632
1633 for_each_possible_cpu(cpu) {
1634 if (cpu == local_cpu)
1635 base = &boot_tvec_bases;
Peter Zijlstra3650b572015-03-31 20:49:02 +05301636#ifdef CONFIG_SMP
Viresh Kumar8def9062015-03-31 20:49:01 +05301637 else
1638 base = per_cpu_ptr(&__tvec_bases, cpu);
Peter Zijlstra3650b572015-03-31 20:49:02 +05301639#endif
Viresh Kumar8def9062015-03-31 20:49:01 +05301640
1641 init_timer_cpu(base, cpu);
1642 }
1643}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645void __init init_timers(void)
1646{
Tejun Heoe52b1db2012-08-08 11:10:25 -07001647 /* ensure there are enough low bits for flags in timer->base pointer */
1648 BUILD_BUG_ON(__alignof__(struct tvec_base) & TIMER_FLAG_MASK);
1649
Viresh Kumar8def9062015-03-31 20:49:01 +05301650 init_timer_cpus();
Viresh Kumarc24a4a32014-02-28 14:15:21 +05301651 init_timer_stats();
Peter Zijlstra3650b572015-03-31 20:49:02 +05301652 timer_register_cpu_notifier();
Carlos R. Mafra962cf362008-05-15 11:15:37 -03001653 open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656/**
1657 * msleep - sleep safely even with waitqueue interruptions
1658 * @msecs: Time in milliseconds to sleep for
1659 */
1660void msleep(unsigned int msecs)
1661{
1662 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1663
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001664 while (timeout)
1665 timeout = schedule_timeout_uninterruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
1668EXPORT_SYMBOL(msleep);
1669
1670/**
Domen Puncer96ec3ef2005-06-25 14:58:43 -07001671 * msleep_interruptible - sleep waiting for signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 * @msecs: Time in milliseconds to sleep for
1673 */
1674unsigned long msleep_interruptible(unsigned int msecs)
1675{
1676 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1677
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001678 while (timeout && !signal_pending(current))
1679 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return jiffies_to_msecs(timeout);
1681}
1682
1683EXPORT_SYMBOL(msleep_interruptible);
Patrick Pannuto5e7f5a12010-08-02 15:01:04 -07001684
Thomas Gleixner6deba082015-04-14 21:09:28 +00001685static void __sched do_usleep_range(unsigned long min, unsigned long max)
Patrick Pannuto5e7f5a12010-08-02 15:01:04 -07001686{
1687 ktime_t kmin;
1688 unsigned long delta;
1689
1690 kmin = ktime_set(0, min * NSEC_PER_USEC);
1691 delta = (max - min) * NSEC_PER_USEC;
Thomas Gleixner6deba082015-04-14 21:09:28 +00001692 schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL);
Patrick Pannuto5e7f5a12010-08-02 15:01:04 -07001693}
1694
1695/**
1696 * usleep_range - Drop in replacement for udelay where wakeup is flexible
1697 * @min: Minimum time in usecs to sleep
1698 * @max: Maximum time in usecs to sleep
1699 */
Thomas Gleixner2ad5d322015-04-14 21:09:30 +00001700void __sched usleep_range(unsigned long min, unsigned long max)
Patrick Pannuto5e7f5a12010-08-02 15:01:04 -07001701{
1702 __set_current_state(TASK_UNINTERRUPTIBLE);
1703 do_usleep_range(min, max);
1704}
1705EXPORT_SYMBOL(usleep_range);