]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/timer.c
xps: Transmit Packet Steering
[net-next-2.6.git] / kernel / timer.c
CommitLineData
1da177e4
LT
1/*
2 * linux/kernel/timer.c
3 *
8524070b 4 * Kernel internal timers, basic process system calls
1da177e4
LT
5 *
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>
23#include <linux/module.h>
24#include <linux/interrupt.h>
25#include <linux/percpu.h>
26#include <linux/init.h>
27#include <linux/mm.h>
28#include <linux/swap.h>
b488893a 29#include <linux/pid_namespace.h>
1da177e4
LT
30#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>
97a41e26 37#include <linux/delay.h>
79bf2bb3 38#include <linux/tick.h>
82f67cd9 39#include <linux/kallsyms.h>
e360adbe 40#include <linux/irq_work.h>
eea08f32 41#include <linux/sched.h>
5a0e3ad6 42#include <linux/slab.h>
1da177e4
LT
43
44#include <asm/uaccess.h>
45#include <asm/unistd.h>
46#include <asm/div64.h>
47#include <asm/timex.h>
48#include <asm/io.h>
49
2b022e3d
XG
50#define CREATE_TRACE_POINTS
51#include <trace/events/timer.h>
52
ecea8d19
TG
53u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
54
55EXPORT_SYMBOL(jiffies_64);
56
1da177e4
LT
57/*
58 * per-CPU timer vector definitions:
59 */
1da177e4
LT
60#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
61#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
62#define TVN_SIZE (1 << TVN_BITS)
63#define TVR_SIZE (1 << TVR_BITS)
64#define TVN_MASK (TVN_SIZE - 1)
65#define TVR_MASK (TVR_SIZE - 1)
66
a6fa8e5a 67struct tvec {
1da177e4 68 struct list_head vec[TVN_SIZE];
a6fa8e5a 69};
1da177e4 70
a6fa8e5a 71struct tvec_root {
1da177e4 72 struct list_head vec[TVR_SIZE];
a6fa8e5a 73};
1da177e4 74
a6fa8e5a 75struct tvec_base {
3691c519
ON
76 spinlock_t lock;
77 struct timer_list *running_timer;
1da177e4 78 unsigned long timer_jiffies;
97fd9ed4 79 unsigned long next_timer;
a6fa8e5a
PM
80 struct tvec_root tv1;
81 struct tvec tv2;
82 struct tvec tv3;
83 struct tvec tv4;
84 struct tvec tv5;
6e453a67 85} ____cacheline_aligned;
1da177e4 86
a6fa8e5a 87struct tvec_base boot_tvec_bases;
3691c519 88EXPORT_SYMBOL(boot_tvec_bases);
a6fa8e5a 89static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
1da177e4 90
6e453a67 91/*
a6fa8e5a 92 * Note that all tvec_bases are 2 byte aligned and lower bit of
866e2611
BF
93 * base in timer_list is guaranteed to be zero. Use the LSB to
94 * indicate whether the timer is deferrable.
95 *
96 * A deferrable timer will work normally when the system is busy, but
97 * will not cause a CPU to come out of idle just to service it; instead,
98 * the timer will be serviced when the CPU eventually wakes up with a
99 * subsequent non-deferrable timer.
6e453a67
VP
100 */
101#define TBASE_DEFERRABLE_FLAG (0x1)
102
103/* Functions below help us manage 'deferrable' flag */
a6fa8e5a 104static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
6e453a67 105{
e9910846 106 return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);
6e453a67
VP
107}
108
a6fa8e5a 109static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
6e453a67 110{
a6fa8e5a 111 return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
6e453a67
VP
112}
113
114static inline void timer_set_deferrable(struct timer_list *timer)
115{
a6fa8e5a 116 timer->base = ((struct tvec_base *)((unsigned long)(timer->base) |
6819457d 117 TBASE_DEFERRABLE_FLAG));
6e453a67
VP
118}
119
120static inline void
a6fa8e5a 121timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
6e453a67 122{
a6fa8e5a 123 timer->base = (struct tvec_base *)((unsigned long)(new_base) |
6819457d 124 tbase_get_deferrable(timer->base));
6e453a67
VP
125}
126
9c133c46
AS
127static unsigned long round_jiffies_common(unsigned long j, int cpu,
128 bool force_up)
4c36a5de
AV
129{
130 int rem;
131 unsigned long original = j;
132
133 /*
134 * We don't want all cpus firing their timers at once hitting the
135 * same lock or cachelines, so we skew each extra cpu with an extra
136 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
137 * already did this.
138 * The skew is done by adding 3*cpunr, then round, then subtract this
139 * extra offset again.
140 */
141 j += cpu * 3;
142
143 rem = j % HZ;
144
145 /*
146 * If the target jiffie is just after a whole second (which can happen
147 * due to delays of the timer irq, long irq off times etc etc) then
148 * we should round down to the whole second, not up. Use 1/4th second
149 * as cutoff for this rounding as an extreme upper bound for this.
9c133c46 150 * But never round down if @force_up is set.
4c36a5de 151 */
9c133c46 152 if (rem < HZ/4 && !force_up) /* round down */
4c36a5de
AV
153 j = j - rem;
154 else /* round up */
155 j = j - rem + HZ;
156
157 /* now that we have rounded, subtract the extra skew again */
158 j -= cpu * 3;
159
160 if (j <= jiffies) /* rounding ate our timeout entirely; */
161 return original;
162 return j;
163}
9c133c46
AS
164
165/**
166 * __round_jiffies - function to round jiffies to a full second
167 * @j: the time in (absolute) jiffies that should be rounded
168 * @cpu: the processor number on which the timeout will happen
169 *
170 * __round_jiffies() rounds an absolute time in the future (in jiffies)
171 * up or down to (approximately) full seconds. This is useful for timers
172 * for which the exact time they fire does not matter too much, as long as
173 * they fire approximately every X seconds.
174 *
175 * By rounding these timers to whole seconds, all such timers will fire
176 * at the same time, rather than at various times spread out. The goal
177 * of this is to have the CPU wake up less, which saves power.
178 *
179 * The exact rounding is skewed for each processor to avoid all
180 * processors firing at the exact same time, which could lead
181 * to lock contention or spurious cache line bouncing.
182 *
183 * The return value is the rounded version of the @j parameter.
184 */
185unsigned long __round_jiffies(unsigned long j, int cpu)
186{
187 return round_jiffies_common(j, cpu, false);
188}
4c36a5de
AV
189EXPORT_SYMBOL_GPL(__round_jiffies);
190
191/**
192 * __round_jiffies_relative - function to round jiffies to a full second
193 * @j: the time in (relative) jiffies that should be rounded
194 * @cpu: the processor number on which the timeout will happen
195 *
72fd4a35 196 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
4c36a5de
AV
197 * up or down to (approximately) full seconds. This is useful for timers
198 * for which the exact time they fire does not matter too much, as long as
199 * they fire approximately every X seconds.
200 *
201 * By rounding these timers to whole seconds, all such timers will fire
202 * at the same time, rather than at various times spread out. The goal
203 * of this is to have the CPU wake up less, which saves power.
204 *
205 * The exact rounding is skewed for each processor to avoid all
206 * processors firing at the exact same time, which could lead
207 * to lock contention or spurious cache line bouncing.
208 *
72fd4a35 209 * The return value is the rounded version of the @j parameter.
4c36a5de
AV
210 */
211unsigned long __round_jiffies_relative(unsigned long j, int cpu)
212{
9c133c46
AS
213 unsigned long j0 = jiffies;
214
215 /* Use j0 because jiffies might change while we run */
216 return round_jiffies_common(j + j0, cpu, false) - j0;
4c36a5de
AV
217}
218EXPORT_SYMBOL_GPL(__round_jiffies_relative);
219
220/**
221 * round_jiffies - function to round jiffies to a full second
222 * @j: the time in (absolute) jiffies that should be rounded
223 *
72fd4a35 224 * round_jiffies() rounds an absolute time in the future (in jiffies)
4c36a5de
AV
225 * up or down to (approximately) full seconds. This is useful for timers
226 * for which the exact time they fire does not matter too much, as long as
227 * they fire approximately every X seconds.
228 *
229 * By rounding these timers to whole seconds, all such timers will fire
230 * at the same time, rather than at various times spread out. The goal
231 * of this is to have the CPU wake up less, which saves power.
232 *
72fd4a35 233 * The return value is the rounded version of the @j parameter.
4c36a5de
AV
234 */
235unsigned long round_jiffies(unsigned long j)
236{
9c133c46 237 return round_jiffies_common(j, raw_smp_processor_id(), false);
4c36a5de
AV
238}
239EXPORT_SYMBOL_GPL(round_jiffies);
240
241/**
242 * round_jiffies_relative - function to round jiffies to a full second
243 * @j: the time in (relative) jiffies that should be rounded
244 *
72fd4a35 245 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
4c36a5de
AV
246 * up or down to (approximately) full seconds. This is useful for timers
247 * for which the exact time they fire does not matter too much, as long as
248 * they fire approximately every X seconds.
249 *
250 * By rounding these timers to whole seconds, all such timers will fire
251 * at the same time, rather than at various times spread out. The goal
252 * of this is to have the CPU wake up less, which saves power.
253 *
72fd4a35 254 * The return value is the rounded version of the @j parameter.
4c36a5de
AV
255 */
256unsigned long round_jiffies_relative(unsigned long j)
257{
258 return __round_jiffies_relative(j, raw_smp_processor_id());
259}
260EXPORT_SYMBOL_GPL(round_jiffies_relative);
261
9c133c46
AS
262/**
263 * __round_jiffies_up - function to round jiffies up to a full second
264 * @j: the time in (absolute) jiffies that should be rounded
265 * @cpu: the processor number on which the timeout will happen
266 *
267 * This is the same as __round_jiffies() except that it will never
268 * round down. This is useful for timeouts for which the exact time
269 * of firing does not matter too much, as long as they don't fire too
270 * early.
271 */
272unsigned long __round_jiffies_up(unsigned long j, int cpu)
273{
274 return round_jiffies_common(j, cpu, true);
275}
276EXPORT_SYMBOL_GPL(__round_jiffies_up);
277
278/**
279 * __round_jiffies_up_relative - function to round jiffies up to a full second
280 * @j: the time in (relative) jiffies that should be rounded
281 * @cpu: the processor number on which the timeout will happen
282 *
283 * This is the same as __round_jiffies_relative() except that it will never
284 * round down. This is useful for timeouts for which the exact time
285 * of firing does not matter too much, as long as they don't fire too
286 * early.
287 */
288unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
289{
290 unsigned long j0 = jiffies;
291
292 /* Use j0 because jiffies might change while we run */
293 return round_jiffies_common(j + j0, cpu, true) - j0;
294}
295EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
296
297/**
298 * round_jiffies_up - function to round jiffies up to a full second
299 * @j: the time in (absolute) jiffies that should be rounded
300 *
301 * This is the same as round_jiffies() except that it will never
302 * round down. This is useful for timeouts for which the exact time
303 * of firing does not matter too much, as long as they don't fire too
304 * early.
305 */
306unsigned long round_jiffies_up(unsigned long j)
307{
308 return round_jiffies_common(j, raw_smp_processor_id(), true);
309}
310EXPORT_SYMBOL_GPL(round_jiffies_up);
311
312/**
313 * round_jiffies_up_relative - function to round jiffies up to a full second
314 * @j: the time in (relative) jiffies that should be rounded
315 *
316 * This is the same as round_jiffies_relative() except that it will never
317 * round down. This is useful for timeouts for which the exact time
318 * of firing does not matter too much, as long as they don't fire too
319 * early.
320 */
321unsigned long round_jiffies_up_relative(unsigned long j)
322{
323 return __round_jiffies_up_relative(j, raw_smp_processor_id());
324}
325EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
326
3bbb9ec9
AV
327/**
328 * set_timer_slack - set the allowed slack for a timer
0caa6210 329 * @timer: the timer to be modified
3bbb9ec9
AV
330 * @slack_hz: the amount of time (in jiffies) allowed for rounding
331 *
332 * Set the amount of time, in jiffies, that a certain timer has
333 * in terms of slack. By setting this value, the timer subsystem
334 * will schedule the actual timer somewhere between
335 * the time mod_timer() asks for, and that time plus the slack.
336 *
337 * By setting the slack to -1, a percentage of the delay is used
338 * instead.
339 */
340void set_timer_slack(struct timer_list *timer, int slack_hz)
341{
342 timer->slack = slack_hz;
343}
344EXPORT_SYMBOL_GPL(set_timer_slack);
345
4c36a5de 346
a6fa8e5a 347static inline void set_running_timer(struct tvec_base *base,
1da177e4
LT
348 struct timer_list *timer)
349{
350#ifdef CONFIG_SMP
3691c519 351 base->running_timer = timer;
1da177e4
LT
352#endif
353}
354
a6fa8e5a 355static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
1da177e4
LT
356{
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;
381 /* If the timeout is larger than 0xffffffff on 64-bit
382 * architectures then we use the maximum timeout:
383 */
384 if (idx > 0xffffffffUL) {
385 idx = 0xffffffffUL;
386 expires = idx + base->timer_jiffies;
387 }
388 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
389 vec = base->tv5.vec + i;
390 }
391 /*
392 * Timers are FIFO:
393 */
394 list_add_tail(&timer->entry, vec);
395}
396
82f67cd9
IM
397#ifdef CONFIG_TIMER_STATS
398void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
399{
400 if (timer->start_site)
401 return;
402
403 timer->start_site = addr;
404 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
405 timer->start_pid = current->pid;
406}
c5c061b8
VP
407
408static void timer_stats_account_timer(struct timer_list *timer)
409{
410 unsigned int flag = 0;
411
507e1231
HC
412 if (likely(!timer->start_site))
413 return;
c5c061b8
VP
414 if (unlikely(tbase_get_deferrable(timer->base)))
415 flag |= TIMER_STATS_FLAG_DEFERRABLE;
416
417 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
418 timer->function, timer->start_comm, flag);
419}
420
421#else
422static void timer_stats_account_timer(struct timer_list *timer) {}
82f67cd9
IM
423#endif
424
c6f3a97f
TG
425#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
426
427static struct debug_obj_descr timer_debug_descr;
428
429/*
430 * fixup_init is called when:
431 * - an active object is initialized
55c888d6 432 */
c6f3a97f
TG
433static int timer_fixup_init(void *addr, enum debug_obj_state state)
434{
435 struct timer_list *timer = addr;
436
437 switch (state) {
438 case ODEBUG_STATE_ACTIVE:
439 del_timer_sync(timer);
440 debug_object_init(timer, &timer_debug_descr);
441 return 1;
442 default:
443 return 0;
444 }
445}
446
447/*
448 * fixup_activate is called when:
449 * - an active object is activated
450 * - an unknown object is activated (might be a statically initialized object)
451 */
452static int timer_fixup_activate(void *addr, enum debug_obj_state state)
453{
454 struct timer_list *timer = addr;
455
456 switch (state) {
457
458 case ODEBUG_STATE_NOTAVAILABLE:
459 /*
460 * This is not really a fixup. The timer was
461 * statically initialized. We just make sure that it
462 * is tracked in the object tracker.
463 */
464 if (timer->entry.next == NULL &&
465 timer->entry.prev == TIMER_ENTRY_STATIC) {
466 debug_object_init(timer, &timer_debug_descr);
467 debug_object_activate(timer, &timer_debug_descr);
468 return 0;
469 } else {
470 WARN_ON_ONCE(1);
471 }
472 return 0;
473
474 case ODEBUG_STATE_ACTIVE:
475 WARN_ON(1);
476
477 default:
478 return 0;
479 }
480}
481
482/*
483 * fixup_free is called when:
484 * - an active object is freed
485 */
486static int timer_fixup_free(void *addr, enum debug_obj_state state)
487{
488 struct timer_list *timer = addr;
489
490 switch (state) {
491 case ODEBUG_STATE_ACTIVE:
492 del_timer_sync(timer);
493 debug_object_free(timer, &timer_debug_descr);
494 return 1;
495 default:
496 return 0;
497 }
498}
499
500static struct debug_obj_descr timer_debug_descr = {
501 .name = "timer_list",
502 .fixup_init = timer_fixup_init,
503 .fixup_activate = timer_fixup_activate,
504 .fixup_free = timer_fixup_free,
505};
506
507static inline void debug_timer_init(struct timer_list *timer)
508{
509 debug_object_init(timer, &timer_debug_descr);
510}
511
512static inline void debug_timer_activate(struct timer_list *timer)
513{
514 debug_object_activate(timer, &timer_debug_descr);
515}
516
517static inline void debug_timer_deactivate(struct timer_list *timer)
518{
519 debug_object_deactivate(timer, &timer_debug_descr);
520}
521
522static inline void debug_timer_free(struct timer_list *timer)
523{
524 debug_object_free(timer, &timer_debug_descr);
525}
526
6f2b9b9a
JB
527static void __init_timer(struct timer_list *timer,
528 const char *name,
529 struct lock_class_key *key);
c6f3a97f 530
6f2b9b9a
JB
531void init_timer_on_stack_key(struct timer_list *timer,
532 const char *name,
533 struct lock_class_key *key)
c6f3a97f
TG
534{
535 debug_object_init_on_stack(timer, &timer_debug_descr);
6f2b9b9a 536 __init_timer(timer, name, key);
c6f3a97f 537}
6f2b9b9a 538EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
c6f3a97f
TG
539
540void destroy_timer_on_stack(struct timer_list *timer)
541{
542 debug_object_free(timer, &timer_debug_descr);
543}
544EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
545
546#else
547static inline void debug_timer_init(struct timer_list *timer) { }
548static inline void debug_timer_activate(struct timer_list *timer) { }
549static inline void debug_timer_deactivate(struct timer_list *timer) { }
550#endif
551
2b022e3d
XG
552static inline void debug_init(struct timer_list *timer)
553{
554 debug_timer_init(timer);
555 trace_timer_init(timer);
556}
557
558static inline void
559debug_activate(struct timer_list *timer, unsigned long expires)
560{
561 debug_timer_activate(timer);
562 trace_timer_start(timer, expires);
563}
564
565static inline void debug_deactivate(struct timer_list *timer)
566{
567 debug_timer_deactivate(timer);
568 trace_timer_cancel(timer);
569}
570
6f2b9b9a
JB
571static void __init_timer(struct timer_list *timer,
572 const char *name,
573 struct lock_class_key *key)
55c888d6
ON
574{
575 timer->entry.next = NULL;
bfe5d834 576 timer->base = __raw_get_cpu_var(tvec_bases);
3bbb9ec9 577 timer->slack = -1;
82f67cd9
IM
578#ifdef CONFIG_TIMER_STATS
579 timer->start_site = NULL;
580 timer->start_pid = -1;
581 memset(timer->start_comm, 0, TASK_COMM_LEN);
582#endif
6f2b9b9a 583 lockdep_init_map(&timer->lockdep_map, name, key, 0);
55c888d6 584}
c6f3a97f 585
8cadd283
JB
586void setup_deferrable_timer_on_stack_key(struct timer_list *timer,
587 const char *name,
588 struct lock_class_key *key,
589 void (*function)(unsigned long),
590 unsigned long data)
591{
592 timer->function = function;
593 timer->data = data;
594 init_timer_on_stack_key(timer, name, key);
595 timer_set_deferrable(timer);
596}
597EXPORT_SYMBOL_GPL(setup_deferrable_timer_on_stack_key);
598
c6f3a97f 599/**
633fe795 600 * init_timer_key - initialize a timer
c6f3a97f 601 * @timer: the timer to be initialized
633fe795
RD
602 * @name: name of the timer
603 * @key: lockdep class key of the fake lock used for tracking timer
604 * sync lock dependencies
c6f3a97f 605 *
633fe795 606 * init_timer_key() must be done to a timer prior calling *any* of the
c6f3a97f
TG
607 * other timer functions.
608 */
6f2b9b9a
JB
609void init_timer_key(struct timer_list *timer,
610 const char *name,
611 struct lock_class_key *key)
c6f3a97f 612{
2b022e3d 613 debug_init(timer);
6f2b9b9a 614 __init_timer(timer, name, key);
c6f3a97f 615}
6f2b9b9a 616EXPORT_SYMBOL(init_timer_key);
55c888d6 617
6f2b9b9a
JB
618void init_timer_deferrable_key(struct timer_list *timer,
619 const char *name,
620 struct lock_class_key *key)
6e453a67 621{
6f2b9b9a 622 init_timer_key(timer, name, key);
6e453a67
VP
623 timer_set_deferrable(timer);
624}
6f2b9b9a 625EXPORT_SYMBOL(init_timer_deferrable_key);
6e453a67 626
55c888d6 627static inline void detach_timer(struct timer_list *timer,
82f67cd9 628 int clear_pending)
55c888d6
ON
629{
630 struct list_head *entry = &timer->entry;
631
2b022e3d 632 debug_deactivate(timer);
c6f3a97f 633
55c888d6
ON
634 __list_del(entry->prev, entry->next);
635 if (clear_pending)
636 entry->next = NULL;
637 entry->prev = LIST_POISON2;
638}
639
640/*
3691c519 641 * We are using hashed locking: holding per_cpu(tvec_bases).lock
55c888d6
ON
642 * means that all timers which are tied to this base via timer->base are
643 * locked, and the base itself is locked too.
644 *
645 * So __run_timers/migrate_timers can safely modify all timers which could
646 * be found on ->tvX lists.
647 *
648 * When the timer's base is locked, and the timer removed from list, it is
649 * possible to set timer->base = NULL and drop the lock: the timer remains
650 * locked.
651 */
a6fa8e5a 652static struct tvec_base *lock_timer_base(struct timer_list *timer,
55c888d6 653 unsigned long *flags)
89e7e374 654 __acquires(timer->base->lock)
55c888d6 655{
a6fa8e5a 656 struct tvec_base *base;
55c888d6
ON
657
658 for (;;) {
a6fa8e5a 659 struct tvec_base *prelock_base = timer->base;
6e453a67 660 base = tbase_get_base(prelock_base);
55c888d6
ON
661 if (likely(base != NULL)) {
662 spin_lock_irqsave(&base->lock, *flags);
6e453a67 663 if (likely(prelock_base == timer->base))
55c888d6
ON
664 return base;
665 /* The timer has migrated to another CPU */
666 spin_unlock_irqrestore(&base->lock, *flags);
667 }
668 cpu_relax();
669 }
670}
671
74019224 672static inline int
597d0275
AB
673__mod_timer(struct timer_list *timer, unsigned long expires,
674 bool pending_only, int pinned)
1da177e4 675{
a6fa8e5a 676 struct tvec_base *base, *new_base;
1da177e4 677 unsigned long flags;
eea08f32 678 int ret = 0 , cpu;
1da177e4 679
82f67cd9 680 timer_stats_timer_set_start_info(timer);
1da177e4 681 BUG_ON(!timer->function);
1da177e4 682
55c888d6
ON
683 base = lock_timer_base(timer, &flags);
684
685 if (timer_pending(timer)) {
686 detach_timer(timer, 0);
97fd9ed4
MS
687 if (timer->expires == base->next_timer &&
688 !tbase_get_deferrable(timer->base))
689 base->next_timer = base->timer_jiffies;
55c888d6 690 ret = 1;
74019224
IM
691 } else {
692 if (pending_only)
693 goto out_unlock;
55c888d6
ON
694 }
695
2b022e3d 696 debug_activate(timer, expires);
c6f3a97f 697
eea08f32
AB
698 cpu = smp_processor_id();
699
700#if defined(CONFIG_NO_HZ) && defined(CONFIG_SMP)
83cd4fe2
VP
701 if (!pinned && get_sysctl_timer_migration() && idle_cpu(cpu))
702 cpu = get_nohz_timer_target();
eea08f32
AB
703#endif
704 new_base = per_cpu(tvec_bases, cpu);
705
3691c519 706 if (base != new_base) {
1da177e4 707 /*
55c888d6
ON
708 * We are trying to schedule the timer on the local CPU.
709 * However we can't change timer's base while it is running,
710 * otherwise del_timer_sync() can't detect that the timer's
711 * handler yet has not finished. This also guarantees that
712 * the timer is serialized wrt itself.
1da177e4 713 */
a2c348fe 714 if (likely(base->running_timer != timer)) {
55c888d6 715 /* See the comment in lock_timer_base() */
6e453a67 716 timer_set_base(timer, NULL);
55c888d6 717 spin_unlock(&base->lock);
a2c348fe
ON
718 base = new_base;
719 spin_lock(&base->lock);
6e453a67 720 timer_set_base(timer, base);
1da177e4
LT
721 }
722 }
723
1da177e4 724 timer->expires = expires;
97fd9ed4
MS
725 if (time_before(timer->expires, base->next_timer) &&
726 !tbase_get_deferrable(timer->base))
727 base->next_timer = timer->expires;
a2c348fe 728 internal_add_timer(base, timer);
74019224
IM
729
730out_unlock:
a2c348fe 731 spin_unlock_irqrestore(&base->lock, flags);
1da177e4
LT
732
733 return ret;
734}
735
2aae4a10 736/**
74019224
IM
737 * mod_timer_pending - modify a pending timer's timeout
738 * @timer: the pending timer to be modified
739 * @expires: new timeout in jiffies
1da177e4 740 *
74019224
IM
741 * mod_timer_pending() is the same for pending timers as mod_timer(),
742 * but will not re-activate and modify already deleted timers.
743 *
744 * It is useful for unserialized use of timers.
1da177e4 745 */
74019224 746int mod_timer_pending(struct timer_list *timer, unsigned long expires)
1da177e4 747{
597d0275 748 return __mod_timer(timer, expires, true, TIMER_NOT_PINNED);
1da177e4 749}
74019224 750EXPORT_SYMBOL(mod_timer_pending);
1da177e4 751
3bbb9ec9
AV
752/*
753 * Decide where to put the timer while taking the slack into account
754 *
755 * Algorithm:
756 * 1) calculate the maximum (absolute) time
757 * 2) calculate the highest bit where the expires and new max are different
758 * 3) use this bit to make a mask
759 * 4) use the bitmask to round down the maximum time, so that all last
760 * bits are zeros
761 */
762static inline
763unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
764{
765 unsigned long expires_limit, mask;
766 int bit;
767
f00e047e 768 expires_limit = expires;
3bbb9ec9 769
8e63d779 770 if (timer->slack >= 0) {
f00e047e 771 expires_limit = expires + timer->slack;
8e63d779 772 } else {
2abfb9e1 773 unsigned long now = jiffies;
3bbb9ec9 774
8e63d779
TG
775 /* No slack, if already expired else auto slack 0.4% */
776 if (time_after(expires, now))
777 expires_limit = expires + (expires - now)/256;
778 }
3bbb9ec9 779 mask = expires ^ expires_limit;
3bbb9ec9
AV
780 if (mask == 0)
781 return expires;
782
783 bit = find_last_bit(&mask, BITS_PER_LONG);
784
785 mask = (1 << bit) - 1;
786
787 expires_limit = expires_limit & ~(mask);
788
789 return expires_limit;
790}
791
2aae4a10 792/**
1da177e4
LT
793 * mod_timer - modify a timer's timeout
794 * @timer: the timer to be modified
2aae4a10 795 * @expires: new timeout in jiffies
1da177e4 796 *
72fd4a35 797 * mod_timer() is a more efficient way to update the expire field of an
1da177e4
LT
798 * active timer (if the timer is inactive it will be activated)
799 *
800 * mod_timer(timer, expires) is equivalent to:
801 *
802 * del_timer(timer); timer->expires = expires; add_timer(timer);
803 *
804 * Note that if there are multiple unserialized concurrent users of the
805 * same timer, then mod_timer() is the only safe way to modify the timeout,
806 * since add_timer() cannot modify an already running timer.
807 *
808 * The function returns whether it has modified a pending timer or not.
809 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
810 * active timer returns 1.)
811 */
812int mod_timer(struct timer_list *timer, unsigned long expires)
813{
1da177e4
LT
814 /*
815 * This is a common optimization triggered by the
816 * networking code - if the timer is re-modified
817 * to be the same thing then just return:
818 */
4841158b 819 if (timer_pending(timer) && timer->expires == expires)
1da177e4
LT
820 return 1;
821
3bbb9ec9
AV
822 expires = apply_slack(timer, expires);
823
597d0275 824 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
1da177e4 825}
1da177e4
LT
826EXPORT_SYMBOL(mod_timer);
827
597d0275
AB
828/**
829 * mod_timer_pinned - modify a timer's timeout
830 * @timer: the timer to be modified
831 * @expires: new timeout in jiffies
832 *
833 * mod_timer_pinned() is a way to update the expire field of an
834 * active timer (if the timer is inactive it will be activated)
835 * and not allow the timer to be migrated to a different CPU.
836 *
837 * mod_timer_pinned(timer, expires) is equivalent to:
838 *
839 * del_timer(timer); timer->expires = expires; add_timer(timer);
840 */
841int mod_timer_pinned(struct timer_list *timer, unsigned long expires)
842{
843 if (timer->expires == expires && timer_pending(timer))
844 return 1;
845
846 return __mod_timer(timer, expires, false, TIMER_PINNED);
847}
848EXPORT_SYMBOL(mod_timer_pinned);
849
74019224
IM
850/**
851 * add_timer - start a timer
852 * @timer: the timer to be added
853 *
854 * The kernel will do a ->function(->data) callback from the
855 * timer interrupt at the ->expires point in the future. The
856 * current time is 'jiffies'.
857 *
858 * The timer's ->expires, ->function (and if the handler uses it, ->data)
859 * fields must be set prior calling this function.
860 *
861 * Timers with an ->expires field in the past will be executed in the next
862 * timer tick.
863 */
864void add_timer(struct timer_list *timer)
865{
866 BUG_ON(timer_pending(timer));
867 mod_timer(timer, timer->expires);
868}
869EXPORT_SYMBOL(add_timer);
870
871/**
872 * add_timer_on - start a timer on a particular CPU
873 * @timer: the timer to be added
874 * @cpu: the CPU to start it on
875 *
876 * This is not very scalable on SMP. Double adds are not possible.
877 */
878void add_timer_on(struct timer_list *timer, int cpu)
879{
880 struct tvec_base *base = per_cpu(tvec_bases, cpu);
881 unsigned long flags;
882
883 timer_stats_timer_set_start_info(timer);
884 BUG_ON(timer_pending(timer) || !timer->function);
885 spin_lock_irqsave(&base->lock, flags);
886 timer_set_base(timer, base);
2b022e3d 887 debug_activate(timer, timer->expires);
97fd9ed4
MS
888 if (time_before(timer->expires, base->next_timer) &&
889 !tbase_get_deferrable(timer->base))
890 base->next_timer = timer->expires;
74019224
IM
891 internal_add_timer(base, timer);
892 /*
893 * Check whether the other CPU is idle and needs to be
894 * triggered to reevaluate the timer wheel when nohz is
895 * active. We are protected against the other CPU fiddling
896 * with the timer by holding the timer base lock. This also
897 * makes sure that a CPU on the way to idle can not evaluate
898 * the timer wheel.
899 */
900 wake_up_idle_cpu(cpu);
901 spin_unlock_irqrestore(&base->lock, flags);
902}
a9862e05 903EXPORT_SYMBOL_GPL(add_timer_on);
74019224 904
2aae4a10 905/**
1da177e4
LT
906 * del_timer - deactive a timer.
907 * @timer: the timer to be deactivated
908 *
909 * del_timer() deactivates a timer - this works on both active and inactive
910 * timers.
911 *
912 * The function returns whether it has deactivated a pending timer or not.
913 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
914 * active timer returns 1.)
915 */
916int del_timer(struct timer_list *timer)
917{
a6fa8e5a 918 struct tvec_base *base;
1da177e4 919 unsigned long flags;
55c888d6 920 int ret = 0;
1da177e4 921
82f67cd9 922 timer_stats_timer_clear_start_info(timer);
55c888d6
ON
923 if (timer_pending(timer)) {
924 base = lock_timer_base(timer, &flags);
925 if (timer_pending(timer)) {
926 detach_timer(timer, 1);
97fd9ed4
MS
927 if (timer->expires == base->next_timer &&
928 !tbase_get_deferrable(timer->base))
929 base->next_timer = base->timer_jiffies;
55c888d6
ON
930 ret = 1;
931 }
1da177e4 932 spin_unlock_irqrestore(&base->lock, flags);
1da177e4 933 }
1da177e4 934
55c888d6 935 return ret;
1da177e4 936}
1da177e4
LT
937EXPORT_SYMBOL(del_timer);
938
939#ifdef CONFIG_SMP
2aae4a10
REB
940/**
941 * try_to_del_timer_sync - Try to deactivate a timer
942 * @timer: timer do del
943 *
fd450b73
ON
944 * This function tries to deactivate a timer. Upon successful (ret >= 0)
945 * exit the timer is not queued and the handler is not running on any CPU.
946 *
947 * It must not be called from interrupt contexts.
948 */
949int try_to_del_timer_sync(struct timer_list *timer)
950{
a6fa8e5a 951 struct tvec_base *base;
fd450b73
ON
952 unsigned long flags;
953 int ret = -1;
954
955 base = lock_timer_base(timer, &flags);
956
957 if (base->running_timer == timer)
958 goto out;
959
829b6c1e 960 timer_stats_timer_clear_start_info(timer);
fd450b73
ON
961 ret = 0;
962 if (timer_pending(timer)) {
963 detach_timer(timer, 1);
97fd9ed4
MS
964 if (timer->expires == base->next_timer &&
965 !tbase_get_deferrable(timer->base))
966 base->next_timer = base->timer_jiffies;
fd450b73
ON
967 ret = 1;
968 }
969out:
970 spin_unlock_irqrestore(&base->lock, flags);
971
972 return ret;
973}
e19dff1f
DH
974EXPORT_SYMBOL(try_to_del_timer_sync);
975
2aae4a10 976/**
1da177e4
LT
977 * del_timer_sync - deactivate a timer and wait for the handler to finish.
978 * @timer: the timer to be deactivated
979 *
980 * This function only differs from del_timer() on SMP: besides deactivating
981 * the timer it also makes sure the handler has finished executing on other
982 * CPUs.
983 *
72fd4a35 984 * Synchronization rules: Callers must prevent restarting of the timer,
1da177e4
LT
985 * otherwise this function is meaningless. It must not be called from
986 * interrupt contexts. The caller must not hold locks which would prevent
55c888d6
ON
987 * completion of the timer's handler. The timer's handler must not call
988 * add_timer_on(). Upon exit the timer is not queued and the handler is
989 * not running on any CPU.
1da177e4
LT
990 *
991 * The function returns whether it has deactivated a pending timer or not.
1da177e4
LT
992 */
993int del_timer_sync(struct timer_list *timer)
994{
6f2b9b9a
JB
995#ifdef CONFIG_LOCKDEP
996 unsigned long flags;
997
998 local_irq_save(flags);
999 lock_map_acquire(&timer->lockdep_map);
1000 lock_map_release(&timer->lockdep_map);
1001 local_irq_restore(flags);
1002#endif
1003
fd450b73
ON
1004 for (;;) {
1005 int ret = try_to_del_timer_sync(timer);
1006 if (ret >= 0)
1007 return ret;
a0009652 1008 cpu_relax();
fd450b73 1009 }
1da177e4 1010}
55c888d6 1011EXPORT_SYMBOL(del_timer_sync);
1da177e4
LT
1012#endif
1013
a6fa8e5a 1014static int cascade(struct tvec_base *base, struct tvec *tv, int index)
1da177e4
LT
1015{
1016 /* cascade all the timers from tv up one level */
3439dd86
P
1017 struct timer_list *timer, *tmp;
1018 struct list_head tv_list;
1019
1020 list_replace_init(tv->vec + index, &tv_list);
1da177e4 1021
1da177e4 1022 /*
3439dd86
P
1023 * We are removing _all_ timers from the list, so we
1024 * don't have to detach them individually.
1da177e4 1025 */
3439dd86 1026 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
6e453a67 1027 BUG_ON(tbase_get_base(timer->base) != base);
3439dd86 1028 internal_add_timer(base, timer);
1da177e4 1029 }
1da177e4
LT
1030
1031 return index;
1032}
1033
576da126
TG
1034static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
1035 unsigned long data)
1036{
1037 int preempt_count = preempt_count();
1038
1039#ifdef CONFIG_LOCKDEP
1040 /*
1041 * It is permissible to free the timer from inside the
1042 * function that is called from it, this we need to take into
1043 * account for lockdep too. To avoid bogus "held lock freed"
1044 * warnings as well as problems when looking into
1045 * timer->lockdep_map, make a copy and use that here.
1046 */
1047 struct lockdep_map lockdep_map = timer->lockdep_map;
1048#endif
1049 /*
1050 * Couple the lock chain with the lock chain at
1051 * del_timer_sync() by acquiring the lock_map around the fn()
1052 * call here and in del_timer_sync().
1053 */
1054 lock_map_acquire(&lockdep_map);
1055
1056 trace_timer_expire_entry(timer);
1057 fn(data);
1058 trace_timer_expire_exit(timer);
1059
1060 lock_map_release(&lockdep_map);
1061
1062 if (preempt_count != preempt_count()) {
802702e0
TG
1063 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
1064 fn, preempt_count, preempt_count());
1065 /*
1066 * Restore the preempt count. That gives us a decent
1067 * chance to survive and extract information. If the
1068 * callback kept a lock held, bad luck, but not worse
1069 * than the BUG() we had.
1070 */
1071 preempt_count() = preempt_count;
576da126
TG
1072 }
1073}
1074
2aae4a10
REB
1075#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
1076
1077/**
1da177e4
LT
1078 * __run_timers - run all expired timers (if any) on this CPU.
1079 * @base: the timer vector to be processed.
1080 *
1081 * This function cascades all vectors and executes all expired timer
1082 * vectors.
1083 */
a6fa8e5a 1084static inline void __run_timers(struct tvec_base *base)
1da177e4
LT
1085{
1086 struct timer_list *timer;
1087
3691c519 1088 spin_lock_irq(&base->lock);
1da177e4 1089 while (time_after_eq(jiffies, base->timer_jiffies)) {
626ab0e6 1090 struct list_head work_list;
1da177e4 1091 struct list_head *head = &work_list;
6819457d 1092 int index = base->timer_jiffies & TVR_MASK;
626ab0e6 1093
1da177e4
LT
1094 /*
1095 * Cascade timers:
1096 */
1097 if (!index &&
1098 (!cascade(base, &base->tv2, INDEX(0))) &&
1099 (!cascade(base, &base->tv3, INDEX(1))) &&
1100 !cascade(base, &base->tv4, INDEX(2)))
1101 cascade(base, &base->tv5, INDEX(3));
626ab0e6
ON
1102 ++base->timer_jiffies;
1103 list_replace_init(base->tv1.vec + index, &work_list);
55c888d6 1104 while (!list_empty(head)) {
1da177e4
LT
1105 void (*fn)(unsigned long);
1106 unsigned long data;
1107
b5e61818 1108 timer = list_first_entry(head, struct timer_list,entry);
6819457d
TG
1109 fn = timer->function;
1110 data = timer->data;
1da177e4 1111
82f67cd9
IM
1112 timer_stats_account_timer(timer);
1113
1da177e4 1114 set_running_timer(base, timer);
55c888d6 1115 detach_timer(timer, 1);
6f2b9b9a 1116
3691c519 1117 spin_unlock_irq(&base->lock);
576da126 1118 call_timer_fn(timer, fn, data);
3691c519 1119 spin_lock_irq(&base->lock);
1da177e4
LT
1120 }
1121 }
1122 set_running_timer(base, NULL);
3691c519 1123 spin_unlock_irq(&base->lock);
1da177e4
LT
1124}
1125
ee9c5785 1126#ifdef CONFIG_NO_HZ
1da177e4
LT
1127/*
1128 * Find out when the next timer event is due to happen. This
90cba64a
RD
1129 * is used on S/390 to stop all activity when a CPU is idle.
1130 * This function needs to be called with interrupts disabled.
1da177e4 1131 */
a6fa8e5a 1132static unsigned long __next_timer_interrupt(struct tvec_base *base)
1da177e4 1133{
1cfd6849 1134 unsigned long timer_jiffies = base->timer_jiffies;
eaad084b 1135 unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
1cfd6849 1136 int index, slot, array, found = 0;
1da177e4 1137 struct timer_list *nte;
a6fa8e5a 1138 struct tvec *varray[4];
1da177e4
LT
1139
1140 /* Look for timer events in tv1. */
1cfd6849 1141 index = slot = timer_jiffies & TVR_MASK;
1da177e4 1142 do {
1cfd6849 1143 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
6819457d
TG
1144 if (tbase_get_deferrable(nte->base))
1145 continue;
6e453a67 1146
1cfd6849 1147 found = 1;
1da177e4 1148 expires = nte->expires;
1cfd6849
TG
1149 /* Look at the cascade bucket(s)? */
1150 if (!index || slot < index)
1151 goto cascade;
1152 return expires;
1da177e4 1153 }
1cfd6849
TG
1154 slot = (slot + 1) & TVR_MASK;
1155 } while (slot != index);
1156
1157cascade:
1158 /* Calculate the next cascade event */
1159 if (index)
1160 timer_jiffies += TVR_SIZE - index;
1161 timer_jiffies >>= TVR_BITS;
1da177e4
LT
1162
1163 /* Check tv2-tv5. */
1164 varray[0] = &base->tv2;
1165 varray[1] = &base->tv3;
1166 varray[2] = &base->tv4;
1167 varray[3] = &base->tv5;
1cfd6849
TG
1168
1169 for (array = 0; array < 4; array++) {
a6fa8e5a 1170 struct tvec *varp = varray[array];
1cfd6849
TG
1171
1172 index = slot = timer_jiffies & TVN_MASK;
1da177e4 1173 do {
1cfd6849 1174 list_for_each_entry(nte, varp->vec + slot, entry) {
a0419888
JH
1175 if (tbase_get_deferrable(nte->base))
1176 continue;
1177
1cfd6849 1178 found = 1;
1da177e4
LT
1179 if (time_before(nte->expires, expires))
1180 expires = nte->expires;
1cfd6849
TG
1181 }
1182 /*
1183 * Do we still search for the first timer or are
1184 * we looking up the cascade buckets ?
1185 */
1186 if (found) {
1187 /* Look at the cascade bucket(s)? */
1188 if (!index || slot < index)
1189 break;
1190 return expires;
1191 }
1192 slot = (slot + 1) & TVN_MASK;
1193 } while (slot != index);
1194
1195 if (index)
1196 timer_jiffies += TVN_SIZE - index;
1197 timer_jiffies >>= TVN_BITS;
1da177e4 1198 }
1cfd6849
TG
1199 return expires;
1200}
69239749 1201
1cfd6849
TG
1202/*
1203 * Check, if the next hrtimer event is before the next timer wheel
1204 * event:
1205 */
1206static unsigned long cmp_next_hrtimer_event(unsigned long now,
1207 unsigned long expires)
1208{
1209 ktime_t hr_delta = hrtimer_get_next_event();
1210 struct timespec tsdelta;
9501b6cf 1211 unsigned long delta;
1cfd6849
TG
1212
1213 if (hr_delta.tv64 == KTIME_MAX)
1214 return expires;
0662b713 1215
9501b6cf
TG
1216 /*
1217 * Expired timer available, let it expire in the next tick
1218 */
1219 if (hr_delta.tv64 <= 0)
1220 return now + 1;
69239749 1221
1cfd6849 1222 tsdelta = ktime_to_timespec(hr_delta);
9501b6cf 1223 delta = timespec_to_jiffies(&tsdelta);
eaad084b
TG
1224
1225 /*
1226 * Limit the delta to the max value, which is checked in
1227 * tick_nohz_stop_sched_tick():
1228 */
1229 if (delta > NEXT_TIMER_MAX_DELTA)
1230 delta = NEXT_TIMER_MAX_DELTA;
1231
9501b6cf
TG
1232 /*
1233 * Take rounding errors in to account and make sure, that it
1234 * expires in the next tick. Otherwise we go into an endless
1235 * ping pong due to tick_nohz_stop_sched_tick() retriggering
1236 * the timer softirq
1237 */
1238 if (delta < 1)
1239 delta = 1;
1240 now += delta;
1cfd6849
TG
1241 if (time_before(now, expires))
1242 return now;
1da177e4
LT
1243 return expires;
1244}
1cfd6849
TG
1245
1246/**
8dce39c2 1247 * get_next_timer_interrupt - return the jiffy of the next pending timer
05fb6bf0 1248 * @now: current time (in jiffies)
1cfd6849 1249 */
fd064b9b 1250unsigned long get_next_timer_interrupt(unsigned long now)
1cfd6849 1251{
a6fa8e5a 1252 struct tvec_base *base = __get_cpu_var(tvec_bases);
fd064b9b 1253 unsigned long expires;
1cfd6849
TG
1254
1255 spin_lock(&base->lock);
97fd9ed4
MS
1256 if (time_before_eq(base->next_timer, base->timer_jiffies))
1257 base->next_timer = __next_timer_interrupt(base);
1258 expires = base->next_timer;
1cfd6849
TG
1259 spin_unlock(&base->lock);
1260
1261 if (time_before_eq(expires, now))
1262 return now;
1263
1264 return cmp_next_hrtimer_event(now, expires);
1265}
1da177e4
LT
1266#endif
1267
1da177e4 1268/*
5b4db0c2 1269 * Called from the timer interrupt handler to charge one tick to the current
1da177e4
LT
1270 * process. user_tick is 1 if the tick is user time, 0 for system.
1271 */
1272void update_process_times(int user_tick)
1273{
1274 struct task_struct *p = current;
1275 int cpu = smp_processor_id();
1276
1277 /* Note: this timer irq context must be accounted for as well. */
fa13a5a1 1278 account_process_tick(p, user_tick);
1da177e4 1279 run_local_timers();
a157229c 1280 rcu_check_callbacks(cpu, user_tick);
b845b517 1281 printk_tick();
e360adbe
PZ
1282#ifdef CONFIG_IRQ_WORK
1283 if (in_irq())
1284 irq_work_run();
1285#endif
1da177e4 1286 scheduler_tick();
6819457d 1287 run_posix_cpu_timers(p);
1da177e4
LT
1288}
1289
1da177e4
LT
1290/*
1291 * This function runs timers and the timer-tq in bottom half context.
1292 */
1293static void run_timer_softirq(struct softirq_action *h)
1294{
a6fa8e5a 1295 struct tvec_base *base = __get_cpu_var(tvec_bases);
1da177e4 1296
d3d74453 1297 hrtimer_run_pending();
82f67cd9 1298
1da177e4
LT
1299 if (time_after_eq(jiffies, base->timer_jiffies))
1300 __run_timers(base);
1301}
1302
1303/*
1304 * Called by the local, per-CPU timer interrupt on SMP.
1305 */
1306void run_local_timers(void)
1307{
d3d74453 1308 hrtimer_run_queues();
1da177e4
LT
1309 raise_softirq(TIMER_SOFTIRQ);
1310}
1311
1da177e4
LT
1312/*
1313 * The 64-bit jiffies value is not atomic - you MUST NOT read it
1314 * without sampling the sequence number in xtime_lock.
1315 * jiffies is defined in the linker script...
1316 */
1317
3171a030 1318void do_timer(unsigned long ticks)
1da177e4 1319{
3171a030 1320 jiffies_64 += ticks;
dce48a84
TG
1321 update_wall_time();
1322 calc_global_load();
1da177e4
LT
1323}
1324
1325#ifdef __ARCH_WANT_SYS_ALARM
1326
1327/*
1328 * For backwards compatibility? This can be done in libc so Alpha
1329 * and all newer ports shouldn't need it.
1330 */
58fd3aa2 1331SYSCALL_DEFINE1(alarm, unsigned int, seconds)
1da177e4 1332{
c08b8a49 1333 return alarm_setitimer(seconds);
1da177e4
LT
1334}
1335
1336#endif
1337
1338#ifndef __alpha__
1339
1340/*
1341 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
1342 * should be moved into arch/i386 instead?
1343 */
1344
1345/**
1346 * sys_getpid - return the thread group id of the current process
1347 *
1348 * Note, despite the name, this returns the tgid not the pid. The tgid and
1349 * the pid are identical unless CLONE_THREAD was specified on clone() in
1350 * which case the tgid is the same in all threads of the same group.
1351 *
1352 * This is SMP safe as current->tgid does not change.
1353 */
58fd3aa2 1354SYSCALL_DEFINE0(getpid)
1da177e4 1355{
b488893a 1356 return task_tgid_vnr(current);
1da177e4
LT
1357}
1358
1359/*
6997a6fa
KK
1360 * Accessing ->real_parent is not SMP-safe, it could
1361 * change from under us. However, we can use a stale
1362 * value of ->real_parent under rcu_read_lock(), see
1363 * release_task()->call_rcu(delayed_put_task_struct).
1da177e4 1364 */
dbf040d9 1365SYSCALL_DEFINE0(getppid)
1da177e4
LT
1366{
1367 int pid;
1da177e4 1368
6997a6fa 1369 rcu_read_lock();
6c5f3e7b 1370 pid = task_tgid_vnr(current->real_parent);
6997a6fa 1371 rcu_read_unlock();
1da177e4 1372
1da177e4
LT
1373 return pid;
1374}
1375
dbf040d9 1376SYSCALL_DEFINE0(getuid)
1da177e4
LT
1377{
1378 /* Only we change this so SMP safe */
76aac0e9 1379 return current_uid();
1da177e4
LT
1380}
1381
dbf040d9 1382SYSCALL_DEFINE0(geteuid)
1da177e4
LT
1383{
1384 /* Only we change this so SMP safe */
76aac0e9 1385 return current_euid();
1da177e4
LT
1386}
1387
dbf040d9 1388SYSCALL_DEFINE0(getgid)
1da177e4
LT
1389{
1390 /* Only we change this so SMP safe */
76aac0e9 1391 return current_gid();
1da177e4
LT
1392}
1393
dbf040d9 1394SYSCALL_DEFINE0(getegid)
1da177e4
LT
1395{
1396 /* Only we change this so SMP safe */
76aac0e9 1397 return current_egid();
1da177e4
LT
1398}
1399
1400#endif
1401
1402static void process_timeout(unsigned long __data)
1403{
36c8b586 1404 wake_up_process((struct task_struct *)__data);
1da177e4
LT
1405}
1406
1407/**
1408 * schedule_timeout - sleep until timeout
1409 * @timeout: timeout value in jiffies
1410 *
1411 * Make the current task sleep until @timeout jiffies have
1412 * elapsed. The routine will return immediately unless
1413 * the current task state has been set (see set_current_state()).
1414 *
1415 * You can set the task state as follows -
1416 *
1417 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1418 * pass before the routine returns. The routine will return 0
1419 *
1420 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1421 * delivered to the current task. In this case the remaining time
1422 * in jiffies will be returned, or 0 if the timer expired in time
1423 *
1424 * The current task state is guaranteed to be TASK_RUNNING when this
1425 * routine returns.
1426 *
1427 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1428 * the CPU away without a bound on the timeout. In this case the return
1429 * value will be %MAX_SCHEDULE_TIMEOUT.
1430 *
1431 * In all cases the return value is guaranteed to be non-negative.
1432 */
7ad5b3a5 1433signed long __sched schedule_timeout(signed long timeout)
1da177e4
LT
1434{
1435 struct timer_list timer;
1436 unsigned long expire;
1437
1438 switch (timeout)
1439 {
1440 case MAX_SCHEDULE_TIMEOUT:
1441 /*
1442 * These two special cases are useful to be comfortable
1443 * in the caller. Nothing more. We could take
1444 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1445 * but I' d like to return a valid offset (>=0) to allow
1446 * the caller to do everything it want with the retval.
1447 */
1448 schedule();
1449 goto out;
1450 default:
1451 /*
1452 * Another bit of PARANOID. Note that the retval will be
1453 * 0 since no piece of kernel is supposed to do a check
1454 * for a negative retval of schedule_timeout() (since it
1455 * should never happens anyway). You just have the printk()
1456 * that will tell you if something is gone wrong and where.
1457 */
5b149bcc 1458 if (timeout < 0) {
1da177e4 1459 printk(KERN_ERR "schedule_timeout: wrong timeout "
5b149bcc
AM
1460 "value %lx\n", timeout);
1461 dump_stack();
1da177e4
LT
1462 current->state = TASK_RUNNING;
1463 goto out;
1464 }
1465 }
1466
1467 expire = timeout + jiffies;
1468
c6f3a97f 1469 setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
597d0275 1470 __mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
1da177e4
LT
1471 schedule();
1472 del_singleshot_timer_sync(&timer);
1473
c6f3a97f
TG
1474 /* Remove the timer from the object tracker */
1475 destroy_timer_on_stack(&timer);
1476
1da177e4
LT
1477 timeout = expire - jiffies;
1478
1479 out:
1480 return timeout < 0 ? 0 : timeout;
1481}
1da177e4
LT
1482EXPORT_SYMBOL(schedule_timeout);
1483
8a1c1757
AM
1484/*
1485 * We can use __set_current_state() here because schedule_timeout() calls
1486 * schedule() unconditionally.
1487 */
64ed93a2
NA
1488signed long __sched schedule_timeout_interruptible(signed long timeout)
1489{
a5a0d52c
AM
1490 __set_current_state(TASK_INTERRUPTIBLE);
1491 return schedule_timeout(timeout);
64ed93a2
NA
1492}
1493EXPORT_SYMBOL(schedule_timeout_interruptible);
1494
294d5cc2
MW
1495signed long __sched schedule_timeout_killable(signed long timeout)
1496{
1497 __set_current_state(TASK_KILLABLE);
1498 return schedule_timeout(timeout);
1499}
1500EXPORT_SYMBOL(schedule_timeout_killable);
1501
64ed93a2
NA
1502signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1503{
a5a0d52c
AM
1504 __set_current_state(TASK_UNINTERRUPTIBLE);
1505 return schedule_timeout(timeout);
64ed93a2
NA
1506}
1507EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1508
1da177e4 1509/* Thread ID - the internal kernel "pid" */
58fd3aa2 1510SYSCALL_DEFINE0(gettid)
1da177e4 1511{
b488893a 1512 return task_pid_vnr(current);
1da177e4
LT
1513}
1514
2aae4a10 1515/**
d4d23add 1516 * do_sysinfo - fill in sysinfo struct
2aae4a10 1517 * @info: pointer to buffer to fill
6819457d 1518 */
d4d23add 1519int do_sysinfo(struct sysinfo *info)
1da177e4 1520{
1da177e4
LT
1521 unsigned long mem_total, sav_total;
1522 unsigned int mem_unit, bitcount;
2d02494f 1523 struct timespec tp;
1da177e4 1524
d4d23add 1525 memset(info, 0, sizeof(struct sysinfo));
1da177e4 1526
2d02494f
TG
1527 ktime_get_ts(&tp);
1528 monotonic_to_bootbased(&tp);
1529 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1da177e4 1530
2d02494f 1531 get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
1da177e4 1532
2d02494f 1533 info->procs = nr_threads;
1da177e4 1534
d4d23add
KM
1535 si_meminfo(info);
1536 si_swapinfo(info);
1da177e4
LT
1537
1538 /*
1539 * If the sum of all the available memory (i.e. ram + swap)
1540 * is less than can be stored in a 32 bit unsigned long then
1541 * we can be binary compatible with 2.2.x kernels. If not,
1542 * well, in that case 2.2.x was broken anyways...
1543 *
1544 * -Erik Andersen <andersee@debian.org>
1545 */
1546
d4d23add
KM
1547 mem_total = info->totalram + info->totalswap;
1548 if (mem_total < info->totalram || mem_total < info->totalswap)
1da177e4
LT
1549 goto out;
1550 bitcount = 0;
d4d23add 1551 mem_unit = info->mem_unit;
1da177e4
LT
1552 while (mem_unit > 1) {
1553 bitcount++;
1554 mem_unit >>= 1;
1555 sav_total = mem_total;
1556 mem_total <<= 1;
1557 if (mem_total < sav_total)
1558 goto out;
1559 }
1560
1561 /*
1562 * If mem_total did not overflow, multiply all memory values by
d4d23add 1563 * info->mem_unit and set it to 1. This leaves things compatible
1da177e4
LT
1564 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1565 * kernels...
1566 */
1567
d4d23add
KM
1568 info->mem_unit = 1;
1569 info->totalram <<= bitcount;
1570 info->freeram <<= bitcount;
1571 info->sharedram <<= bitcount;
1572 info->bufferram <<= bitcount;
1573 info->totalswap <<= bitcount;
1574 info->freeswap <<= bitcount;
1575 info->totalhigh <<= bitcount;
1576 info->freehigh <<= bitcount;
1577
1578out:
1579 return 0;
1580}
1581
1e7bfb21 1582SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
d4d23add
KM
1583{
1584 struct sysinfo val;
1585
1586 do_sysinfo(&val);
1da177e4 1587
1da177e4
LT
1588 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1589 return -EFAULT;
1590
1591 return 0;
1592}
1593
b4be6258 1594static int __cpuinit init_timers_cpu(int cpu)
1da177e4
LT
1595{
1596 int j;
a6fa8e5a 1597 struct tvec_base *base;
b4be6258 1598 static char __cpuinitdata tvec_base_done[NR_CPUS];
55c888d6 1599
ba6edfcd 1600 if (!tvec_base_done[cpu]) {
a4a6198b
JB
1601 static char boot_done;
1602
a4a6198b 1603 if (boot_done) {
ba6edfcd
AM
1604 /*
1605 * The APs use this path later in boot
1606 */
94f6030c
CL
1607 base = kmalloc_node(sizeof(*base),
1608 GFP_KERNEL | __GFP_ZERO,
a4a6198b
JB
1609 cpu_to_node(cpu));
1610 if (!base)
1611 return -ENOMEM;
6e453a67
VP
1612
1613 /* Make sure that tvec_base is 2 byte aligned */
1614 if (tbase_get_deferrable(base)) {
1615 WARN_ON(1);
1616 kfree(base);
1617 return -ENOMEM;
1618 }
ba6edfcd 1619 per_cpu(tvec_bases, cpu) = base;
a4a6198b 1620 } else {
ba6edfcd
AM
1621 /*
1622 * This is for the boot CPU - we use compile-time
1623 * static initialisation because per-cpu memory isn't
1624 * ready yet and because the memory allocators are not
1625 * initialised either.
1626 */
a4a6198b 1627 boot_done = 1;
ba6edfcd 1628 base = &boot_tvec_bases;
a4a6198b 1629 }
ba6edfcd
AM
1630 tvec_base_done[cpu] = 1;
1631 } else {
1632 base = per_cpu(tvec_bases, cpu);
a4a6198b 1633 }
ba6edfcd 1634
3691c519 1635 spin_lock_init(&base->lock);
d730e882 1636
1da177e4
LT
1637 for (j = 0; j < TVN_SIZE; j++) {
1638 INIT_LIST_HEAD(base->tv5.vec + j);
1639 INIT_LIST_HEAD(base->tv4.vec + j);
1640 INIT_LIST_HEAD(base->tv3.vec + j);
1641 INIT_LIST_HEAD(base->tv2.vec + j);
1642 }
1643 for (j = 0; j < TVR_SIZE; j++)
1644 INIT_LIST_HEAD(base->tv1.vec + j);
1645
1646 base->timer_jiffies = jiffies;
97fd9ed4 1647 base->next_timer = base->timer_jiffies;
a4a6198b 1648 return 0;
1da177e4
LT
1649}
1650
1651#ifdef CONFIG_HOTPLUG_CPU
a6fa8e5a 1652static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
1da177e4
LT
1653{
1654 struct timer_list *timer;
1655
1656 while (!list_empty(head)) {
b5e61818 1657 timer = list_first_entry(head, struct timer_list, entry);
55c888d6 1658 detach_timer(timer, 0);
6e453a67 1659 timer_set_base(timer, new_base);
97fd9ed4
MS
1660 if (time_before(timer->expires, new_base->next_timer) &&
1661 !tbase_get_deferrable(timer->base))
1662 new_base->next_timer = timer->expires;
1da177e4 1663 internal_add_timer(new_base, timer);
1da177e4 1664 }
1da177e4
LT
1665}
1666
48ccf3da 1667static void __cpuinit migrate_timers(int cpu)
1da177e4 1668{
a6fa8e5a
PM
1669 struct tvec_base *old_base;
1670 struct tvec_base *new_base;
1da177e4
LT
1671 int i;
1672
1673 BUG_ON(cpu_online(cpu));
a4a6198b
JB
1674 old_base = per_cpu(tvec_bases, cpu);
1675 new_base = get_cpu_var(tvec_bases);
d82f0b0f
ON
1676 /*
1677 * The caller is globally serialized and nobody else
1678 * takes two locks at once, deadlock is not possible.
1679 */
1680 spin_lock_irq(&new_base->lock);
0d180406 1681 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
3691c519
ON
1682
1683 BUG_ON(old_base->running_timer);
1da177e4 1684
1da177e4 1685 for (i = 0; i < TVR_SIZE; i++)
55c888d6
ON
1686 migrate_timer_list(new_base, old_base->tv1.vec + i);
1687 for (i = 0; i < TVN_SIZE; i++) {
1688 migrate_timer_list(new_base, old_base->tv2.vec + i);
1689 migrate_timer_list(new_base, old_base->tv3.vec + i);
1690 migrate_timer_list(new_base, old_base->tv4.vec + i);
1691 migrate_timer_list(new_base, old_base->tv5.vec + i);
1692 }
1693
0d180406 1694 spin_unlock(&old_base->lock);
d82f0b0f 1695 spin_unlock_irq(&new_base->lock);
1da177e4 1696 put_cpu_var(tvec_bases);
1da177e4
LT
1697}
1698#endif /* CONFIG_HOTPLUG_CPU */
1699
8c78f307 1700static int __cpuinit timer_cpu_notify(struct notifier_block *self,
1da177e4
LT
1701 unsigned long action, void *hcpu)
1702{
1703 long cpu = (long)hcpu;
80b5184c
AM
1704 int err;
1705
1da177e4
LT
1706 switch(action) {
1707 case CPU_UP_PREPARE:
8bb78442 1708 case CPU_UP_PREPARE_FROZEN:
80b5184c
AM
1709 err = init_timers_cpu(cpu);
1710 if (err < 0)
1711 return notifier_from_errno(err);
1da177e4
LT
1712 break;
1713#ifdef CONFIG_HOTPLUG_CPU
1714 case CPU_DEAD:
8bb78442 1715 case CPU_DEAD_FROZEN:
1da177e4
LT
1716 migrate_timers(cpu);
1717 break;
1718#endif
1719 default:
1720 break;
1721 }
1722 return NOTIFY_OK;
1723}
1724
8c78f307 1725static struct notifier_block __cpuinitdata timers_nb = {
1da177e4
LT
1726 .notifier_call = timer_cpu_notify,
1727};
1728
1729
1730void __init init_timers(void)
1731{
07dccf33 1732 int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1da177e4 1733 (void *)(long)smp_processor_id());
07dccf33 1734
82f67cd9
IM
1735 init_timer_stats();
1736
9e506f7a 1737 BUG_ON(err != NOTIFY_OK);
1da177e4 1738 register_cpu_notifier(&timers_nb);
962cf36c 1739 open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
1da177e4
LT
1740}
1741
1da177e4
LT
1742/**
1743 * msleep - sleep safely even with waitqueue interruptions
1744 * @msecs: Time in milliseconds to sleep for
1745 */
1746void msleep(unsigned int msecs)
1747{
1748 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1749
75bcc8c5
NA
1750 while (timeout)
1751 timeout = schedule_timeout_uninterruptible(timeout);
1da177e4
LT
1752}
1753
1754EXPORT_SYMBOL(msleep);
1755
1756/**
96ec3efd 1757 * msleep_interruptible - sleep waiting for signals
1da177e4
LT
1758 * @msecs: Time in milliseconds to sleep for
1759 */
1760unsigned long msleep_interruptible(unsigned int msecs)
1761{
1762 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1763
75bcc8c5
NA
1764 while (timeout && !signal_pending(current))
1765 timeout = schedule_timeout_interruptible(timeout);
1da177e4
LT
1766 return jiffies_to_msecs(timeout);
1767}
1768
1769EXPORT_SYMBOL(msleep_interruptible);
5e7f5a17
PP
1770
1771static int __sched do_usleep_range(unsigned long min, unsigned long max)
1772{
1773 ktime_t kmin;
1774 unsigned long delta;
1775
1776 kmin = ktime_set(0, min * NSEC_PER_USEC);
1777 delta = (max - min) * NSEC_PER_USEC;
1778 return schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL);
1779}
1780
1781/**
1782 * usleep_range - Drop in replacement for udelay where wakeup is flexible
1783 * @min: Minimum time in usecs to sleep
1784 * @max: Maximum time in usecs to sleep
1785 */
1786void usleep_range(unsigned long min, unsigned long max)
1787{
1788 __set_current_state(TASK_UNINTERRUPTIBLE);
1789 do_usleep_range(min, max);
1790}
1791EXPORT_SYMBOL(usleep_range);