blob: 201bee07b8e030afdf36952b93b38b37c718ba96 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Adrian Bunk97a41e22006-01-08 01:02:17 -080036#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/uaccess.h>
39#include <asm/unistd.h>
40#include <asm/div64.h>
41#include <asm/timex.h>
42#include <asm/io.h>
43
Thomas Gleixnerecea8d12005-10-30 15:03:00 -080044u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
45
46EXPORT_SYMBOL(jiffies_64);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * per-CPU timer vector definitions:
50 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
52#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
53#define TVN_SIZE (1 << TVN_BITS)
54#define TVR_SIZE (1 << TVR_BITS)
55#define TVN_MASK (TVN_SIZE - 1)
56#define TVR_MASK (TVR_SIZE - 1)
57
58typedef struct tvec_s {
59 struct list_head vec[TVN_SIZE];
60} tvec_t;
61
62typedef struct tvec_root_s {
63 struct list_head vec[TVR_SIZE];
64} tvec_root_t;
65
66struct tvec_t_base_s {
Oleg Nesterov3691c512006-03-31 02:30:30 -080067 spinlock_t lock;
68 struct timer_list *running_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 unsigned long timer_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 tvec_root_t tv1;
71 tvec_t tv2;
72 tvec_t tv3;
73 tvec_t tv4;
74 tvec_t tv5;
75} ____cacheline_aligned_in_smp;
76
77typedef struct tvec_t_base_s tvec_base_t;
Andrew Mortonba6edfc2006-04-10 22:53:58 -070078
Oleg Nesterov3691c512006-03-31 02:30:30 -080079tvec_base_t boot_tvec_bases;
80EXPORT_SYMBOL(boot_tvec_bases);
Josh Triplett51d8c5e2006-07-30 03:04:14 -070081static DEFINE_PER_CPU(tvec_base_t *, tvec_bases) = &boot_tvec_bases;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -080083/**
84 * __round_jiffies - function to round jiffies to a full second
85 * @j: the time in (absolute) jiffies that should be rounded
86 * @cpu: the processor number on which the timeout will happen
87 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080088 * __round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -080089 * up or down to (approximately) full seconds. This is useful for timers
90 * for which the exact time they fire does not matter too much, as long as
91 * they fire approximately every X seconds.
92 *
93 * By rounding these timers to whole seconds, all such timers will fire
94 * at the same time, rather than at various times spread out. The goal
95 * of this is to have the CPU wake up less, which saves power.
96 *
97 * The exact rounding is skewed for each processor to avoid all
98 * processors firing at the exact same time, which could lead
99 * to lock contention or spurious cache line bouncing.
100 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800101 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800102 */
103unsigned long __round_jiffies(unsigned long j, int cpu)
104{
105 int rem;
106 unsigned long original = j;
107
108 /*
109 * We don't want all cpus firing their timers at once hitting the
110 * same lock or cachelines, so we skew each extra cpu with an extra
111 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
112 * already did this.
113 * The skew is done by adding 3*cpunr, then round, then subtract this
114 * extra offset again.
115 */
116 j += cpu * 3;
117
118 rem = j % HZ;
119
120 /*
121 * If the target jiffie is just after a whole second (which can happen
122 * due to delays of the timer irq, long irq off times etc etc) then
123 * we should round down to the whole second, not up. Use 1/4th second
124 * as cutoff for this rounding as an extreme upper bound for this.
125 */
126 if (rem < HZ/4) /* round down */
127 j = j - rem;
128 else /* round up */
129 j = j - rem + HZ;
130
131 /* now that we have rounded, subtract the extra skew again */
132 j -= cpu * 3;
133
134 if (j <= jiffies) /* rounding ate our timeout entirely; */
135 return original;
136 return j;
137}
138EXPORT_SYMBOL_GPL(__round_jiffies);
139
140/**
141 * __round_jiffies_relative - function to round jiffies to a full second
142 * @j: the time in (relative) jiffies that should be rounded
143 * @cpu: the processor number on which the timeout will happen
144 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800145 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800146 * up or down to (approximately) full seconds. This is useful for timers
147 * for which the exact time they fire does not matter too much, as long as
148 * they fire approximately every X seconds.
149 *
150 * By rounding these timers to whole seconds, all such timers will fire
151 * at the same time, rather than at various times spread out. The goal
152 * of this is to have the CPU wake up less, which saves power.
153 *
154 * The exact rounding is skewed for each processor to avoid all
155 * processors firing at the exact same time, which could lead
156 * to lock contention or spurious cache line bouncing.
157 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800158 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800159 */
160unsigned long __round_jiffies_relative(unsigned long j, int cpu)
161{
162 /*
163 * In theory the following code can skip a jiffy in case jiffies
164 * increments right between the addition and the later subtraction.
165 * However since the entire point of this function is to use approximate
166 * timeouts, it's entirely ok to not handle that.
167 */
168 return __round_jiffies(j + jiffies, cpu) - jiffies;
169}
170EXPORT_SYMBOL_GPL(__round_jiffies_relative);
171
172/**
173 * round_jiffies - function to round jiffies to a full second
174 * @j: the time in (absolute) jiffies that should be rounded
175 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800176 * round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800177 * up or down to (approximately) full seconds. This is useful for timers
178 * for which the exact time they fire does not matter too much, as long as
179 * they fire approximately every X seconds.
180 *
181 * By rounding these timers to whole seconds, all such timers will fire
182 * at the same time, rather than at various times spread out. The goal
183 * of this is to have the CPU wake up less, which saves power.
184 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800185 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800186 */
187unsigned long round_jiffies(unsigned long j)
188{
189 return __round_jiffies(j, raw_smp_processor_id());
190}
191EXPORT_SYMBOL_GPL(round_jiffies);
192
193/**
194 * round_jiffies_relative - function to round jiffies to a full second
195 * @j: the time in (relative) jiffies that should be rounded
196 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800197 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800198 * up or down to (approximately) full seconds. This is useful for timers
199 * for which the exact time they fire does not matter too much, as long as
200 * they fire approximately every X seconds.
201 *
202 * By rounding these timers to whole seconds, all such timers will fire
203 * at the same time, rather than at various times spread out. The goal
204 * of this is to have the CPU wake up less, which saves power.
205 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800206 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800207 */
208unsigned long round_jiffies_relative(unsigned long j)
209{
210 return __round_jiffies_relative(j, raw_smp_processor_id());
211}
212EXPORT_SYMBOL_GPL(round_jiffies_relative);
213
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215static inline void set_running_timer(tvec_base_t *base,
216 struct timer_list *timer)
217{
218#ifdef CONFIG_SMP
Oleg Nesterov3691c512006-03-31 02:30:30 -0800219 base->running_timer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#endif
221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
224{
225 unsigned long expires = timer->expires;
226 unsigned long idx = expires - base->timer_jiffies;
227 struct list_head *vec;
228
229 if (idx < TVR_SIZE) {
230 int i = expires & TVR_MASK;
231 vec = base->tv1.vec + i;
232 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
233 int i = (expires >> TVR_BITS) & TVN_MASK;
234 vec = base->tv2.vec + i;
235 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
236 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
237 vec = base->tv3.vec + i;
238 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
239 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
240 vec = base->tv4.vec + i;
241 } else if ((signed long) idx < 0) {
242 /*
243 * Can happen if you add a timer with expires == jiffies,
244 * or you set a timer to go off in the past
245 */
246 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
247 } else {
248 int i;
249 /* If the timeout is larger than 0xffffffff on 64-bit
250 * architectures then we use the maximum timeout:
251 */
252 if (idx > 0xffffffffUL) {
253 idx = 0xffffffffUL;
254 expires = idx + base->timer_jiffies;
255 }
256 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
257 vec = base->tv5.vec + i;
258 }
259 /*
260 * Timers are FIFO:
261 */
262 list_add_tail(&timer->entry, vec);
263}
264
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700265/**
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700266 * init_timer - initialize a timer.
267 * @timer: the timer to be initialized
268 *
269 * init_timer() must be done to a timer prior calling *any* of the
270 * other timer functions.
271 */
272void fastcall init_timer(struct timer_list *timer)
273{
274 timer->entry.next = NULL;
Paul Mackerrasbfe5d832006-06-25 05:47:14 -0700275 timer->base = __raw_get_cpu_var(tvec_bases);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700276}
277EXPORT_SYMBOL(init_timer);
278
279static inline void detach_timer(struct timer_list *timer,
280 int clear_pending)
281{
282 struct list_head *entry = &timer->entry;
283
284 __list_del(entry->prev, entry->next);
285 if (clear_pending)
286 entry->next = NULL;
287 entry->prev = LIST_POISON2;
288}
289
290/*
Oleg Nesterov3691c512006-03-31 02:30:30 -0800291 * We are using hashed locking: holding per_cpu(tvec_bases).lock
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700292 * means that all timers which are tied to this base via timer->base are
293 * locked, and the base itself is locked too.
294 *
295 * So __run_timers/migrate_timers can safely modify all timers which could
296 * be found on ->tvX lists.
297 *
298 * When the timer's base is locked, and the timer removed from list, it is
299 * possible to set timer->base = NULL and drop the lock: the timer remains
300 * locked.
301 */
Oleg Nesterov3691c512006-03-31 02:30:30 -0800302static tvec_base_t *lock_timer_base(struct timer_list *timer,
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700303 unsigned long *flags)
Josh Triplett89e7e3742006-09-29 01:59:36 -0700304 __acquires(timer->base->lock)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700305{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800306 tvec_base_t *base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700307
308 for (;;) {
309 base = timer->base;
310 if (likely(base != NULL)) {
311 spin_lock_irqsave(&base->lock, *flags);
312 if (likely(base == timer->base))
313 return base;
314 /* The timer has migrated to another CPU */
315 spin_unlock_irqrestore(&base->lock, *flags);
316 }
317 cpu_relax();
318 }
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321int __mod_timer(struct timer_list *timer, unsigned long expires)
322{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800323 tvec_base_t *base, *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 unsigned long flags;
325 int ret = 0;
326
327 BUG_ON(!timer->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700329 base = lock_timer_base(timer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700331 if (timer_pending(timer)) {
332 detach_timer(timer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 ret = 1;
334 }
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700335
Jan Beulicha4a61982006-03-24 03:15:54 -0800336 new_base = __get_cpu_var(tvec_bases);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700337
Oleg Nesterov3691c512006-03-31 02:30:30 -0800338 if (base != new_base) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700339 /*
340 * We are trying to schedule the timer on the local CPU.
341 * However we can't change timer's base while it is running,
342 * otherwise del_timer_sync() can't detect that the timer's
343 * handler yet has not finished. This also guarantees that
344 * the timer is serialized wrt itself.
345 */
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800346 if (likely(base->running_timer != timer)) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700347 /* See the comment in lock_timer_base() */
348 timer->base = NULL;
349 spin_unlock(&base->lock);
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800350 base = new_base;
351 spin_lock(&base->lock);
352 timer->base = base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700353 }
354 }
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 timer->expires = expires;
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800357 internal_add_timer(base, timer);
358 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 return ret;
361}
362
363EXPORT_SYMBOL(__mod_timer);
364
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700365/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * add_timer_on - start a timer on a particular CPU
367 * @timer: the timer to be added
368 * @cpu: the CPU to start it on
369 *
370 * This is not very scalable on SMP. Double adds are not possible.
371 */
372void add_timer_on(struct timer_list *timer, int cpu)
373{
Jan Beulicha4a61982006-03-24 03:15:54 -0800374 tvec_base_t *base = per_cpu(tvec_bases, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 BUG_ON(timer_pending(timer) || !timer->function);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800378 spin_lock_irqsave(&base->lock, flags);
379 timer->base = base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 internal_add_timer(base, timer);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800381 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700385/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * mod_timer - modify a timer's timeout
387 * @timer: the timer to be modified
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700388 * @expires: new timeout in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800390 * mod_timer() is a more efficient way to update the expire field of an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * active timer (if the timer is inactive it will be activated)
392 *
393 * mod_timer(timer, expires) is equivalent to:
394 *
395 * del_timer(timer); timer->expires = expires; add_timer(timer);
396 *
397 * Note that if there are multiple unserialized concurrent users of the
398 * same timer, then mod_timer() is the only safe way to modify the timeout,
399 * since add_timer() cannot modify an already running timer.
400 *
401 * The function returns whether it has modified a pending timer or not.
402 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
403 * active timer returns 1.)
404 */
405int mod_timer(struct timer_list *timer, unsigned long expires)
406{
407 BUG_ON(!timer->function);
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /*
410 * This is a common optimization triggered by the
411 * networking code - if the timer is re-modified
412 * to be the same thing then just return:
413 */
414 if (timer->expires == expires && timer_pending(timer))
415 return 1;
416
417 return __mod_timer(timer, expires);
418}
419
420EXPORT_SYMBOL(mod_timer);
421
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700422/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * del_timer - deactive a timer.
424 * @timer: the timer to be deactivated
425 *
426 * del_timer() deactivates a timer - this works on both active and inactive
427 * timers.
428 *
429 * The function returns whether it has deactivated a pending timer or not.
430 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
431 * active timer returns 1.)
432 */
433int del_timer(struct timer_list *timer)
434{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800435 tvec_base_t *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700437 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700439 if (timer_pending(timer)) {
440 base = lock_timer_base(timer, &flags);
441 if (timer_pending(timer)) {
442 detach_timer(timer, 1);
443 ret = 1;
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700448 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451EXPORT_SYMBOL(del_timer);
452
453#ifdef CONFIG_SMP
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700454/**
455 * try_to_del_timer_sync - Try to deactivate a timer
456 * @timer: timer do del
457 *
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700458 * This function tries to deactivate a timer. Upon successful (ret >= 0)
459 * exit the timer is not queued and the handler is not running on any CPU.
460 *
461 * It must not be called from interrupt contexts.
462 */
463int try_to_del_timer_sync(struct timer_list *timer)
464{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800465 tvec_base_t *base;
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700466 unsigned long flags;
467 int ret = -1;
468
469 base = lock_timer_base(timer, &flags);
470
471 if (base->running_timer == timer)
472 goto out;
473
474 ret = 0;
475 if (timer_pending(timer)) {
476 detach_timer(timer, 1);
477 ret = 1;
478 }
479out:
480 spin_unlock_irqrestore(&base->lock, flags);
481
482 return ret;
483}
484
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700485/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * del_timer_sync - deactivate a timer and wait for the handler to finish.
487 * @timer: the timer to be deactivated
488 *
489 * This function only differs from del_timer() on SMP: besides deactivating
490 * the timer it also makes sure the handler has finished executing on other
491 * CPUs.
492 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800493 * Synchronization rules: Callers must prevent restarting of the timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * otherwise this function is meaningless. It must not be called from
495 * interrupt contexts. The caller must not hold locks which would prevent
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700496 * completion of the timer's handler. The timer's handler must not call
497 * add_timer_on(). Upon exit the timer is not queued and the handler is
498 * not running on any CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 *
500 * The function returns whether it has deactivated a pending timer or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 */
502int del_timer_sync(struct timer_list *timer)
503{
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700504 for (;;) {
505 int ret = try_to_del_timer_sync(timer);
506 if (ret >= 0)
507 return ret;
Andrew Mortona0009652006-07-14 00:24:06 -0700508 cpu_relax();
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512EXPORT_SYMBOL(del_timer_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513#endif
514
515static int cascade(tvec_base_t *base, tvec_t *tv, int index)
516{
517 /* cascade all the timers from tv up one level */
Porpoise3439dd82006-06-23 02:05:56 -0700518 struct timer_list *timer, *tmp;
519 struct list_head tv_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Porpoise3439dd82006-06-23 02:05:56 -0700521 list_replace_init(tv->vec + index, &tv_list);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /*
Porpoise3439dd82006-06-23 02:05:56 -0700524 * We are removing _all_ timers from the list, so we
525 * don't have to detach them individually.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 */
Porpoise3439dd82006-06-23 02:05:56 -0700527 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
528 BUG_ON(timer->base != base);
529 internal_add_timer(base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 return index;
533}
534
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700535#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
536
537/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * __run_timers - run all expired timers (if any) on this CPU.
539 * @base: the timer vector to be processed.
540 *
541 * This function cascades all vectors and executes all expired timer
542 * vectors.
543 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544static inline void __run_timers(tvec_base_t *base)
545{
546 struct timer_list *timer;
547
Oleg Nesterov3691c512006-03-31 02:30:30 -0800548 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 while (time_after_eq(jiffies, base->timer_jiffies)) {
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700550 struct list_head work_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct list_head *head = &work_list;
552 int index = base->timer_jiffies & TVR_MASK;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /*
555 * Cascade timers:
556 */
557 if (!index &&
558 (!cascade(base, &base->tv2, INDEX(0))) &&
559 (!cascade(base, &base->tv3, INDEX(1))) &&
560 !cascade(base, &base->tv4, INDEX(2)))
561 cascade(base, &base->tv5, INDEX(3));
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700562 ++base->timer_jiffies;
563 list_replace_init(base->tv1.vec + index, &work_list);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700564 while (!list_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 void (*fn)(unsigned long);
566 unsigned long data;
567
568 timer = list_entry(head->next,struct timer_list,entry);
569 fn = timer->function;
570 data = timer->data;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 set_running_timer(base, timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700573 detach_timer(timer, 1);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800574 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700576 int preempt_count = preempt_count();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 fn(data);
578 if (preempt_count != preempt_count()) {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700579 printk(KERN_WARNING "huh, entered %p "
580 "with preempt_count %08x, exited"
581 " with %08x?\n",
582 fn, preempt_count,
583 preempt_count());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 BUG();
585 }
586 }
Oleg Nesterov3691c512006-03-31 02:30:30 -0800587 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589 }
590 set_running_timer(base, NULL);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800591 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594#ifdef CONFIG_NO_IDLE_HZ
595/*
596 * Find out when the next timer event is due to happen. This
597 * is used on S/390 to stop all activity when a cpus is idle.
598 * This functions needs to be called disabled.
599 */
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800600static unsigned long __next_timer_interrupt(tvec_base_t *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800602 unsigned long timer_jiffies = base->timer_jiffies;
603 unsigned long expires = timer_jiffies + (LONG_MAX >> 1);
604 int index, slot, array, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct timer_list *nte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 tvec_t *varray[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* Look for timer events in tv1. */
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800609 index = slot = timer_jiffies & TVR_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800611 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
612 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800614 /* Look at the cascade bucket(s)? */
615 if (!index || slot < index)
616 goto cascade;
617 return expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800619 slot = (slot + 1) & TVR_MASK;
620 } while (slot != index);
621
622cascade:
623 /* Calculate the next cascade event */
624 if (index)
625 timer_jiffies += TVR_SIZE - index;
626 timer_jiffies >>= TVR_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* Check tv2-tv5. */
629 varray[0] = &base->tv2;
630 varray[1] = &base->tv3;
631 varray[2] = &base->tv4;
632 varray[3] = &base->tv5;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800633
634 for (array = 0; array < 4; array++) {
635 tvec_t *varp = varray[array];
636
637 index = slot = timer_jiffies & TVN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800639 list_for_each_entry(nte, varp->vec + slot, entry) {
640 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (time_before(nte->expires, expires))
642 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800643 }
644 /*
645 * Do we still search for the first timer or are
646 * we looking up the cascade buckets ?
647 */
648 if (found) {
649 /* Look at the cascade bucket(s)? */
650 if (!index || slot < index)
651 break;
652 return expires;
653 }
654 slot = (slot + 1) & TVN_MASK;
655 } while (slot != index);
656
657 if (index)
658 timer_jiffies += TVN_SIZE - index;
659 timer_jiffies >>= TVN_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800661 return expires;
662}
663
664/*
665 * Check, if the next hrtimer event is before the next timer wheel
666 * event:
667 */
668static unsigned long cmp_next_hrtimer_event(unsigned long now,
669 unsigned long expires)
670{
671 ktime_t hr_delta = hrtimer_get_next_event();
672 struct timespec tsdelta;
673
674 if (hr_delta.tv64 == KTIME_MAX)
675 return expires;
676
677 if (hr_delta.tv64 <= TICK_NSEC)
678 return now;
679
680 tsdelta = ktime_to_timespec(hr_delta);
681 now += timespec_to_jiffies(&tsdelta);
682 if (time_before(now, expires))
683 return now;
684 return expires;
685}
686
687/**
688 * next_timer_interrupt - return the jiffy of the next pending timer
689 */
690unsigned long next_timer_interrupt(void)
691{
692 tvec_base_t *base = __get_cpu_var(tvec_bases);
693 unsigned long expires, now = jiffies;
694
695 spin_lock(&base->lock);
696 expires = __next_timer_interrupt(base);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800697 spin_unlock(&base->lock);
Tony Lindgren69239742006-03-06 15:42:45 -0800698
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800699 if (time_before_eq(expires, now))
700 return now;
Zachary Amsden0662b712006-05-20 15:00:24 -0700701
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800702 return cmp_next_hrtimer_event(now, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704#endif
705
706/******************************************************************/
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/*
709 * The current time
710 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
711 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
712 * at zero at system boot time, so wall_to_monotonic will be negative,
713 * however, we will ALWAYS keep the tv_nsec part positive so we can use
714 * the usual normalization.
715 */
716struct timespec xtime __attribute__ ((aligned (16)));
717struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
718
719EXPORT_SYMBOL(xtime);
720
Paul Mackerras726c14b2006-02-17 10:30:23 +1100721
john stultzad596172006-06-26 00:25:06 -0700722/* XXX - all of this timekeeping code should be later moved to time.c */
723#include <linux/clocksource.h>
724static struct clocksource *clock; /* pointer to current clocksource */
john stultzcf3c7692006-06-26 00:25:08 -0700725
726#ifdef CONFIG_GENERIC_TIME
727/**
728 * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook
729 *
730 * private function, must hold xtime_lock lock when being
731 * called. Returns the number of nanoseconds since the
732 * last call to update_wall_time() (adjusted by NTP scaling)
733 */
734static inline s64 __get_nsec_offset(void)
735{
736 cycle_t cycle_now, cycle_delta;
737 s64 ns_offset;
738
739 /* read clocksource: */
john stultza2752542006-06-26 00:25:14 -0700740 cycle_now = clocksource_read(clock);
john stultzcf3c7692006-06-26 00:25:08 -0700741
742 /* calculate the delta since the last update_wall_time: */
Roman Zippel19923c12006-06-26 00:25:18 -0700743 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
john stultzcf3c7692006-06-26 00:25:08 -0700744
745 /* convert to nanoseconds: */
746 ns_offset = cyc2ns(clock, cycle_delta);
747
748 return ns_offset;
749}
750
751/**
752 * __get_realtime_clock_ts - Returns the time of day in a timespec
753 * @ts: pointer to the timespec to be set
754 *
755 * Returns the time of day in a timespec. Used by
756 * do_gettimeofday() and get_realtime_clock_ts().
757 */
758static inline void __get_realtime_clock_ts(struct timespec *ts)
759{
760 unsigned long seq;
761 s64 nsecs;
762
763 do {
764 seq = read_seqbegin(&xtime_lock);
765
766 *ts = xtime;
767 nsecs = __get_nsec_offset();
768
769 } while (read_seqretry(&xtime_lock, seq));
770
771 timespec_add_ns(ts, nsecs);
772}
773
774/**
john stultza2752542006-06-26 00:25:14 -0700775 * getnstimeofday - Returns the time of day in a timespec
john stultzcf3c7692006-06-26 00:25:08 -0700776 * @ts: pointer to the timespec to be set
777 *
778 * Returns the time of day in a timespec.
779 */
780void getnstimeofday(struct timespec *ts)
781{
782 __get_realtime_clock_ts(ts);
783}
784
785EXPORT_SYMBOL(getnstimeofday);
786
787/**
788 * do_gettimeofday - Returns the time of day in a timeval
789 * @tv: pointer to the timeval to be set
790 *
791 * NOTE: Users should be converted to using get_realtime_clock_ts()
792 */
793void do_gettimeofday(struct timeval *tv)
794{
795 struct timespec now;
796
797 __get_realtime_clock_ts(&now);
798 tv->tv_sec = now.tv_sec;
799 tv->tv_usec = now.tv_nsec/1000;
800}
801
802EXPORT_SYMBOL(do_gettimeofday);
803/**
804 * do_settimeofday - Sets the time of day
805 * @tv: pointer to the timespec variable containing the new time
806 *
807 * Sets the time of day to the new time and update NTP and notify hrtimers
808 */
809int do_settimeofday(struct timespec *tv)
810{
811 unsigned long flags;
812 time_t wtm_sec, sec = tv->tv_sec;
813 long wtm_nsec, nsec = tv->tv_nsec;
814
815 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
816 return -EINVAL;
817
818 write_seqlock_irqsave(&xtime_lock, flags);
819
820 nsec -= __get_nsec_offset();
821
822 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
823 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
824
825 set_normalized_timespec(&xtime, sec, nsec);
826 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
827
Roman Zippele154ff32006-07-10 04:44:32 -0700828 clock->error = 0;
john stultzcf3c7692006-06-26 00:25:08 -0700829 ntp_clear();
830
831 write_sequnlock_irqrestore(&xtime_lock, flags);
832
833 /* signal hrtimers about time change */
834 clock_was_set();
835
836 return 0;
837}
838
839EXPORT_SYMBOL(do_settimeofday);
840
841/**
842 * change_clocksource - Swaps clocksources if a new one is available
843 *
844 * Accumulates current time interval and initializes new clocksource
845 */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800846static void change_clocksource(void)
john stultzcf3c7692006-06-26 00:25:08 -0700847{
848 struct clocksource *new;
849 cycle_t now;
850 u64 nsec;
john stultzcf3c7692006-06-26 00:25:08 -0700851
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800852 new = clocksource_get_next();
853
854 if (clock == new)
855 return;
856
857 now = clocksource_read(new);
858 nsec = __get_nsec_offset();
859 timespec_add_ns(&xtime, nsec);
860
861 clock = new;
862 clock->cycle_last = now;
863
864 clock->error = 0;
865 clock->xtime_nsec = 0;
866 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
867
868 printk(KERN_INFO "Time: %s clocksource has been installed.\n",
869 clock->name);
john stultzcf3c7692006-06-26 00:25:08 -0700870}
871#else
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800872static inline void change_clocksource(void) { }
john stultzcf3c7692006-06-26 00:25:08 -0700873#endif
874
875/**
876 * timeofday_is_continuous - check to see if timekeeping is free running
877 */
878int timekeeping_is_continuous(void)
879{
880 unsigned long seq;
881 int ret;
882
883 do {
884 seq = read_seqbegin(&xtime_lock);
885
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800886 ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultzcf3c7692006-06-26 00:25:08 -0700887
888 } while (read_seqretry(&xtime_lock, seq));
889
890 return ret;
891}
892
John Stultz411187f2007-02-16 01:27:30 -0800893/**
894 * read_persistent_clock - Return time in seconds from the persistent clock.
895 *
896 * Weak dummy function for arches that do not yet support it.
897 * Returns seconds from epoch using the battery backed persistent clock.
898 * Returns zero if unsupported.
899 *
900 * XXX - Do be sure to remove it once all arches implement it.
901 */
902unsigned long __attribute__((weak)) read_persistent_clock(void)
903{
904 return 0;
905}
906
Paul Mackerras726c14b2006-02-17 10:30:23 +1100907/*
john stultzad596172006-06-26 00:25:06 -0700908 * timekeeping_init - Initializes the clocksource and common timekeeping values
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
john stultzad596172006-06-26 00:25:06 -0700910void __init timekeeping_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
john stultzad596172006-06-26 00:25:06 -0700912 unsigned long flags;
John Stultz411187f2007-02-16 01:27:30 -0800913 unsigned long sec = read_persistent_clock();
john stultzad596172006-06-26 00:25:06 -0700914
915 write_seqlock_irqsave(&xtime_lock, flags);
Roman Zippelb0ee7552006-09-30 23:28:22 -0700916
917 ntp_clear();
918
john stultza2752542006-06-26 00:25:14 -0700919 clock = clocksource_get_next();
john stultzf4304ab2007-02-16 01:27:26 -0800920 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
Roman Zippel19923c12006-06-26 00:25:18 -0700921 clock->cycle_last = clocksource_read(clock);
Roman Zippelb0ee7552006-09-30 23:28:22 -0700922
John Stultz411187f2007-02-16 01:27:30 -0800923 xtime.tv_sec = sec;
924 xtime.tv_nsec = 0;
925 set_normalized_timespec(&wall_to_monotonic,
926 -xtime.tv_sec, -xtime.tv_nsec);
927
john stultzad596172006-06-26 00:25:06 -0700928 write_sequnlock_irqrestore(&xtime_lock, flags);
929}
930
931
John Stultz411187f2007-02-16 01:27:30 -0800932/* flag for if timekeeping is suspended */
john stultz3e143472006-07-14 00:24:17 -0700933static int timekeeping_suspended;
John Stultz411187f2007-02-16 01:27:30 -0800934/* time in seconds when suspend began */
935static unsigned long timekeeping_suspend_time;
936
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700937/**
john stultzad596172006-06-26 00:25:06 -0700938 * timekeeping_resume - Resumes the generic timekeeping subsystem.
939 * @dev: unused
940 *
941 * This is for the generic clocksource timekeeping.
Atsushi Nemoto8ef38602006-09-30 23:28:31 -0700942 * xtime/wall_to_monotonic/jiffies/etc are
john stultzad596172006-06-26 00:25:06 -0700943 * still managed by arch specific suspend/resume code.
944 */
945static int timekeeping_resume(struct sys_device *dev)
946{
947 unsigned long flags;
John Stultz411187f2007-02-16 01:27:30 -0800948 unsigned long now = read_persistent_clock();
john stultzad596172006-06-26 00:25:06 -0700949
950 write_seqlock_irqsave(&xtime_lock, flags);
John Stultz411187f2007-02-16 01:27:30 -0800951
952 if (now && (now > timekeeping_suspend_time)) {
953 unsigned long sleep_length = now - timekeeping_suspend_time;
954
955 xtime.tv_sec += sleep_length;
956 wall_to_monotonic.tv_sec -= sleep_length;
957 }
958 /* re-base the last cycle value */
Roman Zippel19923c12006-06-26 00:25:18 -0700959 clock->cycle_last = clocksource_read(clock);
john stultz3e143472006-07-14 00:24:17 -0700960 clock->error = 0;
961 timekeeping_suspended = 0;
962 write_sequnlock_irqrestore(&xtime_lock, flags);
John Stultz411187f2007-02-16 01:27:30 -0800963
964 touch_softlockup_watchdog();
965 hrtimer_notify_resume();
966
john stultz3e143472006-07-14 00:24:17 -0700967 return 0;
968}
969
970static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
971{
972 unsigned long flags;
973
974 write_seqlock_irqsave(&xtime_lock, flags);
975 timekeeping_suspended = 1;
John Stultz411187f2007-02-16 01:27:30 -0800976 timekeeping_suspend_time = read_persistent_clock();
john stultzad596172006-06-26 00:25:06 -0700977 write_sequnlock_irqrestore(&xtime_lock, flags);
978 return 0;
979}
980
981/* sysfs resume/suspend bits for timekeeping */
982static struct sysdev_class timekeeping_sysclass = {
983 .resume = timekeeping_resume,
john stultz3e143472006-07-14 00:24:17 -0700984 .suspend = timekeeping_suspend,
john stultzad596172006-06-26 00:25:06 -0700985 set_kset_name("timekeeping"),
986};
987
988static struct sys_device device_timer = {
989 .id = 0,
990 .cls = &timekeeping_sysclass,
991};
992
993static int __init timekeeping_init_device(void)
994{
995 int error = sysdev_class_register(&timekeeping_sysclass);
996 if (!error)
997 error = sysdev_register(&device_timer);
998 return error;
999}
1000
1001device_initcall(timekeeping_init_device);
1002
1003/*
Roman Zippele154ff32006-07-10 04:44:32 -07001004 * If the error is already larger, we look ahead even further
Roman Zippel19923c12006-06-26 00:25:18 -07001005 * to compensate for late or lost adjustments.
1006 */
Daniel Walkerf5f1a242006-12-10 02:21:33 -08001007static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
1008 s64 *offset)
Roman Zippel19923c12006-06-26 00:25:18 -07001009{
Roman Zippele154ff32006-07-10 04:44:32 -07001010 s64 tick_error, i;
1011 u32 look_ahead, adj;
1012 s32 error2, mult;
Roman Zippel19923c12006-06-26 00:25:18 -07001013
1014 /*
Roman Zippele154ff32006-07-10 04:44:32 -07001015 * Use the current error value to determine how much to look ahead.
1016 * The larger the error the slower we adjust for it to avoid problems
1017 * with losing too many ticks, otherwise we would overadjust and
1018 * produce an even larger error. The smaller the adjustment the
1019 * faster we try to adjust for it, as lost ticks can do less harm
1020 * here. This is tuned so that an error of about 1 msec is adusted
1021 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
Roman Zippel19923c12006-06-26 00:25:18 -07001022 */
Roman Zippele154ff32006-07-10 04:44:32 -07001023 error2 = clock->error >> (TICK_LENGTH_SHIFT + 22 - 2 * SHIFT_HZ);
1024 error2 = abs(error2);
1025 for (look_ahead = 0; error2 > 0; look_ahead++)
1026 error2 >>= 2;
Roman Zippel19923c12006-06-26 00:25:18 -07001027
1028 /*
Roman Zippele154ff32006-07-10 04:44:32 -07001029 * Now calculate the error in (1 << look_ahead) ticks, but first
1030 * remove the single look ahead already included in the error.
Roman Zippel19923c12006-06-26 00:25:18 -07001031 */
Daniel Walkerf5f1a242006-12-10 02:21:33 -08001032 tick_error = current_tick_length() >>
1033 (TICK_LENGTH_SHIFT - clock->shift + 1);
Roman Zippele154ff32006-07-10 04:44:32 -07001034 tick_error -= clock->xtime_interval >> 1;
1035 error = ((error - tick_error) >> look_ahead) + tick_error;
Roman Zippel19923c12006-06-26 00:25:18 -07001036
Roman Zippele154ff32006-07-10 04:44:32 -07001037 /* Finally calculate the adjustment shift value. */
1038 i = *interval;
1039 mult = 1;
1040 if (error < 0) {
1041 error = -error;
1042 *interval = -*interval;
1043 *offset = -*offset;
1044 mult = -1;
Roman Zippel19923c12006-06-26 00:25:18 -07001045 }
Roman Zippele154ff32006-07-10 04:44:32 -07001046 for (adj = 0; error > i; adj++)
1047 error >>= 1;
Roman Zippel19923c12006-06-26 00:25:18 -07001048
1049 *interval <<= adj;
1050 *offset <<= adj;
Roman Zippele154ff32006-07-10 04:44:32 -07001051 return mult << adj;
Roman Zippel19923c12006-06-26 00:25:18 -07001052}
1053
1054/*
1055 * Adjust the multiplier to reduce the error value,
1056 * this is optimized for the most common adjustments of -1,0,1,
1057 * for other values we can do a bit more work.
1058 */
1059static void clocksource_adjust(struct clocksource *clock, s64 offset)
1060{
1061 s64 error, interval = clock->cycle_interval;
1062 int adj;
1063
1064 error = clock->error >> (TICK_LENGTH_SHIFT - clock->shift - 1);
1065 if (error > interval) {
Roman Zippele154ff32006-07-10 04:44:32 -07001066 error >>= 2;
1067 if (likely(error <= interval))
1068 adj = 1;
1069 else
1070 adj = clocksource_bigadjust(error, &interval, &offset);
Roman Zippel19923c12006-06-26 00:25:18 -07001071 } else if (error < -interval) {
Roman Zippele154ff32006-07-10 04:44:32 -07001072 error >>= 2;
1073 if (likely(error >= -interval)) {
1074 adj = -1;
1075 interval = -interval;
1076 offset = -offset;
1077 } else
1078 adj = clocksource_bigadjust(error, &interval, &offset);
Roman Zippel19923c12006-06-26 00:25:18 -07001079 } else
1080 return;
1081
1082 clock->mult += adj;
1083 clock->xtime_interval += interval;
1084 clock->xtime_nsec -= offset;
Daniel Walkerf5f1a242006-12-10 02:21:33 -08001085 clock->error -= (interval - offset) <<
1086 (TICK_LENGTH_SHIFT - clock->shift);
Roman Zippel19923c12006-06-26 00:25:18 -07001087}
1088
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001089/**
john stultzad596172006-06-26 00:25:06 -07001090 * update_wall_time - Uses the current clocksource to increment the wall time
1091 *
1092 * Called from the timer interrupt, must hold a write on xtime_lock.
1093 */
1094static void update_wall_time(void)
1095{
Roman Zippel19923c12006-06-26 00:25:18 -07001096 cycle_t offset;
john stultzad596172006-06-26 00:25:06 -07001097
john stultz3e143472006-07-14 00:24:17 -07001098 /* Make sure we're fully resumed: */
1099 if (unlikely(timekeeping_suspended))
1100 return;
john stultz5eb6d202006-06-26 00:25:07 -07001101
Roman Zippel19923c12006-06-26 00:25:18 -07001102#ifdef CONFIG_GENERIC_TIME
1103 offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
1104#else
1105 offset = clock->cycle_interval;
1106#endif
john stultz3e143472006-07-14 00:24:17 -07001107 clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift;
john stultzad596172006-06-26 00:25:06 -07001108
1109 /* normally this loop will run just once, however in the
1110 * case of lost or late ticks, it will accumulate correctly.
1111 */
Roman Zippel19923c12006-06-26 00:25:18 -07001112 while (offset >= clock->cycle_interval) {
john stultzad596172006-06-26 00:25:06 -07001113 /* accumulate one interval */
Roman Zippel19923c12006-06-26 00:25:18 -07001114 clock->xtime_nsec += clock->xtime_interval;
1115 clock->cycle_last += clock->cycle_interval;
1116 offset -= clock->cycle_interval;
1117
1118 if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
1119 clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
1120 xtime.tv_sec++;
1121 second_overflow();
1122 }
john stultzad596172006-06-26 00:25:06 -07001123
john stultz5eb6d202006-06-26 00:25:07 -07001124 /* interpolator bits */
Roman Zippel19923c12006-06-26 00:25:18 -07001125 time_interpolator_update(clock->xtime_interval
john stultz5eb6d202006-06-26 00:25:07 -07001126 >> clock->shift);
john stultz5eb6d202006-06-26 00:25:07 -07001127
1128 /* accumulate error between NTP and clock interval */
Roman Zippel19923c12006-06-26 00:25:18 -07001129 clock->error += current_tick_length();
1130 clock->error -= clock->xtime_interval << (TICK_LENGTH_SHIFT - clock->shift);
john stultzad596172006-06-26 00:25:06 -07001131 }
Roman Zippel19923c12006-06-26 00:25:18 -07001132
1133 /* correct the clock when NTP error is too big */
1134 clocksource_adjust(clock, offset);
1135
john stultz5eb6d202006-06-26 00:25:07 -07001136 /* store full nanoseconds into xtime */
Roman Zippele154ff32006-07-10 04:44:32 -07001137 xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift;
Roman Zippel19923c12006-06-26 00:25:18 -07001138 clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
john stultzcf3c7692006-06-26 00:25:08 -07001139
1140 /* check to see if there is a new clocksource to use */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -08001141 change_clocksource();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
1144/*
1145 * Called from the timer interrupt handler to charge one tick to the current
1146 * process. user_tick is 1 if the tick is user time, 0 for system.
1147 */
1148void update_process_times(int user_tick)
1149{
1150 struct task_struct *p = current;
1151 int cpu = smp_processor_id();
1152
1153 /* Note: this timer irq context must be accounted for as well. */
1154 if (user_tick)
1155 account_user_time(p, jiffies_to_cputime(1));
1156 else
1157 account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
1158 run_local_timers();
1159 if (rcu_pending(cpu))
1160 rcu_check_callbacks(cpu, user_tick);
1161 scheduler_tick();
1162 run_posix_cpu_timers(p);
1163}
1164
1165/*
1166 * Nr of active tasks - counted in fixed-point numbers
1167 */
1168static unsigned long count_active_tasks(void)
1169{
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08001170 return nr_active() * FIXED_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
1173/*
1174 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
1175 * imply that avenrun[] is the standard name for this kind of thing.
1176 * Nothing else seems to be standardized: the fractional size etc
1177 * all seem to differ on different machines.
1178 *
1179 * Requires xtime_lock to access.
1180 */
1181unsigned long avenrun[3];
1182
1183EXPORT_SYMBOL(avenrun);
1184
1185/*
1186 * calc_load - given tick count, update the avenrun load estimates.
1187 * This is called while holding a write_lock on xtime_lock.
1188 */
1189static inline void calc_load(unsigned long ticks)
1190{
1191 unsigned long active_tasks; /* fixed-point */
1192 static int count = LOAD_FREQ;
1193
Eric Dumazetcd7175e2006-12-13 00:35:45 -08001194 count -= ticks;
1195 if (unlikely(count < 0)) {
1196 active_tasks = count_active_tasks();
1197 do {
1198 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
1199 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
1200 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
1201 count += LOAD_FREQ;
1202 } while (count < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204}
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206/*
1207 * This read-write spinlock protects us from races in SMP while
1208 * playing with xtime and avenrun.
1209 */
Eric Dumazet5809f9d2007-02-13 13:26:21 +01001210__attribute__((weak)) __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212EXPORT_SYMBOL(xtime_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214/*
1215 * This function runs timers and the timer-tq in bottom half context.
1216 */
1217static void run_timer_softirq(struct softirq_action *h)
1218{
Jan Beulicha4a61982006-03-24 03:15:54 -08001219 tvec_base_t *base = __get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001221 hrtimer_run_queues();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (time_after_eq(jiffies, base->timer_jiffies))
1223 __run_timers(base);
1224}
1225
1226/*
1227 * Called by the local, per-CPU timer interrupt on SMP.
1228 */
1229void run_local_timers(void)
1230{
1231 raise_softirq(TIMER_SOFTIRQ);
Ingo Molnar6687a972006-03-24 03:18:41 -08001232 softlockup_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
1235/*
1236 * Called by the timer interrupt. xtime_lock must already be taken
1237 * by the timer IRQ!
1238 */
Atsushi Nemoto3171a032006-09-29 02:00:32 -07001239static inline void update_times(unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
john stultzad596172006-06-26 00:25:06 -07001241 update_wall_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 calc_load(ticks);
1243}
1244
1245/*
1246 * The 64-bit jiffies value is not atomic - you MUST NOT read it
1247 * without sampling the sequence number in xtime_lock.
1248 * jiffies is defined in the linker script...
1249 */
1250
Atsushi Nemoto3171a032006-09-29 02:00:32 -07001251void do_timer(unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Atsushi Nemoto3171a032006-09-29 02:00:32 -07001253 jiffies_64 += ticks;
1254 update_times(ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257#ifdef __ARCH_WANT_SYS_ALARM
1258
1259/*
1260 * For backwards compatibility? This can be done in libc so Alpha
1261 * and all newer ports shouldn't need it.
1262 */
1263asmlinkage unsigned long sys_alarm(unsigned int seconds)
1264{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -08001265 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
1268#endif
1269
1270#ifndef __alpha__
1271
1272/*
1273 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
1274 * should be moved into arch/i386 instead?
1275 */
1276
1277/**
1278 * sys_getpid - return the thread group id of the current process
1279 *
1280 * Note, despite the name, this returns the tgid not the pid. The tgid and
1281 * the pid are identical unless CLONE_THREAD was specified on clone() in
1282 * which case the tgid is the same in all threads of the same group.
1283 *
1284 * This is SMP safe as current->tgid does not change.
1285 */
1286asmlinkage long sys_getpid(void)
1287{
1288 return current->tgid;
1289}
1290
1291/*
Kirill Korotaev6997a6f2006-08-13 23:24:23 -07001292 * Accessing ->real_parent is not SMP-safe, it could
1293 * change from under us. However, we can use a stale
1294 * value of ->real_parent under rcu_read_lock(), see
1295 * release_task()->call_rcu(delayed_put_task_struct).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
1297asmlinkage long sys_getppid(void)
1298{
1299 int pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Kirill Korotaev6997a6f2006-08-13 23:24:23 -07001301 rcu_read_lock();
1302 pid = rcu_dereference(current->real_parent)->tgid;
1303 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return pid;
1306}
1307
1308asmlinkage long sys_getuid(void)
1309{
1310 /* Only we change this so SMP safe */
1311 return current->uid;
1312}
1313
1314asmlinkage long sys_geteuid(void)
1315{
1316 /* Only we change this so SMP safe */
1317 return current->euid;
1318}
1319
1320asmlinkage long sys_getgid(void)
1321{
1322 /* Only we change this so SMP safe */
1323 return current->gid;
1324}
1325
1326asmlinkage long sys_getegid(void)
1327{
1328 /* Only we change this so SMP safe */
1329 return current->egid;
1330}
1331
1332#endif
1333
1334static void process_timeout(unsigned long __data)
1335{
Ingo Molnar36c8b582006-07-03 00:25:41 -07001336 wake_up_process((struct task_struct *)__data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
1339/**
1340 * schedule_timeout - sleep until timeout
1341 * @timeout: timeout value in jiffies
1342 *
1343 * Make the current task sleep until @timeout jiffies have
1344 * elapsed. The routine will return immediately unless
1345 * the current task state has been set (see set_current_state()).
1346 *
1347 * You can set the task state as follows -
1348 *
1349 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1350 * pass before the routine returns. The routine will return 0
1351 *
1352 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1353 * delivered to the current task. In this case the remaining time
1354 * in jiffies will be returned, or 0 if the timer expired in time
1355 *
1356 * The current task state is guaranteed to be TASK_RUNNING when this
1357 * routine returns.
1358 *
1359 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1360 * the CPU away without a bound on the timeout. In this case the return
1361 * value will be %MAX_SCHEDULE_TIMEOUT.
1362 *
1363 * In all cases the return value is guaranteed to be non-negative.
1364 */
1365fastcall signed long __sched schedule_timeout(signed long timeout)
1366{
1367 struct timer_list timer;
1368 unsigned long expire;
1369
1370 switch (timeout)
1371 {
1372 case MAX_SCHEDULE_TIMEOUT:
1373 /*
1374 * These two special cases are useful to be comfortable
1375 * in the caller. Nothing more. We could take
1376 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1377 * but I' d like to return a valid offset (>=0) to allow
1378 * the caller to do everything it want with the retval.
1379 */
1380 schedule();
1381 goto out;
1382 default:
1383 /*
1384 * Another bit of PARANOID. Note that the retval will be
1385 * 0 since no piece of kernel is supposed to do a check
1386 * for a negative retval of schedule_timeout() (since it
1387 * should never happens anyway). You just have the printk()
1388 * that will tell you if something is gone wrong and where.
1389 */
Andrew Morton5b149bc2006-12-22 01:10:14 -08001390 if (timeout < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 printk(KERN_ERR "schedule_timeout: wrong timeout "
Andrew Morton5b149bc2006-12-22 01:10:14 -08001392 "value %lx\n", timeout);
1393 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 current->state = TASK_RUNNING;
1395 goto out;
1396 }
1397 }
1398
1399 expire = timeout + jiffies;
1400
Oleg Nesterova8db2db2005-10-30 15:01:38 -08001401 setup_timer(&timer, process_timeout, (unsigned long)current);
1402 __mod_timer(&timer, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 schedule();
1404 del_singleshot_timer_sync(&timer);
1405
1406 timeout = expire - jiffies;
1407
1408 out:
1409 return timeout < 0 ? 0 : timeout;
1410}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411EXPORT_SYMBOL(schedule_timeout);
1412
Andrew Morton8a1c1752005-09-13 01:25:15 -07001413/*
1414 * We can use __set_current_state() here because schedule_timeout() calls
1415 * schedule() unconditionally.
1416 */
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001417signed long __sched schedule_timeout_interruptible(signed long timeout)
1418{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001419 __set_current_state(TASK_INTERRUPTIBLE);
1420 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001421}
1422EXPORT_SYMBOL(schedule_timeout_interruptible);
1423
1424signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1425{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001426 __set_current_state(TASK_UNINTERRUPTIBLE);
1427 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001428}
1429EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431/* Thread ID - the internal kernel "pid" */
1432asmlinkage long sys_gettid(void)
1433{
1434 return current->pid;
1435}
1436
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001437/**
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001438 * do_sysinfo - fill in sysinfo struct
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001439 * @info: pointer to buffer to fill
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 */
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001441int do_sysinfo(struct sysinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 unsigned long mem_total, sav_total;
1444 unsigned int mem_unit, bitcount;
1445 unsigned long seq;
1446
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001447 memset(info, 0, sizeof(struct sysinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 do {
1450 struct timespec tp;
1451 seq = read_seqbegin(&xtime_lock);
1452
1453 /*
1454 * This is annoying. The below is the same thing
1455 * posix_get_clock_monotonic() does, but it wants to
1456 * take the lock which we want to cover the loads stuff
1457 * too.
1458 */
1459
1460 getnstimeofday(&tp);
1461 tp.tv_sec += wall_to_monotonic.tv_sec;
1462 tp.tv_nsec += wall_to_monotonic.tv_nsec;
1463 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1464 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1465 tp.tv_sec++;
1466 }
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001467 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001469 info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1470 info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1471 info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001473 info->procs = nr_threads;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 } while (read_seqretry(&xtime_lock, seq));
1475
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001476 si_meminfo(info);
1477 si_swapinfo(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 /*
1480 * If the sum of all the available memory (i.e. ram + swap)
1481 * is less than can be stored in a 32 bit unsigned long then
1482 * we can be binary compatible with 2.2.x kernels. If not,
1483 * well, in that case 2.2.x was broken anyways...
1484 *
1485 * -Erik Andersen <andersee@debian.org>
1486 */
1487
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001488 mem_total = info->totalram + info->totalswap;
1489 if (mem_total < info->totalram || mem_total < info->totalswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 goto out;
1491 bitcount = 0;
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001492 mem_unit = info->mem_unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 while (mem_unit > 1) {
1494 bitcount++;
1495 mem_unit >>= 1;
1496 sav_total = mem_total;
1497 mem_total <<= 1;
1498 if (mem_total < sav_total)
1499 goto out;
1500 }
1501
1502 /*
1503 * If mem_total did not overflow, multiply all memory values by
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001504 * info->mem_unit and set it to 1. This leaves things compatible
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1506 * kernels...
1507 */
1508
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001509 info->mem_unit = 1;
1510 info->totalram <<= bitcount;
1511 info->freeram <<= bitcount;
1512 info->sharedram <<= bitcount;
1513 info->bufferram <<= bitcount;
1514 info->totalswap <<= bitcount;
1515 info->freeswap <<= bitcount;
1516 info->totalhigh <<= bitcount;
1517 info->freehigh <<= bitcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001519out:
1520 return 0;
1521}
1522
1523asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1524{
1525 struct sysinfo val;
1526
1527 do_sysinfo(&val);
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1530 return -EFAULT;
1531
1532 return 0;
1533}
1534
Ingo Molnard730e882006-07-03 00:25:10 -07001535/*
1536 * lockdep: we want to track each per-CPU base as a separate lock-class,
1537 * but timer-bases are kmalloc()-ed, so we need to attach separate
1538 * keys to them:
1539 */
1540static struct lock_class_key base_lock_keys[NR_CPUS];
1541
Jan Beulicha4a61982006-03-24 03:15:54 -08001542static int __devinit init_timers_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
1544 int j;
1545 tvec_base_t *base;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001546 static char __devinitdata tvec_base_done[NR_CPUS];
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001547
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001548 if (!tvec_base_done[cpu]) {
Jan Beulicha4a61982006-03-24 03:15:54 -08001549 static char boot_done;
1550
Jan Beulicha4a61982006-03-24 03:15:54 -08001551 if (boot_done) {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001552 /*
1553 * The APs use this path later in boot
1554 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001555 base = kmalloc_node(sizeof(*base), GFP_KERNEL,
1556 cpu_to_node(cpu));
1557 if (!base)
1558 return -ENOMEM;
1559 memset(base, 0, sizeof(*base));
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001560 per_cpu(tvec_bases, cpu) = base;
Jan Beulicha4a61982006-03-24 03:15:54 -08001561 } else {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001562 /*
1563 * This is for the boot CPU - we use compile-time
1564 * static initialisation because per-cpu memory isn't
1565 * ready yet and because the memory allocators are not
1566 * initialised either.
1567 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001568 boot_done = 1;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001569 base = &boot_tvec_bases;
Jan Beulicha4a61982006-03-24 03:15:54 -08001570 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001571 tvec_base_done[cpu] = 1;
1572 } else {
1573 base = per_cpu(tvec_bases, cpu);
Jan Beulicha4a61982006-03-24 03:15:54 -08001574 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001575
Oleg Nesterov3691c512006-03-31 02:30:30 -08001576 spin_lock_init(&base->lock);
Ingo Molnard730e882006-07-03 00:25:10 -07001577 lockdep_set_class(&base->lock, base_lock_keys + cpu);
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 for (j = 0; j < TVN_SIZE; j++) {
1580 INIT_LIST_HEAD(base->tv5.vec + j);
1581 INIT_LIST_HEAD(base->tv4.vec + j);
1582 INIT_LIST_HEAD(base->tv3.vec + j);
1583 INIT_LIST_HEAD(base->tv2.vec + j);
1584 }
1585 for (j = 0; j < TVR_SIZE; j++)
1586 INIT_LIST_HEAD(base->tv1.vec + j);
1587
1588 base->timer_jiffies = jiffies;
Jan Beulicha4a61982006-03-24 03:15:54 -08001589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
1592#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001593static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
1595 struct timer_list *timer;
1596
1597 while (!list_empty(head)) {
1598 timer = list_entry(head->next, struct timer_list, entry);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001599 detach_timer(timer, 0);
Oleg Nesterov3691c512006-03-31 02:30:30 -08001600 timer->base = new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 internal_add_timer(new_base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
1605static void __devinit migrate_timers(int cpu)
1606{
1607 tvec_base_t *old_base;
1608 tvec_base_t *new_base;
1609 int i;
1610
1611 BUG_ON(cpu_online(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001612 old_base = per_cpu(tvec_bases, cpu);
1613 new_base = get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 local_irq_disable();
Oleg Nesterov3691c512006-03-31 02:30:30 -08001616 spin_lock(&new_base->lock);
1617 spin_lock(&old_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Oleg Nesterov3691c512006-03-31 02:30:30 -08001619 BUG_ON(old_base->running_timer);
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 for (i = 0; i < TVR_SIZE; i++)
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001622 migrate_timer_list(new_base, old_base->tv1.vec + i);
1623 for (i = 0; i < TVN_SIZE; i++) {
1624 migrate_timer_list(new_base, old_base->tv2.vec + i);
1625 migrate_timer_list(new_base, old_base->tv3.vec + i);
1626 migrate_timer_list(new_base, old_base->tv4.vec + i);
1627 migrate_timer_list(new_base, old_base->tv5.vec + i);
1628 }
1629
Oleg Nesterov3691c512006-03-31 02:30:30 -08001630 spin_unlock(&old_base->lock);
1631 spin_unlock(&new_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 local_irq_enable();
1633 put_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635#endif /* CONFIG_HOTPLUG_CPU */
1636
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001637static int __cpuinit timer_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 unsigned long action, void *hcpu)
1639{
1640 long cpu = (long)hcpu;
1641 switch(action) {
1642 case CPU_UP_PREPARE:
Jan Beulicha4a61982006-03-24 03:15:54 -08001643 if (init_timers_cpu(cpu) < 0)
1644 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 break;
1646#ifdef CONFIG_HOTPLUG_CPU
1647 case CPU_DEAD:
1648 migrate_timers(cpu);
1649 break;
1650#endif
1651 default:
1652 break;
1653 }
1654 return NOTIFY_OK;
1655}
1656
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001657static struct notifier_block __cpuinitdata timers_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 .notifier_call = timer_cpu_notify,
1659};
1660
1661
1662void __init init_timers(void)
1663{
Akinobu Mita07dccf32006-09-29 02:00:22 -07001664 int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 (void *)(long)smp_processor_id());
Akinobu Mita07dccf32006-09-29 02:00:22 -07001666
1667 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 register_cpu_notifier(&timers_nb);
1669 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1670}
1671
1672#ifdef CONFIG_TIME_INTERPOLATION
1673
Christoph Lameter67890d72006-03-16 23:04:00 -08001674struct time_interpolator *time_interpolator __read_mostly;
1675static struct time_interpolator *time_interpolator_list __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676static DEFINE_SPINLOCK(time_interpolator_lock);
1677
Helge Deller3db5db42007-02-10 01:45:40 -08001678static inline cycles_t time_interpolator_get_cycles(unsigned int src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 unsigned long (*x)(void);
1681
1682 switch (src)
1683 {
1684 case TIME_SOURCE_FUNCTION:
1685 x = time_interpolator->addr;
1686 return x();
1687
1688 case TIME_SOURCE_MMIO64 :
Christoph Lameter685db652006-03-02 02:54:35 -08001689 return readq_relaxed((void __iomem *)time_interpolator->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 case TIME_SOURCE_MMIO32 :
Christoph Lameter685db652006-03-02 02:54:35 -08001692 return readl_relaxed((void __iomem *)time_interpolator->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 default: return get_cycles();
1695 }
1696}
1697
Alex Williamson486d46a2005-09-06 15:17:04 -07001698static inline u64 time_interpolator_get_counter(int writelock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 unsigned int src = time_interpolator->source;
1701
1702 if (time_interpolator->jitter)
1703 {
Helge Deller3db5db42007-02-10 01:45:40 -08001704 cycles_t lcycle;
1705 cycles_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 do {
1708 lcycle = time_interpolator->last_cycle;
1709 now = time_interpolator_get_cycles(src);
1710 if (lcycle && time_after(lcycle, now))
1711 return lcycle;
Alex Williamson486d46a2005-09-06 15:17:04 -07001712
1713 /* When holding the xtime write lock, there's no need
1714 * to add the overhead of the cmpxchg. Readers are
1715 * force to retry until the write lock is released.
1716 */
1717 if (writelock) {
1718 time_interpolator->last_cycle = now;
1719 return now;
1720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /* Keep track of the last timer value returned. The use of cmpxchg here
1722 * will cause contention in an SMP environment.
1723 */
1724 } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
1725 return now;
1726 }
1727 else
1728 return time_interpolator_get_cycles(src);
1729}
1730
1731void time_interpolator_reset(void)
1732{
1733 time_interpolator->offset = 0;
Alex Williamson486d46a2005-09-06 15:17:04 -07001734 time_interpolator->last_counter = time_interpolator_get_counter(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
1737#define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
1738
1739unsigned long time_interpolator_get_offset(void)
1740{
1741 /* If we do not have a time interpolator set up then just return zero */
1742 if (!time_interpolator)
1743 return 0;
1744
1745 return time_interpolator->offset +
Alex Williamson486d46a2005-09-06 15:17:04 -07001746 GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
1749#define INTERPOLATOR_ADJUST 65536
1750#define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
1751
john stultz4c7ee8d2006-09-30 23:28:22 -07001752void time_interpolator_update(long delta_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
1754 u64 counter;
1755 unsigned long offset;
1756
1757 /* If there is no time interpolator set up then do nothing */
1758 if (!time_interpolator)
1759 return;
1760
Andrew Mortona5a0d522005-10-30 15:01:42 -08001761 /*
1762 * The interpolator compensates for late ticks by accumulating the late
1763 * time in time_interpolator->offset. A tick earlier than expected will
1764 * lead to a reset of the offset and a corresponding jump of the clock
1765 * forward. Again this only works if the interpolator clock is running
1766 * slightly slower than the regular clock and the tuning logic insures
1767 * that.
1768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Alex Williamson486d46a2005-09-06 15:17:04 -07001770 counter = time_interpolator_get_counter(1);
Andrew Mortona5a0d522005-10-30 15:01:42 -08001771 offset = time_interpolator->offset +
1772 GET_TI_NSECS(counter, time_interpolator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
1775 time_interpolator->offset = offset - delta_nsec;
1776 else {
1777 time_interpolator->skips++;
1778 time_interpolator->ns_skipped += delta_nsec - offset;
1779 time_interpolator->offset = 0;
1780 }
1781 time_interpolator->last_counter = counter;
1782
1783 /* Tuning logic for time interpolator invoked every minute or so.
1784 * Decrease interpolator clock speed if no skips occurred and an offset is carried.
1785 * Increase interpolator clock speed if we skip too much time.
1786 */
1787 if (jiffies % INTERPOLATOR_ADJUST == 0)
1788 {
Jordan Hargraveb20367a2006-04-07 19:50:18 +02001789 if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 time_interpolator->nsec_per_cyc--;
1791 if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
1792 time_interpolator->nsec_per_cyc++;
1793 time_interpolator->skips = 0;
1794 time_interpolator->ns_skipped = 0;
1795 }
1796}
1797
1798static inline int
1799is_better_time_interpolator(struct time_interpolator *new)
1800{
1801 if (!time_interpolator)
1802 return 1;
1803 return new->frequency > 2*time_interpolator->frequency ||
1804 (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
1805}
1806
1807void
1808register_time_interpolator(struct time_interpolator *ti)
1809{
1810 unsigned long flags;
1811
1812 /* Sanity check */
Eric Sesterhenn9f312522006-04-02 13:45:55 +02001813 BUG_ON(ti->frequency == 0 || ti->mask == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
1816 spin_lock(&time_interpolator_lock);
1817 write_seqlock_irqsave(&xtime_lock, flags);
1818 if (is_better_time_interpolator(ti)) {
1819 time_interpolator = ti;
1820 time_interpolator_reset();
1821 }
1822 write_sequnlock_irqrestore(&xtime_lock, flags);
1823
1824 ti->next = time_interpolator_list;
1825 time_interpolator_list = ti;
1826 spin_unlock(&time_interpolator_lock);
1827}
1828
1829void
1830unregister_time_interpolator(struct time_interpolator *ti)
1831{
1832 struct time_interpolator *curr, **prev;
1833 unsigned long flags;
1834
1835 spin_lock(&time_interpolator_lock);
1836 prev = &time_interpolator_list;
1837 for (curr = *prev; curr; curr = curr->next) {
1838 if (curr == ti) {
1839 *prev = curr->next;
1840 break;
1841 }
1842 prev = &curr->next;
1843 }
1844
1845 write_seqlock_irqsave(&xtime_lock, flags);
1846 if (ti == time_interpolator) {
1847 /* we lost the best time-interpolator: */
1848 time_interpolator = NULL;
1849 /* find the next-best interpolator */
1850 for (curr = time_interpolator_list; curr; curr = curr->next)
1851 if (is_better_time_interpolator(curr))
1852 time_interpolator = curr;
1853 time_interpolator_reset();
1854 }
1855 write_sequnlock_irqrestore(&xtime_lock, flags);
1856 spin_unlock(&time_interpolator_lock);
1857}
1858#endif /* CONFIG_TIME_INTERPOLATION */
1859
1860/**
1861 * msleep - sleep safely even with waitqueue interruptions
1862 * @msecs: Time in milliseconds to sleep for
1863 */
1864void msleep(unsigned int msecs)
1865{
1866 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1867
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001868 while (timeout)
1869 timeout = schedule_timeout_uninterruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
1872EXPORT_SYMBOL(msleep);
1873
1874/**
Domen Puncer96ec3ef2005-06-25 14:58:43 -07001875 * msleep_interruptible - sleep waiting for signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 * @msecs: Time in milliseconds to sleep for
1877 */
1878unsigned long msleep_interruptible(unsigned int msecs)
1879{
1880 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1881
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001882 while (timeout && !signal_pending(current))
1883 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return jiffies_to_msecs(timeout);
1885}
1886
1887EXPORT_SYMBOL(msleep_interruptible);