blob: 8837737888361960307a2931177bd2a7d66631e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/timer.c
3 *
4 * Kernel internal timers, kernel timekeeping, basic process system calls
5 *
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 *
8 * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
9 *
10 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
11 * "A Kernel Model for Precision Timekeeping" by Dave Mills
12 * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
13 * serialize accesses to xtime/lost_ticks).
14 * Copyright (C) 1998 Andrea Arcangeli
15 * 1999-03-10 Improved NTP compatibility by Ulrich Windl
16 * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love
17 * 2000-10-05 Implemented scalable SMP per-CPU timer handling.
18 * Copyright (C) 2000, 2001, 2002 Ingo Molnar
19 * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
20 */
21
22#include <linux/kernel_stat.h>
23#include <linux/module.h>
24#include <linux/interrupt.h>
25#include <linux/percpu.h>
26#include <linux/init.h>
27#include <linux/mm.h>
28#include <linux/swap.h>
29#include <linux/notifier.h>
30#include <linux/thread_info.h>
31#include <linux/time.h>
32#include <linux/jiffies.h>
33#include <linux/posix-timers.h>
34#include <linux/cpu.h>
35#include <linux/syscalls.h>
Adrian Bunk97a41e22006-01-08 01:02:17 -080036#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/uaccess.h>
39#include <asm/unistd.h>
40#include <asm/div64.h>
41#include <asm/timex.h>
42#include <asm/io.h>
43
44#ifdef CONFIG_TIME_INTERPOLATION
45static void time_interpolator_update(long delta_nsec);
46#else
47#define time_interpolator_update(x)
48#endif
49
Thomas Gleixnerecea8d12005-10-30 15:03:00 -080050u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
51
52EXPORT_SYMBOL(jiffies_64);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
55 * per-CPU timer vector definitions:
56 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
58#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
59#define TVN_SIZE (1 << TVN_BITS)
60#define TVR_SIZE (1 << TVR_BITS)
61#define TVN_MASK (TVN_SIZE - 1)
62#define TVR_MASK (TVR_SIZE - 1)
63
64typedef struct tvec_s {
65 struct list_head vec[TVN_SIZE];
66} tvec_t;
67
68typedef struct tvec_root_s {
69 struct list_head vec[TVR_SIZE];
70} tvec_root_t;
71
72struct tvec_t_base_s {
Oleg Nesterov3691c512006-03-31 02:30:30 -080073 spinlock_t lock;
74 struct timer_list *running_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 unsigned long timer_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 tvec_root_t tv1;
77 tvec_t tv2;
78 tvec_t tv3;
79 tvec_t tv4;
80 tvec_t tv5;
81} ____cacheline_aligned_in_smp;
82
83typedef struct tvec_t_base_s tvec_base_t;
Andrew Mortonba6edfc2006-04-10 22:53:58 -070084
Oleg Nesterov3691c512006-03-31 02:30:30 -080085tvec_base_t boot_tvec_bases;
86EXPORT_SYMBOL(boot_tvec_bases);
Andrew Mortonba6edfc2006-04-10 22:53:58 -070087static DEFINE_PER_CPU(tvec_base_t *, tvec_bases) = { &boot_tvec_bases };
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89static inline void set_running_timer(tvec_base_t *base,
90 struct timer_list *timer)
91{
92#ifdef CONFIG_SMP
Oleg Nesterov3691c512006-03-31 02:30:30 -080093 base->running_timer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#endif
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
98{
99 unsigned long expires = timer->expires;
100 unsigned long idx = expires - base->timer_jiffies;
101 struct list_head *vec;
102
103 if (idx < TVR_SIZE) {
104 int i = expires & TVR_MASK;
105 vec = base->tv1.vec + i;
106 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
107 int i = (expires >> TVR_BITS) & TVN_MASK;
108 vec = base->tv2.vec + i;
109 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
110 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
111 vec = base->tv3.vec + i;
112 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
113 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
114 vec = base->tv4.vec + i;
115 } else if ((signed long) idx < 0) {
116 /*
117 * Can happen if you add a timer with expires == jiffies,
118 * or you set a timer to go off in the past
119 */
120 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
121 } else {
122 int i;
123 /* If the timeout is larger than 0xffffffff on 64-bit
124 * architectures then we use the maximum timeout:
125 */
126 if (idx > 0xffffffffUL) {
127 idx = 0xffffffffUL;
128 expires = idx + base->timer_jiffies;
129 }
130 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
131 vec = base->tv5.vec + i;
132 }
133 /*
134 * Timers are FIFO:
135 */
136 list_add_tail(&timer->entry, vec);
137}
138
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700139/***
140 * init_timer - initialize a timer.
141 * @timer: the timer to be initialized
142 *
143 * init_timer() must be done to a timer prior calling *any* of the
144 * other timer functions.
145 */
146void fastcall init_timer(struct timer_list *timer)
147{
148 timer->entry.next = NULL;
Oleg Nesterov3691c512006-03-31 02:30:30 -0800149 timer->base = per_cpu(tvec_bases, raw_smp_processor_id());
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700150}
151EXPORT_SYMBOL(init_timer);
152
153static inline void detach_timer(struct timer_list *timer,
154 int clear_pending)
155{
156 struct list_head *entry = &timer->entry;
157
158 __list_del(entry->prev, entry->next);
159 if (clear_pending)
160 entry->next = NULL;
161 entry->prev = LIST_POISON2;
162}
163
164/*
Oleg Nesterov3691c512006-03-31 02:30:30 -0800165 * We are using hashed locking: holding per_cpu(tvec_bases).lock
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700166 * means that all timers which are tied to this base via timer->base are
167 * locked, and the base itself is locked too.
168 *
169 * So __run_timers/migrate_timers can safely modify all timers which could
170 * be found on ->tvX lists.
171 *
172 * When the timer's base is locked, and the timer removed from list, it is
173 * possible to set timer->base = NULL and drop the lock: the timer remains
174 * locked.
175 */
Oleg Nesterov3691c512006-03-31 02:30:30 -0800176static tvec_base_t *lock_timer_base(struct timer_list *timer,
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700177 unsigned long *flags)
178{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800179 tvec_base_t *base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700180
181 for (;;) {
182 base = timer->base;
183 if (likely(base != NULL)) {
184 spin_lock_irqsave(&base->lock, *flags);
185 if (likely(base == timer->base))
186 return base;
187 /* The timer has migrated to another CPU */
188 spin_unlock_irqrestore(&base->lock, *flags);
189 }
190 cpu_relax();
191 }
192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194int __mod_timer(struct timer_list *timer, unsigned long expires)
195{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800196 tvec_base_t *base, *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 unsigned long flags;
198 int ret = 0;
199
200 BUG_ON(!timer->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700202 base = lock_timer_base(timer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700204 if (timer_pending(timer)) {
205 detach_timer(timer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 ret = 1;
207 }
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700208
Jan Beulicha4a61982006-03-24 03:15:54 -0800209 new_base = __get_cpu_var(tvec_bases);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700210
Oleg Nesterov3691c512006-03-31 02:30:30 -0800211 if (base != new_base) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700212 /*
213 * We are trying to schedule the timer on the local CPU.
214 * However we can't change timer's base while it is running,
215 * otherwise del_timer_sync() can't detect that the timer's
216 * handler yet has not finished. This also guarantees that
217 * the timer is serialized wrt itself.
218 */
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800219 if (likely(base->running_timer != timer)) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700220 /* See the comment in lock_timer_base() */
221 timer->base = NULL;
222 spin_unlock(&base->lock);
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800223 base = new_base;
224 spin_lock(&base->lock);
225 timer->base = base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700226 }
227 }
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 timer->expires = expires;
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800230 internal_add_timer(base, timer);
231 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 return ret;
234}
235
236EXPORT_SYMBOL(__mod_timer);
237
238/***
239 * add_timer_on - start a timer on a particular CPU
240 * @timer: the timer to be added
241 * @cpu: the CPU to start it on
242 *
243 * This is not very scalable on SMP. Double adds are not possible.
244 */
245void add_timer_on(struct timer_list *timer, int cpu)
246{
Jan Beulicha4a61982006-03-24 03:15:54 -0800247 tvec_base_t *base = per_cpu(tvec_bases, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 BUG_ON(timer_pending(timer) || !timer->function);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800251 spin_lock_irqsave(&base->lock, flags);
252 timer->base = base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 internal_add_timer(base, timer);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800254 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257
258/***
259 * mod_timer - modify a timer's timeout
260 * @timer: the timer to be modified
261 *
262 * mod_timer is a more efficient way to update the expire field of an
263 * active timer (if the timer is inactive it will be activated)
264 *
265 * mod_timer(timer, expires) is equivalent to:
266 *
267 * del_timer(timer); timer->expires = expires; add_timer(timer);
268 *
269 * Note that if there are multiple unserialized concurrent users of the
270 * same timer, then mod_timer() is the only safe way to modify the timeout,
271 * since add_timer() cannot modify an already running timer.
272 *
273 * The function returns whether it has modified a pending timer or not.
274 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
275 * active timer returns 1.)
276 */
277int mod_timer(struct timer_list *timer, unsigned long expires)
278{
279 BUG_ON(!timer->function);
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /*
282 * This is a common optimization triggered by the
283 * networking code - if the timer is re-modified
284 * to be the same thing then just return:
285 */
286 if (timer->expires == expires && timer_pending(timer))
287 return 1;
288
289 return __mod_timer(timer, expires);
290}
291
292EXPORT_SYMBOL(mod_timer);
293
294/***
295 * del_timer - deactive a timer.
296 * @timer: the timer to be deactivated
297 *
298 * del_timer() deactivates a timer - this works on both active and inactive
299 * timers.
300 *
301 * The function returns whether it has deactivated a pending timer or not.
302 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
303 * active timer returns 1.)
304 */
305int del_timer(struct timer_list *timer)
306{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800307 tvec_base_t *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700309 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700311 if (timer_pending(timer)) {
312 base = lock_timer_base(timer, &flags);
313 if (timer_pending(timer)) {
314 detach_timer(timer, 1);
315 ret = 1;
316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700320 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323EXPORT_SYMBOL(del_timer);
324
325#ifdef CONFIG_SMP
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700326/*
327 * This function tries to deactivate a timer. Upon successful (ret >= 0)
328 * exit the timer is not queued and the handler is not running on any CPU.
329 *
330 * It must not be called from interrupt contexts.
331 */
332int try_to_del_timer_sync(struct timer_list *timer)
333{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800334 tvec_base_t *base;
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700335 unsigned long flags;
336 int ret = -1;
337
338 base = lock_timer_base(timer, &flags);
339
340 if (base->running_timer == timer)
341 goto out;
342
343 ret = 0;
344 if (timer_pending(timer)) {
345 detach_timer(timer, 1);
346 ret = 1;
347 }
348out:
349 spin_unlock_irqrestore(&base->lock, flags);
350
351 return ret;
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/***
355 * del_timer_sync - deactivate a timer and wait for the handler to finish.
356 * @timer: the timer to be deactivated
357 *
358 * This function only differs from del_timer() on SMP: besides deactivating
359 * the timer it also makes sure the handler has finished executing on other
360 * CPUs.
361 *
362 * Synchronization rules: callers must prevent restarting of the timer,
363 * otherwise this function is meaningless. It must not be called from
364 * interrupt contexts. The caller must not hold locks which would prevent
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700365 * completion of the timer's handler. The timer's handler must not call
366 * add_timer_on(). Upon exit the timer is not queued and the handler is
367 * not running on any CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 *
369 * The function returns whether it has deactivated a pending timer or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
371int del_timer_sync(struct timer_list *timer)
372{
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700373 for (;;) {
374 int ret = try_to_del_timer_sync(timer);
375 if (ret >= 0)
376 return ret;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380EXPORT_SYMBOL(del_timer_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381#endif
382
383static int cascade(tvec_base_t *base, tvec_t *tv, int index)
384{
385 /* cascade all the timers from tv up one level */
386 struct list_head *head, *curr;
387
388 head = tv->vec + index;
389 curr = head->next;
390 /*
391 * We are removing _all_ timers from the list, so we don't have to
392 * detach them individually, just clear the list afterwards.
393 */
394 while (curr != head) {
395 struct timer_list *tmp;
396
397 tmp = list_entry(curr, struct timer_list, entry);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800398 BUG_ON(tmp->base != base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 curr = curr->next;
400 internal_add_timer(base, tmp);
401 }
402 INIT_LIST_HEAD(head);
403
404 return index;
405}
406
407/***
408 * __run_timers - run all expired timers (if any) on this CPU.
409 * @base: the timer vector to be processed.
410 *
411 * This function cascades all vectors and executes all expired timer
412 * vectors.
413 */
414#define INDEX(N) (base->timer_jiffies >> (TVR_BITS + N * TVN_BITS)) & TVN_MASK
415
416static inline void __run_timers(tvec_base_t *base)
417{
418 struct timer_list *timer;
419
Oleg Nesterov3691c512006-03-31 02:30:30 -0800420 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 while (time_after_eq(jiffies, base->timer_jiffies)) {
422 struct list_head work_list = LIST_HEAD_INIT(work_list);
423 struct list_head *head = &work_list;
424 int index = base->timer_jiffies & TVR_MASK;
425
426 /*
427 * Cascade timers:
428 */
429 if (!index &&
430 (!cascade(base, &base->tv2, INDEX(0))) &&
431 (!cascade(base, &base->tv3, INDEX(1))) &&
432 !cascade(base, &base->tv4, INDEX(2)))
433 cascade(base, &base->tv5, INDEX(3));
434 ++base->timer_jiffies;
435 list_splice_init(base->tv1.vec + index, &work_list);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700436 while (!list_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 void (*fn)(unsigned long);
438 unsigned long data;
439
440 timer = list_entry(head->next,struct timer_list,entry);
441 fn = timer->function;
442 data = timer->data;
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 set_running_timer(base, timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700445 detach_timer(timer, 1);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800446 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700448 int preempt_count = preempt_count();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 fn(data);
450 if (preempt_count != preempt_count()) {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700451 printk(KERN_WARNING "huh, entered %p "
452 "with preempt_count %08x, exited"
453 " with %08x?\n",
454 fn, preempt_count,
455 preempt_count());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 BUG();
457 }
458 }
Oleg Nesterov3691c512006-03-31 02:30:30 -0800459 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461 }
462 set_running_timer(base, NULL);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800463 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466#ifdef CONFIG_NO_IDLE_HZ
467/*
468 * Find out when the next timer event is due to happen. This
469 * is used on S/390 to stop all activity when a cpus is idle.
470 * This functions needs to be called disabled.
471 */
472unsigned long next_timer_interrupt(void)
473{
474 tvec_base_t *base;
475 struct list_head *list;
476 struct timer_list *nte;
477 unsigned long expires;
Tony Lindgren69239742006-03-06 15:42:45 -0800478 unsigned long hr_expires = MAX_JIFFY_OFFSET;
479 ktime_t hr_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 tvec_t *varray[4];
481 int i, j;
482
Tony Lindgren69239742006-03-06 15:42:45 -0800483 hr_delta = hrtimer_get_next_event();
484 if (hr_delta.tv64 != KTIME_MAX) {
485 struct timespec tsdelta;
486 tsdelta = ktime_to_timespec(hr_delta);
487 hr_expires = timespec_to_jiffies(&tsdelta);
488 if (hr_expires < 3)
489 return hr_expires + jiffies;
490 }
491 hr_expires += jiffies;
492
Jan Beulicha4a61982006-03-24 03:15:54 -0800493 base = __get_cpu_var(tvec_bases);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800494 spin_lock(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 expires = base->timer_jiffies + (LONG_MAX >> 1);
Al Viro53f087f2006-02-01 05:56:41 -0500496 list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* Look for timer events in tv1. */
499 j = base->timer_jiffies & TVR_MASK;
500 do {
501 list_for_each_entry(nte, base->tv1.vec + j, entry) {
502 expires = nte->expires;
503 if (j < (base->timer_jiffies & TVR_MASK))
504 list = base->tv2.vec + (INDEX(0));
505 goto found;
506 }
507 j = (j + 1) & TVR_MASK;
508 } while (j != (base->timer_jiffies & TVR_MASK));
509
510 /* Check tv2-tv5. */
511 varray[0] = &base->tv2;
512 varray[1] = &base->tv3;
513 varray[2] = &base->tv4;
514 varray[3] = &base->tv5;
515 for (i = 0; i < 4; i++) {
516 j = INDEX(i);
517 do {
518 if (list_empty(varray[i]->vec + j)) {
519 j = (j + 1) & TVN_MASK;
520 continue;
521 }
522 list_for_each_entry(nte, varray[i]->vec + j, entry)
523 if (time_before(nte->expires, expires))
524 expires = nte->expires;
525 if (j < (INDEX(i)) && i < 3)
526 list = varray[i + 1]->vec + (INDEX(i + 1));
527 goto found;
528 } while (j != (INDEX(i)));
529 }
530found:
531 if (list) {
532 /*
533 * The search wrapped. We need to look at the next list
534 * from next tv element that would cascade into tv element
535 * where we found the timer element.
536 */
537 list_for_each_entry(nte, list, entry) {
538 if (time_before(nte->expires, expires))
539 expires = nte->expires;
540 }
541 }
Oleg Nesterov3691c512006-03-31 02:30:30 -0800542 spin_unlock(&base->lock);
Tony Lindgren69239742006-03-06 15:42:45 -0800543
544 if (time_before(hr_expires, expires))
545 return hr_expires;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return expires;
548}
549#endif
550
551/******************************************************************/
552
553/*
554 * Timekeeping variables
555 */
556unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
557unsigned long tick_nsec = TICK_NSEC; /* ACTHZ period (nsec) */
558
559/*
560 * The current time
561 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
562 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
563 * at zero at system boot time, so wall_to_monotonic will be negative,
564 * however, we will ALWAYS keep the tv_nsec part positive so we can use
565 * the usual normalization.
566 */
567struct timespec xtime __attribute__ ((aligned (16)));
568struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
569
570EXPORT_SYMBOL(xtime);
571
572/* Don't completely fail for HZ > 500. */
573int tickadj = 500/HZ ? : 1; /* microsecs */
574
575
576/*
577 * phase-lock loop variables
578 */
579/* TIME_ERROR prevents overwriting the CMOS clock */
580int time_state = TIME_OK; /* clock synchronization status */
581int time_status = STA_UNSYNC; /* clock status bits */
582long time_offset; /* time adjustment (us) */
583long time_constant = 2; /* pll time constant */
584long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
585long time_precision = 1; /* clock precision (us) */
586long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
587long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
588static long time_phase; /* phase offset (scaled us) */
589long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
590 /* frequency offset (scaled ppm)*/
591static long time_adj; /* tick adjust (scaled 1 / HZ) */
592long time_reftime; /* time at last adjustment (s) */
593long time_adjust;
594long time_next_adjust;
595
596/*
597 * this routine handles the overflow of the microsecond field
598 *
599 * The tricky bits of code to handle the accurate clock support
600 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
601 * They were originally developed for SUN and DEC kernels.
602 * All the kudos should go to Dave for this stuff.
603 *
604 */
605static void second_overflow(void)
606{
Andrew Mortona5a0d522005-10-30 15:01:42 -0800607 long ltemp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Andrew Mortona5a0d522005-10-30 15:01:42 -0800609 /* Bump the maxerror field */
610 time_maxerror += time_tolerance >> SHIFT_USEC;
611 if (time_maxerror > NTP_PHASE_LIMIT) {
612 time_maxerror = NTP_PHASE_LIMIT;
613 time_status |= STA_UNSYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Andrew Mortona5a0d522005-10-30 15:01:42 -0800616 /*
617 * Leap second processing. If in leap-insert state at the end of the
618 * day, the system clock is set back one second; if in leap-delete
619 * state, the system clock is set ahead one second. The microtime()
620 * routine or external clock driver will insure that reported time is
621 * always monotonic. The ugly divides should be replaced.
622 */
623 switch (time_state) {
624 case TIME_OK:
625 if (time_status & STA_INS)
626 time_state = TIME_INS;
627 else if (time_status & STA_DEL)
628 time_state = TIME_DEL;
629 break;
630 case TIME_INS:
631 if (xtime.tv_sec % 86400 == 0) {
632 xtime.tv_sec--;
633 wall_to_monotonic.tv_sec++;
634 /*
635 * The timer interpolator will make time change
636 * gradually instead of an immediate jump by one second
637 */
638 time_interpolator_update(-NSEC_PER_SEC);
639 time_state = TIME_OOP;
640 clock_was_set();
641 printk(KERN_NOTICE "Clock: inserting leap second "
642 "23:59:60 UTC\n");
643 }
644 break;
645 case TIME_DEL:
646 if ((xtime.tv_sec + 1) % 86400 == 0) {
647 xtime.tv_sec++;
648 wall_to_monotonic.tv_sec--;
649 /*
650 * Use of time interpolator for a gradual change of
651 * time
652 */
653 time_interpolator_update(NSEC_PER_SEC);
654 time_state = TIME_WAIT;
655 clock_was_set();
656 printk(KERN_NOTICE "Clock: deleting leap second "
657 "23:59:59 UTC\n");
658 }
659 break;
660 case TIME_OOP:
661 time_state = TIME_WAIT;
662 break;
663 case TIME_WAIT:
664 if (!(time_status & (STA_INS | STA_DEL)))
665 time_state = TIME_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Andrew Mortona5a0d522005-10-30 15:01:42 -0800668 /*
669 * Compute the phase adjustment for the next second. In PLL mode, the
670 * offset is reduced by a fixed factor times the time constant. In FLL
671 * mode the offset is used directly. In either mode, the maximum phase
672 * adjustment for each second is clamped so as to spread the adjustment
673 * over not more than the number of seconds between updates.
674 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 ltemp = time_offset;
676 if (!(time_status & STA_FLL))
john stultz1bb34a42005-10-30 15:01:42 -0800677 ltemp = shift_right(ltemp, SHIFT_KG + time_constant);
678 ltemp = min(ltemp, (MAXPHASE / MINSEC) << SHIFT_UPDATE);
679 ltemp = max(ltemp, -(MAXPHASE / MINSEC) << SHIFT_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 time_offset -= ltemp;
681 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Andrew Mortona5a0d522005-10-30 15:01:42 -0800683 /*
684 * Compute the frequency estimate and additional phase adjustment due
Roman Zippel5ddcfa82006-03-25 03:08:28 -0800685 * to frequency error for the next second.
Andrew Mortona5a0d522005-10-30 15:01:42 -0800686 */
Roman Zippel5ddcfa82006-03-25 03:08:28 -0800687 ltemp = time_freq;
Andrew Mortona5a0d522005-10-30 15:01:42 -0800688 time_adj += shift_right(ltemp,(SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690#if HZ == 100
Andrew Mortona5a0d522005-10-30 15:01:42 -0800691 /*
692 * Compensate for (HZ==100) != (1 << SHIFT_HZ). Add 25% and 3.125% to
693 * get 128.125; => only 0.125% error (p. 14)
694 */
695 time_adj += shift_right(time_adj, 2) + shift_right(time_adj, 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696#endif
YOSHIFUJI Hideaki4b8f5732005-10-29 18:15:42 -0700697#if HZ == 250
Andrew Mortona5a0d522005-10-30 15:01:42 -0800698 /*
699 * Compensate for (HZ==250) != (1 << SHIFT_HZ). Add 1.5625% and
700 * 0.78125% to get 255.85938; => only 0.05% error (p. 14)
701 */
702 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
YOSHIFUJI Hideaki4b8f5732005-10-29 18:15:42 -0700703#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704#if HZ == 1000
Andrew Mortona5a0d522005-10-30 15:01:42 -0800705 /*
706 * Compensate for (HZ==1000) != (1 << SHIFT_HZ). Add 1.5625% and
707 * 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
708 */
709 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710#endif
711}
712
Paul Mackerras726c14b2006-02-17 10:30:23 +1100713/*
714 * Returns how many microseconds we need to add to xtime this tick
715 * in doing an adjustment requested with adjtime.
716 */
717static long adjtime_adjustment(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Paul Mackerras726c14b2006-02-17 10:30:23 +1100719 long time_adjust_step;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Paul Mackerras726c14b2006-02-17 10:30:23 +1100721 time_adjust_step = time_adjust;
722 if (time_adjust_step) {
Andrew Mortona5a0d522005-10-30 15:01:42 -0800723 /*
724 * We are doing an adjtime thing. Prepare time_adjust_step to
725 * be within bounds. Note that a positive time_adjust means we
726 * want the clock to run faster.
727 *
728 * Limit the amount of the step to be in the range
729 * -tickadj .. +tickadj
730 */
731 time_adjust_step = min(time_adjust_step, (long)tickadj);
732 time_adjust_step = max(time_adjust_step, (long)-tickadj);
Paul Mackerras726c14b2006-02-17 10:30:23 +1100733 }
734 return time_adjust_step;
735}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Paul Mackerras726c14b2006-02-17 10:30:23 +1100737/* in the NTP reference this is called "hardclock()" */
738static void update_wall_time_one_tick(void)
739{
740 long time_adjust_step, delta_nsec;
741
742 time_adjust_step = adjtime_adjustment();
743 if (time_adjust_step)
Andrew Mortona5a0d522005-10-30 15:01:42 -0800744 /* Reduce by this step the amount of time left */
745 time_adjust -= time_adjust_step;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 delta_nsec = tick_nsec + time_adjust_step * 1000;
747 /*
748 * Advance the phase, once it gets to one microsecond, then
749 * advance the tick more.
750 */
751 time_phase += time_adj;
john stultz1bb34a42005-10-30 15:01:42 -0800752 if ((time_phase >= FINENSEC) || (time_phase <= -FINENSEC)) {
753 long ltemp = shift_right(time_phase, (SHIFT_SCALE - 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 time_phase -= ltemp << (SHIFT_SCALE - 10);
755 delta_nsec += ltemp;
756 }
757 xtime.tv_nsec += delta_nsec;
758 time_interpolator_update(delta_nsec);
759
760 /* Changes by adjtime() do not take effect till next tick. */
761 if (time_next_adjust != 0) {
762 time_adjust = time_next_adjust;
763 time_next_adjust = 0;
764 }
765}
766
767/*
Paul Mackerras726c14b2006-02-17 10:30:23 +1100768 * Return how long ticks are at the moment, that is, how much time
769 * update_wall_time_one_tick will add to xtime next time we call it
770 * (assuming no calls to do_adjtimex in the meantime).
771 * The return value is in fixed-point nanoseconds with SHIFT_SCALE-10
772 * bits to the right of the binary point.
773 * This function has no side-effects.
774 */
775u64 current_tick_length(void)
776{
777 long delta_nsec;
778
779 delta_nsec = tick_nsec + adjtime_adjustment() * 1000;
780 return ((u64) delta_nsec << (SHIFT_SCALE - 10)) + time_adj;
781}
782
783/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * Using a loop looks inefficient, but "ticks" is
785 * usually just one (we shouldn't be losing ticks,
786 * we're doing this this way mainly for interrupt
787 * latency reasons, not because we think we'll
788 * have lots of lost timer ticks
789 */
790static void update_wall_time(unsigned long ticks)
791{
792 do {
793 ticks--;
794 update_wall_time_one_tick();
795 if (xtime.tv_nsec >= 1000000000) {
796 xtime.tv_nsec -= 1000000000;
797 xtime.tv_sec++;
798 second_overflow();
799 }
800 } while (ticks);
801}
802
803/*
804 * Called from the timer interrupt handler to charge one tick to the current
805 * process. user_tick is 1 if the tick is user time, 0 for system.
806 */
807void update_process_times(int user_tick)
808{
809 struct task_struct *p = current;
810 int cpu = smp_processor_id();
811
812 /* Note: this timer irq context must be accounted for as well. */
813 if (user_tick)
814 account_user_time(p, jiffies_to_cputime(1));
815 else
816 account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
817 run_local_timers();
818 if (rcu_pending(cpu))
819 rcu_check_callbacks(cpu, user_tick);
820 scheduler_tick();
821 run_posix_cpu_timers(p);
822}
823
824/*
825 * Nr of active tasks - counted in fixed-point numbers
826 */
827static unsigned long count_active_tasks(void)
828{
Jack Steinerdb1b1fe2006-03-31 02:31:21 -0800829 return nr_active() * FIXED_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
832/*
833 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
834 * imply that avenrun[] is the standard name for this kind of thing.
835 * Nothing else seems to be standardized: the fractional size etc
836 * all seem to differ on different machines.
837 *
838 * Requires xtime_lock to access.
839 */
840unsigned long avenrun[3];
841
842EXPORT_SYMBOL(avenrun);
843
844/*
845 * calc_load - given tick count, update the avenrun load estimates.
846 * This is called while holding a write_lock on xtime_lock.
847 */
848static inline void calc_load(unsigned long ticks)
849{
850 unsigned long active_tasks; /* fixed-point */
851 static int count = LOAD_FREQ;
852
853 count -= ticks;
854 if (count < 0) {
855 count += LOAD_FREQ;
856 active_tasks = count_active_tasks();
857 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
858 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
859 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
860 }
861}
862
863/* jiffies at the most recent update of wall time */
864unsigned long wall_jiffies = INITIAL_JIFFIES;
865
866/*
867 * This read-write spinlock protects us from races in SMP while
868 * playing with xtime and avenrun.
869 */
870#ifndef ARCH_HAVE_XTIME_LOCK
871seqlock_t xtime_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
872
873EXPORT_SYMBOL(xtime_lock);
874#endif
875
876/*
877 * This function runs timers and the timer-tq in bottom half context.
878 */
879static void run_timer_softirq(struct softirq_action *h)
880{
Jan Beulicha4a61982006-03-24 03:15:54 -0800881 tvec_base_t *base = __get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800883 hrtimer_run_queues();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (time_after_eq(jiffies, base->timer_jiffies))
885 __run_timers(base);
886}
887
888/*
889 * Called by the local, per-CPU timer interrupt on SMP.
890 */
891void run_local_timers(void)
892{
893 raise_softirq(TIMER_SOFTIRQ);
Ingo Molnar6687a972006-03-24 03:18:41 -0800894 softlockup_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
897/*
898 * Called by the timer interrupt. xtime_lock must already be taken
899 * by the timer IRQ!
900 */
901static inline void update_times(void)
902{
903 unsigned long ticks;
904
905 ticks = jiffies - wall_jiffies;
906 if (ticks) {
907 wall_jiffies += ticks;
908 update_wall_time(ticks);
909 }
910 calc_load(ticks);
911}
912
913/*
914 * The 64-bit jiffies value is not atomic - you MUST NOT read it
915 * without sampling the sequence number in xtime_lock.
916 * jiffies is defined in the linker script...
917 */
918
919void do_timer(struct pt_regs *regs)
920{
921 jiffies_64++;
Atsushi Nemoto5aee4052006-03-06 15:42:51 -0800922 /* prevent loading jiffies before storing new jiffies_64 value. */
923 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 update_times();
925}
926
927#ifdef __ARCH_WANT_SYS_ALARM
928
929/*
930 * For backwards compatibility? This can be done in libc so Alpha
931 * and all newer ports shouldn't need it.
932 */
933asmlinkage unsigned long sys_alarm(unsigned int seconds)
934{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -0800935 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938#endif
939
940#ifndef __alpha__
941
942/*
943 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
944 * should be moved into arch/i386 instead?
945 */
946
947/**
948 * sys_getpid - return the thread group id of the current process
949 *
950 * Note, despite the name, this returns the tgid not the pid. The tgid and
951 * the pid are identical unless CLONE_THREAD was specified on clone() in
952 * which case the tgid is the same in all threads of the same group.
953 *
954 * This is SMP safe as current->tgid does not change.
955 */
956asmlinkage long sys_getpid(void)
957{
958 return current->tgid;
959}
960
961/*
962 * Accessing ->group_leader->real_parent is not SMP-safe, it could
963 * change from under us. However, rather than getting any lock
964 * we can use an optimistic algorithm: get the parent
965 * pid, and go back and check that the parent is still
966 * the same. If it has changed (which is extremely unlikely
967 * indeed), we just try again..
968 *
969 * NOTE! This depends on the fact that even if we _do_
970 * get an old value of "parent", we can happily dereference
971 * the pointer (it was and remains a dereferencable kernel pointer
972 * no matter what): we just can't necessarily trust the result
973 * until we know that the parent pointer is valid.
974 *
975 * NOTE2: ->group_leader never changes from under us.
976 */
977asmlinkage long sys_getppid(void)
978{
979 int pid;
980 struct task_struct *me = current;
981 struct task_struct *parent;
982
983 parent = me->group_leader->real_parent;
984 for (;;) {
985 pid = parent->tgid;
David Meybohm4c5640c2005-08-22 13:11:08 -0700986#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 struct task_struct *old = parent;
989
990 /*
991 * Make sure we read the pid before re-reading the
992 * parent pointer:
993 */
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700994 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 parent = me->group_leader->real_parent;
996 if (old != parent)
997 continue;
998}
999#endif
1000 break;
1001 }
1002 return pid;
1003}
1004
1005asmlinkage long sys_getuid(void)
1006{
1007 /* Only we change this so SMP safe */
1008 return current->uid;
1009}
1010
1011asmlinkage long sys_geteuid(void)
1012{
1013 /* Only we change this so SMP safe */
1014 return current->euid;
1015}
1016
1017asmlinkage long sys_getgid(void)
1018{
1019 /* Only we change this so SMP safe */
1020 return current->gid;
1021}
1022
1023asmlinkage long sys_getegid(void)
1024{
1025 /* Only we change this so SMP safe */
1026 return current->egid;
1027}
1028
1029#endif
1030
1031static void process_timeout(unsigned long __data)
1032{
1033 wake_up_process((task_t *)__data);
1034}
1035
1036/**
1037 * schedule_timeout - sleep until timeout
1038 * @timeout: timeout value in jiffies
1039 *
1040 * Make the current task sleep until @timeout jiffies have
1041 * elapsed. The routine will return immediately unless
1042 * the current task state has been set (see set_current_state()).
1043 *
1044 * You can set the task state as follows -
1045 *
1046 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1047 * pass before the routine returns. The routine will return 0
1048 *
1049 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1050 * delivered to the current task. In this case the remaining time
1051 * in jiffies will be returned, or 0 if the timer expired in time
1052 *
1053 * The current task state is guaranteed to be TASK_RUNNING when this
1054 * routine returns.
1055 *
1056 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1057 * the CPU away without a bound on the timeout. In this case the return
1058 * value will be %MAX_SCHEDULE_TIMEOUT.
1059 *
1060 * In all cases the return value is guaranteed to be non-negative.
1061 */
1062fastcall signed long __sched schedule_timeout(signed long timeout)
1063{
1064 struct timer_list timer;
1065 unsigned long expire;
1066
1067 switch (timeout)
1068 {
1069 case MAX_SCHEDULE_TIMEOUT:
1070 /*
1071 * These two special cases are useful to be comfortable
1072 * in the caller. Nothing more. We could take
1073 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1074 * but I' d like to return a valid offset (>=0) to allow
1075 * the caller to do everything it want with the retval.
1076 */
1077 schedule();
1078 goto out;
1079 default:
1080 /*
1081 * Another bit of PARANOID. Note that the retval will be
1082 * 0 since no piece of kernel is supposed to do a check
1083 * for a negative retval of schedule_timeout() (since it
1084 * should never happens anyway). You just have the printk()
1085 * that will tell you if something is gone wrong and where.
1086 */
1087 if (timeout < 0)
1088 {
1089 printk(KERN_ERR "schedule_timeout: wrong timeout "
Andrew Mortona5a0d522005-10-30 15:01:42 -08001090 "value %lx from %p\n", timeout,
1091 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 current->state = TASK_RUNNING;
1093 goto out;
1094 }
1095 }
1096
1097 expire = timeout + jiffies;
1098
Oleg Nesterova8db2db2005-10-30 15:01:38 -08001099 setup_timer(&timer, process_timeout, (unsigned long)current);
1100 __mod_timer(&timer, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 schedule();
1102 del_singleshot_timer_sync(&timer);
1103
1104 timeout = expire - jiffies;
1105
1106 out:
1107 return timeout < 0 ? 0 : timeout;
1108}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109EXPORT_SYMBOL(schedule_timeout);
1110
Andrew Morton8a1c1752005-09-13 01:25:15 -07001111/*
1112 * We can use __set_current_state() here because schedule_timeout() calls
1113 * schedule() unconditionally.
1114 */
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001115signed long __sched schedule_timeout_interruptible(signed long timeout)
1116{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001117 __set_current_state(TASK_INTERRUPTIBLE);
1118 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001119}
1120EXPORT_SYMBOL(schedule_timeout_interruptible);
1121
1122signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1123{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001124 __set_current_state(TASK_UNINTERRUPTIBLE);
1125 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001126}
1127EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129/* Thread ID - the internal kernel "pid" */
1130asmlinkage long sys_gettid(void)
1131{
1132 return current->pid;
1133}
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135/*
1136 * sys_sysinfo - fill in sysinfo struct
1137 */
1138asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1139{
1140 struct sysinfo val;
1141 unsigned long mem_total, sav_total;
1142 unsigned int mem_unit, bitcount;
1143 unsigned long seq;
1144
1145 memset((char *)&val, 0, sizeof(struct sysinfo));
1146
1147 do {
1148 struct timespec tp;
1149 seq = read_seqbegin(&xtime_lock);
1150
1151 /*
1152 * This is annoying. The below is the same thing
1153 * posix_get_clock_monotonic() does, but it wants to
1154 * take the lock which we want to cover the loads stuff
1155 * too.
1156 */
1157
1158 getnstimeofday(&tp);
1159 tp.tv_sec += wall_to_monotonic.tv_sec;
1160 tp.tv_nsec += wall_to_monotonic.tv_nsec;
1161 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1162 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1163 tp.tv_sec++;
1164 }
1165 val.uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1166
1167 val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1168 val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1169 val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
1170
1171 val.procs = nr_threads;
1172 } while (read_seqretry(&xtime_lock, seq));
1173
1174 si_meminfo(&val);
1175 si_swapinfo(&val);
1176
1177 /*
1178 * If the sum of all the available memory (i.e. ram + swap)
1179 * is less than can be stored in a 32 bit unsigned long then
1180 * we can be binary compatible with 2.2.x kernels. If not,
1181 * well, in that case 2.2.x was broken anyways...
1182 *
1183 * -Erik Andersen <andersee@debian.org>
1184 */
1185
1186 mem_total = val.totalram + val.totalswap;
1187 if (mem_total < val.totalram || mem_total < val.totalswap)
1188 goto out;
1189 bitcount = 0;
1190 mem_unit = val.mem_unit;
1191 while (mem_unit > 1) {
1192 bitcount++;
1193 mem_unit >>= 1;
1194 sav_total = mem_total;
1195 mem_total <<= 1;
1196 if (mem_total < sav_total)
1197 goto out;
1198 }
1199
1200 /*
1201 * If mem_total did not overflow, multiply all memory values by
1202 * val.mem_unit and set it to 1. This leaves things compatible
1203 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1204 * kernels...
1205 */
1206
1207 val.mem_unit = 1;
1208 val.totalram <<= bitcount;
1209 val.freeram <<= bitcount;
1210 val.sharedram <<= bitcount;
1211 val.bufferram <<= bitcount;
1212 val.totalswap <<= bitcount;
1213 val.freeswap <<= bitcount;
1214 val.totalhigh <<= bitcount;
1215 val.freehigh <<= bitcount;
1216
1217 out:
1218 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1219 return -EFAULT;
1220
1221 return 0;
1222}
1223
Jan Beulicha4a61982006-03-24 03:15:54 -08001224static int __devinit init_timers_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 int j;
1227 tvec_base_t *base;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001228 static char __devinitdata tvec_base_done[NR_CPUS];
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001229
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001230 if (!tvec_base_done[cpu]) {
Jan Beulicha4a61982006-03-24 03:15:54 -08001231 static char boot_done;
1232
Jan Beulicha4a61982006-03-24 03:15:54 -08001233 if (boot_done) {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001234 /*
1235 * The APs use this path later in boot
1236 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001237 base = kmalloc_node(sizeof(*base), GFP_KERNEL,
1238 cpu_to_node(cpu));
1239 if (!base)
1240 return -ENOMEM;
1241 memset(base, 0, sizeof(*base));
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001242 per_cpu(tvec_bases, cpu) = base;
Jan Beulicha4a61982006-03-24 03:15:54 -08001243 } else {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001244 /*
1245 * This is for the boot CPU - we use compile-time
1246 * static initialisation because per-cpu memory isn't
1247 * ready yet and because the memory allocators are not
1248 * initialised either.
1249 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001250 boot_done = 1;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001251 base = &boot_tvec_bases;
Jan Beulicha4a61982006-03-24 03:15:54 -08001252 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001253 tvec_base_done[cpu] = 1;
1254 } else {
1255 base = per_cpu(tvec_bases, cpu);
Jan Beulicha4a61982006-03-24 03:15:54 -08001256 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001257
Oleg Nesterov3691c512006-03-31 02:30:30 -08001258 spin_lock_init(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 for (j = 0; j < TVN_SIZE; j++) {
1260 INIT_LIST_HEAD(base->tv5.vec + j);
1261 INIT_LIST_HEAD(base->tv4.vec + j);
1262 INIT_LIST_HEAD(base->tv3.vec + j);
1263 INIT_LIST_HEAD(base->tv2.vec + j);
1264 }
1265 for (j = 0; j < TVR_SIZE; j++)
1266 INIT_LIST_HEAD(base->tv1.vec + j);
1267
1268 base->timer_jiffies = jiffies;
Jan Beulicha4a61982006-03-24 03:15:54 -08001269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001273static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 struct timer_list *timer;
1276
1277 while (!list_empty(head)) {
1278 timer = list_entry(head->next, struct timer_list, entry);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001279 detach_timer(timer, 0);
Oleg Nesterov3691c512006-03-31 02:30:30 -08001280 timer->base = new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 internal_add_timer(new_base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
1285static void __devinit migrate_timers(int cpu)
1286{
1287 tvec_base_t *old_base;
1288 tvec_base_t *new_base;
1289 int i;
1290
1291 BUG_ON(cpu_online(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001292 old_base = per_cpu(tvec_bases, cpu);
1293 new_base = get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 local_irq_disable();
Oleg Nesterov3691c512006-03-31 02:30:30 -08001296 spin_lock(&new_base->lock);
1297 spin_lock(&old_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Oleg Nesterov3691c512006-03-31 02:30:30 -08001299 BUG_ON(old_base->running_timer);
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 for (i = 0; i < TVR_SIZE; i++)
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001302 migrate_timer_list(new_base, old_base->tv1.vec + i);
1303 for (i = 0; i < TVN_SIZE; i++) {
1304 migrate_timer_list(new_base, old_base->tv2.vec + i);
1305 migrate_timer_list(new_base, old_base->tv3.vec + i);
1306 migrate_timer_list(new_base, old_base->tv4.vec + i);
1307 migrate_timer_list(new_base, old_base->tv5.vec + i);
1308 }
1309
Oleg Nesterov3691c512006-03-31 02:30:30 -08001310 spin_unlock(&old_base->lock);
1311 spin_unlock(&new_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 local_irq_enable();
1313 put_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315#endif /* CONFIG_HOTPLUG_CPU */
1316
1317static int __devinit timer_cpu_notify(struct notifier_block *self,
1318 unsigned long action, void *hcpu)
1319{
1320 long cpu = (long)hcpu;
1321 switch(action) {
1322 case CPU_UP_PREPARE:
Jan Beulicha4a61982006-03-24 03:15:54 -08001323 if (init_timers_cpu(cpu) < 0)
1324 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 break;
1326#ifdef CONFIG_HOTPLUG_CPU
1327 case CPU_DEAD:
1328 migrate_timers(cpu);
1329 break;
1330#endif
1331 default:
1332 break;
1333 }
1334 return NOTIFY_OK;
1335}
1336
1337static struct notifier_block __devinitdata timers_nb = {
1338 .notifier_call = timer_cpu_notify,
1339};
1340
1341
1342void __init init_timers(void)
1343{
1344 timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1345 (void *)(long)smp_processor_id());
1346 register_cpu_notifier(&timers_nb);
1347 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1348}
1349
1350#ifdef CONFIG_TIME_INTERPOLATION
1351
Christoph Lameter67890d72006-03-16 23:04:00 -08001352struct time_interpolator *time_interpolator __read_mostly;
1353static struct time_interpolator *time_interpolator_list __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354static DEFINE_SPINLOCK(time_interpolator_lock);
1355
1356static inline u64 time_interpolator_get_cycles(unsigned int src)
1357{
1358 unsigned long (*x)(void);
1359
1360 switch (src)
1361 {
1362 case TIME_SOURCE_FUNCTION:
1363 x = time_interpolator->addr;
1364 return x();
1365
1366 case TIME_SOURCE_MMIO64 :
Christoph Lameter685db652006-03-02 02:54:35 -08001367 return readq_relaxed((void __iomem *)time_interpolator->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 case TIME_SOURCE_MMIO32 :
Christoph Lameter685db652006-03-02 02:54:35 -08001370 return readl_relaxed((void __iomem *)time_interpolator->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 default: return get_cycles();
1373 }
1374}
1375
Alex Williamson486d46a2005-09-06 15:17:04 -07001376static inline u64 time_interpolator_get_counter(int writelock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 unsigned int src = time_interpolator->source;
1379
1380 if (time_interpolator->jitter)
1381 {
1382 u64 lcycle;
1383 u64 now;
1384
1385 do {
1386 lcycle = time_interpolator->last_cycle;
1387 now = time_interpolator_get_cycles(src);
1388 if (lcycle && time_after(lcycle, now))
1389 return lcycle;
Alex Williamson486d46a2005-09-06 15:17:04 -07001390
1391 /* When holding the xtime write lock, there's no need
1392 * to add the overhead of the cmpxchg. Readers are
1393 * force to retry until the write lock is released.
1394 */
1395 if (writelock) {
1396 time_interpolator->last_cycle = now;
1397 return now;
1398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /* Keep track of the last timer value returned. The use of cmpxchg here
1400 * will cause contention in an SMP environment.
1401 */
1402 } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
1403 return now;
1404 }
1405 else
1406 return time_interpolator_get_cycles(src);
1407}
1408
1409void time_interpolator_reset(void)
1410{
1411 time_interpolator->offset = 0;
Alex Williamson486d46a2005-09-06 15:17:04 -07001412 time_interpolator->last_counter = time_interpolator_get_counter(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415#define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
1416
1417unsigned long time_interpolator_get_offset(void)
1418{
1419 /* If we do not have a time interpolator set up then just return zero */
1420 if (!time_interpolator)
1421 return 0;
1422
1423 return time_interpolator->offset +
Alex Williamson486d46a2005-09-06 15:17:04 -07001424 GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
1427#define INTERPOLATOR_ADJUST 65536
1428#define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
1429
1430static void time_interpolator_update(long delta_nsec)
1431{
1432 u64 counter;
1433 unsigned long offset;
1434
1435 /* If there is no time interpolator set up then do nothing */
1436 if (!time_interpolator)
1437 return;
1438
Andrew Mortona5a0d522005-10-30 15:01:42 -08001439 /*
1440 * The interpolator compensates for late ticks by accumulating the late
1441 * time in time_interpolator->offset. A tick earlier than expected will
1442 * lead to a reset of the offset and a corresponding jump of the clock
1443 * forward. Again this only works if the interpolator clock is running
1444 * slightly slower than the regular clock and the tuning logic insures
1445 * that.
1446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Alex Williamson486d46a2005-09-06 15:17:04 -07001448 counter = time_interpolator_get_counter(1);
Andrew Mortona5a0d522005-10-30 15:01:42 -08001449 offset = time_interpolator->offset +
1450 GET_TI_NSECS(counter, time_interpolator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
1453 time_interpolator->offset = offset - delta_nsec;
1454 else {
1455 time_interpolator->skips++;
1456 time_interpolator->ns_skipped += delta_nsec - offset;
1457 time_interpolator->offset = 0;
1458 }
1459 time_interpolator->last_counter = counter;
1460
1461 /* Tuning logic for time interpolator invoked every minute or so.
1462 * Decrease interpolator clock speed if no skips occurred and an offset is carried.
1463 * Increase interpolator clock speed if we skip too much time.
1464 */
1465 if (jiffies % INTERPOLATOR_ADJUST == 0)
1466 {
Jordan Hargraveb20367a2006-04-07 19:50:18 +02001467 if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 time_interpolator->nsec_per_cyc--;
1469 if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
1470 time_interpolator->nsec_per_cyc++;
1471 time_interpolator->skips = 0;
1472 time_interpolator->ns_skipped = 0;
1473 }
1474}
1475
1476static inline int
1477is_better_time_interpolator(struct time_interpolator *new)
1478{
1479 if (!time_interpolator)
1480 return 1;
1481 return new->frequency > 2*time_interpolator->frequency ||
1482 (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
1483}
1484
1485void
1486register_time_interpolator(struct time_interpolator *ti)
1487{
1488 unsigned long flags;
1489
1490 /* Sanity check */
Eric Sesterhenn9f312522006-04-02 13:45:55 +02001491 BUG_ON(ti->frequency == 0 || ti->mask == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
1494 spin_lock(&time_interpolator_lock);
1495 write_seqlock_irqsave(&xtime_lock, flags);
1496 if (is_better_time_interpolator(ti)) {
1497 time_interpolator = ti;
1498 time_interpolator_reset();
1499 }
1500 write_sequnlock_irqrestore(&xtime_lock, flags);
1501
1502 ti->next = time_interpolator_list;
1503 time_interpolator_list = ti;
1504 spin_unlock(&time_interpolator_lock);
1505}
1506
1507void
1508unregister_time_interpolator(struct time_interpolator *ti)
1509{
1510 struct time_interpolator *curr, **prev;
1511 unsigned long flags;
1512
1513 spin_lock(&time_interpolator_lock);
1514 prev = &time_interpolator_list;
1515 for (curr = *prev; curr; curr = curr->next) {
1516 if (curr == ti) {
1517 *prev = curr->next;
1518 break;
1519 }
1520 prev = &curr->next;
1521 }
1522
1523 write_seqlock_irqsave(&xtime_lock, flags);
1524 if (ti == time_interpolator) {
1525 /* we lost the best time-interpolator: */
1526 time_interpolator = NULL;
1527 /* find the next-best interpolator */
1528 for (curr = time_interpolator_list; curr; curr = curr->next)
1529 if (is_better_time_interpolator(curr))
1530 time_interpolator = curr;
1531 time_interpolator_reset();
1532 }
1533 write_sequnlock_irqrestore(&xtime_lock, flags);
1534 spin_unlock(&time_interpolator_lock);
1535}
1536#endif /* CONFIG_TIME_INTERPOLATION */
1537
1538/**
1539 * msleep - sleep safely even with waitqueue interruptions
1540 * @msecs: Time in milliseconds to sleep for
1541 */
1542void msleep(unsigned int msecs)
1543{
1544 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1545
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001546 while (timeout)
1547 timeout = schedule_timeout_uninterruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
1550EXPORT_SYMBOL(msleep);
1551
1552/**
Domen Puncer96ec3ef2005-06-25 14:58:43 -07001553 * msleep_interruptible - sleep waiting for signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 * @msecs: Time in milliseconds to sleep for
1555 */
1556unsigned long msleep_interruptible(unsigned int msecs)
1557{
1558 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1559
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001560 while (timeout && !signal_pending(current))
1561 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return jiffies_to_msecs(timeout);
1563}
1564
1565EXPORT_SYMBOL(msleep_interruptible);