Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
1da177e4
LT
40
41#include <asm/uaccess.h>
42#include <asm/unistd.h>
43#include <asm/div64.h>
44#include <asm/timex.h>
45#include <asm/io.h>
46
ecea8d19
TG
47u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
48
49EXPORT_SYMBOL(jiffies_64);
50
1da177e4
LT
51/*
52 * per-CPU timer vector definitions:
53 */
1da177e4
LT
54#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
55#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
56#define TVN_SIZE (1 << TVN_BITS)
57#define TVR_SIZE (1 << TVR_BITS)
58#define TVN_MASK (TVN_SIZE - 1)
59#define TVR_MASK (TVR_SIZE - 1)
60
a6fa8e5a 61struct tvec {
1da177e4 62 struct list_head vec[TVN_SIZE];
a6fa8e5a 63};
1da177e4 64
a6fa8e5a 65struct tvec_root {
1da177e4 66 struct list_head vec[TVR_SIZE];
a6fa8e5a 67};
1da177e4 68
a6fa8e5a 69struct tvec_base {
3691c519
ON
70 spinlock_t lock;
71 struct timer_list *running_timer;
1da177e4 72 unsigned long timer_jiffies;
a6fa8e5a
PM
73 struct tvec_root tv1;
74 struct tvec tv2;
75 struct tvec tv3;
76 struct tvec tv4;
77 struct tvec tv5;
6e453a67 78} ____cacheline_aligned;
1da177e4 79
a6fa8e5a 80struct tvec_base boot_tvec_bases;
3691c519 81EXPORT_SYMBOL(boot_tvec_bases);
a6fa8e5a 82static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
1da177e4 83
6e453a67 84/*
a6fa8e5a 85 * Note that all tvec_bases are 2 byte aligned and lower bit of
6e453a67
VP
86 * base in timer_list is guaranteed to be zero. Use the LSB for
87 * the new flag to indicate whether the timer is deferrable
88 */
89#define TBASE_DEFERRABLE_FLAG (0x1)
90
91/* Functions below help us manage 'deferrable' flag */
a6fa8e5a 92static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
6e453a67 93{
e9910846 94 return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);
6e453a67
VP
95}
96
a6fa8e5a 97static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
6e453a67 98{
a6fa8e5a 99 return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
6e453a67
VP
100}
101
102static inline void timer_set_deferrable(struct timer_list *timer)
103{
a6fa8e5a 104 timer->base = ((struct tvec_base *)((unsigned long)(timer->base) |
6819457d 105 TBASE_DEFERRABLE_FLAG));
6e453a67
VP
106}
107
108static inline void
a6fa8e5a 109timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
6e453a67 110{
a6fa8e5a 111 timer->base = (struct tvec_base *)((unsigned long)(new_base) |
6819457d 112 tbase_get_deferrable(timer->base));
6e453a67
VP
113}
114
4c36a5de
AV
115/**
116 * __round_jiffies - function to round jiffies to a full second
117 * @j: the time in (absolute) jiffies that should be rounded
118 * @cpu: the processor number on which the timeout will happen
119 *
72fd4a35 120 * __round_jiffies() rounds an absolute time in the future (in jiffies)
4c36a5de
AV
121 * up or down to (approximately) full seconds. This is useful for timers
122 * for which the exact time they fire does not matter too much, as long as
123 * they fire approximately every X seconds.
124 *
125 * By rounding these timers to whole seconds, all such timers will fire
126 * at the same time, rather than at various times spread out. The goal
127 * of this is to have the CPU wake up less, which saves power.
128 *
129 * The exact rounding is skewed for each processor to avoid all
130 * processors firing at the exact same time, which could lead
131 * to lock contention or spurious cache line bouncing.
132 *
72fd4a35 133 * The return value is the rounded version of the @j parameter.
4c36a5de
AV
134 */
135unsigned long __round_jiffies(unsigned long j, int cpu)
136{
137 int rem;
138 unsigned long original = j;
139
140 /*
141 * We don't want all cpus firing their timers at once hitting the
142 * same lock or cachelines, so we skew each extra cpu with an extra
143 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
144 * already did this.
145 * The skew is done by adding 3*cpunr, then round, then subtract this
146 * extra offset again.
147 */
148 j += cpu * 3;
149
150 rem = j % HZ;
151
152 /*
153 * If the target jiffie is just after a whole second (which can happen
154 * due to delays of the timer irq, long irq off times etc etc) then
155 * we should round down to the whole second, not up. Use 1/4th second
156 * as cutoff for this rounding as an extreme upper bound for this.
157 */
158 if (rem < HZ/4) /* 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
166 if (j <= jiffies) /* rounding ate our timeout entirely; */
167 return original;
168 return j;
169}
170EXPORT_SYMBOL_GPL(__round_jiffies);
171
172/**
173 * __round_jiffies_relative - function to round jiffies to a full second
174 * @j: the time in (relative) jiffies that should be rounded
175 * @cpu: the processor number on which the timeout will happen
176 *
72fd4a35 177 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
4c36a5de
AV
178 * up or down to (approximately) full seconds. This is useful for timers
179 * for which the exact time they fire does not matter too much, as long as
180 * they fire approximately every X seconds.
181 *
182 * By rounding these timers to whole seconds, all such timers will fire
183 * at the same time, rather than at various times spread out. The goal
184 * of this is to have the CPU wake up less, which saves power.
185 *
186 * The exact rounding is skewed for each processor to avoid all
187 * processors firing at the exact same time, which could lead
188 * to lock contention or spurious cache line bouncing.
189 *
72fd4a35 190 * The return value is the rounded version of the @j parameter.
4c36a5de
AV
191 */
192unsigned long __round_jiffies_relative(unsigned long j, int cpu)
193{
194 /*
195 * In theory the following code can skip a jiffy in case jiffies
196 * increments right between the addition and the later subtraction.
197 * However since the entire point of this function is to use approximate
198 * timeouts, it's entirely ok to not handle that.
199 */
200 return __round_jiffies(j + jiffies, cpu) - jiffies;
201}
202EXPORT_SYMBOL_GPL(__round_jiffies_relative);
203
204/**
205 * round_jiffies - function to round jiffies to a full second
206 * @j: the time in (absolute) jiffies that should be rounded
207 *
72fd4a35 208 * round_jiffies() rounds an absolute time in the future (in jiffies)
4c36a5de
AV
209 * up or down to (approximately) full seconds. This is useful for timers
210 * for which the exact time they fire does not matter too much, as long as
211 * they fire approximately every X seconds.
212 *
213 * By rounding these timers to whole seconds, all such timers will fire
214 * at the same time, rather than at various times spread out. The goal
215 * of this is to have the CPU wake up less, which saves power.
216 *
72fd4a35 217 * The return value is the rounded version of the @j parameter.
4c36a5de
AV
218 */
219unsigned long round_jiffies(unsigned long j)
220{
221 return __round_jiffies(j, raw_smp_processor_id());
222}
223EXPORT_SYMBOL_GPL(round_jiffies);
224
225/**
226 * round_jiffies_relative - function to round jiffies to a full second
227 * @j: the time in (relative) jiffies that should be rounded
228 *
72fd4a35 229 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
4c36a5de
AV
230 * up or down to (approximately) full seconds. This is useful for timers
231 * for which the exact time they fire does not matter too much, as long as
232 * they fire approximately every X seconds.
233 *
234 * By rounding these timers to whole seconds, all such timers will fire
235 * at the same time, rather than at various times spread out. The goal
236 * of this is to have the CPU wake up less, which saves power.
237 *
72fd4a35 238 * The return value is the rounded version of the @j parameter.
4c36a5de
AV
239 */
240unsigned long round_jiffies_relative(unsigned long j)
241{
242 return __round_jiffies_relative(j, raw_smp_processor_id());
243}
244EXPORT_SYMBOL_GPL(round_jiffies_relative);
245
246
a6fa8e5a 247static inline void set_running_timer(struct tvec_base *base,
1da177e4
LT
248 struct timer_list *timer)
249{
250#ifdef CONFIG_SMP
3691c519 251 base->running_timer = timer;
1da177e4
LT
252#endif
253}
254
a6fa8e5a 255static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
1da177e4
LT
256{
257 unsigned long expires = timer->expires;
258 unsigned long idx = expires - base->timer_jiffies;
259 struct list_head *vec;
260
261 if (idx < TVR_SIZE) {
262 int i = expires & TVR_MASK;
263 vec = base->tv1.vec + i;
264 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
265 int i = (expires >> TVR_BITS) & TVN_MASK;
266 vec = base->tv2.vec + i;
267 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
268 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
269 vec = base->tv3.vec + i;
270 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
271 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
272 vec = base->tv4.vec + i;
273 } else if ((signed long) idx < 0) {
274 /*
275 * Can happen if you add a timer with expires == jiffies,
276 * or you set a timer to go off in the past
277 */
278 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
279 } else {
280 int i;
281 /* If the timeout is larger than 0xffffffff on 64-bit
282 * architectures then we use the maximum timeout:
283 */
284 if (idx > 0xffffffffUL) {
285 idx = 0xffffffffUL;
286 expires = idx + base->timer_jiffies;
287 }
288 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
289 vec = base->tv5.vec + i;
290 }
291 /*
292 * Timers are FIFO:
293 */
294 list_add_tail(&timer->entry, vec);
295}
296
82f67cd9
IM
297#ifdef CONFIG_TIMER_STATS
298void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
299{
300 if (timer->start_site)
301 return;
302
303 timer->start_site = addr;
304 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
305 timer->start_pid = current->pid;
306}
c5c061b8
VP
307
308static void timer_stats_account_timer(struct timer_list *timer)
309{
310 unsigned int flag = 0;
311
312 if (unlikely(tbase_get_deferrable(timer->base)))
313 flag |= TIMER_STATS_FLAG_DEFERRABLE;
314
315 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
316 timer->function, timer->start_comm, flag);
317}
318
319#else
320static void timer_stats_account_timer(struct timer_list *timer) {}
82f67cd9
IM
321#endif
322
c6f3a97f
TG
323#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
324
325static struct debug_obj_descr timer_debug_descr;
326
327/*
328 * fixup_init is called when:
329 * - an active object is initialized
55c888d6 330 */
c6f3a97f
TG
331static int timer_fixup_init(void *addr, enum debug_obj_state state)
332{
333 struct timer_list *timer = addr;
334
335 switch (state) {
336 case ODEBUG_STATE_ACTIVE:
337 del_timer_sync(timer);
338 debug_object_init(timer, &timer_debug_descr);
339 return 1;
340 default:
341 return 0;
342 }
343}
344
345/*
346 * fixup_activate is called when:
347 * - an active object is activated
348 * - an unknown object is activated (might be a statically initialized object)
349 */
350static int timer_fixup_activate(void *addr, enum debug_obj_state state)
351{
352 struct timer_list *timer = addr;
353
354 switch (state) {
355
356 case ODEBUG_STATE_NOTAVAILABLE:
357 /*
358 * This is not really a fixup. The timer was
359 * statically initialized. We just make sure that it
360 * is tracked in the object tracker.
361 */
362 if (timer->entry.next == NULL &&
363 timer->entry.prev == TIMER_ENTRY_STATIC) {
364 debug_object_init(timer, &timer_debug_descr);
365 debug_object_activate(timer, &timer_debug_descr);
366 return 0;
367 } else {
368 WARN_ON_ONCE(1);
369 }
370 return 0;
371
372 case ODEBUG_STATE_ACTIVE:
373 WARN_ON(1);
374
375 default:
376 return 0;
377 }
378}
379
380/*
381 * fixup_free is called when:
382 * - an active object is freed
383 */
384static int timer_fixup_free(void *addr, enum debug_obj_state state)
385{
386 struct timer_list *timer = addr;
387
388 switch (state) {
389 case ODEBUG_STATE_ACTIVE:
390 del_timer_sync(timer);
391 debug_object_free(timer, &timer_debug_descr);
392 return 1;
393 default:
394 return 0;
395 }
396}
397
398static struct debug_obj_descr timer_debug_descr = {
399 .name = "timer_list",
400 .fixup_init = timer_fixup_init,
401 .fixup_activate = timer_fixup_activate,
402 .fixup_free = timer_fixup_free,
403};
404
405static inline void debug_timer_init(struct timer_list *timer)
406{
407 debug_object_init(timer, &timer_debug_descr);
408}
409
410static inline void debug_timer_activate(struct timer_list *timer)
411{
412 debug_object_activate(timer, &timer_debug_descr);
413}
414
415static inline void debug_timer_deactivate(struct timer_list *timer)
416{
417 debug_object_deactivate(timer, &timer_debug_descr);
418}
419
420static inline void debug_timer_free(struct timer_list *timer)
421{
422 debug_object_free(timer, &timer_debug_descr);
423}
424
425static void __init_timer(struct timer_list *timer);
426
427void init_timer_on_stack(struct timer_list *timer)
428{
429 debug_object_init_on_stack(timer, &timer_debug_descr);
430 __init_timer(timer);
431}
432EXPORT_SYMBOL_GPL(init_timer_on_stack);
433
434void destroy_timer_on_stack(struct timer_list *timer)
435{
436 debug_object_free(timer, &timer_debug_descr);
437}
438EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
439
440#else
441static inline void debug_timer_init(struct timer_list *timer) { }
442static inline void debug_timer_activate(struct timer_list *timer) { }
443static inline void debug_timer_deactivate(struct timer_list *timer) { }
444#endif
445
446static void __init_timer(struct timer_list *timer)
55c888d6
ON
447{
448 timer->entry.next = NULL;
bfe5d834 449 timer->base = __raw_get_cpu_var(tvec_bases);
82f67cd9
IM
450#ifdef CONFIG_TIMER_STATS
451 timer->start_site = NULL;
452 timer->start_pid = -1;
453 memset(timer->start_comm, 0, TASK_COMM_LEN);
454#endif
55c888d6 455}
c6f3a97f
TG
456
457/**
458 * init_timer - initialize a timer.
459 * @timer: the timer to be initialized
460 *
461 * init_timer() must be done to a timer prior calling *any* of the
462 * other timer functions.
463 */
464void init_timer(struct timer_list *timer)
465{
466 debug_timer_init(timer);
467 __init_timer(timer);
468}
55c888d6
ON
469EXPORT_SYMBOL(init_timer);
470
7ad5b3a5 471void init_timer_deferrable(struct timer_list *timer)
6e453a67
VP
472{
473 init_timer(timer);
474 timer_set_deferrable(timer);
475}
476EXPORT_SYMBOL(init_timer_deferrable);
477
55c888d6 478static inline void detach_timer(struct timer_list *timer,
82f67cd9 479 int clear_pending)
55c888d6
ON
480{
481 struct list_head *entry = &timer->entry;
482
c6f3a97f
TG
483 debug_timer_deactivate(timer);
484
55c888d6
ON
485 __list_del(entry->prev, entry->next);
486 if (clear_pending)
487 entry->next = NULL;
488 entry->prev = LIST_POISON2;
489}
490
491/*
3691c519 492 * We are using hashed locking: holding per_cpu(tvec_bases).lock
55c888d6
ON
493 * means that all timers which are tied to this base via timer->base are
494 * locked, and the base itself is locked too.
495 *
496 * So __run_timers/migrate_timers can safely modify all timers which could
497 * be found on ->tvX lists.
498 *
499 * When the timer's base is locked, and the timer removed from list, it is
500 * possible to set timer->base = NULL and drop the lock: the timer remains
501 * locked.
502 */
a6fa8e5a 503static struct tvec_base *lock_timer_base(struct timer_list *timer,
55c888d6 504 unsigned long *flags)
89e7e374 505 __acquires(timer->base->lock)
55c888d6 506{
a6fa8e5a 507 struct tvec_base *base;
55c888d6
ON
508
509 for (;;) {
a6fa8e5a 510 struct tvec_base *prelock_base = timer->base;
6e453a67 511 base = tbase_get_base(prelock_base);
55c888d6
ON
512 if (likely(base != NULL)) {
513 spin_lock_irqsave(&base->lock, *flags);
6e453a67 514 if (likely(prelock_base == timer->base))
55c888d6
ON
515 return base;
516 /* The timer has migrated to another CPU */
517 spin_unlock_irqrestore(&base->lock, *flags);
518 }
519 cpu_relax();
520 }
521}
522
1da177e4
LT
523int __mod_timer(struct timer_list *timer, unsigned long expires)
524{
a6fa8e5a 525 struct tvec_base *base, *new_base;
1da177e4
LT
526 unsigned long flags;
527 int ret = 0;
528
82f67cd9 529 timer_stats_timer_set_start_info(timer);
1da177e4 530 BUG_ON(!timer->function);
1da177e4 531
55c888d6
ON
532 base = lock_timer_base(timer, &flags);
533
534 if (timer_pending(timer)) {
535 detach_timer(timer, 0);
536 ret = 1;
537 }
538
c6f3a97f
TG
539 debug_timer_activate(timer);
540
a4a6198b 541 new_base = __get_cpu_var(tvec_bases);
1da177e4 542
3691c519 543 if (base != new_base) {
1da177e4 544 /*
55c888d6
ON
545 * We are trying to schedule the timer on the local CPU.
546 * However we can't change timer's base while it is running,
547 * otherwise del_timer_sync() can't detect that the timer's
548 * handler yet has not finished. This also guarantees that
549 * the timer is serialized wrt itself.
1da177e4 550 */
a2c348fe 551 if (likely(base->running_timer != timer)) {
55c888d6 552 /* See the comment in lock_timer_base() */
6e453a67 553 timer_set_base(timer, NULL);
55c888d6 554 spin_unlock(&base->lock);
a2c348fe
ON
555 base = new_base;
556 spin_lock(&base->lock);
6e453a67 557 timer_set_base(timer, base);
1da177e4
LT
558 }
559 }
560
1da177e4 561 timer->expires = expires;
a2c348fe
ON
562 internal_add_timer(base, timer);
563 spin_unlock_irqrestore(&base->lock, flags);
1da177e4
LT
564
565 return ret;
566}
567
568EXPORT_SYMBOL(__mod_timer);
569
2aae4a10 570/**
1da177e4
LT
571 * add_timer_on - start a timer on a particular CPU
572 * @timer: the timer to be added
573 * @cpu: the CPU to start it on
574 *
575 * This is not very scalable on SMP. Double adds are not possible.
576 */
577void add_timer_on(struct timer_list *timer, int cpu)
578{
a6fa8e5a 579 struct tvec_base *base = per_cpu(tvec_bases, cpu);
6819457d 580 unsigned long flags;
55c888d6 581
82f67cd9 582 timer_stats_timer_set_start_info(timer);
6819457d 583 BUG_ON(timer_pending(timer) || !timer->function);
3691c519 584 spin_lock_irqsave(&base->lock, flags);
6e453a67 585 timer_set_base(timer, base);
c6f3a97f 586 debug_timer_activate(timer);
1da177e4 587 internal_add_timer(base, timer);
06d8308c
TG
588 /*
589 * Check whether the other CPU is idle and needs to be
590 * triggered to reevaluate the timer wheel when nohz is
591 * active. We are protected against the other CPU fiddling
592 * with the timer by holding the timer base lock. This also
593 * makes sure that a CPU on the way to idle can not evaluate
594 * the timer wheel.
595 */
596 wake_up_idle_cpu(cpu);
3691c519 597 spin_unlock_irqrestore(&base->lock, flags);
1da177e4
LT
598}
599
2aae4a10 600/**
1da177e4
LT
601 * mod_timer - modify a timer's timeout
602 * @timer: the timer to be modified
2aae4a10 603 * @expires: new timeout in jiffies
1da177e4 604 *
72fd4a35 605 * mod_timer() is a more efficient way to update the expire field of an
1da177e4
LT
606 * active timer (if the timer is inactive it will be activated)
607 *
608 * mod_timer(timer, expires) is equivalent to:
609 *
610 * del_timer(timer); timer->expires = expires; add_timer(timer);
611 *
612 * Note that if there are multiple unserialized concurrent users of the
613 * same timer, then mod_timer() is the only safe way to modify the timeout,
614 * since add_timer() cannot modify an already running timer.
615 *
616 * The function returns whether it has modified a pending timer or not.
617 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
618 * active timer returns 1.)
619 */
620int mod_timer(struct timer_list *timer, unsigned long expires)
621{
622 BUG_ON(!timer->function);
623
82f67cd9 624 timer_stats_timer_set_start_info(timer);
1da177e4
LT
625 /*
626 * This is a common optimization triggered by the
627 * networking code - if the timer is re-modified
628 * to be the same thing then just return:
629 */
630 if (timer->expires == expires && timer_pending(timer))
631 return 1;
632
633 return __mod_timer(timer, expires);
634}
635
636EXPORT_SYMBOL(mod_timer);
637
2aae4a10 638/**
1da177e4
LT
639 * del_timer - deactive a timer.
640 * @timer: the timer to be deactivated
641 *
642 * del_timer() deactivates a timer - this works on both active and inactive
643 * timers.
644 *
645 * The function returns whether it has deactivated a pending timer or not.
646 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
647 * active timer returns 1.)
648 */
649int del_timer(struct timer_list *timer)
650{
a6fa8e5a 651 struct tvec_base *base;
1da177e4 652 unsigned long flags;
55c888d6 653 int ret = 0;
1da177e4 654
82f67cd9 655 timer_stats_timer_clear_start_info(timer);
55c888d6
ON
656 if (timer_pending(timer)) {
657 base = lock_timer_base(timer, &flags);
658 if (timer_pending(timer)) {
659 detach_timer(timer, 1);
660 ret = 1;
661 }
1da177e4 662 spin_unlock_irqrestore(&base->lock, flags);
1da177e4 663 }
1da177e4 664
55c888d6 665 return ret;
1da177e4
LT
666}
667
668EXPORT_SYMBOL(del_timer);
669
670#ifdef CONFIG_SMP
2aae4a10
REB
671/**
672 * try_to_del_timer_sync - Try to deactivate a timer
673 * @timer: timer do del
674 *
fd450b73
ON
675 * This function tries to deactivate a timer. Upon successful (ret >= 0)
676 * exit the timer is not queued and the handler is not running on any CPU.
677 *
678 * It must not be called from interrupt contexts.
679 */
680int try_to_del_timer_sync(struct timer_list *timer)
681{
a6fa8e5a 682 struct tvec_base *base;
fd450b73
ON
683 unsigned long flags;
684 int ret = -1;
685
686 base = lock_timer_base(timer, &flags);
687
688 if (base->running_timer == timer)
689 goto out;
690
691 ret = 0;
692 if (timer_pending(timer)) {
693 detach_timer(timer, 1);
694 ret = 1;
695 }
696out:
697 spin_unlock_irqrestore(&base->lock, flags);
698
699 return ret;
700}
701
e19dff1f
DH
702EXPORT_SYMBOL(try_to_del_timer_sync);
703
2aae4a10 704/**
1da177e4
LT
705 * del_timer_sync - deactivate a timer and wait for the handler to finish.
706 * @timer: the timer to be deactivated
707 *
708 * This function only differs from del_timer() on SMP: besides deactivating
709 * the timer it also makes sure the handler has finished executing on other
710 * CPUs.
711 *
72fd4a35 712 * Synchronization rules: Callers must prevent restarting of the timer,
1da177e4
LT
713 * otherwise this function is meaningless. It must not be called from
714 * interrupt contexts. The caller must not hold locks which would prevent
55c888d6
ON
715 * completion of the timer's handler. The timer's handler must not call
716 * add_timer_on(). Upon exit the timer is not queued and the handler is
717 * not running on any CPU.
1da177e4
LT
718 *
719 * The function returns whether it has deactivated a pending timer or not.
1da177e4
LT
720 */
721int del_timer_sync(struct timer_list *timer)
722{
fd450b73
ON
723 for (;;) {
724 int ret = try_to_del_timer_sync(timer);
725 if (ret >= 0)
726 return ret;
a0009652 727 cpu_relax();
fd450b73 728 }
1da177e4 729}
1da177e4 730
55c888d6 731EXPORT_SYMBOL(del_timer_sync);
1da177e4
LT
732#endif
733
a6fa8e5a 734static int cascade(struct tvec_base *base, struct tvec *tv, int index)
1da177e4
LT
735{
736 /* cascade all the timers from tv up one level */
3439dd86
P
737 struct timer_list *timer, *tmp;
738 struct list_head tv_list;
739
740 list_replace_init(tv->vec + index, &tv_list);
1da177e4 741
1da177e4 742 /*
3439dd86
P
743 * We are removing _all_ timers from the list, so we
744 * don't have to detach them individually.
1da177e4 745 */
3439dd86 746 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
6e453a67 747 BUG_ON(tbase_get_base(timer->base) != base);
3439dd86 748 internal_add_timer(base, timer);
1da177e4 749 }
1da177e4
LT
750
751 return index;
752}
753
2aae4a10
REB
754#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
755
756/**
1da177e4
LT
757 * __run_timers - run all expired timers (if any) on this CPU.
758 * @base: the timer vector to be processed.
759 *
760 * This function cascades all vectors and executes all expired timer
761 * vectors.
762 */
a6fa8e5a 763static inline void __run_timers(struct tvec_base *base)
1da177e4
LT
764{
765 struct timer_list *timer;
766
3691c519 767 spin_lock_irq(&base->lock);
1da177e4 768 while (time_after_eq(jiffies, base->timer_jiffies)) {
626ab0e6 769 struct list_head work_list;
1da177e4 770 struct list_head *head = &work_list;
6819457d 771 int index = base->timer_jiffies & TVR_MASK;
626ab0e6 772
1da177e4
LT
773 /*
774 * Cascade timers:
775 */
776 if (!index &&
777 (!cascade(base, &base->tv2, INDEX(0))) &&
778 (!cascade(base, &base->tv3, INDEX(1))) &&
779 !cascade(base, &base->tv4, INDEX(2)))
780 cascade(base, &base->tv5, INDEX(3));
626ab0e6
ON
781 ++base->timer_jiffies;
782 list_replace_init(base->tv1.vec + index, &work_list);
55c888d6 783 while (!list_empty(head)) {
1da177e4
LT
784 void (*fn)(unsigned long);
785 unsigned long data;
786
b5e61818 787 timer = list_first_entry(head, struct timer_list,entry);
6819457d
TG
788 fn = timer->function;
789 data = timer->data;
1da177e4 790
82f67cd9
IM
791 timer_stats_account_timer(timer);
792
1da177e4 793 set_running_timer(base, timer);
55c888d6 794 detach_timer(timer, 1);
3691c519 795 spin_unlock_irq(&base->lock);
1da177e4 796 {
be5b4fbd 797 int preempt_count = preempt_count();
1da177e4
LT
798 fn(data);
799 if (preempt_count != preempt_count()) {
4c9dc641 800 printk(KERN_ERR "huh, entered %p "
be5b4fbd
JJ
801 "with preempt_count %08x, exited"
802 " with %08x?\n",
803 fn, preempt_count,
804 preempt_count());
1da177e4
LT
805 BUG();
806 }
807 }
3691c519 808 spin_lock_irq(&base->lock);
1da177e4
LT
809 }
810 }
811 set_running_timer(base, NULL);
3691c519 812 spin_unlock_irq(&base->lock);
1da177e4
LT
813}
814
ee9c5785 815#ifdef CONFIG_NO_HZ
1da177e4
LT
816/*
817 * Find out when the next timer event is due to happen. This
818 * is used on S/390 to stop all activity when a cpus is idle.
819 * This functions needs to be called disabled.
820 */
a6fa8e5a 821static unsigned long __next_timer_interrupt(struct tvec_base *base)
1da177e4 822{
1cfd6849 823 unsigned long timer_jiffies = base->timer_jiffies;
eaad084b 824 unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
1cfd6849 825 int index, slot, array, found = 0;
1da177e4 826 struct timer_list *nte;
a6fa8e5a 827 struct tvec *varray[4];
1da177e4
LT
828
829 /* Look for timer events in tv1. */
1cfd6849 830 index = slot = timer_jiffies & TVR_MASK;
1da177e4 831 do {
1cfd6849 832 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
6819457d
TG
833 if (tbase_get_deferrable(nte->base))
834 continue;
6e453a67 835
1cfd6849 836 found = 1;
1da177e4 837 expires = nte->expires;
1cfd6849
TG
838 /* Look at the cascade bucket(s)? */
839 if (!index || slot < index)
840 goto cascade;
841 return expires;
1da177e4 842 }
1cfd6849
TG
843 slot = (slot + 1) & TVR_MASK;
844 } while (slot != index);
845
846cascade:
847 /* Calculate the next cascade event */
848 if (index)
849 timer_jiffies += TVR_SIZE - index;
850 timer_jiffies >>= TVR_BITS;
1da177e4
LT
851
852 /* Check tv2-tv5. */
853 varray[0] = &base->tv2;
854 varray[1] = &base->tv3;
855 varray[2] = &base->tv4;
856 varray[3] = &base->tv5;
1cfd6849
TG
857
858 for (array = 0; array < 4; array++) {
a6fa8e5a 859 struct tvec *varp = varray[array];
1cfd6849
TG
860
861 index = slot = timer_jiffies & TVN_MASK;
1da177e4 862 do {
1cfd6849
TG
863 list_for_each_entry(nte, varp->vec + slot, entry) {
864 found = 1;
1da177e4
LT
865 if (time_before(nte->expires, expires))
866 expires = nte->expires;
1cfd6849
TG
867 }
868 /*
869 * Do we still search for the first timer or are
870 * we looking up the cascade buckets ?
871 */
872 if (found) {
873 /* Look at the cascade bucket(s)? */
874 if (!index || slot < index)
875 break;
876 return expires;
877 }
878 slot = (slot + 1) & TVN_MASK;
879 } while (slot != index);
880
881 if (index)
882 timer_jiffies += TVN_SIZE - index;
883 timer_jiffies >>= TVN_BITS;
1da177e4 884 }
1cfd6849
TG
885 return expires;
886}
69239749 887
1cfd6849
TG
888/*
889 * Check, if the next hrtimer event is before the next timer wheel
890 * event:
891 */
892static unsigned long cmp_next_hrtimer_event(unsigned long now,
893 unsigned long expires)
894{
895 ktime_t hr_delta = hrtimer_get_next_event();
896 struct timespec tsdelta;
9501b6cf 897 unsigned long delta;
1cfd6849
TG
898
899 if (hr_delta.tv64 == KTIME_MAX)
900 return expires;
0662b713 901
9501b6cf
TG
902 /*
903 * Expired timer available, let it expire in the next tick
904 */
905 if (hr_delta.tv64 <= 0)
906 return now + 1;
69239749 907
1cfd6849 908 tsdelta = ktime_to_timespec(hr_delta);
9501b6cf 909 delta = timespec_to_jiffies(&tsdelta);
eaad084b
TG
910
911 /*
912 * Limit the delta to the max value, which is checked in
913 * tick_nohz_stop_sched_tick():
914 */
915 if (delta > NEXT_TIMER_MAX_DELTA)
916 delta = NEXT_TIMER_MAX_DELTA;
917
9501b6cf
TG
918 /*
919 * Take rounding errors in to account and make sure, that it
920 * expires in the next tick. Otherwise we go into an endless
921 * ping pong due to tick_nohz_stop_sched_tick() retriggering
922 * the timer softirq
923 */
924 if (delta < 1)
925 delta = 1;
926 now += delta;
1cfd6849
TG
927 if (time_before(now, expires))
928 return now;
1da177e4
LT
929 return expires;
930}
1cfd6849
TG
931
932/**
8dce39c2 933 * get_next_timer_interrupt - return the jiffy of the next pending timer
05fb6bf0 934 * @now: current time (in jiffies)
1cfd6849 935 */
fd064b9b 936unsigned long get_next_timer_interrupt(unsigned long now)
1cfd6849 937{
a6fa8e5a 938 struct tvec_base *base = __get_cpu_var(tvec_bases);
fd064b9b 939 unsigned long expires;
1cfd6849
TG
940
941 spin_lock(&base->lock);
942 expires = __next_timer_interrupt(base);
943 spin_unlock(&base->lock);
944
945 if (time_before_eq(expires, now))
946 return now;
947
948 return cmp_next_hrtimer_event(now, expires);
949}
1da177e4
LT
950#endif
951
fa13a5a1
PM
952#ifndef CONFIG_VIRT_CPU_ACCOUNTING
953void account_process_tick(struct task_struct *p, int user_tick)
954{
06b8e878
MN
955 cputime_t one_jiffy = jiffies_to_cputime(1);
956
fa13a5a1 957 if (user_tick) {
06b8e878
MN
958 account_user_time(p, one_jiffy);
959 account_user_time_scaled(p, cputime_to_scaled(one_jiffy));
fa13a5a1 960 } else {
06b8e878
MN
961 account_system_time(p, HARDIRQ_OFFSET, one_jiffy);
962 account_system_time_scaled(p, cputime_to_scaled(one_jiffy));
fa13a5a1
PM
963 }
964}
965#endif
966
1da177e4 967/*
5b4db0c2 968 * Called from the timer interrupt handler to charge one tick to the current
1da177e4
LT
969 * process. user_tick is 1 if the tick is user time, 0 for system.
970 */
971void update_process_times(int user_tick)
972{
973 struct task_struct *p = current;
974 int cpu = smp_processor_id();
975
976 /* Note: this timer irq context must be accounted for as well. */
fa13a5a1 977 account_process_tick(p, user_tick);
1da177e4
LT
978 run_local_timers();
979 if (rcu_pending(cpu))
980 rcu_check_callbacks(cpu, user_tick);
b845b517 981 printk_tick();
1da177e4 982 scheduler_tick();
6819457d 983 run_posix_cpu_timers(p);
1da177e4
LT
984}
985
986/*
987 * Nr of active tasks - counted in fixed-point numbers
988 */
989static unsigned long count_active_tasks(void)
990{
db1b1fef 991 return nr_active() * FIXED_1;
1da177e4
LT
992}
993
994/*
995 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
996 * imply that avenrun[] is the standard name for this kind of thing.
997 * Nothing else seems to be standardized: the fractional size etc
998 * all seem to differ on different machines.
999 *
1000 * Requires xtime_lock to access.
1001 */
1002unsigned long avenrun[3];
1003
1004EXPORT_SYMBOL(avenrun);
1005
1006/*
1007 * calc_load - given tick count, update the avenrun load estimates.
1008 * This is called while holding a write_lock on xtime_lock.
1009 */
1010static inline void calc_load(unsigned long ticks)
1011{
1012 unsigned long active_tasks; /* fixed-point */
1013 static int count = LOAD_FREQ;
1014
cd7175ed
ED
1015 count -= ticks;
1016 if (unlikely(count < 0)) {
1017 active_tasks = count_active_tasks();
1018 do {
1019 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
1020 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
1021 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
1022 count += LOAD_FREQ;
1023 } while (count < 0);
1da177e4
LT
1024 }
1025}
1026
1da177e4
LT
1027/*
1028 * This function runs timers and the timer-tq in bottom half context.
1029 */
1030static void run_timer_softirq(struct softirq_action *h)
1031{
a6fa8e5a 1032 struct tvec_base *base = __get_cpu_var(tvec_bases);
1da177e4 1033
d3d74453 1034 hrtimer_run_pending();
82f67cd9 1035
1da177e4
LT
1036 if (time_after_eq(jiffies, base->timer_jiffies))
1037 __run_timers(base);
1038}
1039
1040/*
1041 * Called by the local, per-CPU timer interrupt on SMP.
1042 */
1043void run_local_timers(void)
1044{
d3d74453 1045 hrtimer_run_queues();
1da177e4 1046 raise_softirq(TIMER_SOFTIRQ);
6687a97d 1047 softlockup_tick();
1da177e4
LT
1048}
1049
1050/*
1051 * Called by the timer interrupt. xtime_lock must already be taken
1052 * by the timer IRQ!
1053 */
3171a030 1054static inline void update_times(unsigned long ticks)
1da177e4 1055{
ad596171 1056 update_wall_time();
1da177e4
LT
1057 calc_load(ticks);
1058}
6819457d 1059
1da177e4
LT
1060/*
1061 * The 64-bit jiffies value is not atomic - you MUST NOT read it
1062 * without sampling the sequence number in xtime_lock.
1063 * jiffies is defined in the linker script...
1064 */
1065
3171a030 1066void do_timer(unsigned long ticks)
1da177e4 1067{
3171a030
AN
1068 jiffies_64 += ticks;
1069 update_times(ticks);
1da177e4
LT
1070}
1071
1072#ifdef __ARCH_WANT_SYS_ALARM
1073
1074/*
1075 * For backwards compatibility? This can be done in libc so Alpha
1076 * and all newer ports shouldn't need it.
1077 */
1078asmlinkage unsigned long sys_alarm(unsigned int seconds)
1079{
c08b8a49 1080 return alarm_setitimer(seconds);
1da177e4
LT
1081}
1082
1083#endif
1084
1085#ifndef __alpha__
1086
1087/*
1088 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
1089 * should be moved into arch/i386 instead?
1090 */
1091
1092/**
1093 * sys_getpid - return the thread group id of the current process
1094 *
1095 * Note, despite the name, this returns the tgid not the pid. The tgid and
1096 * the pid are identical unless CLONE_THREAD was specified on clone() in
1097 * which case the tgid is the same in all threads of the same group.
1098 *
1099 * This is SMP safe as current->tgid does not change.
1100 */
1101asmlinkage long sys_getpid(void)
1102{
b488893a 1103 return task_tgid_vnr(current);
1da177e4
LT
1104}
1105
1106/*
6997a6fa
KK
1107 * Accessing ->real_parent is not SMP-safe, it could
1108 * change from under us. However, we can use a stale
1109 * value of ->real_parent under rcu_read_lock(), see
1110 * release_task()->call_rcu(delayed_put_task_struct).
1da177e4
LT
1111 */
1112asmlinkage long sys_getppid(void)
1113{
1114 int pid;
1da177e4 1115
6997a6fa 1116 rcu_read_lock();
6c5f3e7b 1117 pid = task_tgid_vnr(current->real_parent);
6997a6fa 1118 rcu_read_unlock();
1da177e4 1119
1da177e4
LT
1120 return pid;
1121}
1122
1123asmlinkage long sys_getuid(void)
1124{
1125 /* Only we change this so SMP safe */
1126 return current->uid;
1127}
1128
1129asmlinkage long sys_geteuid(void)
1130{
1131 /* Only we change this so SMP safe */
1132 return current->euid;
1133}
1134
1135asmlinkage long sys_getgid(void)
1136{
1137 /* Only we change this so SMP safe */
1138 return current->gid;
1139}
1140
1141asmlinkage long sys_getegid(void)
1142{
1143 /* Only we change this so SMP safe */
1144 return current->egid;
1145}
1146
1147#endif
1148
1149static void process_timeout(unsigned long __data)
1150{
36c8b586 1151 wake_up_process((struct task_struct *)__data);
1da177e4
LT
1152}
1153
1154/**
1155 * schedule_timeout - sleep until timeout
1156 * @timeout: timeout value in jiffies
1157 *
1158 * Make the current task sleep until @timeout jiffies have
1159 * elapsed. The routine will return immediately unless
1160 * the current task state has been set (see set_current_state()).
1161 *
1162 * You can set the task state as follows -
1163 *
1164 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1165 * pass before the routine returns. The routine will return 0
1166 *
1167 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1168 * delivered to the current task. In this case the remaining time
1169 * in jiffies will be returned, or 0 if the timer expired in time
1170 *
1171 * The current task state is guaranteed to be TASK_RUNNING when this
1172 * routine returns.
1173 *
1174 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1175 * the CPU away without a bound on the timeout. In this case the return
1176 * value will be %MAX_SCHEDULE_TIMEOUT.
1177 *
1178 * In all cases the return value is guaranteed to be non-negative.
1179 */
7ad5b3a5 1180signed long __sched schedule_timeout(signed long timeout)
1da177e4
LT
1181{
1182 struct timer_list timer;
1183 unsigned long expire;
1184
1185 switch (timeout)
1186 {
1187 case MAX_SCHEDULE_TIMEOUT:
1188 /*
1189 * These two special cases are useful to be comfortable
1190 * in the caller. Nothing more. We could take
1191 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1192 * but I' d like to return a valid offset (>=0) to allow
1193 * the caller to do everything it want with the retval.
1194 */
1195 schedule();
1196 goto out;
1197 default:
1198 /*
1199 * Another bit of PARANOID. Note that the retval will be
1200 * 0 since no piece of kernel is supposed to do a check
1201 * for a negative retval of schedule_timeout() (since it
1202 * should never happens anyway). You just have the printk()
1203 * that will tell you if something is gone wrong and where.
1204 */
5b149bcc 1205 if (timeout < 0) {
1da177e4 1206 printk(KERN_ERR "schedule_timeout: wrong timeout "
5b149bcc
AM
1207 "value %lx\n", timeout);
1208 dump_stack();
1da177e4
LT
1209 current->state = TASK_RUNNING;
1210 goto out;
1211 }
1212 }
1213
1214 expire = timeout + jiffies;
1215
c6f3a97f 1216 setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
a8db2db1 1217 __mod_timer(&timer, expire);
1da177e4
LT
1218 schedule();
1219 del_singleshot_timer_sync(&timer);
1220
c6f3a97f
TG
1221 /* Remove the timer from the object tracker */
1222 destroy_timer_on_stack(&timer);
1223
1da177e4
LT
1224 timeout = expire - jiffies;
1225
1226 out:
1227 return timeout < 0 ? 0 : timeout;
1228}
1da177e4
LT
1229EXPORT_SYMBOL(schedule_timeout);
1230
8a1c1757
AM
1231/*
1232 * We can use __set_current_state() here because schedule_timeout() calls
1233 * schedule() unconditionally.
1234 */
64ed93a2
NA
1235signed long __sched schedule_timeout_interruptible(signed long timeout)
1236{
a5a0d52c
AM
1237 __set_current_state(TASK_INTERRUPTIBLE);
1238 return schedule_timeout(timeout);
64ed93a2
NA
1239}
1240EXPORT_SYMBOL(schedule_timeout_interruptible);
1241
294d5cc2
MW
1242signed long __sched schedule_timeout_killable(signed long timeout)
1243{
1244 __set_current_state(TASK_KILLABLE);
1245 return schedule_timeout(timeout);
1246}
1247EXPORT_SYMBOL(schedule_timeout_killable);
1248
64ed93a2
NA
1249signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1250{
a5a0d52c
AM
1251 __set_current_state(TASK_UNINTERRUPTIBLE);
1252 return schedule_timeout(timeout);
64ed93a2
NA
1253}
1254EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1255
1da177e4
LT
1256/* Thread ID - the internal kernel "pid" */
1257asmlinkage long sys_gettid(void)
1258{
b488893a 1259 return task_pid_vnr(current);
1da177e4
LT
1260}
1261
2aae4a10 1262/**
d4d23add 1263 * do_sysinfo - fill in sysinfo struct
2aae4a10 1264 * @info: pointer to buffer to fill
6819457d 1265 */
d4d23add 1266int do_sysinfo(struct sysinfo *info)
1da177e4 1267{
1da177e4
LT
1268 unsigned long mem_total, sav_total;
1269 unsigned int mem_unit, bitcount;
1270 unsigned long seq;
1271
d4d23add 1272 memset(info, 0, sizeof(struct sysinfo));
1da177e4
LT
1273
1274 do {
1275 struct timespec tp;
1276 seq = read_seqbegin(&xtime_lock);
1277
1278 /*
1279 * This is annoying. The below is the same thing
1280 * posix_get_clock_monotonic() does, but it wants to
1281 * take the lock which we want to cover the loads stuff
1282 * too.
1283 */
1284
1285 getnstimeofday(&tp);
1286 tp.tv_sec += wall_to_monotonic.tv_sec;
1287 tp.tv_nsec += wall_to_monotonic.tv_nsec;
d6214141 1288 monotonic_to_bootbased(&tp);
1da177e4
LT
1289 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1290 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1291 tp.tv_sec++;
1292 }
d4d23add 1293 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1da177e4 1294
d4d23add
KM
1295 info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1296 info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1297 info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
1da177e4 1298
d4d23add 1299 info->procs = nr_threads;
1da177e4
LT
1300 } while (read_seqretry(&xtime_lock, seq));
1301
d4d23add
KM
1302 si_meminfo(info);
1303 si_swapinfo(info);
1da177e4
LT
1304
1305 /*
1306 * If the sum of all the available memory (i.e. ram + swap)
1307 * is less than can be stored in a 32 bit unsigned long then
1308 * we can be binary compatible with 2.2.x kernels. If not,
1309 * well, in that case 2.2.x was broken anyways...
1310 *
1311 * -Erik Andersen <andersee@debian.org>
1312 */
1313
d4d23add
KM
1314 mem_total = info->totalram + info->totalswap;
1315 if (mem_total < info->totalram || mem_total < info->totalswap)
1da177e4
LT
1316 goto out;
1317 bitcount = 0;
d4d23add 1318 mem_unit = info->mem_unit;
1da177e4
LT
1319 while (mem_unit > 1) {
1320 bitcount++;
1321 mem_unit >>= 1;
1322 sav_total = mem_total;
1323 mem_total <<= 1;
1324 if (mem_total < sav_total)
1325 goto out;
1326 }
1327
1328 /*
1329 * If mem_total did not overflow, multiply all memory values by
d4d23add 1330 * info->mem_unit and set it to 1. This leaves things compatible
1da177e4
LT
1331 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1332 * kernels...
1333 */
1334
d4d23add
KM
1335 info->mem_unit = 1;
1336 info->totalram <<= bitcount;
1337 info->freeram <<= bitcount;
1338 info->sharedram <<= bitcount;
1339 info->bufferram <<= bitcount;
1340 info->totalswap <<= bitcount;
1341 info->freeswap <<= bitcount;
1342 info->totalhigh <<= bitcount;
1343 info->freehigh <<= bitcount;
1344
1345out:
1346 return 0;
1347}
1348
1349asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1350{
1351 struct sysinfo val;
1352
1353 do_sysinfo(&val);
1da177e4 1354
1da177e4
LT
1355 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1356 return -EFAULT;
1357
1358 return 0;
1359}
1360
b4be6258 1361static int __cpuinit init_timers_cpu(int cpu)
1da177e4
LT
1362{
1363 int j;
a6fa8e5a 1364 struct tvec_base *base;
b4be6258 1365 static char __cpuinitdata tvec_base_done[NR_CPUS];
55c888d6 1366
ba6edfcd 1367 if (!tvec_base_done[cpu]) {
a4a6198b
JB
1368 static char boot_done;
1369
a4a6198b 1370 if (boot_done) {
ba6edfcd
AM
1371 /*
1372 * The APs use this path later in boot
1373 */
94f6030c
CL
1374 base = kmalloc_node(sizeof(*base),
1375 GFP_KERNEL | __GFP_ZERO,
a4a6198b
JB
1376 cpu_to_node(cpu));
1377 if (!base)
1378 return -ENOMEM;
6e453a67
VP
1379
1380 /* Make sure that tvec_base is 2 byte aligned */
1381 if (tbase_get_deferrable(base)) {
1382 WARN_ON(1);
1383 kfree(base);
1384 return -ENOMEM;
1385 }
ba6edfcd 1386 per_cpu(tvec_bases, cpu) = base;
a4a6198b 1387 } else {
ba6edfcd
AM
1388 /*
1389 * This is for the boot CPU - we use compile-time
1390 * static initialisation because per-cpu memory isn't
1391 * ready yet and because the memory allocators are not
1392 * initialised either.
1393 */
a4a6198b 1394 boot_done = 1;
ba6edfcd 1395 base = &boot_tvec_bases;
a4a6198b 1396 }
ba6edfcd
AM
1397 tvec_base_done[cpu] = 1;
1398 } else {
1399 base = per_cpu(tvec_bases, cpu);
a4a6198b 1400 }
ba6edfcd 1401
3691c519 1402 spin_lock_init(&base->lock);
d730e882 1403
1da177e4
LT
1404 for (j = 0; j < TVN_SIZE; j++) {
1405 INIT_LIST_HEAD(base->tv5.vec + j);
1406 INIT_LIST_HEAD(base->tv4.vec + j);
1407 INIT_LIST_HEAD(base->tv3.vec + j);
1408 INIT_LIST_HEAD(base->tv2.vec + j);
1409 }
1410 for (j = 0; j < TVR_SIZE; j++)
1411 INIT_LIST_HEAD(base->tv1.vec + j);
1412
1413 base->timer_jiffies = jiffies;
a4a6198b 1414 return 0;
1da177e4
LT
1415}
1416
1417#ifdef CONFIG_HOTPLUG_CPU
a6fa8e5a 1418static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
1da177e4
LT
1419{
1420 struct timer_list *timer;
1421
1422 while (!list_empty(head)) {
b5e61818 1423 timer = list_first_entry(head, struct timer_list, entry);
55c888d6 1424 detach_timer(timer, 0);
6e453a67 1425 timer_set_base(timer, new_base);
1da177e4 1426 internal_add_timer(new_base, timer);
1da177e4 1427 }
1da177e4
LT
1428}
1429
48ccf3da 1430static void __cpuinit migrate_timers(int cpu)
1da177e4 1431{
a6fa8e5a
PM
1432 struct tvec_base *old_base;
1433 struct tvec_base *new_base;
1da177e4
LT
1434 int i;
1435
1436 BUG_ON(cpu_online(cpu));
a4a6198b
JB
1437 old_base = per_cpu(tvec_bases, cpu);
1438 new_base = get_cpu_var(tvec_bases);
1da177e4
LT
1439
1440 local_irq_disable();
0d180406
ON
1441 spin_lock(&new_base->lock);
1442 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
3691c519
ON
1443
1444 BUG_ON(old_base->running_timer);
1da177e4 1445
1da177e4 1446 for (i = 0; i < TVR_SIZE; i++)
55c888d6
ON
1447 migrate_timer_list(new_base, old_base->tv1.vec + i);
1448 for (i = 0; i < TVN_SIZE; i++) {
1449 migrate_timer_list(new_base, old_base->tv2.vec + i);
1450 migrate_timer_list(new_base, old_base->tv3.vec + i);
1451 migrate_timer_list(new_base, old_base->tv4.vec + i);
1452 migrate_timer_list(new_base, old_base->tv5.vec + i);
1453 }
1454
0d180406
ON
1455 spin_unlock(&old_base->lock);
1456 spin_unlock(&new_base->lock);
1da177e4
LT
1457 local_irq_enable();
1458 put_cpu_var(tvec_bases);
1da177e4
LT
1459}
1460#endif /* CONFIG_HOTPLUG_CPU */
1461
8c78f307 1462static int __cpuinit timer_cpu_notify(struct notifier_block *self,
1da177e4
LT
1463 unsigned long action, void *hcpu)
1464{
1465 long cpu = (long)hcpu;
1466 switch(action) {
1467 case CPU_UP_PREPARE:
8bb78442 1468 case CPU_UP_PREPARE_FROZEN:
a4a6198b
JB
1469 if (init_timers_cpu(cpu) < 0)
1470 return NOTIFY_BAD;
1da177e4
LT
1471 break;
1472#ifdef CONFIG_HOTPLUG_CPU
1473 case CPU_DEAD:
8bb78442 1474 case CPU_DEAD_FROZEN:
1da177e4
LT
1475 migrate_timers(cpu);
1476 break;
1477#endif
1478 default:
1479 break;
1480 }
1481 return NOTIFY_OK;
1482}
1483
8c78f307 1484static struct notifier_block __cpuinitdata timers_nb = {
1da177e4
LT
1485 .notifier_call = timer_cpu_notify,
1486};
1487
1488
1489void __init init_timers(void)
1490{
07dccf33 1491 int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1da177e4 1492 (void *)(long)smp_processor_id());
07dccf33 1493
82f67cd9
IM
1494 init_timer_stats();
1495
07dccf33 1496 BUG_ON(err == NOTIFY_BAD);
1da177e4 1497 register_cpu_notifier(&timers_nb);
962cf36c 1498 open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
1da177e4
LT
1499}
1500
1da177e4
LT
1501/**
1502 * msleep - sleep safely even with waitqueue interruptions
1503 * @msecs: Time in milliseconds to sleep for
1504 */
1505void msleep(unsigned int msecs)
1506{
1507 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1508
75bcc8c5
NA
1509 while (timeout)
1510 timeout = schedule_timeout_uninterruptible(timeout);
1da177e4
LT
1511}
1512
1513EXPORT_SYMBOL(msleep);
1514
1515/**
96ec3efd 1516 * msleep_interruptible - sleep waiting for signals
1da177e4
LT
1517 * @msecs: Time in milliseconds to sleep for
1518 */
1519unsigned long msleep_interruptible(unsigned int msecs)
1520{
1521 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1522
75bcc8c5
NA
1523 while (timeout && !signal_pending(current))
1524 timeout = schedule_timeout_interruptible(timeout);
1da177e4
LT
1525 return jiffies_to_msecs(timeout);
1526}
1527
1528EXPORT_SYMBOL(msleep_interruptible);