blob: 9062a82ee8ec40d9a6ef985c625bd8daaf5778c5 [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;
Jan Beulicha4a61982006-03-24 03:15:54 -080084static DEFINE_PER_CPU(tvec_base_t *, tvec_bases);
Oleg Nesterov3691c512006-03-31 02:30:30 -080085tvec_base_t boot_tvec_bases;
86EXPORT_SYMBOL(boot_tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static inline void set_running_timer(tvec_base_t *base,
89 struct timer_list *timer)
90{
91#ifdef CONFIG_SMP
Oleg Nesterov3691c512006-03-31 02:30:30 -080092 base->running_timer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#endif
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
97{
98 unsigned long expires = timer->expires;
99 unsigned long idx = expires - base->timer_jiffies;
100 struct list_head *vec;
101
102 if (idx < TVR_SIZE) {
103 int i = expires & TVR_MASK;
104 vec = base->tv1.vec + i;
105 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
106 int i = (expires >> TVR_BITS) & TVN_MASK;
107 vec = base->tv2.vec + i;
108 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
109 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
110 vec = base->tv3.vec + i;
111 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
112 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
113 vec = base->tv4.vec + i;
114 } else if ((signed long) idx < 0) {
115 /*
116 * Can happen if you add a timer with expires == jiffies,
117 * or you set a timer to go off in the past
118 */
119 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
120 } else {
121 int i;
122 /* If the timeout is larger than 0xffffffff on 64-bit
123 * architectures then we use the maximum timeout:
124 */
125 if (idx > 0xffffffffUL) {
126 idx = 0xffffffffUL;
127 expires = idx + base->timer_jiffies;
128 }
129 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
130 vec = base->tv5.vec + i;
131 }
132 /*
133 * Timers are FIFO:
134 */
135 list_add_tail(&timer->entry, vec);
136}
137
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700138/***
139 * init_timer - initialize a timer.
140 * @timer: the timer to be initialized
141 *
142 * init_timer() must be done to a timer prior calling *any* of the
143 * other timer functions.
144 */
145void fastcall init_timer(struct timer_list *timer)
146{
147 timer->entry.next = NULL;
Oleg Nesterov3691c512006-03-31 02:30:30 -0800148 timer->base = per_cpu(tvec_bases, raw_smp_processor_id());
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700149}
150EXPORT_SYMBOL(init_timer);
151
152static inline void detach_timer(struct timer_list *timer,
153 int clear_pending)
154{
155 struct list_head *entry = &timer->entry;
156
157 __list_del(entry->prev, entry->next);
158 if (clear_pending)
159 entry->next = NULL;
160 entry->prev = LIST_POISON2;
161}
162
163/*
Oleg Nesterov3691c512006-03-31 02:30:30 -0800164 * We are using hashed locking: holding per_cpu(tvec_bases).lock
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700165 * means that all timers which are tied to this base via timer->base are
166 * locked, and the base itself is locked too.
167 *
168 * So __run_timers/migrate_timers can safely modify all timers which could
169 * be found on ->tvX lists.
170 *
171 * When the timer's base is locked, and the timer removed from list, it is
172 * possible to set timer->base = NULL and drop the lock: the timer remains
173 * locked.
174 */
Oleg Nesterov3691c512006-03-31 02:30:30 -0800175static tvec_base_t *lock_timer_base(struct timer_list *timer,
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700176 unsigned long *flags)
177{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800178 tvec_base_t *base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700179
180 for (;;) {
181 base = timer->base;
182 if (likely(base != NULL)) {
183 spin_lock_irqsave(&base->lock, *flags);
184 if (likely(base == timer->base))
185 return base;
186 /* The timer has migrated to another CPU */
187 spin_unlock_irqrestore(&base->lock, *flags);
188 }
189 cpu_relax();
190 }
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193int __mod_timer(struct timer_list *timer, unsigned long expires)
194{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800195 tvec_base_t *base, *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 unsigned long flags;
197 int ret = 0;
198
199 BUG_ON(!timer->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700201 base = lock_timer_base(timer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700203 if (timer_pending(timer)) {
204 detach_timer(timer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 ret = 1;
206 }
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700207
Jan Beulicha4a61982006-03-24 03:15:54 -0800208 new_base = __get_cpu_var(tvec_bases);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700209
Oleg Nesterov3691c512006-03-31 02:30:30 -0800210 if (base != new_base) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700211 /*
212 * We are trying to schedule the timer on the local CPU.
213 * However we can't change timer's base while it is running,
214 * otherwise del_timer_sync() can't detect that the timer's
215 * handler yet has not finished. This also guarantees that
216 * the timer is serialized wrt itself.
217 */
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800218 if (likely(base->running_timer != timer)) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700219 /* See the comment in lock_timer_base() */
220 timer->base = NULL;
221 spin_unlock(&base->lock);
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800222 base = new_base;
223 spin_lock(&base->lock);
224 timer->base = base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700225 }
226 }
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 timer->expires = expires;
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800229 internal_add_timer(base, timer);
230 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 return ret;
233}
234
235EXPORT_SYMBOL(__mod_timer);
236
237/***
238 * add_timer_on - start a timer on a particular CPU
239 * @timer: the timer to be added
240 * @cpu: the CPU to start it on
241 *
242 * This is not very scalable on SMP. Double adds are not possible.
243 */
244void add_timer_on(struct timer_list *timer, int cpu)
245{
Jan Beulicha4a61982006-03-24 03:15:54 -0800246 tvec_base_t *base = per_cpu(tvec_bases, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 BUG_ON(timer_pending(timer) || !timer->function);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800250 spin_lock_irqsave(&base->lock, flags);
251 timer->base = base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 internal_add_timer(base, timer);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800253 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256
257/***
258 * mod_timer - modify a timer's timeout
259 * @timer: the timer to be modified
260 *
261 * mod_timer is a more efficient way to update the expire field of an
262 * active timer (if the timer is inactive it will be activated)
263 *
264 * mod_timer(timer, expires) is equivalent to:
265 *
266 * del_timer(timer); timer->expires = expires; add_timer(timer);
267 *
268 * Note that if there are multiple unserialized concurrent users of the
269 * same timer, then mod_timer() is the only safe way to modify the timeout,
270 * since add_timer() cannot modify an already running timer.
271 *
272 * The function returns whether it has modified a pending timer or not.
273 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
274 * active timer returns 1.)
275 */
276int mod_timer(struct timer_list *timer, unsigned long expires)
277{
278 BUG_ON(!timer->function);
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /*
281 * This is a common optimization triggered by the
282 * networking code - if the timer is re-modified
283 * to be the same thing then just return:
284 */
285 if (timer->expires == expires && timer_pending(timer))
286 return 1;
287
288 return __mod_timer(timer, expires);
289}
290
291EXPORT_SYMBOL(mod_timer);
292
293/***
294 * del_timer - deactive a timer.
295 * @timer: the timer to be deactivated
296 *
297 * del_timer() deactivates a timer - this works on both active and inactive
298 * timers.
299 *
300 * The function returns whether it has deactivated a pending timer or not.
301 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
302 * active timer returns 1.)
303 */
304int del_timer(struct timer_list *timer)
305{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800306 tvec_base_t *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700308 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700310 if (timer_pending(timer)) {
311 base = lock_timer_base(timer, &flags);
312 if (timer_pending(timer)) {
313 detach_timer(timer, 1);
314 ret = 1;
315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700319 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322EXPORT_SYMBOL(del_timer);
323
324#ifdef CONFIG_SMP
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700325/*
326 * This function tries to deactivate a timer. Upon successful (ret >= 0)
327 * exit the timer is not queued and the handler is not running on any CPU.
328 *
329 * It must not be called from interrupt contexts.
330 */
331int try_to_del_timer_sync(struct timer_list *timer)
332{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800333 tvec_base_t *base;
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700334 unsigned long flags;
335 int ret = -1;
336
337 base = lock_timer_base(timer, &flags);
338
339 if (base->running_timer == timer)
340 goto out;
341
342 ret = 0;
343 if (timer_pending(timer)) {
344 detach_timer(timer, 1);
345 ret = 1;
346 }
347out:
348 spin_unlock_irqrestore(&base->lock, flags);
349
350 return ret;
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/***
354 * del_timer_sync - deactivate a timer and wait for the handler to finish.
355 * @timer: the timer to be deactivated
356 *
357 * This function only differs from del_timer() on SMP: besides deactivating
358 * the timer it also makes sure the handler has finished executing on other
359 * CPUs.
360 *
361 * Synchronization rules: callers must prevent restarting of the timer,
362 * otherwise this function is meaningless. It must not be called from
363 * interrupt contexts. The caller must not hold locks which would prevent
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700364 * completion of the timer's handler. The timer's handler must not call
365 * add_timer_on(). Upon exit the timer is not queued and the handler is
366 * not running on any CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 *
368 * The function returns whether it has deactivated a pending timer or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 */
370int del_timer_sync(struct timer_list *timer)
371{
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700372 for (;;) {
373 int ret = try_to_del_timer_sync(timer);
374 if (ret >= 0)
375 return ret;
376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379EXPORT_SYMBOL(del_timer_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#endif
381
382static int cascade(tvec_base_t *base, tvec_t *tv, int index)
383{
384 /* cascade all the timers from tv up one level */
385 struct list_head *head, *curr;
386
387 head = tv->vec + index;
388 curr = head->next;
389 /*
390 * We are removing _all_ timers from the list, so we don't have to
391 * detach them individually, just clear the list afterwards.
392 */
393 while (curr != head) {
394 struct timer_list *tmp;
395
396 tmp = list_entry(curr, struct timer_list, entry);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800397 BUG_ON(tmp->base != base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 curr = curr->next;
399 internal_add_timer(base, tmp);
400 }
401 INIT_LIST_HEAD(head);
402
403 return index;
404}
405
406/***
407 * __run_timers - run all expired timers (if any) on this CPU.
408 * @base: the timer vector to be processed.
409 *
410 * This function cascades all vectors and executes all expired timer
411 * vectors.
412 */
413#define INDEX(N) (base->timer_jiffies >> (TVR_BITS + N * TVN_BITS)) & TVN_MASK
414
415static inline void __run_timers(tvec_base_t *base)
416{
417 struct timer_list *timer;
418
Oleg Nesterov3691c512006-03-31 02:30:30 -0800419 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 while (time_after_eq(jiffies, base->timer_jiffies)) {
421 struct list_head work_list = LIST_HEAD_INIT(work_list);
422 struct list_head *head = &work_list;
423 int index = base->timer_jiffies & TVR_MASK;
424
425 /*
426 * Cascade timers:
427 */
428 if (!index &&
429 (!cascade(base, &base->tv2, INDEX(0))) &&
430 (!cascade(base, &base->tv3, INDEX(1))) &&
431 !cascade(base, &base->tv4, INDEX(2)))
432 cascade(base, &base->tv5, INDEX(3));
433 ++base->timer_jiffies;
434 list_splice_init(base->tv1.vec + index, &work_list);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700435 while (!list_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 void (*fn)(unsigned long);
437 unsigned long data;
438
439 timer = list_entry(head->next,struct timer_list,entry);
440 fn = timer->function;
441 data = timer->data;
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 set_running_timer(base, timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700444 detach_timer(timer, 1);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800445 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700447 int preempt_count = preempt_count();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 fn(data);
449 if (preempt_count != preempt_count()) {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700450 printk(KERN_WARNING "huh, entered %p "
451 "with preempt_count %08x, exited"
452 " with %08x?\n",
453 fn, preempt_count,
454 preempt_count());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 BUG();
456 }
457 }
Oleg Nesterov3691c512006-03-31 02:30:30 -0800458 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460 }
461 set_running_timer(base, NULL);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800462 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
465#ifdef CONFIG_NO_IDLE_HZ
466/*
467 * Find out when the next timer event is due to happen. This
468 * is used on S/390 to stop all activity when a cpus is idle.
469 * This functions needs to be called disabled.
470 */
471unsigned long next_timer_interrupt(void)
472{
473 tvec_base_t *base;
474 struct list_head *list;
475 struct timer_list *nte;
476 unsigned long expires;
Tony Lindgren69239742006-03-06 15:42:45 -0800477 unsigned long hr_expires = MAX_JIFFY_OFFSET;
478 ktime_t hr_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 tvec_t *varray[4];
480 int i, j;
481
Tony Lindgren69239742006-03-06 15:42:45 -0800482 hr_delta = hrtimer_get_next_event();
483 if (hr_delta.tv64 != KTIME_MAX) {
484 struct timespec tsdelta;
485 tsdelta = ktime_to_timespec(hr_delta);
486 hr_expires = timespec_to_jiffies(&tsdelta);
487 if (hr_expires < 3)
488 return hr_expires + jiffies;
489 }
490 hr_expires += jiffies;
491
Jan Beulicha4a61982006-03-24 03:15:54 -0800492 base = __get_cpu_var(tvec_bases);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800493 spin_lock(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 expires = base->timer_jiffies + (LONG_MAX >> 1);
Al Viro53f087f2006-02-01 05:56:41 -0500495 list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /* Look for timer events in tv1. */
498 j = base->timer_jiffies & TVR_MASK;
499 do {
500 list_for_each_entry(nte, base->tv1.vec + j, entry) {
501 expires = nte->expires;
502 if (j < (base->timer_jiffies & TVR_MASK))
503 list = base->tv2.vec + (INDEX(0));
504 goto found;
505 }
506 j = (j + 1) & TVR_MASK;
507 } while (j != (base->timer_jiffies & TVR_MASK));
508
509 /* Check tv2-tv5. */
510 varray[0] = &base->tv2;
511 varray[1] = &base->tv3;
512 varray[2] = &base->tv4;
513 varray[3] = &base->tv5;
514 for (i = 0; i < 4; i++) {
515 j = INDEX(i);
516 do {
517 if (list_empty(varray[i]->vec + j)) {
518 j = (j + 1) & TVN_MASK;
519 continue;
520 }
521 list_for_each_entry(nte, varray[i]->vec + j, entry)
522 if (time_before(nte->expires, expires))
523 expires = nte->expires;
524 if (j < (INDEX(i)) && i < 3)
525 list = varray[i + 1]->vec + (INDEX(i + 1));
526 goto found;
527 } while (j != (INDEX(i)));
528 }
529found:
530 if (list) {
531 /*
532 * The search wrapped. We need to look at the next list
533 * from next tv element that would cascade into tv element
534 * where we found the timer element.
535 */
536 list_for_each_entry(nte, list, entry) {
537 if (time_before(nte->expires, expires))
538 expires = nte->expires;
539 }
540 }
Oleg Nesterov3691c512006-03-31 02:30:30 -0800541 spin_unlock(&base->lock);
Tony Lindgren69239742006-03-06 15:42:45 -0800542
543 if (time_before(hr_expires, expires))
544 return hr_expires;
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return expires;
547}
548#endif
549
550/******************************************************************/
551
552/*
553 * Timekeeping variables
554 */
555unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
556unsigned long tick_nsec = TICK_NSEC; /* ACTHZ period (nsec) */
557
558/*
559 * The current time
560 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
561 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
562 * at zero at system boot time, so wall_to_monotonic will be negative,
563 * however, we will ALWAYS keep the tv_nsec part positive so we can use
564 * the usual normalization.
565 */
566struct timespec xtime __attribute__ ((aligned (16)));
567struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
568
569EXPORT_SYMBOL(xtime);
570
571/* Don't completely fail for HZ > 500. */
572int tickadj = 500/HZ ? : 1; /* microsecs */
573
574
575/*
576 * phase-lock loop variables
577 */
578/* TIME_ERROR prevents overwriting the CMOS clock */
579int time_state = TIME_OK; /* clock synchronization status */
580int time_status = STA_UNSYNC; /* clock status bits */
581long time_offset; /* time adjustment (us) */
582long time_constant = 2; /* pll time constant */
583long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
584long time_precision = 1; /* clock precision (us) */
585long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
586long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
587static long time_phase; /* phase offset (scaled us) */
588long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
589 /* frequency offset (scaled ppm)*/
590static long time_adj; /* tick adjust (scaled 1 / HZ) */
591long time_reftime; /* time at last adjustment (s) */
592long time_adjust;
593long time_next_adjust;
594
595/*
596 * this routine handles the overflow of the microsecond field
597 *
598 * The tricky bits of code to handle the accurate clock support
599 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
600 * They were originally developed for SUN and DEC kernels.
601 * All the kudos should go to Dave for this stuff.
602 *
603 */
604static void second_overflow(void)
605{
Andrew Mortona5a0d522005-10-30 15:01:42 -0800606 long ltemp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Andrew Mortona5a0d522005-10-30 15:01:42 -0800608 /* Bump the maxerror field */
609 time_maxerror += time_tolerance >> SHIFT_USEC;
610 if (time_maxerror > NTP_PHASE_LIMIT) {
611 time_maxerror = NTP_PHASE_LIMIT;
612 time_status |= STA_UNSYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Andrew Mortona5a0d522005-10-30 15:01:42 -0800615 /*
616 * Leap second processing. If in leap-insert state at the end of the
617 * day, the system clock is set back one second; if in leap-delete
618 * state, the system clock is set ahead one second. The microtime()
619 * routine or external clock driver will insure that reported time is
620 * always monotonic. The ugly divides should be replaced.
621 */
622 switch (time_state) {
623 case TIME_OK:
624 if (time_status & STA_INS)
625 time_state = TIME_INS;
626 else if (time_status & STA_DEL)
627 time_state = TIME_DEL;
628 break;
629 case TIME_INS:
630 if (xtime.tv_sec % 86400 == 0) {
631 xtime.tv_sec--;
632 wall_to_monotonic.tv_sec++;
633 /*
634 * The timer interpolator will make time change
635 * gradually instead of an immediate jump by one second
636 */
637 time_interpolator_update(-NSEC_PER_SEC);
638 time_state = TIME_OOP;
639 clock_was_set();
640 printk(KERN_NOTICE "Clock: inserting leap second "
641 "23:59:60 UTC\n");
642 }
643 break;
644 case TIME_DEL:
645 if ((xtime.tv_sec + 1) % 86400 == 0) {
646 xtime.tv_sec++;
647 wall_to_monotonic.tv_sec--;
648 /*
649 * Use of time interpolator for a gradual change of
650 * time
651 */
652 time_interpolator_update(NSEC_PER_SEC);
653 time_state = TIME_WAIT;
654 clock_was_set();
655 printk(KERN_NOTICE "Clock: deleting leap second "
656 "23:59:59 UTC\n");
657 }
658 break;
659 case TIME_OOP:
660 time_state = TIME_WAIT;
661 break;
662 case TIME_WAIT:
663 if (!(time_status & (STA_INS | STA_DEL)))
664 time_state = TIME_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Andrew Mortona5a0d522005-10-30 15:01:42 -0800667 /*
668 * Compute the phase adjustment for the next second. In PLL mode, the
669 * offset is reduced by a fixed factor times the time constant. In FLL
670 * mode the offset is used directly. In either mode, the maximum phase
671 * adjustment for each second is clamped so as to spread the adjustment
672 * over not more than the number of seconds between updates.
673 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 ltemp = time_offset;
675 if (!(time_status & STA_FLL))
john stultz1bb34a42005-10-30 15:01:42 -0800676 ltemp = shift_right(ltemp, SHIFT_KG + time_constant);
677 ltemp = min(ltemp, (MAXPHASE / MINSEC) << SHIFT_UPDATE);
678 ltemp = max(ltemp, -(MAXPHASE / MINSEC) << SHIFT_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 time_offset -= ltemp;
680 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Andrew Mortona5a0d522005-10-30 15:01:42 -0800682 /*
683 * Compute the frequency estimate and additional phase adjustment due
Roman Zippel5ddcfa82006-03-25 03:08:28 -0800684 * to frequency error for the next second.
Andrew Mortona5a0d522005-10-30 15:01:42 -0800685 */
Roman Zippel5ddcfa82006-03-25 03:08:28 -0800686 ltemp = time_freq;
Andrew Mortona5a0d522005-10-30 15:01:42 -0800687 time_adj += shift_right(ltemp,(SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689#if HZ == 100
Andrew Mortona5a0d522005-10-30 15:01:42 -0800690 /*
691 * Compensate for (HZ==100) != (1 << SHIFT_HZ). Add 25% and 3.125% to
692 * get 128.125; => only 0.125% error (p. 14)
693 */
694 time_adj += shift_right(time_adj, 2) + shift_right(time_adj, 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695#endif
YOSHIFUJI Hideaki4b8f5732005-10-29 18:15:42 -0700696#if HZ == 250
Andrew Mortona5a0d522005-10-30 15:01:42 -0800697 /*
698 * Compensate for (HZ==250) != (1 << SHIFT_HZ). Add 1.5625% and
699 * 0.78125% to get 255.85938; => only 0.05% error (p. 14)
700 */
701 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
YOSHIFUJI Hideaki4b8f5732005-10-29 18:15:42 -0700702#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703#if HZ == 1000
Andrew Mortona5a0d522005-10-30 15:01:42 -0800704 /*
705 * Compensate for (HZ==1000) != (1 << SHIFT_HZ). Add 1.5625% and
706 * 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
707 */
708 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709#endif
710}
711
Paul Mackerras726c14b2006-02-17 10:30:23 +1100712/*
713 * Returns how many microseconds we need to add to xtime this tick
714 * in doing an adjustment requested with adjtime.
715 */
716static long adjtime_adjustment(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Paul Mackerras726c14b2006-02-17 10:30:23 +1100718 long time_adjust_step;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Paul Mackerras726c14b2006-02-17 10:30:23 +1100720 time_adjust_step = time_adjust;
721 if (time_adjust_step) {
Andrew Mortona5a0d522005-10-30 15:01:42 -0800722 /*
723 * We are doing an adjtime thing. Prepare time_adjust_step to
724 * be within bounds. Note that a positive time_adjust means we
725 * want the clock to run faster.
726 *
727 * Limit the amount of the step to be in the range
728 * -tickadj .. +tickadj
729 */
730 time_adjust_step = min(time_adjust_step, (long)tickadj);
731 time_adjust_step = max(time_adjust_step, (long)-tickadj);
Paul Mackerras726c14b2006-02-17 10:30:23 +1100732 }
733 return time_adjust_step;
734}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Paul Mackerras726c14b2006-02-17 10:30:23 +1100736/* in the NTP reference this is called "hardclock()" */
737static void update_wall_time_one_tick(void)
738{
739 long time_adjust_step, delta_nsec;
740
741 time_adjust_step = adjtime_adjustment();
742 if (time_adjust_step)
Andrew Mortona5a0d522005-10-30 15:01:42 -0800743 /* Reduce by this step the amount of time left */
744 time_adjust -= time_adjust_step;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 delta_nsec = tick_nsec + time_adjust_step * 1000;
746 /*
747 * Advance the phase, once it gets to one microsecond, then
748 * advance the tick more.
749 */
750 time_phase += time_adj;
john stultz1bb34a42005-10-30 15:01:42 -0800751 if ((time_phase >= FINENSEC) || (time_phase <= -FINENSEC)) {
752 long ltemp = shift_right(time_phase, (SHIFT_SCALE - 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 time_phase -= ltemp << (SHIFT_SCALE - 10);
754 delta_nsec += ltemp;
755 }
756 xtime.tv_nsec += delta_nsec;
757 time_interpolator_update(delta_nsec);
758
759 /* Changes by adjtime() do not take effect till next tick. */
760 if (time_next_adjust != 0) {
761 time_adjust = time_next_adjust;
762 time_next_adjust = 0;
763 }
764}
765
766/*
Paul Mackerras726c14b2006-02-17 10:30:23 +1100767 * Return how long ticks are at the moment, that is, how much time
768 * update_wall_time_one_tick will add to xtime next time we call it
769 * (assuming no calls to do_adjtimex in the meantime).
770 * The return value is in fixed-point nanoseconds with SHIFT_SCALE-10
771 * bits to the right of the binary point.
772 * This function has no side-effects.
773 */
774u64 current_tick_length(void)
775{
776 long delta_nsec;
777
778 delta_nsec = tick_nsec + adjtime_adjustment() * 1000;
779 return ((u64) delta_nsec << (SHIFT_SCALE - 10)) + time_adj;
780}
781
782/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * Using a loop looks inefficient, but "ticks" is
784 * usually just one (we shouldn't be losing ticks,
785 * we're doing this this way mainly for interrupt
786 * latency reasons, not because we think we'll
787 * have lots of lost timer ticks
788 */
789static void update_wall_time(unsigned long ticks)
790{
791 do {
792 ticks--;
793 update_wall_time_one_tick();
794 if (xtime.tv_nsec >= 1000000000) {
795 xtime.tv_nsec -= 1000000000;
796 xtime.tv_sec++;
797 second_overflow();
798 }
799 } while (ticks);
800}
801
802/*
803 * Called from the timer interrupt handler to charge one tick to the current
804 * process. user_tick is 1 if the tick is user time, 0 for system.
805 */
806void update_process_times(int user_tick)
807{
808 struct task_struct *p = current;
809 int cpu = smp_processor_id();
810
811 /* Note: this timer irq context must be accounted for as well. */
812 if (user_tick)
813 account_user_time(p, jiffies_to_cputime(1));
814 else
815 account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
816 run_local_timers();
817 if (rcu_pending(cpu))
818 rcu_check_callbacks(cpu, user_tick);
819 scheduler_tick();
820 run_posix_cpu_timers(p);
821}
822
823/*
824 * Nr of active tasks - counted in fixed-point numbers
825 */
826static unsigned long count_active_tasks(void)
827{
828 return (nr_running() + nr_uninterruptible()) * FIXED_1;
829}
830
831/*
832 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
833 * imply that avenrun[] is the standard name for this kind of thing.
834 * Nothing else seems to be standardized: the fractional size etc
835 * all seem to differ on different machines.
836 *
837 * Requires xtime_lock to access.
838 */
839unsigned long avenrun[3];
840
841EXPORT_SYMBOL(avenrun);
842
843/*
844 * calc_load - given tick count, update the avenrun load estimates.
845 * This is called while holding a write_lock on xtime_lock.
846 */
847static inline void calc_load(unsigned long ticks)
848{
849 unsigned long active_tasks; /* fixed-point */
850 static int count = LOAD_FREQ;
851
852 count -= ticks;
853 if (count < 0) {
854 count += LOAD_FREQ;
855 active_tasks = count_active_tasks();
856 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
857 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
858 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
859 }
860}
861
862/* jiffies at the most recent update of wall time */
863unsigned long wall_jiffies = INITIAL_JIFFIES;
864
865/*
866 * This read-write spinlock protects us from races in SMP while
867 * playing with xtime and avenrun.
868 */
869#ifndef ARCH_HAVE_XTIME_LOCK
870seqlock_t xtime_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
871
872EXPORT_SYMBOL(xtime_lock);
873#endif
874
875/*
876 * This function runs timers and the timer-tq in bottom half context.
877 */
878static void run_timer_softirq(struct softirq_action *h)
879{
Jan Beulicha4a61982006-03-24 03:15:54 -0800880 tvec_base_t *base = __get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800882 hrtimer_run_queues();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (time_after_eq(jiffies, base->timer_jiffies))
884 __run_timers(base);
885}
886
887/*
888 * Called by the local, per-CPU timer interrupt on SMP.
889 */
890void run_local_timers(void)
891{
892 raise_softirq(TIMER_SOFTIRQ);
Ingo Molnar6687a972006-03-24 03:18:41 -0800893 softlockup_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896/*
897 * Called by the timer interrupt. xtime_lock must already be taken
898 * by the timer IRQ!
899 */
900static inline void update_times(void)
901{
902 unsigned long ticks;
903
904 ticks = jiffies - wall_jiffies;
905 if (ticks) {
906 wall_jiffies += ticks;
907 update_wall_time(ticks);
908 }
909 calc_load(ticks);
910}
911
912/*
913 * The 64-bit jiffies value is not atomic - you MUST NOT read it
914 * without sampling the sequence number in xtime_lock.
915 * jiffies is defined in the linker script...
916 */
917
918void do_timer(struct pt_regs *regs)
919{
920 jiffies_64++;
Atsushi Nemoto5aee4052006-03-06 15:42:51 -0800921 /* prevent loading jiffies before storing new jiffies_64 value. */
922 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 update_times();
924}
925
926#ifdef __ARCH_WANT_SYS_ALARM
927
928/*
929 * For backwards compatibility? This can be done in libc so Alpha
930 * and all newer ports shouldn't need it.
931 */
932asmlinkage unsigned long sys_alarm(unsigned int seconds)
933{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -0800934 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937#endif
938
939#ifndef __alpha__
940
941/*
942 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
943 * should be moved into arch/i386 instead?
944 */
945
946/**
947 * sys_getpid - return the thread group id of the current process
948 *
949 * Note, despite the name, this returns the tgid not the pid. The tgid and
950 * the pid are identical unless CLONE_THREAD was specified on clone() in
951 * which case the tgid is the same in all threads of the same group.
952 *
953 * This is SMP safe as current->tgid does not change.
954 */
955asmlinkage long sys_getpid(void)
956{
957 return current->tgid;
958}
959
960/*
961 * Accessing ->group_leader->real_parent is not SMP-safe, it could
962 * change from under us. However, rather than getting any lock
963 * we can use an optimistic algorithm: get the parent
964 * pid, and go back and check that the parent is still
965 * the same. If it has changed (which is extremely unlikely
966 * indeed), we just try again..
967 *
968 * NOTE! This depends on the fact that even if we _do_
969 * get an old value of "parent", we can happily dereference
970 * the pointer (it was and remains a dereferencable kernel pointer
971 * no matter what): we just can't necessarily trust the result
972 * until we know that the parent pointer is valid.
973 *
974 * NOTE2: ->group_leader never changes from under us.
975 */
976asmlinkage long sys_getppid(void)
977{
978 int pid;
979 struct task_struct *me = current;
980 struct task_struct *parent;
981
982 parent = me->group_leader->real_parent;
983 for (;;) {
984 pid = parent->tgid;
David Meybohm4c5640c2005-08-22 13:11:08 -0700985#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 struct task_struct *old = parent;
988
989 /*
990 * Make sure we read the pid before re-reading the
991 * parent pointer:
992 */
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700993 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 parent = me->group_leader->real_parent;
995 if (old != parent)
996 continue;
997}
998#endif
999 break;
1000 }
1001 return pid;
1002}
1003
1004asmlinkage long sys_getuid(void)
1005{
1006 /* Only we change this so SMP safe */
1007 return current->uid;
1008}
1009
1010asmlinkage long sys_geteuid(void)
1011{
1012 /* Only we change this so SMP safe */
1013 return current->euid;
1014}
1015
1016asmlinkage long sys_getgid(void)
1017{
1018 /* Only we change this so SMP safe */
1019 return current->gid;
1020}
1021
1022asmlinkage long sys_getegid(void)
1023{
1024 /* Only we change this so SMP safe */
1025 return current->egid;
1026}
1027
1028#endif
1029
1030static void process_timeout(unsigned long __data)
1031{
1032 wake_up_process((task_t *)__data);
1033}
1034
1035/**
1036 * schedule_timeout - sleep until timeout
1037 * @timeout: timeout value in jiffies
1038 *
1039 * Make the current task sleep until @timeout jiffies have
1040 * elapsed. The routine will return immediately unless
1041 * the current task state has been set (see set_current_state()).
1042 *
1043 * You can set the task state as follows -
1044 *
1045 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1046 * pass before the routine returns. The routine will return 0
1047 *
1048 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1049 * delivered to the current task. In this case the remaining time
1050 * in jiffies will be returned, or 0 if the timer expired in time
1051 *
1052 * The current task state is guaranteed to be TASK_RUNNING when this
1053 * routine returns.
1054 *
1055 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1056 * the CPU away without a bound on the timeout. In this case the return
1057 * value will be %MAX_SCHEDULE_TIMEOUT.
1058 *
1059 * In all cases the return value is guaranteed to be non-negative.
1060 */
1061fastcall signed long __sched schedule_timeout(signed long timeout)
1062{
1063 struct timer_list timer;
1064 unsigned long expire;
1065
1066 switch (timeout)
1067 {
1068 case MAX_SCHEDULE_TIMEOUT:
1069 /*
1070 * These two special cases are useful to be comfortable
1071 * in the caller. Nothing more. We could take
1072 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1073 * but I' d like to return a valid offset (>=0) to allow
1074 * the caller to do everything it want with the retval.
1075 */
1076 schedule();
1077 goto out;
1078 default:
1079 /*
1080 * Another bit of PARANOID. Note that the retval will be
1081 * 0 since no piece of kernel is supposed to do a check
1082 * for a negative retval of schedule_timeout() (since it
1083 * should never happens anyway). You just have the printk()
1084 * that will tell you if something is gone wrong and where.
1085 */
1086 if (timeout < 0)
1087 {
1088 printk(KERN_ERR "schedule_timeout: wrong timeout "
Andrew Mortona5a0d522005-10-30 15:01:42 -08001089 "value %lx from %p\n", timeout,
1090 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 current->state = TASK_RUNNING;
1092 goto out;
1093 }
1094 }
1095
1096 expire = timeout + jiffies;
1097
Oleg Nesterova8db2db2005-10-30 15:01:38 -08001098 setup_timer(&timer, process_timeout, (unsigned long)current);
1099 __mod_timer(&timer, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 schedule();
1101 del_singleshot_timer_sync(&timer);
1102
1103 timeout = expire - jiffies;
1104
1105 out:
1106 return timeout < 0 ? 0 : timeout;
1107}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108EXPORT_SYMBOL(schedule_timeout);
1109
Andrew Morton8a1c1752005-09-13 01:25:15 -07001110/*
1111 * We can use __set_current_state() here because schedule_timeout() calls
1112 * schedule() unconditionally.
1113 */
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001114signed long __sched schedule_timeout_interruptible(signed long timeout)
1115{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001116 __set_current_state(TASK_INTERRUPTIBLE);
1117 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001118}
1119EXPORT_SYMBOL(schedule_timeout_interruptible);
1120
1121signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1122{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001123 __set_current_state(TASK_UNINTERRUPTIBLE);
1124 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001125}
1126EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128/* Thread ID - the internal kernel "pid" */
1129asmlinkage long sys_gettid(void)
1130{
1131 return current->pid;
1132}
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134/*
1135 * sys_sysinfo - fill in sysinfo struct
1136 */
1137asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1138{
1139 struct sysinfo val;
1140 unsigned long mem_total, sav_total;
1141 unsigned int mem_unit, bitcount;
1142 unsigned long seq;
1143
1144 memset((char *)&val, 0, sizeof(struct sysinfo));
1145
1146 do {
1147 struct timespec tp;
1148 seq = read_seqbegin(&xtime_lock);
1149
1150 /*
1151 * This is annoying. The below is the same thing
1152 * posix_get_clock_monotonic() does, but it wants to
1153 * take the lock which we want to cover the loads stuff
1154 * too.
1155 */
1156
1157 getnstimeofday(&tp);
1158 tp.tv_sec += wall_to_monotonic.tv_sec;
1159 tp.tv_nsec += wall_to_monotonic.tv_nsec;
1160 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1161 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1162 tp.tv_sec++;
1163 }
1164 val.uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1165
1166 val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1167 val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1168 val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
1169
1170 val.procs = nr_threads;
1171 } while (read_seqretry(&xtime_lock, seq));
1172
1173 si_meminfo(&val);
1174 si_swapinfo(&val);
1175
1176 /*
1177 * If the sum of all the available memory (i.e. ram + swap)
1178 * is less than can be stored in a 32 bit unsigned long then
1179 * we can be binary compatible with 2.2.x kernels. If not,
1180 * well, in that case 2.2.x was broken anyways...
1181 *
1182 * -Erik Andersen <andersee@debian.org>
1183 */
1184
1185 mem_total = val.totalram + val.totalswap;
1186 if (mem_total < val.totalram || mem_total < val.totalswap)
1187 goto out;
1188 bitcount = 0;
1189 mem_unit = val.mem_unit;
1190 while (mem_unit > 1) {
1191 bitcount++;
1192 mem_unit >>= 1;
1193 sav_total = mem_total;
1194 mem_total <<= 1;
1195 if (mem_total < sav_total)
1196 goto out;
1197 }
1198
1199 /*
1200 * If mem_total did not overflow, multiply all memory values by
1201 * val.mem_unit and set it to 1. This leaves things compatible
1202 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1203 * kernels...
1204 */
1205
1206 val.mem_unit = 1;
1207 val.totalram <<= bitcount;
1208 val.freeram <<= bitcount;
1209 val.sharedram <<= bitcount;
1210 val.bufferram <<= bitcount;
1211 val.totalswap <<= bitcount;
1212 val.freeswap <<= bitcount;
1213 val.totalhigh <<= bitcount;
1214 val.freehigh <<= bitcount;
1215
1216 out:
1217 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1218 return -EFAULT;
1219
1220 return 0;
1221}
1222
Jan Beulicha4a61982006-03-24 03:15:54 -08001223static int __devinit init_timers_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 int j;
1226 tvec_base_t *base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001227
Jan Beulicha4a61982006-03-24 03:15:54 -08001228 base = per_cpu(tvec_bases, cpu);
1229 if (!base) {
1230 static char boot_done;
1231
1232 /*
1233 * Cannot do allocation in init_timers as that runs before the
1234 * allocator initializes (and would waste memory if there are
1235 * more possible CPUs than will ever be installed/brought up).
1236 */
1237 if (boot_done) {
1238 base = kmalloc_node(sizeof(*base), GFP_KERNEL,
1239 cpu_to_node(cpu));
1240 if (!base)
1241 return -ENOMEM;
1242 memset(base, 0, sizeof(*base));
1243 } else {
1244 base = &boot_tvec_bases;
1245 boot_done = 1;
1246 }
1247 per_cpu(tvec_bases, cpu) = base;
1248 }
Oleg Nesterov3691c512006-03-31 02:30:30 -08001249 spin_lock_init(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 for (j = 0; j < TVN_SIZE; j++) {
1251 INIT_LIST_HEAD(base->tv5.vec + j);
1252 INIT_LIST_HEAD(base->tv4.vec + j);
1253 INIT_LIST_HEAD(base->tv3.vec + j);
1254 INIT_LIST_HEAD(base->tv2.vec + j);
1255 }
1256 for (j = 0; j < TVR_SIZE; j++)
1257 INIT_LIST_HEAD(base->tv1.vec + j);
1258
1259 base->timer_jiffies = jiffies;
Jan Beulicha4a61982006-03-24 03:15:54 -08001260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
1263#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001264static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 struct timer_list *timer;
1267
1268 while (!list_empty(head)) {
1269 timer = list_entry(head->next, struct timer_list, entry);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001270 detach_timer(timer, 0);
Oleg Nesterov3691c512006-03-31 02:30:30 -08001271 timer->base = new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 internal_add_timer(new_base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
1276static void __devinit migrate_timers(int cpu)
1277{
1278 tvec_base_t *old_base;
1279 tvec_base_t *new_base;
1280 int i;
1281
1282 BUG_ON(cpu_online(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001283 old_base = per_cpu(tvec_bases, cpu);
1284 new_base = get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 local_irq_disable();
Oleg Nesterov3691c512006-03-31 02:30:30 -08001287 spin_lock(&new_base->lock);
1288 spin_lock(&old_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Oleg Nesterov3691c512006-03-31 02:30:30 -08001290 BUG_ON(old_base->running_timer);
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 for (i = 0; i < TVR_SIZE; i++)
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001293 migrate_timer_list(new_base, old_base->tv1.vec + i);
1294 for (i = 0; i < TVN_SIZE; i++) {
1295 migrate_timer_list(new_base, old_base->tv2.vec + i);
1296 migrate_timer_list(new_base, old_base->tv3.vec + i);
1297 migrate_timer_list(new_base, old_base->tv4.vec + i);
1298 migrate_timer_list(new_base, old_base->tv5.vec + i);
1299 }
1300
Oleg Nesterov3691c512006-03-31 02:30:30 -08001301 spin_unlock(&old_base->lock);
1302 spin_unlock(&new_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 local_irq_enable();
1304 put_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306#endif /* CONFIG_HOTPLUG_CPU */
1307
1308static int __devinit timer_cpu_notify(struct notifier_block *self,
1309 unsigned long action, void *hcpu)
1310{
1311 long cpu = (long)hcpu;
1312 switch(action) {
1313 case CPU_UP_PREPARE:
Jan Beulicha4a61982006-03-24 03:15:54 -08001314 if (init_timers_cpu(cpu) < 0)
1315 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 break;
1317#ifdef CONFIG_HOTPLUG_CPU
1318 case CPU_DEAD:
1319 migrate_timers(cpu);
1320 break;
1321#endif
1322 default:
1323 break;
1324 }
1325 return NOTIFY_OK;
1326}
1327
1328static struct notifier_block __devinitdata timers_nb = {
1329 .notifier_call = timer_cpu_notify,
1330};
1331
1332
1333void __init init_timers(void)
1334{
1335 timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1336 (void *)(long)smp_processor_id());
1337 register_cpu_notifier(&timers_nb);
1338 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1339}
1340
1341#ifdef CONFIG_TIME_INTERPOLATION
1342
Christoph Lameter67890d72006-03-16 23:04:00 -08001343struct time_interpolator *time_interpolator __read_mostly;
1344static struct time_interpolator *time_interpolator_list __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345static DEFINE_SPINLOCK(time_interpolator_lock);
1346
1347static inline u64 time_interpolator_get_cycles(unsigned int src)
1348{
1349 unsigned long (*x)(void);
1350
1351 switch (src)
1352 {
1353 case TIME_SOURCE_FUNCTION:
1354 x = time_interpolator->addr;
1355 return x();
1356
1357 case TIME_SOURCE_MMIO64 :
Christoph Lameter685db652006-03-02 02:54:35 -08001358 return readq_relaxed((void __iomem *)time_interpolator->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 case TIME_SOURCE_MMIO32 :
Christoph Lameter685db652006-03-02 02:54:35 -08001361 return readl_relaxed((void __iomem *)time_interpolator->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 default: return get_cycles();
1364 }
1365}
1366
Alex Williamson486d46ae2005-09-06 15:17:04 -07001367static inline u64 time_interpolator_get_counter(int writelock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 unsigned int src = time_interpolator->source;
1370
1371 if (time_interpolator->jitter)
1372 {
1373 u64 lcycle;
1374 u64 now;
1375
1376 do {
1377 lcycle = time_interpolator->last_cycle;
1378 now = time_interpolator_get_cycles(src);
1379 if (lcycle && time_after(lcycle, now))
1380 return lcycle;
Alex Williamson486d46ae2005-09-06 15:17:04 -07001381
1382 /* When holding the xtime write lock, there's no need
1383 * to add the overhead of the cmpxchg. Readers are
1384 * force to retry until the write lock is released.
1385 */
1386 if (writelock) {
1387 time_interpolator->last_cycle = now;
1388 return now;
1389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* Keep track of the last timer value returned. The use of cmpxchg here
1391 * will cause contention in an SMP environment.
1392 */
1393 } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
1394 return now;
1395 }
1396 else
1397 return time_interpolator_get_cycles(src);
1398}
1399
1400void time_interpolator_reset(void)
1401{
1402 time_interpolator->offset = 0;
Alex Williamson486d46ae2005-09-06 15:17:04 -07001403 time_interpolator->last_counter = time_interpolator_get_counter(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
1406#define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
1407
1408unsigned long time_interpolator_get_offset(void)
1409{
1410 /* If we do not have a time interpolator set up then just return zero */
1411 if (!time_interpolator)
1412 return 0;
1413
1414 return time_interpolator->offset +
Alex Williamson486d46ae2005-09-06 15:17:04 -07001415 GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
1418#define INTERPOLATOR_ADJUST 65536
1419#define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
1420
1421static void time_interpolator_update(long delta_nsec)
1422{
1423 u64 counter;
1424 unsigned long offset;
1425
1426 /* If there is no time interpolator set up then do nothing */
1427 if (!time_interpolator)
1428 return;
1429
Andrew Mortona5a0d522005-10-30 15:01:42 -08001430 /*
1431 * The interpolator compensates for late ticks by accumulating the late
1432 * time in time_interpolator->offset. A tick earlier than expected will
1433 * lead to a reset of the offset and a corresponding jump of the clock
1434 * forward. Again this only works if the interpolator clock is running
1435 * slightly slower than the regular clock and the tuning logic insures
1436 * that.
1437 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Alex Williamson486d46ae2005-09-06 15:17:04 -07001439 counter = time_interpolator_get_counter(1);
Andrew Mortona5a0d522005-10-30 15:01:42 -08001440 offset = time_interpolator->offset +
1441 GET_TI_NSECS(counter, time_interpolator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
1444 time_interpolator->offset = offset - delta_nsec;
1445 else {
1446 time_interpolator->skips++;
1447 time_interpolator->ns_skipped += delta_nsec - offset;
1448 time_interpolator->offset = 0;
1449 }
1450 time_interpolator->last_counter = counter;
1451
1452 /* Tuning logic for time interpolator invoked every minute or so.
1453 * Decrease interpolator clock speed if no skips occurred and an offset is carried.
1454 * Increase interpolator clock speed if we skip too much time.
1455 */
1456 if (jiffies % INTERPOLATOR_ADJUST == 0)
1457 {
1458 if (time_interpolator->skips == 0 && time_interpolator->offset > TICK_NSEC)
1459 time_interpolator->nsec_per_cyc--;
1460 if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
1461 time_interpolator->nsec_per_cyc++;
1462 time_interpolator->skips = 0;
1463 time_interpolator->ns_skipped = 0;
1464 }
1465}
1466
1467static inline int
1468is_better_time_interpolator(struct time_interpolator *new)
1469{
1470 if (!time_interpolator)
1471 return 1;
1472 return new->frequency > 2*time_interpolator->frequency ||
1473 (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
1474}
1475
1476void
1477register_time_interpolator(struct time_interpolator *ti)
1478{
1479 unsigned long flags;
1480
1481 /* Sanity check */
1482 if (ti->frequency == 0 || ti->mask == 0)
1483 BUG();
1484
1485 ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
1486 spin_lock(&time_interpolator_lock);
1487 write_seqlock_irqsave(&xtime_lock, flags);
1488 if (is_better_time_interpolator(ti)) {
1489 time_interpolator = ti;
1490 time_interpolator_reset();
1491 }
1492 write_sequnlock_irqrestore(&xtime_lock, flags);
1493
1494 ti->next = time_interpolator_list;
1495 time_interpolator_list = ti;
1496 spin_unlock(&time_interpolator_lock);
1497}
1498
1499void
1500unregister_time_interpolator(struct time_interpolator *ti)
1501{
1502 struct time_interpolator *curr, **prev;
1503 unsigned long flags;
1504
1505 spin_lock(&time_interpolator_lock);
1506 prev = &time_interpolator_list;
1507 for (curr = *prev; curr; curr = curr->next) {
1508 if (curr == ti) {
1509 *prev = curr->next;
1510 break;
1511 }
1512 prev = &curr->next;
1513 }
1514
1515 write_seqlock_irqsave(&xtime_lock, flags);
1516 if (ti == time_interpolator) {
1517 /* we lost the best time-interpolator: */
1518 time_interpolator = NULL;
1519 /* find the next-best interpolator */
1520 for (curr = time_interpolator_list; curr; curr = curr->next)
1521 if (is_better_time_interpolator(curr))
1522 time_interpolator = curr;
1523 time_interpolator_reset();
1524 }
1525 write_sequnlock_irqrestore(&xtime_lock, flags);
1526 spin_unlock(&time_interpolator_lock);
1527}
1528#endif /* CONFIG_TIME_INTERPOLATION */
1529
1530/**
1531 * msleep - sleep safely even with waitqueue interruptions
1532 * @msecs: Time in milliseconds to sleep for
1533 */
1534void msleep(unsigned int msecs)
1535{
1536 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1537
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001538 while (timeout)
1539 timeout = schedule_timeout_uninterruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
1542EXPORT_SYMBOL(msleep);
1543
1544/**
Domen Puncer96ec3ef2005-06-25 14:58:43 -07001545 * msleep_interruptible - sleep waiting for signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 * @msecs: Time in milliseconds to sleep for
1547 */
1548unsigned long msleep_interruptible(unsigned int msecs)
1549{
1550 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1551
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001552 while (timeout && !signal_pending(current))
1553 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return jiffies_to_msecs(timeout);
1555}
1556
1557EXPORT_SYMBOL(msleep_interruptible);