blob: 1ab3106a2b5dba6c5f8de68a58b5e102f2eba720 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/timer.c
3 *
john stultz85240702007-05-08 00:27:59 -07004 * Kernel internal timers, basic process system calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 *
8 * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
9 *
10 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
11 * "A Kernel Model for Precision Timekeeping" by Dave Mills
12 * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
13 * serialize accesses to xtime/lost_ticks).
14 * Copyright (C) 1998 Andrea Arcangeli
15 * 1999-03-10 Improved NTP compatibility by Ulrich Windl
16 * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love
17 * 2000-10-05 Implemented scalable SMP per-CPU timer handling.
18 * Copyright (C) 2000, 2001, 2002 Ingo Molnar
19 * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
20 */
21
22#include <linux/kernel_stat.h>
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>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080037#include <linux/tick.h>
Ingo Molnar82f67cd2007-02-16 01:28:13 -080038#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
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
Thomas Gleixnerecea8d12005-10-30 15:03:00 -080046u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
47
48EXPORT_SYMBOL(jiffies_64);
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * per-CPU timer vector definitions:
52 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#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 {
Oleg Nesterov3691c512006-03-31 02:30:30 -080069 spinlock_t lock;
70 struct timer_list *running_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 unsigned long timer_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 tvec_root_t tv1;
73 tvec_t tv2;
74 tvec_t tv3;
75 tvec_t tv4;
76 tvec_t tv5;
Venki Pallipadi6e453a62007-05-08 00:27:44 -070077} ____cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79typedef struct tvec_t_base_s tvec_base_t;
Andrew Mortonba6edfc2006-04-10 22:53:58 -070080
Oleg Nesterov3691c512006-03-31 02:30:30 -080081tvec_base_t boot_tvec_bases;
82EXPORT_SYMBOL(boot_tvec_bases);
Josh Triplett51d8c5e2006-07-30 03:04:14 -070083static DEFINE_PER_CPU(tvec_base_t *, tvec_bases) = &boot_tvec_bases;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Venki Pallipadi6e453a62007-05-08 00:27:44 -070085/*
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{
akpm@linux-foundation.orge9910842007-05-10 03:16:01 -070095 return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);
Venki Pallipadi6e453a62007-05-08 00:27:44 -070096}
97
98static inline tvec_base_t *tbase_get_base(tvec_base_t *base)
99{
akpm@linux-foundation.orge9910842007-05-10 03:16:01 -0700100 return ((tvec_base_t *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700101}
102
103static inline void timer_set_deferrable(struct timer_list *timer)
104{
akpm@linux-foundation.orge9910842007-05-10 03:16:01 -0700105 timer->base = ((tvec_base_t *)((unsigned long)(timer->base) |
106 TBASE_DEFERRABLE_FLAG));
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700107}
108
109static inline void
110timer_set_base(struct timer_list *timer, tvec_base_t *new_base)
111{
akpm@linux-foundation.orge9910842007-05-10 03:16:01 -0700112 timer->base = (tvec_base_t *)((unsigned long)(new_base) |
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700113 tbase_get_deferrable(timer->base));
114}
115
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800116/**
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 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800121 * __round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800122 * 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 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800134 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800135 */
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 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800178 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800179 * up or down to (approximately) full seconds. This is useful for timers
180 * for which the exact time they fire does not matter too much, as long as
181 * they fire approximately every X seconds.
182 *
183 * By rounding these timers to whole seconds, all such timers will fire
184 * at the same time, rather than at various times spread out. The goal
185 * of this is to have the CPU wake up less, which saves power.
186 *
187 * The exact rounding is skewed for each processor to avoid all
188 * processors firing at the exact same time, which could lead
189 * to lock contention or spurious cache line bouncing.
190 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800191 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800192 */
193unsigned long __round_jiffies_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 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800209 * round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800210 * 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 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800218 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800219 */
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 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800230 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800231 * 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 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800239 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800240 */
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static inline void set_running_timer(tvec_base_t *base,
249 struct timer_list *timer)
250{
251#ifdef CONFIG_SMP
Oleg Nesterov3691c512006-03-31 02:30:30 -0800252 base->running_timer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253#endif
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static 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
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800298#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
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700310/**
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700311 * 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;
Paul Mackerrasbfe5d832006-06-25 05:47:14 -0700320 timer->base = __raw_get_cpu_var(tvec_bases);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800321#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
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700326}
327EXPORT_SYMBOL(init_timer);
328
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700329void 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
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700336static inline void detach_timer(struct timer_list *timer,
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800337 int clear_pending)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700338{
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/*
Oleg Nesterov3691c512006-03-31 02:30:30 -0800348 * We are using hashed locking: holding per_cpu(tvec_bases).lock
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700349 * 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 */
Oleg Nesterov3691c512006-03-31 02:30:30 -0800359static tvec_base_t *lock_timer_base(struct timer_list *timer,
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700360 unsigned long *flags)
Josh Triplett89e7e3742006-09-29 01:59:36 -0700361 __acquires(timer->base->lock)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700362{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800363 tvec_base_t *base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700364
365 for (;;) {
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700366 tvec_base_t *prelock_base = timer->base;
367 base = tbase_get_base(prelock_base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700368 if (likely(base != NULL)) {
369 spin_lock_irqsave(&base->lock, *flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700370 if (likely(prelock_base == timer->base))
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700371 return base;
372 /* The timer has migrated to another CPU */
373 spin_unlock_irqrestore(&base->lock, *flags);
374 }
375 cpu_relax();
376 }
377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379int __mod_timer(struct timer_list *timer, unsigned long expires)
380{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800381 tvec_base_t *base, *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 unsigned long flags;
383 int ret = 0;
384
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800385 timer_stats_timer_set_start_info(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 BUG_ON(!timer->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700388 base = lock_timer_base(timer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700390 if (timer_pending(timer)) {
391 detach_timer(timer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 ret = 1;
393 }
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700394
Jan Beulicha4a61982006-03-24 03:15:54 -0800395 new_base = __get_cpu_var(tvec_bases);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700396
Oleg Nesterov3691c512006-03-31 02:30:30 -0800397 if (base != new_base) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700398 /*
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.
404 */
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800405 if (likely(base->running_timer != timer)) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700406 /* See the comment in lock_timer_base() */
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700407 timer_set_base(timer, NULL);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700408 spin_unlock(&base->lock);
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800409 base = new_base;
410 spin_lock(&base->lock);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700411 timer_set_base(timer, base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700412 }
413 }
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 timer->expires = expires;
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800416 internal_add_timer(base, timer);
417 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 return ret;
420}
421
422EXPORT_SYMBOL(__mod_timer);
423
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700424/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * 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{
Jan Beulicha4a61982006-03-24 03:15:54 -0800433 tvec_base_t *base = per_cpu(tvec_bases, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700435
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800436 timer_stats_timer_set_start_info(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 BUG_ON(timer_pending(timer) || !timer->function);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800438 spin_lock_irqsave(&base->lock, flags);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700439 timer_set_base(timer, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 internal_add_timer(base, timer);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800441 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700445/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * mod_timer - modify a timer's timeout
447 * @timer: the timer to be modified
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700448 * @expires: new timeout in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800450 * mod_timer() is a more efficient way to update the expire field of an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * 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
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800469 timer_stats_timer_set_start_info(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /*
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
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700483/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * 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{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800496 tvec_base_t *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700498 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800500 timer_stats_timer_clear_start_info(timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700501 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700510 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513EXPORT_SYMBOL(del_timer);
514
515#ifdef CONFIG_SMP
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700516/**
517 * try_to_del_timer_sync - Try to deactivate a timer
518 * @timer: timer do del
519 *
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700520 * 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{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800527 tvec_base_t *base;
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700528 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
David Howellse19dff12007-04-26 15:46:56 -0700547EXPORT_SYMBOL(try_to_del_timer_sync);
548
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700549/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * 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 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800557 * Synchronization rules: Callers must prevent restarting of the timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * otherwise this function is meaningless. It must not be called from
559 * interrupt contexts. The caller must not hold locks which would prevent
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700560 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 *
564 * The function returns whether it has deactivated a pending timer or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
566int del_timer_sync(struct timer_list *timer)
567{
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700568 for (;;) {
569 int ret = try_to_del_timer_sync(timer);
570 if (ret >= 0)
571 return ret;
Andrew Mortona0009652006-07-14 00:24:06 -0700572 cpu_relax();
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576EXPORT_SYMBOL(del_timer_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#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 */
Porpoise3439dd82006-06-23 02:05:56 -0700582 struct timer_list *timer, *tmp;
583 struct list_head tv_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Porpoise3439dd82006-06-23 02:05:56 -0700585 list_replace_init(tv->vec + index, &tv_list);
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /*
Porpoise3439dd82006-06-23 02:05:56 -0700588 * We are removing _all_ timers from the list, so we
589 * don't have to detach them individually.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
Porpoise3439dd82006-06-23 02:05:56 -0700591 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700592 BUG_ON(tbase_get_base(timer->base) != base);
Porpoise3439dd82006-06-23 02:05:56 -0700593 internal_add_timer(base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 return index;
597}
598
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700599#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
600
601/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * __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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608static inline void __run_timers(tvec_base_t *base)
609{
610 struct timer_list *timer;
611
Oleg Nesterov3691c512006-03-31 02:30:30 -0800612 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 while (time_after_eq(jiffies, base->timer_jiffies)) {
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700614 struct list_head work_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 struct list_head *head = &work_list;
616 int index = base->timer_jiffies & TVR_MASK;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /*
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));
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700626 ++base->timer_jiffies;
627 list_replace_init(base->tv1.vec + index, &work_list);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700628 while (!list_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 void (*fn)(unsigned long);
630 unsigned long data;
631
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700632 timer = list_first_entry(head, struct timer_list,entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 fn = timer->function;
634 data = timer->data;
635
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800636 timer_stats_account_timer(timer);
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 set_running_timer(base, timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700639 detach_timer(timer, 1);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800640 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700642 int preempt_count = preempt_count();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 fn(data);
644 if (preempt_count != preempt_count()) {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700645 printk(KERN_WARNING "huh, entered %p "
646 "with preempt_count %08x, exited"
647 " with %08x?\n",
648 fn, preempt_count,
649 preempt_count());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 BUG();
651 }
652 }
Oleg Nesterov3691c512006-03-31 02:30:30 -0800653 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655 }
656 set_running_timer(base, NULL);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800657 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800660#if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661/*
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 */
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800666static unsigned long __next_timer_interrupt(tvec_base_t *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800668 unsigned long timer_jiffies = base->timer_jiffies;
Thomas Gleixnereaad0842007-05-29 23:47:39 +0200669 unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800670 int index, slot, array, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct timer_list *nte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 tvec_t *varray[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /* Look for timer events in tv1. */
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800675 index = slot = timer_jiffies & TVR_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800677 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700678 if (tbase_get_deferrable(nte->base))
679 continue;
680
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800681 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800683 /* Look at the cascade bucket(s)? */
684 if (!index || slot < index)
685 goto cascade;
686 return expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800688 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 /* Check tv2-tv5. */
698 varray[0] = &base->tv2;
699 varray[1] = &base->tv3;
700 varray[2] = &base->tv4;
701 varray[3] = &base->tv5;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800702
703 for (array = 0; array < 4; array++) {
704 tvec_t *varp = varray[array];
705
706 index = slot = timer_jiffies & TVN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800708 list_for_each_entry(nte, varp->vec + slot, entry) {
709 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (time_before(nte->expires, expires))
711 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800712 }
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800730 return expires;
731}
732
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;
Thomas Gleixner9501b6c2007-03-25 14:31:17 +0200742 unsigned long delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800743
744 if (hr_delta.tv64 == KTIME_MAX)
745 return expires;
746
Thomas Gleixner9501b6c2007-03-25 14:31:17 +0200747 /*
748 * Expired timer available, let it expire in the next tick
749 */
750 if (hr_delta.tv64 <= 0)
751 return now + 1;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800752
753 tsdelta = ktime_to_timespec(hr_delta);
Thomas Gleixner9501b6c2007-03-25 14:31:17 +0200754 delta = timespec_to_jiffies(&tsdelta);
Thomas Gleixnereaad0842007-05-29 23:47:39 +0200755
756 /*
757 * Limit the delta to the max value, which is checked in
758 * tick_nohz_stop_sched_tick():
759 */
760 if (delta > NEXT_TIMER_MAX_DELTA)
761 delta = NEXT_TIMER_MAX_DELTA;
762
Thomas Gleixner9501b6c2007-03-25 14:31:17 +0200763 /*
764 * Take rounding errors in to account and make sure, that it
765 * expires in the next tick. Otherwise we go into an endless
766 * ping pong due to tick_nohz_stop_sched_tick() retriggering
767 * the timer softirq
768 */
769 if (delta < 1)
770 delta = 1;
771 now += delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800772 if (time_before(now, expires))
773 return now;
774 return expires;
775}
776
777/**
778 * next_timer_interrupt - return the jiffy of the next pending timer
Randy Dunlap05fb6bf2007-02-28 20:12:13 -0800779 * @now: current time (in jiffies)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800780 */
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800781unsigned long get_next_timer_interrupt(unsigned long now)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800782{
783 tvec_base_t *base = __get_cpu_var(tvec_bases);
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800784 unsigned long expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800785
786 spin_lock(&base->lock);
787 expires = __next_timer_interrupt(base);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800788 spin_unlock(&base->lock);
Tony Lindgren69239742006-03-06 15:42:45 -0800789
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800790 if (time_before_eq(expires, now))
791 return now;
Zachary Amsden0662b712006-05-20 15:00:24 -0700792
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800793 return cmp_next_hrtimer_event(now, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800795
796#ifdef CONFIG_NO_IDLE_HZ
797unsigned long next_timer_interrupt(void)
798{
799 return get_next_timer_interrupt(jiffies);
800}
801#endif
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803#endif
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805/*
806 * Called from the timer interrupt handler to charge one tick to the current
807 * process. user_tick is 1 if the tick is user time, 0 for system.
808 */
809void update_process_times(int user_tick)
810{
811 struct task_struct *p = current;
812 int cpu = smp_processor_id();
813
814 /* Note: this timer irq context must be accounted for as well. */
815 if (user_tick)
816 account_user_time(p, jiffies_to_cputime(1));
817 else
818 account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
819 run_local_timers();
820 if (rcu_pending(cpu))
821 rcu_check_callbacks(cpu, user_tick);
822 scheduler_tick();
823 run_posix_cpu_timers(p);
824}
825
826/*
827 * Nr of active tasks - counted in fixed-point numbers
828 */
829static unsigned long count_active_tasks(void)
830{
Jack Steinerdb1b1fe2006-03-31 02:31:21 -0800831 return nr_active() * FIXED_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834/*
835 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
836 * imply that avenrun[] is the standard name for this kind of thing.
837 * Nothing else seems to be standardized: the fractional size etc
838 * all seem to differ on different machines.
839 *
840 * Requires xtime_lock to access.
841 */
842unsigned long avenrun[3];
843
844EXPORT_SYMBOL(avenrun);
845
846/*
847 * calc_load - given tick count, update the avenrun load estimates.
848 * This is called while holding a write_lock on xtime_lock.
849 */
850static inline void calc_load(unsigned long ticks)
851{
852 unsigned long active_tasks; /* fixed-point */
853 static int count = LOAD_FREQ;
854
Eric Dumazetcd7175e2006-12-13 00:35:45 -0800855 count -= ticks;
856 if (unlikely(count < 0)) {
857 active_tasks = count_active_tasks();
858 do {
859 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
860 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
861 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
862 count += LOAD_FREQ;
863 } while (count < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 * This function runs timers and the timer-tq in bottom half context.
869 */
870static void run_timer_softirq(struct softirq_action *h)
871{
Jan Beulicha4a61982006-03-24 03:15:54 -0800872 tvec_base_t *base = __get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800874 hrtimer_run_queues();
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (time_after_eq(jiffies, base->timer_jiffies))
877 __run_timers(base);
878}
879
880/*
881 * Called by the local, per-CPU timer interrupt on SMP.
882 */
883void run_local_timers(void)
884{
885 raise_softirq(TIMER_SOFTIRQ);
Ingo Molnar6687a972006-03-24 03:18:41 -0800886 softlockup_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889/*
890 * Called by the timer interrupt. xtime_lock must already be taken
891 * by the timer IRQ!
892 */
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700893static inline void update_times(unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
john stultzad596172006-06-26 00:25:06 -0700895 update_wall_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 calc_load(ticks);
897}
898
899/*
900 * The 64-bit jiffies value is not atomic - you MUST NOT read it
901 * without sampling the sequence number in xtime_lock.
902 * jiffies is defined in the linker script...
903 */
904
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700905void do_timer(unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700907 jiffies_64 += ticks;
908 update_times(ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911#ifdef __ARCH_WANT_SYS_ALARM
912
913/*
914 * For backwards compatibility? This can be done in libc so Alpha
915 * and all newer ports shouldn't need it.
916 */
917asmlinkage unsigned long sys_alarm(unsigned int seconds)
918{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -0800919 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
922#endif
923
924#ifndef __alpha__
925
926/*
927 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
928 * should be moved into arch/i386 instead?
929 */
930
931/**
932 * sys_getpid - return the thread group id of the current process
933 *
934 * Note, despite the name, this returns the tgid not the pid. The tgid and
935 * the pid are identical unless CLONE_THREAD was specified on clone() in
936 * which case the tgid is the same in all threads of the same group.
937 *
938 * This is SMP safe as current->tgid does not change.
939 */
940asmlinkage long sys_getpid(void)
941{
942 return current->tgid;
943}
944
945/*
Kirill Korotaev6997a6f2006-08-13 23:24:23 -0700946 * Accessing ->real_parent is not SMP-safe, it could
947 * change from under us. However, we can use a stale
948 * value of ->real_parent under rcu_read_lock(), see
949 * release_task()->call_rcu(delayed_put_task_struct).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
951asmlinkage long sys_getppid(void)
952{
953 int pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Kirill Korotaev6997a6f2006-08-13 23:24:23 -0700955 rcu_read_lock();
956 pid = rcu_dereference(current->real_parent)->tgid;
957 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return pid;
960}
961
962asmlinkage long sys_getuid(void)
963{
964 /* Only we change this so SMP safe */
965 return current->uid;
966}
967
968asmlinkage long sys_geteuid(void)
969{
970 /* Only we change this so SMP safe */
971 return current->euid;
972}
973
974asmlinkage long sys_getgid(void)
975{
976 /* Only we change this so SMP safe */
977 return current->gid;
978}
979
980asmlinkage long sys_getegid(void)
981{
982 /* Only we change this so SMP safe */
983 return current->egid;
984}
985
986#endif
987
988static void process_timeout(unsigned long __data)
989{
Ingo Molnar36c8b582006-07-03 00:25:41 -0700990 wake_up_process((struct task_struct *)__data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
993/**
994 * schedule_timeout - sleep until timeout
995 * @timeout: timeout value in jiffies
996 *
997 * Make the current task sleep until @timeout jiffies have
998 * elapsed. The routine will return immediately unless
999 * the current task state has been set (see set_current_state()).
1000 *
1001 * You can set the task state as follows -
1002 *
1003 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1004 * pass before the routine returns. The routine will return 0
1005 *
1006 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1007 * delivered to the current task. In this case the remaining time
1008 * in jiffies will be returned, or 0 if the timer expired in time
1009 *
1010 * The current task state is guaranteed to be TASK_RUNNING when this
1011 * routine returns.
1012 *
1013 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1014 * the CPU away without a bound on the timeout. In this case the return
1015 * value will be %MAX_SCHEDULE_TIMEOUT.
1016 *
1017 * In all cases the return value is guaranteed to be non-negative.
1018 */
1019fastcall signed long __sched schedule_timeout(signed long timeout)
1020{
1021 struct timer_list timer;
1022 unsigned long expire;
1023
1024 switch (timeout)
1025 {
1026 case MAX_SCHEDULE_TIMEOUT:
1027 /*
1028 * These two special cases are useful to be comfortable
1029 * in the caller. Nothing more. We could take
1030 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1031 * but I' d like to return a valid offset (>=0) to allow
1032 * the caller to do everything it want with the retval.
1033 */
1034 schedule();
1035 goto out;
1036 default:
1037 /*
1038 * Another bit of PARANOID. Note that the retval will be
1039 * 0 since no piece of kernel is supposed to do a check
1040 * for a negative retval of schedule_timeout() (since it
1041 * should never happens anyway). You just have the printk()
1042 * that will tell you if something is gone wrong and where.
1043 */
Andrew Morton5b149bc2006-12-22 01:10:14 -08001044 if (timeout < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 printk(KERN_ERR "schedule_timeout: wrong timeout "
Andrew Morton5b149bc2006-12-22 01:10:14 -08001046 "value %lx\n", timeout);
1047 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 current->state = TASK_RUNNING;
1049 goto out;
1050 }
1051 }
1052
1053 expire = timeout + jiffies;
1054
Oleg Nesterova8db2db2005-10-30 15:01:38 -08001055 setup_timer(&timer, process_timeout, (unsigned long)current);
1056 __mod_timer(&timer, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 schedule();
1058 del_singleshot_timer_sync(&timer);
1059
1060 timeout = expire - jiffies;
1061
1062 out:
1063 return timeout < 0 ? 0 : timeout;
1064}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065EXPORT_SYMBOL(schedule_timeout);
1066
Andrew Morton8a1c1752005-09-13 01:25:15 -07001067/*
1068 * We can use __set_current_state() here because schedule_timeout() calls
1069 * schedule() unconditionally.
1070 */
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001071signed long __sched schedule_timeout_interruptible(signed long timeout)
1072{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001073 __set_current_state(TASK_INTERRUPTIBLE);
1074 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001075}
1076EXPORT_SYMBOL(schedule_timeout_interruptible);
1077
1078signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1079{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001080 __set_current_state(TASK_UNINTERRUPTIBLE);
1081 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001082}
1083EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085/* Thread ID - the internal kernel "pid" */
1086asmlinkage long sys_gettid(void)
1087{
1088 return current->pid;
1089}
1090
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001091/**
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001092 * do_sysinfo - fill in sysinfo struct
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001093 * @info: pointer to buffer to fill
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 */
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001095int do_sysinfo(struct sysinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 unsigned long mem_total, sav_total;
1098 unsigned int mem_unit, bitcount;
1099 unsigned long seq;
1100
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001101 memset(info, 0, sizeof(struct sysinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 do {
1104 struct timespec tp;
1105 seq = read_seqbegin(&xtime_lock);
1106
1107 /*
1108 * This is annoying. The below is the same thing
1109 * posix_get_clock_monotonic() does, but it wants to
1110 * take the lock which we want to cover the loads stuff
1111 * too.
1112 */
1113
1114 getnstimeofday(&tp);
1115 tp.tv_sec += wall_to_monotonic.tv_sec;
1116 tp.tv_nsec += wall_to_monotonic.tv_nsec;
Tomas Janousekd6214142007-07-15 23:39:42 -07001117 monotonic_to_bootbased(&tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1119 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1120 tp.tv_sec++;
1121 }
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001122 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001124 info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1125 info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1126 info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001128 info->procs = nr_threads;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 } while (read_seqretry(&xtime_lock, seq));
1130
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001131 si_meminfo(info);
1132 si_swapinfo(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /*
1135 * If the sum of all the available memory (i.e. ram + swap)
1136 * is less than can be stored in a 32 bit unsigned long then
1137 * we can be binary compatible with 2.2.x kernels. If not,
1138 * well, in that case 2.2.x was broken anyways...
1139 *
1140 * -Erik Andersen <andersee@debian.org>
1141 */
1142
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001143 mem_total = info->totalram + info->totalswap;
1144 if (mem_total < info->totalram || mem_total < info->totalswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 goto out;
1146 bitcount = 0;
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001147 mem_unit = info->mem_unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 while (mem_unit > 1) {
1149 bitcount++;
1150 mem_unit >>= 1;
1151 sav_total = mem_total;
1152 mem_total <<= 1;
1153 if (mem_total < sav_total)
1154 goto out;
1155 }
1156
1157 /*
1158 * If mem_total did not overflow, multiply all memory values by
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001159 * info->mem_unit and set it to 1. This leaves things compatible
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1161 * kernels...
1162 */
1163
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001164 info->mem_unit = 1;
1165 info->totalram <<= bitcount;
1166 info->freeram <<= bitcount;
1167 info->sharedram <<= bitcount;
1168 info->bufferram <<= bitcount;
1169 info->totalswap <<= bitcount;
1170 info->freeswap <<= bitcount;
1171 info->totalhigh <<= bitcount;
1172 info->freehigh <<= bitcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001174out:
1175 return 0;
1176}
1177
1178asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1179{
1180 struct sysinfo val;
1181
1182 do_sysinfo(&val);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1185 return -EFAULT;
1186
1187 return 0;
1188}
1189
Ingo Molnard730e882006-07-03 00:25:10 -07001190/*
1191 * lockdep: we want to track each per-CPU base as a separate lock-class,
1192 * but timer-bases are kmalloc()-ed, so we need to attach separate
1193 * keys to them:
1194 */
1195static struct lock_class_key base_lock_keys[NR_CPUS];
1196
Jan Beulicha4a61982006-03-24 03:15:54 -08001197static int __devinit init_timers_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 int j;
1200 tvec_base_t *base;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001201 static char __devinitdata tvec_base_done[NR_CPUS];
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001202
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001203 if (!tvec_base_done[cpu]) {
Jan Beulicha4a61982006-03-24 03:15:54 -08001204 static char boot_done;
1205
Jan Beulicha4a61982006-03-24 03:15:54 -08001206 if (boot_done) {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001207 /*
1208 * The APs use this path later in boot
1209 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001210 base = kmalloc_node(sizeof(*base), GFP_KERNEL,
1211 cpu_to_node(cpu));
1212 if (!base)
1213 return -ENOMEM;
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001214
1215 /* Make sure that tvec_base is 2 byte aligned */
1216 if (tbase_get_deferrable(base)) {
1217 WARN_ON(1);
1218 kfree(base);
1219 return -ENOMEM;
1220 }
Jan Beulicha4a61982006-03-24 03:15:54 -08001221 memset(base, 0, sizeof(*base));
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001222 per_cpu(tvec_bases, cpu) = base;
Jan Beulicha4a61982006-03-24 03:15:54 -08001223 } else {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001224 /*
1225 * This is for the boot CPU - we use compile-time
1226 * static initialisation because per-cpu memory isn't
1227 * ready yet and because the memory allocators are not
1228 * initialised either.
1229 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001230 boot_done = 1;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001231 base = &boot_tvec_bases;
Jan Beulicha4a61982006-03-24 03:15:54 -08001232 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001233 tvec_base_done[cpu] = 1;
1234 } else {
1235 base = per_cpu(tvec_bases, cpu);
Jan Beulicha4a61982006-03-24 03:15:54 -08001236 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001237
Oleg Nesterov3691c512006-03-31 02:30:30 -08001238 spin_lock_init(&base->lock);
Ingo Molnard730e882006-07-03 00:25:10 -07001239 lockdep_set_class(&base->lock, base_lock_keys + cpu);
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 for (j = 0; j < TVN_SIZE; j++) {
1242 INIT_LIST_HEAD(base->tv5.vec + j);
1243 INIT_LIST_HEAD(base->tv4.vec + j);
1244 INIT_LIST_HEAD(base->tv3.vec + j);
1245 INIT_LIST_HEAD(base->tv2.vec + j);
1246 }
1247 for (j = 0; j < TVR_SIZE; j++)
1248 INIT_LIST_HEAD(base->tv1.vec + j);
1249
1250 base->timer_jiffies = jiffies;
Jan Beulicha4a61982006-03-24 03:15:54 -08001251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
1254#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001255static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 struct timer_list *timer;
1258
1259 while (!list_empty(head)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001260 timer = list_first_entry(head, struct timer_list, entry);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001261 detach_timer(timer, 0);
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001262 timer_set_base(timer, new_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 internal_add_timer(new_base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
1267static void __devinit migrate_timers(int cpu)
1268{
1269 tvec_base_t *old_base;
1270 tvec_base_t *new_base;
1271 int i;
1272
1273 BUG_ON(cpu_online(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001274 old_base = per_cpu(tvec_bases, cpu);
1275 new_base = get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 local_irq_disable();
Heiko Carstense81ce1f2007-03-05 00:30:51 -08001278 double_spin_lock(&new_base->lock, &old_base->lock,
1279 smp_processor_id() < cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Oleg Nesterov3691c512006-03-31 02:30:30 -08001281 BUG_ON(old_base->running_timer);
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 for (i = 0; i < TVR_SIZE; i++)
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001284 migrate_timer_list(new_base, old_base->tv1.vec + i);
1285 for (i = 0; i < TVN_SIZE; i++) {
1286 migrate_timer_list(new_base, old_base->tv2.vec + i);
1287 migrate_timer_list(new_base, old_base->tv3.vec + i);
1288 migrate_timer_list(new_base, old_base->tv4.vec + i);
1289 migrate_timer_list(new_base, old_base->tv5.vec + i);
1290 }
1291
Heiko Carstense81ce1f2007-03-05 00:30:51 -08001292 double_spin_unlock(&new_base->lock, &old_base->lock,
1293 smp_processor_id() < cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 local_irq_enable();
1295 put_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297#endif /* CONFIG_HOTPLUG_CPU */
1298
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001299static int __cpuinit timer_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 unsigned long action, void *hcpu)
1301{
1302 long cpu = (long)hcpu;
1303 switch(action) {
1304 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001305 case CPU_UP_PREPARE_FROZEN:
Jan Beulicha4a61982006-03-24 03:15:54 -08001306 if (init_timers_cpu(cpu) < 0)
1307 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 break;
1309#ifdef CONFIG_HOTPLUG_CPU
1310 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001311 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 migrate_timers(cpu);
1313 break;
1314#endif
1315 default:
1316 break;
1317 }
1318 return NOTIFY_OK;
1319}
1320
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001321static struct notifier_block __cpuinitdata timers_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 .notifier_call = timer_cpu_notify,
1323};
1324
1325
1326void __init init_timers(void)
1327{
Akinobu Mita07dccf32006-09-29 02:00:22 -07001328 int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 (void *)(long)smp_processor_id());
Akinobu Mita07dccf32006-09-29 02:00:22 -07001330
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001331 init_timer_stats();
1332
Akinobu Mita07dccf32006-09-29 02:00:22 -07001333 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 register_cpu_notifier(&timers_nb);
1335 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1336}
1337
1338#ifdef CONFIG_TIME_INTERPOLATION
1339
Christoph Lameter67890d72006-03-16 23:04:00 -08001340struct time_interpolator *time_interpolator __read_mostly;
1341static struct time_interpolator *time_interpolator_list __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342static DEFINE_SPINLOCK(time_interpolator_lock);
1343
Helge Deller3db5db42007-02-10 01:45:40 -08001344static inline cycles_t time_interpolator_get_cycles(unsigned int src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 unsigned long (*x)(void);
1347
1348 switch (src)
1349 {
1350 case TIME_SOURCE_FUNCTION:
1351 x = time_interpolator->addr;
1352 return x();
1353
1354 case TIME_SOURCE_MMIO64 :
Christoph Lameter685db652006-03-02 02:54:35 -08001355 return readq_relaxed((void __iomem *)time_interpolator->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 case TIME_SOURCE_MMIO32 :
Christoph Lameter685db652006-03-02 02:54:35 -08001358 return readl_relaxed((void __iomem *)time_interpolator->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 default: return get_cycles();
1361 }
1362}
1363
Alex Williamson486d46a2005-09-06 15:17:04 -07001364static inline u64 time_interpolator_get_counter(int writelock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 unsigned int src = time_interpolator->source;
1367
1368 if (time_interpolator->jitter)
1369 {
Helge Deller3db5db42007-02-10 01:45:40 -08001370 cycles_t lcycle;
1371 cycles_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 do {
1374 lcycle = time_interpolator->last_cycle;
1375 now = time_interpolator_get_cycles(src);
1376 if (lcycle && time_after(lcycle, now))
1377 return lcycle;
Alex Williamson486d46a2005-09-06 15:17:04 -07001378
1379 /* When holding the xtime write lock, there's no need
1380 * to add the overhead of the cmpxchg. Readers are
1381 * force to retry until the write lock is released.
1382 */
1383 if (writelock) {
1384 time_interpolator->last_cycle = now;
1385 return now;
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /* Keep track of the last timer value returned. The use of cmpxchg here
1388 * will cause contention in an SMP environment.
1389 */
1390 } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
1391 return now;
1392 }
1393 else
1394 return time_interpolator_get_cycles(src);
1395}
1396
1397void time_interpolator_reset(void)
1398{
1399 time_interpolator->offset = 0;
Alex Williamson486d46a2005-09-06 15:17:04 -07001400 time_interpolator->last_counter = time_interpolator_get_counter(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
1403#define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
1404
1405unsigned long time_interpolator_get_offset(void)
1406{
1407 /* If we do not have a time interpolator set up then just return zero */
1408 if (!time_interpolator)
1409 return 0;
1410
1411 return time_interpolator->offset +
Alex Williamson486d46a2005-09-06 15:17:04 -07001412 GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415#define INTERPOLATOR_ADJUST 65536
1416#define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
1417
john stultz4c7ee8d2006-09-30 23:28:22 -07001418void time_interpolator_update(long delta_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 u64 counter;
1421 unsigned long offset;
1422
1423 /* If there is no time interpolator set up then do nothing */
1424 if (!time_interpolator)
1425 return;
1426
Andrew Mortona5a0d522005-10-30 15:01:42 -08001427 /*
1428 * The interpolator compensates for late ticks by accumulating the late
1429 * time in time_interpolator->offset. A tick earlier than expected will
1430 * lead to a reset of the offset and a corresponding jump of the clock
1431 * forward. Again this only works if the interpolator clock is running
1432 * slightly slower than the regular clock and the tuning logic insures
1433 * that.
1434 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Alex Williamson486d46a2005-09-06 15:17:04 -07001436 counter = time_interpolator_get_counter(1);
Andrew Mortona5a0d522005-10-30 15:01:42 -08001437 offset = time_interpolator->offset +
1438 GET_TI_NSECS(counter, time_interpolator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
1441 time_interpolator->offset = offset - delta_nsec;
1442 else {
1443 time_interpolator->skips++;
1444 time_interpolator->ns_skipped += delta_nsec - offset;
1445 time_interpolator->offset = 0;
1446 }
1447 time_interpolator->last_counter = counter;
1448
1449 /* Tuning logic for time interpolator invoked every minute or so.
1450 * Decrease interpolator clock speed if no skips occurred and an offset is carried.
1451 * Increase interpolator clock speed if we skip too much time.
1452 */
1453 if (jiffies % INTERPOLATOR_ADJUST == 0)
1454 {
Jordan Hargraveb20367a2006-04-07 19:50:18 +02001455 if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 time_interpolator->nsec_per_cyc--;
1457 if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
1458 time_interpolator->nsec_per_cyc++;
1459 time_interpolator->skips = 0;
1460 time_interpolator->ns_skipped = 0;
1461 }
1462}
1463
1464static inline int
1465is_better_time_interpolator(struct time_interpolator *new)
1466{
1467 if (!time_interpolator)
1468 return 1;
1469 return new->frequency > 2*time_interpolator->frequency ||
1470 (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
1471}
1472
1473void
1474register_time_interpolator(struct time_interpolator *ti)
1475{
1476 unsigned long flags;
1477
1478 /* Sanity check */
Eric Sesterhenn9f312522006-04-02 13:45:55 +02001479 BUG_ON(ti->frequency == 0 || ti->mask == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
1482 spin_lock(&time_interpolator_lock);
1483 write_seqlock_irqsave(&xtime_lock, flags);
1484 if (is_better_time_interpolator(ti)) {
1485 time_interpolator = ti;
1486 time_interpolator_reset();
1487 }
1488 write_sequnlock_irqrestore(&xtime_lock, flags);
1489
1490 ti->next = time_interpolator_list;
1491 time_interpolator_list = ti;
1492 spin_unlock(&time_interpolator_lock);
1493}
1494
1495void
1496unregister_time_interpolator(struct time_interpolator *ti)
1497{
1498 struct time_interpolator *curr, **prev;
1499 unsigned long flags;
1500
1501 spin_lock(&time_interpolator_lock);
1502 prev = &time_interpolator_list;
1503 for (curr = *prev; curr; curr = curr->next) {
1504 if (curr == ti) {
1505 *prev = curr->next;
1506 break;
1507 }
1508 prev = &curr->next;
1509 }
1510
1511 write_seqlock_irqsave(&xtime_lock, flags);
1512 if (ti == time_interpolator) {
1513 /* we lost the best time-interpolator: */
1514 time_interpolator = NULL;
1515 /* find the next-best interpolator */
1516 for (curr = time_interpolator_list; curr; curr = curr->next)
1517 if (is_better_time_interpolator(curr))
1518 time_interpolator = curr;
1519 time_interpolator_reset();
1520 }
1521 write_sequnlock_irqrestore(&xtime_lock, flags);
1522 spin_unlock(&time_interpolator_lock);
1523}
1524#endif /* CONFIG_TIME_INTERPOLATION */
1525
1526/**
1527 * msleep - sleep safely even with waitqueue interruptions
1528 * @msecs: Time in milliseconds to sleep for
1529 */
1530void msleep(unsigned int msecs)
1531{
1532 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1533
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001534 while (timeout)
1535 timeout = schedule_timeout_uninterruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
1538EXPORT_SYMBOL(msleep);
1539
1540/**
Domen Puncer96ec3ef2005-06-25 14:58:43 -07001541 * msleep_interruptible - sleep waiting for signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 * @msecs: Time in milliseconds to sleep for
1543 */
1544unsigned long msleep_interruptible(unsigned int msecs)
1545{
1546 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1547
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001548 while (timeout && !signal_pending(current))
1549 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return jiffies_to_msecs(timeout);
1551}
1552
1553EXPORT_SYMBOL(msleep_interruptible);