blob: dd6c2c1c561bf1ecc1e3bf162b33e8028de0c194 [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>
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;
77} ____cacheline_aligned_in_smp;
78
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
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -080085/**
86 * __round_jiffies - function to round jiffies to a full second
87 * @j: the time in (absolute) jiffies that should be rounded
88 * @cpu: the processor number on which the timeout will happen
89 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080090 * __round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -080091 * up or down to (approximately) full seconds. This is useful for timers
92 * for which the exact time they fire does not matter too much, as long as
93 * they fire approximately every X seconds.
94 *
95 * By rounding these timers to whole seconds, all such timers will fire
96 * at the same time, rather than at various times spread out. The goal
97 * of this is to have the CPU wake up less, which saves power.
98 *
99 * The exact rounding is skewed for each processor to avoid all
100 * processors firing at the exact same time, which could lead
101 * to lock contention or spurious cache line bouncing.
102 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800103 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800104 */
105unsigned long __round_jiffies(unsigned long j, int cpu)
106{
107 int rem;
108 unsigned long original = j;
109
110 /*
111 * We don't want all cpus firing their timers at once hitting the
112 * same lock or cachelines, so we skew each extra cpu with an extra
113 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
114 * already did this.
115 * The skew is done by adding 3*cpunr, then round, then subtract this
116 * extra offset again.
117 */
118 j += cpu * 3;
119
120 rem = j % HZ;
121
122 /*
123 * If the target jiffie is just after a whole second (which can happen
124 * due to delays of the timer irq, long irq off times etc etc) then
125 * we should round down to the whole second, not up. Use 1/4th second
126 * as cutoff for this rounding as an extreme upper bound for this.
127 */
128 if (rem < HZ/4) /* round down */
129 j = j - rem;
130 else /* round up */
131 j = j - rem + HZ;
132
133 /* now that we have rounded, subtract the extra skew again */
134 j -= cpu * 3;
135
136 if (j <= jiffies) /* rounding ate our timeout entirely; */
137 return original;
138 return j;
139}
140EXPORT_SYMBOL_GPL(__round_jiffies);
141
142/**
143 * __round_jiffies_relative - function to round jiffies to a full second
144 * @j: the time in (relative) jiffies that should be rounded
145 * @cpu: the processor number on which the timeout will happen
146 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800147 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800148 * up or down to (approximately) full seconds. This is useful for timers
149 * for which the exact time they fire does not matter too much, as long as
150 * they fire approximately every X seconds.
151 *
152 * By rounding these timers to whole seconds, all such timers will fire
153 * at the same time, rather than at various times spread out. The goal
154 * of this is to have the CPU wake up less, which saves power.
155 *
156 * The exact rounding is skewed for each processor to avoid all
157 * processors firing at the exact same time, which could lead
158 * to lock contention or spurious cache line bouncing.
159 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800160 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800161 */
162unsigned long __round_jiffies_relative(unsigned long j, int cpu)
163{
164 /*
165 * In theory the following code can skip a jiffy in case jiffies
166 * increments right between the addition and the later subtraction.
167 * However since the entire point of this function is to use approximate
168 * timeouts, it's entirely ok to not handle that.
169 */
170 return __round_jiffies(j + jiffies, cpu) - jiffies;
171}
172EXPORT_SYMBOL_GPL(__round_jiffies_relative);
173
174/**
175 * round_jiffies - function to round jiffies to a full second
176 * @j: the time in (absolute) jiffies that should be rounded
177 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800178 * round_jiffies() rounds an absolute time 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 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800187 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800188 */
189unsigned long round_jiffies(unsigned long j)
190{
191 return __round_jiffies(j, raw_smp_processor_id());
192}
193EXPORT_SYMBOL_GPL(round_jiffies);
194
195/**
196 * round_jiffies_relative - function to round jiffies to a full second
197 * @j: the time in (relative) jiffies that should be rounded
198 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800199 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800200 * up or down to (approximately) full seconds. This is useful for timers
201 * for which the exact time they fire does not matter too much, as long as
202 * they fire approximately every X seconds.
203 *
204 * By rounding these timers to whole seconds, all such timers will fire
205 * at the same time, rather than at various times spread out. The goal
206 * of this is to have the CPU wake up less, which saves power.
207 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800208 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800209 */
210unsigned long round_jiffies_relative(unsigned long j)
211{
212 return __round_jiffies_relative(j, raw_smp_processor_id());
213}
214EXPORT_SYMBOL_GPL(round_jiffies_relative);
215
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static inline void set_running_timer(tvec_base_t *base,
218 struct timer_list *timer)
219{
220#ifdef CONFIG_SMP
Oleg Nesterov3691c512006-03-31 02:30:30 -0800221 base->running_timer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#endif
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
226{
227 unsigned long expires = timer->expires;
228 unsigned long idx = expires - base->timer_jiffies;
229 struct list_head *vec;
230
231 if (idx < TVR_SIZE) {
232 int i = expires & TVR_MASK;
233 vec = base->tv1.vec + i;
234 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
235 int i = (expires >> TVR_BITS) & TVN_MASK;
236 vec = base->tv2.vec + i;
237 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
238 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
239 vec = base->tv3.vec + i;
240 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
241 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
242 vec = base->tv4.vec + i;
243 } else if ((signed long) idx < 0) {
244 /*
245 * Can happen if you add a timer with expires == jiffies,
246 * or you set a timer to go off in the past
247 */
248 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
249 } else {
250 int i;
251 /* If the timeout is larger than 0xffffffff on 64-bit
252 * architectures then we use the maximum timeout:
253 */
254 if (idx > 0xffffffffUL) {
255 idx = 0xffffffffUL;
256 expires = idx + base->timer_jiffies;
257 }
258 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
259 vec = base->tv5.vec + i;
260 }
261 /*
262 * Timers are FIFO:
263 */
264 list_add_tail(&timer->entry, vec);
265}
266
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800267#ifdef CONFIG_TIMER_STATS
268void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
269{
270 if (timer->start_site)
271 return;
272
273 timer->start_site = addr;
274 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
275 timer->start_pid = current->pid;
276}
277#endif
278
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700279/**
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700280 * init_timer - initialize a timer.
281 * @timer: the timer to be initialized
282 *
283 * init_timer() must be done to a timer prior calling *any* of the
284 * other timer functions.
285 */
286void fastcall init_timer(struct timer_list *timer)
287{
288 timer->entry.next = NULL;
Paul Mackerrasbfe5d832006-06-25 05:47:14 -0700289 timer->base = __raw_get_cpu_var(tvec_bases);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800290#ifdef CONFIG_TIMER_STATS
291 timer->start_site = NULL;
292 timer->start_pid = -1;
293 memset(timer->start_comm, 0, TASK_COMM_LEN);
294#endif
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700295}
296EXPORT_SYMBOL(init_timer);
297
298static inline void detach_timer(struct timer_list *timer,
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800299 int clear_pending)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700300{
301 struct list_head *entry = &timer->entry;
302
303 __list_del(entry->prev, entry->next);
304 if (clear_pending)
305 entry->next = NULL;
306 entry->prev = LIST_POISON2;
307}
308
309/*
Oleg Nesterov3691c512006-03-31 02:30:30 -0800310 * We are using hashed locking: holding per_cpu(tvec_bases).lock
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700311 * means that all timers which are tied to this base via timer->base are
312 * locked, and the base itself is locked too.
313 *
314 * So __run_timers/migrate_timers can safely modify all timers which could
315 * be found on ->tvX lists.
316 *
317 * When the timer's base is locked, and the timer removed from list, it is
318 * possible to set timer->base = NULL and drop the lock: the timer remains
319 * locked.
320 */
Oleg Nesterov3691c512006-03-31 02:30:30 -0800321static tvec_base_t *lock_timer_base(struct timer_list *timer,
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700322 unsigned long *flags)
Josh Triplett89e7e3742006-09-29 01:59:36 -0700323 __acquires(timer->base->lock)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700324{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800325 tvec_base_t *base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700326
327 for (;;) {
328 base = timer->base;
329 if (likely(base != NULL)) {
330 spin_lock_irqsave(&base->lock, *flags);
331 if (likely(base == timer->base))
332 return base;
333 /* The timer has migrated to another CPU */
334 spin_unlock_irqrestore(&base->lock, *flags);
335 }
336 cpu_relax();
337 }
338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340int __mod_timer(struct timer_list *timer, unsigned long expires)
341{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800342 tvec_base_t *base, *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 unsigned long flags;
344 int ret = 0;
345
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800346 timer_stats_timer_set_start_info(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 BUG_ON(!timer->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700349 base = lock_timer_base(timer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700351 if (timer_pending(timer)) {
352 detach_timer(timer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 ret = 1;
354 }
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700355
Jan Beulicha4a61982006-03-24 03:15:54 -0800356 new_base = __get_cpu_var(tvec_bases);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700357
Oleg Nesterov3691c512006-03-31 02:30:30 -0800358 if (base != new_base) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700359 /*
360 * We are trying to schedule the timer on the local CPU.
361 * However we can't change timer's base while it is running,
362 * otherwise del_timer_sync() can't detect that the timer's
363 * handler yet has not finished. This also guarantees that
364 * the timer is serialized wrt itself.
365 */
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800366 if (likely(base->running_timer != timer)) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700367 /* See the comment in lock_timer_base() */
368 timer->base = NULL;
369 spin_unlock(&base->lock);
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800370 base = new_base;
371 spin_lock(&base->lock);
372 timer->base = base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700373 }
374 }
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 timer->expires = expires;
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800377 internal_add_timer(base, timer);
378 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 return ret;
381}
382
383EXPORT_SYMBOL(__mod_timer);
384
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700385/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * add_timer_on - start a timer on a particular CPU
387 * @timer: the timer to be added
388 * @cpu: the CPU to start it on
389 *
390 * This is not very scalable on SMP. Double adds are not possible.
391 */
392void add_timer_on(struct timer_list *timer, int cpu)
393{
Jan Beulicha4a61982006-03-24 03:15:54 -0800394 tvec_base_t *base = per_cpu(tvec_bases, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700396
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800397 timer_stats_timer_set_start_info(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 BUG_ON(timer_pending(timer) || !timer->function);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800399 spin_lock_irqsave(&base->lock, flags);
400 timer->base = base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 internal_add_timer(base, timer);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800402 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700406/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * mod_timer - modify a timer's timeout
408 * @timer: the timer to be modified
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700409 * @expires: new timeout in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800411 * mod_timer() is a more efficient way to update the expire field of an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * active timer (if the timer is inactive it will be activated)
413 *
414 * mod_timer(timer, expires) is equivalent to:
415 *
416 * del_timer(timer); timer->expires = expires; add_timer(timer);
417 *
418 * Note that if there are multiple unserialized concurrent users of the
419 * same timer, then mod_timer() is the only safe way to modify the timeout,
420 * since add_timer() cannot modify an already running timer.
421 *
422 * The function returns whether it has modified a pending timer or not.
423 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
424 * active timer returns 1.)
425 */
426int mod_timer(struct timer_list *timer, unsigned long expires)
427{
428 BUG_ON(!timer->function);
429
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800430 timer_stats_timer_set_start_info(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /*
432 * This is a common optimization triggered by the
433 * networking code - if the timer is re-modified
434 * to be the same thing then just return:
435 */
436 if (timer->expires == expires && timer_pending(timer))
437 return 1;
438
439 return __mod_timer(timer, expires);
440}
441
442EXPORT_SYMBOL(mod_timer);
443
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700444/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * del_timer - deactive a timer.
446 * @timer: the timer to be deactivated
447 *
448 * del_timer() deactivates a timer - this works on both active and inactive
449 * timers.
450 *
451 * The function returns whether it has deactivated a pending timer or not.
452 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
453 * active timer returns 1.)
454 */
455int del_timer(struct timer_list *timer)
456{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800457 tvec_base_t *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700459 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800461 timer_stats_timer_clear_start_info(timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700462 if (timer_pending(timer)) {
463 base = lock_timer_base(timer, &flags);
464 if (timer_pending(timer)) {
465 detach_timer(timer, 1);
466 ret = 1;
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700471 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474EXPORT_SYMBOL(del_timer);
475
476#ifdef CONFIG_SMP
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700477/**
478 * try_to_del_timer_sync - Try to deactivate a timer
479 * @timer: timer do del
480 *
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700481 * This function tries to deactivate a timer. Upon successful (ret >= 0)
482 * exit the timer is not queued and the handler is not running on any CPU.
483 *
484 * It must not be called from interrupt contexts.
485 */
486int try_to_del_timer_sync(struct timer_list *timer)
487{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800488 tvec_base_t *base;
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700489 unsigned long flags;
490 int ret = -1;
491
492 base = lock_timer_base(timer, &flags);
493
494 if (base->running_timer == timer)
495 goto out;
496
497 ret = 0;
498 if (timer_pending(timer)) {
499 detach_timer(timer, 1);
500 ret = 1;
501 }
502out:
503 spin_unlock_irqrestore(&base->lock, flags);
504
505 return ret;
506}
507
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700508/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 * del_timer_sync - deactivate a timer and wait for the handler to finish.
510 * @timer: the timer to be deactivated
511 *
512 * This function only differs from del_timer() on SMP: besides deactivating
513 * the timer it also makes sure the handler has finished executing on other
514 * CPUs.
515 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800516 * Synchronization rules: Callers must prevent restarting of the timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 * otherwise this function is meaningless. It must not be called from
518 * interrupt contexts. The caller must not hold locks which would prevent
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700519 * completion of the timer's handler. The timer's handler must not call
520 * add_timer_on(). Upon exit the timer is not queued and the handler is
521 * not running on any CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 *
523 * The function returns whether it has deactivated a pending timer or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 */
525int del_timer_sync(struct timer_list *timer)
526{
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700527 for (;;) {
528 int ret = try_to_del_timer_sync(timer);
529 if (ret >= 0)
530 return ret;
Andrew Mortona0009652006-07-14 00:24:06 -0700531 cpu_relax();
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535EXPORT_SYMBOL(del_timer_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536#endif
537
538static int cascade(tvec_base_t *base, tvec_t *tv, int index)
539{
540 /* cascade all the timers from tv up one level */
Porpoise3439dd82006-06-23 02:05:56 -0700541 struct timer_list *timer, *tmp;
542 struct list_head tv_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Porpoise3439dd82006-06-23 02:05:56 -0700544 list_replace_init(tv->vec + index, &tv_list);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /*
Porpoise3439dd82006-06-23 02:05:56 -0700547 * We are removing _all_ timers from the list, so we
548 * don't have to detach them individually.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
Porpoise3439dd82006-06-23 02:05:56 -0700550 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
551 BUG_ON(timer->base != base);
552 internal_add_timer(base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 return index;
556}
557
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700558#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
559
560/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * __run_timers - run all expired timers (if any) on this CPU.
562 * @base: the timer vector to be processed.
563 *
564 * This function cascades all vectors and executes all expired timer
565 * vectors.
566 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567static inline void __run_timers(tvec_base_t *base)
568{
569 struct timer_list *timer;
570
Oleg Nesterov3691c512006-03-31 02:30:30 -0800571 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 while (time_after_eq(jiffies, base->timer_jiffies)) {
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700573 struct list_head work_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 struct list_head *head = &work_list;
575 int index = base->timer_jiffies & TVR_MASK;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /*
578 * Cascade timers:
579 */
580 if (!index &&
581 (!cascade(base, &base->tv2, INDEX(0))) &&
582 (!cascade(base, &base->tv3, INDEX(1))) &&
583 !cascade(base, &base->tv4, INDEX(2)))
584 cascade(base, &base->tv5, INDEX(3));
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700585 ++base->timer_jiffies;
586 list_replace_init(base->tv1.vec + index, &work_list);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700587 while (!list_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 void (*fn)(unsigned long);
589 unsigned long data;
590
591 timer = list_entry(head->next,struct timer_list,entry);
592 fn = timer->function;
593 data = timer->data;
594
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800595 timer_stats_account_timer(timer);
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 set_running_timer(base, timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700598 detach_timer(timer, 1);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800599 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700601 int preempt_count = preempt_count();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 fn(data);
603 if (preempt_count != preempt_count()) {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700604 printk(KERN_WARNING "huh, entered %p "
605 "with preempt_count %08x, exited"
606 " with %08x?\n",
607 fn, preempt_count,
608 preempt_count());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 BUG();
610 }
611 }
Oleg Nesterov3691c512006-03-31 02:30:30 -0800612 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614 }
615 set_running_timer(base, NULL);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800616 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800619#if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/*
621 * Find out when the next timer event is due to happen. This
622 * is used on S/390 to stop all activity when a cpus is idle.
623 * This functions needs to be called disabled.
624 */
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800625static unsigned long __next_timer_interrupt(tvec_base_t *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800627 unsigned long timer_jiffies = base->timer_jiffies;
628 unsigned long expires = timer_jiffies + (LONG_MAX >> 1);
629 int index, slot, array, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct timer_list *nte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 tvec_t *varray[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 /* Look for timer events in tv1. */
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800634 index = slot = timer_jiffies & TVR_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800636 list_for_each_entry(nte, base->tv1.vec + slot, entry) {
637 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800639 /* Look at the cascade bucket(s)? */
640 if (!index || slot < index)
641 goto cascade;
642 return expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800644 slot = (slot + 1) & TVR_MASK;
645 } while (slot != index);
646
647cascade:
648 /* Calculate the next cascade event */
649 if (index)
650 timer_jiffies += TVR_SIZE - index;
651 timer_jiffies >>= TVR_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 /* Check tv2-tv5. */
654 varray[0] = &base->tv2;
655 varray[1] = &base->tv3;
656 varray[2] = &base->tv4;
657 varray[3] = &base->tv5;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800658
659 for (array = 0; array < 4; array++) {
660 tvec_t *varp = varray[array];
661
662 index = slot = timer_jiffies & TVN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 do {
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800664 list_for_each_entry(nte, varp->vec + slot, entry) {
665 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (time_before(nte->expires, expires))
667 expires = nte->expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800668 }
669 /*
670 * Do we still search for the first timer or are
671 * we looking up the cascade buckets ?
672 */
673 if (found) {
674 /* Look at the cascade bucket(s)? */
675 if (!index || slot < index)
676 break;
677 return expires;
678 }
679 slot = (slot + 1) & TVN_MASK;
680 } while (slot != index);
681
682 if (index)
683 timer_jiffies += TVN_SIZE - index;
684 timer_jiffies >>= TVN_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800686 return expires;
687}
688
689/*
690 * Check, if the next hrtimer event is before the next timer wheel
691 * event:
692 */
693static unsigned long cmp_next_hrtimer_event(unsigned long now,
694 unsigned long expires)
695{
696 ktime_t hr_delta = hrtimer_get_next_event();
697 struct timespec tsdelta;
Thomas Gleixner9501b6c2007-03-25 14:31:17 +0200698 unsigned long delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800699
700 if (hr_delta.tv64 == KTIME_MAX)
701 return expires;
702
Thomas Gleixner9501b6c2007-03-25 14:31:17 +0200703 /*
704 * Expired timer available, let it expire in the next tick
705 */
706 if (hr_delta.tv64 <= 0)
707 return now + 1;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800708
709 tsdelta = ktime_to_timespec(hr_delta);
Thomas Gleixner9501b6c2007-03-25 14:31:17 +0200710 delta = timespec_to_jiffies(&tsdelta);
711 /*
712 * Take rounding errors in to account and make sure, that it
713 * expires in the next tick. Otherwise we go into an endless
714 * ping pong due to tick_nohz_stop_sched_tick() retriggering
715 * the timer softirq
716 */
717 if (delta < 1)
718 delta = 1;
719 now += delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800720 if (time_before(now, expires))
721 return now;
722 return expires;
723}
724
725/**
726 * next_timer_interrupt - return the jiffy of the next pending timer
Randy Dunlap05fb6bf2007-02-28 20:12:13 -0800727 * @now: current time (in jiffies)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800728 */
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800729unsigned long get_next_timer_interrupt(unsigned long now)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800730{
731 tvec_base_t *base = __get_cpu_var(tvec_bases);
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800732 unsigned long expires;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800733
734 spin_lock(&base->lock);
735 expires = __next_timer_interrupt(base);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800736 spin_unlock(&base->lock);
Tony Lindgren69239742006-03-06 15:42:45 -0800737
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800738 if (time_before_eq(expires, now))
739 return now;
Zachary Amsden0662b712006-05-20 15:00:24 -0700740
Thomas Gleixner1cfd6842007-02-16 01:27:46 -0800741 return cmp_next_hrtimer_event(now, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800743
744#ifdef CONFIG_NO_IDLE_HZ
745unsigned long next_timer_interrupt(void)
746{
747 return get_next_timer_interrupt(jiffies);
748}
749#endif
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751#endif
752
753/******************************************************************/
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/*
756 * The current time
757 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
758 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
759 * at zero at system boot time, so wall_to_monotonic will be negative,
760 * however, we will ALWAYS keep the tv_nsec part positive so we can use
761 * the usual normalization.
762 */
763struct timespec xtime __attribute__ ((aligned (16)));
764struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
765
766EXPORT_SYMBOL(xtime);
767
Paul Mackerras726c14b2006-02-17 10:30:23 +1100768
john stultzad596172006-06-26 00:25:06 -0700769/* XXX - all of this timekeeping code should be later moved to time.c */
770#include <linux/clocksource.h>
771static struct clocksource *clock; /* pointer to current clocksource */
john stultzcf3c7692006-06-26 00:25:08 -0700772
773#ifdef CONFIG_GENERIC_TIME
774/**
775 * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook
776 *
777 * private function, must hold xtime_lock lock when being
778 * called. Returns the number of nanoseconds since the
779 * last call to update_wall_time() (adjusted by NTP scaling)
780 */
781static inline s64 __get_nsec_offset(void)
782{
783 cycle_t cycle_now, cycle_delta;
784 s64 ns_offset;
785
786 /* read clocksource: */
john stultza2752542006-06-26 00:25:14 -0700787 cycle_now = clocksource_read(clock);
john stultzcf3c7692006-06-26 00:25:08 -0700788
789 /* calculate the delta since the last update_wall_time: */
Roman Zippel19923c12006-06-26 00:25:18 -0700790 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
john stultzcf3c7692006-06-26 00:25:08 -0700791
792 /* convert to nanoseconds: */
793 ns_offset = cyc2ns(clock, cycle_delta);
794
795 return ns_offset;
796}
797
798/**
799 * __get_realtime_clock_ts - Returns the time of day in a timespec
800 * @ts: pointer to the timespec to be set
801 *
802 * Returns the time of day in a timespec. Used by
803 * do_gettimeofday() and get_realtime_clock_ts().
804 */
805static inline void __get_realtime_clock_ts(struct timespec *ts)
806{
807 unsigned long seq;
808 s64 nsecs;
809
810 do {
811 seq = read_seqbegin(&xtime_lock);
812
813 *ts = xtime;
814 nsecs = __get_nsec_offset();
815
816 } while (read_seqretry(&xtime_lock, seq));
817
818 timespec_add_ns(ts, nsecs);
819}
820
821/**
john stultza2752542006-06-26 00:25:14 -0700822 * getnstimeofday - Returns the time of day in a timespec
john stultzcf3c7692006-06-26 00:25:08 -0700823 * @ts: pointer to the timespec to be set
824 *
825 * Returns the time of day in a timespec.
826 */
827void getnstimeofday(struct timespec *ts)
828{
829 __get_realtime_clock_ts(ts);
830}
831
832EXPORT_SYMBOL(getnstimeofday);
833
834/**
835 * do_gettimeofday - Returns the time of day in a timeval
836 * @tv: pointer to the timeval to be set
837 *
838 * NOTE: Users should be converted to using get_realtime_clock_ts()
839 */
840void do_gettimeofday(struct timeval *tv)
841{
842 struct timespec now;
843
844 __get_realtime_clock_ts(&now);
845 tv->tv_sec = now.tv_sec;
846 tv->tv_usec = now.tv_nsec/1000;
847}
848
849EXPORT_SYMBOL(do_gettimeofday);
850/**
851 * do_settimeofday - Sets the time of day
852 * @tv: pointer to the timespec variable containing the new time
853 *
854 * Sets the time of day to the new time and update NTP and notify hrtimers
855 */
856int do_settimeofday(struct timespec *tv)
857{
858 unsigned long flags;
859 time_t wtm_sec, sec = tv->tv_sec;
860 long wtm_nsec, nsec = tv->tv_nsec;
861
862 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
863 return -EINVAL;
864
865 write_seqlock_irqsave(&xtime_lock, flags);
866
867 nsec -= __get_nsec_offset();
868
869 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
870 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
871
872 set_normalized_timespec(&xtime, sec, nsec);
873 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
874
Roman Zippele154ff32006-07-10 04:44:32 -0700875 clock->error = 0;
john stultzcf3c7692006-06-26 00:25:08 -0700876 ntp_clear();
877
Daniel Walker90675a22007-03-06 01:42:11 -0800878 update_vsyscall(&xtime, clock);
879
john stultzcf3c7692006-06-26 00:25:08 -0700880 write_sequnlock_irqrestore(&xtime_lock, flags);
881
882 /* signal hrtimers about time change */
883 clock_was_set();
884
885 return 0;
886}
887
888EXPORT_SYMBOL(do_settimeofday);
889
890/**
891 * change_clocksource - Swaps clocksources if a new one is available
892 *
893 * Accumulates current time interval and initializes new clocksource
894 */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800895static void change_clocksource(void)
john stultzcf3c7692006-06-26 00:25:08 -0700896{
897 struct clocksource *new;
898 cycle_t now;
899 u64 nsec;
john stultzcf3c7692006-06-26 00:25:08 -0700900
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800901 new = clocksource_get_next();
902
903 if (clock == new)
904 return;
905
906 now = clocksource_read(new);
907 nsec = __get_nsec_offset();
908 timespec_add_ns(&xtime, nsec);
909
910 clock = new;
911 clock->cycle_last = now;
912
913 clock->error = 0;
914 clock->xtime_nsec = 0;
915 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
916
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800917 tick_clock_notify();
918
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800919 printk(KERN_INFO "Time: %s clocksource has been installed.\n",
920 clock->name);
john stultzcf3c7692006-06-26 00:25:08 -0700921}
922#else
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800923static inline void change_clocksource(void) { }
john stultzcf3c7692006-06-26 00:25:08 -0700924#endif
925
926/**
Daniel Walker9d6346312007-02-28 20:12:07 -0800927 * timekeeping_is_continuous - check to see if timekeeping is free running
john stultzcf3c7692006-06-26 00:25:08 -0700928 */
929int timekeeping_is_continuous(void)
930{
931 unsigned long seq;
932 int ret;
933
934 do {
935 seq = read_seqbegin(&xtime_lock);
936
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800937 ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultzcf3c7692006-06-26 00:25:08 -0700938
939 } while (read_seqretry(&xtime_lock, seq));
940
941 return ret;
942}
943
John Stultz411187f2007-02-16 01:27:30 -0800944/**
945 * read_persistent_clock - Return time in seconds from the persistent clock.
946 *
947 * Weak dummy function for arches that do not yet support it.
948 * Returns seconds from epoch using the battery backed persistent clock.
949 * Returns zero if unsupported.
950 *
951 * XXX - Do be sure to remove it once all arches implement it.
952 */
953unsigned long __attribute__((weak)) read_persistent_clock(void)
954{
955 return 0;
956}
957
Paul Mackerras726c14b2006-02-17 10:30:23 +1100958/*
john stultzad596172006-06-26 00:25:06 -0700959 * timekeeping_init - Initializes the clocksource and common timekeeping values
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 */
john stultzad596172006-06-26 00:25:06 -0700961void __init timekeeping_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
john stultzad596172006-06-26 00:25:06 -0700963 unsigned long flags;
John Stultz411187f2007-02-16 01:27:30 -0800964 unsigned long sec = read_persistent_clock();
john stultzad596172006-06-26 00:25:06 -0700965
966 write_seqlock_irqsave(&xtime_lock, flags);
Roman Zippelb0ee7552006-09-30 23:28:22 -0700967
968 ntp_clear();
969
john stultza2752542006-06-26 00:25:14 -0700970 clock = clocksource_get_next();
john stultzf4304ab2007-02-16 01:27:26 -0800971 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
Roman Zippel19923c12006-06-26 00:25:18 -0700972 clock->cycle_last = clocksource_read(clock);
Roman Zippelb0ee7552006-09-30 23:28:22 -0700973
John Stultz411187f2007-02-16 01:27:30 -0800974 xtime.tv_sec = sec;
975 xtime.tv_nsec = 0;
976 set_normalized_timespec(&wall_to_monotonic,
977 -xtime.tv_sec, -xtime.tv_nsec);
978
john stultzad596172006-06-26 00:25:06 -0700979 write_sequnlock_irqrestore(&xtime_lock, flags);
980}
981
John Stultz411187f2007-02-16 01:27:30 -0800982/* flag for if timekeeping is suspended */
john stultz3e143472006-07-14 00:24:17 -0700983static int timekeeping_suspended;
John Stultz411187f2007-02-16 01:27:30 -0800984/* time in seconds when suspend began */
985static unsigned long timekeeping_suspend_time;
986
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700987/**
john stultzad596172006-06-26 00:25:06 -0700988 * timekeeping_resume - Resumes the generic timekeeping subsystem.
989 * @dev: unused
990 *
991 * This is for the generic clocksource timekeeping.
Atsushi Nemoto8ef38602006-09-30 23:28:31 -0700992 * xtime/wall_to_monotonic/jiffies/etc are
john stultzad596172006-06-26 00:25:06 -0700993 * still managed by arch specific suspend/resume code.
994 */
995static int timekeeping_resume(struct sys_device *dev)
996{
997 unsigned long flags;
John Stultz411187f2007-02-16 01:27:30 -0800998 unsigned long now = read_persistent_clock();
john stultzad596172006-06-26 00:25:06 -0700999
1000 write_seqlock_irqsave(&xtime_lock, flags);
John Stultz411187f2007-02-16 01:27:30 -08001001
1002 if (now && (now > timekeeping_suspend_time)) {
1003 unsigned long sleep_length = now - timekeeping_suspend_time;
1004
1005 xtime.tv_sec += sleep_length;
1006 wall_to_monotonic.tv_sec -= sleep_length;
1007 }
1008 /* re-base the last cycle value */
Roman Zippel19923c12006-06-26 00:25:18 -07001009 clock->cycle_last = clocksource_read(clock);
john stultz3e143472006-07-14 00:24:17 -07001010 clock->error = 0;
1011 timekeeping_suspended = 0;
1012 write_sequnlock_irqrestore(&xtime_lock, flags);
John Stultz411187f2007-02-16 01:27:30 -08001013
1014 touch_softlockup_watchdog();
Thomas Gleixner6321dd62007-03-06 08:25:42 +01001015
1016 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
1017
Thomas Gleixnerd316c572007-02-16 01:28:00 -08001018 /* Resume hrtimers */
Ingo Molnar995f0542007-04-07 12:05:00 +02001019 hres_timers_resume();
John Stultz411187f2007-02-16 01:27:30 -08001020
john stultz3e143472006-07-14 00:24:17 -07001021 return 0;
1022}
1023
1024static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
1025{
1026 unsigned long flags;
1027
1028 write_seqlock_irqsave(&xtime_lock, flags);
1029 timekeeping_suspended = 1;
John Stultz411187f2007-02-16 01:27:30 -08001030 timekeeping_suspend_time = read_persistent_clock();
john stultzad596172006-06-26 00:25:06 -07001031 write_sequnlock_irqrestore(&xtime_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +01001032
1033 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
1034
john stultzad596172006-06-26 00:25:06 -07001035 return 0;
1036}
1037
1038/* sysfs resume/suspend bits for timekeeping */
1039static struct sysdev_class timekeeping_sysclass = {
1040 .resume = timekeeping_resume,
john stultz3e143472006-07-14 00:24:17 -07001041 .suspend = timekeeping_suspend,
john stultzad596172006-06-26 00:25:06 -07001042 set_kset_name("timekeeping"),
1043};
1044
1045static struct sys_device device_timer = {
1046 .id = 0,
1047 .cls = &timekeeping_sysclass,
1048};
1049
1050static int __init timekeeping_init_device(void)
1051{
1052 int error = sysdev_class_register(&timekeeping_sysclass);
1053 if (!error)
1054 error = sysdev_register(&device_timer);
1055 return error;
1056}
1057
1058device_initcall(timekeeping_init_device);
1059
1060/*
Roman Zippele154ff32006-07-10 04:44:32 -07001061 * If the error is already larger, we look ahead even further
Roman Zippel19923c12006-06-26 00:25:18 -07001062 * to compensate for late or lost adjustments.
1063 */
Daniel Walkerf5f1a242006-12-10 02:21:33 -08001064static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
1065 s64 *offset)
Roman Zippel19923c12006-06-26 00:25:18 -07001066{
Roman Zippele154ff32006-07-10 04:44:32 -07001067 s64 tick_error, i;
1068 u32 look_ahead, adj;
1069 s32 error2, mult;
Roman Zippel19923c12006-06-26 00:25:18 -07001070
1071 /*
Roman Zippele154ff32006-07-10 04:44:32 -07001072 * Use the current error value to determine how much to look ahead.
1073 * The larger the error the slower we adjust for it to avoid problems
1074 * with losing too many ticks, otherwise we would overadjust and
1075 * produce an even larger error. The smaller the adjustment the
1076 * faster we try to adjust for it, as lost ticks can do less harm
1077 * here. This is tuned so that an error of about 1 msec is adusted
1078 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
Roman Zippel19923c12006-06-26 00:25:18 -07001079 */
Roman Zippele154ff32006-07-10 04:44:32 -07001080 error2 = clock->error >> (TICK_LENGTH_SHIFT + 22 - 2 * SHIFT_HZ);
1081 error2 = abs(error2);
1082 for (look_ahead = 0; error2 > 0; look_ahead++)
1083 error2 >>= 2;
Roman Zippel19923c12006-06-26 00:25:18 -07001084
1085 /*
Roman Zippele154ff32006-07-10 04:44:32 -07001086 * Now calculate the error in (1 << look_ahead) ticks, but first
1087 * remove the single look ahead already included in the error.
Roman Zippel19923c12006-06-26 00:25:18 -07001088 */
Daniel Walkerf5f1a242006-12-10 02:21:33 -08001089 tick_error = current_tick_length() >>
1090 (TICK_LENGTH_SHIFT - clock->shift + 1);
Roman Zippele154ff32006-07-10 04:44:32 -07001091 tick_error -= clock->xtime_interval >> 1;
1092 error = ((error - tick_error) >> look_ahead) + tick_error;
Roman Zippel19923c12006-06-26 00:25:18 -07001093
Roman Zippele154ff32006-07-10 04:44:32 -07001094 /* Finally calculate the adjustment shift value. */
1095 i = *interval;
1096 mult = 1;
1097 if (error < 0) {
1098 error = -error;
1099 *interval = -*interval;
1100 *offset = -*offset;
1101 mult = -1;
Roman Zippel19923c12006-06-26 00:25:18 -07001102 }
Roman Zippele154ff32006-07-10 04:44:32 -07001103 for (adj = 0; error > i; adj++)
1104 error >>= 1;
Roman Zippel19923c12006-06-26 00:25:18 -07001105
1106 *interval <<= adj;
1107 *offset <<= adj;
Roman Zippele154ff32006-07-10 04:44:32 -07001108 return mult << adj;
Roman Zippel19923c12006-06-26 00:25:18 -07001109}
1110
1111/*
1112 * Adjust the multiplier to reduce the error value,
1113 * this is optimized for the most common adjustments of -1,0,1,
1114 * for other values we can do a bit more work.
1115 */
1116static void clocksource_adjust(struct clocksource *clock, s64 offset)
1117{
1118 s64 error, interval = clock->cycle_interval;
1119 int adj;
1120
1121 error = clock->error >> (TICK_LENGTH_SHIFT - clock->shift - 1);
1122 if (error > interval) {
Roman Zippele154ff32006-07-10 04:44:32 -07001123 error >>= 2;
1124 if (likely(error <= interval))
1125 adj = 1;
1126 else
1127 adj = clocksource_bigadjust(error, &interval, &offset);
Roman Zippel19923c12006-06-26 00:25:18 -07001128 } else if (error < -interval) {
Roman Zippele154ff32006-07-10 04:44:32 -07001129 error >>= 2;
1130 if (likely(error >= -interval)) {
1131 adj = -1;
1132 interval = -interval;
1133 offset = -offset;
1134 } else
1135 adj = clocksource_bigadjust(error, &interval, &offset);
Roman Zippel19923c12006-06-26 00:25:18 -07001136 } else
1137 return;
1138
1139 clock->mult += adj;
1140 clock->xtime_interval += interval;
1141 clock->xtime_nsec -= offset;
Daniel Walkerf5f1a242006-12-10 02:21:33 -08001142 clock->error -= (interval - offset) <<
1143 (TICK_LENGTH_SHIFT - clock->shift);
Roman Zippel19923c12006-06-26 00:25:18 -07001144}
1145
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001146/**
john stultzad596172006-06-26 00:25:06 -07001147 * update_wall_time - Uses the current clocksource to increment the wall time
1148 *
1149 * Called from the timer interrupt, must hold a write on xtime_lock.
1150 */
1151static void update_wall_time(void)
1152{
Roman Zippel19923c12006-06-26 00:25:18 -07001153 cycle_t offset;
john stultzad596172006-06-26 00:25:06 -07001154
john stultz3e143472006-07-14 00:24:17 -07001155 /* Make sure we're fully resumed: */
1156 if (unlikely(timekeeping_suspended))
1157 return;
john stultz5eb6d202006-06-26 00:25:07 -07001158
Roman Zippel19923c12006-06-26 00:25:18 -07001159#ifdef CONFIG_GENERIC_TIME
1160 offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
1161#else
1162 offset = clock->cycle_interval;
1163#endif
john stultz3e143472006-07-14 00:24:17 -07001164 clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift;
john stultzad596172006-06-26 00:25:06 -07001165
1166 /* normally this loop will run just once, however in the
1167 * case of lost or late ticks, it will accumulate correctly.
1168 */
Roman Zippel19923c12006-06-26 00:25:18 -07001169 while (offset >= clock->cycle_interval) {
john stultzad596172006-06-26 00:25:06 -07001170 /* accumulate one interval */
Roman Zippel19923c12006-06-26 00:25:18 -07001171 clock->xtime_nsec += clock->xtime_interval;
1172 clock->cycle_last += clock->cycle_interval;
1173 offset -= clock->cycle_interval;
1174
1175 if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
1176 clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
1177 xtime.tv_sec++;
1178 second_overflow();
1179 }
john stultzad596172006-06-26 00:25:06 -07001180
john stultz5eb6d202006-06-26 00:25:07 -07001181 /* interpolator bits */
Roman Zippel19923c12006-06-26 00:25:18 -07001182 time_interpolator_update(clock->xtime_interval
john stultz5eb6d202006-06-26 00:25:07 -07001183 >> clock->shift);
john stultz5eb6d202006-06-26 00:25:07 -07001184
1185 /* accumulate error between NTP and clock interval */
Roman Zippel19923c12006-06-26 00:25:18 -07001186 clock->error += current_tick_length();
1187 clock->error -= clock->xtime_interval << (TICK_LENGTH_SHIFT - clock->shift);
john stultzad596172006-06-26 00:25:06 -07001188 }
Roman Zippel19923c12006-06-26 00:25:18 -07001189
1190 /* correct the clock when NTP error is too big */
1191 clocksource_adjust(clock, offset);
1192
john stultz5eb6d202006-06-26 00:25:07 -07001193 /* store full nanoseconds into xtime */
Roman Zippele154ff32006-07-10 04:44:32 -07001194 xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift;
Roman Zippel19923c12006-06-26 00:25:18 -07001195 clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
john stultzcf3c7692006-06-26 00:25:08 -07001196
1197 /* check to see if there is a new clocksource to use */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -08001198 change_clocksource();
john stultzacc9a9d2007-02-16 01:28:17 -08001199 update_vsyscall(&xtime, clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
1202/*
1203 * Called from the timer interrupt handler to charge one tick to the current
1204 * process. user_tick is 1 if the tick is user time, 0 for system.
1205 */
1206void update_process_times(int user_tick)
1207{
1208 struct task_struct *p = current;
1209 int cpu = smp_processor_id();
1210
1211 /* Note: this timer irq context must be accounted for as well. */
1212 if (user_tick)
1213 account_user_time(p, jiffies_to_cputime(1));
1214 else
1215 account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
1216 run_local_timers();
1217 if (rcu_pending(cpu))
1218 rcu_check_callbacks(cpu, user_tick);
1219 scheduler_tick();
1220 run_posix_cpu_timers(p);
1221}
1222
1223/*
1224 * Nr of active tasks - counted in fixed-point numbers
1225 */
1226static unsigned long count_active_tasks(void)
1227{
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08001228 return nr_active() * FIXED_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231/*
1232 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
1233 * imply that avenrun[] is the standard name for this kind of thing.
1234 * Nothing else seems to be standardized: the fractional size etc
1235 * all seem to differ on different machines.
1236 *
1237 * Requires xtime_lock to access.
1238 */
1239unsigned long avenrun[3];
1240
1241EXPORT_SYMBOL(avenrun);
1242
1243/*
1244 * calc_load - given tick count, update the avenrun load estimates.
1245 * This is called while holding a write_lock on xtime_lock.
1246 */
1247static inline void calc_load(unsigned long ticks)
1248{
1249 unsigned long active_tasks; /* fixed-point */
1250 static int count = LOAD_FREQ;
1251
Eric Dumazetcd7175e2006-12-13 00:35:45 -08001252 count -= ticks;
1253 if (unlikely(count < 0)) {
1254 active_tasks = count_active_tasks();
1255 do {
1256 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
1257 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
1258 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
1259 count += LOAD_FREQ;
1260 } while (count < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262}
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264/*
1265 * This read-write spinlock protects us from races in SMP while
1266 * playing with xtime and avenrun.
1267 */
Eric Dumazet5809f9d2007-02-13 13:26:21 +01001268__attribute__((weak)) __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270EXPORT_SYMBOL(xtime_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272/*
1273 * This function runs timers and the timer-tq in bottom half context.
1274 */
1275static void run_timer_softirq(struct softirq_action *h)
1276{
Jan Beulicha4a61982006-03-24 03:15:54 -08001277 tvec_base_t *base = __get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001279 hrtimer_run_queues();
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (time_after_eq(jiffies, base->timer_jiffies))
1282 __run_timers(base);
1283}
1284
1285/*
1286 * Called by the local, per-CPU timer interrupt on SMP.
1287 */
1288void run_local_timers(void)
1289{
1290 raise_softirq(TIMER_SOFTIRQ);
Ingo Molnar6687a972006-03-24 03:18:41 -08001291 softlockup_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294/*
1295 * Called by the timer interrupt. xtime_lock must already be taken
1296 * by the timer IRQ!
1297 */
Atsushi Nemoto3171a032006-09-29 02:00:32 -07001298static inline void update_times(unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
john stultzad596172006-06-26 00:25:06 -07001300 update_wall_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 calc_load(ticks);
1302}
1303
1304/*
1305 * The 64-bit jiffies value is not atomic - you MUST NOT read it
1306 * without sampling the sequence number in xtime_lock.
1307 * jiffies is defined in the linker script...
1308 */
1309
Atsushi Nemoto3171a032006-09-29 02:00:32 -07001310void do_timer(unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Atsushi Nemoto3171a032006-09-29 02:00:32 -07001312 jiffies_64 += ticks;
1313 update_times(ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
1316#ifdef __ARCH_WANT_SYS_ALARM
1317
1318/*
1319 * For backwards compatibility? This can be done in libc so Alpha
1320 * and all newer ports shouldn't need it.
1321 */
1322asmlinkage unsigned long sys_alarm(unsigned int seconds)
1323{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -08001324 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
1327#endif
1328
1329#ifndef __alpha__
1330
1331/*
1332 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
1333 * should be moved into arch/i386 instead?
1334 */
1335
1336/**
1337 * sys_getpid - return the thread group id of the current process
1338 *
1339 * Note, despite the name, this returns the tgid not the pid. The tgid and
1340 * the pid are identical unless CLONE_THREAD was specified on clone() in
1341 * which case the tgid is the same in all threads of the same group.
1342 *
1343 * This is SMP safe as current->tgid does not change.
1344 */
1345asmlinkage long sys_getpid(void)
1346{
1347 return current->tgid;
1348}
1349
1350/*
Kirill Korotaev6997a6f2006-08-13 23:24:23 -07001351 * Accessing ->real_parent is not SMP-safe, it could
1352 * change from under us. However, we can use a stale
1353 * value of ->real_parent under rcu_read_lock(), see
1354 * release_task()->call_rcu(delayed_put_task_struct).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 */
1356asmlinkage long sys_getppid(void)
1357{
1358 int pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Kirill Korotaev6997a6f2006-08-13 23:24:23 -07001360 rcu_read_lock();
1361 pid = rcu_dereference(current->real_parent)->tgid;
1362 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return pid;
1365}
1366
1367asmlinkage long sys_getuid(void)
1368{
1369 /* Only we change this so SMP safe */
1370 return current->uid;
1371}
1372
1373asmlinkage long sys_geteuid(void)
1374{
1375 /* Only we change this so SMP safe */
1376 return current->euid;
1377}
1378
1379asmlinkage long sys_getgid(void)
1380{
1381 /* Only we change this so SMP safe */
1382 return current->gid;
1383}
1384
1385asmlinkage long sys_getegid(void)
1386{
1387 /* Only we change this so SMP safe */
1388 return current->egid;
1389}
1390
1391#endif
1392
1393static void process_timeout(unsigned long __data)
1394{
Ingo Molnar36c8b582006-07-03 00:25:41 -07001395 wake_up_process((struct task_struct *)__data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
1398/**
1399 * schedule_timeout - sleep until timeout
1400 * @timeout: timeout value in jiffies
1401 *
1402 * Make the current task sleep until @timeout jiffies have
1403 * elapsed. The routine will return immediately unless
1404 * the current task state has been set (see set_current_state()).
1405 *
1406 * You can set the task state as follows -
1407 *
1408 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1409 * pass before the routine returns. The routine will return 0
1410 *
1411 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1412 * delivered to the current task. In this case the remaining time
1413 * in jiffies will be returned, or 0 if the timer expired in time
1414 *
1415 * The current task state is guaranteed to be TASK_RUNNING when this
1416 * routine returns.
1417 *
1418 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1419 * the CPU away without a bound on the timeout. In this case the return
1420 * value will be %MAX_SCHEDULE_TIMEOUT.
1421 *
1422 * In all cases the return value is guaranteed to be non-negative.
1423 */
1424fastcall signed long __sched schedule_timeout(signed long timeout)
1425{
1426 struct timer_list timer;
1427 unsigned long expire;
1428
1429 switch (timeout)
1430 {
1431 case MAX_SCHEDULE_TIMEOUT:
1432 /*
1433 * These two special cases are useful to be comfortable
1434 * in the caller. Nothing more. We could take
1435 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1436 * but I' d like to return a valid offset (>=0) to allow
1437 * the caller to do everything it want with the retval.
1438 */
1439 schedule();
1440 goto out;
1441 default:
1442 /*
1443 * Another bit of PARANOID. Note that the retval will be
1444 * 0 since no piece of kernel is supposed to do a check
1445 * for a negative retval of schedule_timeout() (since it
1446 * should never happens anyway). You just have the printk()
1447 * that will tell you if something is gone wrong and where.
1448 */
Andrew Morton5b149bc2006-12-22 01:10:14 -08001449 if (timeout < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 printk(KERN_ERR "schedule_timeout: wrong timeout "
Andrew Morton5b149bc2006-12-22 01:10:14 -08001451 "value %lx\n", timeout);
1452 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 current->state = TASK_RUNNING;
1454 goto out;
1455 }
1456 }
1457
1458 expire = timeout + jiffies;
1459
Oleg Nesterova8db2db2005-10-30 15:01:38 -08001460 setup_timer(&timer, process_timeout, (unsigned long)current);
1461 __mod_timer(&timer, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 schedule();
1463 del_singleshot_timer_sync(&timer);
1464
1465 timeout = expire - jiffies;
1466
1467 out:
1468 return timeout < 0 ? 0 : timeout;
1469}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470EXPORT_SYMBOL(schedule_timeout);
1471
Andrew Morton8a1c1752005-09-13 01:25:15 -07001472/*
1473 * We can use __set_current_state() here because schedule_timeout() calls
1474 * schedule() unconditionally.
1475 */
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001476signed long __sched schedule_timeout_interruptible(signed long timeout)
1477{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001478 __set_current_state(TASK_INTERRUPTIBLE);
1479 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001480}
1481EXPORT_SYMBOL(schedule_timeout_interruptible);
1482
1483signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1484{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001485 __set_current_state(TASK_UNINTERRUPTIBLE);
1486 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001487}
1488EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490/* Thread ID - the internal kernel "pid" */
1491asmlinkage long sys_gettid(void)
1492{
1493 return current->pid;
1494}
1495
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001496/**
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001497 * do_sysinfo - fill in sysinfo struct
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001498 * @info: pointer to buffer to fill
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 */
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001500int do_sysinfo(struct sysinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 unsigned long mem_total, sav_total;
1503 unsigned int mem_unit, bitcount;
1504 unsigned long seq;
1505
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001506 memset(info, 0, sizeof(struct sysinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 do {
1509 struct timespec tp;
1510 seq = read_seqbegin(&xtime_lock);
1511
1512 /*
1513 * This is annoying. The below is the same thing
1514 * posix_get_clock_monotonic() does, but it wants to
1515 * take the lock which we want to cover the loads stuff
1516 * too.
1517 */
1518
1519 getnstimeofday(&tp);
1520 tp.tv_sec += wall_to_monotonic.tv_sec;
1521 tp.tv_nsec += wall_to_monotonic.tv_nsec;
1522 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1523 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1524 tp.tv_sec++;
1525 }
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001526 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001528 info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1529 info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1530 info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001532 info->procs = nr_threads;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 } while (read_seqretry(&xtime_lock, seq));
1534
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001535 si_meminfo(info);
1536 si_swapinfo(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 /*
1539 * If the sum of all the available memory (i.e. ram + swap)
1540 * is less than can be stored in a 32 bit unsigned long then
1541 * we can be binary compatible with 2.2.x kernels. If not,
1542 * well, in that case 2.2.x was broken anyways...
1543 *
1544 * -Erik Andersen <andersee@debian.org>
1545 */
1546
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001547 mem_total = info->totalram + info->totalswap;
1548 if (mem_total < info->totalram || mem_total < info->totalswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 goto out;
1550 bitcount = 0;
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001551 mem_unit = info->mem_unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 while (mem_unit > 1) {
1553 bitcount++;
1554 mem_unit >>= 1;
1555 sav_total = mem_total;
1556 mem_total <<= 1;
1557 if (mem_total < sav_total)
1558 goto out;
1559 }
1560
1561 /*
1562 * If mem_total did not overflow, multiply all memory values by
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001563 * info->mem_unit and set it to 1. This leaves things compatible
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1565 * kernels...
1566 */
1567
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001568 info->mem_unit = 1;
1569 info->totalram <<= bitcount;
1570 info->freeram <<= bitcount;
1571 info->sharedram <<= bitcount;
1572 info->bufferram <<= bitcount;
1573 info->totalswap <<= bitcount;
1574 info->freeswap <<= bitcount;
1575 info->totalhigh <<= bitcount;
1576 info->freehigh <<= bitcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Kyle McMartind4d23ad2007-02-10 01:46:00 -08001578out:
1579 return 0;
1580}
1581
1582asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1583{
1584 struct sysinfo val;
1585
1586 do_sysinfo(&val);
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1589 return -EFAULT;
1590
1591 return 0;
1592}
1593
Ingo Molnard730e882006-07-03 00:25:10 -07001594/*
1595 * lockdep: we want to track each per-CPU base as a separate lock-class,
1596 * but timer-bases are kmalloc()-ed, so we need to attach separate
1597 * keys to them:
1598 */
1599static struct lock_class_key base_lock_keys[NR_CPUS];
1600
Jan Beulicha4a61982006-03-24 03:15:54 -08001601static int __devinit init_timers_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 int j;
1604 tvec_base_t *base;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001605 static char __devinitdata tvec_base_done[NR_CPUS];
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001606
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001607 if (!tvec_base_done[cpu]) {
Jan Beulicha4a61982006-03-24 03:15:54 -08001608 static char boot_done;
1609
Jan Beulicha4a61982006-03-24 03:15:54 -08001610 if (boot_done) {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001611 /*
1612 * The APs use this path later in boot
1613 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001614 base = kmalloc_node(sizeof(*base), GFP_KERNEL,
1615 cpu_to_node(cpu));
1616 if (!base)
1617 return -ENOMEM;
1618 memset(base, 0, sizeof(*base));
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001619 per_cpu(tvec_bases, cpu) = base;
Jan Beulicha4a61982006-03-24 03:15:54 -08001620 } else {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001621 /*
1622 * This is for the boot CPU - we use compile-time
1623 * static initialisation because per-cpu memory isn't
1624 * ready yet and because the memory allocators are not
1625 * initialised either.
1626 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001627 boot_done = 1;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001628 base = &boot_tvec_bases;
Jan Beulicha4a61982006-03-24 03:15:54 -08001629 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001630 tvec_base_done[cpu] = 1;
1631 } else {
1632 base = per_cpu(tvec_bases, cpu);
Jan Beulicha4a61982006-03-24 03:15:54 -08001633 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001634
Oleg Nesterov3691c512006-03-31 02:30:30 -08001635 spin_lock_init(&base->lock);
Ingo Molnard730e882006-07-03 00:25:10 -07001636 lockdep_set_class(&base->lock, base_lock_keys + cpu);
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 for (j = 0; j < TVN_SIZE; j++) {
1639 INIT_LIST_HEAD(base->tv5.vec + j);
1640 INIT_LIST_HEAD(base->tv4.vec + j);
1641 INIT_LIST_HEAD(base->tv3.vec + j);
1642 INIT_LIST_HEAD(base->tv2.vec + j);
1643 }
1644 for (j = 0; j < TVR_SIZE; j++)
1645 INIT_LIST_HEAD(base->tv1.vec + j);
1646
1647 base->timer_jiffies = jiffies;
Jan Beulicha4a61982006-03-24 03:15:54 -08001648 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
1651#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001652static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 struct timer_list *timer;
1655
1656 while (!list_empty(head)) {
1657 timer = list_entry(head->next, struct timer_list, entry);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001658 detach_timer(timer, 0);
Oleg Nesterov3691c512006-03-31 02:30:30 -08001659 timer->base = new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 internal_add_timer(new_base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
1664static void __devinit migrate_timers(int cpu)
1665{
1666 tvec_base_t *old_base;
1667 tvec_base_t *new_base;
1668 int i;
1669
1670 BUG_ON(cpu_online(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001671 old_base = per_cpu(tvec_bases, cpu);
1672 new_base = get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 local_irq_disable();
Heiko Carstense81ce1f2007-03-05 00:30:51 -08001675 double_spin_lock(&new_base->lock, &old_base->lock,
1676 smp_processor_id() < cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Oleg Nesterov3691c512006-03-31 02:30:30 -08001678 BUG_ON(old_base->running_timer);
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 for (i = 0; i < TVR_SIZE; i++)
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001681 migrate_timer_list(new_base, old_base->tv1.vec + i);
1682 for (i = 0; i < TVN_SIZE; i++) {
1683 migrate_timer_list(new_base, old_base->tv2.vec + i);
1684 migrate_timer_list(new_base, old_base->tv3.vec + i);
1685 migrate_timer_list(new_base, old_base->tv4.vec + i);
1686 migrate_timer_list(new_base, old_base->tv5.vec + i);
1687 }
1688
Heiko Carstense81ce1f2007-03-05 00:30:51 -08001689 double_spin_unlock(&new_base->lock, &old_base->lock,
1690 smp_processor_id() < cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 local_irq_enable();
1692 put_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694#endif /* CONFIG_HOTPLUG_CPU */
1695
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001696static int __cpuinit timer_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 unsigned long action, void *hcpu)
1698{
1699 long cpu = (long)hcpu;
1700 switch(action) {
1701 case CPU_UP_PREPARE:
Jan Beulicha4a61982006-03-24 03:15:54 -08001702 if (init_timers_cpu(cpu) < 0)
1703 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 break;
1705#ifdef CONFIG_HOTPLUG_CPU
1706 case CPU_DEAD:
1707 migrate_timers(cpu);
1708 break;
1709#endif
1710 default:
1711 break;
1712 }
1713 return NOTIFY_OK;
1714}
1715
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001716static struct notifier_block __cpuinitdata timers_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 .notifier_call = timer_cpu_notify,
1718};
1719
1720
1721void __init init_timers(void)
1722{
Akinobu Mita07dccf32006-09-29 02:00:22 -07001723 int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 (void *)(long)smp_processor_id());
Akinobu Mita07dccf32006-09-29 02:00:22 -07001725
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001726 init_timer_stats();
1727
Akinobu Mita07dccf32006-09-29 02:00:22 -07001728 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 register_cpu_notifier(&timers_nb);
1730 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1731}
1732
1733#ifdef CONFIG_TIME_INTERPOLATION
1734
Christoph Lameter67890d72006-03-16 23:04:00 -08001735struct time_interpolator *time_interpolator __read_mostly;
1736static struct time_interpolator *time_interpolator_list __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737static DEFINE_SPINLOCK(time_interpolator_lock);
1738
Helge Deller3db5db42007-02-10 01:45:40 -08001739static inline cycles_t time_interpolator_get_cycles(unsigned int src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 unsigned long (*x)(void);
1742
1743 switch (src)
1744 {
1745 case TIME_SOURCE_FUNCTION:
1746 x = time_interpolator->addr;
1747 return x();
1748
1749 case TIME_SOURCE_MMIO64 :
Christoph Lameter685db652006-03-02 02:54:35 -08001750 return readq_relaxed((void __iomem *)time_interpolator->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 case TIME_SOURCE_MMIO32 :
Christoph Lameter685db652006-03-02 02:54:35 -08001753 return readl_relaxed((void __iomem *)time_interpolator->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 default: return get_cycles();
1756 }
1757}
1758
Alex Williamson486d46ae2005-09-06 15:17:04 -07001759static inline u64 time_interpolator_get_counter(int writelock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 unsigned int src = time_interpolator->source;
1762
1763 if (time_interpolator->jitter)
1764 {
Helge Deller3db5db42007-02-10 01:45:40 -08001765 cycles_t lcycle;
1766 cycles_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 do {
1769 lcycle = time_interpolator->last_cycle;
1770 now = time_interpolator_get_cycles(src);
1771 if (lcycle && time_after(lcycle, now))
1772 return lcycle;
Alex Williamson486d46ae2005-09-06 15:17:04 -07001773
1774 /* When holding the xtime write lock, there's no need
1775 * to add the overhead of the cmpxchg. Readers are
1776 * force to retry until the write lock is released.
1777 */
1778 if (writelock) {
1779 time_interpolator->last_cycle = now;
1780 return now;
1781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 /* Keep track of the last timer value returned. The use of cmpxchg here
1783 * will cause contention in an SMP environment.
1784 */
1785 } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
1786 return now;
1787 }
1788 else
1789 return time_interpolator_get_cycles(src);
1790}
1791
1792void time_interpolator_reset(void)
1793{
1794 time_interpolator->offset = 0;
Alex Williamson486d46ae2005-09-06 15:17:04 -07001795 time_interpolator->last_counter = time_interpolator_get_counter(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
1798#define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
1799
1800unsigned long time_interpolator_get_offset(void)
1801{
1802 /* If we do not have a time interpolator set up then just return zero */
1803 if (!time_interpolator)
1804 return 0;
1805
1806 return time_interpolator->offset +
Alex Williamson486d46ae2005-09-06 15:17:04 -07001807 GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
1809
1810#define INTERPOLATOR_ADJUST 65536
1811#define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
1812
john stultz4c7ee8d2006-09-30 23:28:22 -07001813void time_interpolator_update(long delta_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
1815 u64 counter;
1816 unsigned long offset;
1817
1818 /* If there is no time interpolator set up then do nothing */
1819 if (!time_interpolator)
1820 return;
1821
Andrew Mortona5a0d522005-10-30 15:01:42 -08001822 /*
1823 * The interpolator compensates for late ticks by accumulating the late
1824 * time in time_interpolator->offset. A tick earlier than expected will
1825 * lead to a reset of the offset and a corresponding jump of the clock
1826 * forward. Again this only works if the interpolator clock is running
1827 * slightly slower than the regular clock and the tuning logic insures
1828 * that.
1829 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Alex Williamson486d46ae2005-09-06 15:17:04 -07001831 counter = time_interpolator_get_counter(1);
Andrew Mortona5a0d522005-10-30 15:01:42 -08001832 offset = time_interpolator->offset +
1833 GET_TI_NSECS(counter, time_interpolator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
1836 time_interpolator->offset = offset - delta_nsec;
1837 else {
1838 time_interpolator->skips++;
1839 time_interpolator->ns_skipped += delta_nsec - offset;
1840 time_interpolator->offset = 0;
1841 }
1842 time_interpolator->last_counter = counter;
1843
1844 /* Tuning logic for time interpolator invoked every minute or so.
1845 * Decrease interpolator clock speed if no skips occurred and an offset is carried.
1846 * Increase interpolator clock speed if we skip too much time.
1847 */
1848 if (jiffies % INTERPOLATOR_ADJUST == 0)
1849 {
Jordan Hargraveb20367a2006-04-07 19:50:18 +02001850 if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 time_interpolator->nsec_per_cyc--;
1852 if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
1853 time_interpolator->nsec_per_cyc++;
1854 time_interpolator->skips = 0;
1855 time_interpolator->ns_skipped = 0;
1856 }
1857}
1858
1859static inline int
1860is_better_time_interpolator(struct time_interpolator *new)
1861{
1862 if (!time_interpolator)
1863 return 1;
1864 return new->frequency > 2*time_interpolator->frequency ||
1865 (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
1866}
1867
1868void
1869register_time_interpolator(struct time_interpolator *ti)
1870{
1871 unsigned long flags;
1872
1873 /* Sanity check */
Eric Sesterhenn9f312522006-04-02 13:45:55 +02001874 BUG_ON(ti->frequency == 0 || ti->mask == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
1877 spin_lock(&time_interpolator_lock);
1878 write_seqlock_irqsave(&xtime_lock, flags);
1879 if (is_better_time_interpolator(ti)) {
1880 time_interpolator = ti;
1881 time_interpolator_reset();
1882 }
1883 write_sequnlock_irqrestore(&xtime_lock, flags);
1884
1885 ti->next = time_interpolator_list;
1886 time_interpolator_list = ti;
1887 spin_unlock(&time_interpolator_lock);
1888}
1889
1890void
1891unregister_time_interpolator(struct time_interpolator *ti)
1892{
1893 struct time_interpolator *curr, **prev;
1894 unsigned long flags;
1895
1896 spin_lock(&time_interpolator_lock);
1897 prev = &time_interpolator_list;
1898 for (curr = *prev; curr; curr = curr->next) {
1899 if (curr == ti) {
1900 *prev = curr->next;
1901 break;
1902 }
1903 prev = &curr->next;
1904 }
1905
1906 write_seqlock_irqsave(&xtime_lock, flags);
1907 if (ti == time_interpolator) {
1908 /* we lost the best time-interpolator: */
1909 time_interpolator = NULL;
1910 /* find the next-best interpolator */
1911 for (curr = time_interpolator_list; curr; curr = curr->next)
1912 if (is_better_time_interpolator(curr))
1913 time_interpolator = curr;
1914 time_interpolator_reset();
1915 }
1916 write_sequnlock_irqrestore(&xtime_lock, flags);
1917 spin_unlock(&time_interpolator_lock);
1918}
1919#endif /* CONFIG_TIME_INTERPOLATION */
1920
1921/**
1922 * msleep - sleep safely even with waitqueue interruptions
1923 * @msecs: Time in milliseconds to sleep for
1924 */
1925void msleep(unsigned int msecs)
1926{
1927 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1928
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001929 while (timeout)
1930 timeout = schedule_timeout_uninterruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
1933EXPORT_SYMBOL(msleep);
1934
1935/**
Domen Puncer96ec3ef2005-06-25 14:58:43 -07001936 * msleep_interruptible - sleep waiting for signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 * @msecs: Time in milliseconds to sleep for
1938 */
1939unsigned long msleep_interruptible(unsigned int msecs)
1940{
1941 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1942
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001943 while (timeout && !signal_pending(current))
1944 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return jiffies_to_msecs(timeout);
1946}
1947
1948EXPORT_SYMBOL(msleep_interruptible);