blob: 6811436a031ddcf330db92b2e3b2c0f2f98778bf [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;
Paul Mackerrasbfe5d832006-06-25 05:47:14 -0700149 timer->base = __raw_get_cpu_var(tvec_bases);
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 */
Porpoise3439dd82006-06-23 02:05:56 -0700386 struct timer_list *timer, *tmp;
387 struct list_head tv_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Porpoise3439dd82006-06-23 02:05:56 -0700389 list_replace_init(tv->vec + index, &tv_list);
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /*
Porpoise3439dd82006-06-23 02:05:56 -0700392 * We are removing _all_ timers from the list, so we
393 * don't have to detach them individually.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 */
Porpoise3439dd82006-06-23 02:05:56 -0700395 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
396 BUG_ON(timer->base != base);
397 internal_add_timer(base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 return index;
401}
402
403/***
404 * __run_timers - run all expired timers (if any) on this CPU.
405 * @base: the timer vector to be processed.
406 *
407 * This function cascades all vectors and executes all expired timer
408 * vectors.
409 */
410#define INDEX(N) (base->timer_jiffies >> (TVR_BITS + N * TVN_BITS)) & TVN_MASK
411
412static inline void __run_timers(tvec_base_t *base)
413{
414 struct timer_list *timer;
415
Oleg Nesterov3691c512006-03-31 02:30:30 -0800416 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 while (time_after_eq(jiffies, base->timer_jiffies)) {
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700418 struct list_head work_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct list_head *head = &work_list;
420 int index = base->timer_jiffies & TVR_MASK;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /*
423 * Cascade timers:
424 */
425 if (!index &&
426 (!cascade(base, &base->tv2, INDEX(0))) &&
427 (!cascade(base, &base->tv3, INDEX(1))) &&
428 !cascade(base, &base->tv4, INDEX(2)))
429 cascade(base, &base->tv5, INDEX(3));
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700430 ++base->timer_jiffies;
431 list_replace_init(base->tv1.vec + index, &work_list);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700432 while (!list_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 void (*fn)(unsigned long);
434 unsigned long data;
435
436 timer = list_entry(head->next,struct timer_list,entry);
437 fn = timer->function;
438 data = timer->data;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 set_running_timer(base, timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700441 detach_timer(timer, 1);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800442 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700444 int preempt_count = preempt_count();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 fn(data);
446 if (preempt_count != preempt_count()) {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700447 printk(KERN_WARNING "huh, entered %p "
448 "with preempt_count %08x, exited"
449 " with %08x?\n",
450 fn, preempt_count,
451 preempt_count());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 BUG();
453 }
454 }
Oleg Nesterov3691c512006-03-31 02:30:30 -0800455 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 }
458 set_running_timer(base, NULL);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800459 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462#ifdef CONFIG_NO_IDLE_HZ
463/*
464 * Find out when the next timer event is due to happen. This
465 * is used on S/390 to stop all activity when a cpus is idle.
466 * This functions needs to be called disabled.
467 */
468unsigned long next_timer_interrupt(void)
469{
470 tvec_base_t *base;
471 struct list_head *list;
472 struct timer_list *nte;
473 unsigned long expires;
Tony Lindgren69239742006-03-06 15:42:45 -0800474 unsigned long hr_expires = MAX_JIFFY_OFFSET;
475 ktime_t hr_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 tvec_t *varray[4];
477 int i, j;
478
Tony Lindgren69239742006-03-06 15:42:45 -0800479 hr_delta = hrtimer_get_next_event();
480 if (hr_delta.tv64 != KTIME_MAX) {
481 struct timespec tsdelta;
482 tsdelta = ktime_to_timespec(hr_delta);
483 hr_expires = timespec_to_jiffies(&tsdelta);
484 if (hr_expires < 3)
485 return hr_expires + jiffies;
486 }
487 hr_expires += jiffies;
488
Jan Beulicha4a61982006-03-24 03:15:54 -0800489 base = __get_cpu_var(tvec_bases);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800490 spin_lock(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 expires = base->timer_jiffies + (LONG_MAX >> 1);
Al Viro53f087f2006-02-01 05:56:41 -0500492 list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Look for timer events in tv1. */
495 j = base->timer_jiffies & TVR_MASK;
496 do {
497 list_for_each_entry(nte, base->tv1.vec + j, entry) {
498 expires = nte->expires;
499 if (j < (base->timer_jiffies & TVR_MASK))
500 list = base->tv2.vec + (INDEX(0));
501 goto found;
502 }
503 j = (j + 1) & TVR_MASK;
504 } while (j != (base->timer_jiffies & TVR_MASK));
505
506 /* Check tv2-tv5. */
507 varray[0] = &base->tv2;
508 varray[1] = &base->tv3;
509 varray[2] = &base->tv4;
510 varray[3] = &base->tv5;
511 for (i = 0; i < 4; i++) {
512 j = INDEX(i);
513 do {
514 if (list_empty(varray[i]->vec + j)) {
515 j = (j + 1) & TVN_MASK;
516 continue;
517 }
518 list_for_each_entry(nte, varray[i]->vec + j, entry)
519 if (time_before(nte->expires, expires))
520 expires = nte->expires;
521 if (j < (INDEX(i)) && i < 3)
522 list = varray[i + 1]->vec + (INDEX(i + 1));
523 goto found;
524 } while (j != (INDEX(i)));
525 }
526found:
527 if (list) {
528 /*
529 * The search wrapped. We need to look at the next list
530 * from next tv element that would cascade into tv element
531 * where we found the timer element.
532 */
533 list_for_each_entry(nte, list, entry) {
534 if (time_before(nte->expires, expires))
535 expires = nte->expires;
536 }
537 }
Oleg Nesterov3691c512006-03-31 02:30:30 -0800538 spin_unlock(&base->lock);
Tony Lindgren69239742006-03-06 15:42:45 -0800539
Zachary Amsden0662b712006-05-20 15:00:24 -0700540 /*
541 * It can happen that other CPUs service timer IRQs and increment
542 * jiffies, but we have not yet got a local timer tick to process
543 * the timer wheels. In that case, the expiry time can be before
544 * jiffies, but since the high-resolution timer here is relative to
545 * jiffies, the default expression when high-resolution timers are
546 * not active,
547 *
548 * time_before(MAX_JIFFY_OFFSET + jiffies, expires)
549 *
550 * would falsely evaluate to true. If that is the case, just
551 * return jiffies so that we can immediately fire the local timer
552 */
553 if (time_before(expires, jiffies))
554 return jiffies;
555
Tony Lindgren69239742006-03-06 15:42:45 -0800556 if (time_before(hr_expires, expires))
557 return hr_expires;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return expires;
560}
561#endif
562
563/******************************************************************/
564
565/*
566 * Timekeeping variables
567 */
568unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
569unsigned long tick_nsec = TICK_NSEC; /* ACTHZ period (nsec) */
570
571/*
572 * The current time
573 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
574 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
575 * at zero at system boot time, so wall_to_monotonic will be negative,
576 * however, we will ALWAYS keep the tv_nsec part positive so we can use
577 * the usual normalization.
578 */
579struct timespec xtime __attribute__ ((aligned (16)));
580struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
581
582EXPORT_SYMBOL(xtime);
583
584/* Don't completely fail for HZ > 500. */
585int tickadj = 500/HZ ? : 1; /* microsecs */
586
587
588/*
589 * phase-lock loop variables
590 */
591/* TIME_ERROR prevents overwriting the CMOS clock */
592int time_state = TIME_OK; /* clock synchronization status */
593int time_status = STA_UNSYNC; /* clock status bits */
594long time_offset; /* time adjustment (us) */
595long time_constant = 2; /* pll time constant */
596long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
597long time_precision = 1; /* clock precision (us) */
598long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
599long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
601 /* frequency offset (scaled ppm)*/
602static long time_adj; /* tick adjust (scaled 1 / HZ) */
603long time_reftime; /* time at last adjustment (s) */
604long time_adjust;
605long time_next_adjust;
606
607/*
608 * this routine handles the overflow of the microsecond field
609 *
610 * The tricky bits of code to handle the accurate clock support
611 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
612 * They were originally developed for SUN and DEC kernels.
613 * All the kudos should go to Dave for this stuff.
614 *
615 */
616static void second_overflow(void)
617{
Andrew Mortona5a0d522005-10-30 15:01:42 -0800618 long ltemp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Andrew Mortona5a0d522005-10-30 15:01:42 -0800620 /* Bump the maxerror field */
621 time_maxerror += time_tolerance >> SHIFT_USEC;
622 if (time_maxerror > NTP_PHASE_LIMIT) {
623 time_maxerror = NTP_PHASE_LIMIT;
624 time_status |= STA_UNSYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Andrew Mortona5a0d522005-10-30 15:01:42 -0800627 /*
628 * Leap second processing. If in leap-insert state at the end of the
629 * day, the system clock is set back one second; if in leap-delete
630 * state, the system clock is set ahead one second. The microtime()
631 * routine or external clock driver will insure that reported time is
632 * always monotonic. The ugly divides should be replaced.
633 */
634 switch (time_state) {
635 case TIME_OK:
636 if (time_status & STA_INS)
637 time_state = TIME_INS;
638 else if (time_status & STA_DEL)
639 time_state = TIME_DEL;
640 break;
641 case TIME_INS:
642 if (xtime.tv_sec % 86400 == 0) {
643 xtime.tv_sec--;
644 wall_to_monotonic.tv_sec++;
645 /*
646 * The timer interpolator will make time change
647 * gradually instead of an immediate jump by one second
648 */
649 time_interpolator_update(-NSEC_PER_SEC);
650 time_state = TIME_OOP;
651 clock_was_set();
652 printk(KERN_NOTICE "Clock: inserting leap second "
653 "23:59:60 UTC\n");
654 }
655 break;
656 case TIME_DEL:
657 if ((xtime.tv_sec + 1) % 86400 == 0) {
658 xtime.tv_sec++;
659 wall_to_monotonic.tv_sec--;
660 /*
661 * Use of time interpolator for a gradual change of
662 * time
663 */
664 time_interpolator_update(NSEC_PER_SEC);
665 time_state = TIME_WAIT;
666 clock_was_set();
667 printk(KERN_NOTICE "Clock: deleting leap second "
668 "23:59:59 UTC\n");
669 }
670 break;
671 case TIME_OOP:
672 time_state = TIME_WAIT;
673 break;
674 case TIME_WAIT:
675 if (!(time_status & (STA_INS | STA_DEL)))
676 time_state = TIME_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Andrew Mortona5a0d522005-10-30 15:01:42 -0800679 /*
680 * Compute the phase adjustment for the next second. In PLL mode, the
681 * offset is reduced by a fixed factor times the time constant. In FLL
682 * mode the offset is used directly. In either mode, the maximum phase
683 * adjustment for each second is clamped so as to spread the adjustment
684 * over not more than the number of seconds between updates.
685 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 ltemp = time_offset;
687 if (!(time_status & STA_FLL))
john stultz1bb34a42005-10-30 15:01:42 -0800688 ltemp = shift_right(ltemp, SHIFT_KG + time_constant);
689 ltemp = min(ltemp, (MAXPHASE / MINSEC) << SHIFT_UPDATE);
690 ltemp = max(ltemp, -(MAXPHASE / MINSEC) << SHIFT_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 time_offset -= ltemp;
692 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Andrew Mortona5a0d522005-10-30 15:01:42 -0800694 /*
695 * Compute the frequency estimate and additional phase adjustment due
Roman Zippel5ddcfa82006-03-25 03:08:28 -0800696 * to frequency error for the next second.
Andrew Mortona5a0d522005-10-30 15:01:42 -0800697 */
Roman Zippel5ddcfa82006-03-25 03:08:28 -0800698 ltemp = time_freq;
Andrew Mortona5a0d522005-10-30 15:01:42 -0800699 time_adj += shift_right(ltemp,(SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701#if HZ == 100
Andrew Mortona5a0d522005-10-30 15:01:42 -0800702 /*
703 * Compensate for (HZ==100) != (1 << SHIFT_HZ). Add 25% and 3.125% to
704 * get 128.125; => only 0.125% error (p. 14)
705 */
706 time_adj += shift_right(time_adj, 2) + shift_right(time_adj, 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707#endif
YOSHIFUJI Hideaki4b8f5732005-10-29 18:15:42 -0700708#if HZ == 250
Andrew Mortona5a0d522005-10-30 15:01:42 -0800709 /*
710 * Compensate for (HZ==250) != (1 << SHIFT_HZ). Add 1.5625% and
711 * 0.78125% to get 255.85938; => only 0.05% error (p. 14)
712 */
713 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
YOSHIFUJI Hideaki4b8f5732005-10-29 18:15:42 -0700714#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715#if HZ == 1000
Andrew Mortona5a0d522005-10-30 15:01:42 -0800716 /*
717 * Compensate for (HZ==1000) != (1 << SHIFT_HZ). Add 1.5625% and
718 * 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
719 */
720 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721#endif
722}
723
Paul Mackerras726c14b2006-02-17 10:30:23 +1100724/*
725 * Returns how many microseconds we need to add to xtime this tick
726 * in doing an adjustment requested with adjtime.
727 */
728static long adjtime_adjustment(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Paul Mackerras726c14b2006-02-17 10:30:23 +1100730 long time_adjust_step;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Paul Mackerras726c14b2006-02-17 10:30:23 +1100732 time_adjust_step = time_adjust;
733 if (time_adjust_step) {
Andrew Mortona5a0d522005-10-30 15:01:42 -0800734 /*
735 * We are doing an adjtime thing. Prepare time_adjust_step to
736 * be within bounds. Note that a positive time_adjust means we
737 * want the clock to run faster.
738 *
739 * Limit the amount of the step to be in the range
740 * -tickadj .. +tickadj
741 */
742 time_adjust_step = min(time_adjust_step, (long)tickadj);
743 time_adjust_step = max(time_adjust_step, (long)-tickadj);
Paul Mackerras726c14b2006-02-17 10:30:23 +1100744 }
745 return time_adjust_step;
746}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Paul Mackerras726c14b2006-02-17 10:30:23 +1100748/* in the NTP reference this is called "hardclock()" */
john stultz5eb6d202006-06-26 00:25:07 -0700749static void update_ntp_one_tick(void)
Paul Mackerras726c14b2006-02-17 10:30:23 +1100750{
john stultz5eb6d202006-06-26 00:25:07 -0700751 long time_adjust_step;
Paul Mackerras726c14b2006-02-17 10:30:23 +1100752
753 time_adjust_step = adjtime_adjustment();
754 if (time_adjust_step)
Andrew Mortona5a0d522005-10-30 15:01:42 -0800755 /* Reduce by this step the amount of time left */
756 time_adjust -= time_adjust_step;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 /* Changes by adjtime() do not take effect till next tick. */
759 if (time_next_adjust != 0) {
760 time_adjust = time_next_adjust;
761 time_next_adjust = 0;
762 }
763}
764
765/*
Paul Mackerras726c14b2006-02-17 10:30:23 +1100766 * Return how long ticks are at the moment, that is, how much time
767 * update_wall_time_one_tick will add to xtime next time we call it
768 * (assuming no calls to do_adjtimex in the meantime).
john stultz260a4232006-06-26 00:25:07 -0700769 * The return value is in fixed-point nanoseconds shifted by the
770 * specified number of bits to the right of the binary point.
Paul Mackerras726c14b2006-02-17 10:30:23 +1100771 * This function has no side-effects.
772 */
john stultz260a4232006-06-26 00:25:07 -0700773u64 current_tick_length(long shift)
Paul Mackerras726c14b2006-02-17 10:30:23 +1100774{
775 long delta_nsec;
john stultz260a4232006-06-26 00:25:07 -0700776 u64 ret;
Paul Mackerras726c14b2006-02-17 10:30:23 +1100777
john stultz260a4232006-06-26 00:25:07 -0700778 /* calculate the finest interval NTP will allow.
779 * ie: nanosecond value shifted by (SHIFT_SCALE - 10)
780 */
Paul Mackerras726c14b2006-02-17 10:30:23 +1100781 delta_nsec = tick_nsec + adjtime_adjustment() * 1000;
john stultz260a4232006-06-26 00:25:07 -0700782 ret = ((u64) delta_nsec << (SHIFT_SCALE - 10)) + time_adj;
783
784 /* convert from (SHIFT_SCALE - 10) to specified shift scale: */
785 shift = shift - (SHIFT_SCALE - 10);
786 if (shift < 0)
787 ret >>= -shift;
788 else
789 ret <<= shift;
790
791 return ret;
Paul Mackerras726c14b2006-02-17 10:30:23 +1100792}
793
john stultzad596172006-06-26 00:25:06 -0700794/* XXX - all of this timekeeping code should be later moved to time.c */
795#include <linux/clocksource.h>
796static struct clocksource *clock; /* pointer to current clocksource */
797static cycle_t last_clock_cycle; /* cycle value at last update_wall_time */
Paul Mackerras726c14b2006-02-17 10:30:23 +1100798/*
john stultzad596172006-06-26 00:25:06 -0700799 * timekeeping_init - Initializes the clocksource and common timekeeping values
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 */
john stultzad596172006-06-26 00:25:06 -0700801void __init timekeeping_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
john stultzad596172006-06-26 00:25:06 -0700803 unsigned long flags;
804
805 write_seqlock_irqsave(&xtime_lock, flags);
806 clock = get_next_clocksource();
807 calculate_clocksource_interval(clock, tick_nsec);
808 last_clock_cycle = read_clocksource(clock);
809 ntp_clear();
810 write_sequnlock_irqrestore(&xtime_lock, flags);
811}
812
813
814/*
815 * timekeeping_resume - Resumes the generic timekeeping subsystem.
816 * @dev: unused
817 *
818 * This is for the generic clocksource timekeeping.
819 * xtime/wall_to_monotonic/jiffies/wall_jiffies/etc are
820 * still managed by arch specific suspend/resume code.
821 */
822static int timekeeping_resume(struct sys_device *dev)
823{
824 unsigned long flags;
825
826 write_seqlock_irqsave(&xtime_lock, flags);
827 /* restart the last cycle value */
828 last_clock_cycle = read_clocksource(clock);
829 write_sequnlock_irqrestore(&xtime_lock, flags);
830 return 0;
831}
832
833/* sysfs resume/suspend bits for timekeeping */
834static struct sysdev_class timekeeping_sysclass = {
835 .resume = timekeeping_resume,
836 set_kset_name("timekeeping"),
837};
838
839static struct sys_device device_timer = {
840 .id = 0,
841 .cls = &timekeeping_sysclass,
842};
843
844static int __init timekeeping_init_device(void)
845{
846 int error = sysdev_class_register(&timekeeping_sysclass);
847 if (!error)
848 error = sysdev_register(&device_timer);
849 return error;
850}
851
852device_initcall(timekeeping_init_device);
853
854/*
855 * update_wall_time - Uses the current clocksource to increment the wall time
856 *
857 * Called from the timer interrupt, must hold a write on xtime_lock.
858 */
859static void update_wall_time(void)
860{
john stultz5eb6d202006-06-26 00:25:07 -0700861 static s64 remainder_snsecs, error;
862 s64 snsecs_per_sec;
john stultzad596172006-06-26 00:25:06 -0700863 cycle_t now, offset;
864
john stultz5eb6d202006-06-26 00:25:07 -0700865 snsecs_per_sec = (s64)NSEC_PER_SEC << clock->shift;
866 remainder_snsecs += (s64)xtime.tv_nsec << clock->shift;
867
john stultzad596172006-06-26 00:25:06 -0700868 now = read_clocksource(clock);
869 offset = (now - last_clock_cycle)&clock->mask;
870
871 /* normally this loop will run just once, however in the
872 * case of lost or late ticks, it will accumulate correctly.
873 */
874 while (offset > clock->interval_cycles) {
john stultz5eb6d202006-06-26 00:25:07 -0700875 /* get the ntp interval in clock shifted nanoseconds */
876 s64 ntp_snsecs = current_tick_length(clock->shift);
877
john stultzad596172006-06-26 00:25:06 -0700878 /* accumulate one interval */
john stultz5eb6d202006-06-26 00:25:07 -0700879 remainder_snsecs += clock->interval_snsecs;
john stultzad596172006-06-26 00:25:06 -0700880 last_clock_cycle += clock->interval_cycles;
881 offset -= clock->interval_cycles;
882
john stultz5eb6d202006-06-26 00:25:07 -0700883 /* interpolator bits */
884 time_interpolator_update(clock->interval_snsecs
885 >> clock->shift);
886 /* increment the NTP state machine */
887 update_ntp_one_tick();
888
889 /* accumulate error between NTP and clock interval */
890 error += (ntp_snsecs - (s64)clock->interval_snsecs);
891
892 /* correct the clock when NTP error is too big */
893 remainder_snsecs += make_ntp_adj(clock, offset, &error);
894
895 if (remainder_snsecs >= snsecs_per_sec) {
896 remainder_snsecs -= snsecs_per_sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 xtime.tv_sec++;
898 second_overflow();
899 }
john stultzad596172006-06-26 00:25:06 -0700900 }
john stultz5eb6d202006-06-26 00:25:07 -0700901 /* store full nanoseconds into xtime */
902 xtime.tv_nsec = remainder_snsecs >> clock->shift;
903 remainder_snsecs -= (s64)xtime.tv_nsec << clock->shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906/*
907 * Called from the timer interrupt handler to charge one tick to the current
908 * process. user_tick is 1 if the tick is user time, 0 for system.
909 */
910void update_process_times(int user_tick)
911{
912 struct task_struct *p = current;
913 int cpu = smp_processor_id();
914
915 /* Note: this timer irq context must be accounted for as well. */
916 if (user_tick)
917 account_user_time(p, jiffies_to_cputime(1));
918 else
919 account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
920 run_local_timers();
921 if (rcu_pending(cpu))
922 rcu_check_callbacks(cpu, user_tick);
923 scheduler_tick();
924 run_posix_cpu_timers(p);
925}
926
927/*
928 * Nr of active tasks - counted in fixed-point numbers
929 */
930static unsigned long count_active_tasks(void)
931{
Jack Steinerdb1b1fe2006-03-31 02:31:21 -0800932 return nr_active() * FIXED_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
935/*
936 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
937 * imply that avenrun[] is the standard name for this kind of thing.
938 * Nothing else seems to be standardized: the fractional size etc
939 * all seem to differ on different machines.
940 *
941 * Requires xtime_lock to access.
942 */
943unsigned long avenrun[3];
944
945EXPORT_SYMBOL(avenrun);
946
947/*
948 * calc_load - given tick count, update the avenrun load estimates.
949 * This is called while holding a write_lock on xtime_lock.
950 */
951static inline void calc_load(unsigned long ticks)
952{
953 unsigned long active_tasks; /* fixed-point */
954 static int count = LOAD_FREQ;
955
956 count -= ticks;
957 if (count < 0) {
958 count += LOAD_FREQ;
959 active_tasks = count_active_tasks();
960 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
961 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
962 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
963 }
964}
965
966/* jiffies at the most recent update of wall time */
967unsigned long wall_jiffies = INITIAL_JIFFIES;
968
969/*
970 * This read-write spinlock protects us from races in SMP while
971 * playing with xtime and avenrun.
972 */
973#ifndef ARCH_HAVE_XTIME_LOCK
974seqlock_t xtime_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
975
976EXPORT_SYMBOL(xtime_lock);
977#endif
978
979/*
980 * This function runs timers and the timer-tq in bottom half context.
981 */
982static void run_timer_softirq(struct softirq_action *h)
983{
Jan Beulicha4a61982006-03-24 03:15:54 -0800984 tvec_base_t *base = __get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800986 hrtimer_run_queues();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (time_after_eq(jiffies, base->timer_jiffies))
988 __run_timers(base);
989}
990
991/*
992 * Called by the local, per-CPU timer interrupt on SMP.
993 */
994void run_local_timers(void)
995{
996 raise_softirq(TIMER_SOFTIRQ);
Ingo Molnar6687a972006-03-24 03:18:41 -0800997 softlockup_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
1000/*
1001 * Called by the timer interrupt. xtime_lock must already be taken
1002 * by the timer IRQ!
1003 */
1004static inline void update_times(void)
1005{
1006 unsigned long ticks;
1007
1008 ticks = jiffies - wall_jiffies;
john stultzad596172006-06-26 00:25:06 -07001009 wall_jiffies += ticks;
1010 update_wall_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 calc_load(ticks);
1012}
1013
1014/*
1015 * The 64-bit jiffies value is not atomic - you MUST NOT read it
1016 * without sampling the sequence number in xtime_lock.
1017 * jiffies is defined in the linker script...
1018 */
1019
1020void do_timer(struct pt_regs *regs)
1021{
1022 jiffies_64++;
Atsushi Nemoto5aee4052006-03-06 15:42:51 -08001023 /* prevent loading jiffies before storing new jiffies_64 value. */
1024 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 update_times();
1026}
1027
1028#ifdef __ARCH_WANT_SYS_ALARM
1029
1030/*
1031 * For backwards compatibility? This can be done in libc so Alpha
1032 * and all newer ports shouldn't need it.
1033 */
1034asmlinkage unsigned long sys_alarm(unsigned int seconds)
1035{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -08001036 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039#endif
1040
1041#ifndef __alpha__
1042
1043/*
1044 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
1045 * should be moved into arch/i386 instead?
1046 */
1047
1048/**
1049 * sys_getpid - return the thread group id of the current process
1050 *
1051 * Note, despite the name, this returns the tgid not the pid. The tgid and
1052 * the pid are identical unless CLONE_THREAD was specified on clone() in
1053 * which case the tgid is the same in all threads of the same group.
1054 *
1055 * This is SMP safe as current->tgid does not change.
1056 */
1057asmlinkage long sys_getpid(void)
1058{
1059 return current->tgid;
1060}
1061
1062/*
1063 * Accessing ->group_leader->real_parent is not SMP-safe, it could
1064 * change from under us. However, rather than getting any lock
1065 * we can use an optimistic algorithm: get the parent
1066 * pid, and go back and check that the parent is still
1067 * the same. If it has changed (which is extremely unlikely
1068 * indeed), we just try again..
1069 *
1070 * NOTE! This depends on the fact that even if we _do_
1071 * get an old value of "parent", we can happily dereference
1072 * the pointer (it was and remains a dereferencable kernel pointer
1073 * no matter what): we just can't necessarily trust the result
1074 * until we know that the parent pointer is valid.
1075 *
1076 * NOTE2: ->group_leader never changes from under us.
1077 */
1078asmlinkage long sys_getppid(void)
1079{
1080 int pid;
1081 struct task_struct *me = current;
1082 struct task_struct *parent;
1083
1084 parent = me->group_leader->real_parent;
1085 for (;;) {
1086 pid = parent->tgid;
David Meybohm4c5640c2005-08-22 13:11:08 -07001087#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 struct task_struct *old = parent;
1090
1091 /*
1092 * Make sure we read the pid before re-reading the
1093 * parent pointer:
1094 */
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001095 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 parent = me->group_leader->real_parent;
1097 if (old != parent)
1098 continue;
1099}
1100#endif
1101 break;
1102 }
1103 return pid;
1104}
1105
1106asmlinkage long sys_getuid(void)
1107{
1108 /* Only we change this so SMP safe */
1109 return current->uid;
1110}
1111
1112asmlinkage long sys_geteuid(void)
1113{
1114 /* Only we change this so SMP safe */
1115 return current->euid;
1116}
1117
1118asmlinkage long sys_getgid(void)
1119{
1120 /* Only we change this so SMP safe */
1121 return current->gid;
1122}
1123
1124asmlinkage long sys_getegid(void)
1125{
1126 /* Only we change this so SMP safe */
1127 return current->egid;
1128}
1129
1130#endif
1131
1132static void process_timeout(unsigned long __data)
1133{
1134 wake_up_process((task_t *)__data);
1135}
1136
1137/**
1138 * schedule_timeout - sleep until timeout
1139 * @timeout: timeout value in jiffies
1140 *
1141 * Make the current task sleep until @timeout jiffies have
1142 * elapsed. The routine will return immediately unless
1143 * the current task state has been set (see set_current_state()).
1144 *
1145 * You can set the task state as follows -
1146 *
1147 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1148 * pass before the routine returns. The routine will return 0
1149 *
1150 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1151 * delivered to the current task. In this case the remaining time
1152 * in jiffies will be returned, or 0 if the timer expired in time
1153 *
1154 * The current task state is guaranteed to be TASK_RUNNING when this
1155 * routine returns.
1156 *
1157 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1158 * the CPU away without a bound on the timeout. In this case the return
1159 * value will be %MAX_SCHEDULE_TIMEOUT.
1160 *
1161 * In all cases the return value is guaranteed to be non-negative.
1162 */
1163fastcall signed long __sched schedule_timeout(signed long timeout)
1164{
1165 struct timer_list timer;
1166 unsigned long expire;
1167
1168 switch (timeout)
1169 {
1170 case MAX_SCHEDULE_TIMEOUT:
1171 /*
1172 * These two special cases are useful to be comfortable
1173 * in the caller. Nothing more. We could take
1174 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1175 * but I' d like to return a valid offset (>=0) to allow
1176 * the caller to do everything it want with the retval.
1177 */
1178 schedule();
1179 goto out;
1180 default:
1181 /*
1182 * Another bit of PARANOID. Note that the retval will be
1183 * 0 since no piece of kernel is supposed to do a check
1184 * for a negative retval of schedule_timeout() (since it
1185 * should never happens anyway). You just have the printk()
1186 * that will tell you if something is gone wrong and where.
1187 */
1188 if (timeout < 0)
1189 {
1190 printk(KERN_ERR "schedule_timeout: wrong timeout "
Andrew Mortona5a0d522005-10-30 15:01:42 -08001191 "value %lx from %p\n", timeout,
1192 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 current->state = TASK_RUNNING;
1194 goto out;
1195 }
1196 }
1197
1198 expire = timeout + jiffies;
1199
Oleg Nesterova8db2db2005-10-30 15:01:38 -08001200 setup_timer(&timer, process_timeout, (unsigned long)current);
1201 __mod_timer(&timer, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 schedule();
1203 del_singleshot_timer_sync(&timer);
1204
1205 timeout = expire - jiffies;
1206
1207 out:
1208 return timeout < 0 ? 0 : timeout;
1209}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210EXPORT_SYMBOL(schedule_timeout);
1211
Andrew Morton8a1c1752005-09-13 01:25:15 -07001212/*
1213 * We can use __set_current_state() here because schedule_timeout() calls
1214 * schedule() unconditionally.
1215 */
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001216signed long __sched schedule_timeout_interruptible(signed long timeout)
1217{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001218 __set_current_state(TASK_INTERRUPTIBLE);
1219 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001220}
1221EXPORT_SYMBOL(schedule_timeout_interruptible);
1222
1223signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1224{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001225 __set_current_state(TASK_UNINTERRUPTIBLE);
1226 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001227}
1228EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230/* Thread ID - the internal kernel "pid" */
1231asmlinkage long sys_gettid(void)
1232{
1233 return current->pid;
1234}
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236/*
1237 * sys_sysinfo - fill in sysinfo struct
1238 */
1239asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1240{
1241 struct sysinfo val;
1242 unsigned long mem_total, sav_total;
1243 unsigned int mem_unit, bitcount;
1244 unsigned long seq;
1245
1246 memset((char *)&val, 0, sizeof(struct sysinfo));
1247
1248 do {
1249 struct timespec tp;
1250 seq = read_seqbegin(&xtime_lock);
1251
1252 /*
1253 * This is annoying. The below is the same thing
1254 * posix_get_clock_monotonic() does, but it wants to
1255 * take the lock which we want to cover the loads stuff
1256 * too.
1257 */
1258
1259 getnstimeofday(&tp);
1260 tp.tv_sec += wall_to_monotonic.tv_sec;
1261 tp.tv_nsec += wall_to_monotonic.tv_nsec;
1262 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1263 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1264 tp.tv_sec++;
1265 }
1266 val.uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1267
1268 val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1269 val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1270 val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
1271
1272 val.procs = nr_threads;
1273 } while (read_seqretry(&xtime_lock, seq));
1274
1275 si_meminfo(&val);
1276 si_swapinfo(&val);
1277
1278 /*
1279 * If the sum of all the available memory (i.e. ram + swap)
1280 * is less than can be stored in a 32 bit unsigned long then
1281 * we can be binary compatible with 2.2.x kernels. If not,
1282 * well, in that case 2.2.x was broken anyways...
1283 *
1284 * -Erik Andersen <andersee@debian.org>
1285 */
1286
1287 mem_total = val.totalram + val.totalswap;
1288 if (mem_total < val.totalram || mem_total < val.totalswap)
1289 goto out;
1290 bitcount = 0;
1291 mem_unit = val.mem_unit;
1292 while (mem_unit > 1) {
1293 bitcount++;
1294 mem_unit >>= 1;
1295 sav_total = mem_total;
1296 mem_total <<= 1;
1297 if (mem_total < sav_total)
1298 goto out;
1299 }
1300
1301 /*
1302 * If mem_total did not overflow, multiply all memory values by
1303 * val.mem_unit and set it to 1. This leaves things compatible
1304 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1305 * kernels...
1306 */
1307
1308 val.mem_unit = 1;
1309 val.totalram <<= bitcount;
1310 val.freeram <<= bitcount;
1311 val.sharedram <<= bitcount;
1312 val.bufferram <<= bitcount;
1313 val.totalswap <<= bitcount;
1314 val.freeswap <<= bitcount;
1315 val.totalhigh <<= bitcount;
1316 val.freehigh <<= bitcount;
1317
1318 out:
1319 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1320 return -EFAULT;
1321
1322 return 0;
1323}
1324
Jan Beulicha4a61982006-03-24 03:15:54 -08001325static int __devinit init_timers_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 int j;
1328 tvec_base_t *base;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001329 static char __devinitdata tvec_base_done[NR_CPUS];
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001330
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001331 if (!tvec_base_done[cpu]) {
Jan Beulicha4a61982006-03-24 03:15:54 -08001332 static char boot_done;
1333
Jan Beulicha4a61982006-03-24 03:15:54 -08001334 if (boot_done) {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001335 /*
1336 * The APs use this path later in boot
1337 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001338 base = kmalloc_node(sizeof(*base), GFP_KERNEL,
1339 cpu_to_node(cpu));
1340 if (!base)
1341 return -ENOMEM;
1342 memset(base, 0, sizeof(*base));
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001343 per_cpu(tvec_bases, cpu) = base;
Jan Beulicha4a61982006-03-24 03:15:54 -08001344 } else {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001345 /*
1346 * This is for the boot CPU - we use compile-time
1347 * static initialisation because per-cpu memory isn't
1348 * ready yet and because the memory allocators are not
1349 * initialised either.
1350 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001351 boot_done = 1;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001352 base = &boot_tvec_bases;
Jan Beulicha4a61982006-03-24 03:15:54 -08001353 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001354 tvec_base_done[cpu] = 1;
1355 } else {
1356 base = per_cpu(tvec_bases, cpu);
Jan Beulicha4a61982006-03-24 03:15:54 -08001357 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001358
Oleg Nesterov3691c512006-03-31 02:30:30 -08001359 spin_lock_init(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 for (j = 0; j < TVN_SIZE; j++) {
1361 INIT_LIST_HEAD(base->tv5.vec + j);
1362 INIT_LIST_HEAD(base->tv4.vec + j);
1363 INIT_LIST_HEAD(base->tv3.vec + j);
1364 INIT_LIST_HEAD(base->tv2.vec + j);
1365 }
1366 for (j = 0; j < TVR_SIZE; j++)
1367 INIT_LIST_HEAD(base->tv1.vec + j);
1368
1369 base->timer_jiffies = jiffies;
Jan Beulicha4a61982006-03-24 03:15:54 -08001370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
1373#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001374static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 struct timer_list *timer;
1377
1378 while (!list_empty(head)) {
1379 timer = list_entry(head->next, struct timer_list, entry);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001380 detach_timer(timer, 0);
Oleg Nesterov3691c512006-03-31 02:30:30 -08001381 timer->base = new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 internal_add_timer(new_base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386static void __devinit migrate_timers(int cpu)
1387{
1388 tvec_base_t *old_base;
1389 tvec_base_t *new_base;
1390 int i;
1391
1392 BUG_ON(cpu_online(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001393 old_base = per_cpu(tvec_bases, cpu);
1394 new_base = get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 local_irq_disable();
Oleg Nesterov3691c512006-03-31 02:30:30 -08001397 spin_lock(&new_base->lock);
1398 spin_lock(&old_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Oleg Nesterov3691c512006-03-31 02:30:30 -08001400 BUG_ON(old_base->running_timer);
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 for (i = 0; i < TVR_SIZE; i++)
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001403 migrate_timer_list(new_base, old_base->tv1.vec + i);
1404 for (i = 0; i < TVN_SIZE; i++) {
1405 migrate_timer_list(new_base, old_base->tv2.vec + i);
1406 migrate_timer_list(new_base, old_base->tv3.vec + i);
1407 migrate_timer_list(new_base, old_base->tv4.vec + i);
1408 migrate_timer_list(new_base, old_base->tv5.vec + i);
1409 }
1410
Oleg Nesterov3691c512006-03-31 02:30:30 -08001411 spin_unlock(&old_base->lock);
1412 spin_unlock(&new_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 local_irq_enable();
1414 put_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416#endif /* CONFIG_HOTPLUG_CPU */
1417
Chandra Seetharaman83d722f2006-04-24 19:35:21 -07001418static int timer_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 unsigned long action, void *hcpu)
1420{
1421 long cpu = (long)hcpu;
1422 switch(action) {
1423 case CPU_UP_PREPARE:
Jan Beulicha4a61982006-03-24 03:15:54 -08001424 if (init_timers_cpu(cpu) < 0)
1425 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 break;
1427#ifdef CONFIG_HOTPLUG_CPU
1428 case CPU_DEAD:
1429 migrate_timers(cpu);
1430 break;
1431#endif
1432 default:
1433 break;
1434 }
1435 return NOTIFY_OK;
1436}
1437
Chandra Seetharaman649bbaa2006-04-24 19:35:15 -07001438static struct notifier_block timers_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 .notifier_call = timer_cpu_notify,
1440};
1441
1442
1443void __init init_timers(void)
1444{
1445 timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1446 (void *)(long)smp_processor_id());
1447 register_cpu_notifier(&timers_nb);
1448 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1449}
1450
1451#ifdef CONFIG_TIME_INTERPOLATION
1452
Christoph Lameter67890d72006-03-16 23:04:00 -08001453struct time_interpolator *time_interpolator __read_mostly;
1454static struct time_interpolator *time_interpolator_list __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455static DEFINE_SPINLOCK(time_interpolator_lock);
1456
1457static inline u64 time_interpolator_get_cycles(unsigned int src)
1458{
1459 unsigned long (*x)(void);
1460
1461 switch (src)
1462 {
1463 case TIME_SOURCE_FUNCTION:
1464 x = time_interpolator->addr;
1465 return x();
1466
1467 case TIME_SOURCE_MMIO64 :
Christoph Lameter685db652006-03-02 02:54:35 -08001468 return readq_relaxed((void __iomem *)time_interpolator->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 case TIME_SOURCE_MMIO32 :
Christoph Lameter685db652006-03-02 02:54:35 -08001471 return readl_relaxed((void __iomem *)time_interpolator->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 default: return get_cycles();
1474 }
1475}
1476
Alex Williamson486d46ae2005-09-06 15:17:04 -07001477static inline u64 time_interpolator_get_counter(int writelock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 unsigned int src = time_interpolator->source;
1480
1481 if (time_interpolator->jitter)
1482 {
1483 u64 lcycle;
1484 u64 now;
1485
1486 do {
1487 lcycle = time_interpolator->last_cycle;
1488 now = time_interpolator_get_cycles(src);
1489 if (lcycle && time_after(lcycle, now))
1490 return lcycle;
Alex Williamson486d46ae2005-09-06 15:17:04 -07001491
1492 /* When holding the xtime write lock, there's no need
1493 * to add the overhead of the cmpxchg. Readers are
1494 * force to retry until the write lock is released.
1495 */
1496 if (writelock) {
1497 time_interpolator->last_cycle = now;
1498 return now;
1499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 /* Keep track of the last timer value returned. The use of cmpxchg here
1501 * will cause contention in an SMP environment.
1502 */
1503 } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
1504 return now;
1505 }
1506 else
1507 return time_interpolator_get_cycles(src);
1508}
1509
1510void time_interpolator_reset(void)
1511{
1512 time_interpolator->offset = 0;
Alex Williamson486d46ae2005-09-06 15:17:04 -07001513 time_interpolator->last_counter = time_interpolator_get_counter(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
1516#define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
1517
1518unsigned long time_interpolator_get_offset(void)
1519{
1520 /* If we do not have a time interpolator set up then just return zero */
1521 if (!time_interpolator)
1522 return 0;
1523
1524 return time_interpolator->offset +
Alex Williamson486d46ae2005-09-06 15:17:04 -07001525 GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
1528#define INTERPOLATOR_ADJUST 65536
1529#define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
1530
1531static void time_interpolator_update(long delta_nsec)
1532{
1533 u64 counter;
1534 unsigned long offset;
1535
1536 /* If there is no time interpolator set up then do nothing */
1537 if (!time_interpolator)
1538 return;
1539
Andrew Mortona5a0d522005-10-30 15:01:42 -08001540 /*
1541 * The interpolator compensates for late ticks by accumulating the late
1542 * time in time_interpolator->offset. A tick earlier than expected will
1543 * lead to a reset of the offset and a corresponding jump of the clock
1544 * forward. Again this only works if the interpolator clock is running
1545 * slightly slower than the regular clock and the tuning logic insures
1546 * that.
1547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Alex Williamson486d46ae2005-09-06 15:17:04 -07001549 counter = time_interpolator_get_counter(1);
Andrew Mortona5a0d522005-10-30 15:01:42 -08001550 offset = time_interpolator->offset +
1551 GET_TI_NSECS(counter, time_interpolator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
1554 time_interpolator->offset = offset - delta_nsec;
1555 else {
1556 time_interpolator->skips++;
1557 time_interpolator->ns_skipped += delta_nsec - offset;
1558 time_interpolator->offset = 0;
1559 }
1560 time_interpolator->last_counter = counter;
1561
1562 /* Tuning logic for time interpolator invoked every minute or so.
1563 * Decrease interpolator clock speed if no skips occurred and an offset is carried.
1564 * Increase interpolator clock speed if we skip too much time.
1565 */
1566 if (jiffies % INTERPOLATOR_ADJUST == 0)
1567 {
Jordan Hargraveb20367a2006-04-07 19:50:18 +02001568 if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 time_interpolator->nsec_per_cyc--;
1570 if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
1571 time_interpolator->nsec_per_cyc++;
1572 time_interpolator->skips = 0;
1573 time_interpolator->ns_skipped = 0;
1574 }
1575}
1576
1577static inline int
1578is_better_time_interpolator(struct time_interpolator *new)
1579{
1580 if (!time_interpolator)
1581 return 1;
1582 return new->frequency > 2*time_interpolator->frequency ||
1583 (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
1584}
1585
1586void
1587register_time_interpolator(struct time_interpolator *ti)
1588{
1589 unsigned long flags;
1590
1591 /* Sanity check */
Eric Sesterhenn9f312522006-04-02 13:45:55 +02001592 BUG_ON(ti->frequency == 0 || ti->mask == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
1595 spin_lock(&time_interpolator_lock);
1596 write_seqlock_irqsave(&xtime_lock, flags);
1597 if (is_better_time_interpolator(ti)) {
1598 time_interpolator = ti;
1599 time_interpolator_reset();
1600 }
1601 write_sequnlock_irqrestore(&xtime_lock, flags);
1602
1603 ti->next = time_interpolator_list;
1604 time_interpolator_list = ti;
1605 spin_unlock(&time_interpolator_lock);
1606}
1607
1608void
1609unregister_time_interpolator(struct time_interpolator *ti)
1610{
1611 struct time_interpolator *curr, **prev;
1612 unsigned long flags;
1613
1614 spin_lock(&time_interpolator_lock);
1615 prev = &time_interpolator_list;
1616 for (curr = *prev; curr; curr = curr->next) {
1617 if (curr == ti) {
1618 *prev = curr->next;
1619 break;
1620 }
1621 prev = &curr->next;
1622 }
1623
1624 write_seqlock_irqsave(&xtime_lock, flags);
1625 if (ti == time_interpolator) {
1626 /* we lost the best time-interpolator: */
1627 time_interpolator = NULL;
1628 /* find the next-best interpolator */
1629 for (curr = time_interpolator_list; curr; curr = curr->next)
1630 if (is_better_time_interpolator(curr))
1631 time_interpolator = curr;
1632 time_interpolator_reset();
1633 }
1634 write_sequnlock_irqrestore(&xtime_lock, flags);
1635 spin_unlock(&time_interpolator_lock);
1636}
1637#endif /* CONFIG_TIME_INTERPOLATION */
1638
1639/**
1640 * msleep - sleep safely even with waitqueue interruptions
1641 * @msecs: Time in milliseconds to sleep for
1642 */
1643void msleep(unsigned int msecs)
1644{
1645 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1646
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001647 while (timeout)
1648 timeout = schedule_timeout_uninterruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
1651EXPORT_SYMBOL(msleep);
1652
1653/**
Domen Puncer96ec3ef2005-06-25 14:58:43 -07001654 * msleep_interruptible - sleep waiting for signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 * @msecs: Time in milliseconds to sleep for
1656 */
1657unsigned long msleep_interruptible(unsigned int msecs)
1658{
1659 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1660
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001661 while (timeout && !signal_pending(current))
1662 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return jiffies_to_msecs(timeout);
1664}
1665
1666EXPORT_SYMBOL(msleep_interruptible);