]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/timer.c
time: SMP friendly alignment of struct clocksource
[net-next-2.6.git] / kernel / timer.c
CommitLineData
1da177e4
LT
1/*
2 * linux/kernel/timer.c
3 *
4 * Kernel internal timers, kernel timekeeping, basic process system calls
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>
29#include <linux/notifier.h>
30#include <linux/thread_info.h>
31#include <linux/time.h>
32#include <linux/jiffies.h>
33#include <linux/posix-timers.h>
34#include <linux/cpu.h>
35#include <linux/syscalls.h>
97a41e26 36#include <linux/delay.h>
79bf2bb3 37#include <linux/tick.h>
82f67cd9 38#include <linux/kallsyms.h>
1da177e4
LT
39
40#include <asm/uaccess.h>
41#include <asm/unistd.h>
42#include <asm/div64.h>
43#include <asm/timex.h>
44#include <asm/io.h>
45
ecea8d19
TG
46u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
47
48EXPORT_SYMBOL(jiffies_64);
49
1da177e4
LT
50/*
51 * per-CPU timer vector definitions:
52 */
1da177e4
LT
53#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
54#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
55#define TVN_SIZE (1 << TVN_BITS)
56#define TVR_SIZE (1 << TVR_BITS)
57#define TVN_MASK (TVN_SIZE - 1)
58#define TVR_MASK (TVR_SIZE - 1)
59
60typedef struct tvec_s {
61 struct list_head vec[TVN_SIZE];
62} tvec_t;
63
64typedef struct tvec_root_s {
65 struct list_head vec[TVR_SIZE];
66} tvec_root_t;
67
68struct tvec_t_base_s {
3691c519
ON
69 spinlock_t lock;
70 struct timer_list *running_timer;
1da177e4 71 unsigned long timer_jiffies;
1da177e4
LT
72 tvec_root_t tv1;
73 tvec_t tv2;
74 tvec_t tv3;
75 tvec_t tv4;
76 tvec_t tv5;
6e453a67 77} ____cacheline_aligned;
1da177e4
LT
78
79typedef struct tvec_t_base_s tvec_base_t;
ba6edfcd 80
3691c519
ON
81tvec_base_t boot_tvec_bases;
82EXPORT_SYMBOL(boot_tvec_bases);
51d8c5ed 83static DEFINE_PER_CPU(tvec_base_t *, tvec_bases) = &boot_tvec_bases;
1da177e4 84
6e453a67
VP
85/*
86 * Note that all tvec_bases is 2 byte aligned and lower bit of
87 * base in timer_list is guaranteed to be zero. Use the LSB for
88 * the new flag to indicate whether the timer is deferrable
89 */
90#define TBASE_DEFERRABLE_FLAG (0x1)
91
92/* Functions below help us manage 'deferrable' flag */
93static inline unsigned int tbase_get_deferrable(tvec_base_t *base)
94{
95 return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);
96}
97
98static inline tvec_base_t *tbase_get_base(tvec_base_t *base)
99{
100 return ((tvec_base_t *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
101}
102
103static inline void timer_set_deferrable(struct timer_list *timer)
104{
105 timer->base = ((tvec_base_t *)((unsigned long)(timer->base) |
106 TBASE_DEFERRABLE_FLAG));
107}
108
109static inline void
110timer_set_base(struct timer_list *timer, tvec_base_t *new_base)
111{
112 timer->base = (tvec_base_t *)((unsigned long)(new_base) |
113 tbase_get_deferrable(timer->base));
114}
115
4c36a5de
AV
116/**
117 * __round_jiffies - function to round jiffies to a full second
118 * @j: the time in (absolute) jiffies that should be rounded
119 * @cpu: the processor number on which the timeout will happen
120 *
72fd4a35 121 * __round_jiffies() rounds an absolute time in the future (in jiffies)
4c36a5de
AV
122 * up or down to (approximately) full seconds. This is useful for timers
123 * for which the exact time they fire does not matter too much, as long as
124 * they fire approximately every X seconds.
125 *
126 * By rounding these timers to whole seconds, all such timers will fire
127 * at the same time, rather than at various times spread out. The goal
128 * of this is to have the CPU wake up less, which saves power.
129 *
130 * The exact rounding is skewed for each processor to avoid all
131 * processors firing at the exact same time, which could lead
132 * to lock contention or spurious cache line bouncing.
133 *
72fd4a35 134 * The return value is the rounded version of the @j parameter.
4c36a5de
AV
135 */
136unsigned long __round_jiffies(unsigned long j, int cpu)
137{
138 int rem;
139 unsigned long original = j;
140
141 /*
142 * We don't want all cpus firing their timers at once hitting the
143 * same lock or cachelines, so we skew each extra cpu with an extra
144 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
145 * already did this.
146 * The skew is done by adding 3*cpunr, then round, then subtract this
147 * extra offset again.
148 */
149 j += cpu * 3;
150
151 rem = j % HZ;
152
153 /*
154 * If the target jiffie is just after a whole second (which can happen
155 * due to delays of the timer irq, long irq off times etc etc) then
156 * we should round down to the whole second, not up. Use 1/4th second
157 * as cutoff for this rounding as an extreme upper bound for this.
158 */
159 if (rem < HZ/4) /* round down */
160 j = j - rem;
161 else /* round up */
162 j = j - rem + HZ;
163
164 /* now that we have rounded, subtract the extra skew again */
165 j -= cpu * 3;
166
167 if (j <= jiffies) /* rounding ate our timeout entirely; */
168 return original;
169 return j;
170}
171EXPORT_SYMBOL_GPL(__round_jiffies);
172
173/**
174 * __round_jiffies_relative - function to round jiffies to a full second
175 * @j: the time in (relative) jiffies that should be rounded
176 * @cpu: the processor number on which the timeout will happen
177 *
72fd4a35 178 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
4c36a5de
AV
179 * up or down to (approximately) full seconds. This is useful for timers
180 * for which the exact time they fire does not matter too much, as long as
181 * they fire approximately every X seconds.
182 *
183 * By rounding these timers to whole seconds, all such timers will fire
184 * at the same time, rather than at various times spread out. The goal
185 * of this is to have the CPU wake up less, which saves power.
186 *
187 * The exact rounding is skewed for each processor to avoid all
188 * processors firing at the exact same time, which could lead
189 * to lock contention or spurious cache line bouncing.
190 *
72fd4a35 191 * The return value is the rounded version of the @j parameter.
4c36a5de
AV
192 */
193unsigned long __round_jiffies_relative(unsigned long j, int cpu)
194{
195 /*
196 * In theory the following code can skip a jiffy in case jiffies
197 * increments right between the addition and the later subtraction.
198 * However since the entire point of this function is to use approximate
199 * timeouts, it's entirely ok to not handle that.
200 */
201 return __round_jiffies(j + jiffies, cpu) - jiffies;
202}
203EXPORT_SYMBOL_GPL(__round_jiffies_relative);
204
205/**
206 * round_jiffies - function to round jiffies to a full second
207 * @j: the time in (absolute) jiffies that should be rounded
208 *
72fd4a35 209 * round_jiffies() rounds an absolute time in the future (in jiffies)
4c36a5de
AV
210 * up or down to (approximately) full seconds. This is useful for timers
211 * for which the exact time they fire does not matter too much, as long as
212 * they fire approximately every X seconds.
213 *
214 * By rounding these timers to whole seconds, all such timers will fire
215 * at the same time, rather than at various times spread out. The goal
216 * of this is to have the CPU wake up less, which saves power.
217 *
72fd4a35 218 * The return value is the rounded version of the @j parameter.
4c36a5de
AV
219 */
220unsigned long round_jiffies(unsigned long j)
221{
222 return __round_jiffies(j, raw_smp_processor_id());
223}
224EXPORT_SYMBOL_GPL(round_jiffies);
225
226/**
227 * round_jiffies_relative - function to round jiffies to a full second
228 * @j: the time in (relative) jiffies that should be rounded
229 *
72fd4a35 230 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
4c36a5de
AV
231 * up or down to (approximately) full seconds. This is useful for timers
232 * for which the exact time they fire does not matter too much, as long as
233 * they fire approximately every X seconds.
234 *
235 * By rounding these timers to whole seconds, all such timers will fire
236 * at the same time, rather than at various times spread out. The goal
237 * of this is to have the CPU wake up less, which saves power.
238 *
72fd4a35 239 * The return value is the rounded version of the @j parameter.
4c36a5de
AV
240 */
241unsigned long round_jiffies_relative(unsigned long j)
242{
243 return __round_jiffies_relative(j, raw_smp_processor_id());
244}
245EXPORT_SYMBOL_GPL(round_jiffies_relative);
246
247
1da177e4
LT
248static inline void set_running_timer(tvec_base_t *base,
249 struct timer_list *timer)
250{
251#ifdef CONFIG_SMP
3691c519 252 base->running_timer = timer;
1da177e4
LT
253#endif
254}
255
1da177e4
LT
256static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
257{
258 unsigned long expires = timer->expires;
259 unsigned long idx = expires - base->timer_jiffies;
260 struct list_head *vec;
261
262 if (idx < TVR_SIZE) {
263 int i = expires & TVR_MASK;
264 vec = base->tv1.vec + i;
265 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
266 int i = (expires >> TVR_BITS) & TVN_MASK;
267 vec = base->tv2.vec + i;
268 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
269 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
270 vec = base->tv3.vec + i;
271 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
272 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
273 vec = base->tv4.vec + i;
274 } else if ((signed long) idx < 0) {
275 /*
276 * Can happen if you add a timer with expires == jiffies,
277 * or you set a timer to go off in the past
278 */
279 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
280 } else {
281 int i;
282 /* If the timeout is larger than 0xffffffff on 64-bit
283 * architectures then we use the maximum timeout:
284 */
285 if (idx > 0xffffffffUL) {
286 idx = 0xffffffffUL;
287 expires = idx + base->timer_jiffies;
288 }
289 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
290 vec = base->tv5.vec + i;
291 }
292 /*
293 * Timers are FIFO:
294 */
295 list_add_tail(&timer->entry, vec);
296}
297
82f67cd9
IM
298#ifdef CONFIG_TIMER_STATS
299void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
300{
301 if (timer->start_site)
302 return;
303
304 timer->start_site = addr;
305 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
306 timer->start_pid = current->pid;
307}
308#endif
309
2aae4a10 310/**
55c888d6
ON
311 * init_timer - initialize a timer.
312 * @timer: the timer to be initialized
313 *
314 * init_timer() must be done to a timer prior calling *any* of the
315 * other timer functions.
316 */
317void fastcall init_timer(struct timer_list *timer)
318{
319 timer->entry.next = NULL;
bfe5d834 320 timer->base = __raw_get_cpu_var(tvec_bases);
82f67cd9
IM
321#ifdef CONFIG_TIMER_STATS
322 timer->start_site = NULL;
323 timer->start_pid = -1;
324 memset(timer->start_comm, 0, TASK_COMM_LEN);
325#endif
55c888d6
ON
326}
327EXPORT_SYMBOL(init_timer);
328
6e453a67
VP
329void fastcall init_timer_deferrable(struct timer_list *timer)
330{
331 init_timer(timer);
332 timer_set_deferrable(timer);
333}
334EXPORT_SYMBOL(init_timer_deferrable);
335
55c888d6 336static inline void detach_timer(struct timer_list *timer,
82f67cd9 337 int clear_pending)
55c888d6
ON
338{
339 struct list_head *entry = &timer->entry;
340
341 __list_del(entry->prev, entry->next);
342 if (clear_pending)
343 entry->next = NULL;
344 entry->prev = LIST_POISON2;
345}
346
347/*
3691c519 348 * We are using hashed locking: holding per_cpu(tvec_bases).lock
55c888d6
ON
349 * means that all timers which are tied to this base via timer->base are
350 * locked, and the base itself is locked too.
351 *
352 * So __run_timers/migrate_timers can safely modify all timers which could
353 * be found on ->tvX lists.
354 *
355 * When the timer's base is locked, and the timer removed from list, it is
356 * possible to set timer->base = NULL and drop the lock: the timer remains
357 * locked.
358 */
3691c519 359static tvec_base_t *lock_timer_base(struct timer_list *timer,
55c888d6 360 unsigned long *flags)
89e7e374 361 __acquires(timer->base->lock)
55c888d6 362{
3691c519 363 tvec_base_t *base;
55c888d6
ON
364
365 for (;;) {
6e453a67
VP
366 tvec_base_t *prelock_base = timer->base;
367 base = tbase_get_base(prelock_base);
55c888d6
ON
368 if (likely(base != NULL)) {
369 spin_lock_irqsave(&base->lock, *flags);
6e453a67 370 if (likely(prelock_base == timer->base))
55c888d6
ON
371 return base;
372 /* The timer has migrated to another CPU */
373 spin_unlock_irqrestore(&base->lock, *flags);
374 }
375 cpu_relax();
376 }
377}
378
1da177e4
LT
379int __mod_timer(struct timer_list *timer, unsigned long expires)
380{
3691c519 381 tvec_base_t *base, *new_base;
1da177e4
LT
382 unsigned long flags;
383 int ret = 0;
384
82f67cd9 385 timer_stats_timer_set_start_info(timer);
1da177e4 386 BUG_ON(!timer->function);
1da177e4 387
55c888d6
ON
388 base = lock_timer_base(timer, &flags);
389
390 if (timer_pending(timer)) {
391 detach_timer(timer, 0);
392 ret = 1;
393 }
394
a4a6198b 395 new_base = __get_cpu_var(tvec_bases);
1da177e4 396
3691c519 397 if (base != new_base) {
1da177e4 398 /*
55c888d6
ON
399 * We are trying to schedule the timer on the local CPU.
400 * However we can't change timer's base while it is running,
401 * otherwise del_timer_sync() can't detect that the timer's
402 * handler yet has not finished. This also guarantees that
403 * the timer is serialized wrt itself.
1da177e4 404 */
a2c348fe 405 if (likely(base->running_timer != timer)) {
55c888d6 406 /* See the comment in lock_timer_base() */
6e453a67 407 timer_set_base(timer, NULL);
55c888d6 408 spin_unlock(&base->lock);
a2c348fe
ON
409 base = new_base;
410 spin_lock(&base->lock);
6e453a67 411 timer_set_base(timer, base);
1da177e4
LT
412 }
413 }
414
1da177e4 415 timer->expires = expires;
a2c348fe
ON
416 internal_add_timer(base, timer);
417 spin_unlock_irqrestore(&base->lock, flags);
1da177e4
LT
418
419 return ret;
420}
421
422EXPORT_SYMBOL(__mod_timer);
423
2aae4a10 424/**
1da177e4
LT
425 * add_timer_on - start a timer on a particular CPU
426 * @timer: the timer to be added
427 * @cpu: the CPU to start it on
428 *
429 * This is not very scalable on SMP. Double adds are not possible.
430 */
431void add_timer_on(struct timer_list *timer, int cpu)
432{
a4a6198b 433 tvec_base_t *base = per_cpu(tvec_bases, cpu);
1da177e4 434 unsigned long flags;
55c888d6 435
82f67cd9 436 timer_stats_timer_set_start_info(timer);
1da177e4 437 BUG_ON(timer_pending(timer) || !timer->function);
3691c519 438 spin_lock_irqsave(&base->lock, flags);
6e453a67 439 timer_set_base(timer, base);
1da177e4 440 internal_add_timer(base, timer);
3691c519 441 spin_unlock_irqrestore(&base->lock, flags);
1da177e4
LT
442}
443
444
2aae4a10 445/**
1da177e4
LT
446 * mod_timer - modify a timer's timeout
447 * @timer: the timer to be modified
2aae4a10 448 * @expires: new timeout in jiffies
1da177e4 449 *
72fd4a35 450 * mod_timer() is a more efficient way to update the expire field of an
1da177e4
LT
451 * active timer (if the timer is inactive it will be activated)
452 *
453 * mod_timer(timer, expires) is equivalent to:
454 *
455 * del_timer(timer); timer->expires = expires; add_timer(timer);
456 *
457 * Note that if there are multiple unserialized concurrent users of the
458 * same timer, then mod_timer() is the only safe way to modify the timeout,
459 * since add_timer() cannot modify an already running timer.
460 *
461 * The function returns whether it has modified a pending timer or not.
462 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
463 * active timer returns 1.)
464 */
465int mod_timer(struct timer_list *timer, unsigned long expires)
466{
467 BUG_ON(!timer->function);
468
82f67cd9 469 timer_stats_timer_set_start_info(timer);
1da177e4
LT
470 /*
471 * This is a common optimization triggered by the
472 * networking code - if the timer is re-modified
473 * to be the same thing then just return:
474 */
475 if (timer->expires == expires && timer_pending(timer))
476 return 1;
477
478 return __mod_timer(timer, expires);
479}
480
481EXPORT_SYMBOL(mod_timer);
482
2aae4a10 483/**
1da177e4
LT
484 * del_timer - deactive a timer.
485 * @timer: the timer to be deactivated
486 *
487 * del_timer() deactivates a timer - this works on both active and inactive
488 * timers.
489 *
490 * The function returns whether it has deactivated a pending timer or not.
491 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
492 * active timer returns 1.)
493 */
494int del_timer(struct timer_list *timer)
495{
3691c519 496 tvec_base_t *base;
1da177e4 497 unsigned long flags;
55c888d6 498 int ret = 0;
1da177e4 499
82f67cd9 500 timer_stats_timer_clear_start_info(timer);
55c888d6
ON
501 if (timer_pending(timer)) {
502 base = lock_timer_base(timer, &flags);
503 if (timer_pending(timer)) {
504 detach_timer(timer, 1);
505 ret = 1;
506 }
1da177e4 507 spin_unlock_irqrestore(&base->lock, flags);
1da177e4 508 }
1da177e4 509
55c888d6 510 return ret;
1da177e4
LT
511}
512
513EXPORT_SYMBOL(del_timer);
514
515#ifdef CONFIG_SMP
2aae4a10
REB
516/**
517 * try_to_del_timer_sync - Try to deactivate a timer
518 * @timer: timer do del
519 *
fd450b73
ON
520 * This function tries to deactivate a timer. Upon successful (ret >= 0)
521 * exit the timer is not queued and the handler is not running on any CPU.
522 *
523 * It must not be called from interrupt contexts.
524 */
525int try_to_del_timer_sync(struct timer_list *timer)
526{
3691c519 527 tvec_base_t *base;
fd450b73
ON
528 unsigned long flags;
529 int ret = -1;
530
531 base = lock_timer_base(timer, &flags);
532
533 if (base->running_timer == timer)
534 goto out;
535
536 ret = 0;
537 if (timer_pending(timer)) {
538 detach_timer(timer, 1);
539 ret = 1;
540 }
541out:
542 spin_unlock_irqrestore(&base->lock, flags);
543
544 return ret;
545}
546
e19dff1f
DH
547EXPORT_SYMBOL(try_to_del_timer_sync);
548
2aae4a10 549/**
1da177e4
LT
550 * del_timer_sync - deactivate a timer and wait for the handler to finish.
551 * @timer: the timer to be deactivated
552 *
553 * This function only differs from del_timer() on SMP: besides deactivating
554 * the timer it also makes sure the handler has finished executing on other
555 * CPUs.
556 *
72fd4a35 557 * Synchronization rules: Callers must prevent restarting of the timer,
1da177e4
LT
558 * otherwise this function is meaningless. It must not be called from
559 * interrupt contexts. The caller must not hold locks which would prevent
55c888d6
ON
560 * completion of the timer's handler. The timer's handler must not call
561 * add_timer_on(). Upon exit the timer is not queued and the handler is
562 * not running on any CPU.
1da177e4
LT
563 *
564 * The function returns whether it has deactivated a pending timer or not.
1da177e4
LT
565 */
566int del_timer_sync(struct timer_list *timer)
567{
fd450b73
ON
568 for (;;) {
569 int ret = try_to_del_timer_sync(timer);
570 if (ret >= 0)
571 return ret;
a0009652 572 cpu_relax();
fd450b73 573 }
1da177e4 574}
1da177e4 575
55c888d6 576EXPORT_SYMBOL(del_timer_sync);
1da177e4
LT
577#endif
578
579static int cascade(tvec_base_t *base, tvec_t *tv, int index)
580{
581 /* cascade all the timers from tv up one level */
3439dd86
P
582 struct timer_list *timer, *tmp;
583 struct list_head tv_list;
584
585 list_replace_init(tv->vec + index, &tv_list);
1da177e4 586
1da177e4 587 /*
3439dd86
P
588 * We are removing _all_ timers from the list, so we
589 * don't have to detach them individually.
1da177e4 590 */
3439dd86 591 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
6e453a67 592 BUG_ON(tbase_get_base(timer->base) != base);
3439dd86 593 internal_add_timer(base, timer);
1da177e4 594 }
1da177e4
LT
595
596 return index;
597}
598
2aae4a10
REB
599#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
600
601/**
1da177e4
LT
602 * __run_timers - run all expired timers (if any) on this CPU.
603 * @base: the timer vector to be processed.
604 *
605 * This function cascades all vectors and executes all expired timer
606 * vectors.
607 */
1da177e4
LT
608static inline void __run_timers(tvec_base_t *base)
609{
610 struct timer_list *timer;
611
3691c519 612 spin_lock_irq(&base->lock);
1da177e4 613 while (time_after_eq(jiffies, base->timer_jiffies)) {
626ab0e6 614 struct list_head work_list;
1da177e4
LT
615 struct list_head *head = &work_list;
616 int index = base->timer_jiffies & TVR_MASK;
626ab0e6 617
1da177e4
LT
618 /*
619 * Cascade timers:
620 */
621 if (!index &&
622 (!cascade(base, &base->tv2, INDEX(0))) &&
623 (!cascade(base, &base->tv3, INDEX(1))) &&
624 !cascade(base, &base->tv4, INDEX(2)))
625 cascade(base, &base->tv5, INDEX(3));
626ab0e6
ON
626 ++base->timer_jiffies;
627 list_replace_init(base->tv1.vec + index, &work_list);
55c888d6 628 while (!list_empty(head)) {
1da177e4
LT
629 void (*fn)(unsigned long);
630 unsigned long data;
631
632 timer = list_entry(head->next,struct timer_list,entry);
633 fn = timer->function;
634 data = timer->data;
635
82f67cd9
IM
636 timer_stats_account_timer(timer);
637
1da177e4 638 set_running_timer(base, timer);
55c888d6 639 detach_timer(timer, 1);
3691c519 640 spin_unlock_irq(&base->lock);
1da177e4 641 {
be5b4fbd 642 int preempt_count = preempt_count();
1da177e4
LT
643 fn(data);
644 if (preempt_count != preempt_count()) {
be5b4fbd
JJ
645 printk(KERN_WARNING "huh, entered %p "
646 "with preempt_count %08x, exited"
647 " with %08x?\n",
648 fn, preempt_count,
649 preempt_count());
1da177e4
LT
650 BUG();
651 }
652 }
3691c519 653 spin_lock_irq(&base->lock);
1da177e4
LT
654 }
655 }
656 set_running_timer(base, NULL);
3691c519 657 spin_unlock_irq(&base->lock);
1da177e4
LT
658}
659
fd064b9b 660#if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
1da177e4
LT
661/*
662 * Find out when the next timer event is due to happen. This
663 * is used on S/390 to stop all activity when a cpus is idle.
664 * This functions needs to be called disabled.
665 */
1cfd6849 666static unsigned long __next_timer_interrupt(tvec_base_t *base)
1da177e4 667{
1cfd6849
TG
668 unsigned long timer_jiffies = base->timer_jiffies;
669 unsigned long expires = timer_jiffies + (LONG_MAX >> 1);
670 int index, slot, array, found = 0;
1da177e4 671 struct timer_list *nte;
1da177e4 672 tvec_t *varray[4];
1da177e4
LT
673
674 /* Look for timer events in tv1. */
1cfd6849 675 index = slot = timer_jiffies & TVR_MASK;
1da177e4 676 do {
1cfd6849 677 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
6e453a67
VP
678 if (tbase_get_deferrable(nte->base))
679 continue;
680
1cfd6849 681 found = 1;
1da177e4 682 expires = nte->expires;
1cfd6849
TG
683 /* Look at the cascade bucket(s)? */
684 if (!index || slot < index)
685 goto cascade;
686 return expires;
1da177e4 687 }
1cfd6849
TG
688 slot = (slot + 1) & TVR_MASK;
689 } while (slot != index);
690
691cascade:
692 /* Calculate the next cascade event */
693 if (index)
694 timer_jiffies += TVR_SIZE - index;
695 timer_jiffies >>= TVR_BITS;
1da177e4
LT
696
697 /* Check tv2-tv5. */
698 varray[0] = &base->tv2;
699 varray[1] = &base->tv3;
700 varray[2] = &base->tv4;
701 varray[3] = &base->tv5;
1cfd6849
TG
702
703 for (array = 0; array < 4; array++) {
704 tvec_t *varp = varray[array];
705
706 index = slot = timer_jiffies & TVN_MASK;
1da177e4 707 do {
1cfd6849
TG
708 list_for_each_entry(nte, varp->vec + slot, entry) {
709 found = 1;
1da177e4
LT
710 if (time_before(nte->expires, expires))
711 expires = nte->expires;
1cfd6849
TG
712 }
713 /*
714 * Do we still search for the first timer or are
715 * we looking up the cascade buckets ?
716 */
717 if (found) {
718 /* Look at the cascade bucket(s)? */
719 if (!index || slot < index)
720 break;
721 return expires;
722 }
723 slot = (slot + 1) & TVN_MASK;
724 } while (slot != index);
725
726 if (index)
727 timer_jiffies += TVN_SIZE - index;
728 timer_jiffies >>= TVN_BITS;
1da177e4 729 }
1cfd6849
TG
730 return expires;
731}
69239749 732
1cfd6849
TG
733/*
734 * Check, if the next hrtimer event is before the next timer wheel
735 * event:
736 */
737static unsigned long cmp_next_hrtimer_event(unsigned long now,
738 unsigned long expires)
739{
740 ktime_t hr_delta = hrtimer_get_next_event();
741 struct timespec tsdelta;
9501b6cf 742 unsigned long delta;
1cfd6849
TG
743
744 if (hr_delta.tv64 == KTIME_MAX)
745 return expires;
0662b713 746
9501b6cf
TG
747 /*
748 * Expired timer available, let it expire in the next tick
749 */
750 if (hr_delta.tv64 <= 0)
751 return now + 1;
69239749 752
1cfd6849 753 tsdelta = ktime_to_timespec(hr_delta);
9501b6cf
TG
754 delta = timespec_to_jiffies(&tsdelta);
755 /*
756 * Take rounding errors in to account and make sure, that it
757 * expires in the next tick. Otherwise we go into an endless
758 * ping pong due to tick_nohz_stop_sched_tick() retriggering
759 * the timer softirq
760 */
761 if (delta < 1)
762 delta = 1;
763 now += delta;
1cfd6849
TG
764 if (time_before(now, expires))
765 return now;
1da177e4
LT
766 return expires;
767}
1cfd6849
TG
768
769/**
770 * next_timer_interrupt - return the jiffy of the next pending timer
05fb6bf0 771 * @now: current time (in jiffies)
1cfd6849 772 */
fd064b9b 773unsigned long get_next_timer_interrupt(unsigned long now)
1cfd6849
TG
774{
775 tvec_base_t *base = __get_cpu_var(tvec_bases);
fd064b9b 776 unsigned long expires;
1cfd6849
TG
777
778 spin_lock(&base->lock);
779 expires = __next_timer_interrupt(base);
780 spin_unlock(&base->lock);
781
782 if (time_before_eq(expires, now))
783 return now;
784
785 return cmp_next_hrtimer_event(now, expires);
786}
fd064b9b
TG
787
788#ifdef CONFIG_NO_IDLE_HZ
789unsigned long next_timer_interrupt(void)
790{
791 return get_next_timer_interrupt(jiffies);
792}
793#endif
794
1da177e4
LT
795#endif
796
797/******************************************************************/
798
1da177e4
LT
799/*
800 * The current time
801 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
802 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
803 * at zero at system boot time, so wall_to_monotonic will be negative,
804 * however, we will ALWAYS keep the tv_nsec part positive so we can use
805 * the usual normalization.
806 */
807struct timespec xtime __attribute__ ((aligned (16)));
808struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
809
810EXPORT_SYMBOL(xtime);
811
726c14bf 812
ad596171
JS
813/* XXX - all of this timekeeping code should be later moved to time.c */
814#include <linux/clocksource.h>
815static struct clocksource *clock; /* pointer to current clocksource */
cf3c769b
JS
816
817#ifdef CONFIG_GENERIC_TIME
818/**
819 * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook
820 *
821 * private function, must hold xtime_lock lock when being
822 * called. Returns the number of nanoseconds since the
823 * last call to update_wall_time() (adjusted by NTP scaling)
824 */
825static inline s64 __get_nsec_offset(void)
826{
827 cycle_t cycle_now, cycle_delta;
828 s64 ns_offset;
829
830 /* read clocksource: */
a2752549 831 cycle_now = clocksource_read(clock);
cf3c769b
JS
832
833 /* calculate the delta since the last update_wall_time: */
19923c19 834 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
cf3c769b
JS
835
836 /* convert to nanoseconds: */
837 ns_offset = cyc2ns(clock, cycle_delta);
838
839 return ns_offset;
840}
841
842/**
843 * __get_realtime_clock_ts - Returns the time of day in a timespec
844 * @ts: pointer to the timespec to be set
845 *
846 * Returns the time of day in a timespec. Used by
847 * do_gettimeofday() and get_realtime_clock_ts().
848 */
849static inline void __get_realtime_clock_ts(struct timespec *ts)
850{
851 unsigned long seq;
852 s64 nsecs;
853
854 do {
855 seq = read_seqbegin(&xtime_lock);
856
857 *ts = xtime;
858 nsecs = __get_nsec_offset();
859
860 } while (read_seqretry(&xtime_lock, seq));
861
862 timespec_add_ns(ts, nsecs);
863}
864
865/**
a2752549 866 * getnstimeofday - Returns the time of day in a timespec
cf3c769b
JS
867 * @ts: pointer to the timespec to be set
868 *
869 * Returns the time of day in a timespec.
870 */
871void getnstimeofday(struct timespec *ts)
872{
873 __get_realtime_clock_ts(ts);
874}
875
876EXPORT_SYMBOL(getnstimeofday);
877
878/**
879 * do_gettimeofday - Returns the time of day in a timeval
880 * @tv: pointer to the timeval to be set
881 *
882 * NOTE: Users should be converted to using get_realtime_clock_ts()
883 */
884void do_gettimeofday(struct timeval *tv)
885{
886 struct timespec now;
887
888 __get_realtime_clock_ts(&now);
889 tv->tv_sec = now.tv_sec;
890 tv->tv_usec = now.tv_nsec/1000;
891}
892
893EXPORT_SYMBOL(do_gettimeofday);
894/**
895 * do_settimeofday - Sets the time of day
896 * @tv: pointer to the timespec variable containing the new time
897 *
898 * Sets the time of day to the new time and update NTP and notify hrtimers
899 */
900int do_settimeofday(struct timespec *tv)
901{
902 unsigned long flags;
903 time_t wtm_sec, sec = tv->tv_sec;
904 long wtm_nsec, nsec = tv->tv_nsec;
905
906 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
907 return -EINVAL;
908
909 write_seqlock_irqsave(&xtime_lock, flags);
910
911 nsec -= __get_nsec_offset();
912
913 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
914 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
915
916 set_normalized_timespec(&xtime, sec, nsec);
917 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
918
e154ff3d 919 clock->error = 0;
cf3c769b
JS
920 ntp_clear();
921
90675a27
DW
922 update_vsyscall(&xtime, clock);
923
cf3c769b
JS
924 write_sequnlock_irqrestore(&xtime_lock, flags);
925
926 /* signal hrtimers about time change */
927 clock_was_set();
928
929 return 0;
930}
931
932EXPORT_SYMBOL(do_settimeofday);
933
934/**
935 * change_clocksource - Swaps clocksources if a new one is available
936 *
937 * Accumulates current time interval and initializes new clocksource
938 */
5d8b34fd 939static void change_clocksource(void)
cf3c769b
JS
940{
941 struct clocksource *new;
942 cycle_t now;
943 u64 nsec;
5d8b34fd 944
a2752549 945 new = clocksource_get_next();
5d8b34fd
TG
946
947 if (clock == new)
948 return;
949
950 now = clocksource_read(new);
951 nsec = __get_nsec_offset();
952 timespec_add_ns(&xtime, nsec);
953
954 clock = new;
955 clock->cycle_last = now;
956
957 clock->error = 0;
958 clock->xtime_nsec = 0;
959 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
960
79bf2bb3
TG
961 tick_clock_notify();
962
5d8b34fd
TG
963 printk(KERN_INFO "Time: %s clocksource has been installed.\n",
964 clock->name);
cf3c769b
JS
965}
966#else
5d8b34fd 967static inline void change_clocksource(void) { }
cf3c769b
JS
968#endif
969
970/**
9d634631 971 * timekeeping_is_continuous - check to see if timekeeping is free running
cf3c769b
JS
972 */
973int timekeeping_is_continuous(void)
974{
975 unsigned long seq;
976 int ret;
977
978 do {
979 seq = read_seqbegin(&xtime_lock);
980
5d8b34fd 981 ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
cf3c769b
JS
982
983 } while (read_seqretry(&xtime_lock, seq));
984
985 return ret;
986}
987
411187fb
JS
988/**
989 * read_persistent_clock - Return time in seconds from the persistent clock.
990 *
991 * Weak dummy function for arches that do not yet support it.
992 * Returns seconds from epoch using the battery backed persistent clock.
993 * Returns zero if unsupported.
994 *
995 * XXX - Do be sure to remove it once all arches implement it.
996 */
997unsigned long __attribute__((weak)) read_persistent_clock(void)
998{
999 return 0;
1000}
1001
1da177e4 1002/*
ad596171 1003 * timekeeping_init - Initializes the clocksource and common timekeeping values
1da177e4 1004 */
ad596171 1005void __init timekeeping_init(void)
1da177e4 1006{
ad596171 1007 unsigned long flags;
411187fb 1008 unsigned long sec = read_persistent_clock();
ad596171
JS
1009
1010 write_seqlock_irqsave(&xtime_lock, flags);
b0ee7556
RZ
1011
1012 ntp_clear();
1013
a2752549 1014 clock = clocksource_get_next();
f4304ab2 1015 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
19923c19 1016 clock->cycle_last = clocksource_read(clock);
b0ee7556 1017
411187fb
JS
1018 xtime.tv_sec = sec;
1019 xtime.tv_nsec = 0;
1020 set_normalized_timespec(&wall_to_monotonic,
1021 -xtime.tv_sec, -xtime.tv_nsec);
1022
ad596171
JS
1023 write_sequnlock_irqrestore(&xtime_lock, flags);
1024}
1025
411187fb 1026/* flag for if timekeeping is suspended */
3e143475 1027static int timekeeping_suspended;
411187fb
JS
1028/* time in seconds when suspend began */
1029static unsigned long timekeeping_suspend_time;
1030
2aae4a10 1031/**
ad596171
JS
1032 * timekeeping_resume - Resumes the generic timekeeping subsystem.
1033 * @dev: unused
1034 *
1035 * This is for the generic clocksource timekeeping.
8ef38609 1036 * xtime/wall_to_monotonic/jiffies/etc are
ad596171
JS
1037 * still managed by arch specific suspend/resume code.
1038 */
1039static int timekeeping_resume(struct sys_device *dev)
1040{
1041 unsigned long flags;
411187fb 1042 unsigned long now = read_persistent_clock();
ad596171
JS
1043
1044 write_seqlock_irqsave(&xtime_lock, flags);
411187fb
JS
1045
1046 if (now && (now > timekeeping_suspend_time)) {
1047 unsigned long sleep_length = now - timekeeping_suspend_time;
1048
1049 xtime.tv_sec += sleep_length;
1050 wall_to_monotonic.tv_sec -= sleep_length;
1051 }
1052 /* re-base the last cycle value */
19923c19 1053 clock->cycle_last = clocksource_read(clock);
3e143475
JS
1054 clock->error = 0;
1055 timekeeping_suspended = 0;
1056 write_sequnlock_irqrestore(&xtime_lock, flags);
411187fb
JS
1057
1058 touch_softlockup_watchdog();
6321dd60
TG
1059
1060 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
1061
d316c57f 1062 /* Resume hrtimers */
995f054f 1063 hres_timers_resume();
411187fb 1064
3e143475
JS
1065 return 0;
1066}
1067
1068static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
1069{
1070 unsigned long flags;
1071
1072 write_seqlock_irqsave(&xtime_lock, flags);
1073 timekeeping_suspended = 1;
411187fb 1074 timekeeping_suspend_time = read_persistent_clock();
ad596171 1075 write_sequnlock_irqrestore(&xtime_lock, flags);
6321dd60
TG
1076
1077 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
1078
ad596171
JS
1079 return 0;
1080}
1081
1082/* sysfs resume/suspend bits for timekeeping */
1083static struct sysdev_class timekeeping_sysclass = {
1084 .resume = timekeeping_resume,
3e143475 1085 .suspend = timekeeping_suspend,
ad596171
JS
1086 set_kset_name("timekeeping"),
1087};
1088
1089static struct sys_device device_timer = {
1090 .id = 0,
1091 .cls = &timekeeping_sysclass,
1092};
1093
1094static int __init timekeeping_init_device(void)
1095{
1096 int error = sysdev_class_register(&timekeeping_sysclass);
1097 if (!error)
1098 error = sysdev_register(&device_timer);
1099 return error;
1100}
1101
1102device_initcall(timekeeping_init_device);
1103
19923c19 1104/*
e154ff3d 1105 * If the error is already larger, we look ahead even further
19923c19
RZ
1106 * to compensate for late or lost adjustments.
1107 */
f5f1a24a
DW
1108static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
1109 s64 *offset)
19923c19 1110{
e154ff3d
RZ
1111 s64 tick_error, i;
1112 u32 look_ahead, adj;
1113 s32 error2, mult;
19923c19
RZ
1114
1115 /*
e154ff3d
RZ
1116 * Use the current error value to determine how much to look ahead.
1117 * The larger the error the slower we adjust for it to avoid problems
1118 * with losing too many ticks, otherwise we would overadjust and
1119 * produce an even larger error. The smaller the adjustment the
1120 * faster we try to adjust for it, as lost ticks can do less harm
1121 * here. This is tuned so that an error of about 1 msec is adusted
1122 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
19923c19 1123 */
e154ff3d
RZ
1124 error2 = clock->error >> (TICK_LENGTH_SHIFT + 22 - 2 * SHIFT_HZ);
1125 error2 = abs(error2);
1126 for (look_ahead = 0; error2 > 0; look_ahead++)
1127 error2 >>= 2;
19923c19
RZ
1128
1129 /*
e154ff3d
RZ
1130 * Now calculate the error in (1 << look_ahead) ticks, but first
1131 * remove the single look ahead already included in the error.
19923c19 1132 */
f5f1a24a
DW
1133 tick_error = current_tick_length() >>
1134 (TICK_LENGTH_SHIFT - clock->shift + 1);
e154ff3d
RZ
1135 tick_error -= clock->xtime_interval >> 1;
1136 error = ((error - tick_error) >> look_ahead) + tick_error;
1137
1138 /* Finally calculate the adjustment shift value. */
1139 i = *interval;
1140 mult = 1;
1141 if (error < 0) {
1142 error = -error;
1143 *interval = -*interval;
1144 *offset = -*offset;
1145 mult = -1;
19923c19 1146 }
e154ff3d
RZ
1147 for (adj = 0; error > i; adj++)
1148 error >>= 1;
19923c19
RZ
1149
1150 *interval <<= adj;
1151 *offset <<= adj;
e154ff3d 1152 return mult << adj;
19923c19
RZ
1153}
1154
1155/*
1156 * Adjust the multiplier to reduce the error value,
1157 * this is optimized for the most common adjustments of -1,0,1,
1158 * for other values we can do a bit more work.
1159 */
1160static void clocksource_adjust(struct clocksource *clock, s64 offset)
1161{
1162 s64 error, interval = clock->cycle_interval;
1163 int adj;
1164
1165 error = clock->error >> (TICK_LENGTH_SHIFT - clock->shift - 1);
1166 if (error > interval) {
e154ff3d
RZ
1167 error >>= 2;
1168 if (likely(error <= interval))
1169 adj = 1;
1170 else
1171 adj = clocksource_bigadjust(error, &interval, &offset);
19923c19 1172 } else if (error < -interval) {
e154ff3d
RZ
1173 error >>= 2;
1174 if (likely(error >= -interval)) {
1175 adj = -1;
1176 interval = -interval;
1177 offset = -offset;
1178 } else
1179 adj = clocksource_bigadjust(error, &interval, &offset);
19923c19
RZ
1180 } else
1181 return;
1182
1183 clock->mult += adj;
1184 clock->xtime_interval += interval;
1185 clock->xtime_nsec -= offset;
f5f1a24a
DW
1186 clock->error -= (interval - offset) <<
1187 (TICK_LENGTH_SHIFT - clock->shift);
19923c19
RZ
1188}
1189
2aae4a10 1190/**
ad596171
JS
1191 * update_wall_time - Uses the current clocksource to increment the wall time
1192 *
1193 * Called from the timer interrupt, must hold a write on xtime_lock.
1194 */
1195static void update_wall_time(void)
1196{
19923c19 1197 cycle_t offset;
ad596171 1198
3e143475
JS
1199 /* Make sure we're fully resumed: */
1200 if (unlikely(timekeeping_suspended))
1201 return;
5eb6d205 1202
19923c19
RZ
1203#ifdef CONFIG_GENERIC_TIME
1204 offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
1205#else
1206 offset = clock->cycle_interval;
1207#endif
3e143475 1208 clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift;
ad596171
JS
1209
1210 /* normally this loop will run just once, however in the
1211 * case of lost or late ticks, it will accumulate correctly.
1212 */
19923c19 1213 while (offset >= clock->cycle_interval) {
ad596171 1214 /* accumulate one interval */
19923c19
RZ
1215 clock->xtime_nsec += clock->xtime_interval;
1216 clock->cycle_last += clock->cycle_interval;
1217 offset -= clock->cycle_interval;
1218
1219 if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
1220 clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
1221 xtime.tv_sec++;
1222 second_overflow();
1223 }
ad596171 1224
5eb6d205 1225 /* interpolator bits */
19923c19 1226 time_interpolator_update(clock->xtime_interval
5eb6d205 1227 >> clock->shift);
5eb6d205
JS
1228
1229 /* accumulate error between NTP and clock interval */
19923c19
RZ
1230 clock->error += current_tick_length();
1231 clock->error -= clock->xtime_interval << (TICK_LENGTH_SHIFT - clock->shift);
1232 }
5eb6d205 1233
19923c19
RZ
1234 /* correct the clock when NTP error is too big */
1235 clocksource_adjust(clock, offset);
5eb6d205 1236
5eb6d205 1237 /* store full nanoseconds into xtime */
e154ff3d 1238 xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift;
19923c19 1239 clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
cf3c769b
JS
1240
1241 /* check to see if there is a new clocksource to use */
5d8b34fd 1242 change_clocksource();
acc9a9dc 1243 update_vsyscall(&xtime, clock);
1da177e4
LT
1244}
1245
1246/*
1247 * Called from the timer interrupt handler to charge one tick to the current
1248 * process. user_tick is 1 if the tick is user time, 0 for system.
1249 */
1250void update_process_times(int user_tick)
1251{
1252 struct task_struct *p = current;
1253 int cpu = smp_processor_id();
1254
1255 /* Note: this timer irq context must be accounted for as well. */
1256 if (user_tick)
1257 account_user_time(p, jiffies_to_cputime(1));
1258 else
1259 account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
1260 run_local_timers();
1261 if (rcu_pending(cpu))
1262 rcu_check_callbacks(cpu, user_tick);
1263 scheduler_tick();
1264 run_posix_cpu_timers(p);
1265}
1266
1267/*
1268 * Nr of active tasks - counted in fixed-point numbers
1269 */
1270static unsigned long count_active_tasks(void)
1271{
db1b1fef 1272 return nr_active() * FIXED_1;
1da177e4
LT
1273}
1274
1275/*
1276 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
1277 * imply that avenrun[] is the standard name for this kind of thing.
1278 * Nothing else seems to be standardized: the fractional size etc
1279 * all seem to differ on different machines.
1280 *
1281 * Requires xtime_lock to access.
1282 */
1283unsigned long avenrun[3];
1284
1285EXPORT_SYMBOL(avenrun);
1286
1287/*
1288 * calc_load - given tick count, update the avenrun load estimates.
1289 * This is called while holding a write_lock on xtime_lock.
1290 */
1291static inline void calc_load(unsigned long ticks)
1292{
1293 unsigned long active_tasks; /* fixed-point */
1294 static int count = LOAD_FREQ;
1295
cd7175ed
ED
1296 count -= ticks;
1297 if (unlikely(count < 0)) {
1298 active_tasks = count_active_tasks();
1299 do {
1300 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
1301 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
1302 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
1303 count += LOAD_FREQ;
1304 } while (count < 0);
1da177e4
LT
1305 }
1306}
1307
1da177e4
LT
1308/*
1309 * This read-write spinlock protects us from races in SMP while
1310 * playing with xtime and avenrun.
1311 */
5809f9d4 1312__attribute__((weak)) __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
1da177e4
LT
1313
1314EXPORT_SYMBOL(xtime_lock);
1da177e4
LT
1315
1316/*
1317 * This function runs timers and the timer-tq in bottom half context.
1318 */
1319static void run_timer_softirq(struct softirq_action *h)
1320{
a4a6198b 1321 tvec_base_t *base = __get_cpu_var(tvec_bases);
1da177e4 1322
82f67cd9
IM
1323 hrtimer_run_queues();
1324
1da177e4
LT
1325 if (time_after_eq(jiffies, base->timer_jiffies))
1326 __run_timers(base);
1327}
1328
1329/*
1330 * Called by the local, per-CPU timer interrupt on SMP.
1331 */
1332void run_local_timers(void)
1333{
1334 raise_softirq(TIMER_SOFTIRQ);
6687a97d 1335 softlockup_tick();
1da177e4
LT
1336}
1337
1338/*
1339 * Called by the timer interrupt. xtime_lock must already be taken
1340 * by the timer IRQ!
1341 */
3171a030 1342static inline void update_times(unsigned long ticks)
1da177e4 1343{
ad596171 1344 update_wall_time();
1da177e4
LT
1345 calc_load(ticks);
1346}
1347
1348/*
1349 * The 64-bit jiffies value is not atomic - you MUST NOT read it
1350 * without sampling the sequence number in xtime_lock.
1351 * jiffies is defined in the linker script...
1352 */
1353
3171a030 1354void do_timer(unsigned long ticks)
1da177e4 1355{
3171a030
AN
1356 jiffies_64 += ticks;
1357 update_times(ticks);
1da177e4
LT
1358}
1359
1360#ifdef __ARCH_WANT_SYS_ALARM
1361
1362/*
1363 * For backwards compatibility? This can be done in libc so Alpha
1364 * and all newer ports shouldn't need it.
1365 */
1366asmlinkage unsigned long sys_alarm(unsigned int seconds)
1367{
c08b8a49 1368 return alarm_setitimer(seconds);
1da177e4
LT
1369}
1370
1371#endif
1372
1373#ifndef __alpha__
1374
1375/*
1376 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
1377 * should be moved into arch/i386 instead?
1378 */
1379
1380/**
1381 * sys_getpid - return the thread group id of the current process
1382 *
1383 * Note, despite the name, this returns the tgid not the pid. The tgid and
1384 * the pid are identical unless CLONE_THREAD was specified on clone() in
1385 * which case the tgid is the same in all threads of the same group.
1386 *
1387 * This is SMP safe as current->tgid does not change.
1388 */
1389asmlinkage long sys_getpid(void)
1390{
1391 return current->tgid;
1392}
1393
1394/*
6997a6fa
KK
1395 * Accessing ->real_parent is not SMP-safe, it could
1396 * change from under us. However, we can use a stale
1397 * value of ->real_parent under rcu_read_lock(), see
1398 * release_task()->call_rcu(delayed_put_task_struct).
1da177e4
LT
1399 */
1400asmlinkage long sys_getppid(void)
1401{
1402 int pid;
1da177e4 1403
6997a6fa
KK
1404 rcu_read_lock();
1405 pid = rcu_dereference(current->real_parent)->tgid;
1406 rcu_read_unlock();
1da177e4 1407
1da177e4
LT
1408 return pid;
1409}
1410
1411asmlinkage long sys_getuid(void)
1412{
1413 /* Only we change this so SMP safe */
1414 return current->uid;
1415}
1416
1417asmlinkage long sys_geteuid(void)
1418{
1419 /* Only we change this so SMP safe */
1420 return current->euid;
1421}
1422
1423asmlinkage long sys_getgid(void)
1424{
1425 /* Only we change this so SMP safe */
1426 return current->gid;
1427}
1428
1429asmlinkage long sys_getegid(void)
1430{
1431 /* Only we change this so SMP safe */
1432 return current->egid;
1433}
1434
1435#endif
1436
1437static void process_timeout(unsigned long __data)
1438{
36c8b586 1439 wake_up_process((struct task_struct *)__data);
1da177e4
LT
1440}
1441
1442/**
1443 * schedule_timeout - sleep until timeout
1444 * @timeout: timeout value in jiffies
1445 *
1446 * Make the current task sleep until @timeout jiffies have
1447 * elapsed. The routine will return immediately unless
1448 * the current task state has been set (see set_current_state()).
1449 *
1450 * You can set the task state as follows -
1451 *
1452 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1453 * pass before the routine returns. The routine will return 0
1454 *
1455 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1456 * delivered to the current task. In this case the remaining time
1457 * in jiffies will be returned, or 0 if the timer expired in time
1458 *
1459 * The current task state is guaranteed to be TASK_RUNNING when this
1460 * routine returns.
1461 *
1462 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1463 * the CPU away without a bound on the timeout. In this case the return
1464 * value will be %MAX_SCHEDULE_TIMEOUT.
1465 *
1466 * In all cases the return value is guaranteed to be non-negative.
1467 */
1468fastcall signed long __sched schedule_timeout(signed long timeout)
1469{
1470 struct timer_list timer;
1471 unsigned long expire;
1472
1473 switch (timeout)
1474 {
1475 case MAX_SCHEDULE_TIMEOUT:
1476 /*
1477 * These two special cases are useful to be comfortable
1478 * in the caller. Nothing more. We could take
1479 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1480 * but I' d like to return a valid offset (>=0) to allow
1481 * the caller to do everything it want with the retval.
1482 */
1483 schedule();
1484 goto out;
1485 default:
1486 /*
1487 * Another bit of PARANOID. Note that the retval will be
1488 * 0 since no piece of kernel is supposed to do a check
1489 * for a negative retval of schedule_timeout() (since it
1490 * should never happens anyway). You just have the printk()
1491 * that will tell you if something is gone wrong and where.
1492 */
5b149bcc 1493 if (timeout < 0) {
1da177e4 1494 printk(KERN_ERR "schedule_timeout: wrong timeout "
5b149bcc
AM
1495 "value %lx\n", timeout);
1496 dump_stack();
1da177e4
LT
1497 current->state = TASK_RUNNING;
1498 goto out;
1499 }
1500 }
1501
1502 expire = timeout + jiffies;
1503
a8db2db1
ON
1504 setup_timer(&timer, process_timeout, (unsigned long)current);
1505 __mod_timer(&timer, expire);
1da177e4
LT
1506 schedule();
1507 del_singleshot_timer_sync(&timer);
1508
1509 timeout = expire - jiffies;
1510
1511 out:
1512 return timeout < 0 ? 0 : timeout;
1513}
1da177e4
LT
1514EXPORT_SYMBOL(schedule_timeout);
1515
8a1c1757
AM
1516/*
1517 * We can use __set_current_state() here because schedule_timeout() calls
1518 * schedule() unconditionally.
1519 */
64ed93a2
NA
1520signed long __sched schedule_timeout_interruptible(signed long timeout)
1521{
a5a0d52c
AM
1522 __set_current_state(TASK_INTERRUPTIBLE);
1523 return schedule_timeout(timeout);
64ed93a2
NA
1524}
1525EXPORT_SYMBOL(schedule_timeout_interruptible);
1526
1527signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1528{
a5a0d52c
AM
1529 __set_current_state(TASK_UNINTERRUPTIBLE);
1530 return schedule_timeout(timeout);
64ed93a2
NA
1531}
1532EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1533
1da177e4
LT
1534/* Thread ID - the internal kernel "pid" */
1535asmlinkage long sys_gettid(void)
1536{
1537 return current->pid;
1538}
1539
2aae4a10 1540/**
d4d23add 1541 * do_sysinfo - fill in sysinfo struct
2aae4a10 1542 * @info: pointer to buffer to fill
1da177e4 1543 */
d4d23add 1544int do_sysinfo(struct sysinfo *info)
1da177e4 1545{
1da177e4
LT
1546 unsigned long mem_total, sav_total;
1547 unsigned int mem_unit, bitcount;
1548 unsigned long seq;
1549
d4d23add 1550 memset(info, 0, sizeof(struct sysinfo));
1da177e4
LT
1551
1552 do {
1553 struct timespec tp;
1554 seq = read_seqbegin(&xtime_lock);
1555
1556 /*
1557 * This is annoying. The below is the same thing
1558 * posix_get_clock_monotonic() does, but it wants to
1559 * take the lock which we want to cover the loads stuff
1560 * too.
1561 */
1562
1563 getnstimeofday(&tp);
1564 tp.tv_sec += wall_to_monotonic.tv_sec;
1565 tp.tv_nsec += wall_to_monotonic.tv_nsec;
1566 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1567 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1568 tp.tv_sec++;
1569 }
d4d23add 1570 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1da177e4 1571
d4d23add
KM
1572 info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1573 info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1574 info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
1da177e4 1575
d4d23add 1576 info->procs = nr_threads;
1da177e4
LT
1577 } while (read_seqretry(&xtime_lock, seq));
1578
d4d23add
KM
1579 si_meminfo(info);
1580 si_swapinfo(info);
1da177e4
LT
1581
1582 /*
1583 * If the sum of all the available memory (i.e. ram + swap)
1584 * is less than can be stored in a 32 bit unsigned long then
1585 * we can be binary compatible with 2.2.x kernels. If not,
1586 * well, in that case 2.2.x was broken anyways...
1587 *
1588 * -Erik Andersen <andersee@debian.org>
1589 */
1590
d4d23add
KM
1591 mem_total = info->totalram + info->totalswap;
1592 if (mem_total < info->totalram || mem_total < info->totalswap)
1da177e4
LT
1593 goto out;
1594 bitcount = 0;
d4d23add 1595 mem_unit = info->mem_unit;
1da177e4
LT
1596 while (mem_unit > 1) {
1597 bitcount++;
1598 mem_unit >>= 1;
1599 sav_total = mem_total;
1600 mem_total <<= 1;
1601 if (mem_total < sav_total)
1602 goto out;
1603 }
1604
1605 /*
1606 * If mem_total did not overflow, multiply all memory values by
d4d23add 1607 * info->mem_unit and set it to 1. This leaves things compatible
1da177e4
LT
1608 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1609 * kernels...
1610 */
1611
d4d23add
KM
1612 info->mem_unit = 1;
1613 info->totalram <<= bitcount;
1614 info->freeram <<= bitcount;
1615 info->sharedram <<= bitcount;
1616 info->bufferram <<= bitcount;
1617 info->totalswap <<= bitcount;
1618 info->freeswap <<= bitcount;
1619 info->totalhigh <<= bitcount;
1620 info->freehigh <<= bitcount;
1621
1622out:
1623 return 0;
1624}
1625
1626asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1627{
1628 struct sysinfo val;
1629
1630 do_sysinfo(&val);
1da177e4 1631
1da177e4
LT
1632 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1633 return -EFAULT;
1634
1635 return 0;
1636}
1637
d730e882
IM
1638/*
1639 * lockdep: we want to track each per-CPU base as a separate lock-class,
1640 * but timer-bases are kmalloc()-ed, so we need to attach separate
1641 * keys to them:
1642 */
1643static struct lock_class_key base_lock_keys[NR_CPUS];
1644
a4a6198b 1645static int __devinit init_timers_cpu(int cpu)
1da177e4
LT
1646{
1647 int j;
1648 tvec_base_t *base;
ba6edfcd 1649 static char __devinitdata tvec_base_done[NR_CPUS];
55c888d6 1650
ba6edfcd 1651 if (!tvec_base_done[cpu]) {
a4a6198b
JB
1652 static char boot_done;
1653
a4a6198b 1654 if (boot_done) {
ba6edfcd
AM
1655 /*
1656 * The APs use this path later in boot
1657 */
a4a6198b
JB
1658 base = kmalloc_node(sizeof(*base), GFP_KERNEL,
1659 cpu_to_node(cpu));
1660 if (!base)
1661 return -ENOMEM;
6e453a67
VP
1662
1663 /* Make sure that tvec_base is 2 byte aligned */
1664 if (tbase_get_deferrable(base)) {
1665 WARN_ON(1);
1666 kfree(base);
1667 return -ENOMEM;
1668 }
a4a6198b 1669 memset(base, 0, sizeof(*base));
ba6edfcd 1670 per_cpu(tvec_bases, cpu) = base;
a4a6198b 1671 } else {
ba6edfcd
AM
1672 /*
1673 * This is for the boot CPU - we use compile-time
1674 * static initialisation because per-cpu memory isn't
1675 * ready yet and because the memory allocators are not
1676 * initialised either.
1677 */
a4a6198b 1678 boot_done = 1;
ba6edfcd 1679 base = &boot_tvec_bases;
a4a6198b 1680 }
ba6edfcd
AM
1681 tvec_base_done[cpu] = 1;
1682 } else {
1683 base = per_cpu(tvec_bases, cpu);
a4a6198b 1684 }
ba6edfcd 1685
3691c519 1686 spin_lock_init(&base->lock);
d730e882
IM
1687 lockdep_set_class(&base->lock, base_lock_keys + cpu);
1688
1da177e4
LT
1689 for (j = 0; j < TVN_SIZE; j++) {
1690 INIT_LIST_HEAD(base->tv5.vec + j);
1691 INIT_LIST_HEAD(base->tv4.vec + j);
1692 INIT_LIST_HEAD(base->tv3.vec + j);
1693 INIT_LIST_HEAD(base->tv2.vec + j);
1694 }
1695 for (j = 0; j < TVR_SIZE; j++)
1696 INIT_LIST_HEAD(base->tv1.vec + j);
1697
1698 base->timer_jiffies = jiffies;
a4a6198b 1699 return 0;
1da177e4
LT
1700}
1701
1702#ifdef CONFIG_HOTPLUG_CPU
55c888d6 1703static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
1da177e4
LT
1704{
1705 struct timer_list *timer;
1706
1707 while (!list_empty(head)) {
1708 timer = list_entry(head->next, struct timer_list, entry);
55c888d6 1709 detach_timer(timer, 0);
6e453a67 1710 timer_set_base(timer, new_base);
1da177e4 1711 internal_add_timer(new_base, timer);
1da177e4 1712 }
1da177e4
LT
1713}
1714
1715static void __devinit migrate_timers(int cpu)
1716{
1717 tvec_base_t *old_base;
1718 tvec_base_t *new_base;
1719 int i;
1720
1721 BUG_ON(cpu_online(cpu));
a4a6198b
JB
1722 old_base = per_cpu(tvec_bases, cpu);
1723 new_base = get_cpu_var(tvec_bases);
1da177e4
LT
1724
1725 local_irq_disable();
e81ce1f7
HC
1726 double_spin_lock(&new_base->lock, &old_base->lock,
1727 smp_processor_id() < cpu);
3691c519
ON
1728
1729 BUG_ON(old_base->running_timer);
1da177e4 1730
1da177e4 1731 for (i = 0; i < TVR_SIZE; i++)
55c888d6
ON
1732 migrate_timer_list(new_base, old_base->tv1.vec + i);
1733 for (i = 0; i < TVN_SIZE; i++) {
1734 migrate_timer_list(new_base, old_base->tv2.vec + i);
1735 migrate_timer_list(new_base, old_base->tv3.vec + i);
1736 migrate_timer_list(new_base, old_base->tv4.vec + i);
1737 migrate_timer_list(new_base, old_base->tv5.vec + i);
1738 }
1739
e81ce1f7
HC
1740 double_spin_unlock(&new_base->lock, &old_base->lock,
1741 smp_processor_id() < cpu);
1da177e4
LT
1742 local_irq_enable();
1743 put_cpu_var(tvec_bases);
1da177e4
LT
1744}
1745#endif /* CONFIG_HOTPLUG_CPU */
1746
8c78f307 1747static int __cpuinit timer_cpu_notify(struct notifier_block *self,
1da177e4
LT
1748 unsigned long action, void *hcpu)
1749{
1750 long cpu = (long)hcpu;
1751 switch(action) {
1752 case CPU_UP_PREPARE:
a4a6198b
JB
1753 if (init_timers_cpu(cpu) < 0)
1754 return NOTIFY_BAD;
1da177e4
LT
1755 break;
1756#ifdef CONFIG_HOTPLUG_CPU
1757 case CPU_DEAD:
1758 migrate_timers(cpu);
1759 break;
1760#endif
1761 default:
1762 break;
1763 }
1764 return NOTIFY_OK;
1765}
1766
8c78f307 1767static struct notifier_block __cpuinitdata timers_nb = {
1da177e4
LT
1768 .notifier_call = timer_cpu_notify,
1769};
1770
1771
1772void __init init_timers(void)
1773{
07dccf33 1774 int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1da177e4 1775 (void *)(long)smp_processor_id());
07dccf33 1776
82f67cd9
IM
1777 init_timer_stats();
1778
07dccf33 1779 BUG_ON(err == NOTIFY_BAD);
1da177e4
LT
1780 register_cpu_notifier(&timers_nb);
1781 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1782}
1783
1784#ifdef CONFIG_TIME_INTERPOLATION
1785
67890d70
CL
1786struct time_interpolator *time_interpolator __read_mostly;
1787static struct time_interpolator *time_interpolator_list __read_mostly;
1da177e4
LT
1788static DEFINE_SPINLOCK(time_interpolator_lock);
1789
3db5db4f 1790static inline cycles_t time_interpolator_get_cycles(unsigned int src)
1da177e4
LT
1791{
1792 unsigned long (*x)(void);
1793
1794 switch (src)
1795 {
1796 case TIME_SOURCE_FUNCTION:
1797 x = time_interpolator->addr;
1798 return x();
1799
1800 case TIME_SOURCE_MMIO64 :
685db65e 1801 return readq_relaxed((void __iomem *)time_interpolator->addr);
1da177e4
LT
1802
1803 case TIME_SOURCE_MMIO32 :
685db65e 1804 return readl_relaxed((void __iomem *)time_interpolator->addr);
1da177e4
LT
1805
1806 default: return get_cycles();
1807 }
1808}
1809
486d46ae 1810static inline u64 time_interpolator_get_counter(int writelock)
1da177e4
LT
1811{
1812 unsigned int src = time_interpolator->source;
1813
1814 if (time_interpolator->jitter)
1815 {
3db5db4f
HD
1816 cycles_t lcycle;
1817 cycles_t now;
1da177e4
LT
1818
1819 do {
1820 lcycle = time_interpolator->last_cycle;
1821 now = time_interpolator_get_cycles(src);
1822 if (lcycle && time_after(lcycle, now))
1823 return lcycle;
486d46ae
AW
1824
1825 /* When holding the xtime write lock, there's no need
1826 * to add the overhead of the cmpxchg. Readers are
1827 * force to retry until the write lock is released.
1828 */
1829 if (writelock) {
1830 time_interpolator->last_cycle = now;
1831 return now;
1832 }
1da177e4
LT
1833 /* Keep track of the last timer value returned. The use of cmpxchg here
1834 * will cause contention in an SMP environment.
1835 */
1836 } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
1837 return now;
1838 }
1839 else
1840 return time_interpolator_get_cycles(src);
1841}
1842
1843void time_interpolator_reset(void)
1844{
1845 time_interpolator->offset = 0;
486d46ae 1846 time_interpolator->last_counter = time_interpolator_get_counter(1);
1da177e4
LT
1847}
1848
1849#define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
1850
1851unsigned long time_interpolator_get_offset(void)
1852{
1853 /* If we do not have a time interpolator set up then just return zero */
1854 if (!time_interpolator)
1855 return 0;
1856
1857 return time_interpolator->offset +
486d46ae 1858 GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
1da177e4
LT
1859}
1860
1861#define INTERPOLATOR_ADJUST 65536
1862#define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
1863
4c7ee8de 1864void time_interpolator_update(long delta_nsec)
1da177e4
LT
1865{
1866 u64 counter;
1867 unsigned long offset;
1868
1869 /* If there is no time interpolator set up then do nothing */
1870 if (!time_interpolator)
1871 return;
1872
a5a0d52c
AM
1873 /*
1874 * The interpolator compensates for late ticks by accumulating the late
1875 * time in time_interpolator->offset. A tick earlier than expected will
1876 * lead to a reset of the offset and a corresponding jump of the clock
1877 * forward. Again this only works if the interpolator clock is running
1878 * slightly slower than the regular clock and the tuning logic insures
1879 * that.
1880 */
1da177e4 1881
486d46ae 1882 counter = time_interpolator_get_counter(1);
a5a0d52c
AM
1883 offset = time_interpolator->offset +
1884 GET_TI_NSECS(counter, time_interpolator);
1da177e4
LT
1885
1886 if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
1887 time_interpolator->offset = offset - delta_nsec;
1888 else {
1889 time_interpolator->skips++;
1890 time_interpolator->ns_skipped += delta_nsec - offset;
1891 time_interpolator->offset = 0;
1892 }
1893 time_interpolator->last_counter = counter;
1894
1895 /* Tuning logic for time interpolator invoked every minute or so.
1896 * Decrease interpolator clock speed if no skips occurred and an offset is carried.
1897 * Increase interpolator clock speed if we skip too much time.
1898 */
1899 if (jiffies % INTERPOLATOR_ADJUST == 0)
1900 {
b20367a6 1901 if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec)
1da177e4
LT
1902 time_interpolator->nsec_per_cyc--;
1903 if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
1904 time_interpolator->nsec_per_cyc++;
1905 time_interpolator->skips = 0;
1906 time_interpolator->ns_skipped = 0;
1907 }
1908}
1909
1910static inline int
1911is_better_time_interpolator(struct time_interpolator *new)
1912{
1913 if (!time_interpolator)
1914 return 1;
1915 return new->frequency > 2*time_interpolator->frequency ||
1916 (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
1917}
1918
1919void
1920register_time_interpolator(struct time_interpolator *ti)
1921{
1922 unsigned long flags;
1923
1924 /* Sanity check */
9f31252c 1925 BUG_ON(ti->frequency == 0 || ti->mask == 0);
1da177e4
LT
1926
1927 ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
1928 spin_lock(&time_interpolator_lock);
1929 write_seqlock_irqsave(&xtime_lock, flags);
1930 if (is_better_time_interpolator(ti)) {
1931 time_interpolator = ti;
1932 time_interpolator_reset();
1933 }
1934 write_sequnlock_irqrestore(&xtime_lock, flags);
1935
1936 ti->next = time_interpolator_list;
1937 time_interpolator_list = ti;
1938 spin_unlock(&time_interpolator_lock);
1939}
1940
1941void
1942unregister_time_interpolator(struct time_interpolator *ti)
1943{
1944 struct time_interpolator *curr, **prev;
1945 unsigned long flags;
1946
1947 spin_lock(&time_interpolator_lock);
1948 prev = &time_interpolator_list;
1949 for (curr = *prev; curr; curr = curr->next) {
1950 if (curr == ti) {
1951 *prev = curr->next;
1952 break;
1953 }
1954 prev = &curr->next;
1955 }
1956
1957 write_seqlock_irqsave(&xtime_lock, flags);
1958 if (ti == time_interpolator) {
1959 /* we lost the best time-interpolator: */
1960 time_interpolator = NULL;
1961 /* find the next-best interpolator */
1962 for (curr = time_interpolator_list; curr; curr = curr->next)
1963 if (is_better_time_interpolator(curr))
1964 time_interpolator = curr;
1965 time_interpolator_reset();
1966 }
1967 write_sequnlock_irqrestore(&xtime_lock, flags);
1968 spin_unlock(&time_interpolator_lock);
1969}
1970#endif /* CONFIG_TIME_INTERPOLATION */
1971
1972/**
1973 * msleep - sleep safely even with waitqueue interruptions
1974 * @msecs: Time in milliseconds to sleep for
1975 */
1976void msleep(unsigned int msecs)
1977{
1978 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1979
75bcc8c5
NA
1980 while (timeout)
1981 timeout = schedule_timeout_uninterruptible(timeout);
1da177e4
LT
1982}
1983
1984EXPORT_SYMBOL(msleep);
1985
1986/**
96ec3efd 1987 * msleep_interruptible - sleep waiting for signals
1da177e4
LT
1988 * @msecs: Time in milliseconds to sleep for
1989 */
1990unsigned long msleep_interruptible(unsigned int msecs)
1991{
1992 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1993
75bcc8c5
NA
1994 while (timeout && !signal_pending(current))
1995 timeout = schedule_timeout_interruptible(timeout);
1da177e4
LT
1996 return jiffies_to_msecs(timeout);
1997}
1998
1999EXPORT_SYMBOL(msleep_interruptible);